Refactor ResumeSchema to streamline file upload and resume management logic. Consolidate description formatting for resume and resume file retrieval fields. Enhance error handling during file upload and resume creation, ensuring robust handling of existing resumes. This update improves code clarity and maintains functionality for resume file management.

This commit is contained in:
2024-11-28 19:44:40 +07:00
parent ed7ec9a368
commit f2edc9fa8d

View File

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