diff --git a/prisma/migrations/20240802075330_add_bank_relation_with_branch/migration.sql b/prisma/migrations/20240802075330_add_bank_relation_with_branch/migration.sql new file mode 100644 index 0000000..cfb1ba1 --- /dev/null +++ b/prisma/migrations/20240802075330_add_bank_relation_with_branch/migration.sql @@ -0,0 +1,13 @@ +-- CreateTable +CREATE TABLE "BranchBank" ( + "id" TEXT NOT NULL, + "bankName" TEXT NOT NULL, + "accountName" TEXT NOT NULL, + "accountNumber" TEXT NOT NULL, + "branchId" TEXT, + + CONSTRAINT "BranchBank_pkey" PRIMARY KEY ("id") +); + +-- AddForeignKey +ALTER TABLE "BranchBank" ADD CONSTRAINT "BranchBank_branchId_fkey" FOREIGN KEY ("branchId") REFERENCES "Branch"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index afab626..7c9bbb0 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -217,6 +217,8 @@ model Branch { headOffice Branch? @relation(name: "HeadOfficeRelation", fields: [headOfficeId], references: [id]) headOfficeId String? + bank BranchBank[] + status Status @default(CREATED) statusOrder Int @default(0) @@ -236,6 +238,16 @@ model Branch { customerRegistration Customer[] } +model BranchBank { + id String @id @default(uuid()) + bankName String + accountName String + accountNumber String + + branch Branch? @relation(fields: [branchId], references: [id]) + branchId String? +} + model BranchContact { id String @id @default(uuid()) telephoneNo String diff --git a/src/controllers/branch-controller.ts b/src/controllers/branch-controller.ts index fd82fac..06a91f3 100644 --- a/src/controllers/branch-controller.ts +++ b/src/controllers/branch-controller.ts @@ -42,6 +42,12 @@ type BranchCreate = { longitude: string; latitude: string; + bank?: { + bankName: string; + accountName: string; + accountNumber: string; + }[]; + subDistrictId?: string | null; districtId?: string | null; provinceId?: string | null; @@ -68,6 +74,12 @@ type BranchUpdate = { districtId?: string | null; provinceId?: string | null; headOfficeId?: string | null; + + bank?: { + bankName: string; + accountName: string; + accountNumber: string; + }[]; }; function lineImageLoc(id: string) { @@ -273,7 +285,7 @@ export class BranchController extends Controller { "relationHQNotFound", ); - const { provinceId, districtId, subDistrictId, headOfficeId, contact, ...rest } = body; + const { provinceId, districtId, subDistrictId, headOfficeId, bank, contact, ...rest } = body; const year = new Date().getFullYear(); @@ -306,6 +318,7 @@ export class BranchController extends Controller { ...rest, statusOrder: +(rest.status === "INACTIVE"), code, + bank: bank ? { createMany: { data: bank } } : undefined, isHeadOffice: !headOfficeId, province: { connect: provinceId ? { id: provinceId } : undefined }, district: { connect: districtId ? { id: districtId } : undefined }, @@ -401,7 +414,7 @@ export class BranchController extends Controller { ); } - const { provinceId, districtId, subDistrictId, headOfficeId, contact, ...rest } = body; + const { provinceId, districtId, subDistrictId, headOfficeId, bank, contact, ...rest } = body; const branch = await prisma.branch.findUnique({ include: { @@ -431,6 +444,7 @@ export class BranchController extends Controller { ...rest, statusOrder: +(rest.status === "INACTIVE"), isHeadOffice: headOfficeId !== undefined ? headOfficeId === null : undefined, + bank: bank ? { deleteMany: {}, createMany: { data: bank } } : undefined, province: { connect: provinceId ? { id: provinceId } : undefined, disconnect: provinceId === null || undefined,