utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import async from "async";
  2. import { useHasPermission, getUserPermissions } from "../hooks/hasPermission";
  3. // eslint-disable-next-line
  4. import moduleManager from "../../index";
  5. const UtilsModule = moduleManager.modules.utils;
  6. const WSModule = moduleManager.modules.ws;
  7. export default {
  8. getModules: useHasPermission("utils.getModules", function getModules(session, cb) {
  9. async.waterfall(
  10. [
  11. next => {
  12. next(null, UtilsModule.moduleManager.modules);
  13. },
  14. (modules, next) => {
  15. next(
  16. null,
  17. Object.keys(modules).map(moduleName => {
  18. const module = modules[moduleName];
  19. return {
  20. name: module.name,
  21. status: module.status,
  22. stage: module.stage,
  23. jobsInQueue: module.jobQueue.lengthQueue(),
  24. jobsInProgress: module.jobQueue.lengthRunning(),
  25. jobsPaused: module.jobQueue.lengthPaused(),
  26. concurrency: module.jobQueue.concurrency
  27. };
  28. })
  29. );
  30. }
  31. ],
  32. async (err, modules) => {
  33. if (err && err !== true) {
  34. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  35. this.log("ERROR", "GET_MODULES", `User ${session.userId} failed to get modules. '${err}'`);
  36. cb({ status: "error", message: err });
  37. } else {
  38. this.log("SUCCESS", "GET_MODULES", `User ${session.userId} has successfully got the modules info.`);
  39. cb({
  40. status: "success",
  41. message: "Successfully got modules.",
  42. data: { modules }
  43. });
  44. }
  45. }
  46. );
  47. }),
  48. getModule: useHasPermission("utils.getModules", function getModule(session, moduleName, cb) {
  49. async.waterfall(
  50. [
  51. next => {
  52. next(null, UtilsModule.moduleManager.modules[moduleName]);
  53. }
  54. ],
  55. async (err, module) => {
  56. if (err && err !== true) {
  57. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  58. this.log("ERROR", "GET_MODULE", `User ${session.userId} failed to get module. '${err}'`);
  59. cb({ status: "error", message: err });
  60. } else {
  61. this.log("SUCCESS", "GET_MODULE", `User ${session.userId} has successfully got the module info.`);
  62. cb({
  63. status: "success",
  64. message: "Successfully got module info.",
  65. data: {
  66. jobStatistics: module.jobStatistics
  67. }
  68. });
  69. }
  70. }
  71. );
  72. }),
  73. getRooms(session, cb) {
  74. WSModule.runJob("GET_ROOMS_FOR_SOCKET", { socketId: session.socketId })
  75. .then(response => {
  76. this.log("SUCCESS", "GET_ROOMS", `User ${session.userId} has successfully got the module info.`);
  77. cb({
  78. status: "success",
  79. message: "Successfully got rooms.",
  80. data: {
  81. rooms: response
  82. }
  83. });
  84. })
  85. .catch(async err => {
  86. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  87. this.log("ERROR", "GET_ROOMS", `Failed to get rooms. '${err}'`);
  88. cb({ status: "error", message: err });
  89. });
  90. },
  91. /**
  92. * Get permissions
  93. *
  94. * @param {object} session - the session object automatically added by socket.io
  95. * @param {string} stationId - optional, the station id
  96. * @param {Function} cb - gets called with the result
  97. */
  98. async getPermissions(session, stationId, cb) {
  99. const callback = cb || stationId;
  100. async.waterfall(
  101. [
  102. next => {
  103. getUserPermissions(session.userId, cb ? stationId : null)
  104. .then(permissions => {
  105. next(null, permissions);
  106. })
  107. .catch(() => {
  108. next(null, {});
  109. });
  110. }
  111. ],
  112. async (err, permissions) => {
  113. if (err) {
  114. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  115. this.log("ERROR", "GET_PERMISSIONS", `Fetching permissions failed. "${err}"`);
  116. return callback({ status: "error", message: err });
  117. }
  118. this.log("SUCCESS", "GET_PERMISSIONS", "Fetching permissions was successful.");
  119. return callback({ status: "success", data: { permissions } });
  120. }
  121. );
  122. }
  123. };