albums.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. export default {
  10. /**
  11. * Gets album items, used in the admin album page by the AdvancedTable component
  12. * @param {object} session - the session object automatically added by the websocket
  13. * @param page - the page
  14. * @param pageSize - the size per page
  15. * @param properties - the properties to return for each album item
  16. * @param sort - the sort object
  17. * @param queries - the queries array
  18. * @param operator - the operator for queries
  19. * @param cb
  20. */
  21. getData: useHasPermission(
  22. "admin.view.albums",
  23. async function getSet(session, page, pageSize, properties, sort, queries, operator, cb) {
  24. async.waterfall(
  25. [
  26. next => {
  27. DBModule.runJob(
  28. "GET_DATA",
  29. {
  30. page,
  31. pageSize,
  32. properties,
  33. sort,
  34. queries,
  35. operator,
  36. modelName: "albums",
  37. blacklistedProperties: [],
  38. specialProperties: {
  39. createdBy: [
  40. {
  41. $addFields: {
  42. createdByOID: {
  43. $convert: {
  44. input: "$createdBy",
  45. to: "objectId",
  46. onError: "unknown",
  47. onNull: "unknown"
  48. }
  49. }
  50. }
  51. },
  52. {
  53. $lookup: {
  54. from: "users",
  55. localField: "createdByOID",
  56. foreignField: "_id",
  57. as: "createdByUser"
  58. }
  59. },
  60. {
  61. $unwind: {
  62. path: "$createdByUser",
  63. preserveNullAndEmptyArrays: true
  64. }
  65. },
  66. {
  67. $addFields: {
  68. createdByUsername: {
  69. $ifNull: ["$createdByUser.username", "unknown"]
  70. }
  71. }
  72. },
  73. {
  74. $project: {
  75. createdByOID: 0,
  76. createdByUser: 0
  77. }
  78. }
  79. ]
  80. },
  81. specialQueries: {
  82. createdBy: newQuery => ({
  83. $or: [newQuery, { createdByUsername: newQuery.createdBy }]
  84. })
  85. }
  86. },
  87. this
  88. )
  89. .then(response => {
  90. next(null, response);
  91. })
  92. .catch(err => {
  93. next(err);
  94. });
  95. }
  96. ],
  97. async (err, response) => {
  98. if (err && err !== true) {
  99. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  100. this.log("ERROR", "ALBUMS_GET_DATA", `Failed to get data from albums. "${err}"`);
  101. return cb({ status: "error", message: err });
  102. }
  103. this.log("SUCCESS", "ALBUMS_GET_DATA", `Got data from albums successfully.`);
  104. return cb({
  105. status: "success",
  106. message: "Successfully got data from albums.",
  107. data: response
  108. });
  109. }
  110. );
  111. }
  112. ),
  113. };