From 5c836323b0ec040a5ed63f3ce82e00f30422dba2 Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Mon, 7 Oct 2024 20:46:20 +0700 Subject: [PATCH] troi mua qua --- src/center/center.module.ts | 9 +++ src/center/center.schema.ts | 60 +++++++++++++++++++ src/centerstaff/centerstaff.module.ts | 9 +++ src/centerstaff/centerstaff.schema.ts | 34 +++++++++++ src/chatroom/chatroom.module.ts | 9 +++ src/chatroom/chatroom.schema.ts | 28 +++++++++ src/graphql/graphql.module.ts | 16 +++++ src/middlewares/graphql.middleware.ts | 11 ++++ src/resume/resume.module.ts | 9 +++ src/resume/resume.schema.ts | 40 +++++++++++++ src/service/service.module.ts | 9 ++- src/service/service.schema.ts | 30 ++++++++++ src/user/user.schema.ts | 17 ++++++ src/workshop/workshop.module.ts | 9 +++ src/workshop/workshop.schema.ts | 45 ++++++++++++++ .../workshoporganization.module.ts | 9 +++ .../workshoporganization.schema.ts | 30 ++++++++++ .../workshopsubscription.module.ts | 9 +++ .../workshopsubscription.schema.ts | 30 ++++++++++ 19 files changed, 411 insertions(+), 2 deletions(-) create mode 100644 src/center/center.module.ts create mode 100644 src/center/center.schema.ts create mode 100644 src/centerstaff/centerstaff.module.ts create mode 100644 src/centerstaff/centerstaff.schema.ts create mode 100644 src/chatroom/chatroom.module.ts create mode 100644 src/chatroom/chatroom.schema.ts create mode 100644 src/resume/resume.module.ts create mode 100644 src/resume/resume.schema.ts create mode 100644 src/service/service.schema.ts create mode 100644 src/workshop/workshop.module.ts create mode 100644 src/workshop/workshop.schema.ts create mode 100644 src/workshoporganization/workshoporganization.module.ts create mode 100644 src/workshoporganization/workshoporganization.schema.ts create mode 100644 src/workshopsubscription/workshopsubscription.module.ts create mode 100644 src/workshopsubscription/workshopsubscription.schema.ts diff --git a/src/center/center.module.ts b/src/center/center.module.ts new file mode 100644 index 0000000..330fab8 --- /dev/null +++ b/src/center/center.module.ts @@ -0,0 +1,9 @@ +import { Global, Module } from '@nestjs/common'; +import { CenterSchema } from './center.schema'; + +@Global() +@Module({ + providers: [CenterSchema], + exports: [CenterSchema], +}) +export class CenterModule {} diff --git a/src/center/center.schema.ts b/src/center/center.schema.ts new file mode 100644 index 0000000..7e0494d --- /dev/null +++ b/src/center/center.schema.ts @@ -0,0 +1,60 @@ +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 'src/prisma/prisma.service'; + +@Injectable() +export class CenterSchema extends PothosSchema { + constructor( + @Inject(SchemaBuilderToken) private readonly builder: Builder, + private readonly prisma: PrismaService, + ) { + super(); + } + + @PothosRef() + center() { + return this.builder.prismaObject('Center', { + fields: (t) => ({ + id: t.exposeID('id'), + centerOwnerId: t.exposeID('centerOwnerId'), + name: t.exposeString('name'), + description: t.exposeString('description'), + location: t.exposeString('location'), + individual: t.exposeBoolean('individual'), + createdAt: t.expose('createdAt', { type: 'Date' }), + updatedAt: t.expose('updatedAt', { type: 'Date' }), + services: t.relation('services'), + centerOwner: t.relation('centerOwner'), + chatRoom: t.relation('chatRoom'), + centerStaff: t.relation('CenterStaff'), + resume: t.relation('Resume'), + }), + }); + } + + @Pothos() + init(): void { + this.builder.queryFields((t) => ({ + centers: t.prismaField({ + type: [this.center()], + args: { + skip: t.arg.int(), + take: t.arg.int(), + }, + resolve: async (query, root, args, ctx, info) => { + const { skip, take } = args; + return await this.prisma.center.findMany({ + skip: skip ?? 0, + take: take ?? 10, + }); + }, + }), + })); + } +} diff --git a/src/centerstaff/centerstaff.module.ts b/src/centerstaff/centerstaff.module.ts new file mode 100644 index 0000000..70d72ea --- /dev/null +++ b/src/centerstaff/centerstaff.module.ts @@ -0,0 +1,9 @@ +import { Global, Module } from '@nestjs/common'; +import { CenterStaffSchema } from './centerstaff.schema'; + +@Global() +@Module({ + providers: [CenterStaffSchema], + exports: [CenterStaffSchema], +}) +export class CenterStaffModule {} diff --git a/src/centerstaff/centerstaff.schema.ts b/src/centerstaff/centerstaff.schema.ts new file mode 100644 index 0000000..90a653f --- /dev/null +++ b/src/centerstaff/centerstaff.schema.ts @@ -0,0 +1,34 @@ +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 'src/prisma/prisma.service'; + +@Injectable() +export class CenterStaffSchema extends PothosSchema { + constructor( + @Inject(SchemaBuilderToken) private readonly builder: Builder, + private readonly prisma: PrismaService, + ) { + super(); + } + + @PothosRef() + centerStaff() { + return this.builder.prismaObject('CenterStaff', { + fields: (t) => ({ + staffId: t.exposeID('staffId'), + centerId: t.exposeID('centerId'), + serviceId: t.exposeID('serviceId'), + staff: t.relation('staff'), + center: t.relation('center'), + service: t.relation('service'), + createdWorkshop: t.relation('createdWorkshop'), + }), + }); + } +} diff --git a/src/chatroom/chatroom.module.ts b/src/chatroom/chatroom.module.ts new file mode 100644 index 0000000..0a8fad2 --- /dev/null +++ b/src/chatroom/chatroom.module.ts @@ -0,0 +1,9 @@ +import { Global, Module } from '@nestjs/common'; +import { ChatroomSchema } from './chatroom.schema'; + +@Global() +@Module({ + providers: [ChatroomSchema], + exports: [ChatroomSchema], +}) +export class ChatroomModule {} diff --git a/src/chatroom/chatroom.schema.ts b/src/chatroom/chatroom.schema.ts new file mode 100644 index 0000000..555b198 --- /dev/null +++ b/src/chatroom/chatroom.schema.ts @@ -0,0 +1,28 @@ +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 'src/prisma/prisma.service'; + +@Injectable() +export class ChatroomSchema extends PothosSchema { + constructor( + @Inject(SchemaBuilderToken) private readonly builder: Builder, + private readonly prisma: PrismaService, + ) { + super(); + } + + @PothosRef() + chatRoom() { + return this.builder.prismaObject('ChatRoom', { + fields: (t) => ({ + id: t.exposeID('id'), + }), + }); + } +} diff --git a/src/graphql/graphql.module.ts b/src/graphql/graphql.module.ts index 6d5cc0b..0b97427 100644 --- a/src/graphql/graphql.module.ts +++ b/src/graphql/graphql.module.ts @@ -8,11 +8,27 @@ import { PrismaService } from '../prisma/prisma.service'; import { GraphQLValidationMiddleware } from 'src/middlewares/graphql.middleware'; import { PrismaModule } from 'src/prisma/prisma.module'; import { UserModule } from 'src/user/user.module'; +import { CenterModule } from 'src/center/center.module'; import { GraphqlService } from './graphql.service'; +import { ServiceModule } from 'src/service/service.module'; +import { ChatroomModule } from 'src/chatroom/chatroom.module'; +import { CenterStaffModule } from 'src/centerstaff/centerstaff.module'; +import { ResumeModule } from 'src/resume/resume.module'; +import { WorkshopModule } from 'src/workshop/workshop.module'; +import { WorkshopOrganizationModule } from 'src/workshoporganization/workshoporganization.module'; +import { WorkshopSubscriptionModule } from 'src/workshopsubscription/workshopsubscription.module'; @Module({ imports: [ PrismaModule, UserModule, + CenterModule, + ServiceModule, + ChatroomModule, + CenterStaffModule, + ResumeModule, + WorkshopModule, + WorkshopOrganizationModule, + WorkshopSubscriptionModule, PothosModule.forRoot({ builder: { inject: [PrismaService], diff --git a/src/middlewares/graphql.middleware.ts b/src/middlewares/graphql.middleware.ts index 5d042bf..a231765 100644 --- a/src/middlewares/graphql.middleware.ts +++ b/src/middlewares/graphql.middleware.ts @@ -22,6 +22,17 @@ export class GraphQLValidationMiddleware implements NestMiddleware { ], }); } + // handle query only contain \n + if (query.trim() === '') { + return res.status(400).json({ + errors: [ + { + message: + 'Must provide a valid GraphQL query, mutation, or subscription.', + }, + ], + }); + } } // Continue to the next middleware or GraphQL handler diff --git a/src/resume/resume.module.ts b/src/resume/resume.module.ts new file mode 100644 index 0000000..7e2ae4e --- /dev/null +++ b/src/resume/resume.module.ts @@ -0,0 +1,9 @@ +import { Global, Module } from '@nestjs/common'; +import { ResumeSchema } from './resume.schema'; + +@Global() +@Module({ + providers: [ResumeSchema], + exports: [ResumeSchema], +}) +export class ResumeModule {} diff --git a/src/resume/resume.schema.ts b/src/resume/resume.schema.ts new file mode 100644 index 0000000..fb330cf --- /dev/null +++ b/src/resume/resume.schema.ts @@ -0,0 +1,40 @@ +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 'src/prisma/prisma.service'; + +@Injectable() +export class ResumeSchema extends PothosSchema { + constructor( + @Inject(SchemaBuilderToken) private readonly builder: Builder, + private readonly prisma: PrismaService, + ) { + super(); + } + + @PothosRef() + resume() { + return this.builder.prismaObject('Resume', { + fields: (t) => ({ + id: t.exposeID('id'), + userId: t.exposeID('userId'), + centerId: t.exposeID('centerId'), + status: t.exposeString('status'), + createdAt: t.expose('createdAt', { + type: 'Date', + nullable: true, + }), + updatedAt: t.expose('updatedAt', { + type: 'Date', + nullable: true, + }), + center: t.relation('center'), + }), + }); + } +} diff --git a/src/service/service.module.ts b/src/service/service.module.ts index 3e78ead..992752a 100644 --- a/src/service/service.module.ts +++ b/src/service/service.module.ts @@ -1,4 +1,9 @@ -import { Module } from '@nestjs/common'; +import { Global, Module } from '@nestjs/common'; +import { ServiceSchema } from './service.schema'; -@Module({}) +@Global() +@Module({ + providers: [ServiceSchema], + exports: [ServiceSchema], +}) export class ServiceModule {} diff --git a/src/service/service.schema.ts b/src/service/service.schema.ts new file mode 100644 index 0000000..581116d --- /dev/null +++ b/src/service/service.schema.ts @@ -0,0 +1,30 @@ +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 'src/prisma/prisma.service'; + +@Injectable() +export class ServiceSchema extends PothosSchema { + constructor( + @Inject(SchemaBuilderToken) private readonly builder: Builder, + private readonly prisma: PrismaService, + ) { + super(); + } + + @PothosRef() + service() { + return this.builder.prismaObject('Service', { + fields: (t) => ({ + id: t.exposeID('id'), + name: t.exposeString('name'), + description: t.exposeString('description'), + }), + }); + } +} diff --git a/src/user/user.schema.ts b/src/user/user.schema.ts index 3da9af8..eeb6535 100644 --- a/src/user/user.schema.ts +++ b/src/user/user.schema.ts @@ -30,6 +30,7 @@ export class UserSchema extends PothosSchema { role: t.exposeString('role'), createdAt: t.expose('createdAt', { type: 'Date' }), updatedAt: t.expose('updatedAt', { type: 'Date' }), + center: t.relation('center'), }), }); } @@ -61,6 +62,22 @@ export class UserSchema extends PothosSchema { return users; }, }), + + user: t.prismaField({ + type: this.user(), + args: { + id: t.arg.id({ + description: 'The ID of the user to retrieve', + }), + }, + resolve: async (query, root, args, ctx, info) => { + const { id } = args; + if (!id) { + throw new Error('User ID is required'); + } + return await this.prisma.user.findUnique({ where: { id } }); + }, + }), })); } diff --git a/src/workshop/workshop.module.ts b/src/workshop/workshop.module.ts new file mode 100644 index 0000000..a77e118 --- /dev/null +++ b/src/workshop/workshop.module.ts @@ -0,0 +1,9 @@ +import { Global, Module } from '@nestjs/common'; +import { WorkshopSchema } from './workshop.schema'; + +@Global() +@Module({ + providers: [WorkshopSchema], + exports: [WorkshopSchema], +}) +export class WorkshopModule {} diff --git a/src/workshop/workshop.schema.ts b/src/workshop/workshop.schema.ts new file mode 100644 index 0000000..1bc23ba --- /dev/null +++ b/src/workshop/workshop.schema.ts @@ -0,0 +1,45 @@ +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 'src/prisma/prisma.service'; + +@Injectable() +export class WorkshopSchema extends PothosSchema { + constructor( + @Inject(SchemaBuilderToken) private readonly builder: Builder, + private readonly prisma: PrismaService, + ) { + super(); + } + + @PothosRef() + workshop() { + return this.builder.prismaObject('Workshop', { + fields: (t) => ({ + id: t.exposeID('id'), + title: t.exposeString('title'), + description: t.exposeString('description'), + staffId: t.exposeID('staffId'), + serviceId: t.exposeID('serviceId'), + date: t.expose('date', { + type: 'Date', + }), + createdAt: t.expose('createdAt', { + type: 'Date', + }), + updatedAt: t.expose('updatedAt', { + type: 'Date', + }), + service: t.relation('service'), + workshopOrganization: t.relation('workshopOrganization'), + workshopSubscription: t.relation('workshopSubscription'), + staff: t.relation('staff'), + }), + }); + } +} diff --git a/src/workshoporganization/workshoporganization.module.ts b/src/workshoporganization/workshoporganization.module.ts new file mode 100644 index 0000000..d756f0e --- /dev/null +++ b/src/workshoporganization/workshoporganization.module.ts @@ -0,0 +1,9 @@ +import { Global, Module } from '@nestjs/common'; +import { WorkshopOrganizationSchema } from './workshoporganization.schema'; + +@Global() +@Module({ + providers: [WorkshopOrganizationSchema], + exports: [WorkshopOrganizationSchema], +}) +export class WorkshopOrganizationModule {} diff --git a/src/workshoporganization/workshoporganization.schema.ts b/src/workshoporganization/workshoporganization.schema.ts new file mode 100644 index 0000000..c251f3f --- /dev/null +++ b/src/workshoporganization/workshoporganization.schema.ts @@ -0,0 +1,30 @@ +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 'src/prisma/prisma.service'; + +@Injectable() +export class WorkshopOrganizationSchema extends PothosSchema { + constructor( + @Inject(SchemaBuilderToken) private readonly builder: Builder, + private readonly prisma: PrismaService, + ) { + super(); + } + @PothosRef() + workshopOrganization() { + return this.builder.prismaObject('WorkshopOrganization', { + fields: (t) => ({ + workshopId: t.exposeID('workshopId'), + serviceId: t.exposeID('serviceId'), + workshop: t.relation('workshop'), + service: t.relation('service'), + }), + }); + } +} diff --git a/src/workshopsubscription/workshopsubscription.module.ts b/src/workshopsubscription/workshopsubscription.module.ts new file mode 100644 index 0000000..33bef21 --- /dev/null +++ b/src/workshopsubscription/workshopsubscription.module.ts @@ -0,0 +1,9 @@ +import { Global, Module } from '@nestjs/common'; +import { WorkshopSubscriptionSchema } from './workshopsubscription.schema'; + +@Global() +@Module({ + providers: [WorkshopSubscriptionSchema], + exports: [WorkshopSubscriptionSchema], +}) +export class WorkshopSubscriptionModule {} diff --git a/src/workshopsubscription/workshopsubscription.schema.ts b/src/workshopsubscription/workshopsubscription.schema.ts new file mode 100644 index 0000000..2f14065 --- /dev/null +++ b/src/workshopsubscription/workshopsubscription.schema.ts @@ -0,0 +1,30 @@ +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 'src/prisma/prisma.service'; + +@Injectable() +export class WorkshopSubscriptionSchema extends PothosSchema { + constructor( + @Inject(SchemaBuilderToken) private readonly builder: Builder, + private readonly prisma: PrismaService, + ) { + super(); + } + @PothosRef() + workshopSubscription() { + return this.builder.prismaObject('WorkshopSubscription', { + fields: (t) => ({ + userId: t.exposeID('userId'), + workshopId: t.exposeID('workshopId'), + user: t.relation('user'), + workshop: t.relation('workshop'), + }), + }); + } +}