UNPKG

wholesale-vuex

Version:
306 lines (296 loc) 13.8 kB
import humps from 'lodash-humps'; import wholesaleOrderService from './wholesale_order.service'; import wholesaleOrderItemService from './wholesale_order_item.service'; const { doFetchList, doFetchWholesaleReport, doFetchLookupList ,doFetchLogisticsTrackerList} = wholesaleOrderService const { doFetchItemsByIds } = wholesaleOrderItemService export default { namespaced: true, state: { list: [], wholesaleOrdersWithBrands: [], itemsWthBrandsList: [], selected: [], recordsCount: 0, inProgress: false, reportInProgress: false, error: null, wholesaleReport: [], retailerList: [], distributorList: [], brandList: [], salesAgentsList: [], logisticsTracker:[], logisticsTrackerDatesList:[] }, 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; }, setItemsWthBrandsList(state, list) { state.itemsWthBrandsList = list; }, setListCount(state, number) { state.recordsCount = number; }, setWholesaleOrderWithBrands(state, list) { state.wholesaleOrdersWithBrands = list; }, setWholesaleReport(state, data) { state.wholesaleReport = data.statusGroup ? data.statusGroup : [] state.retailerList = data.retailerList && Object.keys(data.retailerList).length !== 0 ? data.retailerList : [] state.distributorList = data.distributorList && Object.keys(data.distributorList).length !== 0 ? data.distributorList : [] state.brandList = data.brandList && Object.keys(data.brandList).length !== 0 ? data.brandList : [], state.salesAgentsList = data.salesAgentsList && Object.keys(data.salesAgentsList).length !== 0 ? data.salesAgentsList : [], state.retailerList.unshift({ id: -1, name: 'All retailers' }) state.distributorList.unshift({ id: -1, name: 'All distributors' }) state.salesAgentsList.unshift({ id: -1, name: 'All sales agents' }) }, setReportInProgress(state, yesOrNo) { state.reportInProgress = yesOrNo }, setLogisticsTrackerList(state,list){ state.logisticsTracker=list if(list.length > 0) { state.logisticsTrackerDatesList = state.logisticsTracker.map( stu => stu.shipmentStartDate +'~'+ stu.shipmentCancelDate); } else{ state.logisticsTrackerDatesList=[]; } } }, actions: { fetchList: async ({ commit, dispatch, rootState }, { appId, soNumberLike, providerBusinessEntityId, seasonType,filterByStatus, filterByDistributor, filterByRetailer, filterBySeason, sortByDate, pageNumber, limit, filterByOrdersProcessingState }) => { commit('inProgress', true); try { let offset = (pageNumber - 1) * limit; const response = await doFetchList({ appId, soNumberLike, providerBusinessEntityId, seasonType, filterByStatus, filterByDistributor, filterByRetailer, filterBySeason, sortByDate, limit, offset, filterByOrdersProcessingState }); 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))); commit('inProgress', false); return humps(list) } else commit('setList', []); commit('inProgress', false); if (recursive && list.length && (!rootState.businessEntityOne.one || list[0].purchaser_business_entity_id !== rootState.businessEntityOne.one.id)) { dispatch('businessEntityOne/fetchOne', { businessEntityId: providerBusinessEntityId, appId: appId }, { root: true }) } } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, lookupList: async ({ commit, dispatch, rootState }, { appId, filter, list }) => { commit('inProgress', true); try { const response = await doFetchLookupList({ appId, filter, list }); if (response/*.list && response.list.items && response.list.items.length*/) { let recordsCount = response.list && response.list.items ? response.list.items.length : 0; commit('setListCount', Number(recordsCount)); commit('setWholesaleReport', { statusGroup: humps(response.status_group), retailerList: humps(response.retailers_group), distributorList: humps(response.distributors_group), brandList: humps(response.brands_group), salesAgentsList: humps(response.sales_agent_group), }); const list = await response.list.items; if (list) { commit('setList', humps(list)); commit('inProgress', false); return humps(list) }else{ commit('setList', humps([])); commit('inProgress', false); return humps([]) } // if (recursive && list.length && (!rootState.businessEntityOne.one || list[0].purchaser_business_entity_id !== rootState.businessEntityOne.one.id) ) { // dispatch('businessEntityOne/fetchOne', { // businessEntityId: providerBusinessEntityId, // appId: appId // }, { root: true }) // } } else { commit('setListCount', Number(0)); commit('setWholesaleReport', {}); commit('setList', []); commit('inProgress', false); } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, fetchItemsWthBrandsList: async ({ commit }, { appId, emailLike, providerBusinessEntityId, sortByDate, pageNumber, limit }) => { commit('inProgress', true); try { let offset = (pageNumber - 1) * limit; const response = await doFetchList({ appId, emailLike, providerBusinessEntityId, sortByDate, limit, offset }); if (response.range != null) { let recordsCount = response.range.split("/"); commit('setListCount', Number(recordsCount[1])); const list = await response.data; if (list) { const wholesaleIds = list.map((item) => { return item.id }) if (wholesaleIds) { const ids = wholesaleIds.join() let wholesaleItemsList = await doFetchItemsByIds({ ids }) const dataList = list.map((item) => { let newRec = {} let brandsList = [] for (let j = 0; j < wholesaleItemsList.length; j++) { if (item.id === wholesaleItemsList[j].wholesale_order_id) { brandsList.push(wholesaleItemsList[j].brand.name) } } newRec = Object.assign({}, item, { brands: [...new Set(brandsList)] }) return newRec }).filter((item) => item.brands.length > 0) commit('setItemsWthBrandsList', humps((dataList))); commit('inProgress', false) } } else commit('setItemsWthBrandsList', []); commit('inProgress', false) } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, fetchWholesaleOrdersWthBrands: async ({ commit, dispatch }, { appId, soNumberLike, providerBusinessEntityId, filterByStatus, filterByDistributor, filterBySeason, sortByDate, pageNumber, limit }) => { commit('inProgress', true); try { let offset = (pageNumber - 1) * limit; const response = await doFetchList({ appId, soNumberLike, providerBusinessEntityId, filterByStatus, filterByDistributor, filterBySeason, sortByDate, limit, offset }); if (response.range != null) { let recordsCount = response.range.split("/"); commit('setListCount', Number(recordsCount[1])); const list = await response.data; if (list) { let brandIds = [] list.filter((item) => { if (item.metadata.brand_id) brandIds.push(item.metadata.brand_id) }) const unqBrandIds = [...new Set(brandIds)] if (unqBrandIds) { const ids = unqBrandIds.join() let brandList = await dispatch('brandList/fetchBrandListByIds', { appId: appId, brandIds: ids, nameLike: '', pageNumber: 1, limit: unqBrandIds.length }, { root: true }) const dataList = list.map((item) => { let newRec = {} let brands = [] for (let j = 0; j < brandList.length; j++) { if (item.metadata.brand_id === brandList[j].id) { brands.push(brandList[j].name) } } newRec = Object.assign({}, item, { brands: [...new Set(brands)] }) return newRec }) commit('setWholesaleOrderWithBrands', humps((dataList))); commit('inProgress', false) return humps(dataList) } } else commit('setWholesaleOrderWithBrands', []); commit('inProgress', false) } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, fetchWholesaleReport: async ({ commit }, { purchaserIds, from, storeIds, to, providerIds,filters }) => { commit('setReportInProgress', true); try { const report = await doFetchWholesaleReport({ purchaserIds, from, storeIds, to, providerIds,filters }); if (report) { commit('setWholesaleReport', humps(report)); commit('setReportInProgress', false); } else { commit('setWholesaleReport', {}); commit('setReportInProgress', false); } } // eslint-disable-next-line no-useless-catch catch (err) { //throw err console.log(err) } finally { commit('setReportInProgress', false) } }, fetchLogisticsTrackerList: async({ commit }, { wholesaleOrderId }) => { commit('inProgress', true) try { const response = await doFetchLogisticsTrackerList({ wholesaleOrderId }) if (response) { const list = await response.shipments if (list) { commit('setLogisticsTrackerList', humps((list))) commit('inProgress', false) return (humps(list)) } else commit('setLogisticsTrackerList', []) commit('inProgress', false) return [] } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, } }