fix: error when record not found on update

This commit is contained in:
Methapon2001 2024-04-05 15:23:59 +07:00
parent 7d26d6a9ea
commit 6899c9823a
2 changed files with 23 additions and 7 deletions

View file

@ -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,

View file

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