diff --git a/src/Resume/resume.schema.ts b/src/Resume/resume.schema.ts index 368743e..67cbe44 100644 --- a/src/Resume/resume.schema.ts +++ b/src/Resume/resume.schema.ts @@ -1,10 +1,5 @@ import { Inject, Injectable, Logger } 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 { MinioService } from '../Minio/minio.service' @@ -129,8 +124,7 @@ export class ResumeSchema extends PothosSchema { }, }), resumes: t.prismaField({ - description: - 'Retrieve a list of resumes with optional filtering, ordering, and pagination.', + description: 'Retrieve a list of resumes with optional filtering, ordering, and pagination.', type: [this.resume()], args: this.builder.generator.findManyArgs('Resume'), resolve: async (query, _root, args) => { @@ -173,8 +167,7 @@ export class ResumeSchema extends PothosSchema { }, }), resumeFiles: t.prismaField({ - description: - 'Retrieve a list of resume files with optional filtering, ordering, and pagination.', + description: 'Retrieve a list of resume files with optional filtering, ordering, and pagination.', type: [this.resumeFile()], args: this.builder.generator.findManyArgs('ResumeFile'), resolve: async (query, _root, args) => { @@ -209,47 +202,40 @@ export class ResumeSchema extends PothosSchema { required: true, }), }, - resolve: async (query, _root, args) => { + resolve: async (_query, _root, args) => { const { resumeFile } = args const { mimetype } = await resumeFile - const { filename, actualFileName } = - await this.minioService.uploadFile(resumeFile, 'resumes') - const fileUrl = await this.minioService.getFileUrl( - filename, - 'resumes', - ) + const { filename, actualFileName } = await this.minioService.uploadFile(resumeFile, 'resumes') + const fileUrl = await this.minioService.getFileUrl(filename, 'resumes') + if (!fileUrl) { + throw new Error('Failed to upload file, please try again later') + } const { userId, centerId } = args - const resume = await this.prisma.resume.upsert({ - ...query, + // check if resume already exists + const existingResume = await this.prisma.resume.findUnique({ where: { - userId_centerId: { - userId, - centerId, - }, - }, - create: { - userId, - centerId, - resumeFile: { - create: { - fileUrl: fileUrl ?? '', - type: mimetype, - actualFileName: actualFileName ?? '', - }, - }, - }, - update: { - resumeFile: { - create: { - fileUrl: fileUrl ?? '', - type: mimetype, - actualFileName: actualFileName ?? '', - }, - }, - status: ResumeStatus.REQUESTED, + userId_centerId: { userId, centerId }, }, }) - return resume + // if resume exists, append resumeFile + if (existingResume) { + return await this.prisma.resume.update({ + where: { id: existingResume.id }, + data: { + resumeFile: { + upsert: { + where: { id: existingResume.id }, + create: { fileUrl, type: mimetype, actualFileName }, + update: { fileUrl, type: mimetype, actualFileName }, + }, + }, + }, + }) + } + // if resume does not exist, create new resume + return await this.prisma.resume.create({ + data: { userId, centerId, resumeFile: { create: { fileUrl, type: mimetype, actualFileName } } }, + }) }, }), @@ -293,10 +279,7 @@ export class ResumeSchema extends PothosSchema { } // if resumeOwner.status is REQUESTED and status is REVIEWING then we need update status and return resume - if ( - oldResume.status === ResumeStatus.REQUESTED && - status === ResumeStatus.REVIEWING - ) { + if (oldResume.status === ResumeStatus.REQUESTED && status === ResumeStatus.REVIEWING) { const resume = await tx.resume.update({ where: { id: resumeId }, data: { status }, @@ -305,10 +288,7 @@ export class ResumeSchema extends PothosSchema { } let _adminNote - if ( - status === ResumeStatus.APPROVED || - status === ResumeStatus.REJECTED - ) { + if (status === ResumeStatus.APPROVED || status === ResumeStatus.REJECTED) { if (status === ResumeStatus.REJECTED && adminNote === '') { throw new Error('Admin note is required when rejecting') }