UNPKG

commerce-vuex

Version:
592 lines (557 loc) 17.8 kB
import { auth, nav } from "oak-vuex"; const { getHubAuthHeader } = auth; const { wrapRequest, wrapRequestWithRange, wrapEmptyRequest } = nav; const hubName = process.env.VUE_APP_RETAINHUB_NAME; const retainhubCommerceApiUrl = process.env.VUE_APP_RETAINHUB_COMMERCE_API_URL; const retainhubProcurementApiUrl = process.env.VUE_APP_RETAINHUB_PROCUREMENT_API_URL; const doFetchList = wrapRequestWithRange( ({ appId, nameLike, businessEntityId, activeProfiles, limit, offset,platformStatus }) => { const requestOptions = { method: "GET", headers: { "Content-type": "application/json", Prefer: "count=estimated", ...getHubAuthHeader(hubName), }, }; const params = { app_id: `eq.${appId}`, limit: limit || 50, offset: offset || 0, }; if (nameLike) params["name"] = `ilike.*${nameLike}*`; if (businessEntityId) params["id"] = `eq.${businessEntityId}`; if (activeProfiles) params["active_profiles"] = `ov.{${activeProfiles}}`; if (platformStatus) params["platform_status"] = `eq.${platformStatus}&owner_id=is.null`; const qs = Object.keys(params) .map((key) => key + "=" + params[key]) .join("&"); return fetch( `${retainhubCommerceApiUrl}/business_entity?${qs}`, requestOptions ); } ); const doFetchBusinessOwnerList = wrapRequest( ({ appId, nameLike}) => { const requestOptions = { method: "GET", headers: { "Content-type": "application/json", Prefer: "count=estimated", ...getHubAuthHeader(hubName), }, }; const params = { app_id: `eq.${appId}`, owner_id:`is.null` }; if (nameLike) params["name"] = `ilike.*${nameLike}*`; const qs = Object.keys(params) .map((key) => key + "=" + params[key]) .join("&"); return fetch( `${retainhubCommerceApiUrl}/business_entity?${qs}`, requestOptions ); } ); const doFetchBusinessEntityListByOwnerId = wrapRequestWithRange( ({ appId, activeProfiles, nameLike, ownerId, limit, offset }) => { const requestOptions = { method: "GET", headers: { "Content-type": "application/json", Prefer: "count=estimated", ...getHubAuthHeader(hubName), }, }; const params = { app_id: `eq.${appId}`, limit: limit || 50, offset: offset || 0, }; if (nameLike) params["name"] = `ilike.*${nameLike}*`; if (ownerId) params["owner_id"] = `eq.${ownerId}`; if (activeProfiles) params["active_profiles"] = `ov.{${activeProfiles}}`; const qs = Object.keys(params) .map((key) => key + "=" + params[key]) .join("&"); return fetch( `${retainhubCommerceApiUrl}/business_entity?${qs}&order=name.asc`, requestOptions ); } ); const doFetchForPortfolioSelection = wrapRequestWithRange( ({ appId, limit, offset }) => { const requestOptions = { method: "GET", headers: { "Content-type": "application/json", Prefer: "count=estimated", ...getHubAuthHeader(hubName), }, }; const params = { app_id: `eq.${appId}`, limit: limit || 50, offset: offset || 0, }; // if (businessEntityId) params['id'] = `eq.${businessEntityId}` const qs = Object.keys(params) .map((key) => key + "=" + params[key]) .join("&"); return fetch( `${retainhubCommerceApiUrl}/business_entity?${qs}&select=id,name,active_profiles`, requestOptions ); } ); const doPostOne = wrapRequest((businessEntity) => { debugger if(businessEntity && businessEntity.country_iso_3){ businessEntity.country_iso3 = businessEntity.country_iso_3; delete businessEntity.country_iso_3; } const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify(businessEntity), }; return fetch(`${retainhubCommerceApiUrl}/business_entity`, requestOptions); }); const doPostServiceOne = wrapRequest((isActvie) => { const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify(isActvie), }; return fetch(`${retainhubCommerceApiUrl}/service`, requestOptions); }); const doPatchOne = wrapRequest((businessEntity) => { if ( businessEntity && (businessEntity.hasOwnProperty("city_xx") || businessEntity.hasOwnProperty("country_xx") || businessEntity.hasOwnProperty("country_iso_3") || businessEntity.hasOwnProperty("postcode_xx") || businessEntity.hasOwnProperty("address_xx")) ) { businessEntity.country_iso3 = businessEntity.country_iso_3; delete businessEntity.city_xx; delete businessEntity.country_xx; delete businessEntity.country_iso_3; delete businessEntity.postcode_xx; delete businessEntity.address_xx; } const requestOptions = { method: "PATCH", headers: { "Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify(businessEntity), }; return fetch( `${retainhubCommerceApiUrl}/business_entity?id=eq.${businessEntity.id}`, requestOptions ); }); const doFetchOne = wrapRequest((businessEntityId) => { const requestOptions = { method: "GET", headers: { "Content-type": "application/json", Accept: "application/vnd.pgrst.object+json", ...getHubAuthHeader(hubName), }, }; return fetch( `${retainhubCommerceApiUrl}/business_entity?id=eq.${businessEntityId}`, requestOptions ); }); const doDeleteSelected = wrapEmptyRequest(({businessEntityId}) => { const requestOptions = { method: "DELETE", headers: { "Content-Type": "application/json", ...getHubAuthHeader(hubName), }, }; return fetch( `${retainhubCommerceApiUrl}/business_entity?id=in.(${businessEntityId})`, requestOptions ); }); const doFetchCatalogReport = wrapRequest( ({ appId, from, to, ownerIds, brandIds, metadataProperty }) => { const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify({ _app_id: appId, _from: from, _to: to, _owner_ids: ownerIds, _brand_ids: brandIds, _metadata_property: metadataProperty, }), }; return fetch( `${retainhubCommerceApiUrl}/rpc/report_catalog`, requestOptions ); } ); const doAccountDetails = wrapRequest( ({ businessEntityId, providerId, fromDate, toDate }) => { const requestOptions = { method: "POST", headers: { "Content-type": "application/json", ...getHubAuthHeader(hubName), }, body: JSON.stringify({ business_entity_id: businessEntityId, provider_id: providerId, from_date: fromDate, to_date: toDate, }), }; return fetch( `${retainhubCommerceApiUrl}/rpc/business_account_balance`, requestOptions ); } ); const doBusinessEntityLookupList = wrapRequest(({ appId, filter }) => { const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify({ app_id: appId, filter: filter, }), }; return fetch( `${retainhubCommerceApiUrl}/rpc/business_entity_lookup `, requestOptions ); }); const doPatchPlatformStatus = wrapRequest( ({ platformStatus, businessEntityId }) => { const requestOptions = { method: "PATCH", headers: { "Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify({ platform_status: platformStatus, }), }; return fetch( `${retainhubCommerceApiUrl}/business_entity?id=eq.${businessEntityId}`, requestOptions ); } ); const doFetchBusinessEntityListByIds = wrapRequest( ({ appId, businessEntityIds }) => { const requestOptions = { method: "GET", headers: { "Content-type": "application/json", Prefer: "count=estimated", ...getHubAuthHeader(hubName), }, }; const params = { app_id: `eq.${appId}` }; if (businessEntityIds) params["id"] = `in.(${businessEntityIds})`; const qs = Object.keys(params) .map((key) => key + "=" + params[key]) .join("&"); return fetch( `${retainhubCommerceApiUrl}/business_entity?${qs}`, requestOptions ); } ); const doPostBusinessEntity = wrapRequest((multipleBusinessEntity) => { if (multipleBusinessEntity) { multipleBusinessEntity.country_iso3 = multipleBusinessEntity.country_iso_3; delete multipleBusinessEntity.country_iso_3; } const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify(multipleBusinessEntity), }; return fetch(`${retainhubCommerceApiUrl}/business_entity`, requestOptions); }); const doPostCreateBusinessOne = wrapRequest( ({ businessDetails, businessBrand, businessServices }) => { const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify({ business_details: businessDetails, business_brand: businessBrand, business_services: businessServices, }), }; return fetch( `${retainhubCommerceApiUrl}/rpc/business_entity_create_one`, requestOptions ); } ); const doPatchBusinessOne = wrapRequest(({ one, businessEntityId }) => { const requestOptions = { method: "PATCH", headers: { "Content-Type": "application/json", Accept: "application/vnd.pgrst.object+json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify(one), }; return fetch( `${retainhubCommerceApiUrl}/business_entity?id=eq.${businessEntityId}`, requestOptions ); }); // do post imported data from csv file const doPostImportedData = wrapRequest(({ data, format }) => { let businessEntityObject = {"id": '', "organization_id": '', "app_id": '', "name": '', "inc": '', "city_xx": '', "country_xx": '', "country_iso3": '', "registration_number": '', "registration_date": '', "tax_id": '', "avatar_url": '', "onboarding_data": {}, "active_profiles": '', "splash_image_url": '', "platform_status": '', "addresses": { "billing": { "city": '', "line": '', "country": '', "postcode":'', "country_iso_3": '' }, "shipping": { "city": '', "line": '', "country": '', "postcode":'', "country_iso_3": '' }}, "owner_id": '', "connected": '', "metadata": {} } let headers = data[0] let businessEntities = [] for(let d=1; d < data.length; d++){ let entity = {} if(data[d].length > 2) // to avoid last empty record { for(let h=0; h< headers.length; h++){ if(headers[h].indexOf('.') !== -1){ let objSplitted = headers[h].split('.') if(!entity[objSplitted[0]]) entity[objSplitted[0]] = {} entity[objSplitted[0]][objSplitted[1]] = data[d][h] == "null" ? null : data[d][h] == "undefined" ? undefined : data[d][h] } if(businessEntityObject.hasOwnProperty(headers[h])){ // for object if(['addresses', 'metadata', 'active_profiles', 'onboarding_data'].includes(headers[h])){ entity[headers[h]] = data[d][h] == "null" ? null : data[d][h] == "undefined" ? undefined : JSON.parse(data[d][h]) } //for string else { entity[headers[h]] = data[d][h] == "null" ? null : data[d][h] == "true" ? true : data[d][h] == "false" ? false : data[d][h] == "undefined" ? undefined : data[d][h] } } } if(entity.name !== undefined) businessEntities.push(entity) } } const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Prefer: ['resolution=merge-duplicates', 'return=representation'], ...getHubAuthHeader(hubName), }, body: JSON.stringify(businessEntities), }; return fetch(`${retainhubCommerceApiUrl}/business_entity`, requestOptions); }); // used for connection process const doPostCreateConnectionBusinessOne = wrapRequest( ({ businessDetails, businessBrand, businessServices, retainhubToken }) => { const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Prefer: "return=representation", 'Authorization' : 'Bearer '+ retainhubToken, }, body: JSON.stringify({ business_details: businessDetails, business_brand: businessBrand, business_services: businessServices, }), }; return fetch( `${retainhubCommerceApiUrl}/rpc/business_entity_create_one`, requestOptions ); } ); // used for connection process const doPostBusinessEnityAttachCopy = wrapRequest( ({ businessEnityAttachCopy, retainhubToken }) => { const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Prefer: "return=representation", 'Authorization' : 'Bearer '+ retainhubToken, }, body: JSON.stringify(businessEnityAttachCopy), }; return fetch( `${retainhubCommerceApiUrl}/rpc/business_entity_attach_copy`, requestOptions ); } ); const doFetchOneForConnection = wrapRequest(({businessEntityId, retainhubToken}) => { const requestOptions = { method: "GET", headers: { "Content-type": "application/json", Accept: "application/vnd.pgrst.object+json", 'Authorization' : 'Bearer '+ retainhubToken, }, }; return fetch( `${retainhubCommerceApiUrl}/business_entity?id=eq.${businessEntityId}`, requestOptions ); }); const doPostIsolateRecords = wrapRequest(({businessEntityId}) => { const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Prefer: "return=representation", ...getHubAuthHeader(hubName) }, body: JSON.stringify({"business_entity_id":businessEntityId}), }; return fetch( `${retainhubCommerceApiUrl}/rpc/business_entity_isolate_records`,requestOptions ); }); const doPatchOneProperty = wrapRequest(({businessEntityId,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(`${retainhubCommerceApiUrl}/business_entity?id=eq.${businessEntityId}`, requestOptions) }) const doFetchOneByName = wrapRequest(({businessEntityName,platformStatus}) => { const requestOptions = { method: "GET", headers: { "Content-type": "application/json", ...getHubAuthHeader(hubName), }, }; return fetch( `${retainhubCommerceApiUrl}/business_entity?name=eq.${businessEntityName}&platform_status=eq.${platformStatus}`, requestOptions ); }); const doBusinessEnityAttachCopy = wrapRequest( ({ businessEnityAttachCopy }) => { const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify(businessEnityAttachCopy), }; return fetch( `${retainhubCommerceApiUrl}/rpc/business_entity_attach_copy`, requestOptions ); } ); const doBusinessEntityPublishNewCopy = wrapRequest( ({ businessEntityId,specs }) => { if(specs && specs.country_iso_3){ specs.country_iso3 = specs.country_iso_3; delete specs.country_iso_3; } const requestOptions = { method: "POST", headers: { "Content-Type": "application/json", Prefer: "return=representation", ...getHubAuthHeader(hubName), }, body: JSON.stringify({"business_entity_id":businessEntityId,"specs":specs}), }; return fetch( `${retainhubCommerceApiUrl}/rpc/business_entity_publish_new_copy`, requestOptions ); } ); export default { doFetchOne, doFetchList, doPostOne, doPatchOne, doDeleteSelected, doFetchForPortfolioSelection, doFetchCatalogReport, doPostServiceOne, doAccountDetails, doBusinessEntityLookupList, doPatchPlatformStatus, doFetchBusinessEntityListByIds, doPostBusinessEntity, doPostCreateBusinessOne, doFetchBusinessEntityListByOwnerId, doPatchBusinessOne, doPostBusinessEnityAttachCopy, doPostCreateConnectionBusinessOne, doFetchOneForConnection, doPostImportedData, doPostIsolateRecords, doPatchOneProperty, doFetchBusinessOwnerList, doFetchOneByName, doBusinessEnityAttachCopy, doBusinessEntityPublishNewCopy };