commerce-vuex
Version:
Commerce vuex module
95 lines (93 loc) • 3.65 kB
JavaScript
import humps from 'lodash-humps';
import subscription from './subscription.service';
const { doFetchList, doFetchSubscribeList } = subscription
import customer from './stripe_customer.service';
const { doFetchCustomerOne } = customer
import product from './stripe_product.service'
const { doFetchProductOne } = product
export default {
namespaced: true,
state: {
list: [],
selected: [],
recordsCount: 0,
inProgress: false,
error: null
},
mutations: {
inProgress(state, yesOrNo) {
state.inProgress = yesOrNo
},
setList(state, list) {
state.selected = [];
for (let i in state.list) {
state.selected.push(false);
state.selected[i] = false;
}
state.list = list;
},
setListCount(state, number) {
state.recordsCount = number;
}
},
actions: {
fetchList: async ({ commit }, { appId, nameLike, pageNumber, limit }) => {
commit('inProgress', true);
try {
let offset = (pageNumber - 1) * limit;
const response = await doFetchList({ appId, nameLike, limit, offset });
if (response.range != null) {
let recordsCount = response.range.split("/");
commit('setListCount', Number(recordsCount[1]));
const list = await response.data;
if (list) {
commit('setList', humps((list)));
} else
commit('setList', []);
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
fetchStripeSubscriptionList: async ({ commit }, { customerId, appId }) => {
commit('inProgress', true);
try {
const customerDetails = await doFetchCustomerOne({ customerId });
if (customerDetails) {
let newSubscriptionList = []
let subscriptionList = await doFetchSubscribeList ({ customerId : customerId });
subscriptionList = humps(subscriptionList.data)
for(let i=0; i<subscriptionList.length; i++){
let subscription = subscriptionList[i]
subscriptionList[i].customerDetails = customerDetails
for(let j=0; j<subscription.items.data.length; j++){
let subItem = subscription.items.data[j]
let product = await doFetchProductOne({productId: subItem.price.product})
subscriptionList[i].items.data[j].product = product
// subItem.product = product
}
newSubscriptionList.push(subscriptionList[i])
}
// let newSubscriptionList = subscriptionList.filter((item)=>{
// return item
// })
commit('setList', newSubscriptionList);
commit('inProgress', false);
return newSubscriptionList
}
}
// eslint-disable-next-line no-useless-catch
catch (err) {
throw err
}
finally {
commit('inProgress', false)
}
},
}
}