Ensure phone numbers are in the DB before adding pool

This commit is contained in:
Eli Ribble 2026-02-14 05:05:31 +00:00
parent ab9f16505e
commit ff1cd00c96
No known key found for this signature in database
4 changed files with 20 additions and 11 deletions

View file

@ -13,6 +13,7 @@ import (
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/platform/text"
"github.com/Gleipnir-Technology/nidus-sync/userfile"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
@ -146,7 +147,13 @@ func ProcessJob(ctx context.Context, file_id int32) error {
case headerPropertyOwnerName:
setter.PropertyOwnerName = omit.From(col)
case headerPropertyOwnerPhone:
setter.PropertyOwnerPhone = omitnull.From(col)
phone, err := text.ParsePhoneNumber(col)
if err != nil {
addError(ctx, txn, c, int32(row_number), int32(i), fmt.Sprintf("'%s' is not a phone number that we recognize. Ideally it should be of the form '+12223334444'", col))
continue
}
text.EnsureInDB(ctx, txn, *phone)
setter.PropertyOwnerPhone = omitnull.From(text.PhoneString(*phone))
case headerResidentOwned:
boolValue, err := parseBool(col)
if err != nil {

View file

@ -88,7 +88,7 @@ func RegisterNotificationPhone(ctx context.Context, txn bob.Tx, report_id string
if err != nil {
return err
}
e := text.EnsureInDB(ctx, phone)
e := text.EnsureInDB(ctx, db.PGInstance.BobDB, phone)
if e != nil {
return newInternalError(e, "Failed to ensure phone is in DB")
}
@ -120,7 +120,7 @@ func RegisterSubscriptionEmail(ctx context.Context, txn bob.Tx, destination stri
return nil
}
func RegisterSubscriptionPhone(ctx context.Context, txn bob.Tx, phone text.E164) *ErrorWithCode {
e := text.EnsureInDB(ctx, phone)
e := text.EnsureInDB(ctx, db.PGInstance.BobDB, phone)
if e != nil {
return newInternalError(e, "Failed to ensure phone is in DB")
}

View file

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
"github.com/nyaruka/phonenumbers"
//"github.com/rs/zerolog/log"
@ -48,7 +49,7 @@ func sendReportSubscription(ctx context.Context, job Job) error {
return fmt.Errorf("job is not for report subscription confirmation")
}
err := ensureInDB(ctx, job.destination())
err := ensureInDB(ctx, db.PGInstance.BobDB, job.destination())
if err != nil {
return fmt.Errorf("Failed to ensure text message destination is in the DB: %w", err)
}

View file

@ -6,6 +6,7 @@ import (
"strings"
"time"
"github.com/Gleipnir-Technology/bob"
"github.com/Gleipnir-Technology/bob/dialect/psql"
"github.com/Gleipnir-Technology/bob/dialect/psql/um"
"github.com/Gleipnir-Technology/nidus-sync/comms/text"
@ -23,8 +24,8 @@ import (
type E164 = phonenumbers.PhoneNumber
func EnsureInDB(ctx context.Context, destination E164) (err error) {
return ensureInDB(ctx, PhoneString(destination))
func EnsureInDB(ctx context.Context, ex bob.Executor, destination E164) (err error) {
return ensureInDB(ctx, ex, PhoneString(destination))
}
func HandleTextMessage(src string, dst string, body string) {
ctx := context.Background()
@ -111,13 +112,13 @@ func StoreSources() error {
if err != nil {
return fmt.Errorf("Failed to parse +1'%s' as phone number: %w", n, err)
}
err = EnsureInDB(ctx, *dest)
err = EnsureInDB(ctx, db.PGInstance.BobDB, *dest)
} else {
dest, err := ParsePhoneNumber(n)
if err != nil {
return fmt.Errorf("Failed to parse '%s' as phone number: %w", n, err)
}
err = EnsureInDB(ctx, *dest)
err = EnsureInDB(ctx, db.PGInstance.BobDB, *dest)
}
if err != nil {
return fmt.Errorf("Failed to add number '%s' to DB: %w", n, err)
@ -183,7 +184,7 @@ func sendInitialText(ctx context.Context, src string, dst string) error {
return nil
}
func ensureInDB(ctx context.Context, destination string) (err error) {
func ensureInDB(ctx context.Context, ex bob.Executor, destination string) (err error) {
_, err = models.FindCommsPhone(ctx, db.PGInstance.BobDB, destination)
if err != nil {
// doesn't exist
@ -192,7 +193,7 @@ func ensureInDB(ctx context.Context, destination string) (err error) {
E164: omit.From(destination),
IsSubscribed: omit.From(false),
Status: omit.From(enums.CommsPhonestatustypeUnconfirmed),
}).One(ctx, db.PGInstance.BobDB)
}).One(ctx, ex)
if err != nil {
return fmt.Errorf("Failed to insert new phone contact: %w", err)
}
@ -330,7 +331,7 @@ func loadPreviousMessagesForLLM(ctx context.Context, dst, src string) ([]llm.Mes
}
func sendText(ctx context.Context, source string, destination string, message string, origin enums.CommsTextorigin, is_welcome bool, is_visible_to_llm bool) error {
err := ensureInDB(ctx, destination)
err := ensureInDB(ctx, db.PGInstance.BobDB, destination)
if err != nil {
return fmt.Errorf("Failed to ensure text message destination is in the DB: %w", err)
}