@koempf/shopgate-utility
Version:
Shopgate WebCheckout utility for Kömpf
40 lines (31 loc) • 1.21 kB
JavaScript
const { getCustomersEndpoint, getCustomerEndpoint } = require('../endpoints')
const { get } = require('../client/apiClient')
const { UnauthorizedError } = require('./errors')
const getCustomer = async (config) => {
const customer = await get(getCustomersEndpoint(), config)
.then(response => response.statusCode === 200 ? response.body : Promise.reject(response))
.then(response => response.data[0] || response.data)
.catch(() => {})
if (!customer) throw new UnauthorizedError()
return {
id: customer.attributes.uuid,
email: customer.attributes.email,
salutation: customer.attributes.salutation,
firstName: customer.attributes.firstName,
lastName: customer.attributes.lastName,
birthday: customer.attributes.birthday
}
}
const getCustomerUrl = async ({ customerId, path }, config) => {
const customer = await get(`${getCustomerEndpoint(customerId)}/${path}`, config)
.then(response => response.statusCode === 200 ? response.body : Promise.reject(response))
.then(response => response.data)
.catch(() => {})
if (!customer) throw new UnauthorizedError()
return customer
}
module.exports = {
getCustomer,
getCustomerUrl
}