dataRequests.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // Creates pipeline array
  35. next => next(null, []),
  36. // Adds the match stage to aggregation pipeline, which is responsible for filtering
  37. (pipeline, next) => {
  38. let queryError;
  39. const newQueries = queries.flatMap(query => {
  40. const { data, filter, filterType } = query;
  41. const newQuery = {};
  42. if (filterType === "regex") {
  43. newQuery[filter.property] = new RegExp(`${data.slice(1, data.length - 1)}`, "i");
  44. } else if (filterType === "contains") {
  45. newQuery[filter.property] = new RegExp(
  46. `${data.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&")}`,
  47. "i"
  48. );
  49. } else if (filterType === "exact") {
  50. newQuery[filter.property] = data.toString();
  51. } else if (filterType === "datetimeBefore") {
  52. newQuery[filter.property] = { $lte: new Date(data) };
  53. } else if (filterType === "datetimeAfter") {
  54. newQuery[filter.property] = { $gte: new Date(data) };
  55. } else if (filterType === "numberLesserEqual") {
  56. newQuery[filter.property] = { $lte: Number(data) };
  57. } else if (filterType === "numberLesser") {
  58. newQuery[filter.property] = { $lt: Number(data) };
  59. } else if (filterType === "numberGreater") {
  60. newQuery[filter.property] = { $gt: Number(data) };
  61. } else if (filterType === "numberGreaterEqual") {
  62. newQuery[filter.property] = { $gte: Number(data) };
  63. } else if (filterType === "numberEquals") {
  64. newQuery[filter.property] = { $eq: Number(data) };
  65. } else if (filterType === "boolean") {
  66. newQuery[filter.property] = { $eq: !!data };
  67. }
  68. return newQuery;
  69. });
  70. if (queryError) next(queryError);
  71. const queryObject = {};
  72. if (newQueries.length > 0) {
  73. if (operator === "and") queryObject.$and = newQueries;
  74. else if (operator === "or") queryObject.$or = newQueries;
  75. else if (operator === "nor") queryObject.$nor = newQueries;
  76. }
  77. pipeline.push({ $match: queryObject });
  78. next(null, pipeline);
  79. },
  80. // Adds sort stage to aggregation pipeline if there is at least one column being sorted, responsible for sorting data
  81. (pipeline, next) => {
  82. const newSort = Object.fromEntries(
  83. Object.entries(sort).map(([property, direction]) => [
  84. property,
  85. direction === "ascending" ? 1 : -1
  86. ])
  87. );
  88. if (Object.keys(newSort).length > 0) pipeline.push({ $sort: newSort });
  89. next(null, pipeline);
  90. },
  91. // Adds first project stage to aggregation pipeline, responsible for including only the requested properties
  92. (pipeline, next) => {
  93. pipeline.push({ $project: Object.fromEntries(properties.map(property => [property, 1])) });
  94. next(null, pipeline);
  95. },
  96. // Adds the facet stage to aggregation pipeline, responsible for returning a total document count, skipping and limitting the documents that will be returned
  97. (pipeline, next) => {
  98. pipeline.push({
  99. $facet: {
  100. count: [{ $count: "count" }],
  101. documents: [{ $skip: pageSize * (page - 1) }, { $limit: pageSize }]
  102. }
  103. });
  104. // console.dir(pipeline, { depth: 6 });
  105. next(null, pipeline);
  106. },
  107. // Executes the aggregation pipeline
  108. (pipeline, next) => {
  109. dataRequestModel.aggregate(pipeline).exec((err, result) => {
  110. // console.dir(err);
  111. // console.dir(result, { depth: 6 });
  112. if (err) return next(err);
  113. if (result[0].count.length === 0) return next(null, 0, []);
  114. const { count } = result[0].count[0];
  115. const { documents } = result[0];
  116. // console.log(111, err, result, count, documents[0]);
  117. return next(null, count, documents);
  118. });
  119. }
  120. ],
  121. async (err, count, dataRequests) => {
  122. if (err && err !== true) {
  123. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  124. this.log("ERROR", "DATA_REQUESTS_GET_DATA", `Failed to get data from data requests. "${err}"`);
  125. return cb({ status: "error", message: err });
  126. }
  127. this.log("SUCCESS", "DATA_REQUESTS_GET_DATA", `Got data from data requests successfully.`);
  128. return cb({
  129. status: "success",
  130. message: "Successfully got data from data requests.",
  131. data: { data: dataRequests, count }
  132. });
  133. }
  134. );
  135. }),
  136. /**
  137. * Resolves a data request
  138. *
  139. * @param {object} session - the session object automatically added by the websocket
  140. * @param {object} dataRequestId - the id of the data request to resolve
  141. * @param {Function} cb - gets called with the result
  142. */
  143. resolve: isAdminRequired(async function update(session, dataRequestId, cb) {
  144. const dataRequestModel = await DBModule.runJob("GET_MODEL", { modelName: "dataRequest" }, this);
  145. async.waterfall(
  146. [
  147. next => {
  148. if (!dataRequestId || typeof dataRequestId !== "string")
  149. return next("Please provide a data request id.");
  150. return next();
  151. },
  152. next => {
  153. dataRequestModel.updateOne({ _id: dataRequestId }, { resolved: true }, { upsert: true }, err =>
  154. next(err)
  155. );
  156. }
  157. ],
  158. async err => {
  159. if (err) {
  160. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  161. this.log(
  162. "ERROR",
  163. "DATA_REQUESTS_RESOLVE",
  164. `Resolving data request ${dataRequestId} failed for user "${session.userId}". "${err}"`
  165. );
  166. return cb({ status: "error", message: err });
  167. }
  168. CacheModule.runJob("PUB", { channel: "dataRequest.resolve", value: dataRequestId });
  169. this.log(
  170. "SUCCESS",
  171. "DATA_REQUESTS_RESOLVE",
  172. `Resolving data request "${dataRequestId}" successful for user ${session.userId}".`
  173. );
  174. return cb({
  175. status: "success",
  176. message: "Successfully resolved data request."
  177. });
  178. }
  179. );
  180. })
  181. };