UNPKG

commerce-vuex

Version:
156 lines (154 loc) 5.07 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 stripeCustomer from './stripe_customer.service' const { doFetchCustomerOne,doPostUpdateAddress, doSetDefaultPaymentMethod, doPostStripeCustomer, doFetchCustomerByEmail } = stripeCustomer const snakes = createHumps(snakeCase) export default { namespaced: true, state: { one: null, inProgress: true, defaultAddress: { city: '', country: '', line1: '', line2: '', postalCode: '', state: '' }, }, getters: { getField, }, mutations: { updateField, setOne(state, one) { state.one = one; if(state.one && !state.one.address){ state.one.address=state.defaultAddress } }, inProgress(state, yesOrNo) { state.inProgress = yesOrNo } }, actions: { new({ commit }) { commit('inProgress', false); commit('clearDates') commit('setOne', { "address": { "city": '', "country": '', "line1": '', "line2": '', "postalCode": '', "state": '' }, }); }, save: async ({ commit }, { customer }) => { commit('inProgress', true); try { const customerOne = await doPostStripeCustomer({ customer }); if (customerOne) { commit('setOne', humps(customerOne)); return humps(customerOne) } else { commit('setOne', null); } } // eslint-disable-next-line no-useless-catch catch (err) { commit('setOne', null); throw err } finally { commit('inProgress', false) } }, fetchCustomerOne: async ({ commit }, { customerId }) => { commit('inProgress', true); try { const customer = await doFetchCustomerOne({ customerId }); if (customer) { commit('setOne', humps(customer)); return humps(customer) } else { commit('setOne', null); } } // eslint-disable-next-line no-useless-catch catch (err) { commit('setOne', null); throw err } finally { commit('inProgress', false) } }, fetchCustomerByEmail: async ({ commit }, { emailId }) => { commit('inProgress', true); try { const customer = await doFetchCustomerByEmail({ emailId }); if (customer && customer.data.length) { commit('setOne', humps(customer.data[0])); return humps(customer.data[0]) } else { commit('setOne', null); return null } } // eslint-disable-next-line no-useless-catch catch (err) { console.log(err) throw err } finally { commit('inProgress', false) } }, updateBilling: async ({ commit }, {address,id}) => { commit('inProgress', true); try { const session = await doPostUpdateAddress(snakes({ address,id })); if (session) { commit('setOne', humps(session)); return humps(session) } else { commit('setOne', null); } } // eslint-disable-next-line no-useless-catch catch (err) { commit('setOne', null); throw err } finally { commit('inProgress', false) } }, setDefaultPaymentMethod: async ({ commit }, { customerId, invoiceSettings }) => { commit('inProgress', true); try { const customer = await doSetDefaultPaymentMethod({ customerId, invoiceSettings: snakes(invoiceSettings) }); if (customer) { commit('setOne', humps(customer)); return humps(customer) } else { commit('setOne', null); } } // eslint-disable-next-line no-useless-catch catch (err) { commit('setOne', null); throw err } finally { commit('inProgress', false) } } }, }