feat: enhance schedule querying logic based on user roles in ScheduleSchema
- Updated ScheduleSchema to implement role-based querying for schedules. - Added logic to handle different user roles: CUSTOMER, CENTER_MENTOR, and CENTER_OWNER. - Improved error handling for cases where the center is not found for mentors and owners. - This change enhances the flexibility and usability of the schedule querying functionality.
This commit is contained in:
@@ -7,7 +7,7 @@ import { ScheduleService } from './schedule.service'
|
|||||||
import { AppConfigService } from '../AppConfig/appconfig.service'
|
import { AppConfigService } from '../AppConfig/appconfig.service'
|
||||||
import { ScheduleConfigType } from './schedule'
|
import { ScheduleConfigType } from './schedule'
|
||||||
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
|
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
|
||||||
|
import { Role } from '@prisma/client'
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ScheduleSchema extends PothosSchema {
|
export class ScheduleSchema extends PothosSchema {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -262,24 +262,49 @@ export class ScheduleSchema extends PothosSchema {
|
|||||||
if (!ctx.http?.me?.id) {
|
if (!ctx.http?.me?.id) {
|
||||||
throw new Error('User not found')
|
throw new Error('User not found')
|
||||||
}
|
}
|
||||||
const center = await this.prisma.center.findFirst({
|
// use case 1: customer query schedules where customer is participant
|
||||||
where: {
|
if (ctx.http.me.role === Role.CUSTOMER) {
|
||||||
OR: [{ centerOwnerId: ctx.http.me.id }, { centerMentors: { some: { mentorId: ctx.http.me.id } } }],
|
const schedules = await this.prisma.schedule.findMany({
|
||||||
},
|
...query,
|
||||||
include: {
|
where: {
|
||||||
centerMentors: true,
|
customerId: ctx.http.me.id,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if (!center) {
|
return schedules
|
||||||
throw new Error('Center not found')
|
}
|
||||||
|
// use case 2: center mentor or center owner query schedules where center mentor or center owner is mentor
|
||||||
|
else if (ctx.http.me.role === Role.CENTER_MENTOR || ctx.http.me.role === Role.CENTER_OWNER) {
|
||||||
|
const center = await this.prisma.center.findFirst({
|
||||||
|
where: {
|
||||||
|
OR: [{ centerOwnerId: ctx.http.me.id }, { centerMentors: { some: { mentorId: ctx.http.me.id } } }],
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
centerMentors: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if (!center) {
|
||||||
|
throw new Error('Center not found')
|
||||||
|
}
|
||||||
|
const schedules = await this.prisma.schedule.findMany({
|
||||||
|
...query,
|
||||||
|
skip: args.skip ?? undefined,
|
||||||
|
take: args.take ?? undefined,
|
||||||
|
orderBy: args.orderBy ?? undefined,
|
||||||
|
where: {
|
||||||
|
AND: [
|
||||||
|
{ managedService: { service: { centerId: center.id } } },
|
||||||
|
{
|
||||||
|
OR: [
|
||||||
|
{ customerId: ctx.http.me.id },
|
||||||
|
{ dates: { some: { participantIds: { has: ctx.http.me.id } } } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
...(args.filter ? [args.filter] : []),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return schedules
|
||||||
}
|
}
|
||||||
return await this.prisma.schedule.findMany({
|
|
||||||
...query,
|
|
||||||
skip: args.skip ?? undefined,
|
|
||||||
take: args.take ?? undefined,
|
|
||||||
orderBy: args.orderBy ?? undefined,
|
|
||||||
where: args.filter ?? undefined,
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user