You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.1 KiB

11 months ago
  1. const prisma = require("../utils/prisma");
  2. const EventLogs = {
  3. logEvent: async function (event, metadata = {}, userId = null) {
  4. try {
  5. const eventLog = await prisma.event_logs.create({
  6. data: {
  7. event,
  8. metadata: metadata ? JSON.stringify(metadata) : null,
  9. userId: userId ? Number(userId) : null,
  10. occurredAt: new Date(),
  11. },
  12. });
  13. console.log(`\x1b[32m[Event Logged]\x1b[0m - ${event}`);
  14. return { eventLog, message: null };
  15. } catch (error) {
  16. console.error(
  17. `\x1b[31m[Event Logging Failed]\x1b[0m - ${event}`,
  18. error.message
  19. );
  20. return { eventLog: null, message: error.message };
  21. }
  22. },
  23. getByEvent: async function (event, limit = null, orderBy = null) {
  24. try {
  25. const logs = await prisma.event_logs.findMany({
  26. where: { event },
  27. ...(limit !== null ? { take: limit } : {}),
  28. ...(orderBy !== null
  29. ? { orderBy }
  30. : { orderBy: { occurredAt: "desc" } }),
  31. });
  32. return logs;
  33. } catch (error) {
  34. console.error(error.message);
  35. return [];
  36. }
  37. },
  38. getByUserId: async function (userId, limit = null, orderBy = null) {
  39. try {
  40. const logs = await prisma.event_logs.findMany({
  41. where: { userId },
  42. ...(limit !== null ? { take: limit } : {}),
  43. ...(orderBy !== null
  44. ? { orderBy }
  45. : { orderBy: { occurredAt: "desc" } }),
  46. });
  47. return logs;
  48. } catch (error) {
  49. console.error(error.message);
  50. return [];
  51. }
  52. },
  53. where: async function (
  54. clause = {},
  55. limit = null,
  56. orderBy = null,
  57. offset = null
  58. ) {
  59. try {
  60. const logs = await prisma.event_logs.findMany({
  61. where: clause,
  62. ...(limit !== null ? { take: limit } : {}),
  63. ...(offset !== null ? { skip: offset } : {}),
  64. ...(orderBy !== null
  65. ? { orderBy }
  66. : { orderBy: { occurredAt: "desc" } }),
  67. });
  68. return logs;
  69. } catch (error) {
  70. console.error(error.message);
  71. return [];
  72. }
  73. },
  74. whereWithData: async function (
  75. clause = {},
  76. limit = null,
  77. offset = null,
  78. orderBy = null
  79. ) {
  80. const { User } = require("./user");
  81. try {
  82. const results = await this.where(clause, limit, orderBy, offset);
  83. for (const res of results) {
  84. const user = res.userId ? await User.get({ id: res.userId }) : null;
  85. res.user = user
  86. ? { username: user.username }
  87. : { username: "unknown user" };
  88. }
  89. return results;
  90. } catch (error) {
  91. console.error(error.message);
  92. return [];
  93. }
  94. },
  95. count: async function (clause = {}) {
  96. try {
  97. const count = await prisma.event_logs.count({
  98. where: clause,
  99. });
  100. return count;
  101. } catch (error) {
  102. console.error(error.message);
  103. return 0;
  104. }
  105. },
  106. delete: async function (clause = {}) {
  107. try {
  108. await prisma.event_logs.deleteMany({
  109. where: clause,
  110. });
  111. return true;
  112. } catch (error) {
  113. console.error(error.message);
  114. return false;
  115. }
  116. },
  117. };
  118. module.exports = { EventLogs };