mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
More stuff
This commit is contained in:
@ -1,4 +1,6 @@
|
|||||||
var mongoose = require('mongoose'),
|
var mongoose = require('mongoose'),
|
||||||
|
Device = mongoose.model('Device'),
|
||||||
|
email = require('emailjs'),
|
||||||
TestRun = mongoose.model('TestRun');
|
TestRun = mongoose.model('TestRun');
|
||||||
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
@ -6,68 +8,131 @@ var md5 = require('MD5');
|
|||||||
|
|
||||||
var log = require('log4node');
|
var log = require('log4node');
|
||||||
|
|
||||||
exports.index = function(req, res, next) {
|
module.exports = function(config) {
|
||||||
log.info('test_runs.index');
|
|
||||||
|
|
||||||
TestRun.find({ deleted: false })
|
|
||||||
.exec(returnResult(res));
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.get = function(req, res, next) {
|
function returnResult(res, result) {
|
||||||
var id = req.param('test_run_id');
|
return function(err, data) {
|
||||||
|
if (err) {
|
||||||
log.info("test_runs.get %s", id);
|
log.error("Error: %s", err);
|
||||||
|
res.json(500, err);
|
||||||
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);
|
|
||||||
} else {
|
} else {
|
||||||
res.json(data);
|
if (result) {
|
||||||
|
res.json(result);
|
||||||
|
} else {
|
||||||
|
res.json(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function mutateResult(res, mutator) {
|
function mutateResult(res, mutator) {
|
||||||
return function(err, results) {
|
return function(err, results) {
|
||||||
if (err) {
|
if (err) {
|
||||||
log.error("Error: %s", err);
|
log.error("Error: %s", err);
|
||||||
res.json(500, err);
|
res.json(500, err);
|
||||||
} else {
|
} else {
|
||||||
res.json(mutator(results));
|
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));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ var mongoose = require('mongoose'),
|
|||||||
Client = mongoose.model('Client'),
|
Client = mongoose.model('Client'),
|
||||||
Workorder = mongoose.model('Workorder'),
|
Workorder = mongoose.model('Workorder'),
|
||||||
Counter = mongoose.model('Counter'),
|
Counter = mongoose.model('Counter'),
|
||||||
|
Device = mongoose.model('Device'),
|
||||||
User = mongoose.model('User');
|
User = mongoose.model('User');
|
||||||
|
|
||||||
module.exports = function(config, calendar) {
|
module.exports = function(config, calendar) {
|
||||||
@ -85,6 +86,7 @@ module.exports = function(config, calendar) {
|
|||||||
|
|
||||||
var client;
|
var client;
|
||||||
var techs;
|
var techs;
|
||||||
|
var devices;
|
||||||
var jsonResult;
|
var jsonResult;
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
@ -105,6 +107,14 @@ module.exports = function(config, calendar) {
|
|||||||
callback(err);
|
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) {
|
function(callback) {
|
||||||
User.find({
|
User.find({
|
||||||
'_id': { $in: workorder.techs }
|
'_id': { $in: workorder.techs }
|
||||||
@ -117,7 +127,7 @@ module.exports = function(config, calendar) {
|
|||||||
function(callback) {
|
function(callback) {
|
||||||
calendar.scheduleEvent({
|
calendar.scheduleEvent({
|
||||||
summary: generateSummary(client),
|
summary: generateSummary(client),
|
||||||
description: generateDescription(client, workorder, req.user),
|
description: generateDescription(client, workorder, req.user, null, null, devices),
|
||||||
location: generateLocation(client),
|
location: generateLocation(client),
|
||||||
start: workorder.scheduling.start,
|
start: workorder.scheduling.start,
|
||||||
end: workorder.scheduling.end,
|
end: workorder.scheduling.end,
|
||||||
@ -133,7 +143,7 @@ module.exports = function(config, calendar) {
|
|||||||
if (!notify)
|
if (!notify)
|
||||||
return callback(null);
|
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 techDescription = appendNotes(description, client, workorder);
|
||||||
|
|
||||||
var to = req.body.emails;
|
var to = req.body.emails;
|
||||||
@ -215,6 +225,7 @@ module.exports = function(config, calendar) {
|
|||||||
var workorder;
|
var workorder;
|
||||||
var client;
|
var client;
|
||||||
var techs;
|
var techs;
|
||||||
|
var devices;
|
||||||
var createdBy;
|
var createdBy;
|
||||||
var modifiedBy;
|
var modifiedBy;
|
||||||
|
|
||||||
@ -252,6 +263,14 @@ module.exports = function(config, calendar) {
|
|||||||
callback(err);
|
callback(err);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
function(callback) {
|
||||||
|
Device.find({'_id': { $in: workorder.devices }})
|
||||||
|
.populate({path: 'deviceType'})
|
||||||
|
.exec(function(err, results) {
|
||||||
|
devices = results;
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
},
|
||||||
function(callback) {
|
function(callback) {
|
||||||
if (workorder.createdBy) {
|
if (workorder.createdBy) {
|
||||||
User.findById(workorder.createdBy, function(err, result) {
|
User.findById(workorder.createdBy, function(err, result) {
|
||||||
@ -284,7 +303,7 @@ module.exports = function(config, calendar) {
|
|||||||
function(callback) {
|
function(callback) {
|
||||||
calendar.updateEvent({
|
calendar.updateEvent({
|
||||||
summary: generateSummary(client),
|
summary: generateSummary(client),
|
||||||
description: generateDescription(client, workorder),
|
description: generateDescription(client, workorder, null, null, null, devices),
|
||||||
location: generateLocation(client),
|
location: generateLocation(client),
|
||||||
start: workorder.scheduling.start,
|
start: workorder.scheduling.start,
|
||||||
end: workorder.scheduling.end,
|
end: workorder.scheduling.end,
|
||||||
@ -299,7 +318,7 @@ module.exports = function(config, calendar) {
|
|||||||
return callback(null);
|
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 techDescription = appendNotes(description, client, workorder);
|
||||||
|
|
||||||
var to = req.body.emails;
|
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 =
|
var template =
|
||||||
"Workorder ID:\n" +
|
"Workorder ID:\n" +
|
||||||
" %(biomedId)s\n" +
|
" %(biomedId)s\n" +
|
||||||
@ -447,7 +466,10 @@ function generateDescription(client, workorder, createdBy, modifiedBy) {
|
|||||||
" %(status)s\n" +
|
" %(status)s\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
"Remarks:\n" +
|
"Remarks:\n" +
|
||||||
" %(remarks)s\n";
|
" %(remarks)s\n" +
|
||||||
|
"\n" +
|
||||||
|
"Devices:\n" +
|
||||||
|
"%(devices)s\n";
|
||||||
|
|
||||||
var format = "MMMM Do YYYY, h:mm a"
|
var format = "MMMM Do YYYY, h:mm a"
|
||||||
|
|
||||||
@ -472,6 +494,7 @@ function generateDescription(client, workorder, createdBy, modifiedBy) {
|
|||||||
contact: '',
|
contact: '',
|
||||||
createdBy: '',
|
createdBy: '',
|
||||||
modifiedBy: '',
|
modifiedBy: '',
|
||||||
|
devices: ''
|
||||||
};
|
};
|
||||||
|
|
||||||
if (client.contacts[0]) {
|
if (client.contacts[0]) {
|
||||||
@ -487,7 +510,18 @@ function generateDescription(client, workorder, createdBy, modifiedBy) {
|
|||||||
resources.modifiedBy = modifiedBy.name.first + " " + modifiedBy.name.last;
|
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) {
|
function generateAttendees(techs, workorder) {
|
||||||
|
@ -28,12 +28,8 @@ module.exports = function(config) {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
scheduleEvent: function(event, callback) {
|
scheduleEvent: function(event, callback) {
|
||||||
console.log("schedule event");
|
|
||||||
|
|
||||||
api(function(client, callback) {
|
api(function(client, callback) {
|
||||||
|
|
||||||
console.log("Insert Google Calendar");
|
|
||||||
|
|
||||||
var params = {
|
var params = {
|
||||||
calendarId: 'primary',
|
calendarId: 'primary',
|
||||||
};
|
};
|
||||||
@ -43,9 +39,6 @@ module.exports = function(config) {
|
|||||||
var request = client.calendar.events.insert(params, resource);
|
var request = client.calendar.events.insert(params, resource);
|
||||||
|
|
||||||
request.withAuthClient(oauth2Client).execute(function(err, result) {
|
request.withAuthClient(oauth2Client).execute(function(err, result) {
|
||||||
console.log("in request callback");
|
|
||||||
console.dir(err);
|
|
||||||
console.dir(result);
|
|
||||||
callback(err, result);
|
callback(err, result);
|
||||||
});
|
});
|
||||||
}, callback);
|
}, callback);
|
||||||
@ -60,10 +53,6 @@ module.exports = function(config) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
getRequest.withAuthClient(oauth2Client).execute(function(err, result) {
|
getRequest.withAuthClient(oauth2Client).execute(function(err, result) {
|
||||||
console.log("get result");
|
|
||||||
console.log(err);
|
|
||||||
console.log(result);
|
|
||||||
|
|
||||||
var resource = buildResource(event, result);
|
var resource = buildResource(event, result);
|
||||||
if ('sequence' in resource) {
|
if ('sequence' in resource) {
|
||||||
resource.sequence += 1;
|
resource.sequence += 1;
|
||||||
@ -117,7 +106,6 @@ module.exports = function(config) {
|
|||||||
var handler = function(client) {
|
var handler = function(client) {
|
||||||
workorder(client, function(err, result) {
|
workorder(client, function(err, result) {
|
||||||
if (oauth2Client.credentials.access_token != config.auth.accessToken) {
|
if (oauth2Client.credentials.access_token != config.auth.accessToken) {
|
||||||
console.log("Updating access token");
|
|
||||||
config.auth.accessToken = oauth2Client.credentials.access_token;
|
config.auth.accessToken = oauth2Client.credentials.access_token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,12 +113,9 @@ module.exports = function(config) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(apiClient);
|
|
||||||
if (apiClient) {
|
if (apiClient) {
|
||||||
console.log("Using cached api client");
|
|
||||||
handler(apiClient);
|
handler(apiClient);
|
||||||
} else {
|
} else {
|
||||||
console.log("Getting api client");
|
|
||||||
googleapis.discover('calendar', 'v3').execute(function(err, client) {
|
googleapis.discover('calendar', 'v3').execute(function(err, client) {
|
||||||
if (err) return callback(err);
|
if (err) return callback(err);
|
||||||
apiClient = client;
|
apiClient = client;
|
||||||
|
@ -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', checkLists.create);
|
||||||
app.post('/api/check_lists/:check_list_id', checkLists.update);
|
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', testRuns.index);
|
||||||
app.get('/api/test_runs/:test_run_id', testRuns.get);
|
app.get('/api/test_runs/:test_run_id', testRuns.get);
|
||||||
app.post('/api/test_runs', testRuns.create);
|
app.post('/api/test_runs', testRuns.create);
|
||||||
|
Reference in New Issue
Block a user