airboardgame/public/exec/saveGame.js

31 lines
753 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;
2020-10-28 13:24:51 +01:00
if (!userId) {
throwError("Game creation not allowed for unauthenticated users", 403);
}
2020-10-03 13:37:30 +02:00
try {
existingGame = await store.get("game", id);
} catch {
console.log("Game not found");
}
2020-10-29 12:05:25 +01:00
if (existingGame && existingGame.owner && existingGame.owner !== userId) {
2020-10-28 13:24:51 +01:00
console.log("Forbidden");
2020-10-03 13:37:30 +02:00
throwError("Modification allowed only for owner", 403);
}
let { owner = userId } = body;
const result = await store.save("game", id, { ...body, owner });
return result;
};