From 6899c9823a2aedb00e372d55fd83445b3cae0c4c Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Fri, 5 Apr 2024 15:23:59 +0700 Subject: [PATCH] fix: error when record not found on update --- src/controllers/branch-contact-controller.ts | 22 ++++++++++++++++---- src/controllers/branch-controller.ts | 8 ++++--- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/controllers/branch-contact-controller.ts b/src/controllers/branch-contact-controller.ts index 7a9b3b7..90ccbe7 100644 --- a/src/controllers/branch-contact-controller.ts +++ b/src/controllers/branch-contact-controller.ts @@ -132,13 +132,23 @@ export class BranchContactController extends Controller { @Path() branchId: string, @Path() contactId: string, ) { + if ( + !(await prisma.branchContact.findUnique({ + where: { id: contactId, branchId }, + })) + ) { + throw new HttpError( + HttpStatus.NOT_FOUND, + "Branch contact cannot be found.", + "data_not_found", + ); + } + const record = await prisma.branchContact.update({ data: { ...body, updateBy: req.user.name }, where: { id: contactId, branchId }, }); - if (!record) { - throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); - } + return Object.assign(record, { qrCodeImageUrl: await minio.presignedGetObject( MINIO_BUCKET, @@ -157,7 +167,11 @@ export class BranchContactController extends Controller { async deleteBranchContact(@Path() branchId: string, @Path() contactId: string) { const result = await prisma.branchContact.deleteMany({ where: { id: contactId, branchId } }); if (result.count <= 0) { - throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); + throw new HttpError( + HttpStatus.NOT_FOUND, + "Branch contact cannot be found.", + "data_not_found", + ); } await minio.removeObject(MINIO_BUCKET, imageLocation(contactId), { forceDelete: true, diff --git a/src/controllers/branch-controller.ts b/src/controllers/branch-controller.ts index 0c2c777..bb0ba51 100644 --- a/src/controllers/branch-controller.ts +++ b/src/controllers/branch-controller.ts @@ -259,6 +259,10 @@ export class BranchController extends Controller { const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body; + if (!(await prisma.branch.findUnique({ where: { id: branchId } }))) { + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); + } + const record = await prisma.branch.update({ include: { province: true, district: true, subDistrict: true }, data: { @@ -284,9 +288,7 @@ export class BranchController extends Controller { }, where: { id: branchId }, }); - if (!record) { - throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); - } + return record; }