Skip to content

Commit 47e65a0

Browse files
committed
fix: conditionally delete processed bounce emails and clear tracking only when records are successfully matched within the current instance
1 parent 5d67f03 commit 47e65a0

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/features/payload-cms/payload-cms/tasks/fetch-smtp-bounces.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const updateTrackingRecords = async (
9090
isSuccess: boolean,
9191
dsnString: string,
9292
rawEmail: string,
93-
): Promise<void> => {
93+
): Promise<boolean> => {
9494
let outgoingEmail:
9595
| { smtpResults?: unknown[]; formSubmission?: string | { id: string }; rawDsnEmail?: string }
9696
| undefined;
@@ -137,11 +137,12 @@ const updateTrackingRecords = async (
137137
id: envelopeId,
138138
data: { smtpResults: subResults } as Record<string, unknown>,
139139
});
140-
} catch (error: unknown) {
141-
payload.logger.error({
142-
err: error instanceof Error ? error : new Error(String(error)),
143-
msg: `Failed to update record for bounce tracking ID ${envelopeId}`,
140+
return true;
141+
} catch {
142+
payload.logger.info({
143+
msg: `Bounce tracking ID ${envelopeId} not found, likely belongs to another instance`,
144144
});
145+
return false;
145146
}
146147
} else {
147148
const results = Array.isArray(outgoingEmail.smtpResults) ? [...outgoingEmail.smtpResults] : [];
@@ -203,6 +204,7 @@ const updateTrackingRecords = async (
203204
});
204205
}
205206
}
207+
return true;
206208
}
207209
};
208210

@@ -314,19 +316,34 @@ export const fetchSmtpBouncesTask: TaskConfig<'fetchSmtpBounces'> = {
314316
// Standard Payload ID length check (24 chars for ObjectID)
315317
if (envId?.length === 24) {
316318
const { isSuccess, dsnString } = determineDeliveryStatus(parsedEmail);
317-
await updateTrackingRecords(payload, envId, isSuccess, dsnString, String(rawEmail));
318-
logger.info(`Processed bounce for submission/outgoing email ${envId} successfully.`);
319-
}
320-
321-
// Only delete if successfully processed or if it doesn't match our expected ID format
322-
await pop3.DELE(messageId);
323-
324-
// Clear failure tracking if successful
325-
if (trackingRecord?.id !== undefined) {
326-
await payload.delete({
327-
collection: 'smtp-bounce-mail-tracking',
328-
id: trackingRecord.id,
329-
});
319+
const matched = await updateTrackingRecords(
320+
payload,
321+
envId,
322+
isSuccess,
323+
dsnString,
324+
String(rawEmail),
325+
);
326+
327+
if (matched) {
328+
logger.info(`Processed bounce for submission/outgoing email ${envId} successfully.`);
329+
330+
// Only delete if successfully processed and matches an ID in our database
331+
await pop3.DELE(messageId);
332+
333+
// Clear failure tracking if successful
334+
if (trackingRecord?.id !== undefined) {
335+
await payload.delete({
336+
collection: 'smtp-bounce-mail-tracking',
337+
id: trackingRecord.id,
338+
});
339+
}
340+
} else {
341+
logger.info(
342+
`Ignored message ${messageId} as envId ${envId} was not found in this instance.`,
343+
);
344+
}
345+
} else {
346+
logger.info(`Ignored message ${messageId} as it lacks our expected ID format.`);
330347
}
331348
} catch (error: unknown) {
332349
// We isolate individual message failures so the loop continues

0 commit comments

Comments
 (0)