162 lines
10 KiB
TypeScript
162 lines
10 KiB
TypeScript
import {
|
|
buildImportTableFromRows,
|
|
applyRecipientMappingProfile,
|
|
buildImportTable,
|
|
buildRecipientImportPreview,
|
|
createRecipientImportProvenance,
|
|
createRecipientMappingProfile,
|
|
defaultColumnMappings,
|
|
matchRecipientMappingProfiles,
|
|
materializeRecipientImport,
|
|
parseDelimitedText,
|
|
xlsxSheetsFromArrayBuffer,
|
|
type RecipientColumnMapping
|
|
} from "../src/features/campaigns/utils/bulkImport";
|
|
|
|
function assert(condition: unknown, message = "assertion failed"): void {
|
|
if (!condition) throw new Error(message);
|
|
}
|
|
|
|
function asRecord(value: unknown): Record<string, unknown> {
|
|
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
}
|
|
|
|
function mappingFor(mappings: RecipientColumnMapping[], columnIndex: number): RecipientColumnMapping {
|
|
return mappings.find((mapping) => mapping.columnIndex === columnIndex) ?? { columnIndex, kind: "ignore" };
|
|
}
|
|
|
|
function base64ToArrayBuffer(value: string): ArrayBuffer {
|
|
const binary = atob(value);
|
|
const bytes = new Uint8Array(binary.length);
|
|
for (let index = 0; index < binary.length; index += 1) {
|
|
bytes[index] = binary.charCodeAt(index);
|
|
}
|
|
return bytes.buffer;
|
|
}
|
|
|
|
const parsed = parseDelimitedText('email;name;field.note\n"alice@example.org";"Alice; A";"hello"', ";");
|
|
assert(parsed[1][1] === "Alice; A", "quoted delimiters are preserved");
|
|
|
|
const csv = [
|
|
"email;name;customer_id;field.contract_no;attachment_pattern;active",
|
|
"alice@example.org;Alice;C-1;CN-1;${customer_id}.pdf|archive/*.pdf;yes",
|
|
"bad-address;Broken;C-2;CN-2;broken.pdf;yes",
|
|
";Missing;C-3;CN-3;;yes"
|
|
].join("\n");
|
|
const existingFields = [{ name: "customer_id", label: "Customer ID", type: "string", required: false, can_override: true }];
|
|
const table = buildImportTable(csv, { delimiter: "auto", headerRows: 1, quoted: true });
|
|
const mappings = defaultColumnMappings(table.headers, existingFields);
|
|
const preview = buildRecipientImportPreview(table, mappings, {
|
|
existingFields,
|
|
existingEntries: [{ id: "alice" }],
|
|
valueSeparators: ",;|"
|
|
});
|
|
|
|
assert(table.delimiter === ";", "semicolon delimiter is detected");
|
|
assert(table.headerRows.length === 1 && table.dataRows.length === 3, "header and data rows are separated");
|
|
assert(mappingFor(mappings, 0).kind === "to", "email columns map to To");
|
|
assert(mappingFor(mappings, 2).kind === "field" && mappingFor(mappings, 2).fieldName === "customer_id", "existing fields are detected");
|
|
assert(mappingFor(mappings, 3).kind === "new_field" && mappingFor(mappings, 3).newFieldName === "contract_no", "explicit new field columns are detected");
|
|
|
|
const profile = createRecipientMappingProfile({
|
|
id: "profile-1",
|
|
name: "ERP export",
|
|
table,
|
|
mappings,
|
|
parseOptions: { headerRows: 1, quoted: true },
|
|
valueSeparators: ",;|",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
updatedAt: "2026-01-01T00:00:00.000Z"
|
|
});
|
|
const exactProfileMatches = matchRecipientMappingProfiles(table, [profile]);
|
|
assert(exactProfileMatches[0]?.mode === "ordered" && exactProfileMatches[0].score === 1, "ordered header fingerprints match exactly");
|
|
|
|
const reorderedTable = buildImportTable(
|
|
[
|
|
"name;email;active;attachment_pattern;field.contract_no;customer_id",
|
|
"Alice;alice@example.org;yes;${customer_id}.pdf;CN-1;C-1"
|
|
].join("\n"),
|
|
{ delimiter: "auto", headerRows: 1, quoted: true }
|
|
);
|
|
const reorderedMatches = matchRecipientMappingProfiles(reorderedTable, [profile]);
|
|
assert(reorderedMatches[0]?.mode === "unordered", "unordered header fingerprints match reordered exports");
|
|
const reorderedMappings = applyRecipientMappingProfile(reorderedTable, profile, existingFields);
|
|
assert(mappingFor(reorderedMappings, 0).kind === "name", "reordered profiles apply mappings by header");
|
|
assert(mappingFor(reorderedMappings, 1).kind === "to", "email mapping follows the reordered header");
|
|
assert(mappingFor(reorderedMappings, 4).kind === "new_field" && mappingFor(reorderedMappings, 4).newFieldName === "contract_no", "new field mappings follow reordered headers");
|
|
|
|
const similarTable = buildImportTable(
|
|
[
|
|
"email;name;customer_id;attachment_pattern",
|
|
"alice@example.org;Alice;C-1;${customer_id}.pdf"
|
|
].join("\n"),
|
|
{ delimiter: "auto", headerRows: 1, quoted: true }
|
|
);
|
|
const similarMatches = matchRecipientMappingProfiles(similarTable, [profile]);
|
|
assert(similarMatches[0]?.mode === "similar", "similar profiles are suggested when headers changed");
|
|
|
|
assert(preview.validCount === 1, "one valid row is detected");
|
|
assert(preview.invalidCount === 2, "invalid rows are detected");
|
|
assert(preview.patternCount === 3, "all pattern cells are counted before invalid rows are skipped");
|
|
assert(preview.fieldNamesToCreate.length === 1 && preview.fieldNamesToCreate[0] === "contract_no", "mapped new fields are created");
|
|
assert(preview.rows[0].id === "alice-2", "import ids avoid existing entries");
|
|
assert(preview.rows[0].patterns.length === 2, "pattern cells split on configured separators");
|
|
|
|
const multiTable = buildImportTable('to;name\n"Alice <alice@example.org>|bob@example.org";Team', { delimiter: "auto", headerRows: 1, quoted: true });
|
|
const multiPreview = buildRecipientImportPreview(multiTable, defaultColumnMappings(multiTable.headers, []), {
|
|
existingFields: [],
|
|
valueSeparators: "|"
|
|
});
|
|
assert(multiPreview.validCount === 1, "multi-address cells can produce a valid recipient");
|
|
assert(multiPreview.rows[0].addresses.to.length === 2, "To columns can contain multiple addresses");
|
|
assert(multiPreview.rows[0].addresses.to[0].name === "Alice", "display names are parsed from address cells");
|
|
|
|
const draft = {
|
|
fields: [{ name: "customer_id", label: "Customer ID", type: "string", required: false, can_override: true }],
|
|
entries: { inline: [{ id: "alice", to: [{ email: "old@example.org" }] }], source: { type: "csv" }, mapping: { id: "id" } }
|
|
};
|
|
const imported = materializeRecipientImport(draft, preview, { mode: "append", attachmentBasePath: { id: "bp-1", path: "invoices" } });
|
|
const entries = asRecord(imported.entries);
|
|
const inline = entries.inline as Record<string, unknown>[];
|
|
const fields = imported.fields as Record<string, unknown>[];
|
|
const importedEntry = inline[1];
|
|
const attachments = importedEntry.attachments as Record<string, unknown>[];
|
|
|
|
assert(inline.length === 2, "append keeps existing entries");
|
|
assert(entries.source === undefined && entries.mapping === undefined, "external source metadata is removed for inline imports");
|
|
assert(fields.some((field) => field.name === "contract_no"), "new field definition is added");
|
|
assert(importedEntry.email === "alice@example.org", "entry email is materialized");
|
|
assert(attachments.length === 2, "valid row patterns become attachment rules");
|
|
assert(attachments[0].base_path_id === "bp-1" && attachments[0].base_dir === "invoices", "attachment rules use the selected source");
|
|
assert(attachments[0].file_filter === "${customer_id}.pdf", "field placeholders remain in imported patterns");
|
|
|
|
void runXlsxImportAssertions();
|
|
|
|
async function runXlsxImportAssertions(): Promise<void> {
|
|
const workbookBuffer = base64ToArrayBuffer("UEsDBBQAAAAIACQe2lzFLx19AQEAAC4CAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbK2RzU7DMBCE7zyF5SuKnXJACCXpgZ8jcCgPsDibxIr/5HVL+vY4acsBlZ56Wtk7M9/IrtaTNWyHkbR3NV+JkjN0yrfa9TX/3LwWD5xRAteC8Q5rvkfi6+am2uwDEstmRzUfUgqPUpIa0AIJH9DlTeejhZSPsZcB1Ag9yruyvJfKu4QuFWnO4E31jB1sTWIvU74+FIloiLOng3Bm1RxCMFpBynu5c+0fSnEkiOxcNDToQLdZwOVZwrz5H3D0veeXibpF9gExvYHNKjkZ+e3j+OX9KC6HnGnpu04rbL3a2mwRFCJCSwNiskYsU1jQ7tT7An8Rk1zG6spFfvNPPeTy3c0PUEsDBBQAAAAIACQe2lwGWceCsgAAACgBAAALAAAAX3JlbHMvLnJlbHOFz00KwjAQBeC9pwizt6kuRKRpNyJ0K/UAMZ3+0CQTkqjt7c3SiuBymJnv8YpqNpo90YeRrIBdlgNDq6gdbS/g1ly2R2AhSttKTRYFLBigKjfFFbWM6ScMowssITYIGGJ0J86DGtDIkJFDmzYdeSNjGn3PnVST7JHv8/zA/acB5cpkdSvA1+0OWLO4FPzfpq4bFZ5JPQza+CPi6yL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MUvHX0BAQAALgIAABMAAAAAAAAAAAAAAAAAAAAAAFtDb250ZW50X1R5cGVzXS54bWxQSwECFAAUAAAACAAkHtpcBlnHgrIAAAAoAQAACwAAAAAAAAAAAAAAAAAyAQAAX3JlbHMvLnJlbHNQSwECFAAUAAAACAAkHtpcj5Aw28AAAAAgAQAADwAAAAAAAAAAAAAAAAANAgAAeGwvd29ya2Jvb2sueG1sUEsBAhQAFAAAAAgAJB7aXJpvPHy1AAAAKQEAABoAAAAAAAAAAAAAAAAA+gIAAHhsL19yZWxzL3dvcmtib29rLnhtbC5yZWxzUEsBAhQAFAAAAAgAJB7aXP4EzxPwAAAACAIAABgAAAAAAAAAAAAAAAAA5wMAAHhsL3dvcmtzaGVldHMvc2hlZXQxLnhtbFBLBQYAAAAABQAFAEUBAAANBQAAAAA=");
|
|
const workbookSheets = await xlsxSheetsFromArrayBuffer(workbookBuffer);
|
|
assert(workbookSheets[0]?.name === "Recipients", "xlsx sheets are listed");
|
|
const xlsxTable = buildImportTableFromRows(workbookSheets[0].rows, { headerRows: 1 });
|
|
const xlsxMappings = defaultColumnMappings(xlsxTable.headers, []);
|
|
const xlsxPreview = buildRecipientImportPreview(xlsxTable, xlsxMappings, { existingFields: [], valueSeparators: ",;|" });
|
|
assert(xlsxPreview.validCount === 1, "xlsx rows become import preview rows");
|
|
assert(mappingFor(xlsxMappings, 2).kind === "new_field" && mappingFor(xlsxMappings, 2).newFieldName === "department", "xlsx headers use the same mapping guesser");
|
|
|
|
const provenance = createRecipientImportProvenance({
|
|
preview: xlsxPreview,
|
|
mappings: xlsxMappings,
|
|
mode: "replace",
|
|
sourceType: "xlsx",
|
|
filename: "recipients.xlsx",
|
|
sheetName: "Recipients",
|
|
headerRows: 1,
|
|
valueSeparators: ",;|"
|
|
});
|
|
const importedWithProvenance = materializeRecipientImport(draft, xlsxPreview, { mode: "replace", provenance });
|
|
const provenanceEntries = asRecord(importedWithProvenance.entries);
|
|
const imports = provenanceEntries.imports as Record<string, unknown>[];
|
|
assert(imports.length === 1, "recipient import provenance is stored with entries");
|
|
assert(imports[0].source_type === "xlsx" && imports[0].sheet_name === "Recipients", "xlsx provenance records source details");
|
|
assert((imports[0].mapping as Record<string, unknown>[]).length === xlsxTable.headers.length, "provenance stores column mapping");
|
|
}
|