More stuff

This commit is contained in:
Dobie Wollert
2015-08-10 02:36:56 -07:00
parent a228c2fde0
commit 447d80f1b8
4 changed files with 164 additions and 80 deletions

View File

@ -1,4 +1,6 @@
var mongoose = require('mongoose'),
Device = mongoose.model('Device'),
email = require('emailjs'),
TestRun = mongoose.model('TestRun');
var _ = require('lodash');
@ -6,68 +8,131 @@ var md5 = require('MD5');
var log = require('log4node');
exports.index = function(req, res, next) {
log.info('test_runs.index');
TestRun.find({ deleted: false })
.exec(returnResult(res));
};
module.exports = function(config) {
exports.get = function(req, res, next) {
var id = req.param('test_run_id');
log.info("test_runs.get %s", id);
TestRun.findById(id)
.exec(returnResult(res));
};
exports.create = function(req, res, next) {
log.info("test_runs.create %j", res.body);
var testRun = new TestRun(req.body);
testRun.save(returnResult(res));
};
exports.update = function(req, res, next) {
var id = req.param('test_run_id');
log.info('test_runs.update %s %j', id, req.body);
TestRun.findById(id, function(err, testRun) {
if (err) {
log.error("Error: %s", err);
res.json(500, err);
} else if (!testRun) {
res.json(404, 'Unknown TestRun: %s', id);
} else {
_.assign(testRun, req.body);
testRun.save(returnResult(res, testRun));
}
});
};
function returnResult(res, result) {
return function(err, data) {
if (err) {
log.error("Error: %s", err);
res.json(500, err);
} else {
if (result) {
res.json(result);
function returnResult(res, result) {
return function(err, data) {
if (err) {
log.error("Error: %s", err);
res.json(500, err);
} else {
res.json(data);
if (result) {
res.json(result);
} else {
res.json(data);
}
}
}
}
}
function mutateResult(res, mutator) {
return function(err, results) {
if (err) {
log.error("Error: %s", err);
res.json(500, err);
} else {
res.json(mutator(results));
function mutateResult(res, mutator) {
return function(err, results) {
if (err) {
log.error("Error: %s", err);
res.json(500, err);
} else {
res.json(mutator(results));
}
}
}
}
function sendReportEmail(data) {
Device.findById(data.device)
.populate({path: 'deviceType'})
.exec(function(err, device) {
console.log(device);
var subject = "Test run results for device: (" + device.biomedId + ") " + device.deviceType.make + " " + device.deviceType.model;
var message = "";
for (var i = 0; i < data.fields.length; i++) {
var field = data.fields[i];
message += field.label + "\n";
if (field.type == "range") {
message += "Passing Values: " + field.min + "-" + field.max + "\n";
message += "Value: " + field.value + "\n";
}
message += "Result: " + (field.result ? 'Passed' : 'Failed') + "\n";
if (field.comments) {
message += "Comments: \n";
message += field.comments;
}
message += "\n\n";
}
message += "-- Summary --\n";
message += "Result: " + (data.result ? 'Passed' : 'Failed') + "\n";
if (data.comments) {
message += "Comments: \n";
message += data.comments;
}
var server = email.server.connect({
user: config.email.user,
password: config.email.password,
host: 'smtp.gmail.com',
ssl: true
});
var msg = {
text: message,
from: config.email.user,
to: 'parts@atlanticbiomedical.com',
subject: subject
};
server.send(msg);
});
}
return {
index: function(req, res, next) {
log.info('test_runs.index');
TestRun.find({ deleted: false })
.exec(returnResult(res));
},
get: function(req, res, next) {
var id = req.param('test_run_id');
log.info("test_runs.get %s", id);
TestRun.findById(id)
.exec(returnResult(res));
},
create: function(req, res, next) {
log.info("test_runs.create %j", res.body);
var testRun = new TestRun(req.body);
if (!req.body.result) {
sendReportEmail(req.body);
}
testRun.save(returnResult(res));
},
update: function(req, res, next) {
var id = req.param('test_run_id');
log.info('test_runs.update %s %j', id, req.body);
TestRun.findById(id, function(err, testRun) {
if (err) {
log.error("Error: %s", err);
res.json(500, err);
} else if (!testRun) {
res.json(404, 'Unknown TestRun: %s', id);
} else {
_.assign(testRun, req.body);
testRun.save(returnResult(res, testRun));
}
});
}
};
}

View File

@ -8,6 +8,7 @@ var mongoose = require('mongoose'),
Client = mongoose.model('Client'),
Workorder = mongoose.model('Workorder'),
Counter = mongoose.model('Counter'),
Device = mongoose.model('Device'),
User = mongoose.model('User');
module.exports = function(config, calendar) {
@ -85,6 +86,7 @@ module.exports = function(config, calendar) {
var client;
var techs;
var devices;
var jsonResult;
async.waterfall([
@ -105,6 +107,14 @@ module.exports = function(config, calendar) {
callback(err);
});
},
function(callback) {
Device.find({client: req.body.client, deleted: false })
.populate({path: 'deviceType'})
.exec(function(err, results) {
devices = results;
callback(err);
});
},
function(callback) {
User.find({
'_id': { $in: workorder.techs }
@ -117,7 +127,7 @@ module.exports = function(config, calendar) {
function(callback) {
calendar.scheduleEvent({
summary: generateSummary(client),
description: generateDescription(client, workorder, req.user),
description: generateDescription(client, workorder, req.user, null, null, devices),
location: generateLocation(client),
start: workorder.scheduling.start,
end: workorder.scheduling.end,
@ -133,7 +143,7 @@ module.exports = function(config, calendar) {
if (!notify)
return callback(null);
var description = generateDescription(client, workorder, req.user, null, techs);
var description = generateDescription(client, workorder, req.user, null, techs, devices);
var techDescription = appendNotes(description, client, workorder);
var to = req.body.emails;
@ -215,6 +225,7 @@ module.exports = function(config, calendar) {
var workorder;
var client;
var techs;
var devices;
var createdBy;
var modifiedBy;
@ -252,6 +263,14 @@ module.exports = function(config, calendar) {
callback(err);
});
},
function(callback) {
Device.find({'_id': { $in: workorder.devices }})
.populate({path: 'deviceType'})
.exec(function(err, results) {
devices = results;
callback(err);
});
},
function(callback) {
if (workorder.createdBy) {
User.findById(workorder.createdBy, function(err, result) {
@ -284,7 +303,7 @@ module.exports = function(config, calendar) {
function(callback) {
calendar.updateEvent({
summary: generateSummary(client),
description: generateDescription(client, workorder),
description: generateDescription(client, workorder, null, null, null, devices),
location: generateLocation(client),
start: workorder.scheduling.start,
end: workorder.scheduling.end,
@ -299,7 +318,7 @@ module.exports = function(config, calendar) {
return callback(null);
var description = generateDescription(client, workorder, createdBy, modifiedBy, techs);
var description = generateDescription(client, workorder, createdBy, modifiedBy, techs, devices);
var techDescription = appendNotes(description, client, workorder);
var to = req.body.emails;
@ -414,7 +433,7 @@ function appendNotes(message, client, workorder) {
}
function generateDescription(client, workorder, createdBy, modifiedBy) {
function generateDescription(client, workorder, createdBy, modifiedBy, techs, devices) {
var template =
"Workorder ID:\n" +
" %(biomedId)s\n" +
@ -447,7 +466,10 @@ function generateDescription(client, workorder, createdBy, modifiedBy) {
" %(status)s\n" +
"\n" +
"Remarks:\n" +
" %(remarks)s\n";
" %(remarks)s\n" +
"\n" +
"Devices:\n" +
"%(devices)s\n";
var format = "MMMM Do YYYY, h:mm a"
@ -472,6 +494,7 @@ function generateDescription(client, workorder, createdBy, modifiedBy) {
contact: '',
createdBy: '',
modifiedBy: '',
devices: ''
};
if (client.contacts[0]) {
@ -487,7 +510,18 @@ function generateDescription(client, workorder, createdBy, modifiedBy) {
resources.modifiedBy = modifiedBy.name.first + " " + modifiedBy.name.last;
}
return sprintf(template, resources);
if (devices) {
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
resources.devices += "\t(" + device.biomedId + ") " + device.deviceType.make + " " + device.deviceType.model + "\n";
}
}
var result = sprintf(template, resources);
console.log(result);
return result;
}
function generateAttendees(techs, workorder) {

View File

@ -28,12 +28,8 @@ module.exports = function(config) {
return {
scheduleEvent: function(event, callback) {
console.log("schedule event");
api(function(client, callback) {
console.log("Insert Google Calendar");
var params = {
calendarId: 'primary',
};
@ -43,9 +39,6 @@ module.exports = function(config) {
var request = client.calendar.events.insert(params, resource);
request.withAuthClient(oauth2Client).execute(function(err, result) {
console.log("in request callback");
console.dir(err);
console.dir(result);
callback(err, result);
});
}, callback);
@ -60,10 +53,6 @@ module.exports = function(config) {
});
getRequest.withAuthClient(oauth2Client).execute(function(err, result) {
console.log("get result");
console.log(err);
console.log(result);
var resource = buildResource(event, result);
if ('sequence' in resource) {
resource.sequence += 1;
@ -117,7 +106,6 @@ module.exports = function(config) {
var handler = function(client) {
workorder(client, function(err, result) {
if (oauth2Client.credentials.access_token != config.auth.accessToken) {
console.log("Updating access token");
config.auth.accessToken = oauth2Client.credentials.access_token;
}
@ -125,12 +113,9 @@ module.exports = function(config) {
});
};
console.log(apiClient);
if (apiClient) {
console.log("Using cached api client");
handler(apiClient);
} else {
console.log("Getting api client");
googleapis.discover('calendar', 'v3').execute(function(err, client) {
if (err) return callback(err);
apiClient = client;

View File

@ -88,7 +88,7 @@ module.exports = function(app, auth, piler, calendar, directory, config) {
app.post('/api/check_lists', checkLists.create);
app.post('/api/check_lists/:check_list_id', checkLists.update);
var testRuns = require('../app/controllers/testRuns');
var testRuns = require('../app/controllers/testRuns')(config);
app.get('/api/test_runs', testRuns.index);
app.get('/api/test_runs/:test_run_id', testRuns.get);
app.post('/api/test_runs', testRuns.create);