F3.1: sincronizar catálogo por familia técnica
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class PhaseF31FamilyCatalogSync1789844500000 implements MigrationInterface {
|
||||
name = 'PhaseF31FamilyCatalogSync1789844500000';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
CREATE OR REPLACE FUNCTION sync_asset_inventory_family_catalog()
|
||||
RETURNS trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
DELETE FROM finding_catalog_asset_overrides
|
||||
WHERE asset_id=NEW.id
|
||||
AND reason LIKE 'F3.1 familia técnica:%';
|
||||
|
||||
IF NEW.inventory_family_id IS NOT NULL THEN
|
||||
INSERT INTO finding_catalog_asset_overrides (
|
||||
asset_id,catalog_item_id,is_enabled,reason,created_by,updated_by
|
||||
)
|
||||
SELECT
|
||||
NEW.id,mapping.catalog_item_id,true,
|
||||
'F3.1 familia técnica: catálogo contextual automático',
|
||||
NEW.created_by,NEW.updated_by
|
||||
FROM finding_catalog_item_inventory_families mapping
|
||||
WHERE mapping.inventory_family_id=NEW.inventory_family_id
|
||||
ON CONFLICT (asset_id,catalog_item_id) DO UPDATE SET
|
||||
is_enabled=true,
|
||||
reason='F3.1 familia técnica: catálogo contextual automático',
|
||||
updated_by=NEW.updated_by,
|
||||
updated_at=CURRENT_TIMESTAMP;
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
CREATE TRIGGER trg_assets_inventory_family_catalog
|
||||
AFTER INSERT OR UPDATE OF inventory_family_id ON assets
|
||||
FOR EACH ROW EXECUTE FUNCTION sync_asset_inventory_family_catalog()
|
||||
`);
|
||||
|
||||
await queryRunner.query(`
|
||||
CREATE OR REPLACE FUNCTION sync_inventory_family_mapping_assets()
|
||||
RETURNS trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
IF TG_OP='DELETE' THEN
|
||||
DELETE FROM finding_catalog_asset_overrides override_record
|
||||
USING assets asset
|
||||
WHERE override_record.asset_id=asset.id
|
||||
AND asset.inventory_family_id=OLD.inventory_family_id
|
||||
AND override_record.catalog_item_id=OLD.catalog_item_id
|
||||
AND override_record.reason LIKE 'F3.1 familia técnica:%';
|
||||
RETURN OLD;
|
||||
END IF;
|
||||
|
||||
INSERT INTO finding_catalog_asset_overrides (
|
||||
asset_id,catalog_item_id,is_enabled,reason,created_by,updated_by
|
||||
)
|
||||
SELECT
|
||||
asset.id,NEW.catalog_item_id,true,
|
||||
'F3.1 familia técnica: catálogo contextual automático',
|
||||
asset.created_by,asset.updated_by
|
||||
FROM assets asset
|
||||
WHERE asset.inventory_family_id=NEW.inventory_family_id
|
||||
ON CONFLICT (asset_id,catalog_item_id) DO UPDATE SET
|
||||
is_enabled=true,
|
||||
reason='F3.1 familia técnica: catálogo contextual automático',
|
||||
updated_at=CURRENT_TIMESTAMP;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
CREATE TRIGGER trg_inventory_family_mapping_assets
|
||||
AFTER INSERT OR DELETE ON finding_catalog_item_inventory_families
|
||||
FOR EACH ROW EXECUTE FUNCTION sync_inventory_family_mapping_assets()
|
||||
`);
|
||||
|
||||
await queryRunner.query(`
|
||||
INSERT INTO finding_catalog_asset_overrides (
|
||||
asset_id,catalog_item_id,is_enabled,reason,created_by,updated_by
|
||||
)
|
||||
SELECT
|
||||
asset.id,mapping.catalog_item_id,true,
|
||||
'F3.1 familia técnica: catálogo contextual automático',
|
||||
asset.created_by,asset.updated_by
|
||||
FROM assets asset
|
||||
JOIN finding_catalog_item_inventory_families mapping
|
||||
ON mapping.inventory_family_id=asset.inventory_family_id
|
||||
WHERE asset.inventory_family_id IS NOT NULL
|
||||
ON CONFLICT (asset_id,catalog_item_id) DO UPDATE SET
|
||||
is_enabled=true,
|
||||
reason='F3.1 familia técnica: catálogo contextual automático',
|
||||
updated_at=CURRENT_TIMESTAMP
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query('DROP TRIGGER IF EXISTS trg_inventory_family_mapping_assets ON finding_catalog_item_inventory_families');
|
||||
await queryRunner.query('DROP FUNCTION IF EXISTS sync_inventory_family_mapping_assets()');
|
||||
await queryRunner.query('DROP TRIGGER IF EXISTS trg_assets_inventory_family_catalog ON assets');
|
||||
await queryRunner.query('DROP FUNCTION IF EXISTS sync_asset_inventory_family_catalog()');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user