dataRequests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. async.waterfall(
  32. [
  33. next => {
  34. DBModule.runJob(
  35. "GET_DATA",
  36. {
  37. page,
  38. pageSize,
  39. properties,
  40. sort,
  41. queries,
  42. operator,
  43. modelName: "dataRequest",
  44. blacklistedProperties: [],
  45. specialProperties: {},
  46. specialQueries: {}
  47. },
  48. this
  49. )
  50. .then(response => {
  51. next(null, response);
  52. })
  53. .catch(err => {
  54. next(err);
  55. });
  56. }
  57. ],
  58. async (err, response) => {
  59. if (err && err !== true) {
  60. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  61. this.log("ERROR", "DATA_REQUESTS_GET_DATA", `Failed to get data from data requests. "${err}"`);
  62. return cb({ status: "error", message: err });
  63. }
  64. this.log("SUCCESS", "DATA_REQUESTS_GET_DATA", `Got data from data requests successfully.`);
  65. return cb({
  66. status: "success",
  67. message: "Successfully got data from data requests.",
  68. data: response
  69. });
  70. }
  71. );
  72. }),
  73. /**
  74. * Resolves a data request
  75. *
  76. * @param {object} session - the session object automatically added by the websocket
  77. * @param {object} dataRequestId - the id of the data request to resolve
  78. * @param {Function} cb - gets called with the result
  79. */
  80. resolve: isAdminRequired(async function update(session, dataRequestId, cb) {
  81. const dataRequestModel = await DBModule.runJob("GET_MODEL", { modelName: "dataRequest" }, this);
  82. async.waterfall(
  83. [
  84. next => {
  85. if (!dataRequestId || typeof dataRequestId !== "string")
  86. return next("Please provide a data request id.");
  87. return next();
  88. },
  89. next => {
  90. dataRequestModel.updateOne({ _id: dataRequestId }, { resolved: true }, { upsert: true }, err =>
  91. next(err)
  92. );
  93. }
  94. ],
  95. async err => {
  96. if (err) {
  97. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  98. this.log(
  99. "ERROR",
  100. "DATA_REQUESTS_RESOLVE",
  101. `Resolving data request ${dataRequestId} failed for user "${session.userId}". "${err}"`
  102. );
  103. return cb({ status: "error", message: err });
  104. }
  105. CacheModule.runJob("PUB", { channel: "dataRequest.resolve", value: dataRequestId });
  106. this.log(
  107. "SUCCESS",
  108. "DATA_REQUESTS_RESOLVE",
  109. `Resolving data request "${dataRequestId}" successful for user ${session.userId}".`
  110. );
  111. return cb({
  112. status: "success",
  113. message: "Successfully resolved data request."
  114. });
  115. }
  116. );
  117. })
  118. };