Handle simultaneous quest accept/reject (#12090)

* Implement atomic quest accept/reject

* Persist quest.members early to avoid simultaneous handling of accept/reject

* Fix quest accept test (missing expectation)

* PR fixes
This commit is contained in:
Carl Vuorinen
2020-04-17 23:57:31 +03:00
committed by GitHub
parent 936d3ffc98
commit 2896cf77e0
3 changed files with 40 additions and 7 deletions

View File

@@ -193,12 +193,14 @@ api.acceptQuest = {
if (group.quest.active) throw new NotAuthorized(res.t('questAlreadyStartedFriendly'));
if (group.quest.members[user._id]) throw new BadRequest(res.t('questAlreadyAccepted'));
const acceptedSuccessfully = await group.handleQuestInvitation(user, true);
if (!acceptedSuccessfully) {
throw new NotAuthorized(res.t('questAlreadyAccepted'));
}
user.party.quest.RSVPNeeded = false;
await user.save();
group.markModified('quest');
group.quest.members[user._id] = true;
if (canStartQuestAutomatically(group)) {
await group.startQuest(user);
}
@@ -252,13 +254,15 @@ api.rejectQuest = {
if (group.quest.members[user._id]) throw new BadRequest(res.t('questAlreadyAccepted'));
if (group.quest.members[user._id] === false) throw new BadRequest(res.t('questAlreadyRejected'));
const rejectedSuccessfully = await group.handleQuestInvitation(user, false);
if (!rejectedSuccessfully) {
throw new NotAuthorized(res.t('questAlreadyRejected'));
}
user.party.quest = Group.cleanQuestUser(user.party.quest.progress);
user.markModified('party.quest');
await user.save();
group.quest.members[user._id] = false;
group.markModified('quest.members');
if (canStartQuestAutomatically(group)) {
await group.startQuest(user);
}