dataRequests.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 all unresolved data requests
  20. *
  21. * @param {object} session - the session object automatically added by the websocket
  22. * @param {Function} cb - gets called with the result
  23. */
  24. index: isAdminRequired(async function index(session, cb) {
  25. const dataRequestModel = await DBModule.runJob("GET_MODEL", { modelName: "dataRequest" }, this);
  26. async.waterfall(
  27. [
  28. next => {
  29. dataRequestModel.find({ resolved: false }).sort({ createdAt: "desc" }).exec(next);
  30. }
  31. ],
  32. async (err, requests) => {
  33. if (err) {
  34. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  35. this.log("ERROR", "DATA_REQUESTS_INDEX", `Indexing data requests failed. "${err}"`);
  36. return cb({ status: "error", message: err });
  37. }
  38. this.log("SUCCESS", "DATA_REQUESTS_INDEX", `Indexing data requests successful.`, false);
  39. return cb({ status: "success", data: { requests } });
  40. }
  41. );
  42. }),
  43. /**
  44. * Resolves a data request
  45. *
  46. * @param {object} session - the session object automatically added by the websocket
  47. * @param {object} dataRequestId - the id of the data request to resolve
  48. * @param {Function} cb - gets called with the result
  49. */
  50. resolve: isAdminRequired(async function update(session, dataRequestId, cb) {
  51. const dataRequestModel = await DBModule.runJob("GET_MODEL", { modelName: "dataRequest" }, this);
  52. async.waterfall(
  53. [
  54. next => {
  55. if (!dataRequestId || typeof dataRequestId !== "string")
  56. return next("Please provide a data request id.");
  57. return next();
  58. },
  59. next => {
  60. dataRequestModel.updateOne({ _id: dataRequestId }, { resolved: true }, { upsert: true }, err =>
  61. next(err)
  62. );
  63. }
  64. ],
  65. async err => {
  66. if (err) {
  67. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  68. this.log(
  69. "ERROR",
  70. "DATA_REQUESTS_RESOLVE",
  71. `Resolving data request ${dataRequestId} failed for user "${session.userId}". "${err}"`
  72. );
  73. return cb({ status: "error", message: err });
  74. }
  75. CacheModule.runJob("PUB", { channel: "dataRequest.resolve", value: dataRequestId });
  76. this.log(
  77. "SUCCESS",
  78. "DATA_REQUESTS_RESOLVE",
  79. `Resolving data request "${dataRequestId}" successful for user ${session.userId}".`
  80. );
  81. return cb({
  82. status: "success",
  83. message: "Successfully resolved data request."
  84. });
  85. }
  86. );
  87. })
  88. };