This commit is contained in:
2021-04-16 19:15:12 +00:00
parent 444044ef41
commit 948f79e4df

View File

@@ -244,6 +244,10 @@ export default {
change: true,
delete: true
}
},
saveError: {
fatal: false, //fatal error, further save is pointless, bail early and report
error: null //contains error object
}
};
},
@@ -408,14 +412,9 @@ export default {
const isPost = vm.obj.id == 0;
let err = {
fatal: false, //fatal error, further save is pointless, bail early and report
error: false, //true if any error to be displayed regardless if fatal, set by each save / delete
header: null, //res.error collections exactly as provided by server to be synthesized later
state: null, //only saves one state at a time never more than one so this is like header a singleton
items: [],
scheduledUsers: []
};
//reset error object
this.saveError.fatal = false;
this.saveError.error = null;
/*
@@ -439,15 +438,15 @@ export default {
//(otherwise there's nothing to hang the other things off of)
let headerSaved = false;
if (this.obj.concurrency == 0) {
err = await saveHeader(vm, err);
await saveHeader(vm);
headerSaved = true;
}
//LOCKED? State must be saved first then (assuming it unlocks)
let stateSaved = false;
if (this.obj.isLockedAtServer) {
err = await saveState(vm, err);
if (!err.fatal) {
await saveState(vm);
if (!this.saveError.fatal) {
stateSaved = true;
//update which areas are available to user
//which may have changed due to state being saved (saveState sets the current islocked value)
@@ -456,69 +455,52 @@ export default {
}
//HEADER
if (!err.fatal && !headerSaved) {
err = await saveHeader(vm, err);
if (!this.saveError.fatal && !headerSaved) {
await saveHeader(vm);
}
//WOITEMS
if (!err.fatal) {
if (!this.saveError.fatal) {
//This saves all bottom level collections as well
err = await saveItems(vm, err);
await saveItems(vm);
}
//### STATE last normally
//in case it locks or is completed
if (!err.fatal && !stateSaved) {
err = await saveState(vm, err);
if (!err.fatal) {
if (!this.saveError.fatal && !stateSaved) {
await saveState(vm);
if (!this.saveError.fatal) {
updateRights(vm);
}
}
//## ALL PARTIAL UPDATES COMPLETED
//handle errors
if (err.error) {
if (this.saveError.error != null) {
//# FAIL ROUTE
// console.log("RAW ERROR OBJECT: ", err);
//REHYDRATE ERRORS
//convert to form() error handling understood format
let compiledError = {
code: "2200",
message: "ErrorAPI2200",
details: []
};
if (err.header != null) {
compiledError.details.push(err.header);
}
if (err.state != null) {
compiledError.details.push(err.state);
}
//##### TODO: Move error compilation into saves, no need for this complex rigamarole
//bugbug: details is an array inside so there could be multiple errors for that target so need to iterate it inside the iteration
//bugbug: on a server error there is no details so dont' code to expect it
/*i.e.
{
error: {
code: "2002",
message: "See server log for details",
target: "Server internal error"
}
}
*/
//wouldn't this just be easier to do directly inside the saves themselves??
//why compile it at all, why not make it directly ready to display at the end, there's no other use for this info really
err.items.forEach(z =>
compiledError.details.push({
message: z.e.details[0].message,
error: z.e.details[0].error,
target: `Items[${z.objectIndex}].${z.e.details[0].target}`
})
);
err.scheduledUsers.forEach(z =>
compiledError.details.push({
message: z.e.details[0].message,
error: z.e.details[0].error,
target: `Items[${z.woItemIndex}].scheduledUsers[${z.objectIndex}].${z.e.details[0].target}`
})
);
//console.log("Compiled error ", compiledError);
vm.formState.serverError = compiledError;
vm.formState.serverError = this.saveError.error;
window.$gz.form.setErrorBoxErrors(vm);
/*
/*
`Items[${activeWoItemIndex}].scheduledUsers[${activeItemIndex}].estimatedQuantity`
{
"fatal": false,
@@ -710,9 +692,9 @@ export default {
/////////////////////////////
// HEADER
//
async function saveHeader(vm, err) {
async function saveHeader(vm) {
if (!vm.obj.isDirty) {
return err;
return;
}
const isPost = vm.obj.id == 0;
@@ -724,11 +706,8 @@ async function saveHeader(vm, err) {
let res = await window.$gz.api.upsert(`${API_BASE_URL}`, headerOnly);
if (res.error) {
if (isPost) {
err.fatal = true;
handleSaveError(res.error, true);
}
err.header = res.error;
err.error = true;
return err;
} else {
//update any server changed fields
vm.obj.concurrency = res.data.concurrency;
@@ -740,18 +719,17 @@ async function saveHeader(vm, err) {
vm.obj.states.forEach(z => (z.workOrderId = vm.obj.id));
vm.obj.items.forEach(z => (z.workOrderId = vm.obj.id));
}
return err;
}
}
/////////////////////////////
// STATES
//
async function saveState(vm, err) {
async function saveState(vm) {
//CHANGED?
let totalItems = vm.obj.states.length;
if (totalItems == 0) {
return err;
return;
}
for (let i = 0; i < totalItems; i++) {
let o = vm.obj.states[i];
@@ -759,8 +737,7 @@ async function saveState(vm, err) {
//it's new so save it
let res = await window.$gz.api.upsert(`${API_BASE_URL}states`, o);
if (res.error) {
err.states = res.error; //only saves one at a time so no need for collection stuff here
err.error = true;
handleSaveError(res.error);
} else {
vm.obj.states[i] = res.data;
@@ -769,13 +746,51 @@ async function saveState(vm, err) {
}
}
}
return err;
}
function handleSaveError(e) {
/*
e:{
fatal:true/false,//override, normally this function would determine this on it's own
error:res.error,//server error
itemIndex:null,//if has parent woitem this is where it's index is set
childKey:"scheduledUsers"/"Items",//name of child collection
childIndex:null,//if it's a child this is the index
}
//old compiledError code
err.items.forEach(z =>
compiledError.details.push({
message: z.e.details[0].message,
error: z.e.details[0].error,
target: `Items[${z.objectIndex}].${z.e.details[0].target}`
})
);
err.scheduledUsers.forEach(z =>
compiledError.details.push({
message: z.e.details[0].message,
error: z.e.details[0].error,
target: `Items[${z.woItemIndex}].scheduledUsers[${z.objectIndex}].${z.e.details[0].target}`
})
);
//Goes into this.saveError.error ultimately
saveError: {
fatal: false, //fatal error, further save is pointless, bail early and report
error: null //contains error object
}
*/
if (this.error == null) {
this.error = [];
}
//set Target properly as requried and push error into the error collection
}
/////////////////////////////
// ITEMS
//
async function deleteItems(vm, err) {
async function deleteItems(vm) {
//walk the array backwards as items may or may not be spliced out
for (var i = vm.obj.items.length - 1; i >= 0; i--) {
const d = vm.obj.items[i];
@@ -784,6 +799,7 @@ async function deleteItems(vm, err) {
}
let res = await window.$gz.api.remove(`${API_BASE_URL}items/${d.id}`);
if (res.error) {
handleSaveError(res.error, true);
err.items.push({
e: res.error,
objectIndex: d.objectIndex,