0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-29 13:22:40 +00:00

Cleanup and better handling of pre-save snapshot

This commit is contained in:
Trevor Buckner
2025-07-08 15:50:27 -04:00
parent 41daf8d172
commit 6414e73e7d
2 changed files with 43 additions and 47 deletions

View File

@@ -339,30 +339,30 @@ const api = {
// Initialize brew from request and body, destructure query params, and set the initial value for the after-save method
const brewFromClient = api.excludePropsFromUpdate(req.body);
const brewFromServer = req.brew;
const serverHash = md5(brewFromServer.text);
console.log({serverHash: serverHash});
console.log({clientHash: brewFromClient.hash});
splitTextStyleAndMetadata(brewFromServer);
if((brewFromServer?.version !== brewFromClient?.version)) {
console.log(`Version mismatch on brew ${brewFromClient.editId}`);
brewFromServer.hash = await md5(brewFromServer.text);
if((brewFromServer?.version !== brewFromClient?.version) || (brewFromServer?.hash !== brewFromClient?.hash)) {
if(brewFromClient?.version !== brewFromClient?.version)
console.log(`Version mismatch on brew ${brewFromClient.editId}`);
if(brewFromServer?.hash !== brewFromClient?.hash) {
console.log(`Hash mismatch on brew ${brewFromClient.editId}`);
}
res.setHeader('Content-Type', 'application/json');
return res.status(409).send(JSON.stringify({ message: `The server copy is out of sync with the saved brew. Please save your changes elsewhere, refresh, and try again.` }));
}
let brew = _.assign(brewFromServer, brewFromClient);
brew.title = brew.title.trim();
brew.description = brew.description.trim() || '';
brew.text = applyPatches(brewFromClient.patches, brewFromServer.text)[0];
brew.text = api.mergeBrewText(brew);
console.log(`Brewfromserver: ${JSON.stringify(brewFromServer)}`);
splitTextStyleAndMetadata(brewFromServer);
brewFromClient.text = applyPatches(brewFromClient.patches, brewFromServer.text)[0];
console.log(`Server Text: ${brewFromServer.text}`);
console.log(`Brew text: ${brewFromClient.text}`);
let brew = _.assign(brewFromServer, brewFromClient);
const googleId = brew.googleId;
const { saveToGoogle, removeFromGoogle } = req.query;
let afterSave = async ()=>true;
brew.title = brew.title.trim();
brew.description = brew.description.trim() || '';
brew.text = api.mergeBrewText(brew);
if(brew.googleId && removeFromGoogle) {
// If the google id exists and we're removing it from google, set afterSave to delete the google brew and mark the brew's google id as undefined
afterSave = async ()=>{