chore: combine context

This commit is contained in:
2024-12-20 18:30:50 +07:00
parent 461f2653e3
commit 776881f961
23 changed files with 532 additions and 694 deletions

View File

@@ -225,10 +225,7 @@ export class ScheduleSchema extends PothosSchema {
description: 'Retrieve a single schedule by its unique identifier.',
args: this.builder.generator.findUniqueArgs('Schedule'),
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) {
throw new Error('Cannot retrieve schedule in subscription')
}
if (!ctx.http?.me?.id) {
if (!ctx.me) {
throw new Error('User not found')
}
// only return schedule belong to center
@@ -236,7 +233,7 @@ export class ScheduleSchema extends PothosSchema {
const center = await this.prisma.center.findFirst({
where: {
AND: [
{ OR: [{ centerOwnerId: ctx.http.me.id }, { centerMentors: { some: { mentorId: ctx.http.me.id } } }] },
{ OR: [{ centerOwnerId: ctx.me.id }, { centerMentors: { some: { mentorId: ctx.me.id } } }] },
{ centerStatus: CenterStatus.APPROVED },
],
},
@@ -260,14 +257,11 @@ export class ScheduleSchema extends PothosSchema {
args: this.builder.generator.findManyArgs('Schedule'),
description: 'Retrieve a list of schedules with optional filtering, ordering, and pagination.',
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) {
throw new Error('Cannot retrieve schedules in subscription')
}
if (!ctx.http?.me?.id) {
if (!ctx.me) {
throw new Error('User not found')
}
// use case 1: customer query schedules where customer is participant
if (ctx.http.me.role === Role.CUSTOMER) {
if (ctx.me.role === Role.CUSTOMER) {
const schedules = await this.prisma.schedule.findMany({
...query,
orderBy: args.orderBy ?? undefined,
@@ -278,12 +272,12 @@ export class ScheduleSchema extends PothosSchema {
return schedules
}
// use case 2: center mentor or center owner query schedules where center mentor or center owner is mentor
if (ctx.http.me.role === Role.CENTER_MENTOR) {
if (ctx.me.role === Role.CENTER_MENTOR) {
const center = await this.prisma.center.findFirst({
where: {
centerMentors: {
some: {
mentorId: ctx.http.me.id,
mentorId: ctx.me.id,
},
},
},
@@ -299,7 +293,7 @@ export class ScheduleSchema extends PothosSchema {
orderBy: args.orderBy ?? undefined,
where: {
AND: [
{ managedService: { service: { centerId: center.id }, mentorId: ctx.http.me.id } },
{ managedService: { service: { centerId: center.id }, mentorId: ctx.me.id } },
...(args.filter ? [args.filter] : []),
],
},
@@ -307,9 +301,9 @@ export class ScheduleSchema extends PothosSchema {
return schedules
}
// use case 3: Center owner query all schedules belong to center
if (ctx.http.me.role === Role.CENTER_OWNER) {
if (ctx.me.role === Role.CENTER_OWNER) {
const center = await this.prisma.center.findFirst({
where: { centerOwnerId: ctx.http.me.id },
where: { centerOwnerId: ctx.me.id },
})
if (!center) {
throw new Error('Center not found')
@@ -333,10 +327,7 @@ export class ScheduleSchema extends PothosSchema {
description: 'Retrieve a list of schedule dates.',
args: this.builder.generator.findManyArgs('ScheduleDate'),
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) {
throw new Error('Cannot retrieve schedule dates in subscription')
}
if (!ctx.http?.me?.id) {
if (!ctx.me) {
throw new Error('User not found')
}
return await this.prisma.scheduleDate.findMany({
@@ -345,7 +336,7 @@ export class ScheduleSchema extends PothosSchema {
take: args.take ?? undefined,
orderBy: args.orderBy ?? undefined,
where: {
AND: [{ participantIds: { has: ctx.http.me.id } }, ...(args.filter ? [args.filter] : [])],
AND: [{ participantIds: { has: ctx.me.id } }, ...(args.filter ? [args.filter] : [])],
},
})
},
@@ -404,8 +395,8 @@ d72a864e-2f41-45ab-9c9b-bf0512a31883,e9be51fd-2382-4e43-9988-74e76fde4b56,2024-1
}),
},
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) {
throw new Error('Cannot create schedule in subscription')
if (!ctx.me) {
throw new Error('User not found')
}
Logger.log('args.schedule', args.schedule)
// reject schedule if start date is today or in the past