This commit is contained in:
Dobie Wollert
2014-02-18 01:30:05 -05:00
parent cfcafd74ac
commit c63d462188
4 changed files with 153 additions and 52 deletions

View File

@ -4,9 +4,12 @@ var mongoose = require('mongoose'),
Workorder = mongoose.model('Workorder'), Workorder = mongoose.model('Workorder'),
Tag = mongoose.model('Tag'); Tag = mongoose.model('Tag');
var log = require('log4node');
var frequencies = ["annual","semi","quarterly","sterilizer","tg","ert","rae","medgas","imaging","neptune","anesthesia"]; var frequencies = ["annual","semi","quarterly","sterilizer","tg","ert","rae","medgas","imaging","neptune","anesthesia"];
exports.index = function(req, res) { exports.index = function(req, res) {
log.info("clients.index");
var query = Client.find({ deleted: false }) var query = Client.find({ deleted: false })
.select('name identifier address') .select('name identifier address')
.slice('contacts', 1) .slice('contacts', 1)
@ -23,6 +26,7 @@ exports.index = function(req, res) {
exports.get = function(req, res, next) { exports.get = function(req, res, next) {
var id = req.param('client_id'); var id = req.param('client_id');
log.info("clients.get %s", id);
Client.findById(id) Client.findById(id)
.exec(function(err, client) { .exec(function(err, client) {
if (err) return next(err); if (err) return next(err);
@ -33,6 +37,8 @@ exports.get = function(req, res, next) {
} }
exports.frequencies = function(req, res, next) { exports.frequencies = function(req, res, next) {
log.info("clients.frequencies");
var query = Client.find({ deleted: false }) var query = Client.find({ deleted: false })
.select('name identifier frequencies') .select('name identifier frequencies')
.slice('contacts', 1) .slice('contacts', 1)
@ -48,6 +54,8 @@ exports.frequencies = function(req, res, next) {
exports.workorders = function(req, res, next) { exports.workorders = function(req, res, next) {
var id = req.param('client_id'); var id = req.param('client_id');
log.info("clients.workorders %s", id);
Workorder.find({ client: id, deleted: false }) Workorder.find({ client: id, deleted: false })
.populate({path: 'techs', select: 'name'}) .populate({path: 'techs', select: 'name'})
.sort('-scheduling.start') .sort('-scheduling.start')
@ -61,6 +69,7 @@ exports.workorders = function(req, res, next) {
exports.tags = function(req, res, next) { exports.tags = function(req, res, next) {
var id = req.param('client_id'); var id = req.param('client_id');
log.info("clients.tags %s", id);
Tag.find({ client: id }) Tag.find({ client: id })
.exec(function(err, tags) { .exec(function(err, tags) {
@ -72,7 +81,7 @@ exports.tags = function(req, res, next) {
}; };
exports.create = function(req, res, next) { exports.create = function(req, res, next) {
console.log(req.body); log.info("clients.create %j", req.body);
var client = new Client({ var client = new Client({
name: req.body.name, name: req.body.name,
@ -90,11 +99,8 @@ exports.create = function(req, res, next) {
} }
return client.save(function(err) { return client.save(function(err) {
if (!err) { if (err)
console.log("saved"); log.error("Error: %s", err);
} else {
console.log("error");
}
return res.json(client); return res.json(client);
}) })
@ -102,6 +108,7 @@ exports.create = function(req, res, next) {
exports.update = function(req, res, next) { exports.update = function(req, res, next) {
var id = req.param('client_id'); var id = req.param('client_id');
log.info("clients.update %s %j", id, req.body);
return Client.findById(id, function(err, client) { return Client.findById(id, function(err, client) {
client.name = req.body.name; client.name = req.body.name;
@ -112,11 +119,8 @@ exports.update = function(req, res, next) {
client.notes = req.body.notes; client.notes = req.body.notes;
return client.save(function(err) { return client.save(function(err) {
if (!err) { if (err)
console.log("updated"); log.error("Error: %s", err);
} else {
console.log("error");
}
return res.json(client); return res.json(client);
}); });
@ -126,15 +130,14 @@ exports.update = function(req, res, next) {
exports.destroy = function(req, res, next) { exports.destroy = function(req, res, next) {
var id = req.param('client_id'); var id = req.param('client_id');
log.info("clients.destroy %s", id);
return Client.findById(id, function(err, client) { return Client.findById(id, function(err, client) {
client.deleted = true; client.deleted = true;
return client.save(function(err) { return client.save(function(err) {
if (!err) { if (err)
console.log("deleted"); log.error("Error: %s", err);
} else {
console.log("error");
}
return res.json(client); return res.json(client);
}) })

View File

@ -11,7 +11,8 @@ exports.index = function(req, res) {
Workorder Workorder
.find({ .find({
deleted: false, deleted: false,
'scheduling.start': { '$gte': start, '$lt': end } 'scheduling.start': { '$lte': end },
'scheduling.end': { '$gte': start }
}) })
.populate('techs', 'name') .populate('techs', 'name')
.populate('client', 'name identifier address') .populate('client', 'name identifier address')

View File

@ -1,5 +1,7 @@
var log = require('log4node');
var mongoose = require('mongoose'), var mongoose = require('mongoose'),
email = require('emailjs'),
moment = require('moment'), moment = require('moment'),
async = require('async'), async = require('async'),
sprintf = require('sprintf').sprintf, sprintf = require('sprintf').sprintf,
@ -8,10 +10,12 @@ var mongoose = require('mongoose'),
Counter = mongoose.model('Counter'), Counter = mongoose.model('Counter'),
User = mongoose.model('User'); User = mongoose.model('User');
module.exports = function(calendar) { module.exports = function(config, calendar) {
return { return {
index: function(req, res) { index: function(req, res) {
log.info("workorders.index %j", req.query);
var start = moment(req.query.start).toDate(); var start = moment(req.query.start).toDate();
var end = moment(req.query.end).add('days', 1).toDate(); var end = moment(req.query.end).add('days', 1).toDate();
@ -34,10 +38,13 @@ module.exports = function(calendar) {
get: function(req, res, next) { get: function(req, res, next) {
var id = req.param('workorder_id'); var id = req.param('workorder_id');
log.info("workorders.get %s", id);
Workorder.findById(id) Workorder.findById(id)
.populate('client', 'name identifier') .populate('client', 'name identifier')
.populate('techs', 'name') .populate('techs', 'name')
.populate('createdBy', 'name')
.populate('modifiedBy', 'name')
.exec(function(err, workorder) { .exec(function(err, workorder) {
if (err) return next(err); if (err) return next(err);
if (!workorder) return next(new Error('Failed to load workorder ' + id)); if (!workorder) return next(new Error('Failed to load workorder ' + id));
@ -47,7 +54,15 @@ module.exports = function(calendar) {
}, },
create: function(req, res, next) { create: function(req, res, next) {
console.log(req.body); log.info("workoreders.create %j", req.body);
var server = email.server.connect({
user: config.email.user,
password: config.email.password,
host: 'smtp.gmail.com',
ssl: true
});
var date = new Date(); var date = new Date();
var workorder = new Workorder({ var workorder = new Workorder({
@ -62,13 +77,14 @@ module.exports = function(calendar) {
techs: req.body.techs techs: req.body.techs
}); });
var notify = req.body._notify || "";
var client; var client;
var techs; var techs;
var jsonResult; var jsonResult;
async.waterfall([ async.waterfall([
function(callback) { function(callback) {
console.log("Get next workorder id.");
Counter.collection.findAndModify( Counter.collection.findAndModify(
{ name: 'workorder' }, { name: 'workorder' },
[], [],
@ -80,29 +96,24 @@ module.exports = function(calendar) {
}); });
}, },
function(callback) { function(callback) {
console.log("Get Client");
Client.findById(req.body.client, function(err, result) { Client.findById(req.body.client, function(err, result) {
client = result; client = result;
callback(err); callback(err);
}); });
}, },
function(callback) { function(callback) {
console.log('Get Techs');
User.find({ User.find({
'_id': { $in: workorder.techs } '_id': { $in: workorder.techs }
}, },
function(err, result) { function(err, result) {
console.log(result);
techs = result; techs = result;
callback(err); callback(err);
}); });
}, },
function(callback) { function(callback) {
console.log("Create Calendar Event")
calendar.scheduleEvent({ calendar.scheduleEvent({
summary: generateSummary(client), summary: generateSummary(client),
description: generateDescription(client, workorder), description: generateDescription(client, workorder, req.user),
location: generateLocation(client), location: generateLocation(client),
start: workorder.scheduling.start, start: workorder.scheduling.start,
end: workorder.scheduling.end, end: workorder.scheduling.end,
@ -114,25 +125,37 @@ module.exports = function(calendar) {
callback(err); callback(err);
}); });
}, },
function(callback) {
if (!notify)
return callback(null);
var to = generateToLine(techs);
if (!to)
return callback(null);
server.send({
text: generateDescription(client, workorder, req.user, null, techs),
from: config.email.user,
to: to,
subject: 'Workorder created: ' + workorder.biomedId
}, function(err, message) {
callback(err);
});
},
function(callback) { function(callback) {
console.log("Save Workorder");
workorder.save(function(err, result) { callback(err, result); }); workorder.save(function(err, result) { callback(err, result); });
}, },
function(result, callback) { function(result, callback) {
console.log("Update Client")
jsonResult = result; jsonResult = result;
Client.findByIdAndUpdate(req.body.client, { $push: { 'workorders': result.id } }, Client.findByIdAndUpdate(req.body.client, { $push: { 'workorders': result.id } },
function(err, ignored) { callback(err, result) }); function(err, ignored) { callback(err, result) });
}, },
function(result, callback) { function(result, callback) {
console.log("Update Client - Pms");
if (workorder.maintenanceType) { if (workorder.maintenanceType) {
console.log("Is PM");
var key = 'pms.' + date.getFullYear() + '-' + date.getMonth() + '.' + workorder.maintenanceType; var key = 'pms.' + date.getFullYear() + '-' + date.getMonth() + '.' + workorder.maintenanceType;
var cmd = { $inc: {} }; var cmd = { $inc: {} };
cmd.$inc[key] = 1; cmd.$inc[key] = 1;
console.log(cmd);
Client.findByIdAndUpdate(req.body.client, cmd, function(err, ignored) { callback(err, result) }); Client.findByIdAndUpdate(req.body.client, cmd, function(err, ignored) { callback(err, result) });
} else { } else {
callback(null, result); callback(null, result);
@ -143,25 +166,39 @@ module.exports = function(calendar) {
if (!err) { if (!err) {
res.json(jsonResult); res.json(jsonResult);
} else { } else {
console.log(err);
throw err; throw err;
} }
}); });
}, },
update: function(req, res, next) { update: function(req, res, next) {
var server = email.server.connect({
user: config.email.user,
password: config.email.password,
host: 'smtp.gmail.com',
ssl: true
});
var modifiedOn = new Date();
var id = req.param('workorder_id'); var id = req.param('workorder_id');
log.info("workorders.update %s %j", id, req.body);
var workorder; var workorder;
var client; var client;
var techs; var techs;
var createdBy;
var modifiedBy;
var notify = req.body._notify || "";
async.waterfall([ async.waterfall([
function(callback) { function(callback) {
console.log("Get Workorder");
Workorder.findById(id, function(err, result) { Workorder.findById(id, function(err, result) {
workorder = result; workorder = result;
workorder.modifiedBy = req.user;
workorder.modifiedOn = modifiedOn;
workorder.reason = req.body.reason; workorder.reason = req.body.reason;
workorder.maintenanceType = req.body.maintenanceType || ""; workorder.maintenanceType = req.body.maintenanceType || "";
workorder.remarks = req.body.remarks; workorder.remarks = req.body.remarks;
@ -175,26 +212,41 @@ module.exports = function(calendar) {
}); });
}, },
function(callback) { function(callback) {
console.log("Get Client");
Client.findById(workorder.client, function(err, result) { Client.findById(workorder.client, function(err, result) {
client = result; client = result;
callback(err); callback(err);
}); });
}, },
function(callback) { function(callback) {
console.log('Get Techs'); if (workorder.createdBy) {
User.findById(workorder.createdBy, function(err, result) {
createdBy = result;
callback(err);
});
} else {
callback(null);
}
},
function(callback) {
if (workorder.modifiedBy) {
User.findById(workorder.modifiedBy, function(err, result) {
modifiedBy = result;
callback(err);
});
} else {
callback(null);
}
},
function(callback) {
User.find({ User.find({
'_id': { $in: workorder.techs } '_id': { $in: workorder.techs }
}, },
function(err, result) { function(err, result) {
console.log(result);
techs = result; techs = result;
callback(err); callback(err);
}); });
}, },
function(callback) { function(callback) {
console.log("Update Calendar Event")
calendar.updateEvent({ calendar.updateEvent({
summary: generateSummary(client), summary: generateSummary(client),
description: generateDescription(client, workorder), description: generateDescription(client, workorder),
@ -207,6 +259,23 @@ module.exports = function(calendar) {
callback(err); callback(err);
}); });
}, },
function(callback) {
if (!notify)
return callback(null);
var to = generateToLine(techs);
if (!to)
return callback(null);
server.send({
text: generateDescription(client, workorder, createdBy, modifiedBy, techs),
from: config.email.user,
to: to,
subject: 'Workorder updated: ' + workorder.biomedId
}, function(err, message) {
callback(err);
});
},
function(callback) { function(callback) {
workorder.save(function(err) { workorder.save(function(err) {
callback(err); callback(err);
@ -214,12 +283,8 @@ module.exports = function(calendar) {
} }
], ],
function(err) { function(err) {
if (!err) { if (err)
console.log('updated'); log.error("Error: %s", err);
} else {
console.log('error');
console.log(err);
}
res.json(workorder); res.json(workorder);
}); });
@ -228,21 +293,18 @@ module.exports = function(calendar) {
destroy: function(req, res, next) { destroy: function(req, res, next) {
var id = req.param('workorder_id'); var id = req.param('workorder_id');
log.info("workorders.destroy %s", id);
return Workorder.findById(id, function(err, workorder) { return Workorder.findById(id, function(err, workorder) {
workorder.deleted = true; workorder.deleted = true;
return workorder.save(function(err) { return workorder.save(function(err) {
if (!err) { if (!err) {
console.log("deleted");
calendar.deleteEvent(workorder.calendarId, function(err) { calendar.deleteEvent(workorder.calendarId, function(err) {
if (!err) {
console.log("Calendar event removed.");
}
return res.json(workorder); return res.json(workorder);
}); });
} else { } else {
console.log("error"); log.warn("Error: %s", err);
return res.json(workorder); return res.json(workorder);
} }
}) })
@ -268,11 +330,17 @@ function generateLocation(client) {
return sprintf("%(street1)s %(street2)s %(city)s, %(state)s. %(zip)s", data); return sprintf("%(street1)s %(street2)s %(city)s, %(state)s. %(zip)s", data);
} }
function generateDescription(client, workorder) { function generateDescription(client, workorder, createdBy, modifiedBy) {
var template = var template =
"Workorder ID:\n" + "Workorder ID:\n" +
" %(biomedId)s\n" + " %(biomedId)s\n" +
"\n" + "\n" +
"Created By:\n" +
" %(createdBy)s\n" +
"\n" +
"Last Edited By:\n" +
" %(modifiedBy)s\n" +
"\n" +
"Customer:\n" + "Customer:\n" +
" %(name)s (%(identifier)s)\n" + " %(name)s (%(identifier)s)\n" +
"\n" + "\n" +
@ -307,7 +375,9 @@ function generateDescription(client, workorder) {
status: workorder.status || '', status: workorder.status || '',
remarks: workorder.remarks || '', remarks: workorder.remarks || '',
phone: '', phone: '',
contact: '' contact: '',
createdBy: '',
modifiedBy: '',
}; };
if (client.contacts[0]) { if (client.contacts[0]) {
@ -315,9 +385,35 @@ function generateDescription(client, workorder) {
resources.contact = client.contacts[0].name || ''; resources.contact = client.contacts[0].name || '';
} }
if (createdBy) {
resources.createdBy = createdBy.name.first + " " + createdBy.name.last;
}
if (modifiedBy) {
resources.modifiedBy = modifiedBy.name.first + " " + modifiedBy.name.last;
}
return sprintf(template, resources); return sprintf(template, resources);
} }
function generateAttendees(techs) { function generateAttendees(techs) {
return techs.map(function(t) { return t.email; }); return techs.map(function(t) { return t.email; });
} }
function generateToLine(techs) {
if (!techs) {
return null;
}
var result = '';
for (var i in techs) {
var tech = techs[i]
if (i > 0) {
result += ", ";
}
result += tech.name.first + " " + tech.name.last + " <" + tech.email + ">"
}
return result;
}

View File

@ -7,6 +7,7 @@ var workorderSchema = new Schema({
client: { type: ObjectId, ref: 'Client' }, client: { type: ObjectId, ref: 'Client' },
createdOn: Date, createdOn: Date,
createdBy: { type: ObjectId, ref: 'User' }, createdBy: { type: ObjectId, ref: 'User' },
modifiedBy: { type: ObjectId, ref: 'User' },
reason: String, reason: String,
maintenanceType: String, maintenanceType: String,
remarks: String, remarks: String,