-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauditEntry.js
100 lines (81 loc) · 3.61 KB
/
auditEntry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Represent a AuditEntry entity.
import mongoose from "mongoose";
import Entity from "./entity.js";
export default class AuditEntry extends Entity {
//audit entry schema
//give all schemas to reference and dont require them
static schema = new mongoose.Schema({
user: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: false},
provider: {type: mongoose.Schema.Types.ObjectId, ref: 'Provider', required: false},
providerEntry: {type: mongoose.Schema.Types.ObjectId, ref: 'ProviderEntry', required: false},
managingUser: {type: mongoose.Schema.Types.ObjectId, ref: 'ManagingUser', required: false},
servicesOffered: {type: mongoose.Schema.Types.ObjectId, ref: 'ServicesOffered', required: false},
createdBy: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: false}, //user that requested the change
ref: {type: "String", required: true}, //name of Class
propertiesChanged: {type: "String", required: false}, //properties changed, comma-delimited
propertyValuesBefore: {type: "Object", required: false}, //value of properties before change
propertyValuesAfter: {type: "Object", required: false}, //value of properties after change
action: {type: "String", required: true}, //what crud operation happened
requestMethod: {type: "String", required: true}, //which HTTP request method was called
endpoint: {type: "String", required: true}, //what endpoint was called
createdAt: {type: "Date", default: Date.now, required: true},
});
static async addAuditEntry(userCreatedBy, body, action, requestedMethod, endpoint, doc, ref ){
const auditEntry = new AuditEntry();
if(action === "Update"){
let keysUpdated = "";
for(let [key, value] of Object.entries(body)) {
if(keysUpdated === ""){
keysUpdated += key;
}
else{
keysUpdated += `,${key}`;
}
}
if(doc){
let docProperties = {};
for(let [key, value] of Object.entries(doc.toJSON())) {
if(keysUpdated.includes(key)){
docProperties[key] = value;
}
}
auditEntry.propertyValuesBefore = docProperties;
}
auditEntry.propertiesChanged = keysUpdated;
auditEntry.propertyValuesAfter = body;
}
const idCreatedBy = new mongoose.Types.ObjectId(userCreatedBy._id);
//add changes to audit entry
//get objectId to add to audit entry
if(doc){
const idToSearch = new mongoose.Types.ObjectId(doc._id);
//figure out which entity to set the ObjectID for
switch(ref) {
case "User":
auditEntry.user = idToSearch;
break;
case "Provider":
auditEntry.provider = idToSearch;
break;
case "ProviderEntry":
auditEntry.providerEntry = idToSearch;
break;
case "ServicesOffered":
break;
case "ManagingUser":
auditEntry.managingUser = idToSearch;
break;
}
}
//set the requested user
auditEntry.createdBy = idCreatedBy;
auditEntry.ref = ref;
auditEntry.action = action;
auditEntry.requestMethod = requestedMethod;
auditEntry.endpoint = endpoint;
//create audit entry
await AuditEntry.create(auditEntry);
}
//create audit entry model
static model = mongoose.model("AuditEntry", AuditEntry.schema, "AuditEntries");
}