UNPKG

yekonga-server

Version:
164 lines (139 loc) 5.41 kB
// @ts-nocheck /*global Yekonga */ const auditTrailCollection = "audit_trails"; const H = Yekonga.Helper; class AuditTrail { constructor(options) { this.modelAudit = auditTrailCollection; this.oldValues = []; this.collection = options.collection; this.modelClass = options.modelClass; this.secondaryKey = options.secondaryKey; this.context = {}; } async setOldValues(context, isAdmin = false, model = null) { this.context = context; if(model) { this.oldValues = await model.find(); } else { const { filter } = context; if (filter) { this.oldValues = await Yekonga.Model[this.modelClass].find(filter, context, isAdmin); } } } async record(data, action, documents) { documents = Array.isArray(documents) ? documents : (documents ? [documents] : []); data = H.formatToVariables(data); // console.debug('async record(data, action, documents)', this.context.Auth); var auth = (this.context.Auth) ? this.context.Auth : null; var createdAt = H.getIsoTimestamp(); var updatedValues = await this.getValues(data, action, documents); for (let i = 0; i < updatedValues.length; i++) { const elem = updatedValues[i]; var records = {}; records.profileId = (auth) ? new H.ObjectId(auth.profileId) : null; records.userId = (auth) ? new H.ObjectId(auth.userId) : null; records.event = action; records.newValues = H.formatToColumn(elem.newValues); records.oldValues = H.formatToColumn(elem.oldValues); records.ipAddress = Yekonga.Client.ipAddress; records.browser = Yekonga.Client.browser; records.userAgent = Yekonga.Client.userAgent; records.document = elem.document; records.collection = this.collection; records.model = this.modelClass; records.createdAt = createdAt; await Yekonga.DB.table(auditTrailCollection).create(H.formatToColumn(records)) } } async getValues(data, action, documents) { if (action == 'create') { const updatedValues = []; const newValues = await this.getNewValues(data, 'create'); for (let i = 0; i < newValues.length; i++) { const _new = newValues[i]; var document = null; for (const row of documents) { var matched = true; for (const key in _new) { if (Object.hasOwnProperty.call(_new, key)) { if (_new[key] !== row[key]) { matched = false; break; } } } if (matched) { document = (row["_id"])? row["_id"]: row[this.secondaryKey]; } } updatedValues.push({ document: document, newValues: _new, oldValues: null, }) } return updatedValues; } else if (action == 'delete') { const updatedValues = []; const oldValues = await this.getOldValues(data, 'delete'); for (let i = 0; i < oldValues.length; i++) { const old = oldValues[i]; updatedValues.push({ document: (old["_id"])? old["_id"]: old[this.secondaryKey], newValues: null, oldValues: old, }) } return updatedValues; } else if (action == 'update') { return await this.getUpdateValues(data);; } } async getNewValues(data, action) { if (action == 'create') { return Array.isArray(data) ? data : [data]; } else if (action == 'delete') { return null; } return []; } async getOldValues(data, action) { if (action == 'create') { return null; } else if (action == 'delete') { return this.oldValues; } return []; } async getUpdateValues(data) { var updatedValues = []; var oldValues = this.oldValues; for (let i = 0; i < oldValues.length; i++) { var isChanged = false; const elem = oldValues[i]; const _new = {} const _old = {} for (const key in data) { if (Object.hasOwnProperty.call(data, key)) { if (JSON.stringify(data[key]) !== JSON.stringify(elem[key])) { isChanged = true; _new[key] = data[key]; _old[key] = elem[key]; } } } if (isChanged) { updatedValues.push({ document: (elem["_id"])? elem["_id"]: elem[this.secondaryKey], newValues: _new, oldValues: _old, }) } } // console.debug("updatedValues", updatedValues); return updatedValues; } } module.exports = AuditTrail;