@koempf/shopgate-utility
Version:
Shopgate WebCheckout utility for Kömpf
149 lines (129 loc) • 4.21 kB
JavaScript
const {
getCartsEndpoint,
getCartEndpoint,
getCartItemsEndpoint,
getCartItemEndpoint,
getCartCodesEndpoint,
getCartCodeEndpoint
} = require('../endpoints')
const { get, post, patch, del } = require('../client/apiClient')
const SentryLogger = require('./logger')
const { UnknownError } = require('./errors')
const logger = new SentryLogger('CartService')
const getCarts = async (config) => {
const cart = await get(getCartsEndpoint(), config)
.then(response => response.statusCode === 200 ? response.body : Promise.reject(response))
.then(response => response.data[0] || response.data)
.catch(response => {
logger.error('getCarts Error', JSON.stringify(response.body), config.sentryDsn)
})
return cart
}
const getCart = async ({ cartId }, config) => {
const cart = await get(getCartEndpoint(cartId), config)
.then(response => response.statusCode === 200 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('getCart Error', JSON.stringify(response.body), config.sentryDsn)
})
return cart
}
const createCart = async (config) => {
const params = {
data: {
type: 'carts',
attributes: {
priceMode: 'GROSS_MODE',
currency: 'EUR'
}
}
}
const response = await post(getCartsEndpoint(), params, config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.then(response => response.data)
.catch(response => {
logger.error('createCart Error', JSON.stringify(response.body), config.sentryDsn)
throw new UnknownError()
})
return response
}
const addCartItem = async ({ cartId, sku, quantity = 1 }, config) => {
const params = {
data: {
type: 'items',
attributes: {
sku,
quantity
}
}
}
const response = await post(getCartItemsEndpoint(cartId), params, config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('addCartItem Error', JSON.stringify(response.body), config.sentryDsn)
throw new UnknownError()
})
return response
}
const updateCartItem = async ({ cartId, itemGroupKey, quantity = 1 }, config) => {
const params = {
data: {
type: 'items',
attributes: {
quantity
}
}
}
const response = await patch(getCartItemEndpoint(cartId, itemGroupKey), params, config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('updateCartItem Error', JSON.stringify(response.body), config.sentryDsn)
throw new UnknownError()
})
return response
}
const deleteCartItem = async ({ cartId, itemGroupKey }, config) => {
const response = await del(getCartItemEndpoint(cartId, itemGroupKey), config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('deleteCartItem 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(getCartCodesEndpoint(cartId), params, config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('addCartCodes Error', JSON.stringify(response.body), config.sentryDsn)
throw new UnknownError()
})
return response
}
const deleteCartCodes = async ({ cartId, code }, config) => {
const response = await del(getCartCodeEndpoint(cartId, code), config)
.then(response => response.statusCode < 300 ? response.body : Promise.reject(response))
.catch(response => {
logger.error('deleteCartCodes Error', JSON.stringify(response.body), config.sentryDsn)
throw new UnknownError()
})
return response
}
module.exports = {
getCarts,
getCart,
createCart,
addCartItem,
updateCartItem,
deleteCartItem,
addCartCodes,
deleteCartCodes
}