Fix race-condition in voting
This commit is contained in:
parent
8ea3606dbb
commit
c9b2d7d62e
@ -4,8 +4,6 @@ import {
|
||||
getEntriesFromSnapshot,
|
||||
getVoteSnapshot,
|
||||
getVoteCounts,
|
||||
getVoteEntry,
|
||||
makeVoteEntry,
|
||||
updateVoteEntry,
|
||||
getVote,
|
||||
getActiveVote,
|
||||
@ -42,14 +40,7 @@ const vote = async (
|
||||
return {error: "Invalid vote index", code: 400};
|
||||
}
|
||||
|
||||
const entry = await getVoteEntry(vote.ref, voter);
|
||||
if (!entry) {
|
||||
await makeVoteEntry(vote.ref, voter, voteIndex);
|
||||
await updateVoteCount();
|
||||
return {success: true};
|
||||
}
|
||||
|
||||
await updateVoteEntry(entry, voteIndex);
|
||||
await updateVoteEntry(vote, voter, voteIndex);
|
||||
await updateVoteCount();
|
||||
return {success: true};
|
||||
};
|
||||
|
@ -43,10 +43,11 @@ export const getVoteEntry = async (
|
||||
.where(usernameField, "==", username)
|
||||
.get() as QuerySnapshot<VoteEntry>).docs[0]?.ref;
|
||||
export const updateVoteEntry = async (
|
||||
vote: DocumentReference<VoteEntry>,
|
||||
vote: DocumentSnapshot<Vote>,
|
||||
username: string,
|
||||
voteIndex: number
|
||||
) =>
|
||||
vote.update({[voteIndexField]: voteIndex});
|
||||
vote.ref.collection(entriesCollectionName).doc(username).set({[voteIndexField]: voteIndex});
|
||||
export const makeVoteEntry = async (
|
||||
vote: DocumentReference<Vote>,
|
||||
username: string,
|
||||
|
Loading…
x
Reference in New Issue
Block a user