UNPKG

openhim-core

Version:

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

105 lines (96 loc) 3.38 kB
var Audit, firstCharLowerCase, logger, parseAuditRecordFromXML, parseString, syslogParser; logger = require('winston'); syslogParser = require('glossy').Parse; parseString = require('xml2js').parseString; firstCharLowerCase = require('xml2js').processors.firstCharLowerCase; Audit = require('./model/audits').Audit; parseAuditRecordFromXML = function(xml, callback) { var csdCodeToCode, options, originalTextToDisplayName; csdCodeToCode = function(name) { if (name === 'csd-code') { return 'code'; } else { return name; } }; originalTextToDisplayName = function(name) { if (name === 'originalText') { return 'displayName'; } else { return name; } }; options = { mergeAttrs: true, explicitArray: false, tagNameProcessors: [firstCharLowerCase], attrNameProcessors: [firstCharLowerCase, csdCodeToCode, originalTextToDisplayName] }; return parseString(xml, options, function(err, result) { var ap, audit, i, j, len, len1, poi, ref, ref1; if (err) { return callback(err); } if (!(result != null ? result.auditMessage : void 0)) { return callback(new Error('Document is not a valid AuditMessage')); } audit = {}; if (result.auditMessage.eventIdentification) { audit.eventIdentification = result.auditMessage.eventIdentification; } audit.activeParticipant = []; if (result.auditMessage.activeParticipant) { if (result.auditMessage.activeParticipant instanceof Array) { ref = result.auditMessage.activeParticipant; for (i = 0, len = ref.length; i < len; i++) { ap = ref[i]; audit.activeParticipant.push(ap); } } else { audit.activeParticipant.push(result.auditMessage.activeParticipant); } } if (result.auditMessage.auditSourceIdentification) { audit.auditSourceIdentification = result.auditMessage.auditSourceIdentification; } audit.participantObjectIdentification = []; if (result.auditMessage.participantObjectIdentification) { if (result.auditMessage.participantObjectIdentification instanceof Array) { ref1 = result.auditMessage.participantObjectIdentification; for (j = 0, len1 = ref1.length; j < len1; j++) { poi = ref1[j]; audit.participantObjectIdentification.push(poi); } } else { audit.participantObjectIdentification.push(result.auditMessage.participantObjectIdentification); } } return callback(null, audit); }); }; exports.processAudit = function(msg, callback) { var parsedMsg; parsedMsg = syslogParser.parse(msg); if (!parsedMsg || !parsedMsg.message) { logger.info('Invalid message received'); return callback(); } return parseAuditRecordFromXML(parsedMsg.message, function(xmlErr, result) { var audit; audit = new Audit(result); audit.rawMessage = msg; audit.syslog = parsedMsg; delete audit.syslog.originalMessage; delete audit.syslog.message; return audit.save(function(saveErr) { if (saveErr) { logger.error("An error occurred while processing the audit entry: " + saveErr); } if (xmlErr) { logger.info("Failed to parse message as an AuditMessage XML document: " + xmlErr); } return callback(); }); }); }; //# sourceMappingURL=auditing.js.map