dataRequests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import async from "async";
  2. import { isAdminRequired } from "./hooks";
  3. // eslint-disable-next-line
  4. import moduleManager from "../../index";
  5. const DBModule = moduleManager.modules.db;
  6. const UtilsModule = moduleManager.modules.utils;
  7. const WSModule = moduleManager.modules.ws;
  8. const CacheModule = moduleManager.modules.cache;
  9. CacheModule.runJob("SUB", {
  10. channel: "dataRequest.resolve",
  11. cb: dataRequestId => {
  12. WSModule.runJob("EMIT_TO_ROOM", {
  13. room: "admin.users",
  14. args: ["event:admin.dataRequests.resolved", { data: { dataRequestId } }]
  15. });
  16. }
  17. });
  18. export default {
  19. /**
  20. * Gets data requests, used in the admin users page by the AdvancedTable component
  21. *
  22. * @param {object} session - the session object automatically added by the websocket
  23. * @param page - the page
  24. * @param pageSize - the size per page
  25. * @param properties - the properties to return for each data request
  26. * @param sort - the sort object
  27. * @param queries - the queries array
  28. * @param operator - the operator for queries
  29. * @param cb
  30. */
  31. getData: isAdminRequired(async function getSet(session, page, pageSize, properties, sort, queries, operator, cb) {
  32. async.waterfall(
  33. [
  34. next => {
  35. DBModule.runJob(
  36. "GET_DATA",
  37. {
  38. page,
  39. pageSize,
  40. properties,
  41. sort,
  42. queries,
  43. operator,
  44. modelName: "dataRequest",
  45. blacklistedProperties: [],
  46. specialProperties: {},
  47. specialQueries: {}
  48. },
  49. this
  50. )
  51. .then(response => {
  52. next(null, response);
  53. })
  54. .catch(err => {
  55. next(err);
  56. });
  57. }
  58. ],
  59. async (err, response) => {
  60. if (err && err !== true) {
  61. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  62. this.log("ERROR", "DATA_REQUESTS_GET_DATA", `Failed to get data from data requests. "${err}"`);
  63. return cb({ status: "error", message: err });
  64. }
  65. this.log("SUCCESS", "DATA_REQUESTS_GET_DATA", `Got data from data requests successfully.`);
  66. return cb({
  67. status: "success",
  68. message: "Successfully got data from data requests.",
  69. data: response
  70. });
  71. }
  72. );
  73. }),
  74. /**
  75. * Resolves a data request
  76. *
  77. * @param {object} session - the session object automatically added by the websocket
  78. * @param {object} dataRequestId - the id of the data request to resolve
  79. * @param {Function} cb - gets called with the result
  80. */
  81. resolve: isAdminRequired(async function update(session, dataRequestId, cb) {
  82. const dataRequestModel = await DBModule.runJob("GET_MODEL", { modelName: "dataRequest" }, this);
  83. async.waterfall(
  84. [
  85. next => {
  86. if (!dataRequestId || typeof dataRequestId !== "string")
  87. return next("Please provide a data request id.");
  88. return next();
  89. },
  90. next => {
  91. dataRequestModel.updateOne({ _id: dataRequestId }, { resolved: true }, { upsert: true }, err =>
  92. next(err)
  93. );
  94. }
  95. ],
  96. async err => {
  97. if (err) {
  98. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  99. this.log(
  100. "ERROR",
  101. "DATA_REQUESTS_RESOLVE",
  102. `Resolving data request ${dataRequestId} failed for user "${session.userId}". "${err}"`
  103. );
  104. return cb({ status: "error", message: err });
  105. }
  106. CacheModule.runJob("PUB", { channel: "dataRequest.resolve", value: dataRequestId });
  107. this.log(
  108. "SUCCESS",
  109. "DATA_REQUESTS_RESOLVE",
  110. `Resolving data request "${dataRequestId}" successful for user ${session.userId}".`
  111. );
  112. return cb({
  113. status: "success",
  114. message: "Successfully resolved data request."
  115. });
  116. }
  117. );
  118. })
  119. };