UNPKG

commerce-vuex

Version:
119 lines (105 loc) 4.4 kB
import { auth, nav } from 'oak-vuex/src' const { getHubAuthHeader } = auth const { wrapRequest,wrapRequestWithRange, wrapEmptyRequest, handleError } = nav const hubName = process.env.VUE_APP_RETAINHUB_NAME const retainhubCommerceApiUrl = process.env.VUE_APP_RETAINHUB_COMMERCE_API_URL const opsSlsApiUrl = process.env.VUE_APP_OPS_SLS_API_URL const doFetchList = wrapRequestWithRange(({ appId, search, retailerId,brandId,limit,offset}) => { const requestOptions = { method: 'GET', headers: { 'Content-type': 'application/json', 'Prefer': 'count=exact', ...getHubAuthHeader(hubName) } } const params = { app_id:`eq.${appId}`, limit: limit || 500, offset: offset || 0 } if(search && search.name) params['name'] = `ilike.*${search.name}*` if(search && search.category) params['size_category'] = `eq.${search.category}` if(search && search.collection) params['sku'] = `eq.${search.collection}` if(search && search.brandId) params['brand_id'] = `eq.${search.brandId}` if (retailerId) params['retailer_id'] = `eq.${retailerId}` if (brandId) params['brand_id'] = `eq.${brandId}` const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&'); return fetch(`${retainhubCommerceApiUrl}/retailer_product?${qs}&select=*,brand(name)`, requestOptions); }) const doFetchOne = wrapRequest(({collectionId, appId}) => { const requestOptions = { method: 'GET', headers: { 'Content-type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', ...getHubAuthHeader(hubName) }, }; return fetch(`${retainhubCommerceApiUrl}/retailer_product?id=eq.${collectionId}&app_id:eq.${appId}&select=*,brand(name)`, requestOptions); }) const doPostOne = wrapRequest((collection) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify(collection) } return fetch(`${retainhubCommerceApiUrl}/retailer_product`, requestOptions) }) const doPatchOne = wrapRequest((collection) => { const requestOptions = { method: 'PATCH', headers: { 'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify(collection) } return fetch(`${retainhubCommerceApiUrl}/retailer_product?id=eq.${collection.id}`, requestOptions) }) const doDeleteSelected = wrapEmptyRequest(({collectionId}) => { const requestOptions = { method: 'DELETE', headers: { 'Content-Type': 'application/json', ...getHubAuthHeader(hubName) }, }; return fetch(`${retainhubCommerceApiUrl}/retailer_product?id=in.(${collectionId})`, requestOptions) }) const doPostImportedDataOne = wrapRequest(({collection}) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify(collection) } //return fetch(`${retainhubCommerceApiUrl}/retailer_imported_product`, requestOptions) }) const doExportData = ({ retailerId, brandId, retainhubToken, type, providerBusinessEntityId, purchaserBusinessEntityId, format }) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'text/csv', 'Authorization' : 'Bearer '+ retainhubToken, }, body: JSON.stringify({ retailer_id: retailerId, brand_id: brandId, type, provider_business_entity_id : providerBusinessEntityId, purchaser_business_entity_id : purchaserBusinessEntityId, format }) } return fetch(`${opsSlsApiUrl}/export-items-to-excel`, requestOptions).then((response) =>{ if (!response.ok) { throw response; } else{ return response.blob() } }).catch(async error=>{ return await handleError(error) }) } export default { doFetchOne, doPostOne, doPatchOne, doDeleteSelected, doFetchList, doPostImportedDataOne, doExportData }