feat: add order field

This commit is contained in:
Methapon2001 2024-06-24 13:14:44 +07:00
parent aa8dcdaf86
commit 0bd2c1ecf2
11 changed files with 79 additions and 26 deletions

View file

@ -0,0 +1,29 @@
-- AlterTable
ALTER TABLE "Branch" ADD COLUMN "statusOrder" INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE "Customer" ADD COLUMN "statusOrder" INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE "CustomerBranch" ADD COLUMN "statusOrder" INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE "Employee" ADD COLUMN "statusOrder" INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE "Product" ADD COLUMN "statusOrder" INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE "ProductGroup" ADD COLUMN "statusOrder" INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE "ProductType" ADD COLUMN "statusOrder" INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE "Service" ADD COLUMN "statusOrder" INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE "User" ADD COLUMN "statusOrder" INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE "Work" ADD COLUMN "statusOrder" INTEGER NOT NULL DEFAULT 0;

View file

@ -212,7 +212,8 @@ model Branch {
headOffice Branch? @relation(name: "HeadOfficeRelation", fields: [headOfficeId], references: [id])
headOfficeId String?
status Status @default(CREATED)
status Status @default(CREATED)
statusOrder Int @default(0)
createdBy String?
createdAt DateTime @default(now())
@ -312,7 +313,8 @@ model User {
birthDate DateTime?
status Status @default(CREATED)
status Status @default(CREATED)
statusOrder Int @default(0)
createdBy String?
createdAt DateTime @default(now())
@ -339,7 +341,8 @@ model Customer {
customerNameEN String
taxNo String?
status Status @default(CREATED)
status Status @default(CREATED)
statusOrder Int @default(0)
createdBy String?
createdAt DateTime @default(now())
@ -393,7 +396,8 @@ model CustomerBranch {
payDate DateTime
wageRate Int
status Status @default(CREATED)
status Status @default(CREATED)
statusOrder Int @default(0)
createdBy String?
createdAt DateTime @default(now())
@ -452,7 +456,8 @@ model Employee {
customerBranch CustomerBranch? @relation(fields: [customerBranchId], references: [id], onDelete: SetNull)
customerBranchId String?
status Status @default(CREATED)
status Status @default(CREATED)
statusOrder Int @default(0)
createdBy String?
createdAt DateTime @default(now())
@ -544,8 +549,10 @@ model Service {
detail String
attributes Json?
status Status @default(CREATED)
work Work[]
status Status @default(CREATED)
statusOrder Int @default(0)
work Work[]
createdBy String?
createdAt DateTime @default(now())
@ -560,7 +567,8 @@ model Work {
name String
attributes Json?
status Status @default(CREATED)
status Status @default(CREATED)
statusOrder Int @default(0)
service Service? @relation(fields: [serviceId], references: [id], onDelete: Cascade)
serviceId String?
@ -596,7 +604,8 @@ model ProductGroup {
detail String
remark String
status Status @default(CREATED)
status Status @default(CREATED)
statusOrder Int @default(0)
createdBy String?
createdAt DateTime @default(now())
@ -614,7 +623,8 @@ model ProductType {
detail String
remark String
status Status @default(CREATED)
status Status @default(CREATED)
statusOrder Int @default(0)
createdBy String?
createdAt DateTime @default(now())
@ -638,7 +648,8 @@ model Product {
agentPrice Int
serviceCharge Int
status Status @default(CREATED)
status Status @default(CREATED)
statusOrder Int @default(0)
remark String?

View file

@ -76,11 +76,7 @@ export class BranchContactController extends Controller {
@Body() body: BranchContactCreate,
) {
if (!(await prisma.branch.findFirst({ where: { id: branchId } }))) {
throw new HttpError(
HttpStatus.BAD_REQUEST,
"Branch cannot be found.",
"branchBadReq",
);
throw new HttpError(HttpStatus.BAD_REQUEST, "Branch cannot be found.", "branchBadReq");
}
const record = await prisma.branchContact.create({
data: { ...body, branchId, createdBy: req.user.name, updateBy: req.user.name },

View file

@ -279,6 +279,7 @@ export class BranchController extends Controller {
},
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
code,
isHeadOffice: !headOfficeId,
province: { connect: provinceId ? { id: provinceId } : undefined },
@ -384,6 +385,7 @@ export class BranchController extends Controller {
include: { province: true, district: true, subDistrict: true },
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
isHeadOffice: headOfficeId !== undefined ? headOfficeId === null : undefined,
province: {
connect: provinceId ? { id: provinceId } : undefined,

View file

@ -287,6 +287,7 @@ export class CustomerBranchController extends Controller {
},
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
branchNo: count + 1,
code: `${customer.code}-${(count + 1).toString().padStart(2, "0")}`,
customer: { connect: { id: customerId } },
@ -365,6 +366,7 @@ export class CustomerBranchController extends Controller {
},
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
customer: { connect: customerId ? { id: customerId } : undefined },
province: {
connect: provinceId ? { id: provinceId } : undefined,
@ -403,7 +405,11 @@ export class CustomerBranchController extends Controller {
}
if (record.status !== Status.CREATED) {
throw new HttpError(HttpStatus.FORBIDDEN, "Customer branch is in used.", "customerBranchInUsed");
throw new HttpError(
HttpStatus.FORBIDDEN,
"Customer branch is in used.",
"customerBranchInUsed",
);
}
return await prisma.customerBranch.delete({ where: { id: branchId } }).then((v) => {

View file

@ -296,6 +296,7 @@ export class CustomerController extends Controller {
},
data: {
...payload,
statusOrder: +(payload.status === "INACTIVE"),
code: `${last.key.slice(9)}${last.value.toString().padStart(6, "0")}`,
branch: {
createMany: {
@ -420,6 +421,7 @@ export class CustomerController extends Controller {
where: { id: customerId },
data: {
...payload,
statusOrder: +(payload.status === "INACTIVE"),
branch:
(customerBranch && {
deleteMany: {

View file

@ -395,6 +395,7 @@ export class EmployeeController extends Controller {
},
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
code: `${customerBranch.customer.code}-${customerBranch.branchNo.toString().padStart(2, "0")}-${new Date().getFullYear().toString().slice(-2).padStart(2, "0")}${last.value.toString().padStart(4, "0")}`,
employeeWork: {
createMany: {
@ -562,6 +563,7 @@ export class EmployeeController extends Controller {
},
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
code,
customerBranch: { connect: customerBranchId ? { id: customerBranchId } : undefined },
employeeWork: employeeWork

View file

@ -95,6 +95,7 @@ export class ProductGroup extends Controller {
return await tx.productGroup.create({
data: {
...body,
statusOrder: +(body.status === "INACTIVE"),
code: `G${last.value.toString().padStart(2, "0")}`,
createdBy: req.user.name,
updateBy: req.user.name,
@ -124,7 +125,7 @@ export class ProductGroup extends Controller {
}
const record = await prisma.productGroup.update({
data: { ...body, updateBy: req.user.name },
data: { ...body, statusOrder: +(body.status === "INACTIVE"), updateBy: req.user.name },
where: { id: groupId },
});

View file

@ -171,6 +171,7 @@ export class ProductController extends Controller {
return await prisma.product.create({
data: {
...body,
statusOrder: +(body.status === "INACTIVE"),
code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`,
createdBy: req.user.name,
updateBy: req.user.name,
@ -229,13 +230,13 @@ export class ProductController extends Controller {
}
const record = await prisma.product.update({
data: { ...body, updateBy: req.user.name },
data: { ...body, statusOrder: +(body.status === "INACTIVE"), updateBy: req.user.name },
where: { id: productId },
});
if (productType.status === "CREATED") {
await prisma.productType.update({
where: { id: body.productTypeId },
await prisma.productType.updateMany({
where: { id: body.productTypeId, status: Status.CREATED },
data: { status: Status.ACTIVE },
});
}

View file

@ -114,6 +114,7 @@ export class ProductType extends Controller {
return await tx.productType.create({
data: {
...body,
statusOrder: +(body.status === "INACTIVE"),
code: `T${productGroup.code}${last.value.toString().padStart(2, "0")}`,
createdBy: req.user.name,
updateBy: req.user.name,
@ -161,13 +162,13 @@ export class ProductType extends Controller {
}
const record = await prisma.productType.update({
data: { ...body, updateBy: req.user.name },
data: { ...body, statusOrder: +(body.status === "INACTIVE"), updateBy: req.user.name },
where: { id: typeId },
});
if (productGroup?.status === "CREATED") {
await prisma.productGroup.update({
where: { id: body.productGroupId },
where: { id: body.productGroupId, status: Status.CREATED },
data: { status: Status.ACTIVE },
});
}

View file

@ -209,8 +209,7 @@ export class UserController extends Controller {
where: { id: userId },
});
if (!record)
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
return Object.assign(record, {
profileImageUrl: await minio.presignedGetObject(
@ -268,6 +267,7 @@ export class UserController extends Controller {
firstName: body.firstName,
lastName: body.lastName,
requiredActions: ["UPDATE_PASSWORD"],
enabled: rest.status !== "INACTIVE",
});
if (!userId || typeof userId !== "string") {
@ -288,6 +288,7 @@ export class UserController extends Controller {
data: {
id: userId,
...rest,
statusOrder: +(rest.status === "INACTIVE"),
username,
userRole: role.name,
province: { connect: provinceId ? { id: provinceId } : undefined },
@ -386,7 +387,7 @@ export class UserController extends Controller {
}
if (body.username) {
await editUser(userId, { username: body.username });
await editUser(userId, { username: body.username, enabled: body.status !== "INACTIVE" });
}
const { provinceId, districtId, subDistrictId, ...rest } = body;
@ -415,6 +416,7 @@ export class UserController extends Controller {
include: { province: true, district: true, subDistrict: true },
data: {
...rest,
statusOrder: +(rest.status === "INACTIVE"),
userRole,
code:
(lastUserOfType &&