airboardgame/public/exec/saveGame.js

27 lines
632 B
JavaScript
Raw Normal View History

2020-10-03 13:37:30 +02:00
console.log("Save game");
const throwError = (message, code = 400) => {
const errorObject = new Error(message);
errorObject.statusCode = code;
throw errorObject;
};
const main = async ({ store, id, userId, body }) => {
let existingGame = null;
try {
existingGame = await store.get("game", id);
} catch {
console.log("Game not found");
}
if (existingGame && existingGame.owner !== userId) {
console.log("fobidden");
throwError("Modification allowed only for owner", 403);
}
let { owner = userId } = body;
const result = await store.save("game", id, { ...body, owner });
return result;
};