wholesale-vuex
Version:
Commerce vuex module
488 lines (447 loc) • 19.8 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,seasonType,filterBySeason, filterByDistributor, filterByRetailer, sortByDate, appId, limit, offset, providerBusinessEntityId, filterByOrdersProcessingState }) => {
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->>' + `${seasonType}`] = `ilike.*${filterBySeason}*`
if (filterByDistributor) params['metadata->>distributor_name'] = `ilike.*${filterByDistributor}*`
if (filterByRetailer) params['purchaser_business_entity_id'] = `eq.${filterByRetailer}`
//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 (filterByOrdersProcessingState) params['state'] = `in.(${filterByOrdersProcessingState})`
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}&select=*,provider_business_entity(name),purchaser_business_entity(name)`, requestOptions);
})
const doFetchLookupList = wrapRequest(({appId, filter, list}) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(
{
app_id: appId,
list: list,
filter : filter,
}
)
};
return fetch(`${retainWholesaleApiUrl}/rpc/wholesale_order_lookup
`, requestOptions)
})
const doPostOne = wrapRequest((orderDetails) => {
if(orderDetails && orderDetails.metadata.new_retailer_name){
delete orderDetails.metadata.retailer_id
}
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}/wholesale_order`, requestOptions)
})
const doPatchOne = wrapRequest((orderDetails) => {
if(orderDetails && orderDetails.metadata.retailer_id ==''){
delete orderDetails.metadata.retailer_id
}
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}/wholesale_order?id=eq.${orderDetails.id}`, requestOptions)
})
const doOrderStatusUpdate = wrapRequest(({ id,isProcessing, status, state }) => {
const params = {}
if (isProcessing) params['is_processing'] = isProcessing
if (status) params['status'] = status
if (state) params['state'] = state
const requestOptions = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(params)
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order?id=eq.${id}`, requestOptions)
})
const doFetchOne = wrapRequest(({wholesaleOrderId}) => {
const requestOptions = {
method: 'GET',
headers: { 'Content-type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', ...getHubAuthHeader(hubName) },
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order?id=eq.${wholesaleOrderId}&select=*,provider_business_entity(name),purchaser_business_entity(name)`, requestOptions);
})
const doDeleteSelected = wrapEmptyRequest(({wholesaleOrderId}) => {
const requestOptions = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json', ...getHubAuthHeader(hubName)
},
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order?id=in.(${wholesaleOrderId})`, requestOptions)
})
const doWholesaleOrderGetTimeline = wrapRequest(({ wholesaleOrderId }) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(
{
wholesale_order_id:wholesaleOrderId
}
)
};
return fetch(`${retainWholesaleApiUrl}/rpc/wholesale_order_get_timeline`, requestOptions)
})
const doWholesaleOrderReportChanges = wrapRequest(({ salesOrderId, filter}) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(
{
wholesale_order_id : salesOrderId,
filter: filter
}
)
};
return fetch(`${retainWholesaleApiUrl}/rpc/wholesale_order_report_changes`, requestOptions)
})
const doFetchWholesaleOrderChange = wrapRequest(({salesOrderId}) => {
const requestOptions = {
method: 'GET',
headers: { 'Content-type': 'application/json', ...getHubAuthHeader(hubName) },
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order_change?wholesale_order_id=eq.${salesOrderId}`, requestOptions);
})
const doPostOrderChangeResponse = wrapRequest((salesOrderChangeObject) => {
if(salesOrderChangeObject&&salesOrderChangeObject.change_values&& salesOrderChangeObject.change_values.new.country_iso_3){
const iso3 = salesOrderChangeObject.change_values.new.country_iso_3
delete salesOrderChangeObject.change_values.new.country_iso_3
salesOrderChangeObject.change_values.new.country_iso3 = iso3
}
if(salesOrderChangeObject&&salesOrderChangeObject.change_values&& salesOrderChangeObject.change_values.old.country_iso_3){
const iso3 = salesOrderChangeObject.change_values.old.country_iso_3
delete salesOrderChangeObject.change_values.old.country_iso_3
salesOrderChangeObject.change_values.old.country_iso3 = iso3
}
if(salesOrderChangeObject.id)
delete salesOrderChangeObject.id
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(salesOrderChangeObject)
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order_change`, requestOptions)
})
const doWholesaleOrderCompare = wrapRequest(({wholesaleOrderId,ts1,ts2}) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(
{
wholesale_order_id : wholesaleOrderId,
ts_1: ts1,
ts_2: ts2,
}
)
};
return fetch(`${retainWholesaleApiUrl}/rpc/wholesale_order_compare`, requestOptions)
})
const doFetchWholesaleReport = wrapRequest(({purchaserIds,from,storeIds,to,providerIds,filters}) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
/* body: JSON.stringify({ _purchaser_ids:purchaserIds, _from:from, _store_ids:storeIds, _to:to,_provider_ids:providerIds})*/
body: JSON.stringify({ _purchaser_ids:purchaserIds, _from:from, _store_ids:storeIds, _to:to,_provider_ids:providerIds,_filters:filters})
};
return fetch(`${retainWholesaleApiUrl}/rpc/report_wholesale_orders`, requestOptions)
})
const doWholesaleOrderUpdateItemsFormat = wrapRequest(({salesOrderId, format}) => {
const requestOptions = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(format)
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order?id=eq.${salesOrderId}`, requestOptions)
})
const doWholesaleOrderUpdateBusinessEntity = wrapRequest(({salesOrderId, wholesaleOrdersBusinessEntity}) => {
const requestOptions = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(wholesaleOrdersBusinessEntity)
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order?id=eq.${salesOrderId}`, requestOptions)
})
const doPatchOneProperty = wrapRequest(({wholesaleOrderId,property}) => {
const requestOptions = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(property)
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order?id=eq.${wholesaleOrderId}`, requestOptions)
})
const doFetchLogisticsTrackerList = wrapRequest(({wholesaleOrderId}) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(
{
wholesale_order_id:wholesaleOrderId
}
)
};
return fetch(`${retainWholesaleApiUrl}/rpc/wholesale_order_get_logistics`, requestOptions)
})
const doGetWholesaleOrderChanges = wrapRequest(({ wholesaleOrderChangesetId}) => {
const requestOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
// body: JSON.stringify(
// {
// "wholesale_order_id": wholesaleOrderId,
// "specs": specs
// }
// )
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order_change?wholesale_order_changeset_id=eq.${wholesaleOrderChangesetId}`, requestOptions)
})
const doGetOrderChangesForReport = wrapRequest(({ changesetId }) => {
const requestOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
...getHubAuthHeader(hubName)
},
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order_change?wholesale_order_changeset_id=eq.${changesetId}&provider_ok=is.null`, requestOptions)
})
const doPatchWholesaleOrderChangesetOne = wrapRequest(({ one }) => {
const requestOptions = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(one)
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order_changeset?id=eq.${one.id}`, requestOptions)
})
const doWholesaleOrderDiscardChanges = wrapRequest(({ wholesaleOrderId, specs }) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json', ...getHubAuthHeader(hubName)
},
body: JSON.stringify({
"purchase_order_id": wholesaleOrderId,
"specs": specs
})
};
return fetch(`${retainWholesaleApiUrl}/rpc/wholesale_order_discard_changes`, requestOptions)
})
const doPostSalesOrderChanges = wrapRequest(({ wholesaleOrderChangesetId, change,itemIcpData,icpData,oldIcpData }) => {
// let modifiedObjs = null
// if(changeObjects)
// modifiedObjs = handleSnakeCaseIssue(changeObjects)
// else
// modifiedObjs = handleSnakeCaseIssue(changeObject)
// let prefer = null
// if(changeObject){
// prefer = ['return=representation']
// } else {
// prefer = ['resolution=merge-duplicates', 'return=representation']
// }
if(change && change.change_values && (change.change_values.new || change.change_values.old)){
if(icpData && change.change_values.new){
change.change_values.new.icp_data=icpData
}
if(oldIcpData && change.change_values.old){
change.change_values.old.icp_data=(oldIcpData)
}
}
if(change && change.change_values && (change.change_values.new || change.change_values.old)){
if(itemIcpData && change.change_values.new){
change.change_values.new.icp_data=itemIcpData
}
if(itemIcpData && change.change_values.old){
change.change_values.old.icp_data=itemIcpData
}
}
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Prefer': 'resolution=merge-duplicates', ...getHubAuthHeader(hubName)
},
body: JSON.stringify({
"wholesale_order_changeset_id": wholesaleOrderChangesetId,
"change": change
})
};
return fetch(`${retainWholesaleApiUrl}/rpc/wholesale_order_add_change`, requestOptions)
})
function handleSnakeCaseIssue(changes) {
let allObjects = []
if(changes && changes.app_id){
changes = [changes] // convert single obj to array
}
if(changes && changes.length){
for(let c=0; c<changes.length; c++){
let currentChange = changes[c]
if(currentChange.change == "shipping_address" || currentChange.change == "billing_address"){
let orginalObj = JSON.stringify(currentChange.change_values)
orginalObj = JSON.parse(orginalObj)
if(orginalObj.new){
let iso3 = orginalObj.new.country_iso_3
delete orginalObj.new.country_iso_3
orginalObj.new.country_iso3 = iso3
}
if(orginalObj.old){
let iso3 = orginalObj.old.country_iso_3
delete orginalObj.old.country_iso_3
orginalObj.old.country_iso3 = iso3
}
currentChange.change_values = orginalObj
}
allObjects.push(currentChange)
}
return allObjects
}
}
/* discrepancy report services */
const doFetchWholesaleOrderDiscrepancyByChangesetId = wrapRequest(({ salesOrderId, changesetId, specs }) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(
{
wholesale_order_id : salesOrderId,
wholesale_order_changeset_id: changesetId,
specs
}
)
};
return fetch(`${retainWholesaleApiUrl}/rpc/wholesale_order_report_on_changeset`, requestOptions)
})
const doPatchWholesaleOrderChangeSet = wrapRequest(({ changesetId, property }) => {
const requestOptions = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify({ ...property })
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order_changeset?id=eq.${changesetId}`, requestOptions)
})
const doPatchWholesaleOrderChange = wrapRequest(({ changeId, property }) => {
const requestOptions = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify({ ...property })
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order_change?id=eq.${changeId}`, requestOptions)
})
const doWholesaleBusinessImportAllPos = wrapRequest(({ registeredProviderBusinessEntityId, registeredPurchaserBusinessEntityId}) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json', ...getHubAuthHeader(hubName)
},
body: JSON.stringify({"registered_provider_business_entity_id":registeredProviderBusinessEntityId,"registered_purchaser_business_entity_id":registeredPurchaserBusinessEntityId,'context':{}}),
};
return fetch(`${retainWholesaleApiUrl}/rpc/wholesale_business_import_all_pos`, requestOptions)
});
const doDeleteChangesetXChange = wrapRequest(({ changeId }) => {
const requestOptions = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order_xchange?id=eq.${changeId}`, requestOptions)
})
const doDeleteChangesetChange = wrapRequest(({ changeId }) => {
const requestOptions = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
};
return fetch(`${retainWholesaleApiUrl}/wholesale_order_change?id=eq.${changeId}`, requestOptions)
})
const doPostBalanceOrderPayments = wrapRequest(({ wholesaleOrderId}) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Prefer': 'resolution=merge-duplicates', ...getHubAuthHeader(hubName)
},
body: JSON.stringify({
"wholesale_order_id":wholesaleOrderId
})
};
return fetch(`${retainWholesaleApiUrl}/rpc/wholesale_order_balance_payments`, requestOptions)
})
export default {
doFetchOne, doFetchList, doFetchLookupList,
doPostOne, doPatchOne, doDeleteSelected, doOrderStatusUpdate,doWholesaleOrderGetTimeline,doWholesaleOrderCompare,doFetchWholesaleReport,doWholesaleOrderUpdateItemsFormat,doWholesaleOrderUpdateBusinessEntity,doPatchOneProperty, doWholesaleOrderReportChanges, doFetchWholesaleOrderChange, doPostOrderChangeResponse, doFetchLogisticsTrackerList, doPostSalesOrderChanges, doGetWholesaleOrderChanges, doPatchWholesaleOrderChangesetOne,doWholesaleOrderDiscardChanges,
doFetchWholesaleOrderDiscrepancyByChangesetId, doPatchWholesaleOrderChange, doGetOrderChangesForReport,
doPatchWholesaleOrderChangeSet, doWholesaleBusinessImportAllPos,
doDeleteChangesetXChange,doDeleteChangesetChange,doPostBalanceOrderPayments
}