feat(customers): carry contact person and invoice copy recipients through migration (#1392)
* feat(customers): carry contact person and invoice copy recipients through migration Extends the arcim-migration entity mapper, Fortnox provider mapper, canonical DTOs, customer APIs (web + v1) and invoice send flows so contact person and customer-level invoice CC/BCC addresses survive provider migrations. NULL means unconfigured and empty means an explicit clear, so re-syncs enrich legacy gaps without resurrecting deliberately removed values. Fortnox fixed assets are split into a dedicated follow-up issue. Fixes #1345 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * chore(db): bump customer metadata migration past pack-slug version Main already contains 20260803230000; keep new versions strictly newest so Supabase branching applies them in order. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(customers): complete Customer type consumers and make enrichment payload resolvable The preview-pdf mock customer and the makeCustomer fixture now carry the three new metadata fields, fixing the type-check failure in Build (zero extensions) and Vercel. The enrichment update in the migration orchestrator now spells its payload as an object literal typed CustomerMetadataEnrichment (absent keys drop at serialization), so the phantom-column guard resolves the columns instead of counting another unresolvable dynamic payload past its ceiling. The cc/bcc guards also verify element types instead of casting. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
cb3ef45f14
commit
00ae3540db
@@ -219,6 +219,8 @@ export interface CustomerDto {
|
||||
customerNumber: string;
|
||||
type?: CustomerType;
|
||||
party: PartyDto;
|
||||
invoiceEmailCcAddresses?: string[];
|
||||
invoiceEmailBccAddresses?: string[];
|
||||
deliveryAddresses?: PostalAddress[];
|
||||
financialDimensions?: FinancialDimensionRef[];
|
||||
active: boolean;
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { mapFortnoxToCustomer } from '../mapper'
|
||||
|
||||
describe('Fortnox customer metadata mapping', () => {
|
||||
it('prefers invoice email and carries reference plus CC/BCC recipients', () => {
|
||||
const dto = mapFortnoxToCustomer({
|
||||
CustomerNumber: '1001',
|
||||
Name: 'Kund AB',
|
||||
Email: 'general@example.test',
|
||||
EmailInvoice: 'invoice@example.test',
|
||||
EmailInvoiceCC: 'finance@example.test; owner@example.test, FINANCE@example.test',
|
||||
EmailInvoiceBCC: ['archive@example.test', 'audit@example.test'],
|
||||
YourReference: 'Anna Andersson',
|
||||
Active: true,
|
||||
})
|
||||
|
||||
expect(dto.party.contact).toMatchObject({
|
||||
name: 'Anna Andersson',
|
||||
email: 'invoice@example.test',
|
||||
})
|
||||
expect(dto.invoiceEmailCcAddresses).toEqual([
|
||||
'finance@example.test',
|
||||
'owner@example.test',
|
||||
])
|
||||
expect(dto.invoiceEmailBccAddresses).toEqual([
|
||||
'archive@example.test',
|
||||
'audit@example.test',
|
||||
])
|
||||
})
|
||||
|
||||
it('falls back to the general email and preserves absent copy fields as undefined', () => {
|
||||
const dto = mapFortnoxToCustomer({
|
||||
CustomerNumber: '1002',
|
||||
Name: 'Kund Två AB',
|
||||
Email: 'general@example.test',
|
||||
})
|
||||
|
||||
expect(dto.party.contact?.email).toBe('general@example.test')
|
||||
expect(dto.invoiceEmailCcAddresses).toBeUndefined()
|
||||
expect(dto.invoiceEmailBccAddresses).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -14,6 +14,33 @@ function amount(value: number | undefined | null, currency: string = 'SEK'): Amo
|
||||
return { value: value ?? 0, currencyCode: currency };
|
||||
}
|
||||
|
||||
function nonEmptyString(value: unknown): string | undefined {
|
||||
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
|
||||
}
|
||||
|
||||
function providerEmailAddresses(
|
||||
raw: Record<string, unknown>,
|
||||
field: string,
|
||||
): string[] | undefined {
|
||||
if (!(field in raw)) return undefined;
|
||||
|
||||
const value = raw[field];
|
||||
const parts = Array.isArray(value)
|
||||
? value.flatMap((item) => typeof item === 'string' ? item.split(/[\n,;]+/) : [])
|
||||
: typeof value === 'string'
|
||||
? value.split(/[\n,;]+/)
|
||||
: [];
|
||||
const seen = new Set<string>();
|
||||
|
||||
return parts.flatMap((part) => {
|
||||
const address = part.trim();
|
||||
const key = address.toLocaleLowerCase('en-US');
|
||||
if (!key || seen.has(key)) return [];
|
||||
seen.add(key);
|
||||
return [address];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Single source of truth for "is this invoice fully settled?", used by BOTH
|
||||
* deriveInvoiceStatus and the paymentStatus.paid flag so they can never diverge.
|
||||
@@ -52,8 +79,11 @@ function buildParty(name: string, orgNumber?: string, address?: Record<string, u
|
||||
companyIdSchemeId: 'SE:ORGNR',
|
||||
} : undefined,
|
||||
contact: {
|
||||
email: (address?.['Email'] ?? address?.['EmailInvoice']) as string | undefined,
|
||||
telephone: address?.['Phone1'] as string | undefined,
|
||||
name: nonEmptyString(address?.['YourReference']),
|
||||
// EmailInvoice is the delivery address. Email is the general contact
|
||||
// fallback and must not override an invoice-specific address.
|
||||
email: nonEmptyString(address?.['EmailInvoice']) ?? nonEmptyString(address?.['Email']),
|
||||
telephone: nonEmptyString(address?.['Phone1']),
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -188,6 +218,8 @@ export function mapFortnoxToCustomer(raw: Record<string, unknown>): CustomerDto
|
||||
customerNumber: String(raw['CustomerNumber'] ?? ''),
|
||||
type: raw['Type'] === 'PRIVATE' ? 'private' : 'company',
|
||||
party: buildParty(name, orgNumber, raw),
|
||||
invoiceEmailCcAddresses: providerEmailAddresses(raw, 'EmailInvoiceCC'),
|
||||
invoiceEmailBccAddresses: providerEmailAddresses(raw, 'EmailInvoiceBCC'),
|
||||
active: raw['Active'] !== false,
|
||||
vatNumber: raw['VATNumber'] as string | undefined,
|
||||
defaultPaymentTermsDays: raw['TermsOfPayment'] != null ? Number(raw['TermsOfPayment']) : undefined,
|
||||
|
||||
Reference in New Issue
Block a user