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.

149 lines
3.7 KiB

11 months ago
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. logEventNew: async function (event, metadata = {}, userId = null,prisma) {
  24. try {
  25. const eventLog = await prisma.event_logs.create({
  26. data: {
  27. event,
  28. metadata: metadata ? JSON.stringify(metadata) : null,
  29. userId: userId ? Number(userId) : null,
  30. occurredAt: new Date(),
  31. },
  32. });
  33. console.log(`\x1b[32m[Event Logged]\x1b[0m - ${event}`);
  34. return { eventLog, message: null };
  35. } catch (error) {
  36. console.error(
  37. `\x1b[31m[Event Logging Failed]\x1b[0m - ${event}`,
  38. error.message
  39. );
  40. return { eventLog: null, message: error.message };
  41. }
  42. },
  43. getByEvent: async function (event, limit = null, orderBy = null) {
  44. try {
  45. const logs = await prisma.event_logs.findMany({
  46. where: { event },
  47. ...(limit !== null ? { take: limit } : {}),
  48. ...(orderBy !== null
  49. ? { orderBy }
  50. : { orderBy: { occurredAt: "desc" } }),
  51. });
  52. return logs;
  53. } catch (error) {
  54. console.error(error.message);
  55. return [];
  56. }
  57. },
  58. getByUserId: async function (userId, limit = null, orderBy = null) {
  59. try {
  60. const logs = await prisma.event_logs.findMany({
  61. where: { userId },
  62. ...(limit !== null ? { take: limit } : {}),
  63. ...(orderBy !== null
  64. ? { orderBy }
  65. : { orderBy: { occurredAt: "desc" } }),
  66. });
  67. return logs;
  68. } catch (error) {
  69. console.error(error.message);
  70. return [];
  71. }
  72. },
  73. where: async function (
  74. clause = {},
  75. limit = null,
  76. orderBy = null,
  77. offset = null
  78. ) {
  79. try {
  80. const logs = await prisma.event_logs.findMany({
  81. where: clause,
  82. ...(limit !== null ? { take: limit } : {}),
  83. ...(offset !== null ? { skip: offset } : {}),
  84. ...(orderBy !== null
  85. ? { orderBy }
  86. : { orderBy: { occurredAt: "desc" } }),
  87. });
  88. return logs;
  89. } catch (error) {
  90. console.error(error.message);
  91. return [];
  92. }
  93. },
  94. whereWithData: async function (
  95. clause = {},
  96. limit = null,
  97. offset = null,
  98. orderBy = null
  99. ) {
  100. const { User } = require("./user");
  101. try {
  102. const results = await this.where(clause, limit, orderBy, offset);
  103. for (const res of results) {
  104. const user = res.userId ? await User.get({ id: res.userId }) : null;
  105. res.user = user
  106. ? { username: user.username }
  107. : { username: "unknown user" };
  108. }
  109. return results;
  110. } catch (error) {
  111. console.error(error.message);
  112. return [];
  113. }
  114. },
  115. count: async function (clause = {}) {
  116. try {
  117. const count = await prisma.event_logs.count({
  118. where: clause,
  119. });
  120. return count;
  121. } catch (error) {
  122. console.error(error.message);
  123. return 0;
  124. }
  125. },
  126. delete: async function (clause = {}) {
  127. try {
  128. await prisma.event_logs.deleteMany({
  129. where: clause,
  130. });
  131. return true;
  132. } catch (error) {
  133. console.error(error.message);
  134. return false;
  135. }
  136. },
  137. };
  138. module.exports = { EventLogs };