Initial Commit

This commit is contained in:
root
2013-05-06 03:38:29 -04:00
commit d392a540e7
134 changed files with 22012 additions and 0 deletions

View File

@ -0,0 +1,6 @@
var mongoose = require('mongoose');
exports.profile = function(req, res) {
res.json(req.user);
};

127
app/controllers/clients.js Normal file
View File

@ -0,0 +1,127 @@
var mongoose = require('mongoose'),
Client = mongoose.model('Client'),
Workorder = mongoose.model('Workorder');
var frequencies = ["annual","semi","quarterly","sterilizer","tg","ert","rae","medgas","imaging","neptune","anesthesia"];
exports.index = function(req, res) {
var query = Client.find({ deleted: false })
.select('name identifier')
.slice('contacts', 1)
.sort('name')
.exec(function(err, results) {
if (err) {
res.json(500, err);
} else {
res.json(results);
}
});
}
exports.get = function(req, res, next) {
var id = req.param('client_id');
Client.findById(id)
.exec(function(err, client) {
if (err) return next(err);
if (!client) return next(new Error('Failed to load client ' + id));
res.json(client);
});
}
exports.frequencies = function(req, res, next) {
var query = Client.find({ deleted: false })
.select('name identifier frequencies')
.slice('contacts', 1)
.sort('name')
.exec(function(err, results) {
if (err) {
res.json(500, err);
} else {
res.json(results);
}
});
};
exports.workorders = function(req, res, next) {
var id = req.param('client_id');
Workorder.find({ client: id, deleted: false })
.populate({path: 'techs', select: 'name'})
.sort('-scheduling.start')
.exec(function(err, workorders) {
if (err) return next(err);
if (!workorders) return next(new Error('Failed to load workorders ' + id));
res.json(workorders);
});
};
exports.create = function(req, res, next) {
console.log(req.body);
var client = new Client({
name: req.body.name,
identifier: req.body.identifier,
contacts: req.body.contacts,
address: req.body.address,
frequencies: {}
});
var freq = {};
for (key in frequencies) {
client.frequencies[frequencies[key]] = [false, false, false, false, false, false, false, false, false, false, false, false];
}
return client.save(function(err) {
if (!err) {
console.log("saved");
} else {
console.log("error");
}
return res.json(client);
})
};
exports.update = function(req, res, next) {
var id = req.param('client_id');
return Client.findById(id, function(err, client) {
client.name = req.body.name;
client.identifier = req.body.identifier;
client.contacts = req.body.contacts;
client.address = req.body.address;
client.frequencies = req.body.frequencies;
return client.save(function(err) {
if (!err) {
console.log("updated");
} else {
console.log("error");
}
return res.json(client);
});
});
};
exports.destroy = function(req, res, next) {
var id = req.param('client_id');
return Client.findById(id, function(err, client) {
client.deleted = true;
return client.save(function(err) {
if (!err) {
console.log("deleted");
} else {
console.log("error");
}
return res.json(client);
})
});
};

10
app/controllers/home.js Normal file
View File

@ -0,0 +1,10 @@
module.exports = function(piler) {
return {
index: function(req, res) {
res.render("index.jade", {
js: piler.js.renderTags(),
css: piler.css.renderTags()
});
}
};
};

21
app/controllers/login.js Normal file
View File

@ -0,0 +1,21 @@
module.exports = function(piler) {
return {
login: function(req, res) {
res.render("login.jade", {
js: piler.js.renderTags(),
css: piler.css.renderTags()
});
},
error: function(req, res) {
res.render("error.jade", {
js: piler.js.renderTags(),
css: piler.css.renderTags()
});
},
logout: function(req, res) {
req.logout();
res.redirect('/');
}
};
}

View File

@ -0,0 +1,79 @@
var mongoose = require('mongoose'),
email = require('emailjs'),
sprintf = require('sprintf').sprintf,
User = mongoose.model('User');
module.exports = function(config) {
var server = email.server.connect({
user: config.email.user,
password: config.email.password,
host: 'smtp.gmail.com',
ssl: true
});
return {
send: function(req, res) {
console.log(req.body);
var userId = req.body.user;
if (!userId) {
return res.json(404, null);
}
console.log("Sending message");
User.findById(userId, function(err, user) {
if (err) return res.json(500, err);
server.send({
text: generateMessage(user, req.body),
from: config.email.user,
to: generateToLine(user),
subject: 'Message from portal'
}, function(err, message) {
console.log(err || message);
if (err) {
res.json(500, err);
} else {
res.json(null);
}
});
});
}
};
}
function generateToLine(user) {
return user.name.first + " " + user.name.last + " <" + user.email + ">";
}
function generateMessage(user, message) {
var template =
"Message For: %(user)s\n" +
"\n" +
"Name: %(name)s\n" +
"Company: %(company)s\n" +
"Phone: %(phone)s\n" +
"Extension: %(extension)s\n" +
"\n" +
"%(messages)s\n" +
"%(notes)s\n";
var resources = {
user: user.name.first + " " + user.name.last,
name: message.name || '',
company: message.company || '',
phone: message.phone || '',
extension: message.extension || '',
messages: '',
notes: message.notes || ''
};
message.messages.forEach(function(msg) {
if (msg.checked) {
resources.messages += msg.message + "\n";
}
});
return sprintf(template, resources);
}

View File

@ -0,0 +1,26 @@
var mongoose = require('mongoose'),
moment = require('moment'),
Workorder = mongoose.model('Workorder');
exports.index = function(req, res) {
var date = moment(req.query.date);
var start = date.clone().startOf('day').toDate();
var end = date.clone().endOf('day').toDate();
Workorder
.find({
deleted: false,
'scheduling.start': { '$gte': start, '$lt': end }
})
.populate('techs', 'name')
.populate('client', 'name identifier address')
.select('scheduling techs client')
.exec(function(err, results) {
if (err) {
res.json(500, err);
} else {
res.json(results);
}
});
};

25
app/controllers/users.js Normal file
View File

@ -0,0 +1,25 @@
var mongoose = require('mongoose'),
User = mongoose.model('User');
exports.index = function(req, res) {
var criteria = { deleted: false };
if (req.query.group) {
criteria.groups = req.query.group;
}
if (req.query.perms) {
criteria.perms = req.query.perms;
}
var query = User.find(criteria)
.select('name groups')
.exec(function(err, results) {
if (err) {
res.json(500, err);
} else {
res.json(results);
}
});
};

View File

@ -0,0 +1,284 @@
var mongoose = require('mongoose'),
moment = require('moment'),
async = require('async'),
sprintf = require('sprintf').sprintf,
Client = mongoose.model('Client'),
Workorder = mongoose.model('Workorder'),
Counter = mongoose.model('Counter'),
User = mongoose.model('User');
module.exports = function(calendar) {
return {
index: function(req, res) {
var start = moment(req.query.start).toDate();
var end = moment(req.query.end).add('days', 1).toDate();
Workorder
.find({
deleted: false,
'scheduling.start': { '$gte': start, '$lt': end }
})
.populate('client', 'name identifier address')
.populate('techs', 'name')
.sort('-scheduling.start client.name')
.exec(function(err, results) {
if (err) {
res.json(500, err);
} else {
res.json(results);
}
});
},
get: function(req, res, next) {
var id = req.param('workorder_id');
Workorder.findById(id)
.populate('client', 'name identifier')
.populate('techs', 'name')
.exec(function(err, workorder) {
if (err) return next(err);
if (!workorder) return next(new Error('Failed to load workorder ' + id));
res.json(workorder);
});
},
create: function(req, res, next) {
var workorder = new Workorder({
client: req.body.client,
createdOn: new Date(),
createdBy: req.user,
reason: req.body.reason,
remarks: req.body.remarks || "",
status: req.body.status,
scheduling: req.body.scheduling,
techs: req.body.techs
});
var client;
var techs;
var jsonResult;
async.waterfall([
function(callback) {
console.log("Get next workorder id.");
Counter.collection.findAndModify(
{ name: 'workorder' },
[],
{ $inc: { seq: 1 } },
{ 'new': true, upsert: true },
function(err, result) {
workorder.biomedId = result.seq - 1;
callback(err);
});
},
function(callback) {
console.log("Get Client");
Client.findById(req.body.client, function(err, result) {
client = result;
callback(err);
});
},
function(callback) {
console.log('Get Techs');
User.find({
'_id': { $in: workorder.techs }
},
function(err, result) {
console.log(result);
techs = result;
callback(err);
});
},
function(callback) {
console.log("Create Calendar Event")
calendar.scheduleEvent({
summary: generateSummary(client),
description: generateDescription(client, workorder),
location: generateLocation(client),
start: workorder.scheduling.start,
end: workorder.scheduling.end,
attendees: generateAttendees(techs)
}, function(err, result) {
workorder.calendarId = result.id;
callback(err);
});
},
function(callback) {
console.log("Save Workorder");
workorder.save(function(err, result) { callback(err, result); });
},
function(result, callback) {
console.log("Update Client")
jsonResult = result;
Client.findByIdAndUpdate(req.body.client, { $push: { 'workorders': result.id } },
function(err, ignored) { callback(err, result) });
}
],
function(err, result) {
if (!err) {
res.json(jsonResult);
} else {
console.log(err);
throw err;
}
});
},
update: function(req, res, next) {
var id = req.param('workorder_id');
var workorder;
var client;
var techs;
async.waterfall([
function(callback) {
console.log("Get Workorder");
Workorder.findById(id, function(err, result) {
workorder = result;
workorder.reason = req.body.reason;
workorder.remarks = req.body.remarks;
workorder.scheduling = req.body.scheduling;
workorder.status = req.body.status;
workorder.techs = req.body.techs.map(function(t) { return t._id; });
callback(err);
});
},
function(callback) {
console.log("Get Client");
Client.findById(workorder.client, function(err, result) {
client = result;
callback(err);
});
},
function(callback) {
console.log('Get Techs');
User.find({
'_id': { $in: workorder.techs }
},
function(err, result) {
console.log(result);
techs = result;
callback(err);
});
},
function(callback) {
console.log("Update Calendar Event")
calendar.updateEvent({
summary: generateSummary(client),
description: generateDescription(client, workorder),
location: generateLocation(client),
start: workorder.scheduling.start,
end: workorder.scheduling.end,
attendees: generateAttendees(techs),
eventId: workorder.calendarId
}, function(err, result) {
callback(err);
});
},
function(callback) {
workorder.save(function(err) {
callback(err);
})
}
],
function(err) {
if (!err) {
console.log('updated');
} else {
console.log('error');
console.log(err);
}
res.json(workorder);
});
},
destroy: function(req, res, next) {
var id = req.param('workorder_id');
return Workorder.findById(id, function(err, workorder) {
workorder.deleted = true;
return workorder.save(function(err) {
if (!err) {
console.log("deleted");
} else {
console.log("error");
}
return res.json(workorder);
})
});
}
};
};
function generateSummary(client) {
return client.name + ' (' + client.identifier + ')';
}
function generateLocation(client) {
return sprintf("%(street1)s %(street2)s %(city)s, %(state)s. %(zip)s", client.address);
}
function generateDescription(client, workorder) {
var template =
"Workorder ID:\n" +
" %(biomedId)s\n" +
"\n" +
"Customer:\n" +
" %(name)s (%(identifier)s)\n" +
"\n" +
"Phone:\n" +
" %(phone)s\n" +
"\n" +
"Address:\n" +
" %(street1)s\n" +
" %(street2)s\n" +
" %(city)s, %(state)s. %(zip)s\n" +
"\n" +
"Reason:\n" +
" %(reason)s\n" +
"\n" +
"Status:\n" +
" %(status)s\n" +
"\n" +
"Remarks:\n" +
" %(remarks)s\n";
var resources = {
biomedId: workorder.biomedId || '',
name: client.name || '',
identifier: client.identifier || '',
street1: client.address.street1 || '',
street2: client.address.street2 || '',
city: client.address.city || '',
state: client.address.state || '',
zip: client.address.zip || '',
reason: workorder.reason || '',
status: workorder.status || '',
remarks: workorder.remarks || '',
phone: ''
};
if (client.contacts[0]) {
resources.phone = client.contacts[0].phone || '';
}
return sprintf(template, resources);
}
function generateAttendees(techs) {
return techs.map(function(t) { return t.email; });
}

25
app/model/client.js Normal file
View File

@ -0,0 +1,25 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var clientSchema = new Schema({
name: String,
identifier: String,
address: {
street1: String,
street2: String,
city: String,
state: String,
zip: String
},
contacts: [{
name: String,
phone: String,
email: String
}],
frequencies: {},
workorders: [{ type: ObjectId, ref: 'Workorder' }],
deleted: { type: Boolean, default: false }
});
module.exports = mongoose.model('Client', clientSchema);

12
app/model/counter.js Normal file
View File

@ -0,0 +1,12 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var counterSchema = new Schema({
name: String,
seq: { type: Number, default: 0 }
});
counterSchema.index({ field: 1, model: 1 }, { unique: true, required: true, index: -1 });
module.exports = mongoose.model('Counter', counterSchema);

24
app/model/user.js Normal file
View File

@ -0,0 +1,24 @@
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var userSchema = new Schema({
name: {
first: String,
last: String
},
email: String,
picture: String,
refreshToken: String,
accessToken: String,
groups: [String],
perms: [String],
deleted: { type: Boolean, default: false }
});
userSchema.methods.hasPermission = function(perm) {
return this.perms.indexOf(perm) != -1;
}
var User = module.exports = mongoose.model('User', userSchema);

27
app/model/workorder.js Normal file
View File

@ -0,0 +1,27 @@
var mongoose = require('mongoose')
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var workorderSchema = new Schema({
biomedId: Number,
client: { type: ObjectId, ref: 'Client' },
createdOn: Date,
createdBy: { type: ObjectId, ref: 'User' },
reason: String,
remarks: String,
status: String,
scheduling: {
start: Date,
end: Date
},
calendarId: String,
techs: [{ type: ObjectId, ref: 'User' }],
history: [{
oldValues: {},
newValues: {},
modifiedBy: { type: ObjectId, ref: 'User' }
}],
deleted: { type: Boolean, default: false }
});
module.exports = mongoose.model('Workorder', workorderSchema);

18
app/views/error.jade Normal file
View File

@ -0,0 +1,18 @@
doctype 5
html(lang="en")
head
title Atlantic Biomedical - Login
!{css}
body.login-page
.container-fluid
#loginbox
.brand-container
.brand Atlantic Biomedical
form
.alert.alert-error
| You are not authorized to access this site.
| Please contact your site administrator to request access.
p
| Please login with your Google Apps account to continue.
.form-actions
a.btn.btn-danger(href='/auth') Login with Google Apps

50
app/views/index.jade Normal file
View File

@ -0,0 +1,50 @@
doctype 5
html(lang="en", ng-app="biomed", ng-controller="biomed.PageCtrl")
head
title Atlantic Biomedical
!{css}
body
error-panel
.navbar
.navbar-inner
a.brand(href='/', target='_self') Atlantic Biomedical
progress-panel
ul.nav.pull-right(ng-controller='biomed.AccountCtrl')
li
a(href='http://atlanticbiomedical.com/ticket/', target='_blank')
i.icon-fire
| Tickets
li
a(ng-click="openDialog()")
i.icon-envelope
| Messages
li
a(href='/logout', target='_self')
i.icon-share-alt
| Logout
li(ng-show='account.name')
a(href='#')
i.icon-user
| {{account.name.first}} {{account.name.last}}
li.photo(ng-show='account.picture', ns-show='account.picture')
img(ng-src='{{account.picture}}?sz=50')
.navbar.navbar-secondary(bio-navbar)
.navbar-inner
ul.nav
li(data-match-route='/schedule.*')
a(href='/schedule')
i.icon-calendar
| Schedule
li(data-match-route='/client.*')
a(href='/clients')
i.icon-briefcase
| Clients
li(data-match-route='/workorder.*')
a(href='/workorders')
i.icon-wrench
| Workorders
.container-fluid
ng-view
!{js}

15
app/views/login.jade Normal file
View File

@ -0,0 +1,15 @@
doctype 5
html(lang="en")
head
title Atlantic Biomedical - Login
!{css}
body.login-page
.container-fluid
#loginbox
.brand-container
.brand Atlantic Biomedical
form
p
| Please login with your Google Apps account to continue.
.form-actions
a.btn.btn-danger(href='/auth') Login with Google Apps