diff --git a/src/collections/customers.ts b/src/collections/customers.ts index ef729e8..6bb55f2 100644 --- a/src/collections/customers.ts +++ b/src/collections/customers.ts @@ -71,7 +71,7 @@ export function createCustomersCollection(slug: string = 'customers'): Collectio label: 'State/Province', }, { - name: 'postal_code', + name: 'postalCode', type: 'text', label: 'Postal Code', }, diff --git a/src/collections/invoices.ts b/src/collections/invoices.ts index d6412a7..8f57fc9 100644 --- a/src/collections/invoices.ts +++ b/src/collections/invoices.ts @@ -55,6 +55,7 @@ export function createInvoicesCollection( type: 'group', admin: { description: 'Customer billing information', + condition: customerCollectionSlug ? (data: InvoiceData) => !data.customer : undefined, }, fields: [ { @@ -63,7 +64,7 @@ export function createInvoicesCollection( admin: { description: 'Customer name', }, - required: true, + required: !customerCollectionSlug, }, { name: 'email', @@ -71,7 +72,7 @@ export function createInvoicesCollection( admin: { description: 'Customer email address', }, - required: true, + required: !customerCollectionSlug, }, { name: 'phone', @@ -101,6 +102,7 @@ export function createInvoicesCollection( type: 'group', admin: { description: 'Billing address', + condition: customerCollectionSlug ? (data: InvoiceData) => !data.customer : undefined, }, fields: [ { @@ -109,7 +111,7 @@ export function createInvoicesCollection( admin: { description: 'Address line 1', }, - required: true, + required: !customerCollectionSlug, }, { name: 'line2', @@ -121,7 +123,7 @@ export function createInvoicesCollection( { name: 'city', type: 'text', - required: true, + required: !customerCollectionSlug, }, { name: 'state', @@ -136,7 +138,7 @@ export function createInvoicesCollection( admin: { description: 'Postal or ZIP code', }, - required: true, + required: !customerCollectionSlug, }, { name: 'country', @@ -145,7 +147,7 @@ export function createInvoicesCollection( description: 'Country code (e.g., US, GB)', }, maxLength: 2, - required: true, + required: !customerCollectionSlug, }, ], }, @@ -329,6 +331,18 @@ export function createInvoicesCollection( ], beforeValidate: [ ({ data }: CollectionBeforeValidateHook) => { + if (!data) return + + // Validate customer data: either relationship or embedded info must be provided + if (customerCollectionSlug && !data.customer && (!data.customerInfo?.name || !data.customerInfo?.email)) { + throw new Error('Either select a customer or provide customer information') + } + + // If no customer collection, ensure customer info is provided + if (!customerCollectionSlug && (!data.customerInfo?.name || !data.customerInfo?.email)) { + throw new Error('Customer name and email are required') + } + if (data && data.items && Array.isArray(data.items)) { // Calculate totals for each line item data.items = data.items.map((item: InvoiceItemData) => ({ diff --git a/src/types/index.ts b/src/types/index.ts index 9397668..64b22a1 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -140,7 +140,7 @@ export interface CustomerRecord { country?: string line1?: string line2?: string - postal_code?: string + postalCode?: string state?: string } createdAt: string diff --git a/src/types/payload.ts b/src/types/payload.ts index 8cd42ec..a4f2582 100644 --- a/src/types/payload.ts +++ b/src/types/payload.ts @@ -97,7 +97,7 @@ export interface CustomerData { country?: string line1?: string line2?: string - postal_code?: string + postalCode?: string state?: string } email?: string @@ -142,15 +142,15 @@ export interface InvoiceDocument extends InvoiceData { amount: number createdAt: string currency: string - customer?: string // Now optional - customerInfo: { + customer?: string // Optional relationship + customerInfo?: { // Optional when customer relationship exists company?: string email: string name: string phone?: string taxId?: string } - billingAddress: { + billingAddress?: { // Optional when customer relationship exists city: string country: string line1: string