Relay transactional email through Salesforce instead of SMTP
Our SMTP host's IP reputation gets its mail blocked by allowlist-based receivers (t-online.de). Route voucher confirmations, account activation and password-reset mail through the Salesforce org's MTA + DKIM instead, via a new EmailSendResource Apex REST endpoint. - common/salesforce.client.ts: shared client-credentials access (token cache + retry-once-on-401), extracted from the newsletter sync so it is no longer duplicated - common.mail.nodemailer.ts -> common.mail.ts: posts to the org endpoint, never throws on a delivery failure (logs + returns a boolean), retries once on a transient failure, base64-encodes attachments with a 3 MB cap - drop the nodemailer dependency - fixes activation/reset email failures that previously threw after the transaction had already committed - tickets.confirmation-email.ts: shared confirmation-email builder used by both the public redeem path and a new admin resend action - redemptions gain a confirmation_email_status column (migration 003) so the admin UI can flag a failed send; POST /tickets/admin/redemptions/:id/resend-confirmation rebuilds and resends Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
// resendRedemptionConfirmation rebuilds the confirmation email from stored
|
||||
// redemption data and records the new outcome. Only the resend path is
|
||||
// exercised here; the shared send/record logic is covered by
|
||||
// confirmation-email.test.ts.
|
||||
|
||||
jest.mock('../../src/models/tickets/Tickets.db', () => ({
|
||||
NachklangTicketsDB: {getConnection: jest.fn()}
|
||||
}));
|
||||
jest.mock('../../src/models/tickets/tickets.confirmation-email', () => ({
|
||||
sendRedemptionConfirmation: jest.fn(),
|
||||
recordConfirmationEmailResult: jest.fn()
|
||||
}));
|
||||
|
||||
import {NachklangTicketsDB} from '../../src/models/tickets/Tickets.db';
|
||||
import {recordConfirmationEmailResult, sendRedemptionConfirmation} from '../../src/models/tickets/tickets.confirmation-email';
|
||||
import {resendRedemptionConfirmation} from '../../src/models/tickets/admin/redemptions.admin.service';
|
||||
|
||||
const mockGetConnection = NachklangTicketsDB.getConnection as jest.Mock;
|
||||
const mockSend = sendRedemptionConfirmation as jest.Mock;
|
||||
const mockRecord = recordConfirmationEmailResult as jest.Mock;
|
||||
|
||||
const ACTIVE_ROW = {
|
||||
redemption_id: 5,
|
||||
code: 'ABC123',
|
||||
event_id: 42,
|
||||
status: 'ACTIVE',
|
||||
contact_name: 'Erika Mustermann',
|
||||
contact_email: 'erika@example.com',
|
||||
contact_address: null,
|
||||
guest_count: 2,
|
||||
redeemed_at: new Date('2026-06-01T10:00:00Z'),
|
||||
confirmation_email_status: 'FAILED'
|
||||
};
|
||||
|
||||
// getRedemption issues: 1) SELECT redemptions, 2) SELECT redemption_guests
|
||||
const connFor = (redemptionRows: any[], guestRows: any[] = []) => ({
|
||||
query: jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce(redemptionRows)
|
||||
.mockResolvedValueOnce(guestRows),
|
||||
end: jest.fn().mockResolvedValue(undefined)
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
mockSend.mockResolvedValue(true);
|
||||
});
|
||||
|
||||
describe('resendRedemptionConfirmation', () => {
|
||||
it('returns NOT_FOUND when the redemption does not exist', async () => {
|
||||
mockGetConnection.mockResolvedValue(connFor([]));
|
||||
|
||||
expect(await resendRedemptionConfirmation(5)).toBe('NOT_FOUND');
|
||||
expect(mockSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns NOT_ACTIVE for an undone redemption', async () => {
|
||||
mockGetConnection.mockResolvedValue(connFor([{...ACTIVE_ROW, status: 'UNDONE'}]));
|
||||
|
||||
expect(await resendRedemptionConfirmation(5)).toBe('NOT_ACTIVE');
|
||||
expect(mockSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('resends from the stored data and records SENT on success', async () => {
|
||||
mockGetConnection.mockResolvedValue(connFor([ACTIVE_ROW], [{name: 'Erika Mustermann'}, {name: 'Hans Mustermann'}]));
|
||||
|
||||
const result = await resendRedemptionConfirmation(5);
|
||||
|
||||
expect(result).toBe('SENT');
|
||||
expect(mockSend).toHaveBeenCalledWith({
|
||||
eventId: 42,
|
||||
contactName: 'Erika Mustermann',
|
||||
contactEmail: 'erika@example.com',
|
||||
guestNames: ['Erika Mustermann', 'Hans Mustermann']
|
||||
});
|
||||
expect(mockRecord).toHaveBeenCalledWith(5, true);
|
||||
});
|
||||
|
||||
it('records FAILED and returns FAILED when the relay rejects the send', async () => {
|
||||
mockGetConnection.mockResolvedValue(connFor([ACTIVE_ROW], [{name: 'Erika Mustermann'}]));
|
||||
mockSend.mockResolvedValue(false);
|
||||
|
||||
const result = await resendRedemptionConfirmation(5);
|
||||
|
||||
expect(result).toBe('FAILED');
|
||||
expect(mockRecord).toHaveBeenCalledWith(5, false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user