UNPKG

commerce-vuex

Version:
237 lines (220 loc) 10.2 kB
import { auth, nav } from 'oak-vuex' const { getHubAuthHeader } = auth const { wrapRequest, wrapRequestWithRange } = nav const hubName = process.env.VUE_APP_RETAINHUB_NAME const retainhubCommerceApiUrl = process.env.VUE_APP_RETAINHUB_COMMERCE_API_URL const doFetchList = wrapRequestWithRange(({ appId, brandId, nameLike, limit, offset ,ownerId,name}) => { 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 (brandId) params['id'] = `eq.${brandId}` if (ownerId) params['owner_id'] = `eq.${ownerId}` if (name) params['name'] = `eq.${name}` const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&'); return fetch(`${retainhubCommerceApiUrl}/brand?${qs}&select=*,business_entity!owner_id(name)`, requestOptions) }) const doFetchServiceWithBrandAndBusiness = wrapRequestWithRange(({ filterByOwnerId,appId, 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 || 50, offset: offset || 0 } if (brandId) params['brand_id'] = `eq.${brandId}` if (filterByOwnerId) params['owner_id'] = `eq.${filterByOwnerId}` const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&'); return fetch(`${retainhubCommerceApiUrl}/service_with_brand_and_business?${qs}`, requestOptions) }) const doFetchBrandListByIds = wrapRequestWithRange(({ appId, brandIds, nameLike, 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['id'] = `in.(${brandIds})` const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&'); return fetch(`${retainhubCommerceApiUrl}/brand?${qs}`, requestOptions) }) const doFetchListByOwner = wrapRequest(({ ownerId,appId }) => { const requestOptions = { method: 'GET', headers: { 'Content-type': 'application/json', 'Prefer': 'count=exact', ...getHubAuthHeader(hubName) }, }; return fetch(`${retainhubCommerceApiUrl}/brand?owner_id=eq.${ownerId}&app_id=eq.${appId}`, requestOptions) }) const doFetchForPortfolioSelection = wrapRequest(({ ownerId,appId }) => { const requestOptions = { method: 'GET', headers: { 'Content-type': 'application/json', 'Prefer': 'count=exact', ...getHubAuthHeader(hubName) }, }; return fetch(`${retainhubCommerceApiUrl}/brand?owner_id=eq.${ownerId}&app_id=eq.${appId}&limit=5&select=id,name,owner_id(id,name)`, requestOptions) }) const doPostOne = wrapRequest(({newBrand}) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify(newBrand) }; return fetch(`${retainhubCommerceApiUrl}/brand`, requestOptions) }) const doPatchOne = wrapRequest((brand) => { const requestOptions = { method: 'PATCH', headers: { 'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify(brand) }; return fetch(`${retainhubCommerceApiUrl}/brand?id=eq.${brand.id}`, requestOptions) }) const doFetchOne = wrapRequest((brandId) => { const requestOptions = { method: 'GET', headers: { 'Content-type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', ...getHubAuthHeader(hubName) }, }; return fetch(`${retainhubCommerceApiUrl}/brand?id=eq.${brandId}`, requestOptions); }) const doDeleteSelected = wrapRequest(({brandId}) => { const requestOptions = { method: 'DELETE', headers: { 'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', ...getHubAuthHeader(hubName) }, }; return fetch(`${retainhubCommerceApiUrl}/brand?id=eq.${brandId}`, requestOptions) }) const doCreateNewBrandAndNewOwner = wrapRequest(({appId, name, ownerName, activeProfiles,businessEntityId}) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify( { brand_details:{app_id :appId, name:name}, owner_details : {}/*{app_id :appId, name:ownerName?ownerName:name+' Owner', active_profiles:activeProfiles, owner_id:businessEntityId}*/ , owner_services: []/*[{app_id:appId, name:name, naics_code:'3159', provisionings:{}}, {app_id:appId, name:name, naics_code:'4243', provisionings:{}}]*/ } ) }; return fetch(`${retainhubCommerceApiUrl}/rpc/brand_create_one`, requestOptions) }) const doPostCreateNewBrandServiceOwner = wrapRequest(({brandDetails,ownerDetails,ownerServices}) => { debugger const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify( { brand_details:{app_id :brandDetails.appId, name:brandDetails.name,description:brandDetails.description,logo_url:brandDetails.logoUrl,splash_image_url:brandDetails.splashImageUrl}, owner_details : {},//{id :ownerDetails.id, owner_id:ownerDetails.id} , owner_services: []/*[{app_id:ownerServices.appId, name:ownerServices.name, naics_code:ownerServices.naicsCode, provisionings:ownerServices.provisionings}]*/ } ) }; return fetch(`${retainhubCommerceApiUrl}/rpc/brand_create_one`, requestOptions) }) const doPostBoardNewBrand = wrapRequest(({brandDetails,ownerDetails,ownerServices}) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify( { brand_details:{app_id :brandDetails.appId, name :brandDetails.name}, owner_details : {},//{app_id :ownerDetails.appId, name:ownerDetails.name, active_profiles:ownerDetails.activeProfiles, owner_id:ownerDetails.ownerId} , owner_services: []//[{app_id:ownerServices.appId, name:ownerServices.name, naics_code:ownerServices.naicsCode, provisionings:ownerServices.provisionings, countries_iso3:ownerServices.countriesIso3}] } ) }; return fetch(`${retainhubCommerceApiUrl}/rpc/brand_create_one`, requestOptions) }) const doFetchBrandServicedLookup = wrapRequest(({filter,appId}) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify({ 'filter':filter, 'app_id':appId, }) }; return fetch(`${retainhubCommerceApiUrl}/rpc/brand_serviced_lookup`, requestOptions) }) const doFetchBrandClusterLookupList = wrapRequest(({ filter, appId}) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify({ filter, 'app_id': appId, }) }; return fetch(`${retainhubCommerceApiUrl}/rpc/brand_cluster_lookup`, requestOptions) }) const doFetchBrandWithServiceLookupList = wrapRequest(({ filter, appId}) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify({ filter, 'app_id': appId, }) }; return fetch(`${retainhubCommerceApiUrl}/rpc/brand_with_service_lookup`, requestOptions) }) const doFetchBrandQueryList = wrapRequest(({appId, specsFormat}) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify( { app_id:appId, specs:specsFormat } ) }; return fetch(`${retainhubCommerceApiUrl}/rpc/brand_query`, requestOptions) }) const doPostMergeAllRpc = wrapRequest(({appId,specs}) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify({ 'specs':specs, 'app_id':appId, }) }; return fetch(`${retainhubCommerceApiUrl}/rpc/brand_merge_all`, requestOptions) }) export default { doFetchOne, doFetchList, doPostOne, doPatchOne, doDeleteSelected, doFetchListByOwner,doFetchForPortfolioSelection, doFetchBrandListByIds,doCreateNewBrandAndNewOwner, doFetchBrandServicedLookup,doPostBoardNewBrand,doPostCreateNewBrandServiceOwner, doFetchBrandClusterLookupList, doFetchBrandWithServiceLookupList, doFetchBrandQueryList,doPostMergeAllRpc,doFetchServiceWithBrandAndBusiness }