wholesale-vuex
Version:
Commerce vuex module
158 lines (134 loc) • 6.38 kB
JavaScript
import { auth, nav } from 'oak-vuex'
const { getHubAuthHeader } = auth
const { wrapRequest, wrapRequestWithRange ,wrapEmptyRequest} = nav
const hubName = process.env.VUE_APP_RETAINHUB_NAME
const retainWholesaleApiUrl = process.env.VUE_APP_RETAINHUB_WHOLESALE_API_URL
const doFetchList = wrapRequestWithRange(({ soNumberLike,filterByStatus,filterBySeason, filterByDistributor, sortByDate, appId, limit, offset, providerBusinessEntityId }) => {
const requestOptions = {
method: 'GET',
headers: { 'Content-type': 'application/json', 'Prefer': 'count=exact', ...getHubAuthHeader(hubName) },
};
const params = { app_id:`eq.${appId}`, limit: limit || 50, offset: offset || 0}
if (filterBySeason) params['metadata->>ashta_season'] = `ilike.*${filterBySeason}*`
if (filterByDistributor) params['metadata->>distributor_name'] = `ilike.*${filterByDistributor}*`
//if (emailLike) params['email'] = `ilike.*${emailLike}*`
if (filterByStatus) params['status'] = `eq.${filterByStatus}`
if (soNumberLike) params['so_number'] = `ilike.*${soNumberLike}*`
if (sortByDate) params['order'] = `date.desc`
if(providerBusinessEntityId) params['provider_business_entity_id'] = `eq.${providerBusinessEntityId}`
const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&');
return fetch(`${retainWholesaleApiUrl}/wholesale_order?${qs}`, requestOptions);
})
const doPostOne = wrapRequest((orderDetails) => {
if(orderDetails && orderDetails.so_number)
delete orderDetails.so_number
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(orderDetails)
};
return fetch(`${retainWholesaleApiUrl}/packing_list`, requestOptions)
})
const doPatchOne = wrapRequest((orderDetails) => {
if(orderDetails && orderDetails.so_number)
delete orderDetails.so_number
const requestOptions = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(orderDetails)
};
return fetch(`${retainWholesaleApiUrl}/packing_list?id=eq.${orderDetails.id}`, requestOptions)
})
const doFetchPackingListOne = wrapRequest(({salesOrderId}) => {
const requestOptions = {
method: 'GET',
headers: { 'Content-type': 'application/json', ...getHubAuthHeader(hubName) },
};
return fetch(`${retainWholesaleApiUrl}/packing_list?reference_id=eq.${salesOrderId}&order=packing_date.desc&select=*,provider_business_entity(name),purchaser_business_entity(name)`, requestOptions);
})
const doPostPackingBoxes = wrapRequest((packingBoxes) => {
const requestOptions = {
method: 'POST',
//'Accept': 'application/vnd.pgrst.object+json',
headers: {
'Content-Type': 'application/json', 'Prefer': ['resolution=merge-duplicates', 'return=representation'], ...getHubAuthHeader(hubName)
},
body: JSON.stringify(packingBoxes)
};
return fetch(`${retainWholesaleApiUrl}/packing_list_box`, requestOptions)
})
const doPostPackingListItems = wrapRequest((orderItems) => {
const requestOptions = {
method: 'POST',
//'Accept': 'application/vnd.pgrst.object+json',
headers: {
'Content-Type': 'application/json',
'Prefer': ['resolution=merge-duplicates', 'return=representation'], ...getHubAuthHeader(hubName)
},
body: JSON.stringify(orderItems)
};
return fetch(`${retainWholesaleApiUrl}/packing_list_item`, requestOptions)
})
const doFetchPackingBoxList = wrapRequest(({ appId, packingListId }) => {
const requestOptions = {
method: 'GET',
headers: { 'Content-type': 'application/json', ...getHubAuthHeader(hubName) },
};
const params = { packing_list_id:`eq.${packingListId}`,app_id:`eq.${appId}`}
const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&');
return fetch(`${retainWholesaleApiUrl}/packing_list_box?${qs}&select=*,provider_business_entity(name, addresses),purchaser_business_entity(name)`, requestOptions);
})
const doFetchPackingItemsList = wrapRequest(({ appId, packingListId }) => {
const requestOptions = {
method: 'GET',
headers: { 'Content-type': 'application/json', ...getHubAuthHeader(hubName) },
};
const params = { packing_list_id:`eq.${packingListId}`,app_id:`eq.${appId}`}
const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&');
return fetch(`${retainWholesaleApiUrl}/packing_list_item?${qs}&select=*,provider_business_entity(name, addresses),purchaser_business_entity(name) `, requestOptions);
})
const doDeleteSelectedPackingList = wrapEmptyRequest(({ packingListId }) => {
const requestOptions = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json', ...getHubAuthHeader(hubName)
},
};
return fetch(`${retainWholesaleApiUrl}/packing_list?id=in.(${packingListId})`, requestOptions)
})
const doDeleteSelectedPackingListBoxes = wrapEmptyRequest(({ packingListId }) => {
const requestOptions = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json', ...getHubAuthHeader(hubName)
},
};
return fetch(`${retainWholesaleApiUrl}/packing_list_box?id=in.(${packingListId})`, requestOptions)
})
const doDeleteSelectedPackingListItems = wrapEmptyRequest(({ packingListId }) => {
const requestOptions = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json', ...getHubAuthHeader(hubName)
},
};
return fetch(`${retainWholesaleApiUrl}/packing_list_item?id=in.(${packingListId})`, requestOptions)
})
export default {
doFetchList,
doPostOne, doPatchOne,
doPostPackingBoxes,
doPostPackingListItems,
doFetchPackingItemsList,
doFetchPackingBoxList,
doFetchPackingListOne,
doDeleteSelectedPackingList,
doDeleteSelectedPackingListBoxes,
doDeleteSelectedPackingListItems
}