audit-log.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import errs from "../lib/error.js";
  2. import { castJsonIfNeed } from "../lib/helpers.js";
  3. import auditLogModel from "../models/audit-log.js";
  4. const internalAuditLog = {
  5. /**
  6. * All logs
  7. *
  8. * @param {Access} access
  9. * @param {Array} [expand]
  10. * @param {String} [searchQuery]
  11. * @returns {Promise}
  12. */
  13. getAll: async (access, expand, searchQuery) => {
  14. await access.can("auditlog:list");
  15. const query = auditLogModel
  16. .query()
  17. .orderBy("created_on", "DESC")
  18. .orderBy("id", "DESC")
  19. .limit(100)
  20. .allowGraph("[user]");
  21. // Query is used for searching
  22. if (typeof searchQuery === "string" && searchQuery.length > 0) {
  23. query.where(function () {
  24. this.where(castJsonIfNeed("meta"), "like", `%${searchQuery}`);
  25. });
  26. }
  27. if (typeof expand !== "undefined" && expand !== null) {
  28. query.withGraphFetched(`[${expand.join(", ")}]`);
  29. }
  30. return await query;
  31. },
  32. /**
  33. * @param {Access} access
  34. * @param {Object} [data]
  35. * @param {Integer} [data.id] Defaults to the token user
  36. * @param {Array} [data.expand]
  37. * @return {Promise}
  38. */
  39. get: async (access, data) => {
  40. await access.can("auditlog:list");
  41. const query = auditLogModel
  42. .query()
  43. .andWhere("id", data.id)
  44. .allowGraph("[user]")
  45. .first();
  46. if (typeof data.expand !== "undefined" && data.expand !== null) {
  47. query.withGraphFetched(`[${data.expand.join(", ")}]`);
  48. }
  49. const row = await query;
  50. if (!row?.id) {
  51. throw new errs.ItemNotFoundError(data.id);
  52. }
  53. return row;
  54. },
  55. /**
  56. * This method should not be publicly used, it doesn't check certain things. It will be assumed
  57. * that permission to add to audit log is already considered, however the access token is used for
  58. * default user id determination.
  59. *
  60. * @param {Access} access
  61. * @param {Object} data
  62. * @param {String} data.action
  63. * @param {Number} [data.user_id]
  64. * @param {Number} [data.object_id]
  65. * @param {Number} [data.object_type]
  66. * @param {Object} [data.meta]
  67. * @returns {Promise}
  68. */
  69. add: async (access, data) => {
  70. if (typeof data.user_id === "undefined" || !data.user_id) {
  71. data.user_id = access.token.getUserId(1);
  72. }
  73. if (typeof data.action === "undefined" || !data.action) {
  74. throw new errs.InternalValidationError("Audit log entry must contain an Action");
  75. }
  76. // Make sure at least 1 of the IDs are set and action
  77. return await auditLogModel.query().insert({
  78. user_id: data.user_id,
  79. action: data.action,
  80. object_type: data.object_type || "",
  81. object_id: data.object_id || 0,
  82. meta: data.meta || {},
  83. });
  84. },
  85. };
  86. export default internalAuditLog;