albums.js 2.8 KB

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