Files
epess-web-backend/src/Workshop/workshop.schema.ts

173 lines
6.0 KiB
TypeScript

import { Inject, Injectable } from '@nestjs/common'
import { Pothos, PothosRef, PothosSchema, SchemaBuilderToken } from '@smatch-corp/nestjs-pothos'
import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
import { MinioService } from 'src/Minio/minio.service'
import { Role } from '@prisma/client'
@Injectable()
export class WorkshopSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
private readonly minioService: MinioService,
) {
super()
}
@PothosRef()
workshop() {
return this.builder.prismaObject('Workshop', {
description: 'A workshop in the system.',
fields: (t) => ({
id: t.exposeID('id', {
description: 'The ID of the workshop.',
}),
title: t.exposeString('title', {
description: 'The title of the workshop.',
}),
description: t.exposeString('description', {
description: 'The description of the workshop.',
}),
mentorId: t.exposeID('mentorId', {
description: 'The ID of the mentor who is leading the workshop.',
}),
serviceId: t.exposeID('serviceId', {
description: 'The ID of the service that the workshop is for.',
}),
imageFile: t.relation('imageFile', {
nullable: true,
}),
duration: t.exposeInt('duration', {
description: 'The duration of the workshop in minutes.',
}),
imageFileId: t.exposeID('imageFileId', {
description: 'The ID of the image file for the workshop.',
}),
date: t.expose('date', {
type: 'DateTime',
nullable: true,
description: 'The date and time the workshop is scheduled.',
}),
createdAt: t.expose('createdAt', {
type: 'DateTime',
nullable: true,
description: 'The date and time the workshop was created.',
}),
updatedAt: t.expose('updatedAt', {
type: 'DateTime',
nullable: true,
description: 'The date and time the workshop was updated.',
}),
service: t.relation('service', {
description: 'The service that the workshop is for.',
}),
organization: t.relation('organization', {
description: 'The organization that the workshop is for.',
}),
subscription: t.relation('subscription', {
description: 'The subscription that the workshop is for.',
}),
mentor: t.relation('mentor', {
description: 'The mentor who is leading the workshop.',
}),
meetingRoom: t.relation('workshopMeetingRoom', {
nullable: true,
description: 'The meeting room that the workshop is for.',
}),
}),
})
}
@Pothos()
init(): void {
this.builder.queryFields((t) => ({
workshop: t.prismaField({
type: this.workshop(),
args: this.builder.generator.findUniqueArgs('Workshop'),
description: 'Retrieve a single workshop by its unique identifier.',
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.workshop.findUnique({
...query,
where: args.where,
})
},
}),
workshops: t.prismaField({
type: [this.workshop()],
args: this.builder.generator.findManyArgs('Workshop'),
description: 'Retrieve a list of workshops with optional filtering, ordering, and pagination.',
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.workshop.findMany({
...query,
skip: args.skip ?? undefined,
take: args.take ?? undefined,
orderBy: args.orderBy ?? undefined,
where: args.filter ?? undefined,
})
},
}),
}))
// Mutations section
this.builder.mutationFields((t) => ({
createWorkshop: t.prismaField({
type: this.workshop(),
args: {
input: t.arg({
type: this.builder.generator.getCreateInput('Workshop', ['workshopMeetingRoom']),
required: true,
}),
},
description: 'Create a new workshop.',
resolve: async (query, _root, args, ctx, _info) => {
if (ctx.isSubscription) throw new Error('Workshops cannot be created in subscription context')
if (!ctx.http.me) throw new Error('User is not authenticated to create a workshop')
if (ctx.http.me.role !== Role.CENTER_OWNER) throw new Error('Only center owners can create workshops')
if (!args.input.service.connect) throw new Error('Service is required to create a workshop')
// check if service is active
const service = await this.prisma.service.findUnique({
where: { id: args.input.service.connect.id },
})
if (!service || !service.isActive) throw new Error('Service is not active')
const workshop = await this.prisma.workshop.create({
...query,
data: args.input,
})
// create workshop meeting room
await this.prisma.workshopMeetingRoom.create({
data: {
workshopId: workshop.id,
},
})
return workshop
},
}),
updateWorkshop: t.prismaField({
type: this.workshop(),
args: {
input: t.arg({
type: this.builder.generator.getUpdateInput('Workshop'),
required: true,
}),
where: t.arg({
type: this.builder.generator.getWhereUnique('Workshop'),
required: true,
}),
},
description: 'Update an existing workshop.',
resolve: async (query, _root, args, _ctx, _info) => {
return await this.prisma.workshop.update({
...query,
where: args.where,
data: args.input,
})
},
}),
}))
}
}