Add contacts, rework comms schema

This in a pretty huge change. At a high level we're adding the concept
of a 'contact' which is a person or organization that has zero or more
contact methods (email, phone). This ended up cascading a number of
changes, including critically to the publicreprt schema. In the end it
seemed safer to get to the point where I'm confident we aren't using any
of the old fields for storing reporter information (though I haven't
deleted the columns yet) so I removed the code for defining those
columns.

At this point I think it's not possible for me to regenerate the bob
schema due to the interdependencies between my various schemas, so the
migration is well-and-truly happening.
This commit is contained in:
Eli Ribble 2026-05-15 16:58:28 +00:00
parent 085935fa66
commit f1fe8b4d2b
No known key found for this signature in database
46 changed files with 1127 additions and 633 deletions

View file

@ -4,20 +4,15 @@ import (
"context"
"fmt"
"github.com/Gleipnir-Technology/bob"
"github.com/Gleipnir-Technology/bob/dialect/psql"
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
//"github.com/Gleipnir-Technology/nidus-sync/db/models"
modelcomms "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/model"
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
//"github.com/rs/zerolog/log"
"github.com/stephenafamo/scan"
)
// Send a message from a district to a public reporter within the context of the public report
func ReportMessage(ctx context.Context, txn bob.Executor, user_id int32, report_id int32, destination types.E164, content string) (*int32, error) {
job_id, err := sendTextBegin(ctx, txn, &user_id, &report_id, destination, content, enums.CommsTextjobtypeReportMessage)
func ReportMessage(ctx context.Context, txn db.Ex, user_id int32, report_id int32, destination types.E164, content string) (*int32, error) {
job_id, err := sendTextBegin(ctx, txn, &user_id, &report_id, destination, content, modelcomms.Textjobtype_ReportMessage)
if err != nil {
return nil, fmt.Errorf("Failed to send initial confirmation: %w", err)
}
@ -25,43 +20,11 @@ func ReportMessage(ctx context.Context, txn bob.Executor, user_id int32, report_
}
// Send a message from the system to a public reporter indicating they are subscribed to updates on the report
func ReportSubscriptionConfirmationText(ctx context.Context, txn bob.Executor, destination types.E164, report_id string) error {
func ReportSubscriptionConfirmationText(ctx context.Context, txn db.Ex, destination types.E164, report_id string) error {
content := fmt.Sprintf("Thanks for submitting mosquito report %s. Text for any questions. We'll send you updates as we get them.", report_id)
_, err := sendTextBegin(ctx, txn, nil, nil, destination, content, enums.CommsTextjobtypeReportConfirmation)
_, err := sendTextBegin(ctx, txn, nil, nil, destination, content, modelcomms.Textjobtype_ReportConfirmation)
if err != nil {
return fmt.Errorf("Failed to send initial confirmation: %w", err)
}
return err
}
type reportIDs struct {
ID int32 `db:"id"`
PublicID string `db:"public_id"`
OrganizationID int32 `db:"organization_id"`
}
// Get the list of reports that are still open for a particular text message recipient
// 'still open' is not well-defined throughout the system, but for now we'll go with
// 'not reviewed in any way'.
func reportsForTextRecipient(ctx context.Context, txn bob.Executor, destination types.E164) ([]reportIDs, error) {
rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
sm.Columns(
"r.id",
"r.public_id",
"r.organization_id",
),
sm.From("comms.text_job").As("t"),
sm.InnerJoin("publicreport.report").As("r").OnEQ(
psql.Quote("t", "report_id"),
psql.Quote("r", "id"),
),
sm.Where(psql.Quote("t", "report_id").IsNotNull()),
sm.Where(psql.Quote("t", "destination").EQ(psql.Arg(destination.PhoneString()))),
sm.Where(psql.Quote("r", "status").EQ(psql.Arg(enums.PublicreportReportstatustypeReported))),
), scan.StructMapper[reportIDs]())
if err != nil {
return []reportIDs{}, fmt.Errorf("query reports: %w", err)
}
return rows, nil
}