Skip to content

Commit

Permalink
fix: let campaignId be unused
Browse files Browse the repository at this point in the history
  • Loading branch information
KishenKumarrrrr committed Oct 3, 2024
1 parent 2cc4ccb commit 6fdece7
Showing 1 changed file with 96 additions and 92 deletions.
188 changes: 96 additions & 92 deletions backend/src/email/utils/callback/query.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { QueryTypes, Op, cast, fn } from 'sequelize'
// import { QueryTypes, Op, cast, fn } from 'sequelize'
import { Op } from 'sequelize'
import {
UpdateMessageWithErrorMetadata,
Metadata,
} from '@email/interfaces/callback.interface'
import { EmailBlacklist, EmailMessage } from '@email/models'
import config from '@core/config'
// import config from '@core/config'
import { loggerWithLabel } from '@core/logger'
import { Campaign } from '@core/models'
// import { Campaign } from '@core/models'

const logger = loggerWithLabel(module)

Expand Down Expand Up @@ -134,98 +135,101 @@ export const updateMessageWithRead = async (
}

export const haltCampaignIfThresholdExceeded = async (
campaignId?: number
_campaignId?: number
): Promise<void> => {
if (campaignId === undefined) {
return
}
// This a stop gap because the counting is overloading the DB
return

// Compute threshold for Hard bounces
// Your bounce rate includes only hard bounces to domains you haven't verified.
// Source: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/faqs-enforcement.html#e-faq-bn
const [result] = (await EmailMessage.findAll({
raw: true,
where: { campaignId, status: { [Op.ne]: null } },
attributes: [
[fn('sum', cast({ error_code: 'Hard bounce' }, 'int')), 'invalid'],
[fn('count', 1), 'running_total'],
],
useMaster: false,
})) as any[]
const {
invalid,
running_total: runningTotal,
}: { invalid?: number; running_total?: number } = result
// if (campaignId === undefined) {
// return
// }

if (invalid !== undefined && runningTotal !== undefined) {
const percentageInvalid = invalid / runningTotal
const exceedsHaltNumber =
invalid > config.get('emailCallback.minHaltNumber')
const exceedsHaltPercentage =
percentageInvalid > config.get('emailCallback.minHaltPercentage')
// // Compute threshold for Hard bounces
// // Your bounce rate includes only hard bounces to domains you haven't verified.
// // Source: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/faqs-enforcement.html#e-faq-bn
// const [result] = (await EmailMessage.findAll({
// raw: true,
// where: { campaignId, status: { [Op.ne]: null } },
// attributes: [
// [fn('sum', cast({ error_code: 'Hard bounce' }, 'int')), 'invalid'],
// [fn('count', 1), 'running_total'],
// ],
// useMaster: false,
// })) as any[]
// const {
// invalid,
// running_total: runningTotal,
// }: { invalid?: number; running_total?: number } = result

logger.info({
message: 'Current campaign status',
campaignId,
invalid,
runningTotal,
percentageInvalid,
minHaltNumber: config.get('emailCallback.minHaltNumber'),
minHaltPercentage: config.get('emailCallback.minHaltPercentage'),
exceedsHaltNumber,
exceedsHaltPercentage,
action: 'haltCampaignIfThresholdExceeded',
})
/* With default MIN_HALT_NUMBER=10, MIN_HALT_PERCENTAGE=0.1,
it means that
- if there were 11 messages sent thus far, and 11 invalid recipients, the campaign would halt immediately since 11/11 > MIN_HALT_PERCENTAGE
- if there were 110 messages sent thus far, and 11 invalid recipients, the campaign would not halt, since 11/110 <= MIN_HALT_PERCENTAGE
*/
if (exceedsHaltNumber && exceedsHaltPercentage) {
// Halt
try {
await EmailBlacklist?.sequelize?.transaction(async (transaction) => {
const [numUpdated] = await Campaign.update(
{ halted: true },
{ where: { id: campaignId, halted: false }, transaction }
)
if (numUpdated !== 1) {
logger.info({
message:
'Campaign has already been halted, or forcefully overridden with null to prevent halting',
campaignId,
action: 'haltCampaignIfThresholdExceeded',
})
return
} else {
logger.info({
message: 'Successfully halted campaign',
campaignId,
invalid,
runningTotal,
percentageInvalid,
action: 'haltCampaignIfThresholdExceeded',
})
}
// if (invalid !== undefined && runningTotal !== undefined) {
// const percentageInvalid = invalid / runningTotal
// const exceedsHaltNumber =
// invalid > config.get('emailCallback.minHaltNumber')
// const exceedsHaltPercentage =
// percentageInvalid > config.get('emailCallback.minHaltPercentage')

await EmailBlacklist?.sequelize?.query(
`SELECT stop_jobs(:campaignId)`,
{
replacements: { campaignId },
type: QueryTypes.SELECT,
transaction,
}
)
})
} catch (err) {
logger.error({
message: 'Failed to halt campaign',
campaignId,
error: err,
action: 'haltCampaignIfThresholdExceeded',
})
}
}
}
return
// logger.info({
// message: 'Current campaign status',
// campaignId,
// invalid,
// runningTotal,
// percentageInvalid,
// minHaltNumber: config.get('emailCallback.minHaltNumber'),
// minHaltPercentage: config.get('emailCallback.minHaltPercentage'),
// exceedsHaltNumber,
// exceedsHaltPercentage,
// action: 'haltCampaignIfThresholdExceeded',
// })
// /* With default MIN_HALT_NUMBER=10, MIN_HALT_PERCENTAGE=0.1,
// it means that
// - if there were 11 messages sent thus far, and 11 invalid recipients, the campaign would halt immediately since 11/11 > MIN_HALT_PERCENTAGE
// - if there were 110 messages sent thus far, and 11 invalid recipients, the campaign would not halt, since 11/110 <= MIN_HALT_PERCENTAGE
// */
// if (exceedsHaltNumber && exceedsHaltPercentage) {
// // Halt
// try {
// await EmailBlacklist?.sequelize?.transaction(async (transaction) => {
// const [numUpdated] = await Campaign.update(
// { halted: true },
// { where: { id: campaignId, halted: false }, transaction }
// )
// if (numUpdated !== 1) {
// logger.info({
// message:
// 'Campaign has already been halted, or forcefully overridden with null to prevent halting',
// campaignId,
// action: 'haltCampaignIfThresholdExceeded',
// })
// return
// } else {
// logger.info({
// message: 'Successfully halted campaign',
// campaignId,
// invalid,
// runningTotal,
// percentageInvalid,
// action: 'haltCampaignIfThresholdExceeded',
// })
// }

// await EmailBlacklist?.sequelize?.query(
// `SELECT stop_jobs(:campaignId)`,
// {
// replacements: { campaignId },
// type: QueryTypes.SELECT,
// transaction,
// }
// )
// })
// } catch (err) {
// logger.error({
// message: 'Failed to halt campaign',
// campaignId,
// error: err,
// action: 'haltCampaignIfThresholdExceeded',
// })
// }
// }
// }
// return
}

0 comments on commit 6fdece7

Please sign in to comment.