feat: add code to customer branch for query purpose
This commit is contained in:
parent
88ac8c4b81
commit
7e104a0020
2 changed files with 38 additions and 38 deletions
|
|
@ -121,11 +121,7 @@ export class CustomerBranchController extends Controller {
|
||||||
{ nameEN: { contains: query }, zipCode },
|
{ nameEN: { contains: query }, zipCode },
|
||||||
{ name: { contains: query }, zipCode },
|
{ name: { contains: query }, zipCode },
|
||||||
{ email: { contains: query }, zipCode },
|
{ email: { contains: query }, zipCode },
|
||||||
{
|
{ code: { contains: query }, zipCode },
|
||||||
customer: {
|
|
||||||
code: { contains: query },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
AND: { customerId },
|
AND: { customerId },
|
||||||
} satisfies Prisma.CustomerBranchWhereInput;
|
} satisfies Prisma.CustomerBranchWhereInput;
|
||||||
|
|
@ -219,38 +215,36 @@ export class CustomerBranchController extends Controller {
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async create(@Request() req: RequestWithUser, @Body() body: CustomerBranchCreate) {
|
async create(@Request() req: RequestWithUser, @Body() body: CustomerBranchCreate) {
|
||||||
if (body.provinceId || body.districtId || body.subDistrictId || body.customerId) {
|
const [province, district, subDistrict, customer] = await prisma.$transaction([
|
||||||
const [province, district, subDistrict, customer] = await prisma.$transaction([
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
||||||
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
||||||
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
||||||
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
prisma.customer.findFirst({ where: { id: body.customerId || undefined } }),
|
||||||
prisma.customer.findFirst({ where: { id: body.customerId || undefined } }),
|
]);
|
||||||
]);
|
if (body.provinceId && !province)
|
||||||
if (body.provinceId && !province)
|
throw new HttpError(
|
||||||
throw new HttpError(
|
HttpStatus.BAD_REQUEST,
|
||||||
HttpStatus.BAD_REQUEST,
|
"Province cannot be found.",
|
||||||
"Province cannot be found.",
|
"missing_or_invalid_parameter",
|
||||||
"missing_or_invalid_parameter",
|
);
|
||||||
);
|
if (body.districtId && !district)
|
||||||
if (body.districtId && !district)
|
throw new HttpError(
|
||||||
throw new HttpError(
|
HttpStatus.BAD_REQUEST,
|
||||||
HttpStatus.BAD_REQUEST,
|
"District cannot be found.",
|
||||||
"District cannot be found.",
|
"missing_or_invalid_parameter",
|
||||||
"missing_or_invalid_parameter",
|
);
|
||||||
);
|
if (body.subDistrictId && !subDistrict)
|
||||||
if (body.subDistrictId && !subDistrict)
|
throw new HttpError(
|
||||||
throw new HttpError(
|
HttpStatus.BAD_REQUEST,
|
||||||
HttpStatus.BAD_REQUEST,
|
"Sub-district cannot be found.",
|
||||||
"Sub-district cannot be found.",
|
"missing_or_invalid_parameter",
|
||||||
"missing_or_invalid_parameter",
|
);
|
||||||
);
|
if (!customer)
|
||||||
if (body.customerId && !customer)
|
throw new HttpError(
|
||||||
throw new HttpError(
|
HttpStatus.BAD_REQUEST,
|
||||||
HttpStatus.BAD_REQUEST,
|
"Customer cannot be found.",
|
||||||
"Customer cannot be found.",
|
"missing_or_invalid_parameter",
|
||||||
"missing_or_invalid_parameter",
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { provinceId, districtId, subDistrictId, customerId, ...rest } = body;
|
const { provinceId, districtId, subDistrictId, customerId, ...rest } = body;
|
||||||
|
|
||||||
|
|
@ -269,6 +263,7 @@ export class CustomerBranchController extends Controller {
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
branchNo: count + 1,
|
branchNo: count + 1,
|
||||||
|
code: `${customer.code}-${(count + 1).toString().padStart(2, "0")}`,
|
||||||
customer: { connect: { id: customerId } },
|
customer: { connect: { id: customerId } },
|
||||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||||
district: { connect: districtId ? { id: districtId } : undefined },
|
district: { connect: districtId ? { id: districtId } : undefined },
|
||||||
|
|
|
||||||
|
|
@ -290,6 +290,7 @@ export class CustomerController extends Controller {
|
||||||
customerBranch?.map((v, i) => ({
|
customerBranch?.map((v, i) => ({
|
||||||
...v,
|
...v,
|
||||||
branchNo: i + 1,
|
branchNo: i + 1,
|
||||||
|
code: `${last.key.slice(9)}${last.value.toString().padStart(6, "0")}-${(i + 1).toString().padStart(2, "0")}`,
|
||||||
createdBy: req.user.name,
|
createdBy: req.user.name,
|
||||||
updateBy: req.user.name,
|
updateBy: req.user.name,
|
||||||
})) || [],
|
})) || [],
|
||||||
|
|
@ -325,7 +326,9 @@ export class CustomerController extends Controller {
|
||||||
@Request() req: RequestWithUser,
|
@Request() req: RequestWithUser,
|
||||||
@Body() body: CustomerUpdate,
|
@Body() body: CustomerUpdate,
|
||||||
) {
|
) {
|
||||||
if (!(await prisma.customer.findUnique({ where: { id: customerId } }))) {
|
const customer = await prisma.customer.findUnique({ where: { id: customerId } });
|
||||||
|
|
||||||
|
if (!customer) {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "data_not_found");
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "data_not_found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -417,6 +420,7 @@ export class CustomerController extends Controller {
|
||||||
create: {
|
create: {
|
||||||
...v,
|
...v,
|
||||||
branchNo: i + 1,
|
branchNo: i + 1,
|
||||||
|
code: `${customer.code}-${(i + 1).toString().padStart(2, "0")}`,
|
||||||
createdBy: req.user.name,
|
createdBy: req.user.name,
|
||||||
updateBy: req.user.name,
|
updateBy: req.user.name,
|
||||||
id: undefined,
|
id: undefined,
|
||||||
|
|
@ -424,6 +428,7 @@ export class CustomerController extends Controller {
|
||||||
update: {
|
update: {
|
||||||
...v,
|
...v,
|
||||||
branchNo: i + 1,
|
branchNo: i + 1,
|
||||||
|
code: `${customer.code}-${(i + 1).toString().padStart(2, "0")}`,
|
||||||
updateBy: req.user.name,
|
updateBy: req.user.name,
|
||||||
},
|
},
|
||||||
})),
|
})),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue