mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-08 22:58:17 +00:00
77 lines
2.8 KiB
TypeScript
77 lines
2.8 KiB
TypeScript
import { MigrationInterface, QueryRunner } from 'typeorm';
|
|
|
|
import { CONTRACT_TEMPLATE_DEFAULTS } from '../seed/data/contract-template-defaults';
|
|
|
|
/**
|
|
* Contract paper for empty container import.
|
|
*
|
|
* - contracts.cargo_condition mirrors bookings.cargo_condition, so a general
|
|
* contract can commit to moving bare equipment.
|
|
* - Seeds IMPORT_EMPTY_CONTAINER, the system template the document renderer
|
|
* resolves for those contracts. It carries no customs variant: an empty box
|
|
* has no declaration to clear, the same reason intercity is unsuffixed.
|
|
*/
|
|
const SEEDED_CODES = ['IMPORT_EMPTY_CONTAINER'] as const;
|
|
|
|
export class EmptyContainerContractTemplate3910000000000 implements MigrationInterface {
|
|
name = 'EmptyContainerContractTemplate3910000000000';
|
|
|
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
|
await queryRunner.query(`
|
|
ALTER TABLE freight.contracts
|
|
ADD COLUMN IF NOT EXISTS cargo_condition varchar(10) NOT NULL DEFAULT 'LADEN'
|
|
`);
|
|
|
|
await queryRunner.query(`
|
|
ALTER TABLE freight.contracts
|
|
DROP CONSTRAINT IF EXISTS "CK_contracts_cargo_condition"
|
|
`);
|
|
await queryRunner.query(`
|
|
ALTER TABLE freight.contracts
|
|
ADD CONSTRAINT "CK_contracts_cargo_condition" CHECK (
|
|
cargo_condition IN ('LADEN', 'EMPTY')
|
|
AND (cargo_condition = 'LADEN' OR freight_type = 'CONTAINER')
|
|
)
|
|
`);
|
|
|
|
for (const code of SEEDED_CODES) {
|
|
const seed = CONTRACT_TEMPLATE_DEFAULTS.find((t) => t.code === code);
|
|
if (!seed) throw new Error(`Missing contract template default for ${code}`);
|
|
await queryRunner.query(
|
|
`INSERT INTO freight.contract_templates
|
|
(id, code, name, description, document_title, whereas_clauses, articles,
|
|
is_active, is_system, created_at, updated_at)
|
|
SELECT gen_random_uuid(), $1::varchar, $2, $3, $4, $5::jsonb, $6::jsonb,
|
|
true, true, now(), now()
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM freight.contract_templates
|
|
WHERE code = $1::varchar AND deleted_at IS NULL
|
|
)`,
|
|
[
|
|
seed.code,
|
|
seed.name,
|
|
seed.description,
|
|
seed.documentTitle,
|
|
JSON.stringify(seed.whereasClauses),
|
|
JSON.stringify(
|
|
seed.articles.map((article, index) => ({ ...article, order: index + 1 })),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
|
await queryRunner.query(
|
|
`DELETE FROM freight.contract_templates WHERE code = ANY($1::varchar[]) AND is_system = true`,
|
|
[[...SEEDED_CODES]],
|
|
);
|
|
await queryRunner.query(
|
|
`ALTER TABLE freight.contracts DROP CONSTRAINT IF EXISTS "CK_contracts_cargo_condition"`,
|
|
);
|
|
await queryRunner.query(
|
|
`ALTER TABLE freight.contracts DROP COLUMN IF EXISTS cargo_condition`,
|
|
);
|
|
}
|
|
}
|