From 7627b51a2502741c3f5e554450c59634acd6bd41 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Tue, 23 Apr 2024 18:09:08 +0700 Subject: [PATCH] feat: add missing field --- .../migration.sql | 28 +++++++++ prisma/schema.prisma | 12 +++- src/controllers/customer-branch-controller.ts | 59 +++++++++++++------ src/controllers/customer-controller.ts | 2 + 4 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 prisma/migrations/20240423104607_add_missing_field/migration.sql diff --git a/prisma/migrations/20240423104607_add_missing_field/migration.sql b/prisma/migrations/20240423104607_add_missing_field/migration.sql new file mode 100644 index 0000000..6c8a704 --- /dev/null +++ b/prisma/migrations/20240423104607_add_missing_field/migration.sql @@ -0,0 +1,28 @@ +/* + Warnings: + + - You are about to drop the column `latitude` on the `CustomerBranch` table. All the data in the column will be lost. + - You are about to drop the column `longitude` on the `CustomerBranch` table. All the data in the column will be lost. + - Added the required column `bussinessType` to the `CustomerBranch` table without a default value. This is not possible if the table is not empty. + - Added the required column `employmentOffice` to the `CustomerBranch` table without a default value. This is not possible if the table is not empty. + - Added the required column `jobDescription` to the `CustomerBranch` table without a default value. This is not possible if the table is not empty. + - Added the required column `jobPosition` to the `CustomerBranch` table without a default value. This is not possible if the table is not empty. + - Added the required column `payDate` to the `CustomerBranch` table without a default value. This is not possible if the table is not empty. + - Added the required column `saleEmployee` to the `CustomerBranch` table without a default value. This is not possible if the table is not empty. + - Added the required column `wageDate` to the `CustomerBranch` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "Customer" ADD COLUMN "taxNo" TEXT; + +-- AlterTable +ALTER TABLE "CustomerBranch" DROP COLUMN "latitude", +DROP COLUMN "longitude", +ADD COLUMN "bussinessType" TEXT NOT NULL, +ADD COLUMN "employmentOffice" TEXT NOT NULL, +ADD COLUMN "jobDescription" TEXT NOT NULL, +ADD COLUMN "jobPosition" TEXT NOT NULL, +ADD COLUMN "payDate" TEXT NOT NULL, +ADD COLUMN "saleEmployee" TEXT NOT NULL, +ADD COLUMN "wageDate" TEXT NOT NULL, +ALTER COLUMN "taxNo" DROP NOT NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 791a18c..6d805d0 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -335,6 +335,7 @@ model Customer { customerType CustomerType customerName String customerNameEN String + taxNo String? status Status @default(CREATED) @@ -357,7 +358,7 @@ model CustomerBranch { customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade) customerId String - taxNo String + taxNo String? registerName String registerDate DateTime authorizedCapital String @@ -379,8 +380,13 @@ model CustomerBranch { email String telephoneNo String - latitude String - longitude String + employmentOffice String + bussinessType String + jobPosition String + jobDescription String + saleEmployee String + payDate String + wageDate String status Status @default(CREATED) diff --git a/src/controllers/customer-branch-controller.ts b/src/controllers/customer-branch-controller.ts index 0cf41ad..9cac3da 100644 --- a/src/controllers/customer-branch-controller.ts +++ b/src/controllers/customer-branch-controller.ts @@ -51,6 +51,14 @@ export type CustomerBranchCreate = { registerDate: Date; authorizedCapital: string; + employmentOffice: string; + bussinessType: string; + jobPosition: string; + jobDescription: string; + saleEmployee: string; + payDate: string; + wageDate: string; + subDistrictId?: string | null; districtId?: string | null; provinceId?: string | null; @@ -78,6 +86,14 @@ export type CustomerBranchUpdate = { registerDate?: Date; authorizedCapital?: string; + employmentOffice?: string; + bussinessType?: string; + jobPosition?: string; + jobDescription?: string; + saleEmployee?: string; + payDate?: string; + wageDate?: string; + subDistrictId?: string | null; districtId?: string | null; provinceId?: string | null; @@ -226,27 +242,32 @@ export class CustomerBranchController extends Controller { const { provinceId, districtId, subDistrictId, customerId, ...rest } = body; - const count = await prisma.customerBranch.count({ - where: { customerId }, - }); + const record = await prisma.$transaction( + async (tx) => { + const count = await tx.customerBranch.count({ + where: { customerId }, + }); - const record = await prisma.customerBranch.create({ - include: { - province: true, - district: true, - subDistrict: true, + return await tx.customerBranch.create({ + include: { + province: true, + district: true, + subDistrict: true, + }, + data: { + ...rest, + branchNo: `${count + 1}`, + customer: { connect: { id: customerId } }, + province: { connect: provinceId ? { id: provinceId } : undefined }, + district: { connect: districtId ? { id: districtId } : undefined }, + subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined }, + createdBy: req.user.name, + updateBy: req.user.name, + }, + }); }, - data: { - ...rest, - branchNo: `${count + 1}`, - customer: { connect: { id: customerId } }, - province: { connect: provinceId ? { id: provinceId } : undefined }, - district: { connect: districtId ? { id: districtId } : undefined }, - subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined }, - createdBy: req.user.name, - updateBy: req.user.name, - }, - }); + { isolationLevel: Prisma.TransactionIsolationLevel.Serializable }, + ); await prisma.customer.updateMany({ where: { id: customerId, status: Status.CREATED }, diff --git a/src/controllers/customer-controller.ts b/src/controllers/customer-controller.ts index d251748..276e863 100644 --- a/src/controllers/customer-controller.ts +++ b/src/controllers/customer-controller.ts @@ -31,6 +31,7 @@ export type CustomerCreate = { customerType: CustomerType; customerName: string; customerNameEN: string; + taxNo?: string; customerBranch?: Omit[]; }; @@ -39,6 +40,7 @@ export type CustomerUpdate = { customerType?: CustomerType; customerName?: string; customerNameEN?: string; + taxNo?: string; customerBranch?: (Omit & { id: string })[]; };