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 {
Pothos,
PothosRef,
PothosSchema,
SchemaBuilderToken,
} from '@smatch-corp/nestjs-pothos'
import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } 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'
@@ -36,8 +31,7 @@ export class WorkshopSubscriptionSchema extends PothosSchema {
}),
createdAt: t.expose('createdAt', {
type: 'DateTime',
description:
'The date and time the workshop subscription was created.',
description: 'The date and time the workshop subscription was created.',
}),
notified: t.exposeBoolean('notified', {
description: 'Whether the user has been notified about the workshop.',
@@ -52,8 +46,7 @@ export class WorkshopSubscriptionSchema extends PothosSchema {
workshopSubscription: t.prismaField({
type: this.workshopSubscription(),
args: this.builder.generator.findUniqueArgs('WorkshopSubscription'),
description:
'Retrieve a single workshop subscription by its unique identifier.',
description: 'Retrieve a single workshop subscription by its unique identifier.',
resolve: async (query, _root, args) => {
return await this.prisma.workshopSubscription.findUnique({
...query,
@@ -64,8 +57,7 @@ export class WorkshopSubscriptionSchema extends PothosSchema {
workshopSubscriptions: t.prismaField({
type: [this.workshopSubscription()],
args: this.builder.generator.findManyArgs('WorkshopSubscription'),
description:
'Retrieve a list of workshop subscriptions with optional filtering, ordering, and pagination.',
description: 'Retrieve a list of workshop subscriptions with optional filtering, ordering, and pagination.',
resolve: async (query, _root, args) => {
return await this.prisma.workshopSubscription.findMany({
...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) => ({
subscribeToWorkshop: t.prismaField({
@@ -96,15 +102,12 @@ export class WorkshopSubscriptionSchema extends PothosSchema {
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')
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({
const existingSubscription = await this.prisma.workshopSubscription.findFirst({
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
return await this.prisma.workshopSubscription.create({
data: {