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:
2024-12-03 21:06:22 +07:00
parent 84a9f7dd4f
commit 6f5db20324

View File

@@ -7,7 +7,7 @@ import { ScheduleService } from './schedule.service'
import { AppConfigService } from '../AppConfig/appconfig.service'
import { ScheduleConfigType } from './schedule'
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
import { Role } from '@prisma/client'
@Injectable()
export class ScheduleSchema extends PothosSchema {
constructor(
@@ -262,6 +262,18 @@ export class ScheduleSchema extends PothosSchema {
if (!ctx.http?.me?.id) {
throw new Error('User not found')
}
// use case 1: customer query schedules where customer is participant
if (ctx.http.me.role === Role.CUSTOMER) {
const schedules = await this.prisma.schedule.findMany({
...query,
where: {
customerId: ctx.http.me.id,
},
})
return schedules
}
// 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 } } }],
@@ -273,13 +285,26 @@ export class ScheduleSchema extends PothosSchema {
if (!center) {
throw new Error('Center not found')
}
return await this.prisma.schedule.findMany({
const schedules = await this.prisma.schedule.findMany({
...query,
skip: args.skip ?? undefined,
take: args.take ?? undefined,
orderBy: args.orderBy ?? undefined,
where: args.filter ?? 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
}
},
}),