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

Resolve intermittent 422 error in Guided Templates #6034

Merged
merged 4 commits into from
Jan 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
:user-id="currentUserId"
@task-updated="taskUpdated"
@submit="submit"
@completed="completed"
@@error="error"
></task>
</modal>
</div>
Expand Down
156 changes: 84 additions & 72 deletions resources/js/components/templates/mixins/wizardHelperProcessModal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export default {
data() {
return {
Expand All @@ -8,45 +7,50 @@ export default {
}
},
methods: {
async getHelperProcessStartEvent(triggeredBy = null) {
getHelperProcessStartEvent(triggeredBy = null) {
if (triggeredBy === 'wizard-details-modal') {
this.startEvents = this.template.process.start_events.filter(event => !event.eventDefinitions || event.eventDefinitions.length === 0);
this.helperProcessId = this.template.process.id;

this.triggerHelperProcessStartEvent();
} else {
if (this.wizardTemplateUuid !== null) {
const response = await ProcessMaker.apiClient.get(`wizard-templates/${this.wizardTemplateUuid}/get-helper-process`);
if (response.data) {
this.helperProcessId = response.data.helper_process_id;
this.startEvents = JSON.parse(response.data.start_events).filter(event => !event.eventDefinitions || event.eventDefinitions.length === 0);

this.triggerHelperProcessStartEvent();
}
ProcessMaker.apiClient.get(`wizard-templates/${this.wizardTemplateUuid}/get-helper-process`)
.then(response => {
if (response.data) {
this.helperProcessId = response.data.helper_process_id;
this.startEvents = JSON.parse(response.data.start_events).filter(event => !event.eventDefinitions || event.eventDefinitions.length === 0);

this.triggerHelperProcessStartEvent();
}
});
}
}
}
},
async triggerHelperProcessStartEvent() {
triggerHelperProcessStartEvent() {
try {

const startEventId = this.startEvents[0].id;
const url = `/process_events/${this.helperProcessId}?event=${startEventId}`;

// Start the helper process
const response = await window.ProcessMaker.apiClient.post(url);
const processRequestId = response.data.id;
window.ProcessMaker.apiClient.post(url).then(response => {
const processRequestId = response.data.id;
this.getNextTask(processRequestId);
}).catch(error => {
console.error('Error: ', error);
})

this.getNextTask(processRequestId);
} catch (err) {
const data = err.response?.data;
if (data && data.message) {
ProcessMaker.alert(data.message, 'danger');
}
}
},
async getNextTask(processRequestId) {
getNextTask(processRequestId) {
try {
const response = await ProcessMaker.apiClient.get(`tasks`, {
ProcessMaker.apiClient.get(`tasks`, {
params: {
page: 1,
include: 'user,assignableUsers',
Expand All @@ -56,24 +60,26 @@ export default {
order_by: 'due_at',
order_direction: 'asc'
}
});

const taskData = response.data.data;

if (taskData.length > 0) {
this.task = taskData[0];
this.currentUserId = parseInt(document.head.querySelector('meta[name="user-id"]').content);
this.$bvModal.show('processWizard');
this.showHelperProcess = true;
} else {
if (this.shouldImportProcessTemplate) {
this.importProcessTemplate();
}).then(response => {
const taskData = response.data.data;
if (taskData.length > 0) {
this.task = taskData[0];
this.currentUserId = parseInt(document.head.querySelector('meta[name="user-id"]').content);
this.$bvModal.show('processWizard');
this.showHelperProcess = true;
} else {
this.showHelperProcess = false;
this.close();
if (this.shouldImportProcessTemplate) {
this.importProcessTemplate();
} else {
this.showHelperProcess = false;
this.close();
}
}

}

}).catch(error => {
console.error(error);
});

} catch (error) {
if (error && error.message) {
ProcessMaker.alert(error.message, 'danger');
Expand All @@ -85,14 +91,15 @@ export default {
// Cancels the associated process request to prevent orphaned processes.
this.cancelHelperProcessRequest();
},
async cancelHelperProcessRequest() {
cancelHelperProcessRequest() {
const {process_request_id: processRequestId } = this.task;

try {
await ProcessMaker.apiClient.put(`requests/${processRequestId}`, {
ProcessMaker.apiClient.put(`requests/${processRequestId}`, {
status: "CANCELED"
}).then(response => {
this.showHelperProcess = false;
});
this.showHelperProcess = false;
} catch (error) {
if (data && data.message) {
ProcessMaker.alert(data.message, 'danger');
Expand All @@ -102,64 +109,69 @@ export default {
taskUpdated(task) {
this.task = task;
},
async submit(task) {
submit(task) {
const { id: taskId, process_request_id: processRequestId } = task;

try {
await ProcessMaker.apiClient.put(`tasks/${taskId}`, {
status: "COMPLETED",
data: this.formData
});

// Successfully completed task, get the next one
await this.getNextTask(processRequestId);
if (task.advanceStatus !== 'completed') {
ProcessMaker.apiClient.put(`tasks/${taskId}`, {
status: "COMPLETED",
data: this.formData
}).then(response => {
// Successfully completed task, get the next one
this.getNextTask(processRequestId);
}).catch(error => {
console.log('Error: ', error);
});
} else {
if (this.shouldImportProcessTemplate) {
this.importProcessTemplate();
} else {
this.showHelperProcess = false;
this.close();
}
}
} catch (error) {
if (error && error.message) {
ProcessMaker.alert(error.message, 'danger');
ProcessMaker.alert(error.message, 'danger');
}
}
},
completed(processRequestId) {
console.log("task completed", processRequestId);
},
error(processRequestId) {
console.error('error', processRequestId);
},
async importProcessTemplate() {
console.log("template", this.template);
const response = await ProcessMaker.apiClient.post(`template/create/process/${this.template.process_template_id}`, {
importProcessTemplate() {
ProcessMaker.apiClient.post(`template/create/process/${this.template.process_template_id}`, {
name: this.template.name,
description: this.template.description,
version: '1.0.0', // TODO: Wizards should have a versions property
version: '1.0.0', // TODO: Wizards should have a versions property
process_category_id: this.template.process.process_category_id,
projects: null,
wizardTemplateUuid: this.template.uuid,
}).then(response => {
if (response.data?.existingAssets) {
this.handleExistingAssets(response.data);
} else {
// redirect to the new process launchpad
window.location = `/processes-catalogue/${response.data.processId}`;
}
}).catch(error => {
console.error(error);
});

if (response.data?.existingAssets) {
this.handleExistingAssets(response.data);
} else {
// redirect to the new process launchpad
window.location = `/processes-catalogue/${response.data.processId}`;
}
},
handleExistingAssets(data) {
const assets = JSON.stringify(data.existingAssets);
const responseId = data.id;
const request = JSON.stringify(data.request);
window.history.pushState(
{
assets,
name: this.template.name,
responseId,
request,
redirectTo: 'process-launchpad',
wizardTemplateUuid: this.template.uuid,
assets,
name: this.template.name,
responseId,
request,
redirectTo: 'process-launchpad',
wizardTemplateUuid: this.template.uuid,
},
"",
"/template/assets",
);
window.location = "/template/assets";
}
},
}
},
}
Loading