refactor(helcim): normalize listHelcimCustomerCards return shape

This commit is contained in:
Jennie Robinson Faber 2026-04-27 11:46:25 +01:00
parent 27e73e969a
commit 134aef6ab0
6 changed files with 58 additions and 33 deletions

View file

@ -9,10 +9,7 @@ export default defineEventHandler(async (event) => {
return { cardToken: null }
}
const cardsResponse = await listHelcimCustomerCards(member.helcimCustomerId)
const cards = Array.isArray(cardsResponse)
? cardsResponse
: (cardsResponse?.cards || cardsResponse?.data || [])
const cards = await listHelcimCustomerCards(member.helcimCustomerId)
if (!cards.length) {
return { cardToken: null }

View file

@ -45,10 +45,7 @@ export default defineEventHandler(async (event) => {
const { cardToken } = body
// Step 3: verify the submitted token is attached to this member's customer
const cardsResponse = await listHelcimCustomerCards(member.helcimCustomerId)
const cards = Array.isArray(cardsResponse)
? cardsResponse
: (cardsResponse?.cards || cardsResponse?.data || [])
const cards = await listHelcimCustomerCards(member.helcimCustomerId)
const matchingCard = cards.find((c) => c?.cardToken === cardToken)
if (!matchingCard) {

View file

@ -13,7 +13,7 @@ export default defineEventHandler(async (event) => {
const cards = await listHelcimCustomerCards(body.customerId)
// 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
)

View file

@ -86,8 +86,10 @@ export const createHelcimCustomer = (payload) =>
export const updateHelcimCustomer = (id, payload) =>
helcimFetch(`/customers/${id}`, { method: 'PATCH', body: payload, errorMessage: 'Billing update failed' })
export const listHelcimCustomerCards = (id) =>
helcimFetch(`/customers/${id}/cards`, { errorMessage: 'Card lookup failed' })
export const listHelcimCustomerCards = async (id) => {
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.