dataRequests.js 4.0 KB

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