feat: add government support to the companies. add seeding and constrain on booking

This commit is contained in:
Nathnael
2026-06-27 09:08:15 +00:00
parent 55c42058d0
commit 98e34593c4
14 changed files with 495 additions and 23 deletions

View File

@@ -0,0 +1,136 @@
import {
CompanyKind,
CompanyStatus,
CompanyType,
} from "../../modules/companies/entities/company.entity";
import {
ProfileStatus,
ProfileType,
} from "../../modules/companies/entities/company-profile.entity";
/**
* Canonical list of seeded Ethiopian government entities. Government bookings
* are billed to one of these (with an explicit importer/exporter profile)
* instead of carrying a null company + free-text institution.
*
* IDs are fixed so the seeder is idempotent and the matching migration
* (1821000000003-AddCompanyKindAndGovBookingLinks) can backfill legacy rows to
* the same companies. The migration mirrors these rows in raw SQL — keep both
* in sync when adding new entities.
*/
export const GOV_COMPANY_TYPE = CompanyType.Customer;
export const GOV_COMPANY_KIND = CompanyKind.Government;
export const GOV_COMPANY_STATUS = CompanyStatus.Active;
export const GOV_PROFILE_STATUS = ProfileStatus.Active;
export interface GovProfileSeed {
id: string;
type: ProfileType;
reference: string;
}
export interface GovCompanySeed {
id: string;
name: string;
tin: string;
email: string;
phone: string;
profiles: GovProfileSeed[];
}
const importExport = (
index: number,
importerId: string,
exporterId: string,
): GovProfileSeed[] => [
{
id: importerId,
type: ProfileType.importer,
reference: `IM-9000${index}`,
},
{
id: exporterId,
type: ProfileType.exporter,
reference: `EX-9000${index}`,
},
];
export const GOV_COMPANIES: GovCompanySeed[] = [
{
id: "0a1b0001-0000-4000-8000-000000000001",
name: "Federal Government of Ethiopia",
tin: "0000000001",
email: "procurement@gov.et",
phone: "+251111000001",
profiles: importExport(
1,
"0b1c0001-0000-4000-8000-000000000001",
"0b1c0001-0000-4000-8000-000000000002",
),
},
{
id: "0a1b0002-0000-4000-8000-000000000002",
name: "Ministry of National Defense",
tin: "0000000002",
email: "logistics@mod.gov.et",
phone: "+251111000002",
profiles: importExport(
2,
"0b1c0002-0000-4000-8000-000000000001",
"0b1c0002-0000-4000-8000-000000000002",
),
},
{
id: "0a1b0003-0000-4000-8000-000000000003",
name: "Ethiopian Roads Administration",
tin: "0000000003",
email: "supply@era.gov.et",
phone: "+251111000003",
profiles: importExport(
3,
"0b1c0003-0000-4000-8000-000000000001",
"0b1c0003-0000-4000-8000-000000000002",
),
},
{
id: "0a1b0004-0000-4000-8000-000000000004",
name: "Ministry of Agriculture",
tin: "0000000004",
email: "imports@moa.gov.et",
phone: "+251111000004",
profiles: importExport(
4,
"0b1c0004-0000-4000-8000-000000000001",
"0b1c0004-0000-4000-8000-000000000002",
),
},
{
id: "0a1b0005-0000-4000-8000-000000000005",
name: "Ministry of Trade and Regional Integration",
tin: "0000000005",
email: "trade@motri.gov.et",
phone: "+251111000005",
profiles: importExport(
5,
"0b1c0005-0000-4000-8000-000000000001",
"0b1c0005-0000-4000-8000-000000000002",
),
},
{
id: "0a1b0006-0000-4000-8000-000000000006",
name: "Ethiopian Disaster Risk Management Commission",
tin: "0000000006",
email: "relief@edrmc.gov.et",
phone: "+251111000006",
profiles: importExport(
6,
"0b1c0006-0000-4000-8000-000000000001",
"0b1c0006-0000-4000-8000-000000000002",
),
},
];
/** Fallback entity used to backfill legacy government / null-company bookings. */
export const DEFAULT_GOV_COMPANY = GOV_COMPANIES[0];
export const DEFAULT_GOV_IMPORTER_PROFILE = GOV_COMPANIES[0].profiles[0];

View File

@@ -0,0 +1,72 @@
import { Injectable, Logger } from "@nestjs/common";
import { DataSource } from "typeorm";
import { Company } from "../modules/companies/entities/company.entity";
import { CompanyProfile } from "../modules/companies/entities/company-profile.entity";
import {
GOV_COMPANIES,
GOV_COMPANY_KIND,
GOV_COMPANY_STATUS,
GOV_COMPANY_TYPE,
GOV_PROFILE_STATUS,
} from "./data/gov-companies.data";
/**
* Idempotently seeds the Ethiopian government entities (with importer + exporter
* profiles) that government bookings bill to. Safe to re-run — rows are keyed by
* the fixed IDs in {@link GOV_COMPANIES}; existing rows are left untouched.
*/
@Injectable()
export class GovCompaniesSeeder {
private readonly logger = new Logger(GovCompaniesSeeder.name);
constructor(private readonly dataSource: DataSource) {}
async run(): Promise<void> {
await this.dataSource.transaction(async (manager) => {
const companyRepo = manager.getRepository(Company);
const profileRepo = manager.getRepository(CompanyProfile);
for (const gov of GOV_COMPANIES) {
const existing = await companyRepo.findOne({ where: { id: gov.id } });
if (!existing) {
await companyRepo.save(
companyRepo.create({
id: gov.id,
name: gov.name,
type: GOV_COMPANY_TYPE,
kind: GOV_COMPANY_KIND,
status: GOV_COMPANY_STATUS,
tin: gov.tin,
country: "Ethiopia",
email: gov.email,
phone: gov.phone,
}),
);
this.logger.log(`Created government company: ${gov.name}`);
}
for (const profile of gov.profiles) {
const existingProfile = await profileRepo.findOne({
where: { id: profile.id },
});
if (existingProfile) continue;
await profileRepo.save(
profileRepo.create({
id: profile.id,
companyId: gov.id,
type: profile.type,
reference: profile.reference,
status: GOV_PROFILE_STATUS,
}),
);
this.logger.log(
`Created ${profile.type} profile ${profile.reference} for ${gov.name}`,
);
}
}
});
this.logger.log("Government companies seeded.");
}
}