import async from "async"; import { useHasPermission } from "../hooks/hasPermission"; // eslint-disable-next-line import moduleManager from "../../index"; const DBModule = moduleManager.modules.db; const UtilsModule = moduleManager.modules.utils; const MusicBrainzModule = moduleManager.modules.musicbrainz; export default { /** * Gets album items, used in the admin album page by the AdvancedTable component * @param {object} session - the session object automatically added by the websocket * @param page - the page * @param pageSize - the size per page * @param properties - the properties to return for each album item * @param sort - the sort object * @param queries - the queries array * @param operator - the operator for queries * @param cb */ getData: useHasPermission( "admin.view.albums", async function getSet(session, page, pageSize, properties, sort, queries, operator, cb) { async.waterfall( [ next => { DBModule.runJob( "GET_DATA", { page, pageSize, properties, sort, queries, operator, modelName: "albums", blacklistedProperties: [], specialProperties: { createdBy: [ { $addFields: { createdByOID: { $convert: { input: "$createdBy", to: "objectId", onError: "unknown", onNull: "unknown" } } } }, { $lookup: { from: "users", localField: "createdByOID", foreignField: "_id", as: "createdByUser" } }, { $unwind: { path: "$createdByUser", preserveNullAndEmptyArrays: true } }, { $addFields: { createdByUsername: { $ifNull: ["$createdByUser.username", "unknown"] } } }, { $project: { createdByOID: 0, createdByUser: 0 } } ] }, specialQueries: { createdBy: newQuery => ({ $or: [newQuery, { createdByUsername: newQuery.createdBy }] }) } }, this ) .then(response => { next(null, response); }) .catch(err => { next(err); }); } ], async (err, response) => { if (err && err !== true) { err = await UtilsModule.runJob("GET_ERROR", { error: err }, this); this.log("ERROR", "ALBUMS_GET_DATA", `Failed to get data from albums. "${err}"`); return cb({ status: "error", message: err }); } this.log("SUCCESS", "ALBUMS_GET_DATA", `Got data from albums successfully.`); return cb({ status: "success", message: "Successfully got data from albums.", data: response }); } ); } ), /** * Gets MusicBrainz recordings release release groups data * @param session * @param artistId - the MusicBrainz artist id * @param {Function} cb */ getMusicBrainzRecordingsReleasesReleaseGroups: useHasPermission( "apis.searchDiscogs", async function getMusicBrainzRecordingsReleasesReleaseGroups(session, artistId, cb) { // TODO update permission const response = await MusicBrainzModule.runJob( "GET_RECORDINGS_RELEASES_RELEASE_GROUPS", { artistId }, this ); this.log( "SUCCESS", "ALBUMS_GET_MUSICBRAINZ_RECORDINGS_RELEASES_RELEASE_GROUPS", `User "${session.userId}" got MusicBrainz recordings releases release groups for artist "${artistId}".` ); return cb({ status: "success", data: response }); } ) };