Ensure phone numbers are in the DB before adding pool
This commit is contained in:
parent
ab9f16505e
commit
ff1cd00c96
4 changed files with 20 additions and 11 deletions
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||||
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
||||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
"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/Gleipnir-Technology/nidus-sync/userfile"
|
||||||
"github.com/aarondl/opt/omit"
|
"github.com/aarondl/opt/omit"
|
||||||
"github.com/aarondl/opt/omitnull"
|
"github.com/aarondl/opt/omitnull"
|
||||||
|
|
@ -146,7 +147,13 @@ func ProcessJob(ctx context.Context, file_id int32) error {
|
||||||
case headerPropertyOwnerName:
|
case headerPropertyOwnerName:
|
||||||
setter.PropertyOwnerName = omit.From(col)
|
setter.PropertyOwnerName = omit.From(col)
|
||||||
case headerPropertyOwnerPhone:
|
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:
|
case headerResidentOwned:
|
||||||
boolValue, err := parseBool(col)
|
boolValue, err := parseBool(col)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ func RegisterNotificationPhone(ctx context.Context, txn bob.Tx, report_id string
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
e := text.EnsureInDB(ctx, phone)
|
e := text.EnsureInDB(ctx, db.PGInstance.BobDB, phone)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return newInternalError(e, "Failed to ensure phone is in DB")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
func RegisterSubscriptionPhone(ctx context.Context, txn bob.Tx, phone text.E164) *ErrorWithCode {
|
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 {
|
if e != nil {
|
||||||
return newInternalError(e, "Failed to ensure phone is in DB")
|
return newInternalError(e, "Failed to ensure phone is in DB")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||||
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
||||||
"github.com/nyaruka/phonenumbers"
|
"github.com/nyaruka/phonenumbers"
|
||||||
//"github.com/rs/zerolog/log"
|
//"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")
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to ensure text message destination is in the DB: %w", err)
|
return fmt.Errorf("Failed to ensure text message destination is in the DB: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Gleipnir-Technology/bob"
|
||||||
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
||||||
"github.com/Gleipnir-Technology/bob/dialect/psql/um"
|
"github.com/Gleipnir-Technology/bob/dialect/psql/um"
|
||||||
"github.com/Gleipnir-Technology/nidus-sync/comms/text"
|
"github.com/Gleipnir-Technology/nidus-sync/comms/text"
|
||||||
|
|
@ -23,8 +24,8 @@ import (
|
||||||
|
|
||||||
type E164 = phonenumbers.PhoneNumber
|
type E164 = phonenumbers.PhoneNumber
|
||||||
|
|
||||||
func EnsureInDB(ctx context.Context, destination E164) (err error) {
|
func EnsureInDB(ctx context.Context, ex bob.Executor, destination E164) (err error) {
|
||||||
return ensureInDB(ctx, PhoneString(destination))
|
return ensureInDB(ctx, ex, PhoneString(destination))
|
||||||
}
|
}
|
||||||
func HandleTextMessage(src string, dst string, body string) {
|
func HandleTextMessage(src string, dst string, body string) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
@ -111,13 +112,13 @@ func StoreSources() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to parse +1'%s' as phone number: %w", n, err)
|
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 {
|
} else {
|
||||||
dest, err := ParsePhoneNumber(n)
|
dest, err := ParsePhoneNumber(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to parse '%s' as phone number: %w", n, err)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to add number '%s' to DB: %w", n, err)
|
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
|
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)
|
_, err = models.FindCommsPhone(ctx, db.PGInstance.BobDB, destination)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// doesn't exist
|
// doesn't exist
|
||||||
|
|
@ -192,7 +193,7 @@ func ensureInDB(ctx context.Context, destination string) (err error) {
|
||||||
E164: omit.From(destination),
|
E164: omit.From(destination),
|
||||||
IsSubscribed: omit.From(false),
|
IsSubscribed: omit.From(false),
|
||||||
Status: omit.From(enums.CommsPhonestatustypeUnconfirmed),
|
Status: omit.From(enums.CommsPhonestatustypeUnconfirmed),
|
||||||
}).One(ctx, db.PGInstance.BobDB)
|
}).One(ctx, ex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to insert new phone contact: %w", err)
|
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 {
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to ensure text message destination is in the DB: %w", err)
|
return fmt.Errorf("Failed to ensure text message destination is in the DB: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue