Merge pull request #243 from Tria-plc/freight_feature/profile

Freight feature/profile
This commit is contained in:
marshal
2026-06-22 15:49:25 +03:00
committed by GitHub
135 changed files with 8034 additions and 1618 deletions

View File

@@ -0,0 +1,67 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddActiveModeAndOnboardingToExternalProfiles1791000000000
implements MigrationInterface
{
name = 'AddActiveModeAndOnboardingToExternalProfiles1791000000000';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.external_profiles
ADD COLUMN IF NOT EXISTS active_profile_type varchar(32);
`);
await queryRunner.query(`
ALTER TABLE freight.external_profiles
ADD COLUMN IF NOT EXISTS onboarding_step varchar(40);
`);
await queryRunner.query(`
ALTER TABLE freight.external_profiles
ADD COLUMN IF NOT EXISTS onboarding_completed boolean NOT NULL DEFAULT false;
`);
// Existing users already use the portal — never re-gate them behind the
// new onboarding wizard.
await queryRunner.query(`
UPDATE freight.external_profiles
SET onboarding_completed = true
WHERE onboarding_completed = false;
`);
// Backfill the active mode for existing users from their company's
// operational profiles. Prefer importer, then exporter, then whichever
// single profile the company has (forwarder/dj/transporter).
await queryRunner.query(`
UPDATE freight.external_profiles ep
SET active_profile_type = cp.type
FROM (
SELECT DISTINCT ON (company_id) company_id, type
FROM freight.company_profiles
ORDER BY company_id,
CASE type
WHEN 'importer' THEN 0
WHEN 'exporter' THEN 1
ELSE 2
END
) cp
WHERE ep.company_id = cp.company_id
AND ep.active_profile_type IS NULL;
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.external_profiles
DROP COLUMN IF EXISTS onboarding_completed;
`);
await queryRunner.query(`
ALTER TABLE freight.external_profiles
DROP COLUMN IF EXISTS onboarding_step;
`);
await queryRunner.query(`
ALTER TABLE freight.external_profiles
DROP COLUMN IF EXISTS active_profile_type;
`);
}
}

View File

@@ -0,0 +1,94 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddCompanyProfileIdToBookings1791000000001
implements MigrationInterface
{
name = 'AddCompanyProfileIdToBookings1791000000001';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.bookings
ADD COLUMN IF NOT EXISTS company_profile_id UUID;
`);
await queryRunner.query(`
CREATE INDEX IF NOT EXISTS idx_bookings_company_profile_id
ON freight.bookings(company_profile_id);
`);
await queryRunner.query(`
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'FK_bookings_company_profile_id'
) THEN
ALTER TABLE freight.bookings
ADD CONSTRAINT "FK_bookings_company_profile_id"
FOREIGN KEY (company_profile_id)
REFERENCES freight.company_profiles(id);
END IF;
END $$;
`);
// Backfill by natural mapping: IMPORT → importer profile, EXPORT → exporter
// profile, for each booking's own company.
await queryRunner.query(`
UPDATE freight.bookings b
SET company_profile_id = cp.id
FROM freight.company_profiles cp
WHERE cp.company_id = b.company_id
AND b.company_profile_id IS NULL
AND (
(b.trade_direction = 'IMPORT' AND cp.type = 'importer') OR
(b.trade_direction = 'EXPORT' AND cp.type = 'exporter')
);
`);
// Forwarder / single-profile companies: one profile per company, so the
// mapping is unambiguous regardless of trade direction.
await queryRunner.query(`
UPDATE freight.bookings b
SET company_profile_id = cp.id
FROM freight.company_profiles cp
JOIN freight.companies c ON c.id = cp.company_id
WHERE cp.company_id = b.company_id
AND c.type <> 'customer'
AND b.company_profile_id IS NULL;
`);
// Remaining customer-owned rows (e.g. DOMESTIC, or a direction with no
// matching profile): attribute to the company's importer profile, else its
// exporter profile, so nothing disappears from the customer's list.
await queryRunner.query(`
UPDATE freight.bookings b
SET company_profile_id = cp.id
FROM (
SELECT DISTINCT ON (company_id) company_id, id
FROM freight.company_profiles
ORDER BY company_id,
CASE type
WHEN 'importer' THEN 0
WHEN 'exporter' THEN 1
ELSE 2
END
) cp
WHERE cp.company_id = b.company_id
AND b.company_id IS NOT NULL
AND b.company_profile_id IS NULL;
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.bookings
DROP CONSTRAINT IF EXISTS "FK_bookings_company_profile_id";
`);
await queryRunner.query(`
DROP INDEX IF EXISTS freight.idx_bookings_company_profile_id;
`);
await queryRunner.query(`
ALTER TABLE freight.bookings
DROP COLUMN IF EXISTS company_profile_id;
`);
}
}

View File

@@ -0,0 +1,28 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AddNationalityToCompanies1791000000002
implements MigrationInterface
{
name = "AddNationalityToCompanies1791000000002";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS nationality varchar(32);
`);
// Existing companies default to Ethiopian (country defaults to Ethiopia).
await queryRunner.query(`
UPDATE freight.companies
SET nationality = 'ethiopian'
WHERE nationality IS NULL;
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS nationality;
`);
}
}

View File

@@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AddBusinessLicenseFilesToCompanyProfiles1791000000003
implements MigrationInterface
{
name = "AddBusinessLicenseFilesToCompanyProfiles1791000000003";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.company_profiles
ADD COLUMN IF NOT EXISTS business_license_files jsonb;
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.company_profiles
DROP COLUMN IF EXISTS business_license_files;
`);
}
}

View File

@@ -0,0 +1,109 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AddETradeFieldsToCompanies1791000000003
implements MigrationInterface
{
name = "AddETradeFieldsToCompanies1791000000003";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS licence_number varchar(100);
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS status_description text;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS date_registered varchar(50);
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS renewed_from varchar(50);
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS renewal_date varchar(50);
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS renewed_to varchar(50);
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS region varchar(100);
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS zone varchar(100);
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS woreda varchar(100);
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS kebele varchar(100);
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS house_no varchar(100);
`);
await queryRunner.query(`
ALTER TABLE freight.companies
ADD COLUMN IF NOT EXISTS etrade_phone varchar(20);
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS licence_number;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS status_description;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS date_registered;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS renewed_from;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS renewal_date;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS renewed_to;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS region;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS zone;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS woreda;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS kebele;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS house_no;
`);
await queryRunner.query(`
ALTER TABLE freight.companies
DROP COLUMN IF EXISTS etrade_phone;
`);
}
}

View File

@@ -0,0 +1,70 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
/**
* Creates the generic dropdown settings tables (freight.dropdown_settings +
* freight.dropdown_options) backing the DropdownSetting / DropdownOption
* entities. These tables previously only existed via `synchronize` on some
* databases; this migration makes them part of the migration history so the
* SeedGeneralContractPeriod migration (which inserts into them) can run on a
* fresh database. Idempotent so it is safe on DBs where the tables already exist.
*/
export class CreateDropdownSettings1791999999999
implements MigrationInterface
{
name = 'CreateDropdownSettings1791999999999';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE IF NOT EXISTS "freight"."dropdown_settings" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"code" varchar(128) NOT NULL,
"label" varchar(256) NOT NULL,
"description" text,
"multiple" boolean NOT NULL DEFAULT false,
"meta" jsonb,
"created_at" timestamptz NOT NULL DEFAULT now(),
"updated_at" timestamptz NOT NULL DEFAULT now(),
"deleted_at" timestamptz,
CONSTRAINT "PK_dropdown_settings" PRIMARY KEY ("id")
);
`);
await queryRunner.query(`
CREATE UNIQUE INDEX IF NOT EXISTS "UQ_dropdown_settings_code"
ON "freight"."dropdown_settings" ("code");
`);
await queryRunner.query(`
CREATE TABLE IF NOT EXISTS "freight"."dropdown_options" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"setting_id" uuid NOT NULL,
"value" varchar(256) NOT NULL,
"label" varchar(256) NOT NULL,
"note" text,
"is_disabled" boolean NOT NULL DEFAULT false,
"display_order" integer NOT NULL DEFAULT 0,
"meta" jsonb,
"created_at" timestamptz NOT NULL DEFAULT now(),
"updated_at" timestamptz NOT NULL DEFAULT now(),
"deleted_at" timestamptz,
CONSTRAINT "PK_dropdown_options" PRIMARY KEY ("id"),
CONSTRAINT "FK_dropdown_options_setting" FOREIGN KEY ("setting_id")
REFERENCES "freight"."dropdown_settings" ("id") ON DELETE CASCADE
);
`);
await queryRunner.query(`
CREATE UNIQUE INDEX IF NOT EXISTS "UQ_dropdown_options_setting_value"
ON "freight"."dropdown_options" ("setting_id", "value");
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP TABLE IF EXISTS "freight"."dropdown_options";`,
);
await queryRunner.query(
`DROP TABLE IF EXISTS "freight"."dropdown_settings";`,
);
}
}

View File

@@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddUnitOfMeasureToCargoTypes1792000000000
implements MigrationInterface
{
name = 'AddUnitOfMeasureToCargoTypes1792000000000';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE freight.cargo_types ADD COLUMN IF NOT EXISTS unit_of_measure VARCHAR(16);`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE freight.cargo_types DROP COLUMN IF EXISTS unit_of_measure;`,
);
}
}

View File

@@ -0,0 +1,39 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddBookingTypeAndContractFields1792000000001
implements MigrationInterface
{
name = 'AddBookingTypeAndContractFields1792000000001';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE freight.bookings ADD COLUMN IF NOT EXISTS booking_type VARCHAR(20) NOT NULL DEFAULT 'ONE_TIME';`,
);
await queryRunner.query(
`ALTER TABLE freight.bookings ADD COLUMN IF NOT EXISTS expires_at TIMESTAMPTZ;`,
);
// General contracts have no shipment date at creation — relax the NOT NULL.
await queryRunner.query(
`ALTER TABLE freight.bookings ALTER COLUMN scheduled_date DROP NOT NULL;`,
);
await queryRunner.query(
`CREATE INDEX IF NOT EXISTS idx_bookings_booking_type ON freight.bookings (booking_type);`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP INDEX IF EXISTS freight.idx_bookings_booking_type;`,
);
// Reinstate NOT NULL only if no null rows exist (general contracts would block it).
await queryRunner.query(
`ALTER TABLE freight.bookings ALTER COLUMN scheduled_date SET NOT NULL;`,
);
await queryRunner.query(
`ALTER TABLE freight.bookings DROP COLUMN IF EXISTS expires_at;`,
);
await queryRunner.query(
`ALTER TABLE freight.bookings DROP COLUMN IF EXISTS booking_type;`,
);
}
}

View File

@@ -0,0 +1,74 @@
import { MigrationInterface, QueryRunner, Table, TableIndex } from 'typeorm';
export class CreateBookingOrders1792000000002 implements MigrationInterface {
name = 'CreateBookingOrders1792000000002';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
schema: 'freight',
name: 'booking_orders',
columns: [
{ name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'gen_random_uuid()' },
{ name: 'reference', type: 'varchar', length: '64', isUnique: true },
{ name: 'contract_booking_id', type: 'uuid' },
{ name: 'booking_id', type: 'uuid', isNullable: true },
{ name: 'company_id', type: 'uuid', isNullable: true },
{ name: 'scheduled_date', type: 'timestamptz' },
{ name: 'status', type: 'varchar', length: '40', default: "'PAID'" },
{ name: 'scheduling_status', type: 'varchar', length: '30', default: "'NOT_SCHEDULED'" },
{ name: 'train_schedule_id', type: 'uuid', isNullable: true },
{ name: 'created_at', type: 'timestamptz', default: 'now()' },
{ name: 'updated_at', type: 'timestamptz', default: 'now()' },
{ name: 'deleted_at', type: 'timestamptz', isNullable: true },
],
}),
true,
);
await queryRunner.createIndex(
'freight.booking_orders',
new TableIndex({ name: 'idx_booking_orders_contract', columnNames: ['contract_booking_id'] }),
);
await queryRunner.createIndex(
'freight.booking_orders',
new TableIndex({ name: 'idx_booking_orders_company', columnNames: ['company_id'] }),
);
await queryRunner.createTable(
new Table({
schema: 'freight',
name: 'booking_order_lines',
columns: [
{ name: 'id', type: 'uuid', isPrimary: true, generationStrategy: 'uuid', default: 'gen_random_uuid()' },
{ name: 'order_id', type: 'uuid' },
{ name: 'container_type_id', type: 'uuid', isNullable: true },
{ name: 'quantity', type: 'numeric', precision: 12, scale: 3 },
{ name: 'created_at', type: 'timestamptz', default: 'now()' },
{ name: 'updated_at', type: 'timestamptz', default: 'now()' },
{ name: 'deleted_at', type: 'timestamptz', isNullable: true },
],
foreignKeys: [
{
columnNames: ['order_id'],
referencedSchema: 'freight',
referencedTableName: 'booking_orders',
referencedColumnNames: ['id'],
onDelete: 'CASCADE',
},
],
}),
true,
);
await queryRunner.createIndex(
'freight.booking_order_lines',
new TableIndex({ name: 'idx_booking_order_lines_order', columnNames: ['order_id'] }),
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('freight.booking_order_lines', true);
await queryRunner.dropTable('freight.booking_orders', true);
}
}

View File

@@ -0,0 +1,46 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
/**
* Seeds the global "general contract period" setting (months). Stored as a
* dropdown_settings row with a single option whose `value` holds the month count
* so backoffice can manage it through the existing settings UI later.
*/
export class SeedGeneralContractPeriod1792000000003
implements MigrationInterface
{
name = 'SeedGeneralContractPeriod1792000000003';
private readonly code = 'general_contract_period';
public async up(queryRunner: QueryRunner): Promise<void> {
const existing = await queryRunner.query(
`SELECT id FROM freight.dropdown_settings WHERE code = $1 LIMIT 1;`,
[this.code],
);
if (existing.length > 0) return;
const inserted = await queryRunner.query(
`INSERT INTO freight.dropdown_settings (code, label, description, multiple)
VALUES ($1, $2, $3, false)
RETURNING id;`,
[
this.code,
'General Contract Period (months)',
'How many months a general contract stays open for ordering after activation.',
],
);
const settingId = inserted[0].id;
await queryRunner.query(
`INSERT INTO freight.dropdown_options (setting_id, value, label, display_order)
VALUES ($1, $2, $3, 0);`,
[settingId, '3', '3 months'],
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DELETE FROM freight.dropdown_settings WHERE code = $1;`,
[this.code],
);
}
}