ProjectAuditLogHandler.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import logger from '@overleaf/logger'
  2. import { ProjectAuditLogEntry } from '../../models/ProjectAuditLogEntry.js'
  3. import { callbackify } from '@overleaf/promise-utils'
  4. export default {
  5. promises: {
  6. addEntry,
  7. },
  8. addEntry: callbackify(addEntry), // callback version of addEntry
  9. addEntryInBackground,
  10. }
  11. /**
  12. * Add an audit log entry
  13. *
  14. * The entry should include at least the following fields:
  15. *
  16. * - operation: a string identifying the type of operation
  17. * - userId: the user on behalf of whom the operation was performed
  18. * - message: a string detailing what happened
  19. */
  20. async function addEntry(
  21. projectId,
  22. operation,
  23. initiatorId,
  24. ipAddress,
  25. info = {}
  26. ) {
  27. const entry = {
  28. projectId,
  29. operation,
  30. initiatorId,
  31. ipAddress,
  32. info,
  33. }
  34. await ProjectAuditLogEntry.create(entry)
  35. }
  36. /**
  37. * Add an audit log entry in the background
  38. *
  39. * This function doesn't return a promise. Instead, it catches any error and logs it.
  40. */
  41. function addEntryInBackground(
  42. projectId,
  43. operation,
  44. initiatorId,
  45. ipAddress,
  46. info = {}
  47. ) {
  48. addEntry(projectId, operation, initiatorId, ipAddress, info).catch(err => {
  49. logger.error(
  50. { err, projectId, operation, initiatorId, ipAddress, info },
  51. 'Failed to write audit log'
  52. )
  53. })
  54. }