fix: ensure destination DID is in phone table before text_log insert #107

Closed
ned wants to merge 0 commits from fix/incoming-text-missing-destination-phone into main
Member

Root cause: When an incoming SMS/MMS arrives via the VoIP.ms webhook, HandleTextMessage calls ensurePhoneInDB for the source (customer's number from VoIP.ms from field) but not for the destination (our DID from VoIP.ms to field). The text_log.destination column has a FK constraint text_log_destination_fkey → phone(e164), so the insert fails with SQLSTATE 23503 when the DID hasn't been pre-registered.

Fix: Add ensurePhoneInDB for the destination number before the text_log insert, matching the pattern already used for the source.

Closes #106

**Root cause:** When an incoming SMS/MMS arrives via the VoIP.ms webhook, `HandleTextMessage` calls `ensurePhoneInDB` for the *source* (customer's number from VoIP.ms `from` field) but not for the *destination* (our DID from VoIP.ms `to` field). The `text_log.destination` column has a FK constraint `text_log_destination_fkey → phone(e164)`, so the insert fails with SQLSTATE 23503 when the DID hasn't been pre-registered. **Fix:** Add `ensurePhoneInDB` for the destination number before the `text_log` insert, matching the pattern already used for the source. Closes #106
fix: ensure destination DID is registered in phone table before inserting text_log
All checks were successful
/ golint (push) Successful in 1m1s
/ pnpm-build (push) Successful in 27s
f232664cd2
When an incoming SMS/MMS arrives via the VoIP.ms webhook,
HandleTextMessage only calls ensurePhoneInDB for the source
(the customer's number), but not for the destination (our DID).
The text_log.destination column has a foreign key constraint
referencing phone(e164), so the insert fails with a 23503 error
when the DID has not been pre-registered.

The fix adds an ensurePhoneInDB call for the destination before
the text_log insert, using PhoneInsertIfNotExists which is safe
to call even when the number already exists.
fix: ensure source and destination are in phone table for all outbound texts
All checks were successful
/ golint (push) Successful in 56s
/ pnpm-build (push) Successful in 27s
3211570912
The sendTextDirect function is the common path for every outgoing text
(command responses, initial texts, district messages via job
completion).  It uses config.PhoneNumberReportStr as the text_log.source
field, but never verifies that number exists in the phone table before
inserting.  The text_log.source column has a foreign key constraint
referencing phone(e164), so the insert fails with a 23503 error when
the outbound number hasn't been pre-registered.

The HandleTextMessage path (incoming texts) handles this correctly with
explicit ensurePhoneInDB calls, but respondText / sendTextCommandResponse
go through sendTextDirect, which skips the check.

The fix adds ensurePhoneInDB for both source and destination inside
sendTextDirect, so every outbound text path is covered regardless of
how the function is called.
Owner

Merged in a different way.

Merged in a different way.
eliribble closed this pull request 2026-07-13 21:58:02 +00:00
All checks were successful
/ golint (push) Successful in 56s
/ pnpm-build (push) Successful in 27s

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Gleipnir/nidus-sync!107
No description provided.