Email Services & Business Logic
Email Services & Business Logic
Relevant source files
The following files were used as context for generating this wiki page:
The EmailSender microservice relies on three core service classes to handle the lifecycle of an email, from template rendering and provider dispatch to delivery tracking and service discovery. These services encapsulate the business logic required to transform raw NATS requests into formatted communications and maintain an audit trail in the PostgreSQL database.
EmailService: Rendering & Dispatch
The EmailService is the primary orchestrator for outgoing communications. It performs a multi-step process: fetching configuration, retrieving and interpolating templates, dispatching to the external provider, and logging the transaction.
Implementation Details
- Configuration Lookup: It queries
emailsender.email_configto retrieve thefrom_email,from_name, andreply_tosettings for the Brevo provider [emailsender/src/services/email-service.ts:25-33]. - Template Retrieval: It fetches the specific template from
emailsender.email_templatesbased on thetemplateCodeandlanguageIsoprovided in the request [emailsender/src/services/email-service.ts:36-45]. - Handlebars Rendering: The service uses
handlebarsto compile and execute thesubject,body_html, andbody_textfields using thevariablesobject passed in the request [emailsender/src/services/email-service.ts:48-54]. - Provider Dispatch: It transforms the internal
SendEmailRequestinto aBrevoEmailRequestand invokes theBrevoClient[emailsender/src/services/email-service.ts:57-69]. - Audit Logging: Every attempt (success or failure) is recorded in
emailsender.email_templates_communication_logwith the interpolated message content and provider message IDs [emailsender/src/services/email-service.ts:72-117].
Data Flow: Send Email Request
This diagram bridges the SendEmailRequest type to the EmailService.sendEmail implementation.
Code
Sources: [emailsender/src/services/email-service.ts:20-128], [emailsender/src/nats/types.ts:1-20]
WebhookService: Delivery Tracking
The WebhookService handles asynchronous updates from email providers regarding the delivery status of sent messages (e.g., delivered, opened, bounced).
Delivery Status Flow
When a webhook hits the HTTP server, the WebhookService performs the following:
- Provider Validation: Ensures the provider is supported (currently limited to
brevo) [emailsender/src/services/webhook-service.ts:19-21]. - Payload Extraction: Extracts the
message-idandeventfrom the provider's JSON payload [emailsender/src/services/webhook-service.ts:23-39]. - Status Mapping: Uses
BrevoClient.mapStatus()to convert provider-specific event strings into internal system statuses [emailsender/src/services/webhook-service.ts:42]. - Log Update: Updates the
emailsender.email_templates_communication_logtable where theprovider_message_idmatches, updating thestatusandstatus_changed_attimestamp [emailsender/src/services/webhook-service.ts:48-53].
Code
Sources: [emailsender/src/services/webhook-service.ts:18-57], [emailsender/src/providers/brevo.ts:60-75]
ServiceRegistration: Heartbeat & Discovery
The ServiceRegistration class ensures that the EmailSender service is visible to the rest of the microservices ecosystem by managing its entry in the central service registry.
Registration Logic
- Upsert Pattern: On startup,
register()checkspublic.service_registry. If theSERVICE_CODE(default:EMAILSENDER) exists, it updates thebase_urlandendpoints(webhook and health). Otherwise, it inserts a new record [emailsender/src/services/service-registration.ts:17-44]. - Heartbeat Mechanism: The
startHeartbeat()method initializes asetInterval(defaulting to 60 seconds) that callsupdateHeartbeat(). This updates theupdated_attimestamp in the registry to signal that the service is alive [emailsender/src/services/service-registration.ts:51-70].
Service Registry Schema Mapping
| Registry Field | Code Source / Logic |
|---|---|
code | process.env.SERVICE_CODE [emailsender/src/services/service-registration.ts:9] |
base_url | process.env.SERVICE_BASE_URL [emailsender/src/services/service-registration.ts:10] |
endpoints | JSON object containing /webhook and /health [emailsender/src/services/service-registration.ts:11-14] |
updated_at | Set to NOW() during every heartbeat [emailsender/src/services/service-registration.ts:56] |
Sources: [emailsender/src/services/service-registration.ts:1-71]