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.
128 lines
4.7 KiB
Go
128 lines
4.7 KiB
Go
package publicreport
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
//"time"
|
|
|
|
//"github.com/Gleipnir-Technology/bob"
|
|
"github.com/Gleipnir-Technology/jet/postgres"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
tablecomms "github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/table"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/table"
|
|
)
|
|
|
|
type ReportUpdater = db.Updater[table.ReportTable, model.Report]
|
|
|
|
func NewReportUpdater() ReportUpdater {
|
|
return db.NewUpdater[table.ReportTable, model.Report](
|
|
table.Report,
|
|
table.Report.ID,
|
|
)
|
|
}
|
|
|
|
func ReportInsert(ctx context.Context, txn db.Ex, m model.Report) (model.Report, error) {
|
|
statement := table.Report.INSERT(table.Report.MutableColumns).
|
|
MODEL(m).
|
|
RETURNING(table.Report.AllColumns)
|
|
return db.ExecuteOneTx[model.Report](ctx, txn, statement)
|
|
}
|
|
func ReportsFromAddressID(ctx context.Context, txn db.Ex, org_id int64, address_id int64) ([]model.Report, error) {
|
|
statement := table.Report.SELECT(
|
|
table.Report.AllColumns,
|
|
).FROM(table.Report).
|
|
WHERE(table.Report.AddressID.EQ(postgres.Int(address_id)).AND(
|
|
table.Report.OrganizationID.EQ(postgres.Int(org_id))))
|
|
return db.ExecuteManyTx[model.Report](ctx, txn, statement)
|
|
}
|
|
func ReportFromID(ctx context.Context, txn db.Ex, report_id int64) (model.Report, error) {
|
|
statement := table.Report.SELECT(
|
|
table.Report.AllColumns,
|
|
).FROM(table.Report).
|
|
WHERE(table.Report.ID.EQ(postgres.Int(report_id)))
|
|
return db.ExecuteOneTx[model.Report](ctx, txn, statement)
|
|
}
|
|
func ReportsFromIDs(ctx context.Context, report_ids []int64) ([]model.Report, error) {
|
|
sql_ids := make([]postgres.Expression, len(report_ids))
|
|
for i, report_id := range report_ids {
|
|
sql_ids[i] = postgres.Int(report_id)
|
|
}
|
|
statement := table.Report.SELECT(
|
|
table.Report.AllColumns,
|
|
).FROM(table.Report).
|
|
WHERE(table.Report.ID.IN(sql_ids...))
|
|
return db.ExecuteMany[model.Report](ctx, statement)
|
|
}
|
|
func ReportsFromIDsForOrg(ctx context.Context, txn db.Ex, report_ids []int64, org_id int64) ([]model.Report, error) {
|
|
sql_ids := make([]postgres.Expression, len(report_ids))
|
|
for i, report_id := range report_ids {
|
|
sql_ids[i] = postgres.Int(report_id)
|
|
}
|
|
statement := table.Report.SELECT(
|
|
table.Report.AllColumns,
|
|
).FROM(table.Report).
|
|
WHERE(table.Report.ID.IN(sql_ids...).AND(
|
|
table.Report.OrganizationID.EQ(postgres.Int(org_id))))
|
|
return db.ExecuteManyTx[model.Report](ctx, txn, statement)
|
|
}
|
|
func ReportFromPublicID(ctx context.Context, txn db.Ex, public_id string) (*model.Report, error) {
|
|
statement := table.Report.SELECT(
|
|
table.Report.AllColumns,
|
|
).FROM(table.Report).
|
|
WHERE(table.Report.PublicID.EQ(postgres.String(public_id)))
|
|
result, err := db.ExecuteOneTx[model.Report](ctx, txn, statement)
|
|
if err != nil {
|
|
if errors.Is(err, db.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return nil, fmt.Errorf("query: %w", err)
|
|
}
|
|
return &result, nil
|
|
}
|
|
func ReportFromPublicIDForOrg(ctx context.Context, txn db.Ex, public_id string, org_id int64) (*model.Report, error) {
|
|
statement := table.Report.SELECT(
|
|
table.Report.AllColumns,
|
|
).FROM(table.Report).
|
|
WHERE(table.Report.PublicID.EQ(postgres.String(public_id)).AND(
|
|
table.Report.OrganizationID.EQ(postgres.Int(org_id))))
|
|
result, err := db.ExecuteOneTx[model.Report](ctx, txn, statement)
|
|
if err != nil {
|
|
if errors.Is(err, db.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return nil, fmt.Errorf("query: %w", err)
|
|
}
|
|
return &result, nil
|
|
}
|
|
func ReportsFromReporterName(ctx context.Context, txn db.Ex, org_id int64, name string) ([]model.Report, error) {
|
|
statement := table.Report.SELECT(
|
|
table.Report.AllColumns,
|
|
).FROM(table.Report).
|
|
WHERE(table.Report.ReporterName.EQ(postgres.String(name)).AND(
|
|
table.Report.OrganizationID.EQ(postgres.Int(org_id))))
|
|
return db.ExecuteManyTx[model.Report](ctx, txn, statement)
|
|
}
|
|
func ReportsUnreviewedForOrganization(ctx context.Context, txn db.Ex, org_id int64) ([]model.Report, error) {
|
|
statement := table.Report.SELECT(
|
|
table.Report.AllColumns,
|
|
).FROM(table.Report).
|
|
WHERE(table.Report.Reviewed.IS_NULL().AND(
|
|
table.Report.OrganizationID.EQ(postgres.Int(org_id))))
|
|
return db.ExecuteManyTx[model.Report](ctx, txn, statement)
|
|
}
|
|
func ReportsFromReporterPhone(ctx context.Context, txn db.Ex, destination string) ([]model.Report, error) {
|
|
statement := table.Report.SELECT(
|
|
table.Report.AllColumns,
|
|
).FROM(table.Report).
|
|
FROM(table.Report.INNER_JOIN(
|
|
tablecomms.TextJob,
|
|
tablecomms.TextJob.ReportID.EQ(table.Report.ID),
|
|
)).WHERE(
|
|
tablecomms.TextJob.ReportID.IS_NOT_NULL().AND(
|
|
tablecomms.TextJob.Destination.EQ(postgres.String(destination))).AND(
|
|
table.Report.Status.EQ(postgres.NewEnumValue(model.Reportstatustype_Reported.String()))),
|
|
)
|
|
return db.ExecuteManyTx[model.Report](ctx, txn, statement)
|
|
}
|