From db3364259efb61a94ad6747197120aaa401dec5b Mon Sep 17 00:00:00 2001 From: Agustin Pane Date: Thu, 1 Aug 2019 11:25:59 -0300 Subject: [PATCH] Adds try catch on sendEmailAfterBondingPeriodHAsEndedNotificationToDelegators --- .../notification/notificateDelegatorUtils.js | 140 ++++++++++-------- 1 file changed, 75 insertions(+), 65 deletions(-) diff --git a/server/helpers/notification/notificateDelegatorUtils.js b/server/helpers/notification/notificateDelegatorUtils.js index 066772b..3187cb8 100644 --- a/server/helpers/notification/notificateDelegatorUtils.js +++ b/server/helpers/notification/notificateDelegatorUtils.js @@ -116,46 +116,51 @@ const sendEmailAfterBondingPeriodHasEndedNotificationToDelegators = async curren const currentRoundId = currentRoundInfo.id for (const subscriberItem of subscribers) { - const { subscriber } = subscriberItem + try { + const { subscriber } = subscriberItem - if (!subscriber.lastPendingToBondingPeriodEmailSent) { - subscriber.lastPendingToBondingPeriodEmailSent = currentRoundId - await subscriber.save() - continue - } + if (!subscriber.lastPendingToBondingPeriodEmailSent) { + subscriber.lastPendingToBondingPeriodEmailSent = currentRoundId + await subscriber.save() + continue + } - // Check if notification was already sent, backward to 1 or 2 rounds - const differenceAlreadySended = - +currentRoundId - (+subscriber.lastPendingToBondingPeriodEmailSent || 0) - const isNotificationAlreadySended = - differenceAlreadySended === 0 || - differenceAlreadySended === 1 || - differenceAlreadySended === 2 + // Check if notification was already sent, backward to 1 or 2 rounds + const differenceAlreadySended = + +currentRoundId - (+subscriber.lastPendingToBondingPeriodEmailSent || 0) + const isNotificationAlreadySended = + differenceAlreadySended === 0 || + differenceAlreadySended === 1 || + differenceAlreadySended === 2 - if (isNotificationAlreadySended) { - console.log( - `[Notificate-After-Bonding-Period-Has-Ended] - Not sending email to ${subscriber.email} because already sent an email in the ${subscriber.lastPendingToBondingPeriodEmailSent} round` - ) - continue - } + if (isNotificationAlreadySended) { + console.log( + `[Notificate-After-Bonding-Period-Has-Ended] - Not sending email to ${subscriber.email} because already sent an email in the ${subscriber.lastPendingToBondingPeriodEmailSent} round` + ) + continue + } - const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) + const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) - // Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status - const difference = +currentRoundId - +delegator.startRound - const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2 + // Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status + const difference = +currentRoundId - +delegator.startRound + const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2 - if ( - isDifferenceBetweenRoundsEqualTo && - delegator.status === constants.DELEGATOR_STATUS.Bonded - ) { - subscribersToSendEmails.push( - delegatorEmailUtils.sendDelegatorNotificationBondingPeriodHasEnded( - subscriber, - delegator.delegateAddress, - currentRoundId + if ( + isDifferenceBetweenRoundsEqualTo && + delegator.status === constants.DELEGATOR_STATUS.Bonded + ) { + subscribersToSendEmails.push( + delegatorEmailUtils.sendDelegatorNotificationBondingPeriodHasEnded( + subscriber, + delegator.delegateAddress, + currentRoundId + ) ) - ) + } + } catch (err) { + console.error(`[Notificate-After-Bonding-Period-Has-Ended] - error: ${err}`) + continue } } console.log( @@ -181,45 +186,50 @@ const sendTelegramAfterBondingPeriodHasEndedNotificationToDelegators = async cur const currentRoundId = currentRoundInfo.id for (const subscriberItem of subscribers) { - const { subscriber } = subscriberItem + try { + const { subscriber } = subscriberItem - if (!subscriber.lastPendingToBondingPeriodTelegramSent) { - subscriber.lastPendingToBondingPeriodTelegramSent = currentRoundId - await subscriber.save() - continue - } + if (!subscriber.lastPendingToBondingPeriodTelegramSent) { + subscriber.lastPendingToBondingPeriodTelegramSent = currentRoundId + await subscriber.save() + continue + } - // Check if notification was already sent, backward to 1 or 2 rounds - const differenceAlreadySended = - +currentRoundId - (+subscriber.lastPendingToBondingPeriodTelegramSent || 0) - const isNotificationAlreadySended = - differenceAlreadySended === 0 || - differenceAlreadySended === 1 || - differenceAlreadySended === 2 + // Check if notification was already sent, backward to 1 or 2 rounds + const differenceAlreadySended = + +currentRoundId - (+subscriber.lastPendingToBondingPeriodTelegramSent || 0) + const isNotificationAlreadySended = + differenceAlreadySended === 0 || + differenceAlreadySended === 1 || + differenceAlreadySended === 2 - if (isNotificationAlreadySended) { - console.log( - `[Notificate-After-Bonding-Period-Has-Ended] - Not sending a telegram to ${subscriber.telegramChatId} because already sent a telegram in the ${subscriber.lastPendingToBondingPeriodTelegramSent} round` - ) - continue - } + if (isNotificationAlreadySended) { + console.log( + `[Notificate-After-Bonding-Period-Has-Ended] - Not sending a telegram to ${subscriber.telegramChatId} because already sent a telegram in the ${subscriber.lastPendingToBondingPeriodTelegramSent} round` + ) + continue + } - const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) + const { constants, delegator } = await subscriberUtils.getSubscriptorRole(subscriber) - // Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status - const difference = +currentRoundId - +delegator.startRound - const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2 + // Check difference between rounds, and if status is bonded, if the difference is between 1 or 2 since startRound, it means the subscriber is starting the bonded status + const difference = +currentRoundId - +delegator.startRound + const isDifferenceBetweenRoundsEqualTo = difference === 1 || difference === 2 - if ( - isDifferenceBetweenRoundsEqualTo && - delegator.status === constants.DELEGATOR_STATUS.Bonded - ) { - subscribersToSendTelegrams.push( - delegatorTelegramUtils.sendDelegatorNotificationBondingPeriodHasEnded( - subscriber, - currentRoundId + if ( + isDifferenceBetweenRoundsEqualTo && + delegator.status === constants.DELEGATOR_STATUS.Bonded + ) { + subscribersToSendTelegrams.push( + delegatorTelegramUtils.sendDelegatorNotificationBondingPeriodHasEnded( + subscriber, + currentRoundId + ) ) - ) + } + } catch (err) { + console.error(`[Notificate-After-Bonding-Period-Has-Ended] - error: ${err}`) + continue } } console.log(