albums.js 3.7 KB

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