feat(helcim): create subscription by cadence with recurringAmount
Replace tier-based plan lookup with cadence-keyed lookup, compute recurringAmount via getTierAmount, persist billingCadence on member. Delete both manual-fallback blocks; Helcim failure now surfaces as 500.
This commit is contained in:
parent
be0e6e7699
commit
8d43804c7f
2 changed files with 218 additions and 153 deletions
|
|
@ -2,7 +2,8 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|||
|
||||
import Member from '../../../server/models/member.js'
|
||||
import { requireAuth } from '../../../server/utils/auth.js'
|
||||
import { requiresPayment, getHelcimPlanId, getContributionTierByValue } from '../../../server/config/contributions.js'
|
||||
import { requiresPayment, getHelcimPlanId, getContributionTierByValue, getTierAmount } from '../../../server/config/contributions.js'
|
||||
import { createHelcimSubscription } from '../../../server/utils/helcim.js'
|
||||
import subscriptionHandler from '../../../server/api/helcim/subscription.post.js'
|
||||
import { createMockEvent } from '../helpers/createMockEvent.js'
|
||||
|
||||
|
|
@ -17,25 +18,25 @@ vi.mock('../../../server/utils/slack.ts', () => ({
|
|||
vi.mock('../../../server/config/contributions.js', () => ({
|
||||
requiresPayment: vi.fn(),
|
||||
getHelcimPlanId: vi.fn(),
|
||||
getContributionTierByValue: vi.fn()
|
||||
getContributionTierByValue: vi.fn(),
|
||||
getTierAmount: vi.fn(),
|
||||
}))
|
||||
vi.mock('../../../server/utils/resend.js', () => ({
|
||||
sendWelcomeEmail: vi.fn().mockResolvedValue({ success: true })
|
||||
}))
|
||||
vi.mock('../../../server/utils/helcim.js', () => ({
|
||||
createHelcimSubscription: vi.fn(),
|
||||
generateIdempotencyKey: vi.fn().mockReturnValue('idem-key-123'),
|
||||
}))
|
||||
|
||||
// helcimSubscriptionSchema is a Nitro auto-import used by validateBody
|
||||
vi.stubGlobal('helcimSubscriptionSchema', {})
|
||||
|
||||
describe('helcim subscription endpoint', () => {
|
||||
const savedFetch = globalThis.fetch
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
// Restore fetch in case a test stubbed it
|
||||
globalThis.fetch = savedFetch
|
||||
// Default: first activation from pending_payment
|
||||
Member.findOne.mockResolvedValue({ status: 'pending_payment' })
|
||||
})
|
||||
|
||||
it('requires auth', async () => {
|
||||
|
|
@ -95,17 +96,18 @@ describe('helcim subscription endpoint', () => {
|
|||
expect.objectContaining({ status: 'active', contributionTier: '0' }),
|
||||
{ new: true }
|
||||
)
|
||||
expect(createHelcimSubscription).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('paid tier without cardToken returns 400', async () => {
|
||||
requireAuth.mockResolvedValue(undefined)
|
||||
requiresPayment.mockReturnValue(true)
|
||||
getHelcimPlanId.mockReturnValue('plan-123')
|
||||
getHelcimPlanId.mockReturnValue('99999')
|
||||
|
||||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-1', contributionTier: '15', customerCode: 'code-1' }
|
||||
body: { customerId: 'cust-1', contributionTier: '15', customerCode: 'code-1', cadence: 'monthly' }
|
||||
})
|
||||
|
||||
await expect(subscriptionHandler(event)).rejects.toMatchObject({
|
||||
|
|
@ -114,11 +116,12 @@ describe('helcim subscription endpoint', () => {
|
|||
})
|
||||
})
|
||||
|
||||
it('Helcim API failure still activates member', async () => {
|
||||
it('monthly $15 tier creates subscription with correct paymentPlanId and recurringAmount', async () => {
|
||||
requireAuth.mockResolvedValue(undefined)
|
||||
requiresPayment.mockReturnValue(true)
|
||||
getHelcimPlanId.mockReturnValue('plan-123')
|
||||
getHelcimPlanId.mockReturnValue('99999')
|
||||
getContributionTierByValue.mockReturnValue({ amount: '15' })
|
||||
getTierAmount.mockReturnValue(15)
|
||||
|
||||
const mockMember = {
|
||||
_id: 'member-2',
|
||||
|
|
@ -127,11 +130,156 @@ describe('helcim subscription endpoint', () => {
|
|||
circle: 'founder',
|
||||
contributionTier: '15',
|
||||
status: 'active',
|
||||
save: vi.fn()
|
||||
}
|
||||
Member.findOneAndUpdate.mockResolvedValue(mockMember)
|
||||
createHelcimSubscription.mockResolvedValue({
|
||||
data: [{ id: 'sub-monthly-1', status: 'active', nextBillingDate: '2026-05-18' }]
|
||||
})
|
||||
|
||||
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('Network error')))
|
||||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-1', contributionTier: '15', customerCode: 'code-1', cardToken: 'tok-123', cadence: 'monthly' }
|
||||
})
|
||||
|
||||
const result = await subscriptionHandler(event)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(createHelcimSubscription).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ paymentPlanId: 99999, recurringAmount: 15 }),
|
||||
'idem-key-123'
|
||||
)
|
||||
expect(Member.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{ helcimCustomerId: 'cust-1' },
|
||||
{ $set: expect.objectContaining({ billingCadence: 'monthly', contributionTier: '15', status: 'active' }) },
|
||||
{ new: true, runValidators: false }
|
||||
)
|
||||
})
|
||||
|
||||
it('annual $15 tier creates subscription with correct paymentPlanId and recurringAmount', async () => {
|
||||
requireAuth.mockResolvedValue(undefined)
|
||||
requiresPayment.mockReturnValue(true)
|
||||
getHelcimPlanId.mockReturnValue('88888')
|
||||
getContributionTierByValue.mockReturnValue({ amount: '15' })
|
||||
getTierAmount.mockReturnValue(150)
|
||||
|
||||
const mockMember = {
|
||||
_id: 'member-3',
|
||||
email: 'annual@example.com',
|
||||
name: 'Annual User',
|
||||
circle: 'founder',
|
||||
contributionTier: '15',
|
||||
status: 'active',
|
||||
}
|
||||
Member.findOneAndUpdate.mockResolvedValue(mockMember)
|
||||
createHelcimSubscription.mockResolvedValue({
|
||||
data: [{ id: 'sub-annual-1', status: 'active', nextBillingDate: '2027-04-18' }]
|
||||
})
|
||||
|
||||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-1', contributionTier: '15', customerCode: 'code-1', cardToken: 'tok-123', cadence: 'annual' }
|
||||
})
|
||||
|
||||
const result = await subscriptionHandler(event)
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(createHelcimSubscription).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ paymentPlanId: 88888, recurringAmount: 150 }),
|
||||
'idem-key-123'
|
||||
)
|
||||
expect(Member.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{ helcimCustomerId: 'cust-1' },
|
||||
{ $set: expect.objectContaining({ billingCadence: 'annual', contributionTier: '15', status: 'active' }) },
|
||||
{ new: true, runValidators: false }
|
||||
)
|
||||
})
|
||||
|
||||
it('annual $50 tier recurringAmount is 500', async () => {
|
||||
requireAuth.mockResolvedValue(undefined)
|
||||
requiresPayment.mockReturnValue(true)
|
||||
getHelcimPlanId.mockReturnValue('88888')
|
||||
getContributionTierByValue.mockReturnValue({ amount: '50' })
|
||||
getTierAmount.mockReturnValue(500)
|
||||
|
||||
const mockMember = {
|
||||
_id: 'member-4',
|
||||
email: 'top@example.com',
|
||||
name: 'Top Tier',
|
||||
circle: 'practitioner',
|
||||
contributionTier: '50',
|
||||
status: 'active',
|
||||
}
|
||||
Member.findOneAndUpdate.mockResolvedValue(mockMember)
|
||||
createHelcimSubscription.mockResolvedValue({
|
||||
data: [{ id: 'sub-annual-50', status: 'active', nextBillingDate: '2027-04-18' }]
|
||||
})
|
||||
|
||||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-2', contributionTier: '50', customerCode: 'code-2', cardToken: 'tok-456', cadence: 'annual' }
|
||||
})
|
||||
|
||||
await subscriptionHandler(event)
|
||||
|
||||
expect(createHelcimSubscription).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ paymentPlanId: 88888, recurringAmount: 500 }),
|
||||
'idem-key-123'
|
||||
)
|
||||
})
|
||||
|
||||
it('missing monthly plan id returns 500 with message, no Helcim call, no Mongo write', async () => {
|
||||
requireAuth.mockResolvedValue(undefined)
|
||||
requiresPayment.mockReturnValue(true)
|
||||
getHelcimPlanId.mockReturnValue(null)
|
||||
getContributionTierByValue.mockReturnValue({ amount: '15' })
|
||||
|
||||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-1', contributionTier: '15', customerCode: 'code-1', cardToken: 'tok-123', cadence: 'monthly' }
|
||||
})
|
||||
|
||||
await expect(subscriptionHandler(event)).rejects.toMatchObject({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Monthly plan id not configured',
|
||||
})
|
||||
|
||||
expect(createHelcimSubscription).not.toHaveBeenCalled()
|
||||
expect(Member.findOneAndUpdate).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('missing annual plan id returns 500 with message, no Helcim call, no Mongo write', async () => {
|
||||
requireAuth.mockResolvedValue(undefined)
|
||||
requiresPayment.mockReturnValue(true)
|
||||
getHelcimPlanId.mockReturnValue(null)
|
||||
getContributionTierByValue.mockReturnValue({ amount: '15' })
|
||||
|
||||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-1', contributionTier: '15', customerCode: 'code-1', cardToken: 'tok-123', cadence: 'annual' }
|
||||
})
|
||||
|
||||
await expect(subscriptionHandler(event)).rejects.toMatchObject({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Annual plan id not configured',
|
||||
})
|
||||
|
||||
expect(createHelcimSubscription).not.toHaveBeenCalled()
|
||||
expect(Member.findOneAndUpdate).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('Helcim API failure returns 500 and does NOT activate member', async () => {
|
||||
requireAuth.mockResolvedValue(undefined)
|
||||
requiresPayment.mockReturnValue(true)
|
||||
getHelcimPlanId.mockReturnValue('99999')
|
||||
getContributionTierByValue.mockReturnValue({ amount: '15' })
|
||||
getTierAmount.mockReturnValue(15)
|
||||
|
||||
createHelcimSubscription.mockRejectedValue(new Error('Network error'))
|
||||
|
||||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
|
|
@ -140,23 +288,15 @@ describe('helcim subscription endpoint', () => {
|
|||
customerId: 'cust-1',
|
||||
contributionTier: '15',
|
||||
customerCode: 'code-1',
|
||||
cardToken: 'tok-123'
|
||||
cardToken: 'tok-123',
|
||||
cadence: 'monthly',
|
||||
}
|
||||
})
|
||||
|
||||
const result = await subscriptionHandler(event)
|
||||
await expect(subscriptionHandler(event)).rejects.toMatchObject({
|
||||
statusCode: 500,
|
||||
})
|
||||
|
||||
expect(result.success).toBe(true)
|
||||
expect(result.warning).toBeTruthy()
|
||||
expect(result.member.status).toBe('active')
|
||||
expect(Member.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{ helcimCustomerId: 'cust-1' },
|
||||
expect.objectContaining({ status: 'active', contributionTier: '15' }),
|
||||
{ new: true }
|
||||
)
|
||||
|
||||
vi.unstubAllGlobals()
|
||||
// Re-stub the schema global after unstubAllGlobals
|
||||
vi.stubGlobal('helcimSubscriptionSchema', {})
|
||||
expect(Member.findOneAndUpdate).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue