From 94d77e494cda80c58ab936e0e6a3cfb3736f8500 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 2 Apr 2024 15:32:17 +0700 Subject: [PATCH] refactor: add devMessage to error --- src/controllers/branch/branch-controller.ts | 62 ++++++++++++++++---- src/controllers/branch/contact-controller.ts | 20 +++++-- 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/controllers/branch/branch-controller.ts b/src/controllers/branch/branch-controller.ts index 20828b1..21c1bac 100644 --- a/src/controllers/branch/branch-controller.ts +++ b/src/controllers/branch/branch-controller.ts @@ -95,7 +95,7 @@ export class BranchController extends Controller { @Get("{branchId}") async getBranchById(@Path() branchId: string) { - return await prisma.branch.findFirst({ + const record = await prisma.branch.findFirst({ include: { province: true, district: true, @@ -103,6 +103,12 @@ 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; } @Post() @@ -115,13 +121,29 @@ export class BranchController extends Controller { prisma.branch.findFirst({ where: { id: body.headOfficeId || undefined } }), ]); if (body.provinceId && !province) - throw new HttpError(HttpStatus.BAD_REQUEST, "Province cannot be found."); + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Province cannot be found.", + "missing_or_invalid_parameter", + ); if (body.districtId && !district) - throw new HttpError(HttpStatus.BAD_REQUEST, "District cannot be found."); + throw new HttpError( + HttpStatus.BAD_REQUEST, + "District cannot be found.", + "missing_or_invalid_parameter", + ); if (body.subDistrictId && !subDistrict) - throw new HttpError(HttpStatus.BAD_REQUEST, "Sub-district cannot be found."); + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Sub-district cannot be found.", + "missing_or_invalid_parameter", + ); if (body.headOfficeId && !branch) - throw new HttpError(HttpStatus.BAD_REQUEST, "Head branch cannot be found."); + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Head branch cannot be found.", + "missing_or_invalid_parameter", + ); } const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body; @@ -159,13 +181,29 @@ export class BranchController extends Controller { prisma.branch.findFirst({ where: { id: body.headOfficeId || undefined } }), ]); if (body.provinceId && !province) - throw new HttpError(HttpStatus.BAD_REQUEST, "Province cannot be found."); + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Province cannot be found.", + "missing_or_invalid_parameter", + ); if (body.districtId && !district) - throw new HttpError(HttpStatus.BAD_REQUEST, "District cannot be found."); + throw new HttpError( + HttpStatus.BAD_REQUEST, + "District cannot be found.", + "missing_or_invalid_parameter", + ); if (body.subDistrictId && !subDistrict) - throw new HttpError(HttpStatus.BAD_REQUEST, "Sub-district cannot be found."); + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Sub-district cannot be found.", + "missing_or_invalid_parameter", + ); if (body.headOfficeId && !branch) - throw new HttpError(HttpStatus.BAD_REQUEST, "Head branch cannot be found."); + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Head branch cannot be found.", + "missing_or_invalid_parameter", + ); } const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body; @@ -196,7 +234,7 @@ export class BranchController extends Controller { where: { id: branchId }, }); if (!record) { - throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found."); + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); } return record; } @@ -213,11 +251,11 @@ export class BranchController extends Controller { }); if (!record) { - throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found."); + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); } if (record.status === Status.USED) { - throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used."); + throw new HttpError(HttpStatus.FORBIDDEN, "Branch is in used.", "data_exists"); } return await prisma.branch.delete({ where: { id: branchId } }); diff --git a/src/controllers/branch/contact-controller.ts b/src/controllers/branch/contact-controller.ts index a673f2d..70d10a5 100644 --- a/src/controllers/branch/contact-controller.ts +++ b/src/controllers/branch/contact-controller.ts @@ -65,7 +65,13 @@ export class BranchContactController extends Controller { async getBranchContactById(@Path() branchId: string, @Path() contactId: string) { const record = await prisma.branchContact.findFirst({ where: { id: contactId, branchId } }); - if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found."); + if (!record) { + throw new HttpError( + HttpStatus.NOT_FOUND, + "Branch contact cannot be found.", + "data_not_found", + ); + } return Object.assign(record, { qrCodeImageUrl: await minio.presignedGetObject(MINIO_BUCKET, imageLocation(record.id)), @@ -79,7 +85,11 @@ export class BranchContactController extends Controller { @Body() body: BranchContactCreate, ) { if (!(await prisma.branch.findFirst({ where: { id: branchId } }))) { - throw new HttpError(HttpStatus.BAD_REQUEST, "Branch not found."); + throw new HttpError( + HttpStatus.BAD_REQUEST, + "Branch not found.", + "missing_or_invalid_parameter", + ); } const record = await prisma.branchContact.create({ include: { branch: true }, @@ -112,7 +122,7 @@ export class BranchContactController extends Controller { where: { id: contactId, branchId }, }); if (!record) { - throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found."); + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); } return Object.assign(record, { qrCodeImageUrl: await minio.presignedGetObject( @@ -131,6 +141,8 @@ export class BranchContactController extends Controller { @Delete("{contactId}") 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."); + if (result.count <= 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); + } } }