Skip to content

Commit

Permalink
Adds try catch on sendEmailAfterBondingPeriodHAsEndedNotificationToDe…
Browse files Browse the repository at this point in the history
…legators
  • Loading branch information
Agupane committed Aug 1, 2019
1 parent 8e965dc commit db33642
Showing 1 changed file with 75 additions and 65 deletions.
140 changes: 75 additions & 65 deletions server/helpers/notification/notificateDelegatorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit db33642

Please sign in to comment.