ghostguild-org/tests/server/api/reconcile-payments-route.test.js
Jennie Robinson Faber 3c38333dd1
Some checks failed
Test / vitest (push) Successful in 11m5s
Test / playwright (push) Has been cancelled
Test / Notify on failure (push) Blocked by required conditions
Test / visual (push) Blocked by required conditions
fix(reconcile): pass customerCode (not helcimCustomerId) to Helcim transactions API
2026-04-27 19:31:59 +01:00

335 lines
12 KiB
JavaScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import Member from '../../../server/models/member.js'
import Payment from '../../../server/models/payment.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(), 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', () => ({
upsertPaymentFromHelcim: vi.fn()
}))
// Override useRuntimeConfig from setup.js for these tests
const RECONCILE_TOKEN = 'test-reconcile-secret-32-characters-long-xx'
beforeEach(() => {
vi.stubGlobal('useRuntimeConfig', () => ({
jwtSecret: 'test-jwt-secret',
helcimApiToken: 'test-helcim-token',
reconcileToken: RECONCILE_TOKEN
}))
})
function leanResolver(value) {
return { lean: vi.fn().mockResolvedValue(value) }
}
describe('POST /api/internal/reconcile-payments', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('rejects requests without the shared-secret token', async () => {
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments'
})
await expect(reconcileHandler(event)).rejects.toMatchObject({
statusCode: 401,
statusMessage: 'Unauthorized'
})
})
it('rejects requests with the wrong token', async () => {
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments',
headers: { 'x-reconcile-token': 'not-the-right-secret' }
})
await expect(reconcileHandler(event)).rejects.toMatchObject({
statusCode: 401,
statusMessage: 'Unauthorized'
})
})
it('rejects when reconcileToken is not configured on the server', async () => {
vi.stubGlobal('useRuntimeConfig', () => ({
jwtSecret: 'test-jwt-secret',
helcimApiToken: 'test-helcim-token',
reconcileToken: ''
}))
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments',
headers: { 'x-reconcile-token': 'anything' }
})
await expect(reconcileHandler(event)).rejects.toMatchObject({
statusCode: 401
})
})
it('queries members with helcimCustomerId and returns a summary', async () => {
Member.find.mockReturnValue(leanResolver([
{
_id: 'm1',
email: 'a@example.com',
helcimCustomerId: 'cust-1',
helcimCustomerCode: 'CST-1',
helcimSubscriptionId: 'sub-1',
billingCadence: 'monthly'
}
]))
listHelcimCustomerTransactions.mockResolvedValue([
{ id: 'tx-paid', status: 'paid', amount: 10, currency: 'CAD' },
{ id: 'tx-other', status: 'other', amount: 0, currency: 'CAD' },
{ id: 'tx-failed', status: 'failed', amount: 10, currency: 'CAD' }
])
upsertPaymentFromHelcim.mockResolvedValueOnce({ created: true, payment: { _id: 'p1' } })
upsertPaymentFromHelcim.mockResolvedValueOnce({ created: false, payment: { _id: 'p2' } })
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments',
headers: { 'x-reconcile-token': RECONCILE_TOKEN }
})
const result = await reconcileHandler(event)
// Verify the query shape: filter by helcimCustomerId existence + projection
expect(Member.find).toHaveBeenCalledWith(
{ helcimCustomerId: { $exists: true, $ne: null } },
expect.objectContaining({
helcimCustomerId: 1,
helcimCustomerCode: 1,
helcimSubscriptionId: 1,
billingCadence: 1
})
)
// upsert called for paid + failed (status='other' is skipped)
expect(upsertPaymentFromHelcim).toHaveBeenCalledTimes(2)
expect(result).toMatchObject({
membersScanned: 1,
txExamined: 3,
created: 1,
existed: 1,
skipped: 1,
memberErrors: 0,
apply: true
})
})
it('does NOT pass sendConfirmation: true (no duplicate confirmation emails)', async () => {
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1', helcimCustomerCode: 'CST-1' }
]))
listHelcimCustomerTransactions.mockResolvedValue([
{ id: 'tx-paid', status: 'paid', amount: 10, currency: 'CAD' }
])
upsertPaymentFromHelcim.mockResolvedValue({ created: true, payment: { _id: 'p1' } })
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments',
headers: { 'x-reconcile-token': RECONCILE_TOKEN }
})
await reconcileHandler(event)
// The cron must not pass sendConfirmation. Either no opts or sendConfirmation falsy.
const opts = upsertPaymentFromHelcim.mock.calls[0][2]
if (opts) {
expect(opts.sendConfirmation).toBeFalsy()
}
})
it('continues iterating when listHelcimCustomerTransactions throws for one member', async () => {
Member.find.mockReturnValue(leanResolver([
{ _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).
listHelcimCustomerTransactions.mockImplementation((customerCode) => {
if (customerCode === 'CST-1') return Promise.resolve([{ id: 'tx1', status: 'paid', amount: 5 }])
if (customerCode === 'CST-3') return Promise.resolve([{ id: 'tx3', status: 'paid', amount: 7 }])
return Promise.reject(new Error('helcim 503'))
})
upsertPaymentFromHelcim.mockResolvedValue({ created: true, payment: { _id: 'p' } })
vi.useFakeTimers()
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments',
headers: { 'x-reconcile-token': RECONCILE_TOKEN }
})
const promise = reconcileHandler(event)
// Drain m2's exponential backoff (250 + 500 + 1000ms)
await vi.advanceTimersByTimeAsync(2000)
const result = await promise
expect(result.membersScanned).toBe(3)
expect(result.memberErrors).toBe(1)
expect(result.created).toBe(2) // m1 + m3 succeeded
vi.useRealTimers()
})
it('retries transient Helcim errors with exponential backoff (3 attempts)', async () => {
vi.useFakeTimers()
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1', helcimCustomerCode: 'CST-1' }
]))
listHelcimCustomerTransactions
.mockRejectedValueOnce(new Error('boom 1'))
.mockRejectedValueOnce(new Error('boom 2'))
.mockResolvedValueOnce([{ id: 'tx-paid', status: 'paid', amount: 9 }])
upsertPaymentFromHelcim.mockResolvedValue({ created: true, payment: { _id: 'p1' } })
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments',
headers: { 'x-reconcile-token': RECONCILE_TOKEN }
})
const promise = reconcileHandler(event)
// Advance through the 250ms + 500ms backoff windows
await vi.advanceTimersByTimeAsync(250)
await vi.advanceTimersByTimeAsync(500)
const result = await promise
expect(listHelcimCustomerTransactions).toHaveBeenCalledTimes(3)
expect(result.memberErrors).toBe(0)
expect(result.created).toBe(1)
vi.useRealTimers()
})
it('counts memberErrors when all 3 retry attempts fail', async () => {
vi.useFakeTimers()
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1', helcimCustomerCode: 'CST-1' }
]))
listHelcimCustomerTransactions.mockRejectedValue(new Error('persistent 503'))
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments',
headers: { 'x-reconcile-token': RECONCILE_TOKEN }
})
const promise = reconcileHandler(event)
await vi.advanceTimersByTimeAsync(250)
await vi.advanceTimersByTimeAsync(500)
await vi.advanceTimersByTimeAsync(1000)
const result = await promise
expect(listHelcimCustomerTransactions).toHaveBeenCalledTimes(3)
expect(result.memberErrors).toBe(1)
vi.useRealTimers()
})
it('honors ?apply=false dry-run mode (Payment.findOne, no upsert)', async () => {
Member.find.mockReturnValue(leanResolver([
{ _id: 'm1', helcimCustomerId: 'cust-1', helcimCustomerCode: 'CST-1' }
]))
listHelcimCustomerTransactions.mockResolvedValue([
{ id: 'tx-existing', status: 'paid', amount: 10 },
{ id: 'tx-new', status: 'paid', amount: 12 }
])
Payment.findOne
.mockResolvedValueOnce({ _id: 'p-existing' })
.mockResolvedValueOnce(null)
const event = createMockEvent({
method: 'POST',
path: '/api/internal/reconcile-payments?apply=false',
headers: { 'x-reconcile-token': RECONCILE_TOKEN }
})
const result = await reconcileHandler(event)
expect(upsertPaymentFromHelcim).not.toHaveBeenCalled()
expect(Payment.findOne).toHaveBeenCalledTimes(2)
expect(result).toMatchObject({
apply: false,
created: 1, // would-create
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 }
)
expect(listHelcimCustomerTransactions).toHaveBeenCalledWith('CST-NEW')
})
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()
expect(listHelcimCustomerTransactions).toHaveBeenCalledWith('CST-EXISTING')
})
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()
// Without a customerCode, we skip the transactions API entirely (best-effort, not error).
expect(listHelcimCustomerTransactions).not.toHaveBeenCalled()
expect(result.memberErrors).toBe(0)
})
})
})