dataRequests.js 3.9 KB

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