dataRequests.js 4.5 KB

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