in src/scripts/cronjobs/firstDataBrokerRemovalFixed.tsx [41:119]
async function run() {
const batchSize = Number.parseInt(
process.env.FIRST_DATA_BROKER_REMOVAL_FIXED_EMAIL_BATCH_SIZE ?? "10",
10,
);
if (Number.isNaN(batchSize)) {
throw new Error(
`Could not send first data broker removal fixed emails, because the env var FIRST_DATA_BROKER_REMOVAL_FIXED_EMAIL_BATCH_SIZE has a non-numeric value: [${process.env.FIRST_DATA_BROKER_REMOVAL_FIXED_EMAIL_BATCH_SIZE}].`,
);
}
const potentialSubscribersToEmail =
await getPotentialSubscribersWaitingForFirstDataBrokerRemovalFixedEmail();
const subscribersToEmailWithData = (
await Promise.allSettled(
potentialSubscribersToEmail.map(async (subscriber) => {
try {
// OneRep suggested not relying on webhooks, but instead to fetch the latest
// data from their API. Thus, let's refresh the data in our DB in real-time:
if (subscriber.onerep_profile_id !== null) {
await refreshStoredScanResults(subscriber.onerep_profile_id);
}
const latestScan = await getScanResultsWithBroker(
subscriber.onerep_profile_id,
hasPremium(subscriber),
);
let firstRemovedScanResult = null;
for (const scanResult of latestScan.results) {
// Consider a scan result if:
if (
// The scan result is not manually resolved...
!scanResult.manually_resolved &&
// ...the scan has been removed...
scanResult.status === "removed" &&
// ...and scan result has been created ealier than the currently
// selected `firstRemovedScanResult`.
(!firstRemovedScanResult ||
(firstRemovedScanResult &&
scanResult.created_at.getTime() <
firstRemovedScanResult.created_at.getTime()))
) {
firstRemovedScanResult = scanResult;
}
}
if (!firstRemovedScanResult) {
return;
}
return { subscriber, firstRemovedScanResult };
} catch {
console.error(
`An error ocurred while attemting to get the first removed scan result for subscriber: ${subscriber.id}`,
);
}
}),
)
)
.filter(isFulfilledResult)
.slice(0, batchSize);
await initEmail();
await Promise.allSettled(
subscribersToEmailWithData.map((data) => {
return sendFirstDataBrokerRemovalFixedActivityEmail(
data.value.subscriber,
data.value.firstRemovedScanResult,
);
}),
);
console.log(
`[${new Date(Date.now()).toISOString()}] Sent [${subscribersToEmailWithData.length}] first data broker removal fixed emails.`,
);
tearDown();
}