feat: auto gen branch code
This commit is contained in:
parent
120e2942b8
commit
3abff0594a
1 changed files with 42 additions and 34 deletions
|
|
@ -20,7 +20,6 @@ import HttpStatus from "../interfaces/http-status";
|
||||||
import { RequestWithUser } from "../interfaces/user";
|
import { RequestWithUser } from "../interfaces/user";
|
||||||
|
|
||||||
type BranchCreate = {
|
type BranchCreate = {
|
||||||
code?: string;
|
|
||||||
status?: Status;
|
status?: Status;
|
||||||
taxNo: string;
|
taxNo: string;
|
||||||
nameEN: string;
|
nameEN: string;
|
||||||
|
|
@ -40,7 +39,6 @@ type BranchCreate = {
|
||||||
};
|
};
|
||||||
|
|
||||||
type BranchUpdate = {
|
type BranchUpdate = {
|
||||||
code?: string;
|
|
||||||
status?: "ACTIVE" | "INACTIVE";
|
status?: "ACTIVE" | "INACTIVE";
|
||||||
taxNo?: string;
|
taxNo?: string;
|
||||||
nameEN?: string;
|
nameEN?: string;
|
||||||
|
|
@ -157,41 +155,50 @@ export class BranchController extends Controller {
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async createBranch(@Request() req: RequestWithUser, @Body() body: BranchCreate) {
|
async createBranch(@Request() req: RequestWithUser, @Body() body: BranchCreate) {
|
||||||
if (body.provinceId || body.districtId || body.subDistrictId || body.headOfficeId) {
|
const [province, district, subDistrict, head] = await prisma.$transaction([
|
||||||
const [province, district, subDistrict, branch] = 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.branch.findFirst({ where: { id: body.headOfficeId || undefined } }),
|
||||||
prisma.branch.findFirst({ where: { id: body.headOfficeId || 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 (body.headOfficeId && !head)
|
||||||
if (body.headOfficeId && !branch)
|
throw new HttpError(
|
||||||
throw new HttpError(
|
HttpStatus.BAD_REQUEST,
|
||||||
HttpStatus.BAD_REQUEST,
|
"Head branch cannot be found.",
|
||||||
"Head branch cannot be found.",
|
"missing_or_invalid_parameter",
|
||||||
"missing_or_invalid_parameter",
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body;
|
const { provinceId, districtId, subDistrictId, headOfficeId, ...rest } = body;
|
||||||
|
|
||||||
|
const year = new Date().getFullYear();
|
||||||
|
|
||||||
|
const last = await prisma.branch.findFirst({
|
||||||
|
orderBy: { createdAt: "desc" },
|
||||||
|
where: { headOfficeId: headOfficeId ?? null },
|
||||||
|
});
|
||||||
|
|
||||||
|
const code = !headOfficeId
|
||||||
|
? `HQ${year.toString().slice(2)}${+(last?.code.slice(-1) || 0) + 1}`
|
||||||
|
: `BR${head?.code.slice(2, 5)}${(+(last?.code.slice(-2) || 0) + 1).toString().padStart(2, "0")}`;
|
||||||
|
|
||||||
const record = await prisma.branch.create({
|
const record = await prisma.branch.create({
|
||||||
include: {
|
include: {
|
||||||
province: true,
|
province: true,
|
||||||
|
|
@ -200,6 +207,7 @@ export class BranchController extends Controller {
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
|
code,
|
||||||
isHeadOffice: !headOfficeId,
|
isHeadOffice: !headOfficeId,
|
||||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||||
district: { connect: districtId ? { id: districtId } : undefined },
|
district: { connect: districtId ? { id: districtId } : undefined },
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue