UNPKG

openhim-core

Version:

The OpenHIM core application that provides logging and routing of http requests

164 lines (144 loc) 5.89 kB
var Audit, Q, authorisation, getProjectionObject, logger, utils; Audit = require('../model/audits').Audit; authorisation = require('./authorisation'); Q = require('q'); logger = require('winston'); utils = require("../utils"); getProjectionObject = function(filterRepresentation) { switch (filterRepresentation) { case "simpledetails": return {}; case "full": return {}; default: return { "participantObjectIdentification": 0, "activeParticipant": 0, "rawMessage": 0 }; } }; /* * Adds a Audit */ exports.addAudit = function*() { var audit, auditData, e, result; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to addAudit denied.", 'info'); return; } auditData = this.request.body; try { audit = new Audit(auditData); result = (yield Q.ninvoke(audit, 'save')); logger.info("User " + this.authenticated.email + " created audit with id " + audit.id); this.body = 'Audit successfully created'; return this.status = 201; } catch (_error) { e = _error; logger.error("Could not add a audit via the API: " + e.message); this.body = e.message; return this.status = 400; } }; /* * Retrieves the list of Audits */ exports.getAudits = function*() { var e, filterLimit, filterPage, filterRepresentation, filterSkip, filters, filtersObject, objectID, participantObjectID, patientID, projectionFiltersObject; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getAudits denied.", 'info'); return; } try { filtersObject = this.request.query; filterLimit = filtersObject.filterLimit; filterPage = filtersObject.filterPage; filterRepresentation = filtersObject.filterRepresentation; delete filtersObject.filterLimit; delete filtersObject.filterPage; delete filtersObject.filterRepresentation; filterSkip = filterPage * filterLimit; projectionFiltersObject = getProjectionObject(filterRepresentation); filters = JSON.parse(filtersObject.filters); if (filters['eventIdentification.eventDateTime']) { filters['eventIdentification.eventDateTime'] = JSON.parse(filters['eventIdentification.eventDateTime']); } if (filters['participantObjectIdentification.participantObjectID']) { if (filters['participantObjectIdentification.participantObjectID'].type) { patientID = new RegExp(filters['participantObjectIdentification.participantObjectID'].patientID); objectID = new RegExp(filters['participantObjectIdentification.participantObjectID'].objectID); filters['$and'] = [ { 'participantObjectIdentification.participantObjectID': patientID }, { 'participantObjectIdentification.participantObjectID': objectID } ]; delete filters['participantObjectIdentification.participantObjectID']; } else { participantObjectID = JSON.parse(filters['participantObjectIdentification.participantObjectID']); filters['participantObjectIdentification.participantObjectID'] = new RegExp("" + participantObjectID); } } return this.body = (yield Audit.find(filters, projectionFiltersObject).skip(filterSkip).limit(filterLimit).sort({ 'eventIdentification.eventDateTime': -1 }).exec()); } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Could not retrieve audits via the API: " + e, 'error'); } }; /* * Retrieves the details for a specific Audit Record */ exports.getAuditById = function*(auditId) { var e, projectionFiltersObject, result; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getAuditById denied.", 'info'); return; } auditId = unescape(auditId); try { projectionFiltersObject = getProjectionObject('full'); result = (yield Audit.findById(auditId, projectionFiltersObject).exec()); if (!result) { this.body = "Could not find audits record with ID: " + auditId; return this.status = 404; } else { return this.body = result; } } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Could not get audit by ID via the API: " + e, 'error'); } }; /* * construct audit filtering dropdown options */ exports.getAuditsFilterOptions = function*() { var auditSourceID, e, eventID, eventTypeCode, participantObjectIDTypeCode, responseObject, roleIDCode; if (!authorisation.inGroup('admin', this.authenticated)) { utils.logAndSetResponse(this, 403, "User " + this.authenticated.email + " is not an admin, API access to getAudits denied.", 'info'); return; } try { eventID = (yield Audit.distinct('eventIdentification.eventID').exec()); eventTypeCode = (yield Audit.distinct('eventIdentification.eventTypeCode').exec()); roleIDCode = (yield Audit.distinct('activeParticipant.roleIDCode').exec()); participantObjectIDTypeCode = (yield Audit.distinct('participantObjectIdentification.participantObjectIDTypeCode').exec()); auditSourceID = (yield Audit.distinct('auditSourceIdentification.auditSourceID').exec()); responseObject = { eventType: eventTypeCode, eventID: eventID, activeParticipantRoleID: roleIDCode, participantObjectIDTypeCode: participantObjectIDTypeCode, auditSourceID: auditSourceID }; return this.body = responseObject; } catch (_error) { e = _error; return utils.logAndSetResponse(this, 500, "Could not retrieve audits filter options via the API: " + e, 'error'); } }; //# sourceMappingURL=audits.js.map