commerce-vuex
Version:
Commerce vuex module
139 lines (124 loc) • 6.01 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 oakTradeApiUrl = process.env.VUE_APP_RETAINHUB_COMMERCE_API_URL
const doFetchList = wrapRequestWithRange(({ nameLike,serviceId, appId, businessEntityId, countryIso3, limit, offset,brandId }) => {
const requestOptions = {
method: 'GET',
headers: { 'Content-type': 'application/json', 'Prefer': 'count=exact', ...getHubAuthHeader(hubName) },
};
const params = { app_id:`eq.${appId}`, business_entity_id:`eq.${businessEntityId}`,limit: limit || 50, offset: offset || 0}
if (nameLike) params['name'] = `ilike.*${nameLike}*`
if (serviceId) params['id'] = `eq.${serviceId}`
if (countryIso3) params['countries_iso3'] = `ov.{${countryIso3}}`
if (brandId) params['brand_id'] = `eq.${brandId}`
const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&');
// return fetch(`${oakTradeApiUrl}/service?${qs}&select=*,business_entity!business_entity_id(name)`, requestOptions);
return fetch(`${oakTradeApiUrl}/service_with_brand_and_business?${qs}`, requestOptions);
})
const doFetchServiceLookUpList = wrapRequest(({ filter,appId}) => {
const requestOptions = {
method: 'POST',
headers: { 'Content-type': 'application/json', 'Prefer': 'count=exact', ...getHubAuthHeader(hubName) },
body: JSON.stringify({
filter: filter,
app_id: appId
})
};
return fetch(`${oakTradeApiUrl}/rpc/service_lookup`, requestOptions);
})
const doFetchDistributorList = wrapRequestWithRange(({ nameLike,brandIds, businessEntityId,naicsCode,appId, limit, offset }) => {
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 (nameLike) params['name'] = `ilike.*${nameLike}*`
if (brandIds) params['brands'] = `ov.{${brandIds}}`
if (businessEntityId) params['business_entity_id'] = `not.eq.${businessEntityId}`
if (naicsCode) params['naics_code'] = `eq.${naicsCode}`
const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&');
return fetch(`${oakTradeApiUrl}/service?${qs}&select=*,business_entity!business_entity_id(name)`, requestOptions);
})
const doPostOne = wrapRequest((service) => {
if(service&&service.countries_iso_3){
let iso3 = service.countries_iso_3;
delete service.countries_iso_3;
service.countries_iso3 = iso3;
}
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(service)
};
return fetch(`${oakTradeApiUrl}/service`, requestOptions)
})
const doPatchOne = wrapRequest((service) => {
if(service&&service.countries_iso_3){
let iso3 = service.countries_iso_3;
delete service.countries_iso_3;
service.countries_iso3 = iso3;
}
const requestOptions = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(service)
};
return fetch(`${oakTradeApiUrl}/service?id=eq.${service.id}`, requestOptions)
})
const doFetchOne = wrapRequest(({serviceId}) => {
const requestOptions = {
method: 'GET',
headers: { 'Content-type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', ...getHubAuthHeader(hubName) },
};
// return fetch(`${oakTradeApiUrl}/service?id=eq.${serviceId}`, requestOptions);
return fetch(`${oakTradeApiUrl}/service_with_brand_and_business?id=eq.${serviceId}`, requestOptions);
})
const doDeleteSelected = wrapEmptyRequest(({serviceId}) => {
const requestOptions = {
method: 'DELETE',
headers: { 'Content-type': 'application/json', ...getHubAuthHeader(hubName) },
};
return fetch(`${oakTradeApiUrl}/service?id=in.(${serviceId})`, requestOptions)
})
const doDeleteMultiple = wrapEmptyRequest(({serviceId}) => {
const requestOptions = {
method: 'DELETE',
headers: { 'Content-type': 'application/json', ...getHubAuthHeader(hubName) },
};
return fetch(`${oakTradeApiUrl}/service?id=in.(${serviceId})`, requestOptions)
})
const doFetchServiceTeamsLookUpList = wrapRequest(({serviceTeam}) => {
const requestOptions = {
method: 'POST',
headers: { 'Content-type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', ...getHubAuthHeader(hubName) },
body: JSON.stringify({
app_id: serviceTeam.appId,
filter: { business_entity_id: serviceTeam.businessEntityId, team_id: serviceTeam.teamId }
})
};
return fetch(`${oakTradeApiUrl}/rpc/service_teams_lookup`, requestOptions)
})
const doFetchBusinessEntityWithServiceLookUpList = wrapRequest(({salesAgentServiceFilter}) => {
const requestOptions = {
method: 'POST',
headers: { 'Content-type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', ...getHubAuthHeader(hubName) },
body: JSON.stringify({
app_id: salesAgentServiceFilter.appId,
filter:salesAgentServiceFilter.filter
})
};
return fetch(`${oakTradeApiUrl}/rpc/business_entity_with_service_lookup`, requestOptions)
})
export default {
doFetchOne, doFetchList,
doPostOne, doPatchOne, doDeleteSelected,
doFetchDistributorList,doFetchServiceTeamsLookUpList,doFetchBusinessEntityWithServiceLookUpList,doFetchServiceLookUpList,doDeleteMultiple
}