feat: edit customer branch endpoint
This commit is contained in:
parent
fd37eb6520
commit
6adc961352
1 changed files with 73 additions and 0 deletions
|
|
@ -156,4 +156,77 @@ export class CustomerBranchController extends Controller {
|
||||||
|
|
||||||
return record;
|
return record;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Put("{branchId}")
|
||||||
|
async editById(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Body() body: CustomerBranchUpdate,
|
||||||
|
@Path() branchId: string,
|
||||||
|
) {
|
||||||
|
if (body.provinceId || body.districtId || body.subDistrictId || body.customerId) {
|
||||||
|
const [province, district, subDistrict, customer] = await prisma.$transaction([
|
||||||
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
||||||
|
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
||||||
|
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
||||||
|
prisma.customer.findFirst({ where: { id: body.customerId || undefined } }),
|
||||||
|
]);
|
||||||
|
if (body.provinceId && !province)
|
||||||
|
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.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
if (body.subDistrictId && !subDistrict)
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Sub-district cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
if (body.customerId && !customer)
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Customer cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { provinceId, districtId, subDistrictId, customerId, ...rest } = body;
|
||||||
|
|
||||||
|
const record = await prisma.customerBranch.update({
|
||||||
|
where: { id: branchId },
|
||||||
|
include: {
|
||||||
|
province: true,
|
||||||
|
district: true,
|
||||||
|
subDistrict: true,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
...rest,
|
||||||
|
customer: { connect: customerId ? { id: customerId } : undefined },
|
||||||
|
province: {
|
||||||
|
connect: provinceId ? { id: provinceId } : undefined,
|
||||||
|
disconnect: provinceId === null || undefined,
|
||||||
|
},
|
||||||
|
district: {
|
||||||
|
connect: districtId ? { id: districtId } : undefined,
|
||||||
|
disconnect: districtId === null || undefined,
|
||||||
|
},
|
||||||
|
subDistrict: {
|
||||||
|
connect: subDistrictId ? { id: subDistrictId } : undefined,
|
||||||
|
disconnect: subDistrictId === null || undefined,
|
||||||
|
},
|
||||||
|
createdBy: req.user.name,
|
||||||
|
updateBy: req.user.name,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
this.setStatus(HttpStatus.CREATED);
|
||||||
|
|
||||||
|
return record;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue