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

@ -18,7 +18,6 @@ aliases:
no_tests: true
psql:
schemas:
- "comms"
- "fieldseeker"
- "fileupload"
- "lob"

View file

@ -0,0 +1,19 @@
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package model
import (
"time"
)
type Contact struct {
Created time.Time
ID int32 `sql:"primary_key"`
Name string
OrganizationID int32
}

View file

@ -0,0 +1,16 @@
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package model
type ContactEmail struct {
Address string `sql:"primary_key"`
Confirmed bool
ContactID int32
ID int32
IsSubscribed bool
}

View file

@ -0,0 +1,17 @@
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package model
type ContactPhone struct {
CanSms bool
ConfirmedMessageID *int32
ContactID int32
E164 string `sql:"primary_key"`
IsSubscribed bool
StopMessageID *int32
}

View file

@ -0,0 +1,87 @@
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package table
import (
"github.com/Gleipnir-Technology/jet/postgres"
)
var Contact = newContactTable("comms", "contact", "")
type contactTable struct {
postgres.Table
// Columns
Created postgres.ColumnTimestamp
ID postgres.ColumnInteger
Name postgres.ColumnString
OrganizationID postgres.ColumnInteger
AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
DefaultColumns postgres.ColumnList
}
type ContactTable struct {
contactTable
EXCLUDED contactTable
}
// AS creates new ContactTable with assigned alias
func (a ContactTable) AS(alias string) *ContactTable {
return newContactTable(a.SchemaName(), a.TableName(), alias)
}
// Schema creates new ContactTable with assigned schema name
func (a ContactTable) FromSchema(schemaName string) *ContactTable {
return newContactTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ContactTable with assigned table prefix
func (a ContactTable) WithPrefix(prefix string) *ContactTable {
return newContactTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ContactTable with assigned table suffix
func (a ContactTable) WithSuffix(suffix string) *ContactTable {
return newContactTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newContactTable(schemaName, tableName, alias string) *ContactTable {
return &ContactTable{
contactTable: newContactTableImpl(schemaName, tableName, alias),
EXCLUDED: newContactTableImpl("", "excluded", ""),
}
}
func newContactTableImpl(schemaName, tableName, alias string) contactTable {
var (
CreatedColumn = postgres.TimestampColumn("created")
IDColumn = postgres.IntegerColumn("id")
NameColumn = postgres.StringColumn("name")
OrganizationIDColumn = postgres.IntegerColumn("organization_id")
allColumns = postgres.ColumnList{CreatedColumn, IDColumn, NameColumn, OrganizationIDColumn}
mutableColumns = postgres.ColumnList{CreatedColumn, NameColumn, OrganizationIDColumn}
defaultColumns = postgres.ColumnList{IDColumn}
)
return contactTable{
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),
//Columns
Created: CreatedColumn,
ID: IDColumn,
Name: NameColumn,
OrganizationID: OrganizationIDColumn,
AllColumns: allColumns,
MutableColumns: mutableColumns,
DefaultColumns: defaultColumns,
}
}

View file

@ -0,0 +1,90 @@
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package table
import (
"github.com/Gleipnir-Technology/jet/postgres"
)
var ContactEmail = newContactEmailTable("comms", "contact_email", "")
type contactEmailTable struct {
postgres.Table
// Columns
Address postgres.ColumnString
Confirmed postgres.ColumnBool
ContactID postgres.ColumnInteger
ID postgres.ColumnInteger
IsSubscribed postgres.ColumnBool
AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
DefaultColumns postgres.ColumnList
}
type ContactEmailTable struct {
contactEmailTable
EXCLUDED contactEmailTable
}
// AS creates new ContactEmailTable with assigned alias
func (a ContactEmailTable) AS(alias string) *ContactEmailTable {
return newContactEmailTable(a.SchemaName(), a.TableName(), alias)
}
// Schema creates new ContactEmailTable with assigned schema name
func (a ContactEmailTable) FromSchema(schemaName string) *ContactEmailTable {
return newContactEmailTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ContactEmailTable with assigned table prefix
func (a ContactEmailTable) WithPrefix(prefix string) *ContactEmailTable {
return newContactEmailTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ContactEmailTable with assigned table suffix
func (a ContactEmailTable) WithSuffix(suffix string) *ContactEmailTable {
return newContactEmailTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newContactEmailTable(schemaName, tableName, alias string) *ContactEmailTable {
return &ContactEmailTable{
contactEmailTable: newContactEmailTableImpl(schemaName, tableName, alias),
EXCLUDED: newContactEmailTableImpl("", "excluded", ""),
}
}
func newContactEmailTableImpl(schemaName, tableName, alias string) contactEmailTable {
var (
AddressColumn = postgres.StringColumn("address")
ConfirmedColumn = postgres.BoolColumn("confirmed")
ContactIDColumn = postgres.IntegerColumn("contact_id")
IDColumn = postgres.IntegerColumn("id")
IsSubscribedColumn = postgres.BoolColumn("is_subscribed")
allColumns = postgres.ColumnList{AddressColumn, ConfirmedColumn, ContactIDColumn, IDColumn, IsSubscribedColumn}
mutableColumns = postgres.ColumnList{ConfirmedColumn, ContactIDColumn, IDColumn, IsSubscribedColumn}
defaultColumns = postgres.ColumnList{IDColumn}
)
return contactEmailTable{
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),
//Columns
Address: AddressColumn,
Confirmed: ConfirmedColumn,
ContactID: ContactIDColumn,
ID: IDColumn,
IsSubscribed: IsSubscribedColumn,
AllColumns: allColumns,
MutableColumns: mutableColumns,
DefaultColumns: defaultColumns,
}
}

View file

@ -0,0 +1,93 @@
//
// Code generated by go-jet DO NOT EDIT.
//
// WARNING: Changes to this file may cause incorrect behavior
// and will be lost if the code is regenerated
//
package table
import (
"github.com/Gleipnir-Technology/jet/postgres"
)
var ContactPhone = newContactPhoneTable("comms", "contact_phone", "")
type contactPhoneTable struct {
postgres.Table
// Columns
CanSms postgres.ColumnBool
ConfirmedMessageID postgres.ColumnInteger
ContactID postgres.ColumnInteger
E164 postgres.ColumnString
IsSubscribed postgres.ColumnBool
StopMessageID postgres.ColumnInteger
AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
DefaultColumns postgres.ColumnList
}
type ContactPhoneTable struct {
contactPhoneTable
EXCLUDED contactPhoneTable
}
// AS creates new ContactPhoneTable with assigned alias
func (a ContactPhoneTable) AS(alias string) *ContactPhoneTable {
return newContactPhoneTable(a.SchemaName(), a.TableName(), alias)
}
// Schema creates new ContactPhoneTable with assigned schema name
func (a ContactPhoneTable) FromSchema(schemaName string) *ContactPhoneTable {
return newContactPhoneTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ContactPhoneTable with assigned table prefix
func (a ContactPhoneTable) WithPrefix(prefix string) *ContactPhoneTable {
return newContactPhoneTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ContactPhoneTable with assigned table suffix
func (a ContactPhoneTable) WithSuffix(suffix string) *ContactPhoneTable {
return newContactPhoneTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newContactPhoneTable(schemaName, tableName, alias string) *ContactPhoneTable {
return &ContactPhoneTable{
contactPhoneTable: newContactPhoneTableImpl(schemaName, tableName, alias),
EXCLUDED: newContactPhoneTableImpl("", "excluded", ""),
}
}
func newContactPhoneTableImpl(schemaName, tableName, alias string) contactPhoneTable {
var (
CanSmsColumn = postgres.BoolColumn("can_sms")
ConfirmedMessageIDColumn = postgres.IntegerColumn("confirmed_message_id")
ContactIDColumn = postgres.IntegerColumn("contact_id")
E164Column = postgres.StringColumn("e164")
IsSubscribedColumn = postgres.BoolColumn("is_subscribed")
StopMessageIDColumn = postgres.IntegerColumn("stop_message_id")
allColumns = postgres.ColumnList{CanSmsColumn, ConfirmedMessageIDColumn, ContactIDColumn, E164Column, IsSubscribedColumn, StopMessageIDColumn}
mutableColumns = postgres.ColumnList{CanSmsColumn, ConfirmedMessageIDColumn, ContactIDColumn, IsSubscribedColumn, StopMessageIDColumn}
defaultColumns = postgres.ColumnList{}
)
return contactPhoneTable{
Table: postgres.NewTable(schemaName, tableName, alias, allColumns...),
//Columns
CanSms: CanSmsColumn,
ConfirmedMessageID: ConfirmedMessageIDColumn,
ContactID: ContactIDColumn,
E164: E164Column,
IsSubscribed: IsSubscribedColumn,
StopMessageID: StopMessageIDColumn,
AllColumns: allColumns,
MutableColumns: mutableColumns,
DefaultColumns: defaultColumns,
}
}

View file

@ -10,6 +10,9 @@ package table
// UseSchema sets a new schema name for all generated table SQL builder types. It is recommended to invoke
// this method only once at the beginning of the program.
func UseSchema(schema string) {
Contact = Contact.FromSchema(schema)
ContactEmail = ContactEmail.FromSchema(schema)
ContactPhone = ContactPhone.FromSchema(schema)
EmailContact = EmailContact.FromSchema(schema)
EmailLog = EmailLog.FromSchema(schema)
EmailTemplate = EmailTemplate.FromSchema(schema)

View file

@ -36,4 +36,5 @@ type Report struct {
AddressGid string
ClientUUID *uuid.UUID
ReporterPhoneCanSms bool
ReporterContactID *int32
}

View file

@ -39,6 +39,7 @@ type reportTable struct {
AddressGid postgres.ColumnString
ClientUUID postgres.ColumnString
ReporterPhoneCanSms postgres.ColumnBool
ReporterContactID postgres.ColumnInteger
AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
@ -102,8 +103,9 @@ func newReportTableImpl(schemaName, tableName, alias string) reportTable {
AddressGidColumn = postgres.StringColumn("address_gid")
ClientUUIDColumn = postgres.StringColumn("client_uuid")
ReporterPhoneCanSmsColumn = postgres.BoolColumn("reporter_phone_can_sms")
allColumns = postgres.ColumnList{AddressRawColumn, AddressIDColumn, CreatedColumn, LocationColumn, H3cellColumn, IDColumn, LatlngAccuracyTypeColumn, LatlngAccuracyValueColumn, MapZoomColumn, OrganizationIDColumn, PublicIDColumn, ReporterNameColumn, ReporterEmailColumn, ReporterPhoneColumn, ReporterContactConsentColumn, ReportTypeColumn, ReviewedColumn, ReviewerIDColumn, StatusColumn, AddressGidColumn, ClientUUIDColumn, ReporterPhoneCanSmsColumn}
mutableColumns = postgres.ColumnList{AddressRawColumn, AddressIDColumn, CreatedColumn, LocationColumn, H3cellColumn, LatlngAccuracyTypeColumn, LatlngAccuracyValueColumn, MapZoomColumn, OrganizationIDColumn, PublicIDColumn, ReporterNameColumn, ReporterEmailColumn, ReporterPhoneColumn, ReporterContactConsentColumn, ReportTypeColumn, ReviewedColumn, ReviewerIDColumn, StatusColumn, AddressGidColumn, ClientUUIDColumn, ReporterPhoneCanSmsColumn}
ReporterContactIDColumn = postgres.IntegerColumn("reporter_contact_id")
allColumns = postgres.ColumnList{AddressRawColumn, AddressIDColumn, CreatedColumn, LocationColumn, H3cellColumn, IDColumn, LatlngAccuracyTypeColumn, LatlngAccuracyValueColumn, MapZoomColumn, OrganizationIDColumn, PublicIDColumn, ReporterNameColumn, ReporterEmailColumn, ReporterPhoneColumn, ReporterContactConsentColumn, ReportTypeColumn, ReviewedColumn, ReviewerIDColumn, StatusColumn, AddressGidColumn, ClientUUIDColumn, ReporterPhoneCanSmsColumn, ReporterContactIDColumn}
mutableColumns = postgres.ColumnList{AddressRawColumn, AddressIDColumn, CreatedColumn, LocationColumn, H3cellColumn, LatlngAccuracyTypeColumn, LatlngAccuracyValueColumn, MapZoomColumn, OrganizationIDColumn, PublicIDColumn, ReporterNameColumn, ReporterEmailColumn, ReporterPhoneColumn, ReporterContactConsentColumn, ReportTypeColumn, ReviewedColumn, ReviewerIDColumn, StatusColumn, AddressGidColumn, ClientUUIDColumn, ReporterPhoneCanSmsColumn, ReporterContactIDColumn}
defaultColumns = postgres.ColumnList{IDColumn}
)
@ -133,6 +135,7 @@ func newReportTableImpl(schemaName, tableName, alias string) reportTable {
AddressGid: AddressGidColumn,
ClientUUID: ClientUUIDColumn,
ReporterPhoneCanSms: ReporterPhoneCanSmsColumn,
ReporterContactID: ReporterContactIDColumn,
AllColumns: allColumns,
MutableColumns: mutableColumns,

View file

@ -0,0 +1,119 @@
-- +goose Up
CREATE TABLE comms.contact (
created TIMESTAMP WITHOUT TIME ZONE NOT NULL,
id SERIAL NOT NULL,
name TEXT NOT NULL,
organization_id INTEGER NOT NULL REFERENCES organization(id),
PRIMARY KEY(id)
);
CREATE TABLE comms.contact_email (
address TEXT NOT NULL,
confirmed BOOLEAN NOT NULL,
contact_id INTEGER NOT NULL REFERENCES comms.contact(id),
id SERIAL NOT NULL,
is_subscribed BOOLEAN NOT NULL,
PRIMARY KEY(address)
);
CREATE TABLE comms.contact_phone (
can_sms BOOLEAN NOT NULL,
confirmed_message_id INTEGER REFERENCES comms.text_log(id),
contact_id INTEGER NOT NULL REFERENCES comms.contact(id),
e164 TEXT NOT NULL,
is_subscribed BOOLEAN NOT NULL,
stop_message_id INTEGER REFERENCES comms.text_log(id),
PRIMARY KEY(e164)
);
ALTER TABLE publicreport.report
ADD COLUMN reporter_contact_id INTEGER REFERENCES comms.contact(id);
-- insert a placeholder contact for the system
INSERT INTO comms.contact (
created,
name,
organization_id
) SELECT
CURRENT_TIMESTAMP,
'Nidus',
id
FROM organization WHERE is_catchall = TRUE;
-- insert contacts where we have names, dropping duplicates
INSERT INTO comms.contact (
created,
name,
organization_id
) SELECT DISTINCT ON (reporter_name, organization_id)
created,
reporter_name,
organization_id
FROM publicreport.report
WHERE reporter_name != ''
ORDER BY reporter_name, organization_id, created ASC;
UPDATE publicreport.report
SET reporter_contact_id = comms.contact.id
FROM comms.contact
WHERE comms.contact.name = report.reporter_name
AND reporter_name != '';
-- insert contacts where we don't have names
INSERT INTO comms.contact (
created,
name,
organization_id
) SELECT
created,
reporter_name,
organization_id
FROM publicreport.report
WHERE report.reporter_name = '';
UPDATE publicreport.report
SET reporter_contact_id = comms.contact.id
FROM comms.contact
WHERE comms.contact.created = report.created;
-- At this point every publicreport.report should have an associated contact ID.
-- We'll make sure of this via constraint before moving the email and phone data into
-- the contacts
ALTER TABLE publicreport.report
ALTER COLUMN reporter_contact_id SET NOT NULL;
-- Now copy over all of the contact information we have into the new contact rows
INSERT INTO comms.contact_email (
address,
confirmed,
contact_id,
is_subscribed
) SELECT
reporter_email,
COALESCE(reporter_contact_consent, false),
reporter_contact_id,
FALSE
FROM publicreport.report WHERE report.reporter_email != ''
ON CONFLICT DO NOTHING;
INSERT INTO comms.contact_phone (
can_sms,
confirmed_message_id,
contact_id,
e164,
is_subscribed,
stop_message_id
) SELECT
reporter_phone_can_sms,
NULL,
reporter_contact_id,
reporter_phone,
FALSE,
NULL
FROM publicreport.report WHERE publicreport.report.reporter_phone != ''
ON CONFLICT DO NOTHING;
-- +goose Down
ALTER TABLE publicreport.report
DROP COLUMN reporter_contact_id;
DROP TABLE comms.contact_phone;
DROP TABLE comms.contact_email;
DROP TABLE comms.contact;

View file

@ -153,28 +153,23 @@ func (publicreportReportColumns) AliasedAs(alias string) publicreportReportColum
// All values are optional, and do not have to be set
// Generated columns are not included
type PublicreportReportSetter struct {
AddressRaw omit.Val[string] `db:"address_raw" `
AddressID omitnull.Val[int32] `db:"address_id" `
Created omit.Val[time.Time] `db:"created" `
Location omitnull.Val[string] `db:"location" `
H3cell omitnull.Val[string] `db:"h3cell" `
ID omit.Val[int32] `db:"id,pk" `
LatlngAccuracyType omit.Val[enums.PublicreportAccuracytype] `db:"latlng_accuracy_type" `
LatlngAccuracyValue omit.Val[float32] `db:"latlng_accuracy_value" `
MapZoom omit.Val[float32] `db:"map_zoom" `
OrganizationID omit.Val[int32] `db:"organization_id" `
PublicID omit.Val[string] `db:"public_id" `
ReporterName omit.Val[string] `db:"reporter_name" `
ReporterEmail omit.Val[string] `db:"reporter_email" `
ReporterPhone omit.Val[string] `db:"reporter_phone" `
ReporterContactConsent omitnull.Val[bool] `db:"reporter_contact_consent" `
ReportType omit.Val[enums.PublicreportReporttype] `db:"report_type" `
Reviewed omitnull.Val[time.Time] `db:"reviewed" `
ReviewerID omitnull.Val[int32] `db:"reviewer_id" `
Status omit.Val[enums.PublicreportReportstatustype] `db:"status" `
AddressGid omit.Val[string] `db:"address_gid" `
ClientUUID omitnull.Val[uuid.UUID] `db:"client_uuid" `
ReporterPhoneCanSMS omit.Val[bool] `db:"reporter_phone_can_sms" `
AddressRaw omit.Val[string] `db:"address_raw" `
AddressID omitnull.Val[int32] `db:"address_id" `
Created omit.Val[time.Time] `db:"created" `
Location omitnull.Val[string] `db:"location" `
H3cell omitnull.Val[string] `db:"h3cell" `
ID omit.Val[int32] `db:"id,pk" `
LatlngAccuracyType omit.Val[enums.PublicreportAccuracytype] `db:"latlng_accuracy_type" `
LatlngAccuracyValue omit.Val[float32] `db:"latlng_accuracy_value" `
MapZoom omit.Val[float32] `db:"map_zoom" `
OrganizationID omit.Val[int32] `db:"organization_id" `
PublicID omit.Val[string] `db:"public_id" `
ReportType omit.Val[enums.PublicreportReporttype] `db:"report_type" `
Reviewed omitnull.Val[time.Time] `db:"reviewed" `
ReviewerID omitnull.Val[int32] `db:"reviewer_id" `
Status omit.Val[enums.PublicreportReportstatustype] `db:"status" `
AddressGid omit.Val[string] `db:"address_gid" `
ClientUUID omitnull.Val[uuid.UUID] `db:"client_uuid" `
}
func (s PublicreportReportSetter) SetColumns() []string {
@ -212,18 +207,6 @@ func (s PublicreportReportSetter) SetColumns() []string {
if s.PublicID.IsValue() {
vals = append(vals, "public_id")
}
if s.ReporterName.IsValue() {
vals = append(vals, "reporter_name")
}
if s.ReporterEmail.IsValue() {
vals = append(vals, "reporter_email")
}
if s.ReporterPhone.IsValue() {
vals = append(vals, "reporter_phone")
}
if !s.ReporterContactConsent.IsUnset() {
vals = append(vals, "reporter_contact_consent")
}
if s.ReportType.IsValue() {
vals = append(vals, "report_type")
}
@ -242,9 +225,6 @@ func (s PublicreportReportSetter) SetColumns() []string {
if !s.ClientUUID.IsUnset() {
vals = append(vals, "client_uuid")
}
if s.ReporterPhoneCanSMS.IsValue() {
vals = append(vals, "reporter_phone_can_sms")
}
return vals
}
@ -282,18 +262,6 @@ func (s PublicreportReportSetter) Overwrite(t *PublicreportReport) {
if s.PublicID.IsValue() {
t.PublicID = s.PublicID.MustGet()
}
if s.ReporterName.IsValue() {
t.ReporterName = s.ReporterName.MustGet()
}
if s.ReporterEmail.IsValue() {
t.ReporterEmail = s.ReporterEmail.MustGet()
}
if s.ReporterPhone.IsValue() {
t.ReporterPhone = s.ReporterPhone.MustGet()
}
if !s.ReporterContactConsent.IsUnset() {
t.ReporterContactConsent = s.ReporterContactConsent.MustGetNull()
}
if s.ReportType.IsValue() {
t.ReportType = s.ReportType.MustGet()
}
@ -312,9 +280,6 @@ func (s PublicreportReportSetter) Overwrite(t *PublicreportReport) {
if !s.ClientUUID.IsUnset() {
t.ClientUUID = s.ClientUUID.MustGetNull()
}
if s.ReporterPhoneCanSMS.IsValue() {
t.ReporterPhoneCanSMS = s.ReporterPhoneCanSMS.MustGet()
}
}
func (s *PublicreportReportSetter) Apply(q *dialect.InsertQuery) {
@ -390,29 +355,13 @@ func (s *PublicreportReportSetter) Apply(q *dialect.InsertQuery) {
vals[10] = psql.Raw("DEFAULT")
}
if s.ReporterName.IsValue() {
vals[11] = psql.Arg(s.ReporterName.MustGet())
} else {
vals[11] = psql.Raw("DEFAULT")
}
vals[11] = psql.Raw("DEFAULT")
if s.ReporterEmail.IsValue() {
vals[12] = psql.Arg(s.ReporterEmail.MustGet())
} else {
vals[12] = psql.Raw("DEFAULT")
}
vals[12] = psql.Raw("DEFAULT")
if s.ReporterPhone.IsValue() {
vals[13] = psql.Arg(s.ReporterPhone.MustGet())
} else {
vals[13] = psql.Raw("DEFAULT")
}
vals[13] = psql.Raw("DEFAULT")
if !s.ReporterContactConsent.IsUnset() {
vals[14] = psql.Arg(s.ReporterContactConsent.MustGetNull())
} else {
vals[14] = psql.Raw("DEFAULT")
}
vals[14] = psql.Raw("DEFAULT")
if s.ReportType.IsValue() {
vals[15] = psql.Arg(s.ReportType.MustGet())
@ -450,11 +399,7 @@ func (s *PublicreportReportSetter) Apply(q *dialect.InsertQuery) {
vals[20] = psql.Raw("DEFAULT")
}
if s.ReporterPhoneCanSMS.IsValue() {
vals[21] = psql.Arg(s.ReporterPhoneCanSMS.MustGet())
} else {
vals[21] = psql.Raw("DEFAULT")
}
vals[21] = psql.Raw("DEFAULT")
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
}))
@ -544,34 +489,6 @@ func (s PublicreportReportSetter) Expressions(prefix ...string) []bob.Expression
}})
}
if s.ReporterName.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "reporter_name")...),
psql.Arg(s.ReporterName),
}})
}
if s.ReporterEmail.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "reporter_email")...),
psql.Arg(s.ReporterEmail),
}})
}
if s.ReporterPhone.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "reporter_phone")...),
psql.Arg(s.ReporterPhone),
}})
}
if !s.ReporterContactConsent.IsUnset() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "reporter_contact_consent")...),
psql.Arg(s.ReporterContactConsent),
}})
}
if s.ReportType.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "report_type")...),
@ -614,13 +531,6 @@ func (s PublicreportReportSetter) Expressions(prefix ...string) []bob.Expression
}})
}
if s.ReporterPhoneCanSMS.IsValue() {
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
psql.Quote(append(prefix, "reporter_phone_can_sms")...),
psql.Arg(s.ReporterPhoneCanSMS),
}})
}
return exprs
}

47
db/query/comms/contact.go Normal file
View file

@ -0,0 +1,47 @@
package comms
import (
"context"
//"github.com/Gleipnir-Technology/bob"
"github.com/Gleipnir-Technology/nidus-sync/db"
//"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/enum"
"github.com/Gleipnir-Technology/jet/postgres"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/model"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/table"
)
func ContactInsert(ctx context.Context, txn db.Ex, m model.Contact) (model.Contact, error) {
statement := table.Contact.INSERT(table.Contact.MutableColumns).
MODEL(m).
RETURNING(table.Contact.AllColumns)
return db.ExecuteOneTx[model.Contact](ctx, txn, statement)
}
func ContactFromID(ctx context.Context, txn db.Ex, id int64) (model.Contact, error) {
statement := table.Contact.SELECT(
table.Contact.AllColumns,
).FROM(table.Contact).
WHERE(table.Contact.ID.EQ(postgres.Int(id)))
return db.ExecuteOne[model.Contact](ctx, statement)
}
func ContactUpdateName(ctx context.Context, txn db.Ex, id int64, name string) error {
statement := table.Contact.UPDATE().
SET(
table.Contact.Name.SET(postgres.String(name)),
).
WHERE(table.Contact.OrganizationID.EQ(postgres.Int(id)))
return db.ExecuteNoneTx(ctx, txn, statement)
}
/*
func ContactsFromAddress(ctx context.Context, address string) ([]model.Contact, error) {
statement := table.Contact.SELECT(
table.Contact.AllColumns,
).FROM(table.Contact).
WHERE(table.Contact.Source.EQ(postgres.String(address)).OR(
table.Contact.Destination.EQ(postgres.String(address))))
return db.ExecuteMany[model.Contact](ctx, statement)
}
*/

View file

@ -0,0 +1,59 @@
package comms
import (
"context"
//"github.com/Gleipnir-Technology/bob"
"github.com/Gleipnir-Technology/nidus-sync/db"
//"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/enum"
"github.com/Gleipnir-Technology/jet/postgres"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/model"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/table"
)
func ContactPhoneInsert(ctx context.Context, txn db.Ex, m model.ContactPhone) (model.ContactPhone, error) {
statement := table.ContactPhone.INSERT(table.ContactPhone.MutableColumns).
MODEL(m).
RETURNING(table.ContactPhone.AllColumns)
return db.ExecuteOneTx[model.ContactPhone](ctx, txn, statement)
}
func ContactPhoneFromE164(ctx context.Context, txn db.Ex, e164 string) (model.ContactPhone, error) {
statement := table.ContactPhone.SELECT(
table.ContactPhone.AllColumns,
).FROM(table.ContactPhone).
WHERE(table.ContactPhone.E164.EQ(postgres.String(e164)))
return db.ExecuteOneTx[model.ContactPhone](ctx, txn, statement)
}
func ContactPhoneUpdateConfirmedMessageID(ctx context.Context, txn db.Ex, e164 string, message_id *int32) error {
statement := table.ContactPhone.UPDATE().
SET(table.ContactPhone.ConfirmedMessageID.SET(postgres.IntExp(postgres.NULL))).
WHERE(table.ContactPhone.E164.EQ(postgres.String(e164)))
return db.ExecuteNoneTx(ctx, txn, statement)
}
func ContactPhoneUpdateStopMessageID(ctx context.Context, txn db.Ex, e164 string, message_id *int32) error {
/*
m := model.ContactPhone{}
m.StopMessageID = message_id
statement := table.ContactPhone.UPDATE(
table.ContactPhone.StopMessageID,
).MODEL(m).
WHERE(table.ContactPhone.E164.EQ(postgres.String(e164)))
*/
statement := table.ContactPhone.UPDATE().
SET(table.ContactPhone.StopMessageID.SET(postgres.IntExp(postgres.NULL))).
WHERE(table.ContactPhone.E164.EQ(postgres.String(e164)))
return db.ExecuteNoneTx(ctx, txn, statement)
}
/*
func ContactPhonesFromAddress(ctx context.Context, address string) ([]model.ContactPhone, error) {
statement := table.ContactPhone.SELECT(
table.ContactPhone.AllColumns,
).FROM(table.ContactPhone).
WHERE(table.ContactPhone.Source.EQ(postgres.String(address)).OR(
table.ContactPhone.Destination.EQ(postgres.String(address))))
return db.ExecuteMany[model.ContactPhone](ctx, statement)
}
*/

View file

@ -0,0 +1,42 @@
package comms
import (
"context"
"github.com/Gleipnir-Technology/jet/postgres"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/model"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/table"
)
func TextJobComplete(ctx context.Context, txn db.Ex, id int64) error {
statement := table.TextJob.UPDATE(
table.TextJob.Completed,
).SET(
table.TextJob.Completed.SET(postgres.LOCALTIMESTAMP()),
).WHERE(
table.TextJob.ID.EQ(postgres.Int(id)),
)
return db.ExecuteNoneTx(ctx, txn, statement)
}
func TextJobFromID(ctx context.Context, txn db.Ex, id int64) (model.TextJob, error) {
statement := table.TextJob.SELECT(
table.TextJob.AllColumns,
).FROM(table.TextJob).
WHERE(table.TextJob.ID.EQ(postgres.Int(id)))
return db.ExecuteOneTx[model.TextJob](ctx, txn, statement)
}
func TextJobInsert(ctx context.Context, txn db.Ex, m model.TextJob) (model.TextJob, error) {
statement := table.TextJob.INSERT(
table.TextJob.MutableColumns,
).MODEL(m)
return db.ExecuteOneTx[model.TextJob](ctx, txn, statement)
}
func TextJobsWaitingFromDestination(ctx context.Context, txn db.Ex, destination string) ([]model.TextJob, error) {
statement := table.TextJob.SELECT(
table.TextJob.AllColumns,
).FROM(table.TextJob).
WHERE(table.TextJob.Destination.EQ(postgres.String(destination)).AND(
table.TextJob.Completed.IS_NULL()))
return db.ExecuteManyTx[model.TextJob](ctx, txn, statement)
}

View file

@ -9,18 +9,42 @@ import (
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/table"
)
func TextLogFromID(ctx context.Context, id int64) (model.TextLog, error) {
func TextLogInsert(ctx context.Context, txn db.Ex, m model.TextLog) (model.TextLog, error) {
statement := table.TextLog.INSERT(
table.TextLog.MutableColumns,
).MODEL(m)
return db.ExecuteOneTx[model.TextLog](ctx, txn, statement)
}
func TextLogFromID(ctx context.Context, txn db.Ex, id int64) (model.TextLog, error) {
statement := table.TextLog.SELECT(
table.TextLog.AllColumns,
).FROM(table.TextLog).
WHERE(table.TextLog.ID.EQ(postgres.Int(id)))
return db.ExecuteOne[model.TextLog](ctx, statement)
return db.ExecuteOneTx[model.TextLog](ctx, txn, statement)
}
func TextLogsFromPhoneNumber(ctx context.Context, number string) ([]model.TextLog, error) {
func TextLogsFromPhoneNumber(ctx context.Context, txn db.Ex, number string) ([]model.TextLog, error) {
statement := table.TextLog.SELECT(
table.TextLog.AllColumns,
).FROM(table.TextLog).
WHERE(table.TextLog.Source.EQ(postgres.String(number)).OR(
table.TextLog.Destination.EQ(postgres.String(number))))
return db.ExecuteMany[model.TextLog](ctx, statement)
return db.ExecuteManyTx[model.TextLog](ctx, txn, statement)
}
func TextLogWelcomeFromDestination(ctx context.Context, txn db.Ex, destination string) ([]model.TextLog, error) {
statement := table.TextLog.SELECT(
table.TextLog.AllColumns,
).FROM(table.TextLog).
WHERE(table.TextLog.Destination.EQ(postgres.String(destination)).AND(
table.TextLog.IsWelcome.EQ(postgres.Bool(true))))
return db.ExecuteManyTx[model.TextLog](ctx, txn, statement)
}
func TextLogUpdate(ctx context.Context, txn db.Ex, id int64, twilio_sid string, twilio_status string) error {
statement := table.TextLog.UPDATE(
table.TextLog.TwilioSid,
table.TextLog.TwilioStatus,
).SET(
table.TextLog.TwilioSid.SET(postgres.String(twilio_sid)),
table.TextLog.TwilioStatus.SET(postgres.String(twilio_status)),
).WHERE(table.TextLog.ID.EQ(postgres.Int(id)))
return db.ExecuteNoneTx(ctx, txn, statement)
}

View file

@ -3,7 +3,6 @@ package public
import (
"context"
//"github.com/Gleipnir-Technology/bob"
"github.com/Gleipnir-Technology/nidus-sync/db"
//"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/enum"
"github.com/Gleipnir-Technology/jet/postgres"
@ -11,28 +10,28 @@ import (
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/table"
)
func CommunicationInsert(ctx context.Context, txn db.Tx, m model.Communication) (model.Communication, error) {
func CommunicationInsert(ctx context.Context, txn db.Ex, m model.Communication) (model.Communication, error) {
statement := table.Communication.INSERT(table.Communication.MutableColumns).
MODEL(m).
RETURNING(table.Communication.AllColumns)
return db.ExecuteOneTx[model.Communication](ctx, txn, statement)
}
func CommunicationFromID(ctx context.Context, comm_id int64) (model.Communication, error) {
func CommunicationFromID(ctx context.Context, txn db.Ex, comm_id int64) (model.Communication, error) {
statement := table.Communication.SELECT(
table.Communication.AllColumns,
).FROM(table.Communication).
WHERE(table.Communication.ID.EQ(postgres.Int(comm_id)))
return db.ExecuteOne[model.Communication](ctx, statement)
return db.ExecuteOneTx[model.Communication](ctx, txn, statement)
}
func CommunicationsFromOrganization(ctx context.Context, org_id int64) ([]model.Communication, error) {
func CommunicationsFromOrganization(ctx context.Context, txn db.Ex, org_id int64) ([]model.Communication, error) {
statement := table.Communication.SELECT(
table.Communication.AllColumns,
).FROM(table.Communication).
WHERE(table.Communication.OrganizationID.EQ(postgres.Int(org_id))).
ORDER_BY(table.Communication.Created.DESC())
return db.ExecuteMany[model.Communication](ctx, statement)
return db.ExecuteManyTx[model.Communication](ctx, txn, statement)
}
func CommunicationSetStatus(ctx context.Context, txn db.Tx, org_id int64, comm_id int64, status model.Communicationstatus) error {
func CommunicationSetStatus(ctx context.Context, txn db.Ex, org_id int64, comm_id int64, status model.Communicationstatus) error {
statement := table.Communication.UPDATE().
SET(
table.Communication.Status.SET(postgres.NewEnumValue(status.String())),

View file

@ -0,0 +1,36 @@
package public
import (
"context"
"github.com/Gleipnir-Technology/jet/postgres"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/table"
)
func OrganizationInsert(ctx context.Context, txn db.Ex, m model.Organization) (model.Organization, error) {
statement := table.Organization.INSERT(table.Organization.MutableColumns).
MODEL(m).
RETURNING(table.Organization.AllColumns)
return db.ExecuteOneTx[model.Organization](ctx, txn, statement)
}
func OrganizationFromID(ctx context.Context, txn db.Ex, org_id int64) (model.Organization, error) {
statement := table.Organization.SELECT(
table.Organization.AllColumns,
).FROM(table.Organization).
WHERE(table.Organization.ID.EQ(postgres.Int(org_id)))
return db.ExecuteOne[model.Organization](ctx, statement)
}
/*
func OrganizationSetStatus(ctx context.Context, txn db.Tx, org_id int64, org_id int64, status model.Organizationstatus) error {
statement := table.Organization.UPDATE().
SET(
table.Organization.Status.SET(postgres.NewEnumValue(status.String())),
).
WHERE(table.Organization.OrganizationID.EQ(postgres.Int(org_id)).AND(
table.Organization.ID.EQ(postgres.Int(org_id))))
return db.ExecuteNoneTx(ctx, txn, statement)
}
*/

View file

@ -0,0 +1,18 @@
package public
import (
"context"
"github.com/Gleipnir-Technology/nidus-sync/db"
//"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/enum"
//"github.com/Gleipnir-Technology/jet/postgres"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/model"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/table"
)
func ReportTextInsert(ctx context.Context, txn db.Tx, m model.ReportText) (model.ReportText, error) {
statement := table.ReportText.INSERT(table.ReportText.MutableColumns).
MODEL(m).
RETURNING(table.ReportText.AllColumns)
return db.ExecuteOneTx[model.ReportText](ctx, txn, statement)
}

View file

@ -0,0 +1,26 @@
package publicreport
import (
"context"
"time"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/table"
//"github.com/Gleipnir-Technology/jet/postgres"
)
func NotifyEmailInsert(ctx context.Context, txn db.Ex, m model.NotifyEmail) (model.NotifyEmail, error) {
statement := table.NotifyEmail.INSERT(table.NotifyEmail.MutableColumns).
MODEL(m).
RETURNING(table.NotifyEmail.AllColumns)
return db.ExecuteOneTx[model.NotifyEmail](ctx, txn, statement)
}
func NotifyEmailCreate(ctx context.Context, txn db.Ex, report_id int32, destination string) (model.NotifyEmail, error) {
return NotifyEmailInsert(ctx, txn, model.NotifyEmail{
Created: time.Now(),
Deleted: nil,
EmailAddress: destination,
ReportID: report_id,
})
}

View file

@ -0,0 +1,26 @@
package publicreport
import (
"context"
"time"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/table"
//"github.com/Gleipnir-Technology/jet/postgres"
)
func NotifyPhoneInsert(ctx context.Context, txn db.Ex, m model.NotifyPhone) (model.NotifyPhone, error) {
statement := table.NotifyPhone.INSERT(table.NotifyPhone.MutableColumns).
MODEL(m).
RETURNING(table.NotifyPhone.AllColumns)
return db.ExecuteOneTx[model.NotifyPhone](ctx, txn, statement)
}
func NotifyPhoneCreate(ctx context.Context, txn db.Ex, report_id int32, destination string) (model.NotifyPhone, error) {
return NotifyPhoneInsert(ctx, txn, model.NotifyPhone{
Created: time.Now(),
Deleted: nil,
PhoneE164: destination,
ReportID: report_id,
})
}

View file

@ -9,6 +9,7 @@ import (
//"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"
)
@ -36,12 +37,12 @@ func ReportsFromAddressID(ctx context.Context, txn db.Ex, org_id int64, address_
table.Report.OrganizationID.EQ(postgres.Int(org_id))))
return db.ExecuteManyTx[model.Report](ctx, txn, statement)
}
func ReportFromID(ctx context.Context, report_id int64) (model.Report, error) {
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.ExecuteOne[model.Report](ctx, statement)
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))
@ -111,3 +112,17 @@ func ReportsUnreviewedForOrganization(ctx context.Context, txn db.Ex, org_id int
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)
}

View file

@ -0,0 +1,19 @@
package publicreport
import (
"context"
//"time"
//"github.com/Gleipnir-Technology/bob"
//"github.com/Gleipnir-Technology/jet/postgres"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/table"
)
func SubscribeEmailInsert(ctx context.Context, txn db.Ex, m model.SubscribeEmail) (model.SubscribeEmail, error) {
statement := table.SubscribeEmail.INSERT(table.SubscribeEmail.AllColumns).
MODEL(m).
RETURNING(table.SubscribeEmail.AllColumns)
return db.ExecuteOneTx[model.SubscribeEmail](ctx, txn, statement)
}

View file

@ -0,0 +1,18 @@
package publicreport
import (
"context"
//"time"
//"github.com/Gleipnir-Technology/jet/postgres"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/model"
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/publicreport/table"
)
func SubscribePhoneInsert(ctx context.Context, txn db.Ex, m model.SubscribePhone) (model.SubscribePhone, error) {
statement := table.SubscribePhone.INSERT(table.SubscribePhone.AllColumns).
MODEL(m).
RETURNING(table.SubscribePhone.AllColumns)
return db.ExecuteOneTx[model.SubscribePhone](ctx, txn, statement)
}