On Sociedades Inactivas, a visitor can choose an already incorporated Spanish company and ask about it, or send a general enquiry. Once they press Send, the application needs to keep their request and notify the people who will handle it.
Those two jobs can finish differently. The database might save the message while email fails. If the visitor sees only a failure and sends again, the same request can arrive twice.
I separated the stored enquiry from its notification. In the implemented flow, success means the visitor has a stored reference. Email delivery has its own state and can be retried. This behavior is tested locally; production form activation is still pending.
Follow one request when email fails
The code handles this sequence. It is an example of the failure behavior, not a report of a particular customer losing a message:
| Event | What the application keeps |
|---|---|
| Valid form arrives | Enquiry and pending-delivery record are saved together |
| First email attempt fails | The same enquiry remains saved; delivery stays pending |
| Visitor repeats the same request | The original reference is returned |
| Same request key arrives with changed content | A conflict is returned instead of silently using the old message |
The pending-delivery record is an outbox: notification work stored in the database for a later attempt. The enquiry and that work are written in one transaction, so they succeed or roll back together.
One test deliberately makes the outbox insert fail. The enquiry and its rate-limit increments must roll back as well. Otherwise the database could contain a request with no notification work attached to it.
The database commits before the email attempt
- 01Submission keyCheck the key and payload fingerprint
- 02TransactionSave enquiry and delivery record together
- 03SMTP attemptClaim the record with a lease token
- 04Saved referenceReturn it even if email fails
Sending again should not create a different request
The browser includes a submission key. The server keeps a hash of that key and a fingerprint of the validated message content.
If the same key arrives with the same content, the server returns the original reference before charging the submission quota again. If the key is reused with different content, it returns HTTP 409. The visitor's first message is not silently substituted for an edited one.
A company selected in the form is checked against the current catalogue inside the transaction. The stored company name comes from that lookup. The browser cannot supply an arbitrary name and have it treated as a catalogue entry.
If the company has been removed or sold, the visitor can remove the selection and send a general enquiry. That leaves a way to ask a question without accepting an outdated company choice.
Saving is finished before delivery begins
After the commit, the API waits for its first email attempt. A mail error is caught while the saved reference remains available.
A retrying worker first claims the delivery record with a lease token. That temporary claim lasts 60 seconds. Other workers should leave the record alone while the lease is current. A failed attempt is rescheduled with increasing delays, capped at six hours. The final update checks that the worker still holds the matching token.
This controls who can update the pending work, but it does not promise exactly one email. A mail server might accept a message just before its response is lost. A lease might also expire during a slow send. The stored request and confirmed delivery remain separate facts.
All eleven focused tests pass using disposable databases and mocked mail. They cover storage, duplicate requests, rollback and delivery behavior. The deployment record still has the production contact route returning 503 before saving, with security and mail configuration deferred. The result here is a tested implementation, not evidence of successful live enquiries.
Project sources & checks
sociedades_inactivas_new_web: app/api/contact/route.ts, lib/db/contacts.ts, lib/contact/delivery.ts and tests/contact.test.mjs.
All eleven existing contact tests passed locally, using test databases and mocked delivery.
docs/PROJECT-STATE.md records the production contact route returning 503 before storage, with security configuration and mail setup deferred.