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:
parent
f957dc6982
commit
7b04822a9b
14 changed files with 143 additions and 104 deletions
|
|
@ -15,21 +15,17 @@ func EnsureInDB(ctx context.Context, txn db.Ex, contact modelcomms.Contact, dst
|
|||
}
|
||||
func ensureInDB(ctx context.Context, txn db.Ex, contact modelcomms.Contact, destination string) (err error) {
|
||||
contact_phone := modelcomms.ContactPhone{
|
||||
CanSms: true,
|
||||
ConfirmedMessageID: nil,
|
||||
ContactID: contact.ID,
|
||||
E164: destination,
|
||||
IsSubscribed: false,
|
||||
StopMessageID: nil,
|
||||
ContactID: contact.ID,
|
||||
E164: destination,
|
||||
}
|
||||
_, err = querycomms.ContactPhoneInsert(ctx, txn, contact_phone)
|
||||
return err
|
||||
}
|
||||
func ensurePhoneInDB(ctx context.Context, txn db.Ex, number *types.E164) (modelcomms.Phone, error) {
|
||||
return querycomms.PhoneInsertIfNotExists(ctx, txn, modelcomms.Phone{
|
||||
CanSms: false,
|
||||
E164: number.PhoneString(),
|
||||
IsSubscribed: false,
|
||||
Status: modelcomms.Phonestatustype_Unconfirmed,
|
||||
CanSms: false,
|
||||
ConfirmedMessageID: nil,
|
||||
E164: number.PhoneString(),
|
||||
StopMessageID: nil,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ func resendInitialText(ctx context.Context, txn db.Ex, dst types.E164) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("Failed to find phone %s: %w", dst, err)
|
||||
}
|
||||
err = querycomms.ContactPhoneUpdateStopMessageID(ctx, txn, phone.E164, nil)
|
||||
err = querycomms.PhoneUpdateStopMessageID(ctx, txn, phone.E164, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to clear subscription on phone %s: %w", dst, err)
|
||||
}
|
||||
|
|
@ -100,17 +100,14 @@ func sendTextComplete(ctx context.Context, job modelcomms.TextJob) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("destination phone from e164: %w", err)
|
||||
}
|
||||
log.Debug().Str("phone status", string(destination.Status)).Str("destination", job.Destination).Send()
|
||||
switch destination.Status {
|
||||
case modelcomms.Phonestatustype_Unconfirmed:
|
||||
if destination.ConfirmedMessageID == nil {
|
||||
err := ensureInitialText(ctx, txn, *dst)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to ensure initial text has been sent: %w", err)
|
||||
}
|
||||
return nil
|
||||
//case enums.CommsPhonestatustypeOkToSend:
|
||||
// allow to drop through
|
||||
case modelcomms.Phonestatustype_Stopped:
|
||||
}
|
||||
if destination.StopMessageID != nil {
|
||||
lint.LogOnErrCtx(func(ctx context.Context) error {
|
||||
return resendInitialText(ctx, txn, *dst)
|
||||
}, ctx, "resend initial text")
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ func HandleTextMessage(ctx context.Context, source string, destination string, c
|
|||
if err != nil {
|
||||
return fmt.Errorf("ensure source in DB: %w", err)
|
||||
}
|
||||
is_visible_to_llm := s.Status != modelcomms.Phonestatustype_Unconfirmed
|
||||
is_visible_to_llm := s.ConfirmedMessageID != nil
|
||||
|
||||
l, err := querycomms.TextLogInsert(ctx, txn, modelcomms.TextLog{
|
||||
Content: content,
|
||||
|
|
@ -83,17 +83,17 @@ func respondText(ctx context.Context, log_id int32) error {
|
|||
return fmt.Errorf("parse source: %w", err)
|
||||
}
|
||||
|
||||
contact_phone, err := querycomms.ContactPhoneFromE164(ctx, txn, src.PhoneString())
|
||||
phone, err := querycomms.PhoneFromE164(ctx, txn, src.PhoneString())
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to get contact phone")
|
||||
}
|
||||
|
||||
body_l := strings.TrimSpace(strings.ToLower(l.Content))
|
||||
// If the user isn't confirmed for sending regular texts ensure they get a reprompt
|
||||
if contact_phone.ConfirmedMessageID == nil {
|
||||
if phone.ConfirmedMessageID == nil {
|
||||
switch body_l {
|
||||
case "yes":
|
||||
err = querycomms.ContactPhoneUpdateConfirmedMessageID(ctx, txn, src.PhoneString(), &l.ID)
|
||||
err = querycomms.PhoneUpdateConfirmedMessageID(ctx, txn, src.PhoneString(), &l.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set phone confirmed message ID: %w", err)
|
||||
}
|
||||
|
|
@ -119,7 +119,7 @@ func respondText(ctx context.Context, log_id int32) error {
|
|||
}
|
||||
switch body_l {
|
||||
case "stop":
|
||||
err = querycomms.ContactPhoneUpdateStopMessageID(ctx, txn, src.PhoneString(), &l.ID)
|
||||
err = querycomms.PhoneUpdateStopMessageID(ctx, txn, src.PhoneString(), &l.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set phone stop message ID: %w", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue