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, change: true,
delete: 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; const isPost = vm.obj.id == 0;
let err = { //reset error object
fatal: false, //fatal error, further save is pointless, bail early and report this.saveError.fatal = false;
error: false, //true if any error to be displayed regardless if fatal, set by each save / delete this.saveError.error = null;
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: []
};
/* /*
@@ -439,15 +438,15 @@ export default {
//(otherwise there's nothing to hang the other things off of) //(otherwise there's nothing to hang the other things off of)
let headerSaved = false; let headerSaved = false;
if (this.obj.concurrency == 0) { if (this.obj.concurrency == 0) {
err = await saveHeader(vm, err); await saveHeader(vm);
headerSaved = true; headerSaved = true;
} }
//LOCKED? State must be saved first then (assuming it unlocks) //LOCKED? State must be saved first then (assuming it unlocks)
let stateSaved = false; let stateSaved = false;
if (this.obj.isLockedAtServer) { if (this.obj.isLockedAtServer) {
err = await saveState(vm, err); await saveState(vm);
if (!err.fatal) { if (!this.saveError.fatal) {
stateSaved = true; stateSaved = true;
//update which areas are available to user //update which areas are available to user
//which may have changed due to state being saved (saveState sets the current islocked value) //which may have changed due to state being saved (saveState sets the current islocked value)
@@ -456,69 +455,52 @@ export default {
} }
//HEADER //HEADER
if (!err.fatal && !headerSaved) { if (!this.saveError.fatal && !headerSaved) {
err = await saveHeader(vm, err); await saveHeader(vm);
} }
//WOITEMS //WOITEMS
if (!err.fatal) { if (!this.saveError.fatal) {
//This saves all bottom level collections as well //This saves all bottom level collections as well
err = await saveItems(vm, err); await saveItems(vm);
} }
//### STATE last normally //### STATE last normally
//in case it locks or is completed //in case it locks or is completed
if (!err.fatal && !stateSaved) { if (!this.saveError.fatal && !stateSaved) {
err = await saveState(vm, err); await saveState(vm);
if (!err.fatal) { if (!this.saveError.fatal) {
updateRights(vm); updateRights(vm);
} }
} }
//## ALL PARTIAL UPDATES COMPLETED //## ALL PARTIAL UPDATES COMPLETED
//handle errors //handle errors
if (err.error) { if (this.saveError.error != null) {
//# FAIL ROUTE //# 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 //##### 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: 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?? //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 //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 => vm.formState.serverError = this.saveError.error;
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;
window.$gz.form.setErrorBoxErrors(vm); window.$gz.form.setErrorBoxErrors(vm);
/* /*
`Items[${activeWoItemIndex}].scheduledUsers[${activeItemIndex}].estimatedQuantity` `Items[${activeWoItemIndex}].scheduledUsers[${activeItemIndex}].estimatedQuantity`
{ {
"fatal": false, "fatal": false,
@@ -710,9 +692,9 @@ export default {
///////////////////////////// /////////////////////////////
// HEADER // HEADER
// //
async function saveHeader(vm, err) { async function saveHeader(vm) {
if (!vm.obj.isDirty) { if (!vm.obj.isDirty) {
return err; return;
} }
const isPost = vm.obj.id == 0; 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); let res = await window.$gz.api.upsert(`${API_BASE_URL}`, headerOnly);
if (res.error) { if (res.error) {
if (isPost) { if (isPost) {
err.fatal = true; handleSaveError(res.error, true);
} }
err.header = res.error;
err.error = true;
return err;
} else { } else {
//update any server changed fields //update any server changed fields
vm.obj.concurrency = res.data.concurrency; 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.states.forEach(z => (z.workOrderId = vm.obj.id));
vm.obj.items.forEach(z => (z.workOrderId = vm.obj.id)); vm.obj.items.forEach(z => (z.workOrderId = vm.obj.id));
} }
return err;
} }
} }
///////////////////////////// /////////////////////////////
// STATES // STATES
// //
async function saveState(vm, err) { async function saveState(vm) {
//CHANGED? //CHANGED?
let totalItems = vm.obj.states.length; let totalItems = vm.obj.states.length;
if (totalItems == 0) { if (totalItems == 0) {
return err; return;
} }
for (let i = 0; i < totalItems; i++) { for (let i = 0; i < totalItems; i++) {
let o = vm.obj.states[i]; let o = vm.obj.states[i];
@@ -759,8 +737,7 @@ async function saveState(vm, err) {
//it's new so save it //it's new so save it
let res = await window.$gz.api.upsert(`${API_BASE_URL}states`, o); let res = await window.$gz.api.upsert(`${API_BASE_URL}states`, o);
if (res.error) { if (res.error) {
err.states = res.error; //only saves one at a time so no need for collection stuff here handleSaveError(res.error);
err.error = true;
} else { } else {
vm.obj.states[i] = res.data; 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 // ITEMS
// //
async function deleteItems(vm, err) { async function deleteItems(vm) {
//walk the array backwards as items may or may not be spliced out //walk the array backwards as items may or may not be spliced out
for (var i = vm.obj.items.length - 1; i >= 0; i--) { for (var i = vm.obj.items.length - 1; i >= 0; i--) {
const d = vm.obj.items[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}`); let res = await window.$gz.api.remove(`${API_BASE_URL}items/${d.id}`);
if (res.error) { if (res.error) {
handleSaveError(res.error, true);
err.items.push({ err.items.push({
e: res.error, e: res.error,
objectIndex: d.objectIndex, objectIndex: d.objectIndex,