<commit_before>var path = require('path');
var url = require('url');
var express = require('express');
var cons = require('consolidate');
var cookieParser = require('cookie-parser');
var knexConfig = require('../knexfile');
var knex = require('knex')(knexConfig.development);
var bookshelf = require('bookshelf')(knex);
var bodyParser = require('body-parser');
var _ = require('lodash');
var BPromise = require('bluebird');
var passport = require('passport');
var LdapStrategy = require('passport-ldapauth').Strategy;
var session = require('express-session');

var config = {};

config.ldap = require("./ldap.json");

var app = module.exports = express();

var globalConfig = {
    minify: process.env.MINIFY == 'yes' ? true : false,
    environment: process.env.ENVIRONMENT || 'local'
};

var rootPath = path.dirname(__dirname);
var port = Number(process.env.PORT || 9999);

passport.use(new LdapStrategy(config.ldap,
        function (profile, done) {
            return done(null, profile);
        }
));

app.use(session({
    secret: 'frecht',
    cookie: {
        maxAge: 60 * 1000 * 60 //1 hour
//        maxAge: 20 * 1000 //10 seconds, debug
    },
    resave: false,
    saveUninitialized: false
}));

app.use(passport.initialize());
app.use(passport.session());
app.use(function (req, res, next) {
    if ('HEAD' === req.method || 'OPTIONS' === req.method)
        return next();
    // break session hash / force express to spit out a new cookie once per second at most
    req.session._garbage = Date();
    req.session.touch();
    next();
});
app.use(function (req, res, next) {
    res.locals.login = req.isAuthenticated();
    next();
});

app.set('views', path.join(rootPath, 'server'));
app.engine('html', cons.handlebars);
app.set('view engine', 'html');
app.set('bookshelf', bookshelf);
var models = require('require-directory')(module, './models');

for (var modelName in models) {
    global[modelName] = models[modelName];
    app.set(modelName, models[modelName]);
}

if (globalConfig.environment == 'local') {
    app.use(require('connect-livereload')());
}

app.use(cookieParser());

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

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

passport.deserializeUser(function (obj, done) {
    done(null, obj);
});

var isLoggedIn = function (req, res, next) {
    if (req.user) {
        next();
    } else {
        res.send(401, {error: 'Unauthorized'});
    }
};

app.get('/test', isLoggedIn, function (req, res) {
    res.send('hello');
});

app.use(function (req, res, next) {
    var config = configFromReq(req);
    var parsedUrl = url.parse(req.url);
    var splittedPath = parsedUrl.pathname.split(path.sep);

    if (splittedPath[1]) {
        var fileExtension = getFileExtension(parsedUrl.pathname);
        if (fileExtension == 'js' || fileExtension == 'css') {
            addPathPrefix(splittedPath, getMinPrefix(config));
        }
    }

    parsedUrl.pathname = splittedPath.join(path.sep);
    req.url = url.format(parsedUrl);

    req.config = config;
    next();
});

//app.use('/', express.static(path.join(rootPath, 'dist/app')));
app.use(express.static('dist/app'));

app.get('/', function (req, res) {
    renderIndex(req.config, res);
});

app.use('/api/login', function (req, res, next) {
    return new User()
            .query({where: {username: req.body.username}})
            .fetch()
            .then(function (user) {
                if (!user)
                    return res.status(500).json({err: 'user not allowed'});
                else
                    next();
            });
});

app.post('/api/login',
        passport.authenticate('ldapauth', {failWithError: true}),
        function (req, res, next) {
            // handle success
            return res.json({
                status: "ok",
                id: req.user.id,
                username: req.user.sn,
                session: req.user
            });
        },
        function (err, req, res, next) {
            console.log(err);
            // handle error
            return res.status(500).json(err);
        });

app.post('/api/logout', isLoggedIn, function (req, res) {
    req.logout();
    res.send();
});

app.get('/api/users', isLoggedIn, function (req, res) {
    new User().fetchAll().then(function (users) {
        res.send(users);
    }).catch(function (error) {
        console.log(error.stack);
        res.send('Error getting Users');
    });
});

app.get('/api/meetings', isLoggedIn, function (req, res) {
    new Meeting().query(function (qb) {
        qb.orderBy('updated_at', 'desc');
        if (req.query.limit)
            qb.limit(req.query.limit);
    })
            .fetchAll({
                withRelated: ['participants']
            }).then(function (meetings) {
        res.send(meetings);
    }).catch(function (error) {
        console.log(error.stack);
        res.send('Error getting Meetings');
    });
});

app.get('/api/people', isLoggedIn, function (req, res) {
    var q = req.query.q || '';
    knex('people')
            .select()
            .where('surname', 'like', '%' + q + '%')
            .orWhere('institute', 'like', '%' + q + '%')
            .then(function (people) {
                res.send(people);
            }).catch(function (error) {
        console.log(error.stack);
        res.send('Error getting People');
    });
});

app.get('/api/platforms', isLoggedIn, function (req, res) {
    var q = req.query.q || '';
    knex('meetings')
            .distinct()
            .select('platform')
            .where('platform', 'like', '%' + q + '%')
            .then(function (platforms) {
                platforms = _.map(platforms, 'platform');
                res.send(platforms);
            }).catch(function (error) {
        console.log(error.stack);
        res.send('Error getting Platforms');
    });
});

app.post('/api/meetings', isLoggedIn, function (req, res) {
    var meetingFields = ['location', 'date', 'topics', 'platform'];
    var data = _.pick(req.body, meetingFields);
    var participants = req.body.participants;
    new Meeting(data).save()
            .then(function (meeting) {
                var meetingParticipants = [];
                if (participants)
                    meetingParticipants = _.map(participants, function (p) {
                        return {person_id: p.id, meeting_id: meeting.id};
                    });
                return BPromise.all([meeting, knex('meetings_people').insert(meetingParticipants)]);
            })
            .spread(function (meeting) {
                res.send(meeting);
            })
            .catch(function (error) {
                console.log(error.stack);
                res.send('Error creating Meeting');
            });
});

app.put('/api/meetings/:id', isLoggedIn, function (req, res) {
    var meetingFields = ['location', 'date', 'topics', 'platform'];
    var meetingId = req.params.id;
    var data = _.pick(req.body, meetingFields);
    var participants = req.body.participants;
    new Meeting({id: meetingId})
            .save(data)
            .then(function (meeting) {
                return meeting.participants().detach()
                        .then(function (m) {
                            var participantsIds = _.map(participants, 'id');
                            return meeting.participants().attach(participantsIds);
                        });
            })
            .then(function (meeting) {
                res.send(meeting);
            })
            .catch(function (error) {
                console.log(error.stack);
                res.send('Error creating Meeting');
            });
});

app.post('/api/people', isLoggedIn, function (req, res) {
    var meetingFields = ['surname', 'institute'];
    var data = _.pick(req.body, meetingFields);
    new Person(data).save().then(function (person) {
        res.send(person);
    }).catch(function (error) {
        console.log(error.stack);
        res.send('Error creating Person');
    });
});

app.delete('/api/meetings/:id', isLoggedIn, function (req, res) {
    var meetingId = req.params.id;
    new Meeting({id: meetingId})
            .destroy()
            .then(function () {
                res.send();
            });
});

app.listen(port, function () {
    console.log('Server listening on port ' + port);
});

function renderIndex(config, res) {
    res.render(getMinPrefix(config) + 'views/index');
}

function configFromReq(req) {
    var config = {};
    config.minify = req.cookies.minify == 'true' ? true : false;
    return config;
}

function getMinPrefix(conf) {
    return conf.minify || globalConfig.minify ? '' : '';
}

function addPathPrefix(filePath, prefix) {
    filePath.splice(1, 0, prefix);
}

function getFileExtension(filePath) {
    return filePath.split('.').pop();
}
<commit_msg>Refactor.<commit_after>var path = require('path');
var url = require('url');
var express = require('express');
var cons = require('consolidate');
var cookieParser = require('cookie-parser');
var knexConfig = require('../knexfile');
var knex = require('knex')(knexConfig.development);
var bookshelf = require('bookshelf')(knex);
var bodyParser = require('body-parser');
var _ = require('lodash');
var BPromise = require('bluebird');
var passport = require('passport');
var LdapStrategy = require('passport-ldapauth').Strategy;
var session = require('express-session');

var config = {};

config.ldap = require("./ldap.json");

var app = module.exports = express();

var globalConfig = {
    minify: process.env.MINIFY == 'yes' ? true : false,
    environment: process.env.ENVIRONMENT || 'local'
};

var rootPath = path.dirname(__dirname);
var port = Number(process.env.PORT || 9999);

passport.use(new LdapStrategy(config.ldap,
        function (profile, done) {
            return done(null, profile);
        }
));

app.use(session({
    secret: 'frecht',
    cookie: {
        maxAge: 60 * 1000 * 60 //1 hour
//        maxAge: 20 * 1000 //10 seconds, debug
    },
    resave: false,
    saveUninitialized: false
}));

app.use(passport.initialize());
app.use(passport.session());
app.use(function (req, res, next) {
    if ('HEAD' === req.method || 'OPTIONS' === req.method)
        return next();
    // break session hash / force express to spit out a new cookie once per second at most
    req.session._garbage = Date();
    req.session.touch();
    next();
});
app.use(function (req, res, next) {
    res.locals.login = req.isAuthenticated();
    next();
});

app.set('views', path.join(rootPath, 'server'));
app.engine('html', cons.handlebars);
app.set('view engine', 'html');
app.set('bookshelf', bookshelf);
var models = require('require-directory')(module, './models');

for (var modelName in models) {
    global[modelName] = models[modelName];
    app.set(modelName, models[modelName]);
}

if (globalConfig.environment == 'local') {
    app.use(require('connect-livereload')());
}

app.use(cookieParser());

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

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

passport.deserializeUser(function (obj, done) {
    done(null, obj);
});

var isLoggedIn = function (req, res, next) {
    if (req.user) {
        next();
    } else {
        res.send(401, {error: 'Unauthorized'});
    }
};

app.get('/test', isLoggedIn, function (req, res) {
    res.send('hello');
});

app.use(function (req, res, next) {
    var config = configFromReq(req);
    var parsedUrl = url.parse(req.url);
    var splittedPath = parsedUrl.pathname.split(path.sep);

    if (splittedPath[1]) {
        var fileExtension = getFileExtension(parsedUrl.pathname);
        if (fileExtension == 'js' || fileExtension == 'css') {
            addPathPrefix(splittedPath, getMinPrefix(config));
        }
    }

    parsedUrl.pathname = splittedPath.join(path.sep);
    req.url = url.format(parsedUrl);

    req.config = config;
    next();
});

//app.use('/', express.static(path.join(rootPath, 'dist/app')));
app.use(express.static('dist/app'));

app.get('/', function (req, res) {
    renderIndex(req.config, res);
});

app.use('/api/login', function (req, res, next) {
    return new User()
            .query({where: {username: req.body.username}})
            .fetch()
            .then(function (user) {
                if (!user)
                    return res.status(500).json({err: 'user not allowed'});
                else
                    next();
            });
});

app.post('/api/login',
        passport.authenticate('ldapauth', {failWithError: true}),
        function (req, res, next) {
            // handle success
            return res.json({
                status: "ok",
                id: req.user.id,
                username: req.user.sn,
                session: req.user
            });
        },
        function (err, req, res, next) {
            console.log(err);
            // handle error
            return res.status(500).json(err);
        });

app.post('/api/logout', isLoggedIn, function (req, res) {
    req.logout();
    res.send();
});

app.get('/api/users', isLoggedIn, function (req, res) {
    new User().fetchAll().then(function (users) {
        res.send(users);
    }).catch(function (error) {
        console.log(error.stack);
        res.send('Error getting Users');
    });
});

app.get('/api/meetings', isLoggedIn, function (req, res) {
    new Meeting().query(function (qb) {
        qb.orderBy('updated_at', 'desc');
        if (req.query.limit)
            qb.limit(req.query.limit);
    })
            .fetchAll({
                withRelated: ['participants']
            }).then(function (meetings) {
        res.send(meetings);
    }).catch(function (error) {
        console.log(error.stack);
        res.send('Error getting Meetings');
    });
});

app.get('/api/people', isLoggedIn, function (req, res) {
    var q = req.query.q || '';
    knex('people')
            .select()
            .where('surname', 'like', '%' + q + '%')
            .orWhere('institute', 'like', '%' + q + '%')
            .then(function (people) {
                res.send(people);
            }).catch(function (error) {
        console.log(error.stack);
        res.send('Error getting People');
    });
});

app.get('/api/platforms', isLoggedIn, function (req, res) {
    var q = req.query.q || '';
    knex('meetings')
            .distinct()
            .select('platform')
            .where('platform', 'like', '%' + q + '%')
            .then(function (platforms) {
                platforms = _.map(platforms, 'platform');
                res.send(platforms);
            }).catch(function (error) {
        console.log(error.stack);
        res.send('Error getting Platforms');
    });
});

app.post('/api/meetings', isLoggedIn, function (req, res) {
    var meetingFields = ['location', 'date', 'topics', 'platform'];
    var data = _.pick(req.body, meetingFields);
    var participants = req.body.participants;
    new Meeting(data).save()
            .then(function (meeting) {
                var participantsIds = _.map(participants, 'id');
                return meeting.participants().attach(participantsIds);
            })
            .then(function (meeting) {
                res.send(meeting);
            })
            .catch(function (error) {
                console.log(error.stack);
                res.send('Error creating Meeting');
            });
});

app.put('/api/meetings/:id', isLoggedIn, function (req, res) {
    var meetingFields = ['location', 'date', 'topics', 'platform'];
    var meetingId = req.params.id;
    var data = _.pick(req.body, meetingFields);
    var participants = req.body.participants;
    new Meeting({id: meetingId})
            .save(data)
            .then(function (meeting) {
                return meeting.participants().detach()
                        .then(function (m) {
                            var participantsIds = _.map(participants, 'id');
                            return meeting.participants().attach(participantsIds);
                        });
            })
            .then(function (meeting) {
                res.send(meeting);
            })
            .catch(function (error) {
                console.log(error.stack);
                res.send('Error creating Meeting');
            });
});

app.post('/api/people', isLoggedIn, function (req, res) {
    var meetingFields = ['surname', 'institute'];
    var data = _.pick(req.body, meetingFields);
    new Person(data).save().then(function (person) {
        res.send(person);
    }).catch(function (error) {
        console.log(error.stack);
        res.send('Error creating Person');
    });
});

app.delete('/api/meetings/:id', isLoggedIn, function (req, res) {
    var meetingId = req.params.id;
    new Meeting({id: meetingId})
            .destroy()
            .then(function () {
                res.send();
            });
});

app.listen(port, function () {
    console.log('Server listening on port ' + port);
});

function renderIndex(config, res) {
    res.render(getMinPrefix(config) + 'views/index');
}

function configFromReq(req) {
    var config = {};
    config.minify = req.cookies.minify == 'true' ? true : false;
    return config;
}

function getMinPrefix(conf) {
    return conf.minify || globalConfig.minify ? '' : '';
}

function addPathPrefix(filePath, prefix) {
    filePath.splice(1, 0, prefix);
}

function getFileExtension(filePath) {
    return filePath.split('.').pop();
}
<|endoftext|>


<commit_before>var dashboard = require('./mock-dashboard.js');
var wadi = require('./wadi.js');

var express = require('express');
var expressHandlebars  = require('express-handlebars');

// go find all the activity for wadi repo's

wadi.setDashboard(dashboard);

function trackWADIRepositories() {
  wadi.addEventsFromRepo('mozilla', 'oghliner');
  wadi.addEventsFromRepo('mozilla', 'platatus');
  wadi.addEventsFromRepo('mozilla', 'serviceworker-cookbook');
  wadi.addEventsFromRepo('mozilla', 'progressive-apps-hq');
  wadi.addEventsFromRepo('marco-c', 'wp-web-push');
  wadi.addEventsFromRepo('darkwing', 'wp-sw-cache');
  wadi.addEventsFromRepo('marco-c', 'web-push');
  wadi.addEventsFromRepo('marco-c', 'mercurius');
}

wadi.addBugsTrackedBy(1201717); // web app developer initiative tracker
wadi.addBugsTrackedBy(1059784); // Ship Service Workers on desktop on the release channel
wadi.addBugsTrackedBy(1207262); // Ship Service Workers on Android on the release channel
wadi.addBugsTrackedBy(1003097); // Worker debugging
wadi.addBugsTrackedBy(1156054); // Implement Push API on Android
wadi.addBugsTrackedBy(1201571); // Tracking bug for Push Notifications

gHomeScreenStats = {};

wadi.npmDownloads('oghliner', gHomeScreenStats);
wadi.getStargazers('marco-c', 'wp-web-push', 1, gHomeScreenStats);
wadi.getStargazers('mozilla', 'oghliner', 1, gHomeScreenStats);
wadi.getStargazers('mozilla', 'platatus', 1, gHomeScreenStats);
wadi.getStargazers('mozilla', 'serviceworker-cookbook', 1, gHomeScreenStats);

trackWADIRepositories();
setInterval(trackWADIRepositories, 300000);

var server = express();

server.engine('handlebars', expressHandlebars({defaultLayout: 'main'}));
server.set('view engine', 'handlebars');

server.get('/home', function (req, res) {
    console.log(JSON.stringify(gHomeScreenStats));
    res.render('home', {stats: gHomeScreenStats});
});

server.get('/bugs', function (req, res) {
  var bugDictionary = wadi.getAllBugInfo();
  var keys = Object.keys(bugDictionary);
  var bugArray = keys.map(function (k) { return bugDictionary[k]; });
  var sortedBugs = bugArray.sort(function(x, y) {
    if (x.latest < y.latest) {
      return 1;
    } else if (x.latest > y.latest) {
      return -1;
    } else {
      return 0;
    }
  });

  res.render('bugs', {bugs: sortedBugs});
});

server.get('/events', function (req, res) {
  var events = wadi.getAllEvents();
  var keys = Object.keys(wadi.getAllEvents());

  keys.sort();
  keys.reverse();

  var sortedEvents = keys.map(function (k) { return events[k]; } );

  res.render('events', {events: sortedEvents});
});

server.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});<commit_msg>refactor code, watch more stargazers<commit_after>var dashboard = require('./mock-dashboard.js');
var wadi = require('./wadi.js');

var express = require('express');
var expressHandlebars  = require('express-handlebars');

// go find all the activity for wadi repo's

var gRepositories = [
  { user: 'mozilla', repo: 'oghliner' },
  { user: 'mozilla', repo: 'platatus' },
  { user: 'mozilla', repo: 'serviceworker-cookbook' },
  { user: 'mozilla', repo: 'progressive-apps-hq' },
  { user: 'marco-c', repo: 'wp-web-push' },
  { user: 'marco-c', repo: 'web-push' },
  { user: 'marco-c', repo: 'mercurius' },
  { user: 'darkwing', repo: 'wp-sw-cache' },
];

wadi.setDashboard(dashboard);

function trackWADIRepositories() {
  gRepositories.forEach(function (r) {
      wadi.addEventsFromRepo(r.user, r.repo);
  });
}

wadi.addBugsTrackedBy(1201717); // web app developer initiative tracker
wadi.addBugsTrackedBy(1059784); // Ship Service Workers on desktop on the release channel
wadi.addBugsTrackedBy(1207262); // Ship Service Workers on Android on the release channel
wadi.addBugsTrackedBy(1003097); // Worker debugging
wadi.addBugsTrackedBy(1156054); // Implement Push API on Android
wadi.addBugsTrackedBy(1201571); // Tracking bug for Push Notifications

gHomeScreenStats = {};

wadi.npmDownloads('oghliner', gHomeScreenStats);

gRepositories.forEach(function(r) {
  wadi.getStargazers(r.user, r.repo, 1, gHomeScreenStats);
});

trackWADIRepositories();
setInterval(trackWADIRepositories, 300000);

var server = express();

server.engine('handlebars', expressHandlebars({defaultLayout: 'main'}));
server.set('view engine', 'handlebars');

server.get('/home', function (req, res) {
    console.log(JSON.stringify(gHomeScreenStats));
    res.render('home', {stats: gHomeScreenStats});
});

server.get('/bugs', function (req, res) {
  var bugDictionary = wadi.getAllBugInfo();
  var keys = Object.keys(bugDictionary);
  var bugArray = keys.map(function (k) { return bugDictionary[k]; });
  var sortedBugs = bugArray.sort(function(x, y) {
    if (x.latest < y.latest) {
      return 1;
    } else if (x.latest > y.latest) {
      return -1;
    } else {
      return 0;
    }
  });

  res.render('bugs', {bugs: sortedBugs});
});

server.get('/events', function (req, res) {
  var events = wadi.getAllEvents();
  var keys = Object.keys(wadi.getAllEvents());

  keys.sort();
  keys.reverse();

  var sortedEvents = keys.map(function (k) { return events[k]; } );

  res.render('events', {events: sortedEvents});
});

server.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});<|endoftext|>


<commit_before>//#!/bin/env node
/* Copyright 2014 Krzysztof Daniel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

var db = require('./db').database;
var logger = require('./util/log.js').log.getLogger('maps');

function createNewMap(req, res){

	var userId = req.user.href;
	logger.debug("new map creation requested for user", userId);

	var name = "";
	if(typeof req.body.name !== 'undefined'){
		name = req.body.name;
	}
	var description = "";
	if(typeof req.body.description !== 'undefined'){
		description = req.body.description;
	}

	var date = null;
	if(typeof req.body.description !== 'undefined'){
		date = req.body.date;
	}

	var stub = {
			name : name,
			description : description,
			userDate : date,
			serverDate : new Date()
	};

	var meta = {
			deleted : false,
			userIdGoogle : userId,
			history : [stub]
	};

	logger.debug("creating map", meta);

	db.maps.save(meta, function(err, saved) {
		if (err !== null) {
			logger.error(err);
			res.setHeader('Content-Type', 'application/json');
			res.statusCode = 500;
			res.send(JSON.stringify(err));
			res.end();
		}
		if(saved) {
			logger.debug("new map", saved._id, "created for user", userId);
			res.writeHead(302, {
				// TODO: this may be a hack
				// I do not like the fact that the UI index UI is driven from here,
				// but it is an extremely easy way to change what page is visible in the browser
				'Location' : ('/map/'+saved._id)
			});
			res.end();
		}
	});
}

function deleteMap(req, res, mapId, redirect) {

	var userId = req.user.href;

	mapId = require('./db').toDatabaseId(mapId);
	logger.debug("deleting map", mapId, "for user", userId);

	var userDate = null; //TODO: if I will ever care what time was in the client when a map was deleted.

	db.maps.findAndModify({
		query : {
			userIdGoogle : userId,
			_id : mapId,
			deleted : false
		},
		update : {
			$set : {
				deleted : true
			},
			$push : {
				// null object in the history
				history : {
					userDate : userDate,
					serverDate : new Date()
				}
			}
		},
		upsert : true,
	}, function(err, object) {
		if (err !== null) {
			logger.error(err);
			res.setHeader('Content-Type', 'application/json');
			res.statusCode = 500;
			res.send(JSON.stringify(err));
		} else {
			if(!redirect){
				res.writeHead(200, {'content-type':'text/html'});
				res.end();
			} else {
				res.redirect(redirect);
			}
		}
	});

}

function updateMap(req, res, mapId) {
	var userId = req.user.href;

	mapId = require('./db').toDatabaseId(mapId);
	logger.debug("updating map", mapId, "for user", userId);
	var historyEntry = req.body;
	// objectID there, replace
	historyEntry._id = mapId;
	historyEntry.userId = userId;
	historyEntry.serverDate = new Date();

	logger.debug('map', historyEntry);

	db.maps.findAndModify({
		query : {
			userIdGoogle : userId,
			_id : mapId,
			deleted : false /* don't modify deleted maps */
		},
		update : {
			$push : {
				history : historyEntry
			}
		},
	}, function(err, object) {
		if (err !== null) {
			logger.error(err);
			res.setHeader('Content-Type', 'application/json');
			res.statusCode = 500;
			res.send(JSON.stringify(err));
		} else {
			res.writeHead(200, {
				'content-type' : 'text/html'
			});
			res.end();
		}
	});
}

function partialMapUpdate(req, res, mapId) {
	var userId = req.user.href;

	mapId = require('./db').toDatabaseId(mapId);
	var load = req.body;
	logger.debug("updating map partially", mapId, "for user", userId,
			" request" + JSON.stringify(load));

	db.maps.find({
		"userIdGoogle" : userId,
		"_id" : mapId,
		deleted : false
	/* don't return deleted maps */
	}, {
		history : {
			$slice : -1
		}
	}).toArray(function(err, map) {
		res.setHeader('Content-Type', 'application/json');
		if (err !== null) {
			logger.error(err);
			res.statusCode = 500;
			res.send(JSON.stringify(err));
		} else {
			// clone last history entry
			var historyEntry = (JSON.parse(JSON.stringify(map[0].history[0])));
			historyEntry.serverDate = new Date();
			historyEntry[load.pk] = load.value;

			db.maps.findAndModify({
				query : {
					userIdGoogle : userId,
					_id : mapId,
					deleted : false
				/* don't modify deleted maps */
				},
				update : {
					$push : {
						history : historyEntry
					}
				},
			}, function(err, object) {
				if (err !== null) {
					logger.error(err);
					res.setHeader('Content-Type', 'application/json');
					res.statusCode = 500;
					res.send(JSON.stringify(err));
				} else {
					res.writeHead(200, {
						'content-type' : 'text/html'
					});
					res.end();
				}
			});
		}
	});
}

function getMap(req, res, mapId) {

	var userId = req.user.href;

	mapId = require('./db').toDatabaseId(mapId);
	logger.debug("getting map", mapId, "for user", userId);

	db.maps.find({
		"userIdGoogle" : userId,
		"_id" : mapId,
		deleted : false
	/* don't return deleted maps */
	}, {
		history : {
			$slice : -1
		}
	}).toArray(function(err, maps) {
		res.setHeader('Content-Type', 'application/json');
		if (err !== null) {
			logger.error(err);
			res.statusCode = 500;
			res.send(JSON.stringify(err));
		} else {
			res.send(JSON.stringify(maps[0].history[0]));
		}
	});

}

function getMaps(req, next) {
	var userId = req.user.href;

	logger.debug("getting maps for user", userId);

	db.maps.find({
		"userIdGoogle" : userId,
		deleted : false
	/* don't return deleted maps */
	}, {
		history : {
			$slice : -1
		}
	}).toArray(function(err, maps) {
		if (err !== null) {
			logger.error(err);
		} else {
			//limit what goes with generic maps
			var response = [];
			for (var i = 0; i < maps.length; i++) {
				var singleMap = {};
				singleMap._id = maps[i]._id;
				singleMap.name = maps[i].history[0].name;
				singleMap.description = maps[i].history[0].description;
				singleMap.userDate = maps[i].history[0].userDate;
				singleMap.serverDate = maps[i].history[0].serverDate;
				response.push(singleMap);
			}
			next(response);
		}
	});
}

function migrate(){
	db.maps.find({
		history : {
			$exists : false
		}
	}).toArray(function(err, maps) {
		for(var i = 0; i < maps.length; i++){
			var mapToUpdate = maps[i];
			logger.debug('updating map ' + i);
			logger.debug('updating map ' + mapToUpdate);

			var newBody = {};

			newBody.userId = mapToUpdate.userId;
			newBody.deleted = false;
			newBody._id = mapToUpdate._id;

			var historyEntry = mapToUpdate;
			historyEntry.serverDate = new Date();
			historyEntry.userDate = null;
			historyEntry.nodes = mapToUpdate.nodes;
			historyEntry.connections = mapToUpdate.connections;
			historyEntry.name = mapToUpdate.name;
			historyEntry.description = mapToUpdate.description;

			newBody.history = [ historyEntry ];

			db.maps.findAndModify({
				query : {
					_id : newBody._id,
				},
				update : newBody
			}, function(err, object) {
				if (err !== null) {
					logger.error(err);
				} else {
					logger.debug('success!');
				}
			});
		}
	});
}

exports.createNewMap = createNewMap;
exports.deleteMap = deleteMap;
exports.updateMap = updateMap;
exports.partialMapUpdate = partialMapUpdate;
/* get single map */
exports.getMap = getMap;
/* get all the maps of the user */
exports.getMaps = getMaps;
exports.migrate = migrate;<commit_msg>use proper redirection after map creation.<commit_after>//#!/bin/env node
/* Copyright 2014 Krzysztof Daniel

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

var db = require('./db').database;
var logger = require('./util/log.js').log.getLogger('maps');

function createNewMap(req, res){

	var userId = req.user.href;
	logger.debug("new map creation requested for user", userId);

	var name = "";
	if(typeof req.body.name !== 'undefined'){
		name = req.body.name;
	}
	var description = "";
	if(typeof req.body.description !== 'undefined'){
		description = req.body.description;
	}

	var date = null;
	if(typeof req.body.description !== 'undefined'){
		date = req.body.date;
	}

	var stub = {
			name : name,
			description : description,
			userDate : date,
			serverDate : new Date()
	};

	var meta = {
			deleted : false,
			userIdGoogle : userId,
			history : [stub]
	};

	logger.debug("creating map", meta);

	db.maps.save(meta, function(err, saved) {
		if (err !== null) {
			logger.error(err);
			res.setHeader('Content-Type', 'application/json');
			res.statusCode = 500;
			res.send(JSON.stringify(err));
			res.end();
		}
		if(saved) {
			logger.debug("new map", saved._id, "created for user", userId);
			//tell the client where is the map
			res.redirect('/map/'+saved._id);
		}
	});
}

function deleteMap(req, res, mapId, redirect) {

	var userId = req.user.href;

	mapId = require('./db').toDatabaseId(mapId);
	logger.debug("deleting map", mapId, "for user", userId);

	var userDate = null; //TODO: if I will ever care what time was in the client when a map was deleted.

	db.maps.findAndModify({
		query : {
			userIdGoogle : userId,
			_id : mapId,
			deleted : false
		},
		update : {
			$set : {
				deleted : true
			},
			$push : {
				// null object in the history
				history : {
					userDate : userDate,
					serverDate : new Date()
				}
			}
		},
		upsert : true,
	}, function(err, object) {
		if (err !== null) {
			logger.error(err);
			res.setHeader('Content-Type', 'application/json');
			res.statusCode = 500;
			res.send(JSON.stringify(err));
		} else {
			if(!redirect){
				res.writeHead(200, {'content-type':'text/html'});
				res.end();
			} else {
				res.redirect(redirect);
			}
		}
	});

}

function updateMap(req, res, mapId) {
	var userId = req.user.href;

	mapId = require('./db').toDatabaseId(mapId);
	logger.debug("updating map", mapId, "for user", userId);
	var historyEntry = req.body;
	// objectID there, replace
	historyEntry._id = mapId;
	historyEntry.userId = userId;
	historyEntry.serverDate = new Date();

	logger.debug('map', historyEntry);

	db.maps.findAndModify({
		query : {
			userIdGoogle : userId,
			_id : mapId,
			deleted : false /* don't modify deleted maps */
		},
		update : {
			$push : {
				history : historyEntry
			}
		},
	}, function(err, object) {
		if (err !== null) {
			logger.error(err);
			res.setHeader('Content-Type', 'application/json');
			res.statusCode = 500;
			res.send(JSON.stringify(err));
		} else {
			res.writeHead(200, {
				'content-type' : 'text/html'
			});
			res.end();
		}
	});
}

function partialMapUpdate(req, res, mapId) {
	var userId = req.user.href;

	mapId = require('./db').toDatabaseId(mapId);
	var load = req.body;
	logger.debug("updating map partially", mapId, "for user", userId,
			" request" + JSON.stringify(load));

	db.maps.find({
		"userIdGoogle" : userId,
		"_id" : mapId,
		deleted : false
	/* don't return deleted maps */
	}, {
		history : {
			$slice : -1
		}
	}).toArray(function(err, map) {
		res.setHeader('Content-Type', 'application/json');
		if (err !== null) {
			logger.error(err);
			res.statusCode = 500;
			res.send(JSON.stringify(err));
		} else {
			// clone last history entry
			var historyEntry = (JSON.parse(JSON.stringify(map[0].history[0])));
			historyEntry.serverDate = new Date();
			historyEntry[load.pk] = load.value;

			db.maps.findAndModify({
				query : {
					userIdGoogle : userId,
					_id : mapId,
					deleted : false
				/* don't modify deleted maps */
				},
				update : {
					$push : {
						history : historyEntry
					}
				},
			}, function(err, object) {
				if (err !== null) {
					logger.error(err);
					res.setHeader('Content-Type', 'application/json');
					res.statusCode = 500;
					res.send(JSON.stringify(err));
				} else {
					res.writeHead(200, {
						'content-type' : 'text/html'
					});
					res.end();
				}
			});
		}
	});
}

function getMap(req, res, mapId) {

	var userId = req.user.href;

	mapId = require('./db').toDatabaseId(mapId);
	logger.debug("getting map", mapId, "for user", userId);

	db.maps.find({
		"userIdGoogle" : userId,
		"_id" : mapId,
		deleted : false
	/* don't return deleted maps */
	}, {
		history : {
			$slice : -1
		}
	}).toArray(function(err, maps) {
		res.setHeader('Content-Type', 'application/json');
		if (err !== null) {
			logger.error(err);
			res.statusCode = 500;
			res.send(JSON.stringify(err));
		} else {
			res.send(JSON.stringify(maps[0].history[0]));
		}
	});

}

function getMaps(req, next) {
	var userId = req.user.href;

	logger.debug("getting maps for user", userId);

	db.maps.find({
		"userIdGoogle" : userId,
		deleted : false
	/* don't return deleted maps */
	}, {
		history : {
			$slice : -1
		}
	}).toArray(function(err, maps) {
		if (err !== null) {
			logger.error(err);
		} else {
			//limit what goes with generic maps
			var response = [];
			for (var i = 0; i < maps.length; i++) {
				var singleMap = {};
				singleMap._id = maps[i]._id;
				singleMap.name = maps[i].history[0].name;
				singleMap.description = maps[i].history[0].description;
				singleMap.userDate = maps[i].history[0].userDate;
				singleMap.serverDate = maps[i].history[0].serverDate;
				response.push(singleMap);
			}
			next(response);
		}
	});
}

function migrate(){
	db.maps.find({
		history : {
			$exists : false
		}
	}).toArray(function(err, maps) {
		for(var i = 0; i < maps.length; i++){
			var mapToUpdate = maps[i];
			logger.debug('updating map ' + i);
			logger.debug('updating map ' + mapToUpdate);

			var newBody = {};

			newBody.userId = mapToUpdate.userId;
			newBody.deleted = false;
			newBody._id = mapToUpdate._id;

			var historyEntry = mapToUpdate;
			historyEntry.serverDate = new Date();
			historyEntry.userDate = null;
			historyEntry.nodes = mapToUpdate.nodes;
			historyEntry.connections = mapToUpdate.connections;
			historyEntry.name = mapToUpdate.name;
			historyEntry.description = mapToUpdate.description;

			newBody.history = [ historyEntry ];

			db.maps.findAndModify({
				query : {
					_id : newBody._id,
				},
				update : newBody
			}, function(err, object) {
				if (err !== null) {
					logger.error(err);
				} else {
					logger.debug('success!');
				}
			});
		}
	});
}

exports.createNewMap = createNewMap;
exports.deleteMap = deleteMap;
exports.updateMap = updateMap;
exports.partialMapUpdate = partialMapUpdate;
/* get single map */
exports.getMap = getMap;
/* get all the maps of the user */
exports.getMaps = getMaps;
exports.migrate = migrate;<|endoftext|>


<commit_before>var React = require('react');
var ReactDOM = require('react-dom');
var ReactRouter = require('react-router');
var Link = ReactRouter.Link;
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var IndexRoute = ReactRouter.IndexRoute;
var browserHistory = ReactRouter.browserHistory;

var Stylesheet = require('../public/styles.css');
// var Index = require('../app/components/index/Index');
var NavBar = require('../app/components/navbar/NavBar');
var Signin = require('../app/components/signin/Signin');
var Signup = require('../app/components/signup/Signup');
var Signout = require('../app/components/signout/Signout');
var Lobby = require('../app/components/lobby/Lobby');
var Gameview = require('../app/components/gameview/GameView');

var Auth = require('./auth');
var alertify = require('alertify.js');
alertify.logPosition('bottom right');

var logo = '../public/images/Fwibble-logo-cropped.png';

var port = 3000;
var connectionPoint = 'rocky-forest-16843.herokuapp.com'

if (window.location.hostname === 'localhost'){
  connectionPoint = 'localhost:3000';
}

var io = require('socket.io-client');

console.log('socket connection attempt:', connectionPoint, port, process.env.NODE_ENV)

var socket = io.connect(connectionPoint);

var App = React.createClass({
  contextTypes: {
    router: React.PropTypes.object.isRequired
  },

  getInitialState: function() {

    return {username: null, loggedIn: Auth.loggedIn(), active_game: ''}
  },

  componentDidMount: function(){
    // TODO grab user info based on session token
    // THEN setState based on this info
  },
  setUser: function(data) {
    console.log('setUser data', data)
    Auth.login();
    this.setState({

      username: data.username,
      loggedIn: Auth.loggedIn(),
      active_game: data.active_game
    });

    if(this.state.active_game){
      this.context.router.replace(`/gameview/${data.active_game}`)
    }
    else {
      this.context.router.replace(`/lobby`)
    }
  },
  setActiveGame: function(data){
    this.setState({active_game: data.game_hash});
  },
  joinGame: function(data){
    console.log("calling JOINGAME:", data)
    socket.emit('join:game', {username: data.user, game_hash: data.game_hash})
    this.setState({active_game: data.game_hash});
  },
  fetchUsers: function(gamehash){
    socket.emit('fetch:users', {active_game: gamehash});
  },
  logoutUser: function(){
    socket.emit('logout', {username: this.state.username});
    Auth.logout();
    this.setState({
      username: null,
      loggedIn: Auth.loggedIn(),
      active_game: null
    })
    alertify.success('Successfully logged out');
  },
  endTimer: function(){
    if(this.state.secondsLeft === 0){
    socket.emit('endtimer', {gamehash: this.state.active_game});
    }
  },

  render: function() {

  var navBarShow = this.state.loggedIn ? (<NavBar active_game={this.state.active_game} loggedIn={this.state.loggedIn} logo={this.state.logo}/>) : null;

    return (

        {navBarShow}
        {this.props.children && React.cloneElement(this.props.children, {
          setUser: this.setUser,
          user: this.state.username,
          logoutUser: this.logoutUser,
          joinGame: this.joinGame,
          leaveGame: this.leaveGame,
          setActiveGame: this.setActiveGame,
          active_game: this.state.active_game,
          logo: this.state.logo
        })}

    )
  }
})

ReactDOM.render(
  (
        <Router history={browserHistory} >
          <Route path='/' component={App} >
            <IndexRoute component={Lobby} onEnter={Auth.requireAuth} />
            <Route path='signin' component={Signin} />
            <Route path='signup' component={Signup} />
            <Route path='signout' component={Signout} />
            <Route path='lobby' component={Lobby} onEnter={Auth.requireAuth} />
            <Route path='gameview/:game_hash' component={Gameview} onEnter={Auth.requireAuth} />
          </Route>
        </Router>
  ), document.getElementById('app')
)

/*

NavBar active_game={this.state.active_game} loggedIn={this.state.loggedIn}

*/

/*
   render: function() {
    return (

        <NavBar />
        {this.props.children && React.cloneElement(this.props.children, {
          setUser: this.setUser,
          user: this.state.username
        })}

    )
  }
})

*/<commit_msg>Logout sets active_game to '' instead of null<commit_after>var React = require('react');
var ReactDOM = require('react-dom');
var ReactRouter = require('react-router');
var Link = ReactRouter.Link;
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;
var IndexRoute = ReactRouter.IndexRoute;
var browserHistory = ReactRouter.browserHistory;

var Stylesheet = require('../public/styles.css');
// var Index = require('../app/components/index/Index');
var NavBar = require('../app/components/navbar/NavBar');
var Signin = require('../app/components/signin/Signin');
var Signup = require('../app/components/signup/Signup');
var Signout = require('../app/components/signout/Signout');
var Lobby = require('../app/components/lobby/Lobby');
var Gameview = require('../app/components/gameview/GameView');

var Auth = require('./auth');
var alertify = require('alertify.js');
alertify.logPosition('bottom right');

var logo = '../public/images/Fwibble-logo-cropped.png';

var port = 3000;
var connectionPoint = 'rocky-forest-16843.herokuapp.com'

if (window.location.hostname === 'localhost'){
  connectionPoint = 'localhost:3000';
}

var io = require('socket.io-client');

console.log('socket connection attempt:', connectionPoint, port, process.env.NODE_ENV)

var socket = io.connect(connectionPoint);

var App = React.createClass({
  contextTypes: {
    router: React.PropTypes.object.isRequired
  },

  getInitialState: function() {

    return {username: null, loggedIn: Auth.loggedIn(), active_game: ''}
  },

  componentDidMount: function(){
    // TODO grab user info based on session token
    // THEN setState based on this info
  },
  setUser: function(data) {
    console.log('setUser data', data)
    Auth.login();
    this.setState({

      username: data.username,
      loggedIn: Auth.loggedIn(),
      active_game: data.active_game
    });

    if(this.state.active_game){
      this.context.router.replace(`/gameview/${data.active_game}`)
    }
    else {
      this.context.router.replace(`/lobby`)
    }
  },
  setActiveGame: function(data){
    this.setState({active_game: data.game_hash});
  },
  joinGame: function(data){
    console.log("calling JOINGAME:", data)
    socket.emit('join:game', {username: data.user, game_hash: data.game_hash})
    this.setState({active_game: data.game_hash});
  },
  fetchUsers: function(gamehash){
    socket.emit('fetch:users', {active_game: gamehash});
  },
  logoutUser: function(){
    socket.emit('logout', {username: this.state.username});
    Auth.logout();
    this.setState({
      username: null,
      loggedIn: Auth.loggedIn(),
      active_game: ''
    })
    alertify.success('Successfully logged out');
  },
  endTimer: function(){
    if(this.state.secondsLeft === 0){
    socket.emit('endtimer', {gamehash: this.state.active_game});
    }
  },

  render: function() {

  var navBarShow = this.state.loggedIn ? (<NavBar active_game={this.state.active_game} loggedIn={this.state.loggedIn} logo={this.state.logo}/>) : null;

    return (

        {navBarShow}
        {this.props.children && React.cloneElement(this.props.children, {
          setUser: this.setUser,
          user: this.state.username,
          logoutUser: this.logoutUser,
          joinGame: this.joinGame,
          leaveGame: this.leaveGame,
          setActiveGame: this.setActiveGame,
          active_game: this.state.active_game,
          logo: this.state.logo
        })}

    )
  }
})

ReactDOM.render(
  (
        <Router history={browserHistory} >
          <Route path='/' component={App} >
            <IndexRoute component={Lobby} onEnter={Auth.requireAuth} />
            <Route path='signin' component={Signin} />
            <Route path='signup' component={Signup} />
            <Route path='signout' component={Signout} />
            <Route path='lobby' component={Lobby} onEnter={Auth.requireAuth} />
            <Route path='gameview/:game_hash' component={Gameview} onEnter={Auth.requireAuth} />
          </Route>
        </Router>
  ), document.getElementById('app')
)

/*

NavBar active_game={this.state.active_game} loggedIn={this.state.loggedIn}

*/

/*
   render: function() {
    return (

        <NavBar />
        {this.props.children && React.cloneElement(this.props.children, {
          setUser: this.setUser,
          user: this.state.username
        })}

    )
  }
})

*/<|endoftext|>


<commit_before>/*jslint node: true, unparam: true, nomen: true, vars: true*/
'use strict';

var isProduction = process.env.NODE_ENV === 'production';

var mongoose = require('mongoose').connect(process.env.VDUMKA_MONGO_CONNECTION_STRING);
var models = require('./models');
var books = mongoose.model('book', models.book, 'books');
var articles = mongoose.model('article', models.article, 'articles');
var cachestamps = mongoose.model('cachestamps', models.cachestamps);

var express = require('express');
var app = express();

app.set('port', process.env.PORT || 3000);

app.use(require('body-parser')());
app.use(require('compression')());
app.use(express.static(__dirname + (isProduction ? '/../public' : '/../src')));
app.use(function (req, res, next) {
    req.db = {
        books: books,
        articles: articles,
		cachestamps: cachestamps
    };
    next();
});

app.use('/api/books', require('./routes/books'));
app.use('/api/blog', require('./routes/blog'));

var server = app.listen(app.get('port'), function () {
    console.log('Listening on port %d', server.address().port);
});
<commit_msg>Update app.js<commit_after>/*jslint node: true, unparam: true, nomen: true, vars: true*/
'use strict';

var isProduction = process.env.NODE_ENV === 'production';

var mongoose = require('mongoose').connect(process.env.VDUMKA_MONGO_CONNECTION_STRING);
var models = require('./models');
var books = mongoose.model('book', models.book, 'books');
var articles = mongoose.model('article', models.article, 'articles');
var cachestamps = mongoose.model('cachestamps', models.cachestamps);

var express = require('express');
var app = express();

app.set('port', process.env.PORT || 3000);

app.use(require('body-parser')());
app.use(require('compression')());
app.use(express.static(__dirname + (isProduction ? '/../public' : '/../src')));
app.use(function (req, res, next) {
    req.db = {
        books: books,
        articles: articles,
        cachestamps: cachestamps
    };
    next();
});

app.use('/api/books', require('./routes/books'));
app.use('/api/blog', require('./routes/blog'));

var server = app.listen(app.get('port'), function () {
    console.log('Listening on port %d', server.address().port);
});
<|endoftext|>


<commit_before>const { fork } = require('child_process');
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const nodemailer = require('nodemailer');
var multer = require('multer');
var bodyParser = require('body-parser');
const routes = require('./app.routes.js');
var File = require("./models/file");
db = require('./app.db.js');
var Permission = require("./models/permission");
const HugoGenes = require('./HugoGenes.json');

// Middleware
var app = express();
app.use(function (req, res, next) { //allow cross origin requests
    res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
    // res.header("Access-Control-Allow-Origin", "http://localhost:" + process.env.NODE_PORT + "/api");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    res.header("Access-Control-Allow-Credentials", true);
    next();
});
app.use(cors({ origin: ['http://localhost:4200', 'http://localhost:8080'] }));
app.use(bodyParser.urlencoded({
    limit: '400mb',
    extended: true
}));
app.use(bodyParser.json({limit: '400mb'}));

// Routes
db.getConnection().then( db => {
    console.log("OK READY!");
    routes.init(app);
});

//#region Notify User with email when the file is uploaded and parsed into DB

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'oncoscape.sttrcancer@gmail.com',
        pass: process.env.GMAIL_PASSWORD
    }
});

//#endregion

//#region Uploaded File storage on the server

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '/home/sttrweb/Oncoscape/uploads')
        // cb(null, process.env.APP_ROOT + '/uploads')
    },
    filename: function (req, file, cb) {
        var newFileName = file.fieldname + '-' + Date.now() + '.xlsx';
        cb(null, newFileName);
    }
});
var upload = multer({
    storage: storage,
    preservePath: true
}).single('file');
//#endregion

//#region Data Upload Functions
var checkHugoGeneSymbols = function (geneArr) {
    var overLappedNames = _.intersection(geneArr, HugoGenes);
    var unvalidGeneNames = _.difference(geneArr, overLappedNames);
    return {
        validNameNumber: overLappedNames.length,
        unvalidNamesArr: unvalidGeneNames
    }
};
function camelToDash(str) {
    return str.replace(/\W+/g, '-')
              .replace(/([a-z\d])([A-Z])/g, '$1-$2')
              .replace("-", "_")
              .toLowerCase();
 }
const writingXLSX2Mongo = (msg) => {
    filePath = msg.filePath;
    projectID = msg.projectID;
    console.log('%%%%%%%%%received file');
    console.log('projectID is: ', msg);
    console.log('%%%%%%%%%XLSX.readFile(filePath): ', filePath);
    console.time("Reading XLSX file");
    var workbook = XLSX.readFile(filePath);
    console.timeEnd("Reading XLSX file");
    var allSheetNames =  Object.keys(workbook.Sheets);
    console.log(allSheetNames);
    if (allSheetNames.indexOf("PATIENT") === -1) {
       err = "PATIENT Sheet is missing!";
       res.json({ error_code: 1, err_desc: err }).end();
       return;
    }
    var PatientIDs;
    var PatientArr = [];
    var UploadingSummary = [];
    var result = [];
    var sampleUnion = [];
    allSheetNames.forEach(function(sheet){
        var sheetObj = XLSX.utils.sheet_to_json(workbook.Sheets[sheet], {header:1});
        var arr = [];
        var header = sheetObj[0];
        if(sheet.split("-")[0] === "MOLECULAR"){
            var allSamples = header.splice(1, header.length);
            sheetObjData = sheetObj.splice(1, sheetObj.length);
            var dataType = sheet.split("-")[1];
            var allMarkers = sheetObjData.map(function(m){return m[0].trim()});
            UploadingSummary.push({ "sheet" : sheet,
                                    "samples" : allSamples,
                                    "markers" : allMarkers});
            var molecularCollectionName = projectID + '_data_molecular';
            var counter = 0;
            console.time("Writing to MongoDB one record at a time");
            sheetObjData.forEach(function(record){
                var obj = {};
                obj.type = dataType;
                obj.marker = record[0];
                obj.data = record.splice(1, record.length);
                arr.push(obj);
            });
            db.collection(projectID+"_data_molecular").insertMany(arr, function(err, result){
                                    if (err) console.log(err);
                                });
            console.timeEnd("Writing to MongoDB one record at a time");
        } else {
            sheetObjData = sheetObj.splice(1, sheetObjData.length);
            if(sheet === "PATIENT") {
                console.log("PATIENT sheet");
                Samples = _.uniq(sheetObjData.map(function(m){
                    return m[0];
                }));
                PatientIDs = _.uniq(sheetObjData.map(function(m){
                    return m[1];
                }));
                var enum_fields = [];
                var num_fields = [];
                var date_fields = [];
                var boolean_fields = [];
                var remaining_fields = [];
                header.forEach(function(h){
                    if(h.indexOf("-Date") > -1) {
                        date_fields.push(h.split("-")[0]);
                    } else if (h.indexOf("-String") > -1) {
                        enum_fields.push(h.split("-")[0]);
                    } else if (h.indexOf("-Number") > -1) {
                        num_fields.push(h.split("-")[0]);
                    } else if (h.indexOf("-Boolean") > -1) {
                        boolean_fields.push(h.split("-")[0]);
                    } else {
                        remaining_fields.push(h.split("-")[0]);
                    }
                });
                // Collect unique values of enums from the entire sheetObjData
                var metaObj = {};
                enum_fields.forEach(function(field){
                    metaObj[camelToDash(field)] = _.uniq(sheetObjData.map(function(record){
                                                            return record[header.indexOf(field +"-String")];}));
                                                        });

                PatientArr = PatientIDs.reduce(function(arr_clinical, p){
                    var samples = [];
                    var enumObj = {};
                    var numObj = {};
                    var booleanObj = {};
                    var timeObj = {};
                    var Other = {};
                    sheetObjData.filter(function(record){
                        return record[1] === p;
                    }).forEach(function(m){
                       samples.push({id: m[0]});
                       enum_fields.forEach(function(field){
                        enumObj[camelToDash(field)] = m[header.indexOf(field+"-String")];
                       });
                       num_fields.forEach(function(field){
                        numObj[camelToDash(field)] = m[header.indexOf(field+"-Number")];
                       });
                       boolean_fields.forEach(function(field){
                        booleanObj[camelToDash(field)] = m[header.indexOf(field+"-Boolean")];
                       });
                       date_fields.forEach(function(field){
                        timeObj[camelToDash(field)] = m[header.indexOf(field+"-Date")];
                       });
                       remaining_fields.forEach(function(field){
                        Other[camelToDash(field)] = m[header.indexOf(field)];
                       });
                    });
                    arr_clinical.push({ "id" : p,
                                        "samples" : samples,
                                        "enums" : enumObj,
                                        "time": timeObj,
                                        "nums": numObj,
                                        "boolean": booleanObj,
                                        "metadata": metaObj,
                                        "events": [] });
                    return arr_clinical;
                    }, []);
                    UploadingSummary.push({"sheet" : sheet,
                                           "patients" : PatientIDs,
                                           "samples": Samples});
            } else if (sheet.split("-")[0] === "PATIENTEVENT"){
                console.log(sheet);
                var id = sheet.split("-")[1];
                var allPatients = _.uniq(sheetObjData.map(function(r){return r[0];}));
                UploadingSummary.push({"sheet" : sheet,
                                       "patients" : allPatients});
                sheetObjData.forEach(function(record){
                    var pos = _.findIndex(PatientArr, function(a){
                        return a.id === record[0];
                    })
                    var o = {};
                    o.id = id;
                    header.forEach(function(h){
                        o[h] = record[header.indexOf(h)];
                    });
                    if( pos > -1){
                        PatientArr[pos].events.push(o);
                    } else {
                        PatientArr.push({
                            "id": record[0],
                            "events":[o]
                        });
                    }
                });
            }
        }
    });
    db.collection(projectID+"_data_clinical").insertMany(PatientArr, function(err, result){
                                    if (err) console.log(err);
                                });

    /* Quality Control */
    var allSampleIDs = [];
    var allPatientIDs = [];
    var sampleMapping = [];

    asyncLoop(UploadingSummary, function(sum, next){
            if('markers' in sum){
                sum.geneSymbolValidation = checkHugoGeneSymbols(sum.markers);
                allSampleIDs = _.uniq(allSampleIDs.concat(sum.samples));
            } else if ('patients' in sum){
                allPatientIDs = _.uniq(allPatientIDs.concat(sum.patients));
            }
            next();
        } , function(err){
            if(err){
                console.log(err);
                res.status(404).send(err).end();
            } else {
                sampleMapping.push({'sample': allSampleIDs});
                UploadingSummary.filter(function(m){
                    return 'markers' in m;
                }).map(function(n){
                  n.positions = n.samples.map(function(d){
                              return allSampleIDs.indexOf(d);
                            });
                  sampleMapping.push(_.pick(n, ['sheet', 'samples', 'positions']));
                  return n;
                });
                var map = _.omit(UploadingSummary.filter(function(m){return m.sheet == 'PATIENT';})[0] ,['_id','sheet']);
                UploadingSummary.push(map);
                sampleMapping.push(map)
                console.log('*********************()****************');
                db.collection(projectID+"_uploadingSummary").insertMany(UploadingSummary, function(err, result){
                    if (err) console.log(err);
                });
                db.collection(projectID+"_data_samples").insertMany(sampleMapping, function(err, result){
                    if (err) console.log(err);
                });
            }
        });
}

// app.use('/api/upload', express.static(process.env.APP_ROOT + '/uploads'));
app.use('/api/upload', express.static('/home/sttrweb/Oncoscape/uploads'));
app.post('/api/upload/:id/:email', Permissions.jwtVerification, upload, function (req, res, next) {
    // upload(req, res, function (err) {
    console.log("This section is triggered");
    var projectID = req.params.id;
    var userEmail = req.params.email;
    console.log('projectID: ', projectID);
    console.log('userEmail: ', userEmail);
    var mailOptions = {
        from: 'jennylouzhang@gmail.com',
        to: userEmail,
        subject: 'Notification from Oncoscape Data Uploading App',
        text: 'Data are in database, ready to share.'
      };
    var molecularColleciton = mongoose.model(projectID + "_data_molecular", File.schema);
    var sampleMapCollection = mongoose.model(projectID + "_data_samples", File.schema);
    var clinicalColleciton = mongoose.model(projectID + "_data_clinical", File.schema);
    var uploadingSummaryCollection = mongoose.model(projectID + "_uploadingSummary", File.schema);
    console.log('test before try block');
    try {
        console.log('in try block before fork');
        console.log('filePath: ', req.file.path);
        // const writing2Mongo = fork(process.env.APP_ROOT + '/server/fileUpload.js',
        const writing2Mongo = fork('/home/sttrweb/Oncoscape/server/fileUpload.js',
        { execArgv: ['--max-old-space-size=4000']});
        writing2Mongo.send({ filePath: req.file.path,
                             projectID: projectID
                          });
        writing2Mongo.on('message', () => {
            res.end('Writing is done');
            console.log("*******************!!!!!!********************");
            transporter.sendMail(mailOptions, function(error, info){
                if (error) {
                  console.log(error);
                } else {
                  console.log('Email sent: ' + info.response);
                }
              });
        });

        //#region Cluster Mode
        // if (cluster.isMaster) {
        //     console.log("am I here?1");
        //     var projectID = req.params.id;
        //     var userEmail = req.params.email;
        //     var mailOptions = {
        //         from: 'oncoscape.sttrcancer@gmail.com',
        //         to: userEmail,
        //         subject: 'Notification from Oncoscape Data Uploading App',
        //         text: 'Data are in database, ready to share.'
        //       };
        //     var molecularColleciton = mongoose.model(projectID + "_data_molecular", File.schema);
        //     var sampleMapCollection = mongoose.model(projectID + "_data_samples", File.schema);
        //     var clinicalColleciton = mongoose.model(projectID + "_data_clinical", File.schema);
        //     var uploadingSummaryCollection = mongoose.model(projectID + "_uploadingSummary", File.schema);

        //     const worker = cluster.fork();
        //     // const worker = fork(process.env.APP_ROOT + '/server/fileUpload.js',
        //     // const worker = fork('/home/sttrweb/Oncoscape/server/fileUpload.js',
        //     // { execArgv: ['--max-old-space-size=4000']});
        //     worker.send({ filePath: req.file.path,
        //                   projectID: projectID
        //                 });
        //     console.log('we are here 1......');
        //     worker.on('message', () => {
        //         res.end('Writing is done');
        //         console.log("*******************!!!!!!********************");
        //         transporter.sendMail(mailOptions, function(error, info){
        //             if (error) {
        //                 console.log(error);
        //             } else {
        //                 console.log('Email sent: ' + info.response);
        //             }
        //             });
        //     });
        // } else {
        //     console.log("am I here? 2");
        //     process.on('message', (filePath, HugoGenes) => {
        //         // db.once("open", function (callback) {
        //             console.log('in WORKER CODE BLOCK, filePath: ', filePath);
        //             writingXLSX2Mongo(filePath, HugoGenes);
        //             process.send("DONE from child");
        //         // });
        //     });
        // }
        //#endregion
    } catch (err) {
        console.log(err);
        return;
    }
    // });
    res.status(200).end();
});

//#endregion

// Start Listening
app.listen(process.env.NODE_PORT, function () {
    console.log("UP");
});
<commit_msg>change back email<commit_after>const { fork } = require('child_process');
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const nodemailer = require('nodemailer');
var multer = require('multer');
var bodyParser = require('body-parser');
const routes = require('./app.routes.js');
var File = require("./models/file");
db = require('./app.db.js');
var Permission = require("./models/permission");
const HugoGenes = require('./HugoGenes.json');

// Middleware
var app = express();
app.use(function (req, res, next) { //allow cross origin requests
    res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
    // res.header("Access-Control-Allow-Origin", "http://localhost:" + process.env.NODE_PORT + "/api");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    res.header("Access-Control-Allow-Credentials", true);
    next();
});
app.use(cors({ origin: ['http://localhost:4200', 'http://localhost:8080'] }));
app.use(bodyParser.urlencoded({
    limit: '400mb',
    extended: true
}));
app.use(bodyParser.json({limit: '400mb'}));

// Routes
db.getConnection().then( db => {
    console.log("OK READY!");
    routes.init(app);
});

//#region Notify User with email when the file is uploaded and parsed into DB

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'oncoscape.sttrcancer@gmail.com',
        pass: process.env.GMAIL_PASSWORD
    }
});

//#endregion

//#region Uploaded File storage on the server

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '/home/sttrweb/Oncoscape/uploads')
        // cb(null, process.env.APP_ROOT + '/uploads')
    },
    filename: function (req, file, cb) {
        var newFileName = file.fieldname + '-' + Date.now() + '.xlsx';
        cb(null, newFileName);
    }
});
var upload = multer({
    storage: storage,
    preservePath: true
}).single('file');
//#endregion

//#region Data Upload Functions
var checkHugoGeneSymbols = function (geneArr) {
    var overLappedNames = _.intersection(geneArr, HugoGenes);
    var unvalidGeneNames = _.difference(geneArr, overLappedNames);
    return {
        validNameNumber: overLappedNames.length,
        unvalidNamesArr: unvalidGeneNames
    }
};
function camelToDash(str) {
    return str.replace(/\W+/g, '-')
              .replace(/([a-z\d])([A-Z])/g, '$1-$2')
              .replace("-", "_")
              .toLowerCase();
 }
const writingXLSX2Mongo = (msg) => {
    filePath = msg.filePath;
    projectID = msg.projectID;
    console.log('%%%%%%%%%received file');
    console.log('projectID is: ', msg);
    console.log('%%%%%%%%%XLSX.readFile(filePath): ', filePath);
    console.time("Reading XLSX file");
    var workbook = XLSX.readFile(filePath);
    console.timeEnd("Reading XLSX file");
    var allSheetNames =  Object.keys(workbook.Sheets);
    console.log(allSheetNames);
    if (allSheetNames.indexOf("PATIENT") === -1) {
       err = "PATIENT Sheet is missing!";
       res.json({ error_code: 1, err_desc: err }).end();
       return;
    }
    var PatientIDs;
    var PatientArr = [];
    var UploadingSummary = [];
    var result = [];
    var sampleUnion = [];
    allSheetNames.forEach(function(sheet){
        var sheetObj = XLSX.utils.sheet_to_json(workbook.Sheets[sheet], {header:1});
        var arr = [];
        var header = sheetObj[0];
        if(sheet.split("-")[0] === "MOLECULAR"){
            var allSamples = header.splice(1, header.length);
            sheetObjData = sheetObj.splice(1, sheetObj.length);
            var dataType = sheet.split("-")[1];
            var allMarkers = sheetObjData.map(function(m){return m[0].trim()});
            UploadingSummary.push({ "sheet" : sheet,
                                    "samples" : allSamples,
                                    "markers" : allMarkers});
            var molecularCollectionName = projectID + '_data_molecular';
            var counter = 0;
            console.time("Writing to MongoDB one record at a time");
            sheetObjData.forEach(function(record){
                var obj = {};
                obj.type = dataType;
                obj.marker = record[0];
                obj.data = record.splice(1, record.length);
                arr.push(obj);
            });
            db.collection(projectID+"_data_molecular").insertMany(arr, function(err, result){
                                    if (err) console.log(err);
                                });
            console.timeEnd("Writing to MongoDB one record at a time");
        } else {
            sheetObjData = sheetObj.splice(1, sheetObjData.length);
            if(sheet === "PATIENT") {
                console.log("PATIENT sheet");
                Samples = _.uniq(sheetObjData.map(function(m){
                    return m[0];
                }));
                PatientIDs = _.uniq(sheetObjData.map(function(m){
                    return m[1];
                }));
                var enum_fields = [];
                var num_fields = [];
                var date_fields = [];
                var boolean_fields = [];
                var remaining_fields = [];
                header.forEach(function(h){
                    if(h.indexOf("-Date") > -1) {
                        date_fields.push(h.split("-")[0]);
                    } else if (h.indexOf("-String") > -1) {
                        enum_fields.push(h.split("-")[0]);
                    } else if (h.indexOf("-Number") > -1) {
                        num_fields.push(h.split("-")[0]);
                    } else if (h.indexOf("-Boolean") > -1) {
                        boolean_fields.push(h.split("-")[0]);
                    } else {
                        remaining_fields.push(h.split("-")[0]);
                    }
                });
                // Collect unique values of enums from the entire sheetObjData
                var metaObj = {};
                enum_fields.forEach(function(field){
                    metaObj[camelToDash(field)] = _.uniq(sheetObjData.map(function(record){
                                                            return record[header.indexOf(field +"-String")];}));
                                                        });

                PatientArr = PatientIDs.reduce(function(arr_clinical, p){
                    var samples = [];
                    var enumObj = {};
                    var numObj = {};
                    var booleanObj = {};
                    var timeObj = {};
                    var Other = {};
                    sheetObjData.filter(function(record){
                        return record[1] === p;
                    }).forEach(function(m){
                       samples.push({id: m[0]});
                       enum_fields.forEach(function(field){
                        enumObj[camelToDash(field)] = m[header.indexOf(field+"-String")];
                       });
                       num_fields.forEach(function(field){
                        numObj[camelToDash(field)] = m[header.indexOf(field+"-Number")];
                       });
                       boolean_fields.forEach(function(field){
                        booleanObj[camelToDash(field)] = m[header.indexOf(field+"-Boolean")];
                       });
                       date_fields.forEach(function(field){
                        timeObj[camelToDash(field)] = m[header.indexOf(field+"-Date")];
                       });
                       remaining_fields.forEach(function(field){
                        Other[camelToDash(field)] = m[header.indexOf(field)];
                       });
                    });
                    arr_clinical.push({ "id" : p,
                                        "samples" : samples,
                                        "enums" : enumObj,
                                        "time": timeObj,
                                        "nums": numObj,
                                        "boolean": booleanObj,
                                        "metadata": metaObj,
                                        "events": [] });
                    return arr_clinical;
                    }, []);
                    UploadingSummary.push({"sheet" : sheet,
                                           "patients" : PatientIDs,
                                           "samples": Samples});
            } else if (sheet.split("-")[0] === "PATIENTEVENT"){
                console.log(sheet);
                var id = sheet.split("-")[1];
                var allPatients = _.uniq(sheetObjData.map(function(r){return r[0];}));
                UploadingSummary.push({"sheet" : sheet,
                                       "patients" : allPatients});
                sheetObjData.forEach(function(record){
                    var pos = _.findIndex(PatientArr, function(a){
                        return a.id === record[0];
                    })
                    var o = {};
                    o.id = id;
                    header.forEach(function(h){
                        o[h] = record[header.indexOf(h)];
                    });
                    if( pos > -1){
                        PatientArr[pos].events.push(o);
                    } else {
                        PatientArr.push({
                            "id": record[0],
                            "events":[o]
                        });
                    }
                });
            }
        }
    });
    db.collection(projectID+"_data_clinical").insertMany(PatientArr, function(err, result){
                                    if (err) console.log(err);
                                });

    /* Quality Control */
    var allSampleIDs = [];
    var allPatientIDs = [];
    var sampleMapping = [];

    asyncLoop(UploadingSummary, function(sum, next){
            if('markers' in sum){
                sum.geneSymbolValidation = checkHugoGeneSymbols(sum.markers);
                allSampleIDs = _.uniq(allSampleIDs.concat(sum.samples));
            } else if ('patients' in sum){
                allPatientIDs = _.uniq(allPatientIDs.concat(sum.patients));
            }
            next();
        } , function(err){
            if(err){
                console.log(err);
                res.status(404).send(err).end();
            } else {
                sampleMapping.push({'sample': allSampleIDs});
                UploadingSummary.filter(function(m){
                    return 'markers' in m;
                }).map(function(n){
                  n.positions = n.samples.map(function(d){
                              return allSampleIDs.indexOf(d);
                            });
                  sampleMapping.push(_.pick(n, ['sheet', 'samples', 'positions']));
                  return n;
                });
                var map = _.omit(UploadingSummary.filter(function(m){return m.sheet == 'PATIENT';})[0] ,['_id','sheet']);
                UploadingSummary.push(map);
                sampleMapping.push(map)
                console.log('*********************()****************');
                db.collection(projectID+"_uploadingSummary").insertMany(UploadingSummary, function(err, result){
                    if (err) console.log(err);
                });
                db.collection(projectID+"_data_samples").insertMany(sampleMapping, function(err, result){
                    if (err) console.log(err);
                });
            }
        });
}

// app.use('/api/upload', express.static(process.env.APP_ROOT + '/uploads'));
app.use('/api/upload', express.static('/home/sttrweb/Oncoscape/uploads'));
app.post('/api/upload/:id/:email', Permissions.jwtVerification, upload, function (req, res, next) {
    // upload(req, res, function (err) {
    console.log("This section is triggered");
    var projectID = req.params.id;
    var userEmail = req.params.email;
    console.log('projectID: ', projectID);
    console.log('userEmail: ', userEmail);
    var mailOptions = {
        from: 'oncoscape.sttrcancer@gmail.com',
        to: userEmail,
        subject: 'Notification from Oncoscape Data Uploading App',
        text: 'Data are in database, ready to share.'
      };
    var molecularColleciton = mongoose.model(projectID + "_data_molecular", File.schema);
    var sampleMapCollection = mongoose.model(projectID + "_data_samples", File.schema);
    var clinicalColleciton = mongoose.model(projectID + "_data_clinical", File.schema);
    var uploadingSummaryCollection = mongoose.model(projectID + "_uploadingSummary", File.schema);
    console.log('test before try block');
    try {
        console.log('in try block before fork');
        console.log('filePath: ', req.file.path);
        // const writing2Mongo = fork(process.env.APP_ROOT + '/server/fileUpload.js',
        const writing2Mongo = fork('/home/sttrweb/Oncoscape/server/fileUpload.js',
        { execArgv: ['--max-old-space-size=4000']});
        writing2Mongo.send({ filePath: req.file.path,
                             projectID: projectID
                          });
        writing2Mongo.on('message', () => {
            res.end('Writing is done');
            console.log("*******************!!!!!!********************");
            transporter.sendMail(mailOptions, function(error, info){
                if (error) {
                  console.log(error);
                } else {
                  console.log('Email sent: ' + info.response);
                }
              });
        });

        //#region Cluster Mode
        // if (cluster.isMaster) {
        //     console.log("am I here?1");
        //     var projectID = req.params.id;
        //     var userEmail = req.params.email;
        //     var mailOptions = {
        //         from: 'oncoscape.sttrcancer@gmail.com',
        //         to: userEmail,
        //         subject: 'Notification from Oncoscape Data Uploading App',
        //         text: 'Data are in database, ready to share.'
        //       };
        //     var molecularColleciton = mongoose.model(projectID + "_data_molecular", File.schema);
        //     var sampleMapCollection = mongoose.model(projectID + "_data_samples", File.schema);
        //     var clinicalColleciton = mongoose.model(projectID + "_data_clinical", File.schema);
        //     var uploadingSummaryCollection = mongoose.model(projectID + "_uploadingSummary", File.schema);

        //     const worker = cluster.fork();
        //     // const worker = fork(process.env.APP_ROOT + '/server/fileUpload.js',
        //     // const worker = fork('/home/sttrweb/Oncoscape/server/fileUpload.js',
        //     // { execArgv: ['--max-old-space-size=4000']});
        //     worker.send({ filePath: req.file.path,
        //                   projectID: projectID
        //                 });
        //     console.log('we are here 1......');
        //     worker.on('message', () => {
        //         res.end('Writing is done');
        //         console.log("*******************!!!!!!********************");
        //         transporter.sendMail(mailOptions, function(error, info){
        //             if (error) {
        //                 console.log(error);
        //             } else {
        //                 console.log('Email sent: ' + info.response);
        //             }
        //             });
        //     });
        // } else {
        //     console.log("am I here? 2");
        //     process.on('message', (filePath, HugoGenes) => {
        //         // db.once("open", function (callback) {
        //             console.log('in WORKER CODE BLOCK, filePath: ', filePath);
        //             writingXLSX2Mongo(filePath, HugoGenes);
        //             process.send("DONE from child");
        //         // });
        //     });
        // }
        //#endregion
    } catch (err) {
        console.log(err);
        return;
    }
    // });
    res.status(200).end();
});

//#endregion

// Start Listening
app.listen(process.env.NODE_PORT, function () {
    console.log("UP");
});
<|endoftext|>