77 lines
2.3 KiB
JavaScript
77 lines
2.3 KiB
JavaScript
const { StreamAcl } = require('@/modules/core/dbSchema')
|
|
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 = {
|
|
/**
|
|
* @returns {Promise<{role: string, id: string, name: string, company: string, avatar: string}[]>}
|
|
*/
|
|
async getStreamUsers({ streamId }) {
|
|
const query = StreamAcl.knex()
|
|
.columns({ role: 'stream_acl.role' }, 'id', 'name', 'company', 'avatar')
|
|
.select()
|
|
.where({ resourceId: streamId })
|
|
.rightJoin('users', { 'users.id': 'stream_acl.userId' })
|
|
.select('stream_acl.role', 'name', 'id', 'company', 'avatar')
|
|
.orderBy('stream_acl.role')
|
|
|
|
return await query
|
|
},
|
|
|
|
/**
|
|
* 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<string | null>}
|
|
*/
|
|
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<number>}
|
|
*/
|
|
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<number>}
|
|
*/
|
|
async getOwnedFavoritesCount({ ctx, userId }) {
|
|
if (!userId) {
|
|
throw new InvalidArgumentError('Invalid user ID')
|
|
}
|
|
|
|
return (await ctx.loaders.streams.getOwnedFavoritesCount.load(userId)) || 0
|
|
}
|
|
}
|