0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-09 13:42:38 +00:00

add 409 return when server version is greater than updating version

This also moves the version back onto the stub
This commit is contained in:
Charlie Humphreys
2022-12-13 21:03:51 -06:00
parent 02c0176070
commit 770d0c141d

View File

@@ -111,7 +111,7 @@ const excludePropsFromUpdate = (brew)=>{
const excludeGoogleProps = (brew)=>{ const excludeGoogleProps = (brew)=>{
const modified = _.clone(brew); const modified = _.clone(brew);
const propsToExclude = ['tags', 'systems', 'published', 'authors', 'owner', 'views', 'thumbnail']; const propsToExclude = ['version', 'tags', 'systems', 'published', 'authors', 'owner', 'views', 'thumbnail'];
for (const prop of propsToExclude) { for (const prop of propsToExclude) {
delete modified[prop]; delete modified[prop];
} }
@@ -119,7 +119,7 @@ const excludeGoogleProps = (brew)=>{
}; };
const excludeStubProps = (brew)=>{ const excludeStubProps = (brew)=>{
const propsToExclude = ['text', 'textBin', 'renderer', 'pageCount', 'version']; const propsToExclude = ['text', 'textBin', 'renderer', 'pageCount'];
for (const prop of propsToExclude) { for (const prop of propsToExclude) {
brew[prop] = undefined; brew[prop] = undefined;
} }
@@ -187,7 +187,13 @@ const newBrew = async (req, res)=>{
const updateBrew = async (req, res)=>{ const updateBrew = async (req, res)=>{
// Initialize brew from request and body, destructure query params, set a constant for the google id, and set the initial value for the after-save method // Initialize brew from request and body, destructure query params, set a constant for the google id, and set the initial value for the after-save method
let brew = _.assign(req.brew, excludePropsFromUpdate(req.body)); const updateBrew = excludePropsFromUpdate(req.body);
if(req.brew.version > updateBrew.version) {
res.setHeader('Content-Type', 'application/json');
return res.status(409).send(JSON.stringify({ message: `The brew has been changed on a different device. Please save your changes elsewhere, refresh, and try again.` }));
}
let brew = _.assign(req.brew, updateBrew);
const { saveToGoogle, removeFromGoogle } = req.query; const { saveToGoogle, removeFromGoogle } = req.query;
const googleId = brew.googleId; const googleId = brew.googleId;
let afterSave = async ()=>true; let afterSave = async ()=>true;
@@ -233,6 +239,7 @@ const updateBrew = async (req, res)=>{
brew.text = undefined; brew.text = undefined;
} }
brew.updatedAt = new Date(); brew.updatedAt = new Date();
brew.version += 1;
if(req.account) { if(req.account) {
brew.authors = _.uniq(_.concat(brew.authors, req.account.username)); brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
@@ -252,7 +259,7 @@ const updateBrew = async (req, res)=>{
// if the brew does have a stub id, update it using the stub id as the key. // if the brew does have a stub id, update it using the stub id as the key.
brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew); brew = _.assign(await HomebrewModel.findOne({ _id: brew._id }), brew);
saved = await brew.save() saved = await brew.save()
.catch(saveError); .catch(saveError);
} }
if(!saved) return; if(!saved) return;
// Call and wait for afterSave to complete // Call and wait for afterSave to complete