Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved: support to fetch route with current group and update routes in bulk(feat/route) #41

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/components/ArchivedRoutingModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
</template>

<script setup lang="ts">
import emitter from "@/event-bus";
import { Route } from "@/types";
import {
IonButton,
Expand All @@ -41,26 +40,26 @@ import {
} from "@ionic/vue";
import { closeOutline } from "ionicons/icons";
import { defineProps, ref } from "vue";
import { useStore } from "vuex";

const store = useStore()
const props = defineProps({
archivedRoutings: {
required: true
}
})

let routings = ref(props.archivedRoutings)
let routings = ref(props.archivedRoutings) as any
let routingsToUpdate = ref([]) as any

function closeModal() {
modalController.dismiss({ dismissed: true });
modalController.dismiss({ dismissed: true, routings: routingsToUpdate.value.concat(routings.value) });
}

async function updateOrderRouting(routing: Route, fieldToUpdate: string, value: string) {
const orderRoutingId = await store.dispatch("orderRouting/updateOrderRouting", { orderRoutingId: routing.orderRoutingId, fieldToUpdate, value })
if(orderRoutingId) {
routings.value = (routings.value as any).filter((routing: Route) => routing.orderRoutingId !== orderRoutingId)
emitter.emit("initializeOrderRoutings")
}
routingsToUpdate.value.push({
...routing,
[fieldToUpdate]: value
})
// remove the updated routing from the archivedRoutings
routings.value = routings.value.filter((route: Route) => route.orderRoutingId !== routing.orderRoutingId)
}
</script>
13 changes: 2 additions & 11 deletions src/services/RoutingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fetchRoutingGroups = async (payload: any): Promise<any> => {
});
}

const fetchRoutingGroup = async (routingGroupId: string): Promise<any> => {
const fetchRoutingGroupInformation = async (routingGroupId: string): Promise<any> => {
return api({
url: `groups/${routingGroupId}`,
method: "GET"
Expand All @@ -31,14 +31,6 @@ const updateRoutingGroup = async (payload: any): Promise<any> => {
})
}

const fetchOrderRoutings = async (payload: any): Promise<any> => {
return api({
url: `groups/${payload.routingGroupId}/routings`,
method: "GET",
query: payload
});
}

const fetchOrderRouting = async (orderRoutingId: string): Promise<any> => {
return api({
url: `routings/${orderRoutingId}`,
Expand Down Expand Up @@ -150,9 +142,8 @@ export const OrderRoutingService = {
deleteRoutingFilter,
deleteRuleCondition,
fetchOrderRouting,
fetchOrderRoutings,
fetchRoutingFilters,
fetchRoutingGroup,
fetchRoutingGroupInformation,
fetchRoutingGroups,
fetchRoutingRules,
fetchRuleActions,
Expand Down
125 changes: 19 additions & 106 deletions src/store/modules/orderRouting/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { OrderRoutingService } from "@/services/RoutingService"
import { hasError, showToast, sortSequence } from "@/utils"
import * as types from './mutation-types'
import logger from "@/logger"
import { Route, RouteFilter } from "@/types"
import { RouteFilter } from "@/types"
import { DateTime } from "luxon"

const actions: ActionTree<OrderRoutingState, RootState> = {
async fetchOrderRoutingGroups({ commit }) {
Expand Down Expand Up @@ -35,7 +36,8 @@ const actions: ActionTree<OrderRoutingState, RootState> = {
async createRoutingGroup({ dispatch }, groupName) {
const payload = {
groupName,
productStoreId: "STORE"
productStoreId: "STORE",
createdDate: DateTime.now().toMillis()
}
try {
const resp = await OrderRoutingService.createRoutingGroup(payload)
Expand All @@ -50,40 +52,11 @@ const actions: ActionTree<OrderRoutingState, RootState> = {
}
},

async updateRoutingGroup({ commit, state }, payload) {
const current = JSON.parse(JSON.stringify(state.currentGroup))

const params = {
routingGroupId: payload.routingGroupId,
[payload.fieldToUpdate]: payload.value
}
async fetchCurrentRoutingGroup({ commit }, routingGroupId) {
let currentGroup = {} as any

try {
const resp = await OrderRoutingService.updateRoutingGroup(params);

if(!hasError(resp) && resp.data.routingGroupId) {
current[payload.fieldToUpdate] = payload.value
showToast("Rounting group information updated")
} else {
throw resp.data
}
} catch(err) {
logger.error(err);
}
commit(types.ORDER_ROUTING_CURRENT_GROUP_UPDATED, current)
},

async fetchCurrentRoutingGroup({ dispatch, state }, routingGroupId) {
const current = state.currentGroup
if(current.routingGroupId && current.routingGroupId === routingGroupId) {
dispatch("setCurrentRoutingGroup", current)
return;
}

let currentGroup = {}

try {
const resp = await OrderRoutingService.fetchRoutingGroup(routingGroupId);
const resp = await OrderRoutingService.fetchRoutingGroupInformation(routingGroupId);

if(!hasError(resp) && resp.data) {
currentGroup = resp.data
Expand All @@ -94,61 +67,34 @@ const actions: ActionTree<OrderRoutingState, RootState> = {
logger.error(err);
}

dispatch("setCurrentRoutingGroup", currentGroup)
},

async setCurrentRoutingGroup({ commit }, payload) {
commit(types.ORDER_ROUTING_CURRENT_GROUP_UPDATED, payload)
},

async fetchOrderRoutings({ commit }, routingGroupId) {
let orderRoutings = [] as any;
// filter groups on the basis of productStoreId
const payload = {
routingGroupId
if(currentGroup.routings.length) {
currentGroup.routings = sortSequence(currentGroup.routings)
}

try {
const resp = await OrderRoutingService.fetchOrderRoutings(payload);

if(!hasError(resp) && resp.data.length) {
orderRoutings = resp.data
} else {
throw resp.data
}
} catch(err) {
logger.error(err);
}

if(orderRoutings.length) {
orderRoutings = sortSequence(orderRoutings)
}

commit(types.ORDER_ROUTINGS_UPDATED, orderRoutings)
commit(types.ORDER_ROUTING_CURRENT_GROUP_UPDATED, currentGroup)
},

async createOrderRouting({ commit, state }, payload) {
let orderRoutings = JSON.parse(JSON.stringify(state.routes))
async createOrderRouting({ dispatch, state }, payload) {
const currentGroup = JSON.parse(JSON.stringify(state.currentGroup))
let orderRoutingId = ''

try {
const resp = await OrderRoutingService.createOrderRouting(payload)

if(!hasError(resp) && resp?.data.orderRoutingId) {
orderRoutingId = resp.data.orderRoutingId
orderRoutings.push({
currentGroup["routings"].push({
...payload,
orderRoutingId
})
showToast('New Order Routing Created')
showToast('New routing created')
}

// Sort the routings and update the state only on success
if(orderRoutings.length) {
orderRoutings = sortSequence(orderRoutings)
if(currentGroup["routings"].length) {
currentGroup["routings"] = sortSequence(currentGroup["routings"])
}

commit(types.ORDER_ROUTINGS_UPDATED, orderRoutings)
await dispatch("setCurrentGroup", currentGroup)
} catch(err) {
showToast("Failed to create order routing")
logger.error('err', err)
Expand All @@ -157,41 +103,8 @@ const actions: ActionTree<OrderRoutingState, RootState> = {
return orderRoutingId;
},

async updateOrderRouting({ commit, state }, payload) {
let orderRoutings = JSON.parse(JSON.stringify(state.routes))
const field: Route[keyof Route] = payload.fieldToUpdate
let orderRoutingId = ''

const params = {
orderRoutingId: payload.orderRoutingId,
[field]: payload.value // only one field can be updated once for orderRouting
}

try {
const resp = await OrderRoutingService.updateOrderRouting(params);

if(!hasError(resp) && resp.data.orderRoutingId) {
orderRoutingId = resp.data.orderRoutingId
orderRoutings.map((routing: Route) => {
if(routing.orderRoutingId === orderRoutingId) {
routing[field] = payload.value
}
})
showToast("Order routing information updated")
} else {
throw resp.data
}
} catch(err) {
showToast("Failed to update order routing")
logger.error(err);
}

if(orderRoutings.length) {
orderRoutings = sortSequence(orderRoutings)
}

commit(types.ORDER_ROUTINGS_UPDATED, orderRoutings)
return orderRoutingId;
async setCurrentGroup({ commit }, currentGroup) {
commit(types.ORDER_ROUTING_CURRENT_GROUP_UPDATED, currentGroup)
},

async fetchCurrentOrderRouting({ dispatch, state }, orderRoutingId) {
Expand Down
3 changes: 0 additions & 3 deletions src/store/modules/orderRouting/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ const getters: GetterTree<OrderRoutingState, RootState> = {
getRoutingGroups(state) {
return state.groups
},
getOrderRoutings(state) {
return state.routes
},
getRoutingRules(state) {
return JSON.parse(JSON.stringify(state.rules))
},
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ const jsonToCsv = (file: any, options: JsonToCsvOption = {}) => {
}

const sortSequence = (sequence: Array<Group | Route | Rule>) => {
// Currently, sorting is only performed on sequenceNum, so if two seqence have same seqNum then they will be arranged in FCFS basis
// TODO: Need to check that if for the above case we need to define the sorting on name as well, when seqNum is same
return sequence.sort((a, b) => a.sequenceNum - b.sequenceNum)
}

Expand Down
Loading
Loading