chore: fix script and add seed company

This commit is contained in:
ghost2023
2026-07-01 15:46:25 +03:00
parent aef868bc40
commit 174df0b7aa
5 changed files with 239 additions and 236 deletions

View File

@@ -13,35 +13,6 @@ import { ContractRoute } from "../../modules/contracts/entities/contract-route.e
import { ContractCargoScope } from "../../modules/contracts/entities/contract-cargo-scope.entity";
import { ContractRateSnapshot } from "../../modules/contracts/entities/contract-rate-snapshot.entity";
const COMPANY_IDS = [
"a0000001-0000-4000-8000-000000000001",
"a0000001-0000-4000-8000-000000000002",
"a0000001-0000-4000-8000-000000000003",
"a0000001-0000-4000-8000-000000000004",
];
const PROFILE_IDS = [
"b0000001-0000-4000-8000-000000000001",
"b0000001-0000-4000-8000-000000000002",
"b0000001-0000-4000-8000-000000000003",
"b0000001-0000-4000-8000-000000000004",
"b0000001-0000-4000-8000-000000000005",
"b0000001-0000-4000-8000-000000000006",
"b0000001-0000-4000-8000-000000000007",
"b0000001-0000-4000-8000-000000000008",
];
const EXTERNAL_PROFILE_IDS = [
"c0000001-0000-4000-8000-000000000001",
"c0000001-0000-4000-8000-000000000002",
"c0000001-0000-4000-8000-000000000003",
"c0000001-0000-4000-8000-000000000004",
];
function generateRef(index: number): string {
return `TST-CTR-${String(index).padStart(5, "0")}`;
}
export function registerSeedTestContracts(
vorpal: Vorpal,
ctx: CommandContext,
@@ -124,7 +95,15 @@ export function registerSeedTestContracts(
}
const contractRepo = ds.getRepository(Contract);
let contractCount = 0;
const maxRaw = await ds.query(
`SELECT "reference" FROM "freight"."contracts" WHERE "reference" LIKE 'TST-CTR-%' AND "deleted_at" IS NULL ORDER BY "reference" DESC LIMIT 1`,
);
let nextRef = 1;
if (maxRaw.length > 0) {
const num = parseInt(maxRaw[0].reference.replace("TST-CTR-", ""), 10);
if (!isNaN(num)) nextRef = num + 1;
}
for (let i = 0; i < count; i++) {
const statusIdx = i % statusList.length;
@@ -145,13 +124,7 @@ export function registerSeedTestContracts(
});
if (!profile) continue;
const ref = generateRef(i + 1);
const exists = await contractRepo.findOne({ where: { reference: ref } });
if (exists) {
this.log(` Contract ${ref} already exists — skipping`);
contractCount++;
continue;
}
const ref = `TST-CTR-${String(nextRef + i).padStart(5, "0")}`;
const serviceTypeId =
freightType === "BULK" && railBulk
@@ -239,54 +212,52 @@ export function registerSeedTestContracts(
);
}
contractCount++;
this.log(` Created ${status} ${freightType} ${direction} contract: ${ref} (${company.name})`);
}
this.log(`Done — ${contractCount} contracts ensured`);
this.log(`Done — ${count} new contracts created`);
});
}
interface CompanySeed {
id: string;
name: string;
tin: string;
profiles: Array<{ id: string; type: ProfileType; reference: string }>;
externalProfile: { id: string; userId: string; firstName: string; lastName: string };
profiles: Array<{ type: ProfileType; reference: string }>;
externalProfile: { userId: string; firstName: string; lastName: string };
}
const TEST_COMPANIES: CompanySeed[] = [
{
id: COMPANY_IDS[0], name: "Test Importer Co.", tin: "TST000001",
name: "Test Importer Co.", tin: "TST000001",
profiles: [
{ id: PROFILE_IDS[0], type: ProfileType.importer, reference: "TST-IM-001" },
{ id: PROFILE_IDS[1], type: ProfileType.exporter, reference: "TST-EX-001" },
{ type: ProfileType.importer, reference: "TST-IM-001" },
{ type: ProfileType.exporter, reference: "TST-EX-001" },
],
externalProfile: { id: EXTERNAL_PROFILE_IDS[0], userId: "00000000-0000-0000-0000-000000000001", firstName: "Abebe", lastName: "Kebede" },
externalProfile: { userId: "00000000-0000-0000-0000-000000000001", firstName: "Abebe", lastName: "Kebede" },
},
{
id: COMPANY_IDS[1], name: "Test Exporter Ltd.", tin: "TST000002",
name: "Test Exporter Ltd.", tin: "TST000002",
profiles: [
{ id: PROFILE_IDS[2], type: ProfileType.importer, reference: "TST-IM-002" },
{ id: PROFILE_IDS[3], type: ProfileType.exporter, reference: "TST-EX-002" },
{ type: ProfileType.importer, reference: "TST-IM-002" },
{ type: ProfileType.exporter, reference: "TST-EX-002" },
],
externalProfile: { id: EXTERNAL_PROFILE_IDS[1], userId: "00000000-0000-0000-0000-000000000002", firstName: "Bekele", lastName: "Alemu" },
externalProfile: { userId: "00000000-0000-0000-0000-000000000002", firstName: "Bekele", lastName: "Alemu" },
},
{
id: COMPANY_IDS[2], name: "Bulk Commodities PLC", tin: "TST000003",
name: "Bulk Commodities PLC", tin: "TST000003",
profiles: [
{ id: PROFILE_IDS[4], type: ProfileType.importer, reference: "TST-IM-003" },
{ id: PROFILE_IDS[5], type: ProfileType.exporter, reference: "TST-EX-003" },
{ type: ProfileType.importer, reference: "TST-IM-003" },
{ type: ProfileType.exporter, reference: "TST-EX-003" },
],
externalProfile: { id: EXTERNAL_PROFILE_IDS[2], userId: "00000000-0000-0000-0000-000000000003", firstName: "Chala", lastName: "Tesfaye" },
externalProfile: { userId: "00000000-0000-0000-0000-000000000003", firstName: "Chala", lastName: "Tesfaye" },
},
{
id: COMPANY_IDS[3], name: "Hazardous Logistics Inc.", tin: "TST000004",
name: "Hazardous Logistics Inc.", tin: "TST000004",
profiles: [
{ id: PROFILE_IDS[6], type: ProfileType.importer, reference: "TST-IM-004" },
{ id: PROFILE_IDS[7], type: ProfileType.exporter, reference: "TST-EX-004" },
{ type: ProfileType.importer, reference: "TST-IM-004" },
{ type: ProfileType.exporter, reference: "TST-EX-004" },
],
externalProfile: { id: EXTERNAL_PROFILE_IDS[3], userId: "00000000-0000-0000-0000-000000000004", firstName: "Desta", lastName: "Hailu" },
externalProfile: { userId: "00000000-0000-0000-0000-000000000004", firstName: "Desta", lastName: "Hailu" },
},
];
@@ -297,11 +268,10 @@ async function seedTestCompanies(ds: DataSource, log: (msg: string) => void): Pr
const result: Company[] = [];
for (const seed of TEST_COMPANIES) {
let company = await companyRepo.findOne({ where: { id: seed.id } });
let company = await companyRepo.findOne({ where: { tin: seed.tin } });
if (!company) {
company = await companyRepo.save(
companyRepo.create({
id: seed.id,
name: seed.name,
type: CompanyType.Customer,
kind: CompanyKind.Commercial,
@@ -319,12 +289,13 @@ async function seedTestCompanies(ds: DataSource, log: (msg: string) => void): Pr
}
for (const p of seed.profiles) {
const existing = await profileRepo.findOne({ where: { id: p.id } });
const existing = await profileRepo.findOne({
where: { companyId: company.id, type: p.type },
});
if (!existing) {
await profileRepo.save(
profileRepo.create({
id: p.id,
companyId: seed.id,
companyId: company.id,
type: p.type,
reference: p.reference,
status: ProfileStatus.Active,
@@ -335,13 +306,14 @@ async function seedTestCompanies(ds: DataSource, log: (msg: string) => void): Pr
}
const ext = seed.externalProfile;
const existingExt = await extProfileRepo.findOne({ where: { id: ext.id } });
const existingExt = await extProfileRepo.findOne({
where: { companyId: company.id, userId: ext.userId },
});
if (!existingExt) {
await extProfileRepo.save(
extProfileRepo.create({
id: ext.id,
userId: ext.userId,
companyId: seed.id,
companyId: company.id,
firstName: ext.firstName,
lastName: ext.lastName,
isPrimaryContact: true,