Skip to content

Commit

Permalink
Implemented: logic to fetch brokering job from moqui (hotwax#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
amansinghbais committed Oct 4, 2024
1 parent aa32a6b commit 8a107c0
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 28 deletions.
104 changes: 103 additions & 1 deletion src/services/JobService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { api } from '@/adapter';
import { api, client } from '@/adapter';
import store from '@/store'
import { DateTime } from 'luxon';

Expand Down Expand Up @@ -153,15 +153,117 @@ const scheduleJob = async (payload: any): Promise<any> => {
});
}

const fetchBrokeringJobId = async (payload: any): Promise<any> => {
return api({
url: "performFind",
method: "get",
params: payload
});
}

const fetchBrokeringJobSchedule = async (routingGroupId: string): Promise<any> => {
const omsRedirectionInfo = store.getters['user/getOmsRedirectionInfo'];

const url = omsRedirectionInfo.url
const baseURL = url.startsWith('http') ? url.includes('/rest/s1/order-routing') ? url : `${url}/rest/s1/order-routing/` : `https://${url}.hotwax.io/rest/s1/order-routing/`;

return client({
url: `groups/${routingGroupId}/schedule`,
method: "GET",
baseURL,
headers: {
"api_key": omsRedirectionInfo.token,
"Content-Type": "application/json"
}
});
}

const fetchBrokeringJobActiveRun = async (jobName: string): Promise<any> => {
const omsRedirectionInfo = store.getters['user/getOmsRedirectionInfo'];

const url = omsRedirectionInfo.url
const baseURL = url.startsWith('http') ? url.includes('/rest/s1/order-routing') ? url : `${url}/rest/s1/order-routing/` : `https://${url}.hotwax.io/rest/s1/order-routing/`;

return client({
url: `serviceJobRuns/${jobName}/activeJobRun`,
method: "GET",
baseURL,
headers: {
"api_key": omsRedirectionInfo.token,
"Content-Type": "application/json"
}
});
}

const scheduleMaargJob = async (payload: any): Promise<any> => {
const omsRedirectionInfo = store.getters['user/getOmsRedirectionInfo'];

const url = omsRedirectionInfo.url
const baseURL = url.startsWith('http') ? url.includes('/rest/s1/order-routing') ? url : `${url}/rest/s1/order-routing/` : `https://${url}.hotwax.io/rest/s1/order-routing/`;

return client({
url: `groups/${payload.routingGroupId}/schedule`,
method: "POST",
baseURL,
data: payload,
headers: {
"api_key": omsRedirectionInfo.token,
"Content-Type": "application/json"
}
});
}


const runMaargJobNow = async (routingGroupId: string): Promise<any> => {
const omsRedirectionInfo = store.getters['user/getOmsRedirectionInfo'];

const url = omsRedirectionInfo.url
const baseURL = url.startsWith('http') ? url.includes('/rest/s1/order-routing') ? url : `${url}/rest/s1/order-routing/` : `https://${url}.hotwax.io/rest/s1/order-routing/`;

return client({
url: `groups/${routingGroupId}/runNow`,
method: "POST",
baseURL,
headers: {
"api_key": omsRedirectionInfo.token,
"Content-Type": "application/json"
}
});
}

const fetchRoutingHistory = async (routingGroupId: string, params: any): Promise<any> => {
const omsRedirectionInfo = store.getters['user/getOmsRedirectionInfo'];

const url = omsRedirectionInfo.url
const baseURL = url.startsWith('http') ? url.includes('/rest/s1/order-routing') ? url : `${url}/rest/s1/order-routing/` : `https://${url}.hotwax.io/rest/s1/order-routing/`;

return client({
url: `groups/${routingGroupId}/routingRuns`,
method: "GET",
params,
baseURL,
headers: {
"api_key": omsRedirectionInfo.token,
"Content-Type": "application/json"
}
});
}

export const JobService = {
cancelJob,
fetchBackgroundJobs,
fetchBrokeringJobActiveRun,
fetchBrokeringJobId,
fetchBrokeringJobSchedule,
fetchJobLogs,
fetchJobs,
fetchJobInformation,
fetchRoutingHistory,
pollJobs,
prepareFetchJobsQuery,
prepareFetchLogsQuery,
scheduleJob,
scheduleMaargJob,
runMaargJobNow,
runJobNow
}
3 changes: 2 additions & 1 deletion src/store/modules/job/JobState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default interface JobState {
items: any[];
},
polling: boolean,
ctgryAndBrkrngJobs: any
ctgryAndBrkrngJobs: any,
brokeringJob: any
}
45 changes: 45 additions & 0 deletions src/store/modules/job/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,51 @@ const actions: ActionTree<JobState, RootState> = {
}
return jobs;
},

async fetchBrokeringJob ({ commit }) {
let resp = {} as any, jobId;
let jobInfo = {} as any;

try {
resp = await JobService.fetchBrokeringJobId({
"inputFields": {
"settingTypeEnumId": "JOB_BKR_ORD",
"productStoreId": this.state.user.currentEComStore?.productStoreId,
},
"fieldList": ["settingTypeEnumId", "settingValue", "fromDate", "productStoreId"],
"entityName": "ProductStoreSetting",
"viewSize": 1,
"noConditionFind": "Y"
})

if(!hasError(resp)) {
jobId = resp.data.docs?.length ? resp.data.docs[0]?.settingValue : ""

if(jobId) {
resp = await JobService.fetchBrokeringJobSchedule(jobId)

if(!hasError(resp) && resp.data?.schedule) {
jobInfo = resp.data.schedule
resp = await JobService.fetchBrokeringJobActiveRun(jobInfo.jobName)

if(!hasError(resp)) {
jobInfo["lastRunTime"] = resp.data.lastRunTime
jobInfo["routingGroupId"] = jobId
} else {
throw resp.data;
}
} else {
throw resp.data;
}
}
}
} catch(error: any) {
console.error(error);
}

commit(types.JOB_BROKERING_JOB_UPDATED, jobInfo)
},

clearCtgryAndBrkrngJobs({commit}) {
commit(types.JOB_CTGRY_AND_BRKRNG_UPDATED, { jobs: [] })
}
Expand Down
3 changes: 3 additions & 0 deletions src/store/modules/job/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const getters: GetterTree <JobState, RootState> = {
},
getCtgryAndBrkrngJob: (state) => (systemJobEnumId: string) => {
return state.ctgryAndBrkrngJobs[systemJobEnumId] ? state.ctgryAndBrkrngJobs[systemJobEnumId] : {};
},
getBrokeringJob(state) {
return state.brokeringJob;
}
}
export default getters;
3 changes: 2 additions & 1 deletion src/store/modules/job/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const jobModule: Module<JobState, RootState> = {
total: 0
},
polling: false,
ctgryAndBrkrngJobs: {}
ctgryAndBrkrngJobs: {},
brokeringJob: {}
},
getters,
actions,
Expand Down
3 changes: 2 additions & 1 deletion src/store/modules/job/mutation-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export const JOB_LIST_UPDATED = SN_JOB + '/LIST_UPDATED'
export const JOB_LOGS_UPDATED = SN_JOB + '/LOGS_UPDATED'
export const JOB_POLLING_UPDATED = SN_JOB + '/POLLING_UPDATED'
export const JOB_UPDATED = SN_JOB + '/UPDATED'
export const JOB_CTGRY_AND_BRKRNG_UPDATED = SN_JOB + '/CTGRY_AND_BRKRNG_UPDATED'
export const JOB_CTGRY_AND_BRKRNG_UPDATED = SN_JOB + '/CTGRY_AND_BRKRNG_UPDATED'
export const JOB_BROKERING_JOB_UPDATED = SN_JOB + '/BROKERING_JOB_UPDATED'
3 changes: 3 additions & 0 deletions src/store/modules/job/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ const mutations: MutationTree <JobState> = {
[types.JOB_CTGRY_AND_BRKRNG_UPDATED] (state, payload) {
state.ctgryAndBrkrngJobs = payload;
},
[types.JOB_BROKERING_JOB_UPDATED] (state, payload) {
state.brokeringJob = payload;
},
}
export default mutations;
3 changes: 3 additions & 0 deletions src/store/modules/user/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const getters: GetterTree <UserState, RootState> = {
},
getCurrentEComStore(state) {
return state.currentEComStore
},
getOmsRedirectionInfo(state) {
return state.omsRedirectionInfo
}
}
export default getters;
14 changes: 8 additions & 6 deletions src/views/catalog-product-details.vue
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@
</ion-label>
</ion-item>

<ion-item v-if="Object.keys(getCtgryAndBrkrngJob('JOB_BKR_ORD')).length " detail button @click="openJobActionsPopover($event, getCtgryAndBrkrngJob('JOB_BKR_ORD'), 'Order brokering')">
<ion-item v-if="Object.keys(brokeringJob).length" detail button @click="openJobActionsPopover($event, brokeringJob, 'Order brokering', true)">
<ion-label class="ion-text-wrap">
<h3>{{ $t('Order brokering') }}</h3>
<p>{{ getCtgryAndBrkrngJob('JOB_BKR_ORD').lastRunTime && timeTillJob(getCtgryAndBrkrngJob('JOB_BKR_ORD').lastRunTime) }}</p>
<p>{{ brokeringJob.lastRunTime && timeTillJob(brokeringJob.lastRunTime) }}</p>
</ion-label>
<ion-label slot="end">
<p>{{ getCtgryAndBrkrngJob('JOB_BKR_ORD').runTime ? timeTillJob(getCtgryAndBrkrngJob('JOB_BKR_ORD').runTime) : $t('disabled')}}</p>
<p>{{ (brokeringJob.paused === "N" && brokeringJob.nextExecutionDateTime) ? timeTillJob(brokeringJob.nextExecutionDateTime) : $t('disabled')}}</p>
</ion-label>
</ion-item>

Expand Down Expand Up @@ -461,7 +461,8 @@ export default defineComponent({
product: "product/getCurrentCatalogProduct",
currentEComStore: 'user/getCurrentEComStore',
getCtgryAndBrkrngJob: "job/getCtgryAndBrkrngJob",
getInventoryConfig: "util/getInventoryConfig"
getInventoryConfig: "util/getInventoryConfig",
brokeringJob: "job/getBrokeringJob"
})
},
async ionViewWillEnter() {
Expand Down Expand Up @@ -550,15 +551,16 @@ export default defineComponent({
},
async getCtgryAndBrkrngJobs() {
const systemJobEnumIds = JSON.parse(process.env.VUE_APP_CTGRY_AND_BRKRNG_JOB)
this.store.dispatch('job/fetchBrokeringJob')
this.store.dispatch('job/fetchCtgryAndBrkrngJobs', { systemJobEnumIds }).then(() => {
this.isCtgryAndBrkrngJobsLoaded = true
})
},
async openJobActionsPopover(event: Event, job: any, jobTitle: string) {
async openJobActionsPopover(event: Event, job: any, jobTitle: string, isMaargJob = false) {
job.jobTitle = jobTitle
const popover = await popoverController.create({
component: JobActionsPopover,
componentProps: { job },
componentProps: { job, isMaargJob },
event,
showBackdrop: false
});
Expand Down
58 changes: 48 additions & 10 deletions src/views/job-actions-popover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
{{ $t("Schedule in every 15 minutes") }}
</ion-item>
</ion-list>
<ion-list v-else-if="job.paused === 'Y' && job.nextExecutionDateTime">
<ion-item lines="none" button @click="schdlInEvry15Mins()">
<ion-icon slot="start" :icon="timerOutline" />
{{ $t("schedule") }}
</ion-item>
</ion-list>
<ion-list v-else>
<ion-item lines="none" button @click="runNow()">
<ion-item lines="none" button @click="runNow(job)">
<ion-icon slot="start" :icon="flashOutline" />
{{ $t("Run now") }}
</ion-item>
Expand Down Expand Up @@ -49,23 +55,42 @@ import { translate } from "@/i18n";
export default defineComponent({
name: "JobActionsPopover",
props: ["job"],
props: ["job", "isMaargJob"],
components: {
IonContent,
IonIcon,
IonItem,
IonList
},
mounted() {
console.log(this.job);
},
methods: {
closeJobActionsPopover() {
popoverController.dismiss({ dismissed: true });
},
async runNow() {
async runNow(job: any) {
let resp;
try {
const resp = await JobService.runJobNow(this.job)
if(this.isMaargJob) {
if(!job.jobName) {
resp = await JobService.scheduleMaargJob({ routingGroupId: job.routingGroupId, paused: 'Y' })
if(hasError(resp)) {
throw resp.data;
}
// Updating jobName as if the user again clicks the runNow button then in that we don't want to call the scheduleBrokering service
job.jobName = resp.data.jobName
}
resp = await JobService.runMaargJobNow(job.routingGroupId)
} else {
resp = await JobService.runJobNow(job)
}
if (!hasError(resp)) {
showToast(translate('Service has been scheduled'))
await this.store.dispatch('job/fetchCtgryAndBrkrngJobs')
await Promise.allSettled([this.store.dispatch('job/fetchBrokeringJob'), this.store.dispatch('job/fetchCtgryAndBrkrngJobs')])
} else {
showToast(translate('Something went wrong'))
}
Expand All @@ -79,16 +104,24 @@ export default defineComponent({
async openJobHistoryModal() {
const jobHistoryModal = await modalController.create({
component: JobHistoryModal,
componentProps: { job: this.job }
componentProps: { job: this.job, isMaargJob: this.isMaargJob }
});
return await jobHistoryModal.present();
},
async cancelJob() {
let resp;
console.log(this.isMaargJob);
try {
const resp = await JobService.cancelJob(this.job.jobId)
if(this.isMaargJob) {
resp = await JobService.scheduleMaargJob({ routingGroupId: this.job.routingGroupId, paused: 'Y' })
} else {
resp = await JobService.cancelJob(this.job.jobId)
}
if (!hasError(resp)) {
showToast(translate('Job cancelled successfully'))
await this.store.dispatch('job/fetchCtgryAndBrkrngJobs')
await Promise.allSettled([this.store.dispatch('job/fetchBrokeringJob'), this.store.dispatch('job/fetchCtgryAndBrkrngJobs')])
} else {
showToast(translate('Something went wrong, could not cancel the job'))
}
Expand Down Expand Up @@ -117,11 +150,16 @@ export default defineComponent({
await alert.present();
},
async schdlInEvry15Mins() {
let resp;
try {
const resp = await JobService.scheduleJob({ job: this.job, frequency: 'EVERY_15_MIN', runTime: '' })
if(this.isMaargJob) {
resp = await JobService.scheduleMaargJob({ routingGroupId: this.job.routingGroupId, paused: 'N' })
} else {
resp = await JobService.scheduleJob({ job: this.job, frequency: 'EVERY_15_MIN', runTime: '' })
}
if (!hasError(resp)) {
showToast(translate('Service has been scheduled'));
await this.store.dispatch('job/fetchCtgryAndBrkrngJobs')
await Promise.allSettled([this.store.dispatch('job/fetchBrokeringJob'), this.store.dispatch('job/fetchCtgryAndBrkrngJobs')])
} else {
showToast(translate('Something went wrong'))
}
Expand Down
Loading

0 comments on commit 8a107c0

Please sign in to comment.