Files
biomedjs/app/controllers/clients.js

146 lines
3.6 KiB
JavaScript
Raw Normal View History

2013-05-06 03:38:29 -04:00
var mongoose = require('mongoose'),
Client = mongoose.model('Client'),
2013-07-19 01:27:47 -07:00
Workorder = mongoose.model('Workorder'),
Tag = mongoose.model('Tag');
2013-05-06 03:38:29 -04:00
2014-02-18 01:30:05 -05:00
var log = require('log4node');
2014-07-25 03:00:29 -04:00
var frequencies = ["Medical Device","Sterilizer - TT","Vaporizer","Ice Maker","Anesthesia","Waste Management System","Imaging","Medical Gas Systems","RAE","ERT","N2O Trace Gas","Sterilizer - F","Quarterly","Semi","Annual","legacy","DLLR"];
2013-05-06 03:38:29 -04:00
exports.index = function(req, res) {
2014-02-18 01:30:05 -05:00
log.info("clients.index");
2013-05-06 03:38:29 -04:00
var query = Client.find({ deleted: false })
2013-09-30 01:47:14 -04:00
.select('name identifier address')
2013-05-06 03:38:29 -04:00
.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');
2014-02-18 01:30:05 -05:00
log.info("clients.get %s", id);
2013-05-06 03:38:29 -04:00
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) {
2014-02-18 01:30:05 -05:00
log.info("clients.frequencies");
2013-05-06 03:38:29 -04:00
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');
2014-02-18 01:30:05 -05:00
log.info("clients.workorders %s", id);
2013-05-06 03:38:29 -04:00
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);
});
};
2013-07-19 01:27:47 -07:00
exports.tags = function(req, res, next) {
var id = req.param('client_id');
2014-02-18 01:30:05 -05:00
log.info("clients.tags %s", id);
2013-07-19 01:27:47 -07:00
Tag.find({ client: id })
.exec(function(err, tags) {
if (err) return next(err);
if (!tags) return next(new Error('Failed to load tags ' + id));
res.json(tags);
});
};
2013-05-06 03:38:29 -04:00
exports.create = function(req, res, next) {
2014-02-18 01:30:05 -05:00
log.info("clients.create %j", req.body);
2013-05-06 03:38:29 -04:00
var client = new Client({
name: req.body.name,
identifier: req.body.identifier,
contacts: req.body.contacts,
address: req.body.address,
2013-09-30 01:47:14 -04:00
notes: req.body.notes,
2013-05-06 03:38:29 -04:00
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) {
2014-02-18 01:30:05 -05:00
if (err)
log.error("Error: %s", err);
2013-05-06 03:38:29 -04:00
return res.json(client);
})
};
exports.update = function(req, res, next) {
var id = req.param('client_id');
2014-02-18 01:30:05 -05:00
log.info("clients.update %s %j", id, req.body);
2013-05-06 03:38:29 -04:00
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;
2013-09-30 01:47:14 -04:00
client.notes = req.body.notes;
2013-05-06 03:38:29 -04:00
return client.save(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
return res.json(client);
});
});
};
exports.destroy = function(req, res, next) {
var id = req.param('client_id');
2014-02-18 01:30:05 -05:00
log.info("clients.destroy %s", id);
2013-05-06 03:38:29 -04:00
return Client.findById(id, function(err, client) {
client.deleted = true;
return client.save(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
return res.json(client);
})
});
2013-09-04 03:05:47 -04:00
};