2019-10-23 08:24:09 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
|
|
|
const { Project } = require('../../models/Project')
|
|
|
|
|
|
|
|
const MAX_AUDIT_LOG_ENTRIES = 200
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
promises: {
|
2021-04-27 03:52:58 -04:00
|
|
|
addEntry,
|
|
|
|
},
|
2019-10-23 08:24:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an audit log entry
|
|
|
|
*
|
|
|
|
* The entry should include at least the following fields:
|
|
|
|
*
|
|
|
|
* - operation: a string identifying the type of operation
|
|
|
|
* - userId: the user on behalf of whom the operation was performed
|
|
|
|
* - message: a string detailing what happened
|
|
|
|
*/
|
|
|
|
async function addEntry(projectId, operation, initiatorId, info = {}) {
|
|
|
|
const timestamp = new Date()
|
|
|
|
const entry = {
|
|
|
|
operation,
|
|
|
|
initiatorId,
|
|
|
|
timestamp,
|
2021-04-27 03:52:58 -04:00
|
|
|
info,
|
2019-10-23 08:24:09 -04:00
|
|
|
}
|
|
|
|
const result = await Project.updateOne(
|
|
|
|
{ _id: projectId },
|
|
|
|
{
|
|
|
|
$push: {
|
2021-04-27 03:52:58 -04:00
|
|
|
auditLog: { $each: [entry], $slice: -MAX_AUDIT_LOG_ENTRIES },
|
|
|
|
},
|
2019-10-23 08:24:09 -04:00
|
|
|
}
|
|
|
|
).exec()
|
|
|
|
if (result.nModified === 0) {
|
2020-08-11 05:28:29 -04:00
|
|
|
throw new OError('project not found', { projectId })
|
2019-10-23 08:24:09 -04:00
|
|
|
}
|
|
|
|
}
|