dataRequests.js 4.0 KB

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