# PostgreSQL Email Statement Workflow - Scope Note

## Goal

Build a small n8n workflow that queries an existing PostgreSQL table, groups records by recipient or pair, formats an email statement, and sends through a third-party email service.

## First Paid Slice

- Confirm table schema and date range.
- Write one parameterized query for the weekly run.
- Group records in a JavaScript Code node.
- Generate one HTML email template.
- Send in dry-run mode to an internal test inbox.
- Log each attempted send with status, recipient, record count, and error message.

## Guardrails

- No credentials hardcoded in nodes.
- Dry-run mode before live send.
- Duplicate prevention key: `statement_type + period_start + period_end + recipient`.
- Empty result path sends no client email and logs a skipped run.
- Failed email service response writes to an error log for retry.

## Test Matrix

| Case | Expected result |
| --- | --- |
| Normal weekly records | One email per group |
| No records | Log skipped run |
| Missing recipient email | Flag group for review |
| Duplicate run | Skip or overwrite based on approved policy |
| Email API failure | Log failure and expose retry |

## Handover

- Workflow export or implementation notes.
- SQL query.
- Email template.
- Test results.
- Maintenance note for changing fields or recipients.
