fix: garantizar tipos base Área y Organización
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class PhaseF221BaseOperationalTypes1789754300000 implements MigrationInterface {
|
||||
name = 'PhaseF221BaseOperationalTypes1789754300000';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
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<void> {
|
||||
throw new Error('F2.2.1 base operational types are structural and are not removed automatically');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user