From 2b70b018f951aa867374f486b89b93148f302db4 Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Mon, 25 Nov 2024 23:06:01 +0700 Subject: [PATCH] enhance subcribe to workshop --- .../workshopsubscription.schema.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/WorkshopSubscription/workshopsubscription.schema.ts b/src/WorkshopSubscription/workshopsubscription.schema.ts index 7aca7a9..f5d100a 100644 --- a/src/WorkshopSubscription/workshopsubscription.schema.ts +++ b/src/WorkshopSubscription/workshopsubscription.schema.ts @@ -7,6 +7,7 @@ import { } from '@smatch-corp/nestjs-pothos' import { Builder } from '../Graphql/graphql.builder' import { PrismaService } from '../Prisma/prisma.service' +import { DateTimeUtils } from 'src/common/utils/datetime.utils' @Injectable() export class WorkshopSubscriptionSchema extends PothosSchema { @@ -88,7 +89,23 @@ export class WorkshopSubscriptionSchema extends PothosSchema { resolve: async (_query, _root, args, ctx) => { if (ctx.isSubscription) throw new Error('Not allowed in subscription') const userId = ctx.http.me?.id + // retrieve the workshop + const workshop = await this.prisma.workshop.findUnique({ + where: { id: args.workshopId }, + }) + if (!workshop) throw new Error('Workshop not found') if (!userId) throw new Error('User not authenticated') + // check if workshop is in the future + if (workshop.date < DateTimeUtils.now().toJSDate()) + throw new Error('Workshop has already started or is over') + // check if user is already subscribed to the workshop + const existingSubscription = + await this.prisma.workshopSubscription.findFirst({ + where: { userId, workshopId: args.workshopId }, + }) + if (existingSubscription) + throw new Error('User already subscribed to workshop') + // create the workshop subscription return await this.prisma.workshopSubscription.create({ data: { userId,