mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-08 21:48:18 +00:00
fix issue, add transit flow, fix cancellation
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
/**
|
||||
* Give a transit agent a portal login.
|
||||
*
|
||||
* Every column is NULLABLE and nothing is backfilled: production already holds
|
||||
* transit agents that exist only as a GL-assignable roster entry, and they must
|
||||
* keep working untouched. An agent gains an account when staff invite it — at
|
||||
* which point `user_id` is filled in — so "has a login" is exactly
|
||||
* `user_id IS NOT NULL`, and the assignment flow never has to care.
|
||||
*
|
||||
* The unique indexes are partial (`WHERE ... IS NOT NULL`) because Postgres
|
||||
* treats NULLs as distinct in a plain unique index only per-row; being explicit
|
||||
* documents that many account-less agents are expected to coexist.
|
||||
*/
|
||||
export class TransitAgentAccount3790000000000 implements MigrationInterface {
|
||||
name = "TransitAgentAccount3790000000000";
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE freight.transit_agents
|
||||
ADD COLUMN IF NOT EXISTS user_id uuid,
|
||||
ADD COLUMN IF NOT EXISTS email varchar(150),
|
||||
ADD COLUMN IF NOT EXISTS phone_number varchar(30)`,
|
||||
);
|
||||
// One IAM account can back at most one transit agent — otherwise a single
|
||||
// login would resolve to two agents in `findByUserId`.
|
||||
await queryRunner.query(
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS ux_transit_agents_user_id
|
||||
ON freight.transit_agents (user_id)
|
||||
WHERE user_id IS NOT NULL AND deleted_at IS NULL`,
|
||||
);
|
||||
// Case-insensitive, matching how the repository checks for duplicates.
|
||||
await queryRunner.query(
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS ux_transit_agents_email
|
||||
ON freight.transit_agents (lower(email))
|
||||
WHERE email IS NOT NULL AND deleted_at IS NULL`,
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`DROP INDEX IF EXISTS freight.ux_transit_agents_email`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`DROP INDEX IF EXISTS freight.ux_transit_agents_user_id`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE freight.transit_agents
|
||||
DROP COLUMN IF EXISTS phone_number,
|
||||
DROP COLUMN IF EXISTS email,
|
||||
DROP COLUMN IF EXISTS user_id`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
/**
|
||||
* Wagon footprint pinned for cancellation pricing. `wagons_required` is a LIVE
|
||||
* scheduling field — unassign clears it to NULL — so a paid booking pulled off
|
||||
* a train had nothing left to price a cancellation fee or credit against
|
||||
* ("This booking has no wagon requirement to cancel from."). This column is
|
||||
* stamped once, at first allocation, and never cleared: cancellation reads it
|
||||
* (falling back to a computed count for bookings never allocated).
|
||||
*/
|
||||
export class BookingCancellationWagons3800000000000 implements MigrationInterface {
|
||||
name = 'BookingCancellationWagons3800000000000';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE freight.bookings
|
||||
ADD COLUMN IF NOT EXISTS cancellation_wagons numeric(6,2)
|
||||
`);
|
||||
// Backfill the bookings that still carry a live stamp.
|
||||
await queryRunner.query(`
|
||||
UPDATE freight.bookings
|
||||
SET cancellation_wagons = wagons_required
|
||||
WHERE cancellation_wagons IS NULL
|
||||
AND wagons_required IS NOT NULL
|
||||
AND wagons_required > 0
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE freight.bookings
|
||||
DROP COLUMN IF EXISTS cancellation_wagons
|
||||
`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user