dataRequests.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import async from "async";
  2. import { isAdminRequired } from "./hooks";
  3. import moduleManager from "../../index";
  4. const DBModule = moduleManager.modules.db;
  5. const UtilsModule = moduleManager.modules.utils;
  6. const WSModule = moduleManager.modules.ws;
  7. const CacheModule = moduleManager.modules.cache;
  8. CacheModule.runJob("SUB", {
  9. channel: "dataRequest.resolve",
  10. cb: dataRequestId => {
  11. WSModule.runJob("EMIT_TO_ROOM", {
  12. room: "admin.users",
  13. args: ["event:admin.dataRequests.resolved", { data: { dataRequestId } }]
  14. });
  15. }
  16. });
  17. export default {
  18. /**
  19. * Gets data requests, used in the admin users page by the AdvancedTable component
  20. *
  21. * @param {object} session - the session object automatically added by the websocket
  22. * @param page - the page
  23. * @param pageSize - the size per page
  24. * @param properties - the properties to return for each data request
  25. * @param sort - the sort object
  26. * @param queries - the queries array
  27. * @param operator - the operator for queries
  28. * @param cb
  29. */
  30. getData: isAdminRequired(async function getSet(session, page, pageSize, properties, sort, queries, operator, cb) {
  31. const dataRequestModel = await DBModule.runJob("GET_MODEL", { modelName: "dataRequest" }, this);
  32. async.waterfall(
  33. [
  34. next => {
  35. const newQueries = queries.map(query => {
  36. const { data, filter, filterType } = query;
  37. const newQuery = {};
  38. if (filterType === "regex") {
  39. newQuery[filter.property] = new RegExp(`${data.slice(1, data.length - 1)}`, "i");
  40. } else if (filterType === "contains") {
  41. newQuery[filter.property] = new RegExp(
  42. `${data.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&")}`,
  43. "i"
  44. );
  45. } else if (filterType === "exact") {
  46. newQuery[filter.property] = data.toString();
  47. } else if (filterType === "datetimeBefore") {
  48. newQuery[filter.property] = { $lte: new Date(data) };
  49. } else if (filterType === "datetimeAfter") {
  50. newQuery[filter.property] = { $gte: new Date(data) };
  51. }
  52. return newQuery;
  53. });
  54. const queryObject = {};
  55. if (newQueries.length > 0) {
  56. if (operator === "and") queryObject.$and = newQueries;
  57. else if (operator === "or") queryObject.$or = newQueries;
  58. else if (operator === "nor") queryObject.$nor = newQueries;
  59. }
  60. next(null, queryObject);
  61. },
  62. (queryObject, next) => {
  63. dataRequestModel.find(queryObject).count((err, count) => {
  64. next(err, queryObject, count);
  65. });
  66. },
  67. (queryObject, count, next) => {
  68. dataRequestModel
  69. .find(queryObject)
  70. .sort(sort)
  71. .skip(pageSize * (page - 1))
  72. .limit(pageSize)
  73. .select(properties.join(" "))
  74. .exec((err, dataRequests) => {
  75. next(err, count, dataRequests);
  76. });
  77. }
  78. ],
  79. async (err, count, dataRequests) => {
  80. if (err && err !== true) {
  81. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  82. this.log("ERROR", "DATA_REQUESTS_GET_DATA", `Failed to get data from data requests. "${err}"`);
  83. return cb({ status: "error", message: err });
  84. }
  85. this.log("SUCCESS", "DATA_REQUESTS_GET_DATA", `Got data from data requests successfully.`);
  86. return cb({
  87. status: "success",
  88. message: "Successfully got data from data requests.",
  89. data: { data: dataRequests, count }
  90. });
  91. }
  92. );
  93. }),
  94. /**
  95. * Resolves a data request
  96. *
  97. * @param {object} session - the session object automatically added by the websocket
  98. * @param {object} dataRequestId - the id of the data request to resolve
  99. * @param {Function} cb - gets called with the result
  100. */
  101. resolve: isAdminRequired(async function update(session, dataRequestId, cb) {
  102. const dataRequestModel = await DBModule.runJob("GET_MODEL", { modelName: "dataRequest" }, this);
  103. async.waterfall(
  104. [
  105. next => {
  106. if (!dataRequestId || typeof dataRequestId !== "string")
  107. return next("Please provide a data request id.");
  108. return next();
  109. },
  110. next => {
  111. dataRequestModel.updateOne({ _id: dataRequestId }, { resolved: true }, { upsert: true }, err =>
  112. next(err)
  113. );
  114. }
  115. ],
  116. async err => {
  117. if (err) {
  118. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  119. this.log(
  120. "ERROR",
  121. "DATA_REQUESTS_RESOLVE",
  122. `Resolving data request ${dataRequestId} failed for user "${session.userId}". "${err}"`
  123. );
  124. return cb({ status: "error", message: err });
  125. }
  126. CacheModule.runJob("PUB", { channel: "dataRequest.resolve", value: dataRequestId });
  127. this.log(
  128. "SUCCESS",
  129. "DATA_REQUESTS_RESOLVE",
  130. `Resolving data request "${dataRequestId}" successful for user ${session.userId}".`
  131. );
  132. return cb({
  133. status: "success",
  134. message: "Successfully resolved data request."
  135. });
  136. }
  137. );
  138. })
  139. };