Skip to content

Commit

Permalink
Merge pull request #236 from hotwax/#2te9p7v
Browse files Browse the repository at this point in the history
Fixed: shop should be honored while fetching jobs (#2te9p7v)
  • Loading branch information
adityasharma7 authored Sep 8, 2022
2 parents 1c95c95 + 0e19924 commit 77ea681
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 73 deletions.
166 changes: 98 additions & 68 deletions src/store/modules/job/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ const actions: ActionTree<JobState, RootState> = {
"inputFields": {
"statusId": ["SERVICE_CANCELLED", "SERVICE_CRASHED", "SERVICE_FAILED", "SERVICE_FINISHED"],
"statusId_op": "in",
"systemJobEnumId_op": "not-empty"
"systemJobEnumId_op": "not-empty",
"shopId_fld0_value": store.state.user.currentShopifyConfig?.shopId,
"shopId_fld0_grp": "1",
"shopId_fld0_op": "equals",
"shopId_fld1_grp": "2",
"shopId_fld1_op": "empty"
} as any,
"fieldList": [ "systemJobEnumId", "runTime", "tempExprId", "parentJobId", "serviceName", "jobId", "jobName", "statusId", "cancelDateTime", "finishDateTime", "startDateTime" ],
"noConditionFind": "Y",
Expand Down Expand Up @@ -113,12 +118,13 @@ const actions: ActionTree<JobState, RootState> = {
const params = {
"inputFields": {
"systemJobEnumId_op": "not-empty",
"statusId_fld0_value": "SERVICE_RUNNING",
"statusId_fld0_op": "equals",
"statusId_fld0_grp": "1",
"statusId_fld1_value": "SERVICE_QUEUED",
"statusId_fld1_op": "equals",
"statusId_fld1_grp": "2",
"statusId": ["SERVICE_RUNNING", "SERVICE_QUEUED"],
"statusId_op": "in",
"shopId_fld0_value": store.state.user.currentShopifyConfig?.shopId,
"shopId_fld0_grp": "1",
"shopId_fld0_op": "equals",
"shopId_fld1_grp": "2",
"shopId_fld1_op": "empty"
} as any,
"fieldList": [ "systemJobEnumId", "runTime", "tempExprId", "parentJobId", "serviceName", "jobId", "jobName", "statusId" ],
"noConditionFind": "Y",
Expand Down Expand Up @@ -337,17 +343,22 @@ const actions: ActionTree<JobState, RootState> = {
},

async fetchJobs ({ state, commit, dispatch }, payload) {
const params = {
// We are fetching the draft and pending jobs separately as we could only have single OR condition in query
// Earlier we were having ORing on status only, but now we want to add condition for shopId as well
// Instead of complicating the query, we have made 2 separate call with status conditions and merged them


// Fetching the draft jobs first
const fetchJobRequests = [];
let params = {
"inputFields": {
"statusId_fld0_value": "SERVICE_DRAFT",
"statusId_fld0_op": "equals",
"statusId_fld0_grp": "1",
"statusId_fld1_value": "SERVICE_PENDING",
"statusId_fld1_op": "equals",
"statusId_fld1_grp": "2",
"productStoreId_fld0_value": this.state.user.currentEComStore.productStoreId,
"productStoreId_fld0_op": "equals",
"productStoreId_fld0_grp": "2",
"statusId_value": "SERVICE_DRAFT",
"statusId_op": "equals",
"shopId_fld0_value": this.state.user.currentShopifyConfig?.shopId,
"shopId_fld0_grp": "3",
"shopId_fld0_op": "equals",
"shopId_fld1_grp": "4",
"shopId_fld1_op": "empty",
...payload.inputFields
},
"noConditionFind": "Y",
Expand All @@ -357,64 +368,83 @@ const actions: ActionTree<JobState, RootState> = {
if (payload?.orderBy) {
params['orderBy'] = payload.orderBy
}
const resp = await JobService.fetchJobInformation(params)

if (resp.status === 200 && !hasError(resp) && resp.data.docs) {
const cached = JSON.parse(JSON.stringify(state.cached));

// added condition to store multiple pending jobs in the state for order batch jobs,
// getting job with status Service draft as well, as this information will be needed when scheduling
// a new batch
// TODO: this needs to be updated when we will be storing the draft and pending jobs separately
const batchJobEnums = JSON.parse(process.env?.VUE_APP_BATCH_JOB_ENUMS as string)
let batchJobEnumIds = Object.values(batchJobEnums)?.map((job: any) => job.id);
// If query is for single systemJobEnumId only update it
if (typeof payload.inputFields.systemJobEnumId === "string" && batchJobEnumIds.includes(payload.inputFields.systemJobEnumId)) {
batchJobEnumIds = [ payload.inputFields.systemJobEnumId ];
} else if (typeof payload.inputFields.systemJobEnumId === "object") {
batchJobEnumIds = batchJobEnumIds.filter((batchJobEnumId: any) => payload.inputFields.systemJobEnumId.includes(batchJobEnumId));
}
batchJobEnumIds.map((batchBrokeringJobEnum: any) => {
cached[batchBrokeringJobEnum] = resp.data.docs.filter((job: any) => job.systemJobEnumId === batchBrokeringJobEnum).reduce((batchBrokeringJobs: any, job: any) => {
batchBrokeringJobs.push({
...job,
id: job.jobId,
frequency: job.tempExprId,
enumId: job.systemJobEnumId,
status: job.statusId
})
return batchBrokeringJobs;
}, [])
})

// added condition to store multiple pending jobs in the state for order batch jobs
resp.data.docs.filter((job: any) => !batchJobEnumIds.includes(job.systemJobEnumId) && job.statusId === 'SERVICE_PENDING').reduce((cached: any, job: any) => {
cached[job.systemJobEnumId] = {
...job,
id: job.jobId,
frequency: job.tempExprId,
enumId: job.systemJobEnumId,
status: job.statusId
}
return cached;
}, cached)

resp.data.docs.filter((job: any) => job.statusId === 'SERVICE_DRAFT').map((job: any) => {
return cached[job.systemJobEnumId] = cached[job.systemJobEnumId] ? cached[job.systemJobEnumId] : {
fetchJobRequests.push(JobService.fetchJobInformation(params).catch((err) => {
return err;
}))

// Deep cloning in order to avoid mutating the same reference causing side effects
params = JSON.parse(JSON.stringify(params));

// Fetching pending jobs
params.inputFields['statusId_value'] = "SERVICE_PENDING";
params.inputFields['statusId_op'] = "equals";
params.inputFields['productStoreId_value'] = this.state.user.currentEComStore.productStoreId;
params.inputFields['productStoreId_op'] = "equals";
fetchJobRequests.push(JobService.fetchJobInformation(params).catch((err) => {
return err;
}))

const resp = await Promise.all(fetchJobRequests)
const responseJobs = resp.reduce((responseJobs: any, response: any) => {
response.status === 200 && !hasError(response) && response.data.docs && (responseJobs = [...responseJobs, ...response.data.docs]);
return responseJobs;
}, [])

// TODO Fix Indentation
const cached = JSON.parse(JSON.stringify(state.cached));

// added condition to store multiple pending jobs in the state for order batch jobs,
// getting job with status Service draft as well, as this information will be needed when scheduling
// a new batch
// TODO: this needs to be updated when we will be storing the draft and pending jobs separately
const batchJobEnums = JSON.parse(process.env?.VUE_APP_BATCH_JOB_ENUMS as string)
let batchJobEnumIds = Object.values(batchJobEnums)?.map((job: any) => job.id);
// If query is for single systemJobEnumId only update it
if (typeof payload.inputFields.systemJobEnumId === "string" && batchJobEnumIds.includes(payload.inputFields.systemJobEnumId)) {
batchJobEnumIds = [ payload.inputFields.systemJobEnumId ];
} else if (typeof payload.inputFields.systemJobEnumId === "object") {
batchJobEnumIds = batchJobEnumIds.filter((batchJobEnumId: any) => payload.inputFields.systemJobEnumId.includes(batchJobEnumId));
}
batchJobEnumIds.map((batchBrokeringJobEnum: any) => {
cached[batchBrokeringJobEnum] = responseJobs.filter((job: any) => job.systemJobEnumId === batchBrokeringJobEnum).reduce((batchBrokeringJobs: any, job: any) => {
batchBrokeringJobs.push({
...job,
id: job.jobId,
frequency: job.tempExprId,
enumId: job.systemJobEnumId,
status: job.statusId
}
});
})
return batchBrokeringJobs;
}, [])
})

// added condition to store multiple pending jobs in the state for order batch jobs
responseJobs.filter((job: any) => !batchJobEnumIds.includes(job.systemJobEnumId) && job.statusId === 'SERVICE_PENDING').reduce((cached: any, job: any) => {
cached[job.systemJobEnumId] = {
...job,
id: job.jobId,
frequency: job.tempExprId,
enumId: job.systemJobEnumId,
status: job.statusId
}
return cached;
}, cached)

responseJobs.filter((job: any) => job.statusId === 'SERVICE_DRAFT').map((job: any) => {
return cached[job.systemJobEnumId] = cached[job.systemJobEnumId] ? cached[job.systemJobEnumId] : {
...job,
id: job.jobId,
frequency: job.tempExprId,
enumId: job.systemJobEnumId,
status: job.statusId
}
});

// fetching temp expressions
const tempExpr = Object.values(cached).map((job: any) => job.tempExprId)
await dispatch('fetchTemporalExpression', tempExpr)
// fetching temp expressions
const tempExpr = Object.values(cached).map((job: any) => job.tempExprId)
await dispatch('fetchTemporalExpression', tempExpr)

commit(types.JOB_UPDATED_BULK, cached);
}
commit(types.JOB_UPDATED_BULK, cached);
return resp;
},
async updateJob ({ commit, dispatch }, job) {
Expand Down
7 changes: 2 additions & 5 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,9 @@ const actions: ActionTree<UserState, RootState> = {
/**
* update current shopify config id
*/
async setCurrentShopifyConfig({ commit }, shopifyConfig) {
async setCurrentShopifyConfig({ commit, dispatch }, shopifyConfig) {
commit(types.USER_CURRENT_SHOPIFY_CONFIG_UPDATED, shopifyConfig);
},

async setEComStore({ commit }, payload) {
commit(types.USER_CURRENT_ECOM_STORE_UPDATED, payload.store);
dispatch('job/clearJobState', null, { root: true });
},

/**
Expand Down

0 comments on commit 77ea681

Please sign in to comment.