chore: clean up workshop subscription schema and add subscribed workshops field

- Removed unnecessary line breaks in the workshop subscription schema for improved readability.
- Consolidated import statements for better organization.
- Added a new field `subscribedWorkshops` to retrieve a list of workshops the current user is subscribed to, enhancing the schema's functionality.
- Streamlined error handling in the subscription logic for clarity.
This commit is contained in:
2024-12-05 19:56:20 +07:00
parent 9d64a199e2
commit 62662b0256
2 changed files with 24 additions and 21 deletions

View File

@@ -1,10 +1,5 @@
import { Inject, Injectable } from '@nestjs/common' import { Inject, Injectable } from '@nestjs/common'
import { import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
Pothos,
PothosRef,
PothosSchema,
SchemaBuilderToken,
} 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' import { DateTimeUtils } from 'src/common/utils/datetime.utils'
@@ -36,8 +31,7 @@ export class WorkshopSubscriptionSchema extends PothosSchema {
}), }),
createdAt: t.expose('createdAt', { createdAt: t.expose('createdAt', {
type: 'DateTime', type: 'DateTime',
description: description: 'The date and time the workshop subscription was created.',
'The date and time the workshop subscription was created.',
}), }),
notified: t.exposeBoolean('notified', { notified: t.exposeBoolean('notified', {
description: 'Whether the user has been notified about the workshop.', description: 'Whether the user has been notified about the workshop.',
@@ -52,8 +46,7 @@ export class WorkshopSubscriptionSchema extends PothosSchema {
workshopSubscription: t.prismaField({ workshopSubscription: t.prismaField({
type: this.workshopSubscription(), type: this.workshopSubscription(),
args: this.builder.generator.findUniqueArgs('WorkshopSubscription'), args: this.builder.generator.findUniqueArgs('WorkshopSubscription'),
description: description: 'Retrieve a single workshop subscription by its unique identifier.',
'Retrieve a single workshop subscription by its unique identifier.',
resolve: async (query, _root, args) => { resolve: async (query, _root, args) => {
return await this.prisma.workshopSubscription.findUnique({ return await this.prisma.workshopSubscription.findUnique({
...query, ...query,
@@ -64,8 +57,7 @@ export class WorkshopSubscriptionSchema extends PothosSchema {
workshopSubscriptions: t.prismaField({ workshopSubscriptions: t.prismaField({
type: [this.workshopSubscription()], type: [this.workshopSubscription()],
args: this.builder.generator.findManyArgs('WorkshopSubscription'), args: this.builder.generator.findManyArgs('WorkshopSubscription'),
description: description: 'Retrieve a list of workshop subscriptions with optional filtering, ordering, and pagination.',
'Retrieve a list of workshop subscriptions with optional filtering, ordering, and pagination.',
resolve: async (query, _root, args) => { resolve: async (query, _root, args) => {
return await this.prisma.workshopSubscription.findMany({ return await this.prisma.workshopSubscription.findMany({
...query, ...query,
@@ -76,6 +68,20 @@ export class WorkshopSubscriptionSchema extends PothosSchema {
}) })
}, },
}), }),
subscribedWorkshops: t.prismaField({
type: [this.workshopSubscription()],
description: 'Retrieve a list of workshops that the current user is subscribed to.',
resolve: async (query, _root, _args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Workshops cannot be retrieved in subscription context')
if (!ctx.http.me) throw new Error('User is not authenticated')
return await this.prisma.workshopSubscription.findMany({
...query,
where: {
userId: ctx.http.me.id,
},
})
},
}),
})) }))
this.builder.mutationFields((t) => ({ this.builder.mutationFields((t) => ({
subscribeToWorkshop: t.prismaField({ subscribeToWorkshop: t.prismaField({
@@ -96,15 +102,12 @@ export class WorkshopSubscriptionSchema extends PothosSchema {
if (!workshop) throw new Error('Workshop not found') 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 // check if workshop is in the future
if (workshop.date < DateTimeUtils.now().toJSDate()) if (workshop.date < DateTimeUtils.now().toJSDate()) throw new Error('Workshop has already started or is over')
throw new Error('Workshop has already started or is over')
// check if user is already subscribed to the workshop // check if user is already subscribed to the workshop
const existingSubscription = const existingSubscription = await this.prisma.workshopSubscription.findFirst({
await this.prisma.workshopSubscription.findFirst({ where: { userId, workshopId: args.workshopId },
where: { userId, workshopId: args.workshopId }, })
}) if (existingSubscription) throw new Error('User already subscribed to workshop')
if (existingSubscription)
throw new Error('User already subscribed to workshop')
// create the workshop subscription // create the workshop subscription
return await this.prisma.workshopSubscription.create({ return await this.prisma.workshopSubscription.create({
data: { data: {