refactor(helcim): use helper in unused admin endpoints

This commit is contained in:
Jennie Robinson Faber 2026-04-08 22:11:25 +01:00
parent 0d792c7c70
commit 130e5bfa9f
4 changed files with 29 additions and 113 deletions

View file

@ -1,48 +1,24 @@
// Update customer billing address
import { requireAuth } from '../../utils/auth.js'
const HELCIM_API_BASE = 'https://api.helcim.com/v2'
import { updateHelcimCustomer } from '../../utils/helcim.js'
export default defineEventHandler(async (event) => {
try {
await requireAuth(event)
const config = useRuntimeConfig(event)
const body = await validateBody(event, helcimUpdateBillingSchema)
const { billingAddress } = body
const helcimToken = config.helcimApiToken
// Update customer billing address in Helcim
const response = await fetch(`${HELCIM_API_BASE}/customers/${body.customerId}`, {
method: 'PATCH',
headers: {
'accept': 'application/json',
'content-type': 'application/json',
'api-token': helcimToken
},
body: JSON.stringify({
billingAddress: {
name: billingAddress.name,
street1: billingAddress.street,
city: billingAddress.city,
province: billingAddress.province || billingAddress.state,
country: billingAddress.country,
postalCode: billingAddress.postalCode
}
})
const customerData = await updateHelcimCustomer(body.customerId, {
billingAddress: {
name: billingAddress.name,
street1: billingAddress.street,
city: billingAddress.city,
province: billingAddress.province || billingAddress.state,
country: billingAddress.country,
postalCode: billingAddress.postalCode
}
})
if (!response.ok) {
const errorText = await response.text()
console.error('Billing address update failed:', response.status, errorText)
throw createError({
statusCode: response.status,
statusMessage: 'Billing update failed'
})
}
const customerData = await response.json()
return {
success: true,
@ -56,4 +32,4 @@ export default defineEventHandler(async (event) => {
statusMessage: 'An unexpected error occurred'
})
}
})
})