Migrate contact_email and contact_phone to allow dupes

The key item here is that comms.phone and comms.email are meant to
represent a real global namespace, but comms.contact is meant to
represent an organization-specific namespace. This means the mapping,
comms.contact_phone and comms.contact_email can't key off the global
namespace. Otherwise the contact namespace would implicitly be global.
This commit is contained in:
Eli Ribble 2026-05-22 15:13:03 +00:00
parent a72f228e4e
commit f957dc6982
No known key found for this signature in database
14 changed files with 250 additions and 44 deletions

View file

@ -7,12 +7,10 @@ import (
"time"
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/nyaruka/phonenumbers"
"github.com/rs/zerolog/log"
"source.gleipnir.technology/Gleipnir/nidus-sync/config"
"source.gleipnir.technology/Gleipnir/nidus-sync/db"
"source.gleipnir.technology/Gleipnir/nidus-sync/db/enums"
modelcomms "source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/comms/model"
modelpublicreport "source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/publicreport/model"
"source.gleipnir.technology/Gleipnir/nidus-sync/db/models"
@ -33,30 +31,29 @@ func HandleTextMessage(ctx context.Context, source string, destination string, c
if err != nil {
return fmt.Errorf("parse destination '%s': %w", destination, err)
}
txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil)
txn, err := db.BeginTxn(ctx)
if err != nil {
return fmt.Errorf("start txn: %w", err)
}
defer lint.LogOnErrRollback(txn.Rollback, ctx, "rollback")
status, err := phoneStatus(ctx, *src)
s, err := ensurePhoneInDB(ctx, txn, src)
if err != nil {
return fmt.Errorf("Failed to get phone status")
return fmt.Errorf("ensure source in DB: %w", err)
}
is_visible_to_llm := status != enums.CommsPhonestatustypeUnconfirmed
is_visible_to_llm := s.Status != modelcomms.Phonestatustype_Unconfirmed
l, err := models.CommsTextLogs.Insert(&models.CommsTextLogSetter{
//ID:
Content: omit.From(content),
Created: omit.From(time.Now()),
Destination: omit.From(dst.PhoneString()),
IsVisibleToLLM: omit.From(is_visible_to_llm),
IsWelcome: omit.From(false),
Origin: omit.From(enums.CommsTextoriginCustomer),
Source: omit.From(src.PhoneString()),
TwilioSid: omitnull.FromPtr[string](nil),
TwilioStatus: omit.From(""),
}).One(ctx, txn)
l, err := querycomms.TextLogInsert(ctx, txn, modelcomms.TextLog{
Content: content,
Created: time.Now(),
Destination: dst.PhoneString(),
IsVisibleToLlm: is_visible_to_llm,
IsWelcome: false,
Origin: modelcomms.Textorigin_Customer,
Source: s.E164,
TwilioSid: nil,
TwilioStatus: "",
})
if err != nil {
return fmt.Errorf("insert text log: %w", err)
}