dylanlott
6/25/2015 - 11:42 PM

gistfile1.txt

// ***** Customer View ********************************************************************/
<div class="row header">
	<img src="/assets/img/logo-light.png" alt="fashionphilelogo" class='logosmall col-xs-4' />
	<br>
	<h6 id='locationName'>
		<span class='locationName'> {{customerLocation.name}}  </span>
	</h6>
</div>


	<h1 class='text-center'> Welcome</h1>
	<h3 class='text-center'>Please check-in</h3>

<div class="row">
	<p ng-repeat="customer in customers"></p>
</div>

<div class="row">
<form class='col-xs-6 col-xs-offset-3' ng-submit="addCustomer(customer);join(customer)">
	<div class="form-group requireMe">

		<input type="text" ng-model="customer.name" class='form-control' placeholder="name">
		
	</div>

	<div class="form-group">
		<input type="email" class='form-control' placeholder='email' ng-model="customer.email">
		<toaster-container></toaster-container>
	</div>
	<button class="btn btn-default col-xs-12">Submit</button>
</form>
</div>

<div class="row ipadFooter">
 <span class='navLocation'></span> <img src="/assets/img/logo-light.png" alt="fashionphilelogo" class='logosmall text-center'> @ {{customerLocation.name}}
</div>


//******************* CUSTOMER CTRL ********************************************************************/
var app = angular.module('fashionphile');

app.controller('CustomerCtrl', function($rootScope, $scope, $stateParams, $state, CustomerService, socket, customerLocation, toaster){

  // var socket = io.connect();

  $scope.customerLocation = customerLocation;
  console.log(customerLocation); 

  $scope.addCustomer = function(customer, location){
    var location = $state.params.id;
    CustomerService.addCustomer(customer, location);
    toaster.pop('success', 'You are now in the queue. We\'ll be with you shortly.')
    $scope.customer = {};
  }

  $scope.currentTime = Date.now();

  //socket tests
  $scope.newCustomers = [];

  $scope.join = function(customer) {
    socket.emit('addCustomer', customer);
  };

  socket.on('notification', function(data) {
    $scope.$apply(function () {
      $scope.newCustomers.push(data.customer);
    });
  });

  //end of socket tests 

}) 


/*   SOCKET FACTORY  ******************************************************************** / 
var app = angular.module('fashionphile')

app.factory('socket', ['$rootScope', function($rootScope) {
  var socket = io.connect();

  return {
    on: function('addCustomer'){
      socket.on(addCustomer, function(customer){
        console.log("Socket Factory - customer added", customer)
      });
    },
    emit: function(addCustomer, data) {
      socket.emit(addCustomer, data);
    }
  };
}]);

/* APP.JS for ANGULAR *********************************/
(function(){
  'use strict';

var app = angular.module('fashionphile', [ 'ui.router', 'weekly.barchart', 'editer', 'ui.tree', 'toaster', 'sliderDir', 'ui.bootstrap']);

//config
app
  // .run(function($state,$rootScope){
  //   $rootScope.$state = $state;
  // })

  .config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.when('', '/');
    $urlRouterProvider.otherwise('/');
    $stateProvider
      .state('login', {
          url: '/',
          templateUrl : 'app/login/loginView.html',
          controller  : 'LoginCtrl'
      })
      .state('selection', {
          url: '/selection',
          templateUrl : 'app/selection/selectionView.html',
          controller  : 'SelectionCtrl',
          resolve: {
            locations: function($q, SelectionService) {
              var deferred = $q.defer();
                  SelectionService.getLocations()
                    .then(function(locations) {
                      deferred.resolve(locations);
                  });
                  return deferred.promise;
              }
            }
      })
      .state('customer', {
          url: '/customer/:id',
          templateUrl : 'app/customer/customerView.html',
          controller  : 'CustomerCtrl',
          resolve: {
            customerLocation: function($q, $state, $stateParams, SelectionService) {
              var nameParam = $stateParams.id; 
              var deferred = $q.defer();
                  SelectionService.getLocationByParam(nameParam)
                    .then(function(location) {
                      var currentLocation = location[0];
                      deferred.resolve(currentLocation);
                  });
                  return deferred.promise;
              }
          }
      })
      .state('employee', {
          url: '/employee/:location',
          templateUrl : 'app/employee/employeeView.html',
          controller  : 'EmployeeCtrl',
          resolve: {
            customers: function($state, $stateParams, CustomerService, $q){
              var location = $stateParams.location; 
              var dfd = $q.defer(); 
                CustomerService.getCustomers(location)
                  .then(function(customers){
                    dfd.resolve(customers); 
                  }); 
                return dfd.promise; 
              },
            customerLocation: function($q, $state, $stateParams, SelectionService) {
              var nameParam = $stateParams.location; 
              console.log('nameparam', nameParam)
              var deferred = $q.defer();
                SelectionService.getLocationByParam(nameParam)
                  .then(function(location) {
                    var currentLocation = location[0];
                    deferred.resolve(currentLocation);
                });
                return deferred.promise;
            }
          }
      })
      .state('walldisplay', {
          url: '/walldisplay/:location',
          templateUrl : 'app/wallDisplay/wallDisplayView.html',
          controller  : 'WallDisplayCtrl',
          resolve: {
            customers: function($state, $stateParams, CustomerService, $q){
              var location = $stateParams.location; 
              var dfd = $q.defer(); 
                CustomerService.getCustomers(location)
                  .then(function(customers){
                    dfd.resolve(customers); 
                  }); 
                return dfd.promise; 
              }
          }
      })
      .state('admin', {
          url: '/admin',
          templateUrl : 'app/admin/adminView.html',
          controller  : 'adminCtrl',
          resolve: {
            adminStats: function(adminService, $q){
              var dfd = $q.defer();
                adminService.getStats()
                .then(function(adminStats){
                  dfd.resolve(adminStats[0]);
                });
              return dfd.promise;
            },
            locations: function($q, SelectionService) {
              var deferred = $q.defer();
                  SelectionService.getLocations()
                    .then(function(locations) {
                      deferred.resolve(locations);
                    });
                  return deferred.promise;
              }
          }
      })

      .state('dashboard', {
          url: '/dashboard/:id',
          templateUrl : 'app/dashboard/dashboardView.html',
          controller  : 'dashboardCtrl',
          resolve: {
            stats: function(adminService, $q, $stateParams){
              var location = $stateParams.id; 
              var dfd = $q.defer();
                adminService.getStatsByLocation(location)
                .then(function(stats){
                  console.log("dashboard routers stats.data is", stats.data); 
                  dfd.resolve(stats.data[0]);
                });
              return dfd.promise;
            },
            customerLocation: function($q, $state, $stateParams, SelectionService) {
              var nameParam = $stateParams.id; 
              console.log('nameparam', nameParam)
              var deferred = $q.defer();
                SelectionService.getLocationByParam(nameParam)
                  .then(function(location) {
                    var currentLocation = location[0];
                    deferred.resolve(currentLocation);
                });
                return deferred.promise;
            }
          }
      }); 
  });

})();

/* SERVER.JS *****************************************************/ 
//definititions and requires
var bodyParser = require('body-parser');
var cors = require('cors');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var session = require('express-session');
var mongoose = require('mongoose');
var cookieParser = require('cookie-parser');
var bson = require('bson');

//declare socket
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);

//Middleware
app.use(cors());
app.use(bodyParser.json());
app.use(express.static(__dirname+"/public"));
app.use(cookieParser());
app.use(session({
    secret: 'wolverinePack', 
    resave: false,
    saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());

//Controllers 
var QueueCtrl = require('./controllers/QueueCtrl');
var UserCtrl = require('./controllers/UserCtrl');
var LocationCtrl = require('./controllers/LocationCtrl');
var StatsCtrl = require('./controllers/StatsCtrl'); 

//Models
var User = require('./models/User');
var Location = require('./models/Location');
var Customer = require('./models/Customer');

//Database
var mongoUri = "mongodb://localhost:27017/fashionphile";
mongoose.connect(mongoUri);
mongoose.connection.once('open', function() {
    console.log("Connected to db at " + mongoUri);
});

//Port
var port = 8080; 
app.listen(process.env.EXPRESS_PORT || port, function(){
    console.log("App Express server up and running on port ", port); 
});

//static 
app.use(express.static(__dirname+'/public'));

//local login
passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
}, function(username, password, done) {
    console.log(username, password)
    User.findOne({ email: username }).exec().then(function(user) {
        if (!user) {
            return done(null, false);
            console.log('no user');
        }
        user.comparePassword(password).then(function(isMatch) {
            if (!isMatch) {
                console.log('no match');
                return done(null, false);
            }
            return done(null, user);
        });
    });
}));

//authorization check
var requireAuth = function(req, res, next) {
    if (!req.isAuthenticated()) {
        return res.status(403).send({message: "Logged In"   }).end();
    }
    return next();
}

//deserializer
passport.serializeUser(function(user, done) {
  done(null, user._id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function (err, user) {
    done(err, user);
  });
});










/* SOCKET TESTS ****************************************************** 
**********************************************************************/
io.on('connection', function(socket) {
  console.log('new connection', socket);

  socket.on('addCustomer', function(customer) {
    io.emit('notification', {
      message: 'new customer',
      customer: customer
    });
  });
});

server.listen(4041, function() {
  console.log('Socket.IO server up and running on port 4040');
});

/* end of socket tests 
*********************************************************************/











/* Endpoints 
**********************************************************************/
//Auth
app.post('/api/users/', UserCtrl.createUser);
app.post('/api/users/auth', passport.authenticate('local'), function(req, res) {
    console.log("Logged In"); 
    return res.status(200).end();
});

app.get('/api/location', requireAuth, LocationCtrl.list);
app.get('/api/store/:id', requireAuth, LocationCtrl.listOne);
app.get('/api/name/:name', requireAuth, LocationCtrl.listName);
app.post('/api/location', requireAuth, LocationCtrl.create);
app.delete('/api/location/:id', requireAuth, LocationCtrl.delete);
app.put('/api/location/:id', requireAuth, LocationCtrl.update);

app.get('/api/employee/:id', QueueCtrl.getByLocation)

app.post('/api/customer/', QueueCtrl.add);
app.put('/api/customer/:id', QueueCtrl.helpCustomer); 

app.get('/api/stats', StatsCtrl.getStats); 
app.get('/api/stats/:id', StatsCtrl.getStatsByLocation);