dataRequests.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. const newQueries = queries.map(query => {
  33. const { data, filter, filterType } = query;
  34. const newQuery = {};
  35. if (filterType === "regex") {
  36. newQuery[filter.property] = new RegExp(`${data.slice(1, data.length - 1)}`, "i");
  37. } else if (filterType === "contains") {
  38. newQuery[filter.property] = new RegExp(`${data.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, "i");
  39. } else if (filterType === "exact") {
  40. newQuery[filter.property] = data.toString();
  41. }
  42. return newQuery;
  43. });
  44. const queryObject = {};
  45. if (newQueries.length > 0) {
  46. if (operator === "and") queryObject.$and = newQueries;
  47. else if (operator === "or") queryObject.$or = newQueries;
  48. else if (operator === "nor") queryObject.$nor = newQueries;
  49. }
  50. async.waterfall(
  51. [
  52. next => {
  53. dataRequestModel.find(queryObject).count((err, count) => {
  54. next(err, count);
  55. });
  56. },
  57. (count, next) => {
  58. dataRequestModel
  59. .find(queryObject)
  60. .sort(sort)
  61. .skip(pageSize * (page - 1))
  62. .limit(pageSize)
  63. .select(properties.join(" "))
  64. .exec((err, dataRequests) => {
  65. next(err, count, dataRequests);
  66. });
  67. }
  68. ],
  69. async (err, count, dataRequests) => {
  70. if (err && err !== true) {
  71. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  72. this.log("ERROR", "DATA_REQUESTS_GET_DATA", `Failed to get data from data requests. "${err}"`);
  73. return cb({ status: "error", message: err });
  74. }
  75. this.log("SUCCESS", "DATA_REQUESTS_GET_DATA", `Got data from data requests successfully.`);
  76. return cb({
  77. status: "success",
  78. message: "Successfully got data from data requests.",
  79. data: { data: dataRequests, count }
  80. });
  81. }
  82. );
  83. }),
  84. /**
  85. * Resolves a data request
  86. *
  87. * @param {object} session - the session object automatically added by the websocket
  88. * @param {object} dataRequestId - the id of the data request to resolve
  89. * @param {Function} cb - gets called with the result
  90. */
  91. resolve: isAdminRequired(async function update(session, dataRequestId, cb) {
  92. const dataRequestModel = await DBModule.runJob("GET_MODEL", { modelName: "dataRequest" }, this);
  93. async.waterfall(
  94. [
  95. next => {
  96. if (!dataRequestId || typeof dataRequestId !== "string")
  97. return next("Please provide a data request id.");
  98. return next();
  99. },
  100. next => {
  101. dataRequestModel.updateOne({ _id: dataRequestId }, { resolved: true }, { upsert: true }, err =>
  102. next(err)
  103. );
  104. }
  105. ],
  106. async err => {
  107. if (err) {
  108. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  109. this.log(
  110. "ERROR",
  111. "DATA_REQUESTS_RESOLVE",
  112. `Resolving data request ${dataRequestId} failed for user "${session.userId}". "${err}"`
  113. );
  114. return cb({ status: "error", message: err });
  115. }
  116. CacheModule.runJob("PUB", { channel: "dataRequest.resolve", value: dataRequestId });
  117. this.log(
  118. "SUCCESS",
  119. "DATA_REQUESTS_RESOLVE",
  120. `Resolving data request "${dataRequestId}" successful for user ${session.userId}".`
  121. );
  122. return cb({
  123. status: "success",
  124. message: "Successfully resolved data request."
  125. });
  126. }
  127. );
  128. })
  129. };