From bb5178f2336e7a134094d1d497f7afff209423d9 Mon Sep 17 00:00:00 2001 From: Tristan Slater <1631008+trslater@users.noreply.github.com> Date: Wed, 6 Nov 2024 13:13:15 -0800 Subject: [PATCH] Migrate decision NARU subtypes to tags --- ...-migrate_decision_naru_subtypes_to_tags.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 services/apps/alcs/src/providers/typeorm/migrations/1730926552631-migrate_decision_naru_subtypes_to_tags.ts diff --git a/services/apps/alcs/src/providers/typeorm/migrations/1730926552631-migrate_decision_naru_subtypes_to_tags.ts b/services/apps/alcs/src/providers/typeorm/migrations/1730926552631-migrate_decision_naru_subtypes_to_tags.ts new file mode 100644 index 0000000000..2d33133d1e --- /dev/null +++ b/services/apps/alcs/src/providers/typeorm/migrations/1730926552631-migrate_decision_naru_subtypes_to_tags.ts @@ -0,0 +1,30 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class MigrateDecisionNaruSubtypesToTags1730926552631 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + queryRunner.query(` + with ranked_components as ( + select ad.application_uuid, + adc.naru_subtype_code, + rank() over (partition by ad.application_uuid order by ad.date) + from alcs.application_decision ad + join alcs.application_decision_component adc on adc.application_decision_uuid = ad."uuid" + where ad.is_draft is false + and adc.application_decision_component_type_code = 'NARU' + and adc.naru_subtype_code is not null + ) + insert into alcs.application_tag (application_uuid, tag_uuid) + select rc.application_uuid, t."uuid" + from ranked_components rc + join alcs.tag t on case + when rc.naru_subtype_code = 'ARFU' then 'Additional Residence' + when rc.naru_subtype_code = 'PRIN' then 'Principal Residence > 500m2' + when rc.naru_subtype_code = 'TOUR' then 'Tourism' + end = t.name + where rc.rank = 1 + on conflict do nothing + `); + } + + public async down(queryRunner: QueryRunner): Promise {} +}