refactor(helcim): normalize listHelcimCustomerCards return shape
This commit is contained in:
parent
27e73e969a
commit
134aef6ab0
6 changed files with 58 additions and 33 deletions
|
|
@ -9,10 +9,7 @@ export default defineEventHandler(async (event) => {
|
||||||
return { cardToken: null }
|
return { cardToken: null }
|
||||||
}
|
}
|
||||||
|
|
||||||
const cardsResponse = await listHelcimCustomerCards(member.helcimCustomerId)
|
const cards = await listHelcimCustomerCards(member.helcimCustomerId)
|
||||||
const cards = Array.isArray(cardsResponse)
|
|
||||||
? cardsResponse
|
|
||||||
: (cardsResponse?.cards || cardsResponse?.data || [])
|
|
||||||
|
|
||||||
if (!cards.length) {
|
if (!cards.length) {
|
||||||
return { cardToken: null }
|
return { cardToken: null }
|
||||||
|
|
|
||||||
|
|
@ -45,10 +45,7 @@ export default defineEventHandler(async (event) => {
|
||||||
const { cardToken } = body
|
const { cardToken } = body
|
||||||
|
|
||||||
// Step 3: verify the submitted token is attached to this member's customer
|
// Step 3: verify the submitted token is attached to this member's customer
|
||||||
const cardsResponse = await listHelcimCustomerCards(member.helcimCustomerId)
|
const cards = await listHelcimCustomerCards(member.helcimCustomerId)
|
||||||
const cards = Array.isArray(cardsResponse)
|
|
||||||
? cardsResponse
|
|
||||||
: (cardsResponse?.cards || cardsResponse?.data || [])
|
|
||||||
|
|
||||||
const matchingCard = cards.find((c) => c?.cardToken === cardToken)
|
const matchingCard = cards.find((c) => c?.cardToken === cardToken)
|
||||||
if (!matchingCard) {
|
if (!matchingCard) {
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ export default defineEventHandler(async (event) => {
|
||||||
const cards = await listHelcimCustomerCards(body.customerId)
|
const cards = await listHelcimCustomerCards(body.customerId)
|
||||||
|
|
||||||
// Verify the card token exists for this customer
|
// Verify the card token exists for this customer
|
||||||
const cardExists = Array.isArray(cards) && cards.some(card =>
|
const cardExists = cards.some(card =>
|
||||||
card.cardToken === body.cardToken
|
card.cardToken === body.cardToken
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -86,8 +86,10 @@ export const createHelcimCustomer = (payload) =>
|
||||||
export const updateHelcimCustomer = (id, payload) =>
|
export const updateHelcimCustomer = (id, payload) =>
|
||||||
helcimFetch(`/customers/${id}`, { method: 'PATCH', body: payload, errorMessage: 'Billing update failed' })
|
helcimFetch(`/customers/${id}`, { method: 'PATCH', body: payload, errorMessage: 'Billing update failed' })
|
||||||
|
|
||||||
export const listHelcimCustomerCards = (id) =>
|
export const listHelcimCustomerCards = async (id) => {
|
||||||
helcimFetch(`/customers/${id}/cards`, { errorMessage: 'Card lookup failed' })
|
const raw = await helcimFetch(`/customers/${id}/cards`, { errorMessage: 'Card lookup failed' })
|
||||||
|
return Array.isArray(raw) ? raw : (raw?.cards || raw?.data || [])
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a customer's default payment method by card token.
|
* Set a customer's default payment method by card token.
|
||||||
|
|
|
||||||
|
|
@ -82,28 +82,6 @@ describe('helcim existing-card endpoint', () => {
|
||||||
expect(result.cardToken).toBe('tok-b')
|
expect(result.cardToken).toBe('tok-b')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('unwraps a { cards: [...] } response envelope', async () => {
|
|
||||||
requireAuth.mockResolvedValue({ _id: 'm1', helcimCustomerId: 9876 })
|
|
||||||
listHelcimCustomerCards.mockResolvedValue({
|
|
||||||
cards: [{ cardToken: 'tok-only' }]
|
|
||||||
})
|
|
||||||
|
|
||||||
const result = await existingCardHandler(newEvent())
|
|
||||||
|
|
||||||
expect(result.cardToken).toBe('tok-only')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('unwraps a { data: [...] } response envelope', async () => {
|
|
||||||
requireAuth.mockResolvedValue({ _id: 'm1', helcimCustomerId: 9876 })
|
|
||||||
listHelcimCustomerCards.mockResolvedValue({
|
|
||||||
data: [{ cardToken: 'tok-only' }]
|
|
||||||
})
|
|
||||||
|
|
||||||
const result = await existingCardHandler(newEvent())
|
|
||||||
|
|
||||||
expect(result.cardToken).toBe('tok-only')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns { cardToken: null } if the resolved card has no cardToken', async () => {
|
it('returns { cardToken: null } if the resolved card has no cardToken', async () => {
|
||||||
requireAuth.mockResolvedValue({ _id: 'm1', helcimCustomerId: 9876 })
|
requireAuth.mockResolvedValue({ _id: 'm1', helcimCustomerId: 9876 })
|
||||||
listHelcimCustomerCards.mockResolvedValue([{ default: true }])
|
listHelcimCustomerCards.mockResolvedValue([{ default: true }])
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
listHelcimCustomerCards,
|
||||||
listHelcimCustomerTransactions,
|
listHelcimCustomerTransactions,
|
||||||
updateHelcimCustomerDefaultPaymentMethod,
|
updateHelcimCustomerDefaultPaymentMethod,
|
||||||
updateHelcimSubscriptionPaymentMethod
|
updateHelcimSubscriptionPaymentMethod
|
||||||
|
|
@ -25,6 +26,56 @@ function errResponse(status = 500, body = 'boom') {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
describe('listHelcimCustomerCards', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks()
|
||||||
|
})
|
||||||
|
afterEach(() => {
|
||||||
|
mockFetch.mockReset()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('passes through a bare array response', async () => {
|
||||||
|
const cards = [
|
||||||
|
{ id: 1, cardToken: 'tok-a' },
|
||||||
|
{ id: 2, cardToken: 'tok-b' }
|
||||||
|
]
|
||||||
|
mockFetch.mockResolvedValue(okResponse(cards))
|
||||||
|
|
||||||
|
const result = await listHelcimCustomerCards('2488717')
|
||||||
|
|
||||||
|
expect(result).toEqual(cards)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('unwraps a { cards: [...] } response envelope', async () => {
|
||||||
|
const cards = [{ id: 1, cardToken: 'tok-a' }]
|
||||||
|
mockFetch.mockResolvedValue(okResponse({ cards }))
|
||||||
|
|
||||||
|
const result = await listHelcimCustomerCards('2488717')
|
||||||
|
|
||||||
|
expect(result).toEqual(cards)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('unwraps a { data: [...] } response envelope', async () => {
|
||||||
|
const cards = [{ id: 1, cardToken: 'tok-a' }]
|
||||||
|
mockFetch.mockResolvedValue(okResponse({ data: cards }))
|
||||||
|
|
||||||
|
const result = await listHelcimCustomerCards('2488717')
|
||||||
|
|
||||||
|
expect(result).toEqual(cards)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns an empty array for null, undefined, or unrecognized object responses', async () => {
|
||||||
|
mockFetch.mockResolvedValueOnce(okResponse(null))
|
||||||
|
expect(await listHelcimCustomerCards('2488717')).toEqual([])
|
||||||
|
|
||||||
|
mockFetch.mockResolvedValueOnce(okResponse(undefined))
|
||||||
|
expect(await listHelcimCustomerCards('2488717')).toEqual([])
|
||||||
|
|
||||||
|
mockFetch.mockResolvedValueOnce(okResponse({ unexpected: 'shape' }))
|
||||||
|
expect(await listHelcimCustomerCards('2488717')).toEqual([])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('listHelcimCustomerTransactions', () => {
|
describe('listHelcimCustomerTransactions', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue