report success if existing data matches new data during update

This commit is contained in:
Jesse D. McDonald 2020-04-28 14:38:31 -05:00
parent 35bbd0509a
commit d56b2d3187
1 changed files with 27 additions and 5 deletions

View File

@ -394,12 +394,34 @@ async function postGameHandler(req, res, next) {
/* Now wait for all the queries to finish. */ /* Now wait for all the queries to finish. */
const result = await transactionP; const result = await transactionP;
if (!result || result.modified !== params.$time) { if (result && result.modified === params.$time) {
/* Only signal an update if the record was actually changed. No-ops don't count. */
signalGameUpdate(gameId);
} else {
/*
* If the record exists and all the values (other than .modified) match the
* request, treat this as a successful no-op. This ensures that repeated
* requests for the same update (i.e. retries) do not fail just because the
* modified time was changed.
*/
let same = false;
if (result) {
same = true;
for (const key in body) {
/* Use the value from params since e.g. body.board may be adjusted. */
if (key !== 'modified' && result[key] !== params['$' + key]) {
same = false;
break;
}
}
}
if (!same) {
res.status(409).json({ message: 'update failed', modified: (result || {}).modified }); res.status(409).json({ message: 'update failed', modified: (result || {}).modified });
return; return;
} }
}
signalGameUpdate(gameId);
res.json({ success: true, modified: result.modified }); res.json({ success: true, modified: result.modified });
} }