UNPKG

commerce-vuex

Version:
733 lines (707 loc) 21.9 kB
import { snakeCase } from "lodash"; import humps from "lodash-humps"; import createHumps from "lodash-humps/lib/createHumps"; import { getField, updateField } from "vuex-map-fields"; import businessEntityService from "./business_entity.service"; import { addressesFormate, billingAsShippingAddress, customAddressesFormate, getPaymentTypesInArray, getPaymentTypesInObj, otherPaymentTermsList, updateTermsList } from "./common.service"; const { doPostOne, doPatchOne, doFetchOne, doFetchCatalogReport, doPostServiceOne, doAccountDetails, doPatchPlatformStatus, doPostBusinessEntity, doPostCreateBusinessOne, doPatchBusinessOne, doPostBusinessEnityAttachCopy, doPostCreateConnectionBusinessOne, doFetchOneForConnection, doPostImportedData, doPostIsolateRecords, doPatchOneProperty, doDeleteSelected, doFetchOneByName, doBusinessEnityAttachCopy, doBusinessEntityPublishNewCopy } = businessEntityService; const snakes = createHumps(snakeCase); export default { namespaced: true, state: { one: null, brandOwnerDetails: null, inProgress: true, OrgList: [], catalogReport: {}, catalogReportInProgress: true, billing: { bLine: "", bPostcode: "", bCity: "", bCountry: "", bCountryIso3: "", }, shipping: { sLine: "", sPostcode: "", sCity: "", sCountry: "", sCountryIso3: "", }, contactPerson:{ contactName:"", contactEmail:"", contactPhone:"" }, accountDetails: null, accountDetailsInProgress: true, otherPaymentTypes:[] }, getters: { getField, }, mutations: { updateField, setOne(state, one) { state.one = one; state.one.addresses = one.addresses; if ( state.one && state.one.addresses && state.one.addresses.billing && state.one.addresses.shipping ) { state.billing = customAddressesFormate( state.one.addresses.billing, "billing" ); state.shipping = customAddressesFormate( state.one.addresses.shipping, "shipping" ); } if( state.one.metadata && state.one.metadata.contactPerson === undefined ){ state.one.metadata['contactPerson'] =state.contactPerson } if (state.one.metadata ) { if(state.one.metadata.paymentTermsObject){ state.otherPaymentTypes = getPaymentTypesInArray(state.one.metadata.paymentTermsObject) state.otherPaymentTypes=updateTermsList(state.otherPaymentTypes) }else{ state.one.metadata.paymentTermsObject={} } }else{ state.otherPaymentTypes=[] } if(state.one.metadata === null){ state.one.metadata={} state.one.metadata['contactPerson'] =state.contactPerson } }, setBrandOwnerDetails(state, brandOwnerDetails) { state.brandOwnerDetails = brandOwnerDetails; state.brandOwnerDetails.addresses = brandOwnerDetails.addresses; if ( state.brandOwnerDetails && state.brandOwnerDetails.addresses && state.brandOwnerDetails.addresses.billing && state.brandOwnerDetails.addresses.shipping ) { state.billing = customAddressesFormate( state.brandOwnerDetails.addresses.billing, "billing" ); state.shipping = customAddressesFormate( state.brandOwnerDetails.addresses.shipping, "shipping" ); } }, inProgress(state, yesOrNo) { state.inProgress = yesOrNo; }, setList(state, list) { state.OrgList = list; }, setCatalogReportInProgress(state, yesOrNo) { state.catalogReportInProgress = yesOrNo; }, setCatalogReport(state, aReport) { state.catalogReport = aReport; }, setAddresses(state) { state.one.addresses = { billing: addressesFormate(state.billing, "billing"), shipping: addressesFormate(state.shipping, "shipping"), }; }, clearAddress(state) { (state.billing = { bLine: "", bPostcode: "", bCity: "", bCountry: "", bCountryIso3: "", }), (state.shipping = { sLine: "", sPostcode: "", sCity: "", sCountry: "", sCountryIso3: "", }); }, setShippingAddress(state) { state.shipping = billingAsShippingAddress(state.billing, "shipping"); }, clearShippingAddress(state) { state.shipping = { sLine: "", sPostcode: "", sCity: "", sCountry: "", sCountryIso3: "", }; }, setAccountDetailsInProgress(state, yesOrNo) { state.accountDetailsInProgress = yesOrNo; }, setAccountDetails(state, details) { state.accountDetails = details; }, addDefaultPaymentType(state, val) { if (state.otherPaymentTypes === undefined || state.otherPaymentTypes == null) { state.otherPaymentTypes = [{ type: 'Payment on order confirmation', paid: 30 },{ type: 'Payment after last delivery', paid: 70,days: 0 }] } if (val.type == 'new') { state.otherPaymentTypes = [] state.otherPaymentTypes.splice(0, 0, { type: '', paid: 0, days: 0 }) } else if (val.type == 'default') { state.otherPaymentTypes = [{ type: 'Payment on order confirmation', paid: 30},{ type: 'Payment after last delivery', paid: 70,days: 0 }] state.otherPaymentTypes=updateTermsList(state.otherPaymentTypes) }else if(val.type == 'splitPaymentIn30%Deposit/70%BFDelivery'){ state.otherPaymentTypes = [ { "type": "Payment on order confirmation", "paid": "30", "days": 0 }, { "type": "Payment before first delivery", "paid": "70", "days": 0 } ] }else if(val.type == 'splitPaymentIn30%Deposit/70%ALDelivery'){ state.otherPaymentTypes = [ { "type": "Payment on order confirmation", "paid": "30", "days": 0 }, { "type": "Payment after last delivery", "paid": "70", "days": 0 } ] }else if(val.type === 'reset'){ state.otherPaymentTypes = [] } else { let termsList= state.otherPaymentTypes.map((item)=> item.type) let difference = otherPaymentTermsList.filter(x => !termsList.includes(x)); state.otherPaymentTypes.splice(val.index, 0, { type:difference[0], paid: 0, days: 0 }) state.otherPaymentTypes=updateTermsList(state.otherPaymentTypes) } }, updatePaymentTerms(state){ state.otherPaymentTypes=updateTermsList(state.otherPaymentTypes) }, setOtherPaymentTypes(state, paymentTypes) { state.otherPaymentTypes = getPaymentTypesInArray(paymentTypes) }, getOtherPaymentTypesInObj(state, paymentTypes) { state.otherPaymentTypes = getPaymentTypesInObj(paymentTypes) }, deletePaymentTypeRow(state, index) { state.otherPaymentTypes.splice(index, 1); state.otherPaymentTypes=updateTermsList(state.otherPaymentTypes) //state.otherPaymentTypes=updateTermsList(state.otherPaymentTypes) }, }, actions: { new({ commit }) { commit("inProgress", false); commit("clearAddress"); commit("setOne", { name: "", // organizationId: "", inc: "", addresses: {}, registrationNumber: "", registrationDate: null, taxId: "", avatarUrl: "", splashImageUrl: "", activeProfiles: [], metadata:null, countryIso3:null }); }, fetchOne: async ( { commit, dispatch, rootState, state }, { businessEntityId, brandOwnerId, appId, recursive } ) => { commit("inProgress", true); commit("clearAddress"); //commit('setOne', null); let business = {}; if (undefined === brandOwnerId) { business = await doFetchOne(businessEntityId); } else { business = await doFetchOne(brandOwnerId); } if (business) { if (undefined === brandOwnerId) { commit("setOne", humps(business)); } else { commit("setBrandOwnerDetails", humps(business)); } commit("inProgress", false); if (!humps(business).addresses) { business.addresses = { billing: addressesFormate(state.billing, "billing"), shipping: addressesFormate(state.shipping, "shipping"), }; } return humps(business); } if (recursive && appId != rootState.appOne.one.id) { dispatch("appOne/fetchOne", appId, { root: true }); } commit("inProgress", false); }, fetchBusinessOne: async ( { commit }, { businessEntityId} ) => { commit("inProgress", true); try { let business = await doFetchOne(businessEntityId); if (business) { commit("inProgress", false); return humps(business); }else{ return null } }catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, setShippingAddress: async ({ commit }) => { commit("setShippingAddress"); }, clearShippingAddress: async ({ commit }) => { commit("clearShippingAddress"); }, save: async ({ commit, state }, { appId }) => { commit("inProgress", true); try { commit("setAddresses"); if (undefined === state.one.id) { let business = await doPostOne( snakes(Object.assign({}, state.one, { appId: appId })) ); commit("inProgress", false); commit("clearAddress"); return { new: true, business: humps(business) }; } else { let business = await doPatchOne(snakes(state.one)); commit("inProgress", false); commit("clearAddress"); return { update: true, business: humps(business) }; } } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, updateFields: async ({ commit }, { businessEntityId, one }) => { commit("inProgress", true); try { let business = await doPatchBusinessOne({ businessEntityId, one: snakes(one), }); commit("inProgress", false); return { update: true, business: humps(business) }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, cloneNewBusinessOne: async ({ commit, state }, { newObj }) => { commit("inProgress", true); try { let newBusiness = await doPostOne(snakes(newObj)); commit("inProgress", false); return { new: true, business: humps(newBusiness) }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, fetchCatalogReport: async ( { commit }, { appId, from, to, ownerIds, brandIds, metadataProperty } ) => { commit("setCatalogReportInProgress", true); try { const report = await doFetchCatalogReport({ appId, from, to, ownerIds, brandIds, metadataProperty, }); if (report) { commit("setCatalogReport", humps(report)); commit("setCatalogReportInProgress", false); } else { commit("setCatalogReport", {}); commit("setCatalogReportInProgress", false); } } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("setCatalogReportInProgress", false); } }, toggleBrandOwnerProfile: async ({ commit, state }, { appId, isActive }) => { commit("inProgress", true); try { await doPostServiceOne( snakes(Object.assign({}, isActive, { appId: appId })) ); // create or patch a service according to the profile // naics 3159 name 'Apparel Accessories and Other Apparel Manufacturing' // await doPatchOne(snakes(state.one)); commit("inProgress", false); return { update: true }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, toggleRetailerProfile: async ({ commit, state }, { appId, isActive }) => { commit("inProgress", true); try { await doPostServiceOne( snakes(Object.assign({}, isActive, { appId: appId })) ); // create or patch a service according to the profile // naics 4481 name Apparel, Piece Goods, and Notions Merchant Wholesalers // await doPatchOne(snakes(state.one)); commit("inProgress", false); return { update: true }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, toggleDistributorProfile: async ( { commit, state }, { appId, isActive } ) => { commit("inProgress", true); try { await doPostServiceOne( snakes(Object.assign({}, isActive, { appId: appId })) ); // create or patch a service according to the profile // naics 4243 name Apparel, Piece Goods, and Notions Merchant Wholesalers // await doPatchOne(snakes(state.one)); commit("inProgress", false); return { update: true }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, accountDetails: async ( { commit, state }, { businessEntityId, providerId, fromDate, toDate } ) => { commit("setAccountDetailsInProgress", true); try { const details = await doAccountDetails({ businessEntityId, providerId, fromDate, toDate, }); if (details) { commit("setAccountDetails", humps(details)); commit("setAccountDetailsInProgress", false); } else { commit("setAccountDetails", null); commit("setAccountDetailsInProgress", false); } } catch (err) { throw err; } finally { commit("setAccountDetailsInProgress", false); } }, updatePlatformStatus: async ( { commit }, { platformStatus, businessEntityId } ) => { commit("inProgress", true); try { const business = await doPatchPlatformStatus({ businessEntityId, platformStatus, }); if (business) { commit("setOne", humps(business)); commit("inProgress", false); } else { commit("setOne", null); commit("inProgress", false); } return humps(business); } catch (err) { throw err; } finally { commit("inProgress", false); } }, createNewBusinessEntity: async ({ commit }, multipleBusinessEntity) => { commit("inProgress", true); try { let businesses = await doPostBusinessEntity( snakes(multipleBusinessEntity) ); if (businesses) { commit("inProgress", false); return humps(businesses); } else { return []; } } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, createBusinessOne: async ( { commit }, { businessDetails, businessBrand, businessServices } ) => { commit("inProgress", true); try { let output = await doPostCreateBusinessOne({ businessDetails, businessBrand, businessServices, }); commit("inProgress", false); return { output: humps(output) }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, // used for connection process fetchOneForConnection: async ( { commit, dispatch, rootState, state }, { businessEntityId, retainhubToken, appId, recursive } ) => { commit("inProgress", true); //commit('setOne', null); let business = {}; business = await doFetchOneForConnection({businessEntityId, retainhubToken}); if (business) { commit("setOne", humps(business)); commit("inProgress", false); if (!humps(business).addresses) { business.addresses = { billing: addressesFormate(state.billing, "billing"), shipping: addressesFormate(state.shipping, "shipping"), }; } return humps(business); } if (recursive && appId != rootState.appOne.one.id) { dispatch("appOne/fetchOne", appId, { root: true }); } commit("inProgress", false); }, // used for connection process createConnectionBusinessOne: async ( { commit }, { businessDetails, businessBrand, businessServices, retainhubToken } ) => { commit("inProgress", true); try { let output = await doPostCreateConnectionBusinessOne({ businessDetails, businessBrand, businessServices, retainhubToken }); commit("inProgress", false); return { output: humps(output) }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, // used for connection process businessEnityAttachCopy: async ( { commit }, { businessEnityAttachCopy, retainhubToken } ) => { commit("inProgress", true); try { let output = await doPostBusinessEnityAttachCopy({ businessEnityAttachCopy : snakes(businessEnityAttachCopy), retainhubToken }); commit("inProgress", false); return { output: humps(output) }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, saveImportedData: async ({ commit }, { data, format }) => { commit("inProgress", true); try { let business = await doPostImportedData({ data, format }); commit("inProgress", false); return { new: true, business: humps(business) }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, isolateBusinessEntityRecords: async ({ commit }, { businessEntityId }) => { commit("inProgress", true); try { let business = await doPostIsolateRecords({ businessEntityId }); commit("inProgress", false); return { new: true, business: humps(business) }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, updateBusinessProperty: async ({ commit }, { businessEntityId, property }) => { commit("inProgress", true); try { let order = await doPatchOneProperty({ businessEntityId, property: snakes(property) }); commit("inProgress", false); return { order }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, deleteSelected: async ({ commit }, {businessEntityId }) => { commit('inProgress', true); try { await doDeleteSelected({businessEntityId }); return ({ deleteSelected: true }) } catch (err) { return ({ deleteSelected: false }) } finally { commit('inProgress', false); } }, fetchBusinessOneByName: async ( { commit }, { businessEntityName,platformStatus} ) => { commit("inProgress", true); try { let business = await doFetchOneByName({businessEntityName,platformStatus}); if (business) { commit("inProgress", false); return humps(business); }else{ return null } }catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, doBusinessEnityAttachCopy: async ( { commit }, { businessEnityAttachCopy } ) => { commit("inProgress", true); try { let output = await doBusinessEnityAttachCopy({ businessEnityAttachCopy }); commit("inProgress", false); return { output: humps(output) }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, addDefaultPaymentType: async ({ commit }, val) => { commit('addDefaultPaymentType', val); }, deletePaymentTypeRow: async ({ commit }, index) => { commit('deletePaymentTypeRow', index); }, businessEntityPublishNewCopy: async ( { commit,state }, { businessEntityId,appId } ) => { commit("setAddresses"); commit("inProgress", true); try { let output = await doBusinessEntityPublishNewCopy({ businessEntityId,specs:snakes(Object.assign({}, state.one, { appId: appId })) }); commit("inProgress", false); return { output: humps(output) }; } catch (err) { // eslint-disable-next-line no-useless-catch throw err; } finally { commit("inProgress", false); } }, }, };