Files
epess-web-backend/src/Center/center.schema.ts
2024-10-25 14:49:16 +07:00

195 lines
6.2 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 '../Minio/minio.service';
import { CenterStatus } from '@prisma/client';
@Injectable()
export class CenterSchema extends PothosSchema {
constructor(
@Inject(SchemaBuilderToken) private readonly builder: Builder,
private readonly prisma: PrismaService,
private readonly minioService: MinioService,
) {
super();
}
@PothosRef()
center() {
return this.builder.prismaObject('Center', {
description: 'A center in the system.',
fields: (t) => ({
id: t.exposeID('id', {
description: 'The unique identifier of the center.',
}),
centerOwnerId: t.exposeID('centerOwnerId', {
description: 'The ID of the center owner.',
}),
bank: t.exposeString('bank', {
description: 'The bank of the center.',
}),
bankAccountNumber: t.exposeString('bankAccountNumber', {
description: 'The bank account number of the center.',
}),
name: t.exposeString('name', {
description: 'The name of the center.',
}),
description: t.exposeString('description', {
description: 'The description of the center.',
}),
logoUrl: t.exposeString('logoUrl', {
description: 'The URL of the center logo.',
}),
logoFile: t.relation('logoFile', {
description: 'The file associated with the center logo.',
}),
location: t.exposeString('location', {
description: 'The location of the center.',
}),
individual: t.exposeBoolean('individual', {
nullable: true,
description: 'Whether the center is an individual center.',
}),
createdAt: t.expose('createdAt', { type: 'DateTime' }),
updatedAt: t.expose('updatedAt', { type: 'DateTime' }),
services: t.relation('services', {
description: 'The services provided by the center.',
}),
centerOwner: t.relation('centerOwner', {
description: 'The owner of the center.',
}),
chatRoom: t.relation('chatRoom', {
description: 'The chat room associated with the center.',
}),
centerStaff: t.relation('CenterStaff', {
description: 'The staff members of the center.',
}),
resume: t.relation('Resume', {
description: 'The resume of the center.',
}),
centerStatus: t.expose('centerStatus', {
type: CenterStatus,
description: 'The status of the center.',
}),
uploadedFileId: t.exposeID('uploadedFileId', {
description: 'The ID of the uploaded file.',
}),
}),
});
}
@Pothos()
init(): void {
this.builder.queryFields((t) => ({
centers: t.prismaField({
description:
'Retrieve a list of centers with optional filtering, ordering, and pagination.',
type: [this.center()],
args: this.builder.generator.findManyArgs('Center'),
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.center.findMany({
...query,
skip: args.skip ?? undefined,
take: args.take ?? 10,
orderBy: args.orderBy ?? undefined,
where: args.filter ?? undefined,
});
},
}),
center: t.prismaField({
type: this.center(),
description: 'Retrieve a single center by its unique identifier.',
args: this.builder.generator.findUniqueArgs('Center'),
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.center.findUnique({
...query,
where: args.where,
});
},
}),
// get current center of centerstaff by providing userId
centerByCenterStaff: t.prismaField({
type: this.center(),
description: 'Retrieve a single center by its unique identifier.',
args: {
userId: t.arg({ type: 'String', required: true }),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.center.findFirst({
where: {
CenterStaff: {
some: {
staffId: args.userId,
},
},
},
});
},
}),
}));
// mutation section
this.builder.mutationFields((t) => ({
createCenter: t.prismaField({
description: 'Create a new center.',
type: this.center(),
args: {
input: t.arg({
type: this.builder.generator.getCreateInput('Center'),
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.center.create({
...query,
data: args.input,
});
},
}),
updateCenter: t.prismaField({
type: this.center(),
description: 'Update an existing center.',
args: {
input: t.arg({
type: this.builder.generator.getUpdateInput('Center'),
required: true,
}),
where: t.arg({
type: this.builder.generator.getWhereUnique('Center'),
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.center.update({
...query,
where: args.where,
data: args.input,
});
},
}),
deleteCenter: t.prismaField({
type: this.center(),
description: 'Delete an existing center.',
args: {
where: t.arg({
type: this.builder.generator.getWhereUnique('Center'),
required: true,
}),
},
resolve: async (query, root, args, ctx, info) => {
return await this.prisma.center.delete({
...query,
where: args.where,
});
},
}),
}));
}
}