Skip to content

Commit

Permalink
fix(transaction): isolation (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorWinberg authored Nov 3, 2022
1 parent b35a29f commit 8630292
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
3 changes: 0 additions & 3 deletions client/src/store/store-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export default {
const { isAuthenticated = false, ...user } = payload;
if (user.lvl && state.user.lvl && user.lvl > state.user.lvl) {
EventBus.$emit(EVENT_TYPE.LEVEL_UP, user.lvl);
throw new Error(
`Was this correct? LEVEL_UP: ${user.lvl} > ${state.user.lvl}`
);
}

if (isAuthenticated) {
Expand Down
15 changes: 8 additions & 7 deletions server/src/qrshard/qrshard-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ module.exports = db => ({
const { keys, values, indices } = keyValuePairs(valid, qrshard);
await db.query("BEGIN TRANSACTION");

const res = await alreadyExists(db, userId, qrshard);
if (res.exists || res.err) {
await db.query("ROLLBACK");
return { qrshard: null, err: res.err };
}

const sql = `
INSERT INTO qrshards ( ${keys.concat("user_id")} )
VALUES (${indices.concat(userId)})
RETURNING *`;

const { rows, err } = await db.query(sql, values);

const res = await alreadyExists(db, userId, qrshard);
if (res.exists !== 1 || res.err) {
await db.query("ROLLBACK");
return { qrshard: null, err: res.err };
}

await db.query("COMMIT");
return { qrshard: rows[0], err };
},
Expand Down Expand Up @@ -75,5 +76,5 @@ const alreadyExists = async (db, userId, qrshard) => {
AND created_at::date = now()::date
`;
const { rows, err } = await db.query(sql, [userId, qrshard.qrspot_id]);
return { exists: (rows || []).length > 0, err };
return { exists: (rows || []).length, err };
};

0 comments on commit 8630292

Please sign in to comment.