-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdata-source.js
More file actions
39 lines (34 loc) · 1.09 KB
/
data-source.js
File metadata and controls
39 lines (34 loc) · 1.09 KB
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
import { loadAuditLogByKey, loadAuditLogsByOrgId } from './loaders'
export class AuditLogsDataSource {
constructor({ query, userKey, cleanseInput, i18n, transaction, collections }) {
this._query = query
this._transaction = transaction
this._collections = collections
this.byKey = loadAuditLogByKey({ query, userKey, i18n })
this.getConnectionsByOrgId = loadAuditLogsByOrgId({ query, userKey, cleanseInput, i18n })
}
async logActivity({ initiatedBy, action, target, reason = '' }) {
const auditLog = {
timestamp: new Date().toISOString(),
initiatedBy,
target,
action,
reason,
}
const trx = await this._transaction(this._collections)
try {
await trx.step(
() => this._query`
WITH auditLogs
INSERT ${auditLog} INTO auditLogs
RETURN MERGE({ id: NEW._key, _type: "auditLog" }, NEW)
`,
)
await trx.commit()
} catch (err) {
console.error(`Transaction error occurred while attempting to log user action: ${err}`)
await trx.abort()
}
return auditLog
}
}