Skip to content

Commit

Permalink
Improved: migrated the logic to schedule and update job in the compon…
Browse files Browse the repository at this point in the history
…ent itself from action (#356)
  • Loading branch information
amansinghbais committed Jul 4, 2024
1 parent 3476118 commit 8b406a7
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 104 deletions.
89 changes: 1 addition & 88 deletions src/store/modules/channel/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import RootState from '@/store/RootState'
import * as types from './mutation-types'
import ChannelState from './ChannelState'
import { ChannelService } from '@/services/ChannelService'
import { hasError, showToast } from '@/utils'
import { hasError } from '@/utils'
import logger from '@/logger'
import store from "@/store"
import { translate } from '@/i18n'
import { DateTime } from 'luxon'

const actions: ActionTree<ChannelState, RootState> = {
Expand Down Expand Up @@ -205,92 +204,6 @@ const actions: ActionTree<ChannelState, RootState> = {
return temporalExpressions;
},

async scheduleService({ dispatch }, job) {
let resp;

const payload = {
'JOB_NAME': job.jobName,
'SERVICE_NAME': job.serviceName,
'SERVICE_COUNT': '0',
'SERVICE_TEMP_EXPR': job.jobStatus,
'SERVICE_RUN_AS_SYSTEM':'Y',
'jobFields': {
'productStoreId': store.state.user.currentEComStore.productStoreId,
'systemJobEnumId': job.systemJobEnumId,
'tempExprId': job.jobStatus, // Need to remove this as we are passing frequency in SERVICE_TEMP_EXPR, currently kept it for backward compatibility
'maxRecurrenceCount': '-1',
'parentJobId': job.parentJobId,
'runAsUser': 'system', //default system, but empty in run now. TODO Need to remove this as we are using SERVICE_RUN_AS_SYSTEM, currently kept it for backward compatibility
'recurrenceTimeZone': store.state.user.current.timeZone,
'createdByUserLogin': store.state.user.current.username,
'lastModifiedByUserLogin': store.state.user.current.username,
},
'statusId': "SERVICE_PENDING",
'systemJobEnumId': job.systemJobEnumId
} as any

Object.keys(job.runtimeData).map((key: any) => {
if(key !== "productStoreId" && key !== "shopifyConfigId" && key !== "shopId") {
payload[key] = job.runtimeData[key];
}
})

const jobRunTimeDataKeys = job?.runtimeData ? Object.keys(job?.runtimeData) : [];
if (jobRunTimeDataKeys.includes('shopifyConfigId') || jobRunTimeDataKeys.includes('shopId')) {
jobRunTimeDataKeys.includes('shopifyConfigId') && (payload['shopifyConfigId'] = job.shopifyConfigId);
jobRunTimeDataKeys.includes('shopId') && (payload['shopId'] = job.shopId);
payload['jobFields']['shopId'] = job.shopId;
}

// checking if the runtimeData has productStoreId, and if present then adding it on root level
job?.runtimeData?.productStoreId?.length >= 0 && (payload['productStoreId'] = job.productStoreId)
job?.priority && (payload['SERVICE_PRIORITY'] = job.priority.toString())
job?.runTime && (payload['SERVICE_TIME'] = job.runTime.toString())
job?.sinceId && (payload['sinceId'] = job.sinceId)

try {
resp = await ChannelService.scheduleJob({ ...payload });
if (resp.status == 200 && !hasError(resp)) {
showToast(translate("Service has been scheduled."));
await dispatch("fetchJobs");
} else {
throw resp.data;
}
} catch (err) {
showToast(translate("Failed to schedule service."))
logger.error(err)
}
},

async updateJob ({ dispatch }, job) {
const payload = {
'jobId': job.jobId,
'systemJobEnumId': job.systemJobEnumId,
'recurrenceTimeZone': store.state.user.current.userTimeZone,
'tempExprId': job.jobStatus,
'statusId': "SERVICE_PENDING",
'runTimeEpoch': '', // when updating a job clearning the epoch time, as job honors epoch time as runTime and the new job created also uses epoch time as runTime
'lastModifiedByUserLogin': store.state.user.current.username
} as any

job?.runTime && (payload['runTime'] = job.runTime)
job?.sinceId && (payload['sinceId'] = job.sinceId)
job?.jobName && (payload['jobName'] = job.jobName)

try {
const resp = await ChannelService.updateJob(payload)
if (!hasError(resp)) {
showToast(translate("Service has been scheduled."))
await dispatch("fetchJobs");
} else {
throw resp.data;
}
} catch(error: any) {
showToast(translate("Failed to schedule service."))
logger.error(error)
}
},

async clearChannelState({ commit }) {
commit(types.CHANNEL_INVENTORY_CHANNELS_UPDATED, [])
},
Expand Down
119 changes: 103 additions & 16 deletions src/views/InventoryChannels.vue
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ import EditGroupModal from '@/components/EditGroupModal.vue';
import emitter from '@/event-bus';
import { DateTime } from 'luxon';
import CustomFrequencyModal from "@/components/CustomFrequencyModal.vue";
import { hasJobDataError, showToast } from "@/utils";
import { hasError, hasJobDataError, showToast } from "@/utils";
import { ChannelService } from '@/services/ChannelService';
import logger from "@/logger";
const store = useStore();
Expand Down Expand Up @@ -379,36 +381,121 @@ async function saveChanges(job: any) {
role: "cancel"
}, {
text: translate("Save"),
handler: () => {
handler: async() => {
if(isCustomRunTime(job.runTimeValue) && isRuntimePassed(job)) {
showToast(translate("Job runtime has passed. Please refresh to get the latest job data in order to perform any action."))
return;
}
updateJob(job);
// return if job has missing data or error
if(hasJobDataError(job)) return;
job['jobStatus'] = job.tempExprId !== 'SERVICE_DRAFT' ? job.tempExprId : 'HOURLY';
// Handling the case for 'Now'. Sending the now value will fail the API as by the time
// the job is ran, the given 'now' time would have passed. Hence, passing empty 'run time'
job.runTime = job.runTimeValue != 0 ? (!isCustomRunTime(job.runTimeValue) ? DateTime.now().toMillis() + job.runTimeValue : job.runTimeValue) : ''
if (job?.statusId === 'SERVICE_DRAFT') {
await scheduleService(job)
} else if (job?.statusId === 'SERVICE_PENDING') {
await updateJob(job);
await store.dispatch('channel/updateJob', job)
}
}
}]
});
return alert.present();
}
async function updateJob(job: any) {
// return if job has missing data or error
if(hasJobDataError(job)) return;
async function scheduleService(job: any) {
let resp;
const payload = {
'JOB_NAME': job.jobName,
'SERVICE_NAME': job.serviceName,
'SERVICE_COUNT': '0',
'SERVICE_TEMP_EXPR': job.jobStatus,
'SERVICE_RUN_AS_SYSTEM':'Y',
'jobFields': {
'productStoreId': store.state.user.currentEComStore.productStoreId,
'systemJobEnumId': job.systemJobEnumId,
'tempExprId': job.jobStatus, // Need to remove this as we are passing frequency in SERVICE_TEMP_EXPR, currently kept it for backward compatibility
'maxRecurrenceCount': '-1',
'parentJobId': job.parentJobId,
'runAsUser': 'system', //default system, but empty in run now. TODO Need to remove this as we are using SERVICE_RUN_AS_SYSTEM, currently kept it for backward compatibility
'recurrenceTimeZone': store.state.user.current.timeZone,
'createdByUserLogin': store.state.user.current.username,
'lastModifiedByUserLogin': store.state.user.current.username,
},
'statusId': "SERVICE_PENDING",
'systemJobEnumId': job.systemJobEnumId
} as any
Object.keys(job.runtimeData).map((key: any) => {
if(key !== "productStoreId" && key !== "shopifyConfigId" && key !== "shopId") {
payload[key] = job.runtimeData[key];
}
})
job['jobStatus'] = job.tempExprId !== 'SERVICE_DRAFT' ? job.tempExprId : 'HOURLY';
const jobRunTimeDataKeys = job?.runtimeData ? Object.keys(job?.runtimeData) : [];
if (jobRunTimeDataKeys.includes('shopifyConfigId') || jobRunTimeDataKeys.includes('shopId')) {
jobRunTimeDataKeys.includes('shopifyConfigId') && (payload['shopifyConfigId'] = job.shopifyConfigId);
jobRunTimeDataKeys.includes('shopId') && (payload['shopId'] = job.shopId);
payload['jobFields']['shopId'] = job.shopId;
}
// Handling the case for 'Now'. Sending the now value will fail the API as by the time
// the job is ran, the given 'now' time would have passed. Hence, passing empty 'run time'
job.runTime = job.runTimeValue != 0 ? (!isCustomRunTime(job.runTimeValue) ? DateTime.now().toMillis() + job.runTimeValue : job.runTimeValue) : ''
// checking if the runtimeData has productStoreId, and if present then adding it on root level
job?.runtimeData?.productStoreId?.length >= 0 && (payload['productStoreId'] = job.productStoreId)
job?.priority && (payload['SERVICE_PRIORITY'] = job.priority.toString())
job?.runTime && (payload['SERVICE_TIME'] = job.runTime.toString())
job?.sinceId && (payload['sinceId'] = job.sinceId)
try {
resp = await ChannelService.scheduleJob({ ...payload });
if (resp.status == 200 && !hasError(resp)) {
showToast(translate("Service has been scheduled."));
await store.dispatch("channel/fetchJobs");
generateFrequencyOptions();
generateRuntimeOptions();
} else {
throw resp.data;
}
} catch (err) {
showToast(translate("Failed to schedule service."))
logger.error(err)
}
}
if (job?.statusId === 'SERVICE_DRAFT') {
await store.dispatch('channel/scheduleService', job)
} else if (job?.statusId === 'SERVICE_PENDING') {
await store.dispatch('channel/updateJob', job)
async function updateJob(job: any) {
const payload = {
'jobId': job.jobId,
'systemJobEnumId': job.systemJobEnumId,
'recurrenceTimeZone': store.state.user.current.userTimeZone,
'tempExprId': job.jobStatus,
'statusId': "SERVICE_PENDING",
'runTimeEpoch': '', // when updating a job clearning the epoch time, as job honors epoch time as runTime and the new job created also uses epoch time as runTime
'lastModifiedByUserLogin': store.state.user.current.username
} as any
job?.runTime && (payload['runTime'] = job.runTime)
job?.sinceId && (payload['sinceId'] = job.sinceId)
job?.jobName && (payload['jobName'] = job.jobName)
try {
const resp = await ChannelService.updateJob(payload)
if (!hasError(resp)) {
showToast(translate("Service has been scheduled."))
await store.dispatch("channel/fetchJobs");
generateFrequencyOptions();
generateRuntimeOptions();
} else {
throw resp.data;
}
} catch(error: any) {
showToast(translate("Failed to schedule service."))
logger.error(error)
}
generateFrequencyOptions()
generateRuntimeOptions()
}
function isRuntimePassed(currentJob: any) {
Expand Down

0 comments on commit 8b406a7

Please sign in to comment.