ProjectAuditLogHandler.js 950 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const OError = require('@overleaf/o-error')
  2. const { Project } = require('../../models/Project')
  3. const MAX_AUDIT_LOG_ENTRIES = 200
  4. module.exports = {
  5. promises: {
  6. addEntry
  7. }
  8. }
  9. /**
  10. * Add an audit log entry
  11. *
  12. * The entry should include at least the following fields:
  13. *
  14. * - operation: a string identifying the type of operation
  15. * - userId: the user on behalf of whom the operation was performed
  16. * - message: a string detailing what happened
  17. */
  18. async function addEntry(projectId, operation, initiatorId, info = {}) {
  19. const timestamp = new Date()
  20. const entry = {
  21. operation,
  22. initiatorId,
  23. timestamp,
  24. info
  25. }
  26. const result = await Project.updateOne(
  27. { _id: projectId },
  28. {
  29. $push: {
  30. auditLog: { $each: [entry], $slice: -MAX_AUDIT_LOG_ENTRIES }
  31. }
  32. }
  33. ).exec()
  34. if (result.nModified === 0) {
  35. throw new OError({
  36. message: 'project not found',
  37. info: { projectId }
  38. })
  39. }
  40. }