refactor: add devMessage to error

This commit is contained in:
Methapon2001 2024-04-02 15:32:17 +07:00
parent ba65c1fe83
commit 94d77e494c
2 changed files with 66 additions and 16 deletions

View file

@ -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 } });

View file

@ -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");
}
}
}