From 0bd2c1ecf2cfc013c4848fdd8810887ea6ca2122 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Mon, 24 Jun 2024 13:14:44 +0700 Subject: [PATCH] feat: add order field --- .../migration.sql | 29 ++++++++++++++++ prisma/schema.prisma | 33 ++++++++++++------- src/controllers/branch-contact-controller.ts | 6 +--- src/controllers/branch-controller.ts | 2 ++ src/controllers/customer-branch-controller.ts | 8 ++++- src/controllers/customer-controller.ts | 2 ++ src/controllers/employee-controller.ts | 2 ++ src/controllers/product/group-controller.ts | 3 +- src/controllers/product/product-controller.ts | 7 ++-- src/controllers/product/type-controller.ts | 5 +-- src/controllers/user-controller.ts | 8 +++-- 11 files changed, 79 insertions(+), 26 deletions(-) create mode 100644 prisma/migrations/20240624061417_add_order_status/migration.sql diff --git a/prisma/migrations/20240624061417_add_order_status/migration.sql b/prisma/migrations/20240624061417_add_order_status/migration.sql new file mode 100644 index 0000000..436649e --- /dev/null +++ b/prisma/migrations/20240624061417_add_order_status/migration.sql @@ -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; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ee0089d..69329b8 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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? diff --git a/src/controllers/branch-contact-controller.ts b/src/controllers/branch-contact-controller.ts index e2e3d6c..701e4b3 100644 --- a/src/controllers/branch-contact-controller.ts +++ b/src/controllers/branch-contact-controller.ts @@ -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 }, diff --git a/src/controllers/branch-controller.ts b/src/controllers/branch-controller.ts index bf61974..8008edb 100644 --- a/src/controllers/branch-controller.ts +++ b/src/controllers/branch-controller.ts @@ -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, diff --git a/src/controllers/customer-branch-controller.ts b/src/controllers/customer-branch-controller.ts index 5bd51c4..0b6b5cb 100644 --- a/src/controllers/customer-branch-controller.ts +++ b/src/controllers/customer-branch-controller.ts @@ -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) => { diff --git a/src/controllers/customer-controller.ts b/src/controllers/customer-controller.ts index c421f95..3798e89 100644 --- a/src/controllers/customer-controller.ts +++ b/src/controllers/customer-controller.ts @@ -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: { diff --git a/src/controllers/employee-controller.ts b/src/controllers/employee-controller.ts index a4800fd..f0e11db 100644 --- a/src/controllers/employee-controller.ts +++ b/src/controllers/employee-controller.ts @@ -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 diff --git a/src/controllers/product/group-controller.ts b/src/controllers/product/group-controller.ts index 30e742e..f27b7e1 100644 --- a/src/controllers/product/group-controller.ts +++ b/src/controllers/product/group-controller.ts @@ -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 }, }); diff --git a/src/controllers/product/product-controller.ts b/src/controllers/product/product-controller.ts index a74b0f5..3c71ad2 100644 --- a/src/controllers/product/product-controller.ts +++ b/src/controllers/product/product-controller.ts @@ -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 }, }); } diff --git a/src/controllers/product/type-controller.ts b/src/controllers/product/type-controller.ts index 24eec0a..89d5a22 100644 --- a/src/controllers/product/type-controller.ts +++ b/src/controllers/product/type-controller.ts @@ -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 }, }); } diff --git a/src/controllers/user-controller.ts b/src/controllers/user-controller.ts index 06e58e8..fdfb338 100644 --- a/src/controllers/user-controller.ts +++ b/src/controllers/user-controller.ts @@ -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 &&