Move properties of phones to the phone, not contact_phone

This makes sense because there will naturally be cases where multiple
districts have the same phone number mapped to different contacts.
This commit is contained in:
Eli Ribble 2026-05-22 20:56:22 +00:00
parent f957dc6982
commit 7b04822a9b
No known key found for this signature in database
14 changed files with 143 additions and 104 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"source.gleipnir.technology/Gleipnir/nidus-sync/db"
modelcomms "source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/comms/model"
querycomms "source.gleipnir.technology/Gleipnir/nidus-sync/db/query/comms"
"source.gleipnir.technology/Gleipnir/nidus-sync/platform/types"
)
@ -22,6 +23,20 @@ func ContactSimplesFromIDs(ctx context.Context, txn db.Ex, contact_ids []int64)
if err != nil {
return nil, fmt.Errorf("contact phones from ids: %w", err)
}
e164s := make([]string, 0)
for _, v := range contact_phones {
for _, p := range v {
e164s = append(e164s, p.E164)
}
}
phones, err := querycomms.PhonesFromE164s(ctx, txn, e164s)
if err != nil {
return nil, fmt.Errorf("contact phones from ids: %w", err)
}
phones_by_e164 := make(map[string]modelcomms.Phone, 0)
for _, p := range phones {
phones_by_e164[p.E164] = p
}
results := make([]types.ContactSimple, len(contact_ids))
for i, contact := range contacts {
@ -29,7 +44,7 @@ func ContactSimplesFromIDs(ctx context.Context, txn db.Ex, contact_ids []int64)
if !ok {
return nil, fmt.Errorf("no emails for contact %d", contact.ID)
}
phones, ok := contact_phones[int64(contact.ID)]
cps, ok := contact_phones[int64(contact.ID)]
if !ok {
return nil, fmt.Errorf("no phones for contact %d", contact.ID)
}
@ -38,8 +53,9 @@ func ContactSimplesFromIDs(ctx context.Context, txn db.Ex, contact_ids []int64)
email_string = emails[0].Address
}
phone_simple := types.PhoneSimple{}
if len(phones) > 0 {
phone := phones[0]
if len(cps) > 0 {
contact_phone := cps[0]
phone := phones_by_e164[contact_phone.E164]
phone_simple = types.PhoneSimple{
CanSMS: phone.CanSms,
Number: phone.E164,

View file

@ -159,18 +159,24 @@ func saveReporterEmail(ctx context.Context, txn db.Ex, contact_id int32, email_a
return nil
}
func saveReporterPhone(ctx context.Context, txn db.Ex, contact_id int32, phone *types.E164, can_sms bool) error {
if phone == nil {
func saveReporterPhone(ctx context.Context, txn db.Ex, contact_id int32, number *types.E164, can_sms bool) error {
if number == nil {
return nil
}
p, err := querycomms.ContactPhoneInsert(ctx, txn, modelcomms.ContactPhone{
_, err := querycomms.PhoneInsertIfNotExists(ctx, txn, modelcomms.Phone{
E164: number.PhoneString(),
CanSms: can_sms,
ConfirmedMessageID: nil,
ContactID: contact_id,
E164: phone.PhoneString(),
IsSubscribed: false,
StopMessageID: nil,
})
if err != nil {
return fmt.Errorf("insert phone if not exists: %w", err)
}
p, err := querycomms.ContactPhoneInsert(ctx, txn, modelcomms.ContactPhone{
ContactID: contact_id,
E164: number.PhoneString(),
IsSubscribed: false,
})
if err != nil {
return fmt.Errorf("contact add phone: %w", err)
}