enhance subcribe to workshop

This commit is contained in:
2024-11-25 23:06:01 +07:00
parent 547947eeb8
commit 2b70b018f9

View File

@@ -7,6 +7,7 @@ import {
} from '@smatch-corp/nestjs-pothos' } from '@smatch-corp/nestjs-pothos'
import { Builder } from '../Graphql/graphql.builder' import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service' import { PrismaService } from '../Prisma/prisma.service'
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
@Injectable() @Injectable()
export class WorkshopSubscriptionSchema extends PothosSchema { export class WorkshopSubscriptionSchema extends PothosSchema {
@@ -88,7 +89,23 @@ export class WorkshopSubscriptionSchema extends PothosSchema {
resolve: async (_query, _root, args, ctx) => { resolve: async (_query, _root, args, ctx) => {
if (ctx.isSubscription) throw new Error('Not allowed in subscription') if (ctx.isSubscription) throw new Error('Not allowed in subscription')
const userId = ctx.http.me?.id 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') 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({ return await this.prisma.workshopSubscription.create({
data: { data: {
userId, userId,