UNPKG

commerce-vuex

Version:
174 lines (166 loc) 5.8 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 { dateTimeFormate } from './common.service' import subscription from './subscription.service' const { doPostOne, doPatchOne, doFetchOne, doFetchLatestInvoice, doPayInvoice, doPostStripeSubscriptionOne } = subscription const snakes = createHumps(snakeCase) export default { namespaced: true, state: { one: null, inProgress: true, OrgList: [], startDtTime: '', startDtDate: '', stopDtTime: '', stopDtDate: '' }, getters: { getField, }, mutations: { updateField, setOne(state, one) { state.one = one; state.one.startDt = one.startDt ? dateTimeFormate(one.startDt) : ''; if (state.one.startDt.length) { state.startDtTime = state.one.startDt[0] state.startDtDate = state.one.startDt[1] } state.one.stopDt = one.stopDt ? dateTimeFormate(one.stopDt) : ''; if (state.one.stopDt.length) { state.stopDtTime = state.one.stopDt[0] state.stopDtDate = state.one.stopDt[1] } }, clearServicePricing(state) { state.one.servicePricingId = '' }, inProgress(state, yesOrNo) { state.inProgress = yesOrNo }, setDates(state) { state.one.startDt = new Date(state.startDtDate + " " + state.startDtTime) state.one.stopDt = new Date(state.stopDtDate + " " + state.stopDtTime) }, clearDates(state) { state.startDtTime = '' state.startDtDate = '' state.stopDtTime = '' state.stopDtDate = '' } }, actions: { new({ commit }) { commit('inProgress', false); commit('clearDates') commit('setOne', { name: "", serviceId: "", servicePricingId: "", buyerId: "", buyerType: "business", startDt: "", stopDt: "", charging: "monthly", status: "active" }); }, fetchOne: async ({ commit }, { subscriptionId, appId }) => { commit('inProgress', true); try { const subscription = await doFetchOne({ subscriptionId, appId }); if (subscription) { commit('setOne', humps(subscription)); commit('inProgress', false); return humps(subscription) } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, save: async ({ commit, state }, { appId }) => { commit('inProgress', true); try { commit('setDates') if (undefined === state.one.id) { let subscription = await doPostOne(snakes(Object.assign({}, state.one, { appId: appId }))); commit('inProgress', false); commit('clearDates') return ({ new: true, subscription: humps(subscription) }); } else { let subscription = await doPatchOne(snakes(state.one)); commit('inProgress', false); commit('clearDates') return ({ update: true, subscription: humps(subscription) }); } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, clearServicePricing: async ({ commit }) => { commit('clearServicePricing') }, saveStripeSubscription: async ({ commit }, { subscription }) => { commit('inProgress', true); try { let subscriptionOne = await doPostStripeSubscriptionOne({ subscription }); commit('inProgress', false); return humps(subscriptionOne) } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, fetchLatestInvoice: async ({ commit }, { latestInvoiceId }) => { commit('inProgress', true); try { const invoice = await doFetchLatestInvoice({ latestInvoiceId }); if (invoice) { commit('inProgress', false); return humps(invoice) } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, payLatestInvoice: async ({ commit }, { invoiceId }) => { commit('inProgress', true); try { const invoice = await doPayInvoice({ invoiceId }); if (invoice) { commit('inProgress', false); return humps(invoice) } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, }, }