**`export_job.py` is not mergeable as written, but it is close: four small fixes (items 1–4) are needed before tomorrow, and the rest can follow.** The structure is clean and the query is parameterised and has a correct half-open date window. The problems are almost all in how the job behaves when something goes wrong. I read the file only; I did not run it or open `test_export_job.py`.

## Must fix before merge

1. **A failed export looks like a success (lines 56–57).** `run()` catches every exception, logs one line, and exits with code 0. For example, if the finance endpoint returns 503, cron sees a successful run and finance silently gets no file for that day. Use `log.exception(...)` and re-raise or `sys.exit(1)`.

2. **No logging is configured, so the success logs go nowhere (line 13).** Nothing calls `logging.basicConfig`, so both `log.info` lines are dropped. The `log.error` line reaches stderr only as a bare message with no timestamp or traceback. Configure logging in the `__main__` block.

3. **The upload has no timeout (line 42).** If the finance endpoint accepts the connection and stalls, `requests.post` blocks forever. The job never finishes, and it holds the database connection open in a transaction the whole time. Add `timeout=(10, 120)` or similar, and close the connection before uploading.

4. **Customer emails are written to the log (line 52).** `[row[1] for row in rows]` puts every paying customer's email address into the log once logging is configured. Log the count and the day only.

## Should fix soon

5. **A missed day cannot be re-exported.** `run()` always uses "yesterday" and takes no date argument. If the 14 November run fails, the 15 November run exports only the 14th's successor and the missed day is never sent. Add a `--date` argument.

6. **Tests cover only `write_csv`.** By the docstring, they check the header and one row. Nothing covers the date window in `fetch_invoices`, an upload failure, or `run()`. The failure path in item 1 is the one most worth a test.

7. **`FINANCE_UPLOAD_URL` is read at import time (line 16).** Importing the module without that variable set raises `KeyError`, including from tests. Read it inside `upload()` or `run()`, as `DATABASE_URL` already is.

8. **"Yesterday" depends on two clocks (lines 28, 47).** `date.today()` uses the host's timezone, while the date is turned into midnight in the database session's timezone. If the host is on US Eastern and the database on UTC, an invoice paid at 21:00 Eastern on the 13th lands in the file for the 14th. Pick one timezone explicitly and build timezone-aware bounds.

9. **`amount_cents / 100` assumes two decimal places (line 37).** If JPY is stored in whole yen, a ¥5000 invoice exports as 50.0. Formatting is also inconsistent: 1000 cents becomes `10.0` and 1950 becomes `19.5`. Confirm the storage convention and format with `Decimal`.

10. **Export files with customer emails pile up in `/var/exports` (lines 15, 48).** They are never deleted and get default permissions, usually world-readable. A failed run also leaves a partial file behind. Set mode `0o600` and add a retention rule.

## Minor

11. **Spreadsheet formula injection (line 37).** An email such as `=cmd@example.com` or `+tag@example.com` is treated as a formula when finance opens the CSV in Excel. Prefix such cells with `'`.
12. **No retry on upload.** One transient network error loses the night's export.
13. **Inconsistent error handling (line 49).** `psycopg2.connect` sits outside the `try`, so a connection failure crashes with a traceback while every other failure is swallowed.
14. **A NULL `amount_cents` aborts the whole file (line 37).** One bad row raises `TypeError` and nothing is exported.
15. **No explicit encoding (line 33).** `open(path, "w")` uses the locale's encoding, which can differ under cron; pass `encoding="utf-8"`.
16. **`fetchall()` loads every row into memory (line 29).** Fine at current volume; a server-side cursor would scale better.
17. **`EXPORT_DIR` is hard-coded and not checked for existence (line 15).**
18. **`paid_at.isoformat()` may omit the UTC offset (line 37).** This happens if the column is a timestamp without time zone.
19. **Empty days still upload a header-only file.** Confirm finance wants that.
20. **Backdated payments are missed.** An invoice marked PAID after the run with an earlier `paid_at` is never exported. This is only a concern if `paid_at` can be set retroactively.
21. **No lock against overlapping runs, and no type hints or function docstrings.**

VERDICT: READY WITH CHANGES
