enhance subcribe to workshop
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user