fix: error when record not found on update
This commit is contained in:
parent
7d26d6a9ea
commit
6899c9823a
2 changed files with 23 additions and 7 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue