const { InvalidArgumentError } = require('@/modules/shared/errors') /** * NOTE: Stop adding stuff to this service, create specialized service modules instead for various domains * relating to streams. Otherwise we're not only breaking the single responsibility principle, but also * increasing the chances of circular dependencies (which often cause actual errors) since everything relies * on this service. */ module.exports = { /** * Get active user stream favorite date (using dataloader) * @param {Object} p * @param {import('@/modules/shared/index').GraphQLContext} p.ctx * @param {string} p.streamId * @returns {Promise} */ async getActiveUserStreamFavoriteDate({ ctx, streamId }) { if (!ctx.userId) { return null } if (!streamId) { throw new InvalidArgumentError('Invalid stream ID') } return ( (await ctx.loaders.streams.getUserFavoriteData.load(streamId))?.createdAt || null ) }, /** * Get stream favorites count (using dataloader) * @param {Object} p * @param {import('@/modules/shared/index').GraphQLContext} p.ctx * @param {string} p.streamId * @returns {Promise} */ async getStreamFavoritesCount({ ctx, streamId }) { if (!streamId) { throw new InvalidArgumentError('Invalid stream ID') } return (await ctx.loaders.streams.getFavoritesCount.load(streamId)) || 0 }, /** * @param {Object} p * @param {import('@/modules/shared/index').GraphQLContext} p.ctx * @param {string} p.userId * @returns {Promise} */ async getOwnedFavoritesCount({ ctx, userId }) { if (!userId) { throw new InvalidArgumentError('Invalid user ID') } return (await ctx.loaders.streams.getOwnedFavoritesCount.load(userId)) || 0 } }