From 624f4f2fd7cd4a4e2803e595a97fd5920902d263 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 18 Apr 2024 13:21:37 +0700 Subject: [PATCH] refactor: change field name --- .../20240418061959_rename_field/migration.sql | 9 ++++++ .../migration.sql | 8 ++++++ prisma/schema.prisma | 2 +- src/controllers/branch-controller.ts | 28 +++++++++---------- 4 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 prisma/migrations/20240418061959_rename_field/migration.sql create mode 100644 prisma/migrations/20240418062042_remove_optional/migration.sql diff --git a/prisma/migrations/20240418061959_rename_field/migration.sql b/prisma/migrations/20240418061959_rename_field/migration.sql new file mode 100644 index 0000000..6233aa1 --- /dev/null +++ b/prisma/migrations/20240418061959_rename_field/migration.sql @@ -0,0 +1,9 @@ +/* + Warnings: + + - You are about to drop the column `telephoneHq` on the `Branch` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "Branch" DROP COLUMN "telephoneHq", +ADD COLUMN "telephoneNo" TEXT; diff --git a/prisma/migrations/20240418062042_remove_optional/migration.sql b/prisma/migrations/20240418062042_remove_optional/migration.sql new file mode 100644 index 0000000..ca8ae37 --- /dev/null +++ b/prisma/migrations/20240418062042_remove_optional/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - Made the column `telephoneNo` on table `Branch` required. This step will fail if there are existing NULL values in that column. + +*/ +-- AlterTable +ALTER TABLE "Branch" ALTER COLUMN "telephoneNo" SET NOT NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 47d0d8b..1652ee2 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -182,7 +182,7 @@ model Branch { nameEN String address String addressEN String - telephoneHq String + telephoneNo String province Province? @relation(fields: [provinceId], references: [id], onDelete: SetNull) provinceId String? diff --git a/src/controllers/branch-controller.ts b/src/controllers/branch-controller.ts index 741521d..b4dc67c 100644 --- a/src/controllers/branch-controller.ts +++ b/src/controllers/branch-controller.ts @@ -36,8 +36,8 @@ type BranchCreate = { zipCode: string; email: string; contactName: string; - telephoneHq: string; - telephoneNo: string | string[]; + contact: string | string[]; + telephoneNo: string; lineId: string; longitude: string; latitude: string; @@ -57,9 +57,9 @@ type BranchUpdate = { address?: string; zipCode?: string; email?: string; + telephoneNo: string; contactName?: string; - telephoneHq: string; - telephoneNo?: string | string[]; + contact?: string | string[]; lineId?: string; longitude?: string; latitude?: string; @@ -245,7 +245,7 @@ export class BranchController extends Controller { "missing_or_invalid_parameter", ); - const { provinceId, districtId, subDistrictId, headOfficeId, telephoneNo, ...rest } = body; + const { provinceId, districtId, subDistrictId, headOfficeId, contact, ...rest } = body; const year = new Date().getFullYear(); @@ -286,12 +286,12 @@ export class BranchController extends Controller { this.setStatus(HttpStatus.CREATED); - if (record && telephoneNo) { + if (record && contact) { await prisma.branchContact.createMany({ data: - typeof telephoneNo === "string" - ? [{ telephoneNo, branchId: record.id }] - : telephoneNo.map((v) => ({ telephoneNo: v, branchId: record.id })), + typeof contact === "string" + ? [{ telephoneNo: contact, branchId: record.id }] + : contact.map((v) => ({ telephoneNo: v, branchId: record.id })), }); } @@ -358,7 +358,7 @@ export class BranchController extends Controller { ); } - const { provinceId, districtId, subDistrictId, headOfficeId, telephoneNo, ...rest } = body; + const { provinceId, districtId, subDistrictId, headOfficeId, contact, ...rest } = body; if (!(await prisma.branch.findUnique({ where: { id: branchId } }))) { throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found"); @@ -390,13 +390,13 @@ export class BranchController extends Controller { where: { id: branchId }, }); - if (record && telephoneNo) { + if (record && contact) { await prisma.branchContact.deleteMany({ where: { branchId } }); await prisma.branchContact.createMany({ data: - typeof telephoneNo === "string" - ? [{ telephoneNo, branchId }] - : telephoneNo.map((v) => ({ telephoneNo: v, branchId })), + typeof contact === "string" + ? [{ telephoneNo: contact, branchId }] + : contact.map((v) => ({ telephoneNo: v, branchId })), }); }