From d4056406266e6fc57e33280bd20909c80ddd6bf2 Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Sat, 28 Sep 2019 15:01:21 -0400 Subject: [PATCH] Attempt to fix race condition in /leader-add when leader doesn't exist --- src/interactions/leaderAdd.js | 42 ++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index 57cc5d51..12abc4ce 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -55,18 +55,23 @@ const interactionLeaderAdd = (bot, message) => { 'Full Name': profile.real_name || profile.display_name, } console.log(fields) - return airCreate('Leaders', fields).catch(err => { - console.error( - 'Ran into issue creating new leader airtable record' - ) - throw err - }) + return airCreate('Leaders', fields) + .then(taggedLeader => { + return taggedLeader + }) + .catch(err => { + console.error( + 'Ran into issue creating new leader airtable record' + ) + throw err + }) + } else { + return taggedUser.leader } - return taggedUser }) - .then(taggedUser => { + .then(taggedLeader => { // ensure we can assign the leader to this club - const clubs = taggedUser.leader.fields['Clubs'] || [] + const clubs = taggedLeader.fields['Clubs'] || [] if (clubs.includes(commandUser.club.id)) { bot.replyPrivateDelayed( message, @@ -75,14 +80,21 @@ const interactionLeaderAdd = (bot, message) => { return } clubs.push(commandUser.club.id) - return airPatch('Leaders', taggedUser.leader.id, { + return airPatch('Leaders', taggedLeader.id, { Clubs: clubs, - }).then(() => { - bot.replyPrivateDelayed( - message, - transcript('leaderAdd.success', { taggedUserID, channel }) - ) }) + .then(() => { + bot.replyPrivateDelayed( + message, + transcript('leaderAdd.success', { taggedUserID, channel }) + ) + }) + .catch(err => { + throw err + }) + }) + .catch(err => { + throw err }) }) .catch(err => {