Files
edr-platform/apps/edr-freight-api/src/migrations/2000000000000-AddCustomsPriorityConfig.ts

84 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { MigrationInterface, QueryRunner } from 'typeorm';
/**
* Moves service-level priority off the service_types table and onto the
* admin-managed priority_configs table as a new CUSTOMS rule type.
*
* - Drops service_types.priority_bonus_points (replaced by CUSTOMS configs).
* - Widens priority_configs.type CHECK to allow 'CUSTOMS' (currency must be
* null, same as WAGON).
* - Seeds the two customs wagon-count tiers: 110 → 7 pts, 1153 → 15 pts.
* CUSTOMS rules apply only when the booking's service type includesCustoms.
*/
export class AddCustomsPriorityConfig2000000000000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.service_types DROP COLUMN IF EXISTS priority_bonus_points;
`);
await queryRunner.query(`
ALTER TABLE freight.priority_configs
DROP CONSTRAINT IF EXISTS priority_configs_type_check;
`);
await queryRunner.query(`
ALTER TABLE freight.priority_configs
ADD CONSTRAINT priority_configs_type_check
CHECK (type IN ('WAGON', 'CURRENCY', 'CUSTOMS'));
`);
await queryRunner.query(`
ALTER TABLE freight.priority_configs
DROP CONSTRAINT IF EXISTS chk_currency_for_type;
`);
await queryRunner.query(`
ALTER TABLE freight.priority_configs
ADD CONSTRAINT chk_currency_for_type CHECK (
(type = 'WAGON' AND currency IS NULL) OR
(type = 'CURRENCY' AND currency IS NOT NULL) OR
(type = 'CUSTOMS' AND currency IS NULL)
);
`);
await queryRunner.query(`
INSERT INTO freight.priority_configs
(type, label, currency, min_wagon_count, max_wagon_count, score_points, is_active, display_order)
VALUES
('CUSTOMS', 'With customs 110 wagons', NULL, 1, 10, 7, true, 1),
('CUSTOMS', 'With customs 1153 wagons', NULL, 11, 53, 15, true, 2);
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DELETE FROM freight.priority_configs WHERE type = 'CUSTOMS';
`);
await queryRunner.query(`
ALTER TABLE freight.priority_configs
DROP CONSTRAINT IF EXISTS chk_currency_for_type;
`);
await queryRunner.query(`
ALTER TABLE freight.priority_configs
ADD CONSTRAINT chk_currency_for_type CHECK (
(type = 'WAGON' AND currency IS NULL) OR
(type = 'CURRENCY' AND currency IS NOT NULL)
);
`);
await queryRunner.query(`
ALTER TABLE freight.priority_configs
DROP CONSTRAINT IF EXISTS priority_configs_type_check;
`);
await queryRunner.query(`
ALTER TABLE freight.priority_configs
ADD CONSTRAINT priority_configs_type_check
CHECK (type IN ('WAGON', 'CURRENCY'));
`);
await queryRunner.query(`
ALTER TABLE freight.service_types
ADD COLUMN IF NOT EXISTS priority_bonus_points INT NOT NULL DEFAULT 0;
`);
}
}