diff --git a/src/locales/en.json b/src/locales/en.json index de79eaf..22b9cda 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -20,6 +20,7 @@ "auto cancel days": "auto cancel days", "Brokering": "Brokering", "Brokering Runs": "Brokering Runs", + "Brokering run cloned": "Brokering run cloned", "Brokering run created": "Brokering run created", "Brokering safety stock": "Brokering safety stock", "Built:": "Built: ", @@ -44,6 +45,8 @@ "Enter a valid value": "Enter a valid value", "Error getting user profile": "Error getting user profile", "Execution history": "Execution history", + "Failed to clone brokering run": "Failed to clone brokering run", + "Failed to clone rule": "Failed to clone rule", "Failed to clone the rule": "Failed to clone the rule", "Failed to create brokering run": "Failed to create brokering run", "Failed to create inventory rule": "Failed to create inventory rule", diff --git a/src/services/RoutingService.ts b/src/services/RoutingService.ts index a8f426e..695581d 100644 --- a/src/services/RoutingService.ts +++ b/src/services/RoutingService.ts @@ -54,6 +54,14 @@ const updateRoutingGroup = async (payload: any): Promise => { }) } +const cloneGroup = async (payload: any): Promise => { + return api({ + url: `groups/${payload.routingGroupId}/clone`, + method: "POST", + data: payload + }) +} + const fetchOrderRouting = async (orderRoutingId: string): Promise => { return api({ url: `routings/${orderRoutingId}`, @@ -77,6 +85,14 @@ const createOrderRouting = async (payload: any): Promise => { }) } +const cloneRouting = async (payload: any): Promise => { + return await api({ + url: `routings/${payload.orderRoutingId}/clone`, + method: "POST", + data: payload + }) +} + const updateRouting = async (payload: any): Promise => { return api({ url: `routings/${payload.orderRoutingId}`, @@ -116,6 +132,14 @@ const fetchRule = async (routingRuleId: string): Promise => { }); } +const cloneRule = async (payload: any): Promise => { + return await api({ + url: `rules/${payload.routingRuleId}/clone`, + method: "POST", + data: payload + }) +} + const updateRule = async (payload: any): Promise => { return api({ url: `rules/${payload.routingRuleId}`, @@ -140,7 +164,10 @@ const runNow = async (routingGroupId: string): Promise => { } export const OrderRoutingService = { + cloneGroup, createOrderRouting, + cloneRouting, + cloneRule, createRoutingGroup, createRoutingRule, deleteRoutingFilter, diff --git a/src/store/modules/orderRouting/actions.ts b/src/store/modules/orderRouting/actions.ts index ffd93ea..6638e1b 100644 --- a/src/store/modules/orderRouting/actions.ts +++ b/src/store/modules/orderRouting/actions.ts @@ -163,6 +163,32 @@ const actions: ActionTree = { return orderRoutingId; }, + async cloneOrderRouting({ dispatch }, payload) { + let orderRoutingId = "" + + try { + const resp = await OrderRoutingService.cloneRouting({ + orderRoutingId: payload.orderRoutingId, + newRoutingName: `${payload.orderRoutingName} copy`, + newRoutingGroupId: payload.routingGroupId // group in which this routing needs to be cloned + }) + + if(!hasError(resp) && resp?.data.newOrderRoutingId) { + orderRoutingId = resp.data.newOrderRoutingId + showToast(translate("Routing cloned")) + + // TODO: check if we can get all the information in response so we do not need to make an api call here + // Fetching the group information again as we do not have the complete information for the cloned route + await dispatch("fetchCurrentRoutingGroup", payload.routingGroupId) + } + } catch(err) { + showToast(translate("Failed to clone order routing")) + logger.error(err) + } + + return orderRoutingId; + }, + async fetchCurrentOrderRouting({ dispatch }, orderRoutingId) { let currentRoute = {} as any diff --git a/src/views/BrokeringQuery.vue b/src/views/BrokeringQuery.vue index beac242..53a55bb 100644 --- a/src/views/BrokeringQuery.vue +++ b/src/views/BrokeringQuery.vue @@ -154,10 +154,10 @@ {{ isRuleNameUpdating ? translate("Save") : translate("Rename") }} - + @@ -946,69 +946,24 @@ function updateRuleName(routingRuleId: string) { } async function cloneRule() { - emitter.emit("presentLoader", { message: `Cloning rule ${selectedRoutingRule.value.ruleName}`, backdropDismiss: false }) - - const payload = { - routingRuleId: "", - orderRoutingId: props.orderRoutingId, - ruleName: selectedRoutingRule.value.ruleName + ' copy', - statusId: "RULE_DRAFT", // by default when cloning the rule the new rule will be in draft status - sequenceNum: inventoryRules.value.length && inventoryRules.value[inventoryRules.value.length - 1].sequenceNum >= 0 ? inventoryRules.value[inventoryRules.value.length - 1].sequenceNum + 5 : 0, // added check for `>= 0` as sequenceNum can be 0, that will result in again setting the new route seqNum to 0, - assignmentEnumId: selectedRoutingRule.value.assignmentEnumId, - createdDate: DateTime.now().toMillis() - } - - const routingRuleId = await store.dispatch("orderRouting/createRoutingRule", payload) - - if(!routingRuleId) { - showToast(translate("Failed to clone the rule")) - emitter.emit("dismissLoader") - return; - } - - const sortOptions = rulesInformation.value[selectedRoutingRule.value.routingRuleId]["inventoryFilters"]?.["ENTCT_SORT_BY"] ? rulesInformation.value[selectedRoutingRule.value.routingRuleId]["inventoryFilters"]["ENTCT_SORT_BY"] : {} - const filterOptions = rulesInformation.value[selectedRoutingRule.value.routingRuleId]["inventoryFilters"]?.["ENTCT_FILTER"] ? rulesInformation.value[selectedRoutingRule.value.routingRuleId]["inventoryFilters"]["ENTCT_FILTER"] : {} - const actionOptions = rulesInformation.value[selectedRoutingRule.value.routingRuleId]["actions"] ? rulesInformation.value[selectedRoutingRule.value.routingRuleId]["actions"] : {} - - let inventoryFilters = [] as any, actions = [] as any + emitter.emit("presentLoader", { message: `Cloning ${selectedRoutingRule.value.ruleName}`, backdropDismiss: false }) - Object.values(sortOptions).map((option: any) => { - inventoryFilters.push({ - createdDate: DateTime.now().toMillis(), - conditionTypeEnumId: option.conditionTypeEnumId, - fieldName: option.fieldName, - sequenceNum: option.sequenceNum + try { + const resp = await OrderRoutingService.cloneRule({ + routingRuleId: selectedRoutingRule.value.routingRuleId, + newOrderRoutingId: props.orderRoutingId, + newRuleName: `${selectedRoutingRule.value.ruleName} copy` }) - }) - - Object.values(filterOptions).map((option: any) => { - inventoryFilters.push({ - createdDate: DateTime.now().toMillis(), - conditionTypeEnumId: option.conditionTypeEnumId, - fieldName: option.fieldName, - fieldValue: option.fieldValue, - operator: option.operator, - sequenceNum: option.sequenceNum - }) - }) - Object.values(actionOptions).map((option: any) => { - actions.push({ - actionTypeEnumId: option.actionTypeEnumId, - actionValue: option.actionValue, - createdDate: DateTime.now().toMillis(), - }) - }) - - await store.dispatch("orderRouting/updateRule", { - routingRuleId, - orderRoutingId: props.orderRoutingId, - inventoryFilters, - actions - }) + if(hasError(resp) || !resp.data.newRoutingRuleId) { + throw resp.data + } - inventoryRules.value = JSON.parse(JSON.stringify(currentRouting.value["rules"])) - fetchRuleInformation(routingRuleId) + await store.dispatch("orderRouting/fetchCurrentOrderRouting", props.orderRoutingId) + } catch (err) { + logger.error(err) + showToast(translate("Failed to clone rule")) + } emitter.emit("dismissLoader") } diff --git a/src/views/BrokeringRoute.vue b/src/views/BrokeringRoute.vue index c1574c8..c12553c 100644 --- a/src/views/BrokeringRoute.vue +++ b/src/views/BrokeringRoute.vue @@ -29,10 +29,10 @@ {{ translate("Save") }} - +
@@ -741,136 +741,39 @@ async function editGroupDescription() { descRef.value.$el.setFocus(); } -async function cloneRouting(routing: any) { - emitter.emit("presentLoader", { message: "Cloning route", backdropDismiss: false }) - - // payload for creating the cloned copy of current routing +async function cloneGroup() { const payload = { - orderRoutingId: "", - routingGroupId: props.routingGroupId, - statusId: "ROUTING_DRAFT", // when cloning a routing, the new routing will be in draft status - routingName: routing.routingName + " copy", - sequenceNum: orderRoutings.value.length && orderRoutings.value[orderRoutings.value.length - 1].sequenceNum >= 0 ? orderRoutings.value[orderRoutings.value.length - 1].sequenceNum + 5 : 0, // added check for `>= 0` as sequenceNum can be 0 which will result in again setting the new route seqNum to 0, also considering archivedRouting when calculating new seqNum - description: "", - createdDate: DateTime.now().toMillis() - } - - const orderRoutingId = await store.dispatch("orderRouting/createOrderRouting", payload) - - // No need to perform any action if we do not get routingId in return after routing creation - if(!orderRoutingId) { - showToast(translate("Failed to clone order routing")) - emitter.emit("dismissLoader") - return; + routingGroupId: currentRoutingGroup.value.routingGroupId, + newGroupName: `${currentRoutingGroup.value.groupName} copy` } - - let parentRouting = {} as any; - - // Fetch rules and order filters for the parent routing, as we need to create copy of the rules and filters try { - const resp = await OrderRoutingService.fetchOrderRouting(routing.orderRoutingId); + const resp = await OrderRoutingService.cloneGroup(payload) - if(!hasError(resp) && resp.data) { - parentRouting = resp.data + if(!hasError(resp)) { + // Not fetching the groups list as after cloning as we do not need any information from the newly cloned group + showToast(translate("Brokering run cloned")) } else { throw resp.data } } catch(err) { - showToast(translate("Failed to clone the routing filters and rules")) - logger.error(err); - emitter.emit("dismissLoader"); - return; - } - - if(parentRouting?.rules?.length) { - const parentRoutingRules = await Promise.all(parentRouting.rules.map((rule: any) => OrderRoutingService.fetchRule(rule.routingRuleId))) - parentRouting["rulesInformation"] = parentRoutingRules.reduce((rulesInformation: any, rule: any) => { - rulesInformation[rule.data.ruleName] = rule.data - return rulesInformation - }, {}) - } - - // Payload for applying routing filters and rules in the cloned routing - const routingPayload = { - orderRoutingId, - routingGroupId: parentRouting.routingGroupId, - orderFilters: parentRouting.orderFilters?.length ? parentRouting.orderFilters.reduce((filters: any, filter: any) => { - filters.push({ - conditionTypeEnumId: filter.conditionTypeEnumId, - fieldName: filter.fieldName, - fieldValue: filter.fieldValue, - operator: filter.operator, - sequenceNum: filter.sequenceNum, - createdDate: DateTime.now().toMillis(), - orderRoutingId - }) - return filters - }, []) : [], - rules: parentRouting.rules?.length ? parentRouting.rules.reduce((rules: any, rule: any) => { - rules.push({ - assignmentEnumId: rule.assignmentEnumId, - createdDate: DateTime.now().toMillis(), - ruleName: rule.ruleName, - sequenceNum: rule.sequenceNum, - statusId: "RULE_DRAFT", - orderRoutingId - }) - return rules - }, []) : [] - } - - if(!routingPayload.orderFilters.length && !routingPayload.rules.length) { - emitter.emit("dismissLoader") - return; + showToast(translate("Failed to clone brokering run")) + logger.error(err) } +} - await store.dispatch("orderRouting/updateRouting", routingPayload) - - let clonedRoutingRules = {} as any; - - // As we do not have routingRuleId's for the rules created inside the cloned routing, hence fetching the rule ids - if(Object.keys(parentRouting["rulesInformation"])?.length) { - try { - const resp = await OrderRoutingService.fetchOrderRouting(orderRoutingId); - if(!hasError(resp) && resp.data?.rules?.length) { - clonedRoutingRules = resp.data.rules.reduce((rules: any, rule: any) => { - rules[rule.ruleName] = rule.routingRuleId - return rules - }, {}) - } else { - throw resp.data - } - } catch(err) { - logger.error(err) - } - } +async function cloneRouting(routing: any) { + emitter.emit("presentLoader", { message: "Cloning route", backdropDismiss: false }) - if(Object.keys(clonedRoutingRules).length) { - await Promise.all(Object.values(parentRouting["rulesInformation"]).map((rule: any) => { - store.dispatch("orderRouting/updateRule", { - routingRuleId: clonedRoutingRules[rule.ruleName], - orderRoutingId, - inventoryFilters: rule.inventoryFilters?.length ? rule.inventoryFilters.map((filter: any) => ({ - createdDate: DateTime.now().toMillis(), - conditionTypeEnumId: filter.conditionTypeEnumId, - fieldName: filter.fieldName, - fieldValue: filter.fieldValue, - operator: filter.operator, - sequenceNum: filter.sequenceNum, - })) : [], - actions: rule.actions?.length ? rule.actions.map((filter: any) => ({ - actionTypeEnumId: filter.actionTypeEnumId, - actionValue: filter.actionValue, - createdDate: DateTime.now().toMillis(), - })) : [] - }) - })) - } + const orderRoutingId = await store.dispatch("orderRouting/cloneOrderRouting", { + orderRoutingId: routing.orderRoutingId, + orderRoutingName: routing.routingName, + routingGroupId: props.routingGroupId + }) - // update the routing order for reordering and the cloned updated routings again + // Updating the order routings as we have created a new route that needs to be added on the UI if(orderRoutingId) { - orderRoutings.value = JSON.parse(JSON.stringify(currentRoutingGroup.value))["routings"] - initializeOrderRoutings(); + orderRoutings.value = currentRoutingGroup.value["routings"] ? JSON.parse(JSON.stringify(currentRoutingGroup.value))["routings"] : [] + initializeOrderRoutings() } emitter.emit("dismissLoader")