Customer office relation

This commit is contained in:
Vins 2024-05-27 11:30:12 +02:00
parent 2a1e542763
commit 359dd6a280
6 changed files with 2360 additions and 100 deletions

View File

@ -59,7 +59,7 @@
"file-type-checker": "^1.0.8", "file-type-checker": "^1.0.8",
"fp-ts": "^2.16.1", "fp-ts": "^2.16.1",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.136", "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.137",
"module-alias": "^2.2.2", "module-alias": "^2.2.2",
"monocle-ts": "^2.3.13", "monocle-ts": "^2.3.13",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",

View File

@ -0,0 +1,47 @@
/*
Warnings:
- Added the required column `office_uid` to the `customers` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "customers" ADD COLUMN "office_uid" VARCHAR(255) NOT NULL;
-- Set the office_uid for existing rows based on the office with "idNot" = '0000'
UPDATE customers
SET office_uid = (
SELECT uid
FROM offices
WHERE "idNot" = '0000'
);
-- Alter the column to be NOT NULL
ALTER TABLE customers
ALTER COLUMN office_uid SET NOT NULL;
-- Create a temporary table to store the mapping
CREATE TEMPORARY TABLE customer_office_update AS
SELECT c.uid AS customer_uid, of.office_uid
FROM customers c
JOIN "_OfficeFolderHasCustomers" ofhc ON c.uid = ofhc."A"
JOIN office_folders of ON ofhc."B" = of.uid;
-- Update customers with the corresponding office_uid
UPDATE customers
SET office_uid = (
SELECT office_uid
FROM customer_office_update
WHERE customer_office_update.customer_uid = customers.uid
);
-- Drop the temporary table
DROP TABLE customer_office_update;
-- Add the foreign key constraint
ALTER TABLE customers
ADD CONSTRAINT fk_customers_office
FOREIGN KEY (office_uid) REFERENCES offices(uid)
ON DELETE CASCADE;
-- AddForeignKey
ALTER TABLE "customers" ADD CONSTRAINT "customers_office_uid_fkey" FOREIGN KEY ("office_uid") REFERENCES "offices"("uid") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@ -0,0 +1,2 @@
-- DropForeignKey
ALTER TABLE "customers" DROP CONSTRAINT "fk_customers_office";

View File

@ -110,6 +110,7 @@ model Offices {
document_types DocumentTypes[] document_types DocumentTypes[]
office_roles OfficeRoles[] office_roles OfficeRoles[]
subscriptions Subscriptions[] subscriptions Subscriptions[]
customers Customers[]
@@map("offices") @@map("offices")
} }
@ -125,6 +126,8 @@ model Customers {
documents Documents[] documents Documents[]
password String? @db.VarChar(255) password String? @db.VarChar(255)
totpCodes TotpCodes[] totpCodes TotpCodes[]
office Offices @relation(fields: [office_uid], references: [uid], onDelete: Cascade)
office_uid String @db.VarChar(255)
@@map("customers") @@map("customers")
} }

File diff suppressed because it is too large Load Diff

View File

@ -422,105 +422,6 @@ export default async function main() {
}, },
]; ];
const customers: Customer[] = [
{
contact: contacts[0],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[5],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[6],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[7],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[8],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[9],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[10],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[11],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[12],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[13],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[14],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[15],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[16],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[17],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[18],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
{
contact: contacts[19],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
},
];
const rules: Rule[] = [ const rules: Rule[] = [
{ {
name: "GET users", name: "GET users",
@ -1916,6 +1817,121 @@ export default async function main() {
}, },
]; ];
const customers: Customer[] = [
{
contact: contacts[0],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[5],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[6],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[7],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[8],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[9],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[10],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[11],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[12],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[13],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[14],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[15],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[16],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[17],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[18],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
{
contact: contacts[19],
created_at: new Date(),
updated_at: new Date(),
status: ECustomerStatus.PENDING,
office_membership: offices[0],
},
];
for (const office of offices) { for (const office of offices) {
const officeCreated = await prisma.offices.create({ const officeCreated = await prisma.offices.create({
data: { data: {
@ -2056,6 +2072,11 @@ export default async function main() {
const createArgs: Prisma.CustomersCreateArgs = { const createArgs: Prisma.CustomersCreateArgs = {
data: { data: {
status: ECustomerStatus.PENDING, status: ECustomerStatus.PENDING,
office: {
connect: {
uid: customer.office_membership!.uid!,
},
},
contact: { contact: {
create: { create: {
first_name: customer.contact!.first_name, first_name: customer.contact!.first_name,