@koempf/shopgate-utility
Version:
Shopgate WebCheckout utility for Kömpf
123 lines (105 loc) • 3.61 kB
JavaScript
const {
getGuestCartsEndpoint,
getGuestCartEndpoint,
getGuestCartItemsEndpoint,
getGuestCartItemEndpoint,
getGuestCartCodesEndpoint,
getGuestCartCodeEndpoint
} = require('../endpoints')
const { get, post, patch, del } = require('../client/apiClient')
const SentryLogger = require('./logger')
const { UnknownError } = require('./errors')
const logger = new SentryLogger('CustomerService')
const getGuestCarts = async (config) => {
const cart = await get(getGuestCartsEndpoint(), config)
.then(response => response.statusCode === 200 ? response.body : Promise.reject(response))
.then(response => response.data[0] || response.data)
.catch(() => {})
return cart
}
const getGuestCart = async ({ cartId }, config) => {
const cart = await get(getGuestCartEndpoint(cartId), config)
.then(response => response.statusCode === 200 ? response.body : Promise.reject(response))
.catch(() => {})
return cart
}
const addGuestCartItem = async ({ cartId, sku, quantity = 1 }, config) => {
const params = {
data: {
type: 'guest-cart-items',
attributes: {
sku,
quantity
}
}
}
const response = await post(getGuestCartItemsEndpoint(cartId), params, config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('addGuestCartItem Error', JSON.stringify(response.body), config.sentryDsn)
throw new UnknownError()
})
return response
}
const updateGuestCartItem = async ({ cartId, itemGroupKey, quantity = 1 }, config) => {
const params = {
data: {
type: 'guest-cart-items',
attributes: {
quantity
}
}
}
const response = await patch(getGuestCartItemEndpoint(cartId, itemGroupKey), params, config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('updateGuestCartItem Error', JSON.stringify(response.body), config.sentryDsn)
throw new UnknownError()
})
return response
}
const deleteGuestCartItem = async ({ cartId, itemGroupKey }, config) => {
const response = await del(getGuestCartItemEndpoint(cartId, itemGroupKey), config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('deleteGuestCartItem Error', JSON.stringify(response.body), config.sentryDsn)
throw new UnknownError()
})
return response
}
const addCartCodes = async ({ cartId, code }, config) => {
const params = {
data: {
type: 'cart-codes',
attributes: {
code
}
}
}
const response = await post(getGuestCartCodesEndpoint(cartId), params, config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('addGuestCartCodes Error', JSON.stringify(response.body), config.sentryDsn)
throw new UnknownError()
})
return response
}
const deleteCartCodes = async ({ cartId, code }, config) => {
const response = await del(getGuestCartCodeEndpoint(cartId, code), config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('deleteGuestCartCodes Error', JSON.stringify(response.body), config.sentryDsn)
throw new UnknownError()
})
return response
}
module.exports = {
getGuestCarts,
getGuestCart,
addGuestCartItem,
updateGuestCartItem,
deleteGuestCartItem,
addCartCodes,
deleteCartCodes
}