From 7b5050e08ea15b891abdcfd452b757396fef0e3e Mon Sep 17 00:00:00 2001 From: enlineawork Date: Sun, 6 Sep 2026 22:44:30 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20garantizar=20tipos=20base=20=C3=81rea=20?= =?UTF-8?q?y=20Organizaci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...000-phase-f2-2-1-base-operational-types.ts | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 api-v3/src/database/migrations/1789754300000-phase-f2-2-1-base-operational-types.ts diff --git a/api-v3/src/database/migrations/1789754300000-phase-f2-2-1-base-operational-types.ts b/api-v3/src/database/migrations/1789754300000-phase-f2-2-1-base-operational-types.ts new file mode 100644 index 0000000..be0a8e6 --- /dev/null +++ b/api-v3/src/database/migrations/1789754300000-phase-f2-2-1-base-operational-types.ts @@ -0,0 +1,50 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class PhaseF221BaseOperationalTypes1789754300000 implements MigrationInterface { + name = 'PhaseF221BaseOperationalTypes1789754300000'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + UPDATE asset_types + SET operational_role='AREA', can_be_root=true, is_active=true, updated_at=CURRENT_TIMESTAMP + WHERE lower(code)='area' + `); + await queryRunner.query(` + INSERT INTO asset_types (code,name,description,can_be_root,is_active,operational_role) + SELECT 'area','Área', + 'Área hidrocarburífera administrada como ancla territorial.', + true,true,'AREA' + WHERE NOT EXISTS (SELECT 1 FROM asset_types WHERE operational_role='AREA') + AND NOT EXISTS (SELECT 1 FROM asset_types WHERE lower(code)='area') + `); + + await queryRunner.query(` + UPDATE asset_types + SET operational_role='COMPANY', can_be_root=true, is_active=true, updated_at=CURRENT_TIMESTAMP + WHERE lower(code) IN ('empresa','organizacion') + `); + await queryRunner.query(` + INSERT INTO asset_types (code,name,description,can_be_root,is_active,operational_role) + SELECT 'empresa','Organización', + 'Entidad jurídica u organización administrada. Su rol operativo se registra mediante relaciones históricas.', + true,true,'COMPANY' + WHERE NOT EXISTS (SELECT 1 FROM asset_types WHERE operational_role='COMPANY') + AND NOT EXISTS (SELECT 1 FROM asset_types WHERE lower(code)='empresa') + `); + + const rows = (await queryRunner.query(` + SELECT + count(*) FILTER (WHERE operational_role='AREA')::text AS areas, + count(*) FILTER (WHERE operational_role='COMPANY')::text AS companies + FROM asset_types + `)) as Array<{ areas: string; companies: string }>; + + if (Number(rows[0]?.areas ?? 0) < 1 || Number(rows[0]?.companies ?? 0) < 1) { + throw new Error('F2.2.1 could not guarantee AREA and COMPANY asset types'); + } + } + + public async down(): Promise { + throw new Error('F2.2.1 base operational types are structural and are not removed automatically'); + } +}