feat(helcim): use contributionAmount, inline ×12 annual math
This commit is contained in:
parent
4c8aff34bf
commit
613d077eaa
2 changed files with 38 additions and 48 deletions
|
|
@ -2,7 +2,7 @@ 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, getTierAmount } from '../../../server/config/contributions.js'
|
||||
import { requiresPayment, getHelcimPlanId } 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'
|
||||
|
|
@ -18,8 +18,6 @@ vi.mock('../../../server/utils/slack.ts', () => ({
|
|||
vi.mock('../../../server/config/contributions.js', () => ({
|
||||
requiresPayment: vi.fn(),
|
||||
getHelcimPlanId: vi.fn(),
|
||||
getContributionTierByValue: vi.fn(),
|
||||
getTierAmount: vi.fn(),
|
||||
}))
|
||||
vi.mock('../../../server/utils/resend.js', () => ({
|
||||
sendWelcomeEmail: vi.fn().mockResolvedValue({ success: true })
|
||||
|
|
@ -47,7 +45,7 @@ describe('helcim subscription endpoint', () => {
|
|||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-1', contributionTier: '0', customerCode: 'code-1' }
|
||||
body: { customerId: 'cust-1', contributionAmount: 0, customerCode: 'code-1' }
|
||||
})
|
||||
|
||||
await expect(subscriptionHandler(event)).rejects.toMatchObject({
|
||||
|
|
@ -67,7 +65,7 @@ describe('helcim subscription endpoint', () => {
|
|||
email: 'test@example.com',
|
||||
name: 'Test',
|
||||
circle: 'community',
|
||||
contributionTier: '0',
|
||||
contributionAmount: 0,
|
||||
status: 'active',
|
||||
save: vi.fn()
|
||||
}
|
||||
|
|
@ -76,7 +74,7 @@ describe('helcim subscription endpoint', () => {
|
|||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-1', contributionTier: '0', customerCode: 'code-1' }
|
||||
body: { customerId: 'cust-1', contributionAmount: 0, customerCode: 'code-1' }
|
||||
})
|
||||
|
||||
const result = await subscriptionHandler(event)
|
||||
|
|
@ -88,12 +86,12 @@ describe('helcim subscription endpoint', () => {
|
|||
email: 'test@example.com',
|
||||
name: 'Test',
|
||||
circle: 'community',
|
||||
contributionTier: '0',
|
||||
contributionAmount: 0,
|
||||
status: 'active'
|
||||
})
|
||||
expect(Member.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{ helcimCustomerId: 'cust-1' },
|
||||
expect.objectContaining({ status: 'active', contributionTier: '0' }),
|
||||
expect.objectContaining({ status: 'active', contributionAmount: 0 }),
|
||||
{ new: true }
|
||||
)
|
||||
expect(createHelcimSubscription).not.toHaveBeenCalled()
|
||||
|
|
@ -107,12 +105,12 @@ describe('helcim subscription endpoint', () => {
|
|||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-1', contributionTier: '15', customerCode: 'code-1', cadence: 'monthly' }
|
||||
body: { customerId: 'cust-1', contributionAmount: 15, customerCode: 'code-1', cadence: 'monthly' }
|
||||
})
|
||||
|
||||
await expect(subscriptionHandler(event)).rejects.toMatchObject({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Payment information is required for this contribution tier'
|
||||
statusMessage: 'Payment information is required for a paid contribution'
|
||||
})
|
||||
})
|
||||
|
||||
|
|
@ -120,15 +118,13 @@ describe('helcim subscription endpoint', () => {
|
|||
requireAuth.mockResolvedValue(undefined)
|
||||
requiresPayment.mockReturnValue(true)
|
||||
getHelcimPlanId.mockReturnValue('99999')
|
||||
getContributionTierByValue.mockReturnValue({ amount: '15' })
|
||||
getTierAmount.mockReturnValue(15)
|
||||
|
||||
const mockMember = {
|
||||
_id: 'member-2',
|
||||
email: 'paid@example.com',
|
||||
name: 'Paid User',
|
||||
circle: 'founder',
|
||||
contributionTier: '15',
|
||||
contributionAmount: 15,
|
||||
status: 'active',
|
||||
}
|
||||
Member.findOneAndUpdate.mockResolvedValue(mockMember)
|
||||
|
|
@ -139,7 +135,7 @@ describe('helcim subscription endpoint', () => {
|
|||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-1', contributionTier: '15', customerCode: 'code-1', cardToken: 'tok-123', cadence: 'monthly' }
|
||||
body: { customerId: 'cust-1', contributionAmount: 15, customerCode: 'code-1', cardToken: 'tok-123', cadence: 'monthly' }
|
||||
})
|
||||
|
||||
const result = await subscriptionHandler(event)
|
||||
|
|
@ -151,7 +147,7 @@ describe('helcim subscription endpoint', () => {
|
|||
)
|
||||
expect(Member.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{ helcimCustomerId: 'cust-1' },
|
||||
{ $set: expect.objectContaining({ billingCadence: 'monthly', contributionTier: '15', status: 'active' }) },
|
||||
{ $set: expect.objectContaining({ billingCadence: 'monthly', contributionAmount: 15, status: 'active' }) },
|
||||
{ new: true, runValidators: false }
|
||||
)
|
||||
})
|
||||
|
|
@ -160,15 +156,13 @@ describe('helcim subscription endpoint', () => {
|
|||
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',
|
||||
contributionAmount: 15,
|
||||
status: 'active',
|
||||
}
|
||||
Member.findOneAndUpdate.mockResolvedValue(mockMember)
|
||||
|
|
@ -179,36 +173,34 @@ describe('helcim subscription endpoint', () => {
|
|||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-1', contributionTier: '15', customerCode: 'code-1', cardToken: 'tok-123', cadence: 'annual' }
|
||||
body: { customerId: 'cust-1', contributionAmount: 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 }),
|
||||
expect.objectContaining({ paymentPlanId: 88888, recurringAmount: 180 }),
|
||||
'idem-key-123'
|
||||
)
|
||||
expect(Member.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
{ helcimCustomerId: 'cust-1' },
|
||||
{ $set: expect.objectContaining({ billingCadence: 'annual', contributionTier: '15', status: 'active' }) },
|
||||
{ $set: expect.objectContaining({ billingCadence: 'annual', contributionAmount: 15, status: 'active' }) },
|
||||
{ new: true, runValidators: false }
|
||||
)
|
||||
})
|
||||
|
||||
it('annual $50 tier recurringAmount is 500', async () => {
|
||||
it('annual $50 tier recurringAmount is 600', 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',
|
||||
contributionAmount: 50,
|
||||
status: 'active',
|
||||
}
|
||||
Member.findOneAndUpdate.mockResolvedValue(mockMember)
|
||||
|
|
@ -219,13 +211,13 @@ describe('helcim subscription endpoint', () => {
|
|||
const event = createMockEvent({
|
||||
method: 'POST',
|
||||
path: '/api/helcim/subscription',
|
||||
body: { customerId: 'cust-2', contributionTier: '50', customerCode: 'code-2', cardToken: 'tok-456', cadence: 'annual' }
|
||||
body: { customerId: 'cust-2', contributionAmount: 50, customerCode: 'code-2', cardToken: 'tok-456', cadence: 'annual' }
|
||||
})
|
||||
|
||||
await subscriptionHandler(event)
|
||||
|
||||
expect(createHelcimSubscription).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ paymentPlanId: 88888, recurringAmount: 500 }),
|
||||
expect.objectContaining({ paymentPlanId: 88888, recurringAmount: 600 }),
|
||||
'idem-key-123'
|
||||
)
|
||||
})
|
||||
|
|
@ -234,12 +226,11 @@ describe('helcim subscription endpoint', () => {
|
|||
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' }
|
||||
body: { customerId: 'cust-1', contributionAmount: 15, customerCode: 'code-1', cardToken: 'tok-123', cadence: 'monthly' }
|
||||
})
|
||||
|
||||
await expect(subscriptionHandler(event)).rejects.toMatchObject({
|
||||
|
|
@ -255,12 +246,11 @@ describe('helcim subscription endpoint', () => {
|
|||
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' }
|
||||
body: { customerId: 'cust-1', contributionAmount: 15, customerCode: 'code-1', cardToken: 'tok-123', cadence: 'annual' }
|
||||
})
|
||||
|
||||
await expect(subscriptionHandler(event)).rejects.toMatchObject({
|
||||
|
|
@ -276,8 +266,6 @@ describe('helcim subscription endpoint', () => {
|
|||
requireAuth.mockResolvedValue(undefined)
|
||||
requiresPayment.mockReturnValue(true)
|
||||
getHelcimPlanId.mockReturnValue('99999')
|
||||
getContributionTierByValue.mockReturnValue({ amount: '15' })
|
||||
getTierAmount.mockReturnValue(15)
|
||||
|
||||
createHelcimSubscription.mockRejectedValue(new Error('Network error'))
|
||||
|
||||
|
|
@ -286,7 +274,7 @@ describe('helcim subscription endpoint', () => {
|
|||
path: '/api/helcim/subscription',
|
||||
body: {
|
||||
customerId: 'cust-1',
|
||||
contributionTier: '15',
|
||||
contributionAmount: 15,
|
||||
customerCode: 'code-1',
|
||||
cardToken: 'tok-123',
|
||||
cadence: 'monthly',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue