Files
biomedjs/app/controllers/workorders.js

512 lines
13 KiB
JavaScript
Raw Normal View History

2014-02-18 01:30:05 -05:00
var log = require('log4node');
2013-05-06 03:38:29 -04:00
var mongoose = require('mongoose'),
2014-02-18 01:30:05 -05:00
email = require('emailjs'),
2013-05-06 03:38:29 -04:00
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');
2014-02-18 01:30:05 -05:00
module.exports = function(config, calendar) {
2013-05-06 03:38:29 -04:00
return {
index: function(req, res) {
2014-02-18 01:30:05 -05:00
log.info("workorders.index %j", req.query);
2013-05-06 03:38:29 -04:00
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');
2014-02-18 01:30:05 -05:00
log.info("workorders.get %s", id);
2013-05-06 03:38:29 -04:00
Workorder.findById(id)
.populate('client', 'name identifier')
.populate('techs', 'name')
2014-02-18 01:30:05 -05:00
.populate('createdBy', 'name')
.populate('modifiedBy', 'name')
2013-05-06 03:38:29 -04:00
.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) {
2014-02-18 01:30:05 -05:00
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
});
2013-09-04 03:05:47 -04:00
var date = new Date();
2013-05-06 03:38:29 -04:00
var workorder = new Workorder({
client: req.body.client,
2014-07-25 03:00:29 -04:00
emails: req.body.emails,
2013-09-04 03:05:47 -04:00
createdOn: date,
2013-05-06 03:38:29 -04:00
createdBy: req.user,
reason: req.body.reason,
2013-06-17 04:08:57 -04:00
maintenanceType: req.body.maintenanceType || "",
2013-05-06 03:38:29 -04:00
remarks: req.body.remarks || "",
status: req.body.status,
scheduling: req.body.scheduling,
2015-04-06 06:36:17 -04:00
techs: req.body.techs,
2015-07-26 03:46:43 -04:00
alternativeContact: req.body.alternativeContact,
trackingNumber: req.body.trackingNumber
2013-05-06 03:38:29 -04:00
});
2014-02-18 01:30:05 -05:00
var notify = req.body._notify || "";
2013-05-06 03:38:29 -04:00
var client;
var techs;
var jsonResult;
async.waterfall([
function(callback) {
Counter.collection.findAndModify(
{ name: 'workorder' },
[],
{ $inc: { seq: 1 } },
{ 'new': true, upsert: true },
function(err, result) {
workorder.biomedId = result.seq - 1;
callback(err);
});
},
function(callback) {
Client.findById(req.body.client, function(err, result) {
client = result;
callback(err);
});
},
function(callback) {
User.find({
'_id': { $in: workorder.techs }
},
function(err, result) {
techs = result;
callback(err);
});
},
function(callback) {
calendar.scheduleEvent({
summary: generateSummary(client),
2014-02-18 01:30:05 -05:00
description: generateDescription(client, workorder, req.user),
2013-05-06 03:38:29 -04:00
location: generateLocation(client),
start: workorder.scheduling.start,
end: workorder.scheduling.end,
2014-07-25 03:00:29 -04:00
attendees: generateAttendees(techs, workorder)
2013-05-06 03:38:29 -04:00
}, function(err, result) {
2013-09-04 03:05:47 -04:00
if (result) {
workorder.calendarId = result.id;
}
2013-05-06 03:38:29 -04:00
callback(err);
});
},
2014-02-18 01:30:05 -05:00
function(callback) {
if (!notify)
return callback(null);
2015-04-06 03:28:20 -04:00
var description = generateDescription(client, workorder, req.user, null, techs);
2015-04-19 21:15:06 -04:00
var techDescription = appendNotes(description, client, workorder);
2015-04-06 03:28:20 -04:00
var to = req.body.emails;
var techTo = generateToLine(techs);
var subject = 'Workorder created: ' + workorder.biomedId;
async.waterfall([
function(cb) {
if (to && to.length > 0) {
var msg = {
text: description,
from: config.email.user,
to: to,
subject: subject
};
server.send(msg, function(err, message) { cb(err); });
} else {
cb();
}
},
function(cb) {
if (techTo) {
var msg = {
text: techDescription,
from: config.email.user,
to: techTo,
subject: subject
};
server.send(msg, function(err, message) { cb(err); });
} else {
cb();
}
}
], callback);
2014-02-18 01:30:05 -05:00
},
2013-05-06 03:38:29 -04:00
function(callback) {
workorder.save(function(err, result) { callback(err, result); });
},
function(result, callback) {
jsonResult = result;
Client.findByIdAndUpdate(req.body.client, { $push: { 'workorders': result.id } },
function(err, ignored) { callback(err, result) });
2013-09-04 03:05:47 -04:00
},
function(result, callback) {
if (workorder.maintenanceType) {
var key = 'pms.' + date.getFullYear() + '-' + date.getMonth() + '.' + workorder.maintenanceType;
var cmd = { $inc: {} };
cmd.$inc[key] = 1;
Client.findByIdAndUpdate(req.body.client, cmd, function(err, ignored) { callback(err, result) });
2013-09-30 01:47:14 -04:00
} else {
callback(null, result);
2013-09-04 03:05:47 -04:00
}
},
2013-05-06 03:38:29 -04:00
],
function(err, result) {
if (!err) {
res.json(jsonResult);
} else {
throw err;
}
});
},
update: function(req, res, next) {
2014-02-18 01:30:05 -05:00
var server = email.server.connect({
user: config.email.user,
password: config.email.password,
host: 'smtp.gmail.com',
ssl: true
});
var modifiedOn = new Date();
2013-05-06 03:38:29 -04:00
var id = req.param('workorder_id');
2014-02-18 01:30:05 -05:00
log.info("workorders.update %s %j", id, req.body);
2013-05-06 03:38:29 -04:00
var workorder;
var client;
var techs;
2014-02-18 01:30:05 -05:00
var createdBy;
var modifiedBy;
var notify = req.body._notify || "";
2013-05-06 03:38:29 -04:00
async.waterfall([
function(callback) {
Workorder.findById(id, function(err, result) {
workorder = result;
2014-07-25 03:00:29 -04:00
workorder.emails = req.body.emails;
2014-02-18 01:30:05 -05:00
workorder.modifiedBy = req.user;
workorder.modifiedOn = modifiedOn;
2013-05-06 03:38:29 -04:00
workorder.reason = req.body.reason;
2013-09-04 03:05:47 -04:00
workorder.maintenanceType = req.body.maintenanceType || "";
2013-05-06 03:38:29 -04:00
workorder.remarks = req.body.remarks;
workorder.scheduling = req.body.scheduling;
workorder.status = req.body.status;
2013-09-04 03:05:47 -04:00
workorder.techs = req.body.techs
.filter(function(e) { return e; })
.map(function(t) { return t._id; });
2015-04-06 06:36:17 -04:00
workorder.invoiceNumber = req.body.invoiceNumber;
workorder.invoicedOn = req.body.invoicedOn;
workorder.checkNumber = req.body.checkNumber;
workorder.paidOn = req.body.paidOn;
workorder.alternativeContact = req.body.alternativeContact;
2015-07-26 03:46:43 -04:00
workorder.trackingNumber = req.body.trackingNumber;
2013-05-06 03:38:29 -04:00
callback(err);
});
},
function(callback) {
Client.findById(workorder.client, function(err, result) {
client = result;
callback(err);
});
},
function(callback) {
2014-02-18 01:30:05 -05:00
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) {
2013-05-06 03:38:29 -04:00
User.find({
'_id': { $in: workorder.techs }
},
function(err, result) {
techs = result;
callback(err);
});
},
function(callback) {
calendar.updateEvent({
summary: generateSummary(client),
description: generateDescription(client, workorder),
location: generateLocation(client),
start: workorder.scheduling.start,
end: workorder.scheduling.end,
2014-07-25 03:00:29 -04:00
attendees: generateAttendees(techs, workorder),
2013-05-06 03:38:29 -04:00
eventId: workorder.calendarId
}, function(err, result) {
callback(err);
});
},
2014-02-18 01:30:05 -05:00
function(callback) {
if (!notify)
return callback(null);
2015-04-06 03:28:20 -04:00
var description = generateDescription(client, workorder, createdBy, modifiedBy, techs);
2015-04-19 21:15:06 -04:00
var techDescription = appendNotes(description, client, workorder);
2015-04-06 03:28:20 -04:00
var to = req.body.emails;
var techTo = generateToLine(techs);
var subject = 'Workorder updated: ' + workorder.biomedId;
async.waterfall([
function(cb) {
if (to && to.length > 0) {
var msg = {
text: description,
from: config.email.user,
to: to,
subject: subject
};
server.send(msg, function(err, message) { cb(err); });
} else {
cb();
}
},
function(cb) {
if (techTo) {
var msg = {
text: techDescription,
from: config.email.user,
to: techTo,
subject: subject
};
server.send(msg, function(err, message) { cb(err); });
} else {
cb();
}
}
], callback);
2014-02-18 01:30:05 -05:00
},
2013-05-06 03:38:29 -04:00
function(callback) {
workorder.save(function(err) {
callback(err);
})
}
],
function(err) {
2014-02-18 01:30:05 -05:00
if (err)
log.error("Error: %s", err);
2013-05-06 03:38:29 -04:00
res.json(workorder);
});
},
destroy: function(req, res, next) {
var id = req.param('workorder_id');
2014-02-18 01:30:05 -05:00
log.info("workorders.destroy %s", id);
2013-05-06 03:38:29 -04:00
return Workorder.findById(id, function(err, workorder) {
workorder.deleted = true;
return workorder.save(function(err) {
if (!err) {
2013-09-04 03:05:47 -04:00
calendar.deleteEvent(workorder.calendarId, function(err) {
return res.json(workorder);
});
2013-05-06 03:38:29 -04:00
} else {
2014-02-18 01:30:05 -05:00
log.warn("Error: %s", err);
2013-09-04 03:05:47 -04:00
return res.json(workorder);
2013-05-06 03:38:29 -04:00
}
})
});
}
};
};
function generateSummary(client) {
return client.name + ' (' + client.identifier + ')';
}
function generateLocation(client) {
2013-09-04 03:05:47 -04:00
var data = {
street1: client.address.street1 || '',
street2: client.address.street2 || '',
city: client.address.city || '',
state: client.address.state || '',
zip: client.address.zip || ''
};
return sprintf("%(street1)s %(street2)s %(city)s, %(state)s. %(zip)s", data);
2013-05-06 03:38:29 -04:00
}
2015-04-19 21:15:06 -04:00
function appendNotes(message, client, workorder) {
2015-04-06 03:28:20 -04:00
var template =
"%(message)s\n" +
"Tech Notes:\n" +
" %(notes)s\n" +
2015-04-19 21:15:06 -04:00
"\n" +
"Alternative Contact:\n" +
" %(alternativeContact)s\n" +
2015-04-06 03:28:20 -04:00
"\n";
if (client.notes && client.notes['tech']) {
var resources = {
message: message || '',
2015-04-19 21:15:06 -04:00
notes: client.notes['tech'] || '',
alternativeContact: workorder.alternativeContact || ''
2015-04-06 03:28:20 -04:00
};
return sprintf(template, resources);
} else {
return message;
}
}
2014-02-18 01:30:05 -05:00
function generateDescription(client, workorder, createdBy, modifiedBy) {
2013-05-06 03:38:29 -04:00
var template =
"Workorder ID:\n" +
" %(biomedId)s\n" +
"\n" +
2014-07-25 03:00:29 -04:00
"Scheduled Time:\n" +
" %(scheduleStart)s - %(scheduleEnd)s\n" +
"\n" +
2014-02-18 01:30:05 -05:00
"Created By:\n" +
" %(createdBy)s\n" +
"\n" +
"Last Edited By:\n" +
" %(modifiedBy)s\n" +
"\n" +
2013-05-06 03:38:29 -04:00
"Customer:\n" +
" %(name)s (%(identifier)s)\n" +
"\n" +
2013-05-28 03:48:54 -04:00
"Contact:\n" +
" %(contact)s\n" +
2013-05-06 03:38:29 -04:00
" %(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";
2014-07-25 03:00:29 -04:00
var format = "MMMM Do YYYY, h:mm a"
var scheduleStart = moment(workorder.scheduling.start).format(format);
var scheduleEnd = moment(workorder.scheduling.end).format(format);
2013-05-06 03:38:29 -04:00
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 || '',
2014-07-25 03:00:29 -04:00
scheduleStart: scheduleStart,
scheduleEnd: scheduleEnd,
2013-05-28 03:48:54 -04:00
phone: '',
2014-02-18 01:30:05 -05:00
contact: '',
createdBy: '',
modifiedBy: '',
2013-05-06 03:38:29 -04:00
};
if (client.contacts[0]) {
resources.phone = client.contacts[0].phone || '';
2013-05-28 03:48:54 -04:00
resources.contact = client.contacts[0].name || '';
2013-05-06 03:38:29 -04:00
}
2014-02-18 01:30:05 -05:00
if (createdBy) {
resources.createdBy = createdBy.name.first + " " + createdBy.name.last;
}
if (modifiedBy) {
resources.modifiedBy = modifiedBy.name.first + " " + modifiedBy.name.last;
}
2013-05-06 03:38:29 -04:00
return sprintf(template, resources);
}
2014-07-25 03:00:29 -04:00
function generateAttendees(techs, workorder) {
return techs.map(function(t) { return t.email; }).concat(workorder.emails);
2013-05-28 03:48:54 -04:00
}
2014-02-18 01:30:05 -05:00
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;
}