feat(payments): persist helcimCustomerCode + skip getOrCreateCustomer on card-on-file

This commit is contained in:
Jennie Robinson Faber 2026-04-27 11:54:54 +01:00
parent 134aef6ab0
commit 4442c57223
10 changed files with 330 additions and 33 deletions

View file

@ -2,19 +2,20 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'
import Member from '../../../server/models/member.js'
import Payment from '../../../server/models/payment.js'
import { listHelcimCustomerTransactions } from '../../../server/utils/helcim.js'
import { getHelcimCustomer, listHelcimCustomerTransactions } from '../../../server/utils/helcim.js'
import { upsertPaymentFromHelcim } from '../../../server/utils/payments.js'
import reconcileHandler from '../../../server/api/internal/reconcile-payments.post.js'
import { createMockEvent } from '../helpers/createMockEvent.js'
vi.mock('../../../server/models/member.js', () => ({
default: { find: vi.fn() }
default: { find: vi.fn(), findByIdAndUpdate: vi.fn() }
}))
vi.mock('../../../server/models/payment.js', () => ({
default: { findOne: vi.fn() }
}))
vi.mock('../../../server/utils/mongoose.js', () => ({ connectDB: vi.fn() }))
vi.mock('../../../server/utils/helcim.js', () => ({
getHelcimCustomer: vi.fn(),
listHelcimCustomerTransactions: vi.fn()
}))
vi.mock('../../../server/utils/payments.js', () => ({
@ -88,6 +89,7 @@ describe('POST /api/internal/reconcile-payments', () => {
_id: 'm1',
email: 'a@example.com',
helcimCustomerId: 'cust-1',
helcimCustomerCode: 'CST-1',
helcimSubscriptionId: 'sub-1',
billingCadence: 'monthly'
}
@ -113,6 +115,7 @@ describe('POST /api/internal/reconcile-payments', () => {
{ helcimCustomerId: { $exists: true, $ne: null } },
expect.objectContaining({
helcimCustomerId: 1,
helcimCustomerCode: 1,
helcimSubscriptionId: 1,
billingCadence: 1
})
@ -134,7 +137,7 @@ describe('POST /api/internal/reconcile-payments', () => {
it('does NOT pass sendConfirmation: true (no duplicate confirmation emails)', async () => {
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1' }
{ _id: 'm1', helcimCustomerId: 'cust-1', helcimCustomerCode: 'CST-1' }
]))
listHelcimCustomerTransactions.mockResolvedValue([
{ id: 'tx-paid', status: 'paid', amount: 10, currency: 'CAD' }
@ -157,9 +160,9 @@ describe('POST /api/internal/reconcile-payments', () => {
it('continues iterating when listHelcimCustomerTransactions throws for one member', async () => {
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1' },
{ _id: 'm2', helcimCustomerId: 'cust-2' },
{ _id: 'm3', helcimCustomerId: 'cust-3' }
{ _id: 'm1', helcimCustomerId: 'cust-1', helcimCustomerCode: 'CST-1' },
{ _id: 'm2', helcimCustomerId: 'cust-2', helcimCustomerCode: 'CST-2' },
{ _id: 'm3', helcimCustomerId: 'cust-3', helcimCustomerCode: 'CST-3' }
]))
// m1 succeeds first try, m2 fails all 3 retries, m3 succeeds first try.
// Keyed by customerCode so it works regardless of call order (chunked Promise.all).
@ -191,7 +194,7 @@ describe('POST /api/internal/reconcile-payments', () => {
it('retries transient Helcim errors with exponential backoff (3 attempts)', async () => {
vi.useFakeTimers()
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1' }
{ _id: 'm1', helcimCustomerId: 'cust-1', helcimCustomerCode: 'CST-1' }
]))
listHelcimCustomerTransactions
.mockRejectedValueOnce(new Error('boom 1'))
@ -219,7 +222,7 @@ describe('POST /api/internal/reconcile-payments', () => {
it('counts memberErrors when all 3 retry attempts fail', async () => {
vi.useFakeTimers()
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1' }
{ _id: 'm1', helcimCustomerId: 'cust-1', helcimCustomerCode: 'CST-1' }
]))
listHelcimCustomerTransactions.mockRejectedValue(new Error('persistent 503'))
@ -241,7 +244,7 @@ describe('POST /api/internal/reconcile-payments', () => {
it('honors ?apply=false dry-run mode (Payment.findOne, no upsert)', async () => {
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1' }
{ _id: 'm1', helcimCustomerId: 'cust-1', helcimCustomerCode: 'CST-1' }
]))
listHelcimCustomerTransactions.mockResolvedValue([
{ id: 'tx-existing', status: 'paid', amount: 10 },
@ -266,4 +269,63 @@ describe('POST /api/internal/reconcile-payments', () => {
existed: 1
})
})
describe('helcimCustomerCode backfill', () => {
it('writes helcimCustomerCode when missing on the member doc', async () => {
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1' } // no helcimCustomerCode
]))
getHelcimCustomer.mockResolvedValue({ id: 'cust-1', customerCode: 'CST-NEW' })
listHelcimCustomerTransactions.mockResolvedValue([])
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments',
headers: { 'x-reconcile-token': RECONCILE_TOKEN }
})
await reconcileHandler(event)
expect(getHelcimCustomer).toHaveBeenCalledWith('cust-1')
expect(Member.findByIdAndUpdate).toHaveBeenCalledWith(
'm1',
{ $set: { helcimCustomerCode: 'CST-NEW' } },
{ runValidators: false }
)
})
it('skips backfill when helcimCustomerCode is already present', async () => {
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1', helcimCustomerCode: 'CST-EXISTING' }
]))
listHelcimCustomerTransactions.mockResolvedValue([])
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments',
headers: { 'x-reconcile-token': RECONCILE_TOKEN }
})
await reconcileHandler(event)
expect(getHelcimCustomer).not.toHaveBeenCalled()
expect(Member.findByIdAndUpdate).not.toHaveBeenCalled()
})
it('does not fail the run when getHelcimCustomer throws during backfill', async () => {
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1' }
]))
getHelcimCustomer.mockRejectedValue(new Error('helcim 503'))
listHelcimCustomerTransactions.mockResolvedValue([])
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments',
headers: { 'x-reconcile-token': RECONCILE_TOKEN }
})
const result = await reconcileHandler(event)
expect(Member.findByIdAndUpdate).not.toHaveBeenCalled()
expect(result.memberErrors).toBe(0) // backfill failure is best-effort, not fatal
})
})
})