From ff1cd00c9648c8236c7a672f99adf11df884691a Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Sat, 14 Feb 2026 05:05:31 +0000 Subject: [PATCH] Ensure phone numbers are in the DB before adding pool --- platform/csv/pool.go | 9 ++++++++- platform/report/notification.go | 4 ++-- platform/text/report-subscription.go | 3 ++- platform/text/text.go | 15 ++++++++------- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/platform/csv/pool.go b/platform/csv/pool.go index 199e8911..b9590c69 100644 --- a/platform/csv/pool.go +++ b/platform/csv/pool.go @@ -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 { diff --git a/platform/report/notification.go b/platform/report/notification.go index b9f1b83c..11b0cda7 100644 --- a/platform/report/notification.go +++ b/platform/report/notification.go @@ -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") } diff --git a/platform/text/report-subscription.go b/platform/text/report-subscription.go index 921f72b1..19710bf6 100644 --- a/platform/text/report-subscription.go +++ b/platform/text/report-subscription.go @@ -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) } diff --git a/platform/text/text.go b/platform/text/text.go index dec95246..2f2d8383 100644 --- a/platform/text/text.go +++ b/platform/text/text.go @@ -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) }