mirror of
https://github.com/xtr-dev/payload-billing.git
synced 2025-12-10 02:43:24 +00:00
refactor: Replace hardcoded billing data seeding with plugin-configurable collection overrides
- Remove `seedBillingData` function for sample data creation - Update refunds, invoices, and payments collections to use pluginConfig for dynamic overrides - Introduce utility functions like `extractSlug` for customizable collection slugs - Streamline customer relation and data extractor logic across collections
This commit is contained in:
@@ -2,12 +2,12 @@ import { sqliteAdapter } from '@payloadcms/db-sqlite'
|
||||
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
||||
import path from 'path'
|
||||
import { buildConfig } from 'payload'
|
||||
import { billingPlugin, defaultCustomerInfoExtractor } from '../dist/index.js'
|
||||
import sharp from 'sharp'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
import { testEmailAdapter } from './helpers/testEmailAdapter'
|
||||
import { seed } from './seed'
|
||||
import billingPlugin from '../src/plugin'
|
||||
|
||||
const filename = fileURLToPath(import.meta.url)
|
||||
const dirname = path.dirname(filename)
|
||||
@@ -56,27 +56,29 @@ const buildConfigWithSQLite = () => {
|
||||
},
|
||||
collections: {
|
||||
payments: 'payments',
|
||||
customers: 'customers',
|
||||
invoices: 'invoices',
|
||||
refunds: 'refunds',
|
||||
// customerRelation: false, // Set to false to disable customer relationship in invoices
|
||||
// customerRelation: 'clients', // Or set to a custom collection slug
|
||||
},
|
||||
// Use the default extractor for the built-in customer collection
|
||||
customerInfoExtractor: defaultCustomerInfoExtractor,
|
||||
// Or provide a custom extractor for your own customer collection structure:
|
||||
// // Customer relationship configuration
|
||||
// customerRelationSlug: 'customers', // Use 'customers' collection for relationship
|
||||
// // customerRelationSlug: false, // Or set to false to disable customer relationship
|
||||
// // customerRelationSlug: 'clients', // Or use a custom collection slug
|
||||
//
|
||||
// // Provide an extractor for your customer collection structure:
|
||||
// customerInfoExtractor: (customer) => ({
|
||||
// name: customer.fullName,
|
||||
// email: customer.contactEmail,
|
||||
// phone: customer.phoneNumber,
|
||||
// company: customer.companyName,
|
||||
// taxId: customer.vatNumber,
|
||||
// billingAddress: {
|
||||
// line1: customer.billing.street,
|
||||
// city: customer.billing.city,
|
||||
// postalCode: customer.billing.zip,
|
||||
// country: customer.billing.countryCode,
|
||||
// }
|
||||
// name: customer.name || '',
|
||||
// email: customer.email || '',
|
||||
// phone: customer.phone,
|
||||
// company: customer.company,
|
||||
// taxId: customer.taxId,
|
||||
// billingAddress: customer.address ? {
|
||||
// line1: customer.address.line1 || '',
|
||||
// line2: customer.address.line2,
|
||||
// city: customer.address.city || '',
|
||||
// state: customer.address.state,
|
||||
// postalCode: customer.address.postalCode || '',
|
||||
// country: customer.address.country || '',
|
||||
// } : undefined,
|
||||
// })
|
||||
}),
|
||||
],
|
||||
|
||||
120
dev/seed.ts
120
dev/seed.ts
@@ -26,124 +26,4 @@ export const seed = async (payload: Payload) => {
|
||||
|
||||
async function seedBillingData(payload: Payload): Promise<void> {
|
||||
payload.logger.info('Seeding billing sample data...')
|
||||
|
||||
try {
|
||||
// Check if we already have sample data
|
||||
const existingCustomers = await payload.count({
|
||||
collection: 'customers',
|
||||
where: {
|
||||
email: {
|
||||
equals: 'john.doe@example.com',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (existingCustomers.totalDocs > 0) {
|
||||
payload.logger.info('Sample billing data already exists, skipping seed')
|
||||
return
|
||||
}
|
||||
|
||||
// Create a sample customer
|
||||
const customer = await payload.create({
|
||||
collection: 'customers',
|
||||
data: {
|
||||
email: 'john.doe@example.com',
|
||||
name: 'John Doe',
|
||||
phone: '+1-555-0123',
|
||||
address: {
|
||||
line1: '123 Main St',
|
||||
city: 'New York',
|
||||
state: 'NY',
|
||||
postal_code: '10001',
|
||||
country: 'US'
|
||||
},
|
||||
metadata: {
|
||||
source: 'seed',
|
||||
created_by: 'system'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
payload.logger.info(`Created sample customer: ${customer.id}`)
|
||||
|
||||
// Create a sample invoice
|
||||
const invoice = await payload.create({
|
||||
collection: 'invoices',
|
||||
data: {
|
||||
number: 'INV-001-SAMPLE',
|
||||
customer: customer.id,
|
||||
currency: 'USD',
|
||||
items: [
|
||||
{
|
||||
description: 'Web Development Services',
|
||||
quantity: 10,
|
||||
unitAmount: 5000, // $50.00 per hour
|
||||
totalAmount: 50000 // $500.00 total
|
||||
},
|
||||
{
|
||||
description: 'Design Consultation',
|
||||
quantity: 2,
|
||||
unitAmount: 7500, // $75.00 per hour
|
||||
totalAmount: 15000 // $150.00 total
|
||||
}
|
||||
],
|
||||
subtotal: 65000, // $650.00
|
||||
taxAmount: 5200, // $52.00 (8% tax)
|
||||
amount: 70200, // $702.00 total
|
||||
status: 'open',
|
||||
dueDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString(), // 30 days from now
|
||||
notes: 'Payment terms: Net 30 days. This is sample data for development.',
|
||||
metadata: {
|
||||
project: 'website-redesign',
|
||||
billable_hours: 12,
|
||||
sample: true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
payload.logger.info(`Created sample invoice: ${invoice.number}`)
|
||||
|
||||
// Create a sample payment using test provider
|
||||
const payment = await payload.create({
|
||||
collection: 'payments',
|
||||
data: {
|
||||
provider: 'test',
|
||||
providerId: `test_pay_sample_${Date.now()}`,
|
||||
status: 'succeeded',
|
||||
amount: 70200, // $702.00
|
||||
currency: 'USD',
|
||||
description: `Sample payment for invoice ${invoice.number}`,
|
||||
customer: customer.id,
|
||||
invoice: invoice.id,
|
||||
metadata: {
|
||||
invoice_number: invoice.number,
|
||||
payment_method: 'test_card',
|
||||
sample: true
|
||||
},
|
||||
providerData: {
|
||||
testMode: true,
|
||||
simulatedPayment: true,
|
||||
autoCompleted: true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
payload.logger.info(`Created sample payment: ${payment.id}`)
|
||||
|
||||
// Update invoice status to paid
|
||||
await payload.update({
|
||||
collection: 'invoices',
|
||||
id: invoice.id,
|
||||
data: {
|
||||
status: 'paid',
|
||||
payment: payment.id,
|
||||
paidAt: new Date().toISOString()
|
||||
}
|
||||
})
|
||||
|
||||
payload.logger.info('Billing sample data seeded successfully!')
|
||||
|
||||
} catch (error) {
|
||||
payload.logger.error('Error seeding billing data:', error)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user