From b25daf12fad6288d20986b61e0ae30bf20009eab Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Tue, 10 Feb 2026 04:31:57 +0000 Subject: [PATCH] Add table for holding subscriptions from users --- .../publicreport.subscribe_email.bob.go | 17 + .../publicreport.subscribe_phone.bob.go | 17 + db/dbinfo/publicreport.subscribe_email.bob.go | 152 ++++ db/dbinfo/publicreport.subscribe_phone.bob.go | 152 ++++ db/factory/bobfactory_context.bob.go | 14 + db/factory/bobfactory_main.bob.go | 100 +++ db/factory/comms.email_contact.bob.go | 86 ++ db/factory/comms.phone.bob.go | 86 ++ db/factory/organization.bob.go | 176 +++- .../publicreport.subscribe_email.bob.go | 542 +++++++++++ .../publicreport.subscribe_phone.bob.go | 542 +++++++++++ db/migrations/00058_district_subscription.sql | 15 + db/models/bob_joins.bob.go | 4 + db/models/bob_loaders.bob.go | 8 + db/models/bob_where.bob.go | 6 + db/models/comms.email_contact.bob.go | 254 ++++++ db/models/comms.phone.bob.go | 254 ++++++ db/models/organization.bob.go | 508 +++++++++++ db/models/publicreport.subscribe_email.bob.go | 841 ++++++++++++++++++ db/models/publicreport.subscribe_phone.bob.go | 841 ++++++++++++++++++ platform/report/notification.go | 41 +- 21 files changed, 4648 insertions(+), 8 deletions(-) create mode 100644 db/dberrors/publicreport.subscribe_email.bob.go create mode 100644 db/dberrors/publicreport.subscribe_phone.bob.go create mode 100644 db/dbinfo/publicreport.subscribe_email.bob.go create mode 100644 db/dbinfo/publicreport.subscribe_phone.bob.go create mode 100644 db/factory/publicreport.subscribe_email.bob.go create mode 100644 db/factory/publicreport.subscribe_phone.bob.go create mode 100644 db/migrations/00058_district_subscription.sql create mode 100644 db/models/publicreport.subscribe_email.bob.go create mode 100644 db/models/publicreport.subscribe_phone.bob.go diff --git a/db/dberrors/publicreport.subscribe_email.bob.go b/db/dberrors/publicreport.subscribe_email.bob.go new file mode 100644 index 00000000..8930d101 --- /dev/null +++ b/db/dberrors/publicreport.subscribe_email.bob.go @@ -0,0 +1,17 @@ +// Code generated by BobGen psql v0.42.5. DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package dberrors + +var PublicreportSubscribeEmailErrors = &publicreportSubscribeEmailErrors{ + ErrUniqueSubscribeEmailPkey: &UniqueConstraintError{ + schema: "publicreport", + table: "subscribe_email", + columns: []string{"district_id", "email_address"}, + s: "subscribe_email_pkey", + }, +} + +type publicreportSubscribeEmailErrors struct { + ErrUniqueSubscribeEmailPkey *UniqueConstraintError +} diff --git a/db/dberrors/publicreport.subscribe_phone.bob.go b/db/dberrors/publicreport.subscribe_phone.bob.go new file mode 100644 index 00000000..2576873b --- /dev/null +++ b/db/dberrors/publicreport.subscribe_phone.bob.go @@ -0,0 +1,17 @@ +// Code generated by BobGen psql v0.42.5. DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package dberrors + +var PublicreportSubscribePhoneErrors = &publicreportSubscribePhoneErrors{ + ErrUniqueSubscribePhonePkey: &UniqueConstraintError{ + schema: "publicreport", + table: "subscribe_phone", + columns: []string{"district_id", "phone_e164"}, + s: "subscribe_phone_pkey", + }, +} + +type publicreportSubscribePhoneErrors struct { + ErrUniqueSubscribePhonePkey *UniqueConstraintError +} diff --git a/db/dbinfo/publicreport.subscribe_email.bob.go b/db/dbinfo/publicreport.subscribe_email.bob.go new file mode 100644 index 00000000..c8ca8d8a --- /dev/null +++ b/db/dbinfo/publicreport.subscribe_email.bob.go @@ -0,0 +1,152 @@ +// Code generated by BobGen psql v0.42.5. DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package dbinfo + +import "github.com/aarondl/opt/null" + +var PublicreportSubscribeEmails = Table[ + publicreportSubscribeEmailColumns, + publicreportSubscribeEmailIndexes, + publicreportSubscribeEmailForeignKeys, + publicreportSubscribeEmailUniques, + publicreportSubscribeEmailChecks, +]{ + Schema: "publicreport", + Name: "subscribe_email", + Columns: publicreportSubscribeEmailColumns{ + Created: column{ + Name: "created", + DBType: "timestamp without time zone", + Default: "", + Comment: "", + Nullable: false, + Generated: false, + AutoIncr: false, + }, + Deleted: column{ + Name: "deleted", + DBType: "timestamp without time zone", + Default: "NULL", + Comment: "", + Nullable: true, + Generated: false, + AutoIncr: false, + }, + DistrictID: column{ + Name: "district_id", + DBType: "integer", + Default: "", + Comment: "", + Nullable: false, + Generated: false, + AutoIncr: false, + }, + EmailAddress: column{ + Name: "email_address", + DBType: "text", + Default: "", + Comment: "", + Nullable: false, + Generated: false, + AutoIncr: false, + }, + }, + Indexes: publicreportSubscribeEmailIndexes{ + SubscribeEmailPkey: index{ + Type: "btree", + Name: "subscribe_email_pkey", + Columns: []indexColumn{ + { + Name: "district_id", + Desc: null.FromCond(false, true), + IsExpression: false, + }, + { + Name: "email_address", + Desc: null.FromCond(false, true), + IsExpression: false, + }, + }, + Unique: true, + Comment: "", + NullsFirst: []bool{false, false}, + NullsDistinct: false, + Where: "", + Include: []string{}, + }, + }, + PrimaryKey: &constraint{ + Name: "subscribe_email_pkey", + Columns: []string{"district_id", "email_address"}, + Comment: "", + }, + ForeignKeys: publicreportSubscribeEmailForeignKeys{ + PublicreportSubscribeEmailSubscribeEmailDistrictIDFkey: foreignKey{ + constraint: constraint{ + Name: "publicreport.subscribe_email.subscribe_email_district_id_fkey", + Columns: []string{"district_id"}, + Comment: "", + }, + ForeignTable: "organization", + ForeignColumns: []string{"id"}, + }, + PublicreportSubscribeEmailSubscribeEmailEmailAddressFkey: foreignKey{ + constraint: constraint{ + Name: "publicreport.subscribe_email.subscribe_email_email_address_fkey", + Columns: []string{"email_address"}, + Comment: "", + }, + ForeignTable: "comms.email_contact", + ForeignColumns: []string{"address"}, + }, + }, + + Comment: "", +} + +type publicreportSubscribeEmailColumns struct { + Created column + Deleted column + DistrictID column + EmailAddress column +} + +func (c publicreportSubscribeEmailColumns) AsSlice() []column { + return []column{ + c.Created, c.Deleted, c.DistrictID, c.EmailAddress, + } +} + +type publicreportSubscribeEmailIndexes struct { + SubscribeEmailPkey index +} + +func (i publicreportSubscribeEmailIndexes) AsSlice() []index { + return []index{ + i.SubscribeEmailPkey, + } +} + +type publicreportSubscribeEmailForeignKeys struct { + PublicreportSubscribeEmailSubscribeEmailDistrictIDFkey foreignKey + PublicreportSubscribeEmailSubscribeEmailEmailAddressFkey foreignKey +} + +func (f publicreportSubscribeEmailForeignKeys) AsSlice() []foreignKey { + return []foreignKey{ + f.PublicreportSubscribeEmailSubscribeEmailDistrictIDFkey, f.PublicreportSubscribeEmailSubscribeEmailEmailAddressFkey, + } +} + +type publicreportSubscribeEmailUniques struct{} + +func (u publicreportSubscribeEmailUniques) AsSlice() []constraint { + return []constraint{} +} + +type publicreportSubscribeEmailChecks struct{} + +func (c publicreportSubscribeEmailChecks) AsSlice() []check { + return []check{} +} diff --git a/db/dbinfo/publicreport.subscribe_phone.bob.go b/db/dbinfo/publicreport.subscribe_phone.bob.go new file mode 100644 index 00000000..7252aa56 --- /dev/null +++ b/db/dbinfo/publicreport.subscribe_phone.bob.go @@ -0,0 +1,152 @@ +// Code generated by BobGen psql v0.42.5. DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package dbinfo + +import "github.com/aarondl/opt/null" + +var PublicreportSubscribePhones = Table[ + publicreportSubscribePhoneColumns, + publicreportSubscribePhoneIndexes, + publicreportSubscribePhoneForeignKeys, + publicreportSubscribePhoneUniques, + publicreportSubscribePhoneChecks, +]{ + Schema: "publicreport", + Name: "subscribe_phone", + Columns: publicreportSubscribePhoneColumns{ + Created: column{ + Name: "created", + DBType: "timestamp without time zone", + Default: "", + Comment: "", + Nullable: false, + Generated: false, + AutoIncr: false, + }, + Deleted: column{ + Name: "deleted", + DBType: "timestamp without time zone", + Default: "NULL", + Comment: "", + Nullable: true, + Generated: false, + AutoIncr: false, + }, + DistrictID: column{ + Name: "district_id", + DBType: "integer", + Default: "", + Comment: "", + Nullable: false, + Generated: false, + AutoIncr: false, + }, + PhoneE164: column{ + Name: "phone_e164", + DBType: "text", + Default: "", + Comment: "", + Nullable: false, + Generated: false, + AutoIncr: false, + }, + }, + Indexes: publicreportSubscribePhoneIndexes{ + SubscribePhonePkey: index{ + Type: "btree", + Name: "subscribe_phone_pkey", + Columns: []indexColumn{ + { + Name: "district_id", + Desc: null.FromCond(false, true), + IsExpression: false, + }, + { + Name: "phone_e164", + Desc: null.FromCond(false, true), + IsExpression: false, + }, + }, + Unique: true, + Comment: "", + NullsFirst: []bool{false, false}, + NullsDistinct: false, + Where: "", + Include: []string{}, + }, + }, + PrimaryKey: &constraint{ + Name: "subscribe_phone_pkey", + Columns: []string{"district_id", "phone_e164"}, + Comment: "", + }, + ForeignKeys: publicreportSubscribePhoneForeignKeys{ + PublicreportSubscribePhoneSubscribePhoneDistrictIDFkey: foreignKey{ + constraint: constraint{ + Name: "publicreport.subscribe_phone.subscribe_phone_district_id_fkey", + Columns: []string{"district_id"}, + Comment: "", + }, + ForeignTable: "organization", + ForeignColumns: []string{"id"}, + }, + PublicreportSubscribePhoneSubscribePhonePhoneE164Fkey: foreignKey{ + constraint: constraint{ + Name: "publicreport.subscribe_phone.subscribe_phone_phone_e164_fkey", + Columns: []string{"phone_e164"}, + Comment: "", + }, + ForeignTable: "comms.phone", + ForeignColumns: []string{"e164"}, + }, + }, + + Comment: "", +} + +type publicreportSubscribePhoneColumns struct { + Created column + Deleted column + DistrictID column + PhoneE164 column +} + +func (c publicreportSubscribePhoneColumns) AsSlice() []column { + return []column{ + c.Created, c.Deleted, c.DistrictID, c.PhoneE164, + } +} + +type publicreportSubscribePhoneIndexes struct { + SubscribePhonePkey index +} + +func (i publicreportSubscribePhoneIndexes) AsSlice() []index { + return []index{ + i.SubscribePhonePkey, + } +} + +type publicreportSubscribePhoneForeignKeys struct { + PublicreportSubscribePhoneSubscribePhoneDistrictIDFkey foreignKey + PublicreportSubscribePhoneSubscribePhonePhoneE164Fkey foreignKey +} + +func (f publicreportSubscribePhoneForeignKeys) AsSlice() []foreignKey { + return []foreignKey{ + f.PublicreportSubscribePhoneSubscribePhoneDistrictIDFkey, f.PublicreportSubscribePhoneSubscribePhonePhoneE164Fkey, + } +} + +type publicreportSubscribePhoneUniques struct{} + +func (u publicreportSubscribePhoneUniques) AsSlice() []constraint { + return []constraint{} +} + +type publicreportSubscribePhoneChecks struct{} + +func (c publicreportSubscribePhoneChecks) AsSlice() []check { + return []check{} +} diff --git a/db/factory/bobfactory_context.bob.go b/db/factory/bobfactory_context.bob.go index 05a6e922..088e0008 100644 --- a/db/factory/bobfactory_context.bob.go +++ b/db/factory/bobfactory_context.bob.go @@ -23,6 +23,7 @@ var ( commsEmailContactRelOrganizationsCtx = newContextual[bool]("comms.email_contact.organization.district_subscription_email.district_subscription_email_email_contact_address_fkeydistrict_subscription_email.district_subscription_email_organization_id_fkey") commsEmailContactRelEmailAddressNotifyEmailNuisancesCtx = newContextual[bool]("comms.email_contact.publicreport.notify_email_nuisance.publicreport.notify_email_nuisance.notify_email_nuisance_email_address_fkey") commsEmailContactRelEmailAddressNotifyEmailPoolsCtx = newContextual[bool]("comms.email_contact.publicreport.notify_email_pool.publicreport.notify_email_pool.notify_email_pool_email_address_fkey") + commsEmailContactRelEmailAddressSubscribeEmailsCtx = newContextual[bool]("comms.email_contact.publicreport.subscribe_email.publicreport.subscribe_email.subscribe_email_email_address_fkey") // Relationship Contexts for comms.email_log commsEmailLogWithParentsCascadingCtx = newContextual[bool]("commsEmailLogWithParentsCascading") @@ -41,6 +42,7 @@ var ( commsPhoneRelOrganizationsCtx = newContextual[bool]("comms.phone.organization.district_subscription_phone.district_subscription_phone_organization_id_fkeydistrict_subscription_phone.district_subscription_phone_phone_e164_fkey") commsPhoneRelPhoneE164NotifyPhoneNuisancesCtx = newContextual[bool]("comms.phone.publicreport.notify_phone_nuisance.publicreport.notify_phone_nuisance.notify_phone_nuisance_phone_e164_fkey") commsPhoneRelPhoneE164NotifyPhonePoolsCtx = newContextual[bool]("comms.phone.publicreport.notify_phone_pool.publicreport.notify_phone_pool.notify_phone_pool_phone_e164_fkey") + commsPhoneRelPhoneE164SubscribePhonesCtx = newContextual[bool]("comms.phone.publicreport.subscribe_phone.publicreport.subscribe_phone.subscribe_phone_phone_e164_fkey") // Relationship Contexts for comms.text_job commsTextJobWithParentsCascadingCtx = newContextual[bool]("commsTextJobWithParentsCascading") @@ -298,6 +300,8 @@ var ( organizationRelNuisancesCtx = newContextual[bool]("organization.publicreport.nuisance.publicreport.nuisance.nuisance_organization_id_fkey") organizationRelPublicreportPoolCtx = newContextual[bool]("organization.publicreport.pool.publicreport.pool.pool_organization_id_fkey") organizationRelQuicksCtx = newContextual[bool]("organization.publicreport.quick.publicreport.quick.quick_organization_id_fkey") + organizationRelDistrictSubscribeEmailsCtx = newContextual[bool]("organization.publicreport.subscribe_email.publicreport.subscribe_email.subscribe_email_district_id_fkey") + organizationRelDistrictSubscribePhonesCtx = newContextual[bool]("organization.publicreport.subscribe_phone.publicreport.subscribe_phone.subscribe_phone_district_id_fkey") organizationRelUserCtx = newContextual[bool]("organization.user_.user_.user__organization_id_fkey") // Relationship Contexts for publicreport.image @@ -368,6 +372,16 @@ var ( // Relationship Contexts for publicreport.report_location publicreportReportLocationWithParentsCascadingCtx = newContextual[bool]("publicreportReportLocationWithParentsCascading") + // Relationship Contexts for publicreport.subscribe_email + publicreportSubscribeEmailWithParentsCascadingCtx = newContextual[bool]("publicreportSubscribeEmailWithParentsCascading") + publicreportSubscribeEmailRelDistrictOrganizationCtx = newContextual[bool]("organization.publicreport.subscribe_email.publicreport.subscribe_email.subscribe_email_district_id_fkey") + publicreportSubscribeEmailRelEmailAddressEmailContactCtx = newContextual[bool]("comms.email_contact.publicreport.subscribe_email.publicreport.subscribe_email.subscribe_email_email_address_fkey") + + // Relationship Contexts for publicreport.subscribe_phone + publicreportSubscribePhoneWithParentsCascadingCtx = newContextual[bool]("publicreportSubscribePhoneWithParentsCascading") + publicreportSubscribePhoneRelDistrictOrganizationCtx = newContextual[bool]("organization.publicreport.subscribe_phone.publicreport.subscribe_phone.subscribe_phone_district_id_fkey") + publicreportSubscribePhoneRelPhoneE164PhoneCtx = newContextual[bool]("comms.phone.publicreport.subscribe_phone.publicreport.subscribe_phone.subscribe_phone_phone_e164_fkey") + // Relationship Contexts for raster_columns rasterColumnWithParentsCascadingCtx = newContextual[bool]("rasterColumnWithParentsCascading") diff --git a/db/factory/bobfactory_main.bob.go b/db/factory/bobfactory_main.bob.go index 3d71b0bb..b7329b46 100644 --- a/db/factory/bobfactory_main.bob.go +++ b/db/factory/bobfactory_main.bob.go @@ -89,6 +89,8 @@ type Factory struct { basePublicreportQuickMods PublicreportQuickModSlice basePublicreportQuickImageMods PublicreportQuickImageModSlice basePublicreportReportLocationMods PublicreportReportLocationModSlice + basePublicreportSubscribeEmailMods PublicreportSubscribeEmailModSlice + basePublicreportSubscribePhoneMods PublicreportSubscribePhoneModSlice baseRasterColumnMods RasterColumnModSlice baseRasterOverviewMods RasterOverviewModSlice baseSessionMods SessionModSlice @@ -212,6 +214,9 @@ func (f *Factory) FromExistingCommsEmailContact(m *models.CommsEmailContact) *Co if len(m.R.EmailAddressNotifyEmailPools) > 0 { CommsEmailContactMods.AddExistingEmailAddressNotifyEmailPools(m.R.EmailAddressNotifyEmailPools...).Apply(ctx, o) } + if len(m.R.EmailAddressSubscribeEmails) > 0 { + CommsEmailContactMods.AddExistingEmailAddressSubscribeEmails(m.R.EmailAddressSubscribeEmails...).Apply(ctx, o) + } return o } @@ -336,6 +341,9 @@ func (f *Factory) FromExistingCommsPhone(m *models.CommsPhone) *CommsPhoneTempla if len(m.R.PhoneE164NotifyPhonePools) > 0 { CommsPhoneMods.AddExistingPhoneE164NotifyPhonePools(m.R.PhoneE164NotifyPhonePools...).Apply(ctx, o) } + if len(m.R.PhoneE164SubscribePhones) > 0 { + CommsPhoneMods.AddExistingPhoneE164SubscribePhones(m.R.PhoneE164SubscribePhones...).Apply(ctx, o) + } return o } @@ -3060,6 +3068,12 @@ func (f *Factory) FromExistingOrganization(m *models.Organization) *Organization if len(m.R.Quicks) > 0 { OrganizationMods.AddExistingQuicks(m.R.Quicks...).Apply(ctx, o) } + if len(m.R.DistrictSubscribeEmails) > 0 { + OrganizationMods.AddExistingDistrictSubscribeEmails(m.R.DistrictSubscribeEmails...).Apply(ctx, o) + } + if len(m.R.DistrictSubscribePhones) > 0 { + OrganizationMods.AddExistingDistrictSubscribePhones(m.R.DistrictSubscribePhones...).Apply(ctx, o) + } if len(m.R.User) > 0 { OrganizationMods.AddExistingUser(m.R.User...).Apply(ctx, o) } @@ -3598,6 +3612,76 @@ func (f *Factory) FromExistingPublicreportReportLocation(m *models.PublicreportR return o } +func (f *Factory) NewPublicreportSubscribeEmail(mods ...PublicreportSubscribeEmailMod) *PublicreportSubscribeEmailTemplate { + return f.NewPublicreportSubscribeEmailWithContext(context.Background(), mods...) +} + +func (f *Factory) NewPublicreportSubscribeEmailWithContext(ctx context.Context, mods ...PublicreportSubscribeEmailMod) *PublicreportSubscribeEmailTemplate { + o := &PublicreportSubscribeEmailTemplate{f: f} + + if f != nil { + f.basePublicreportSubscribeEmailMods.Apply(ctx, o) + } + + PublicreportSubscribeEmailModSlice(mods).Apply(ctx, o) + + return o +} + +func (f *Factory) FromExistingPublicreportSubscribeEmail(m *models.PublicreportSubscribeEmail) *PublicreportSubscribeEmailTemplate { + o := &PublicreportSubscribeEmailTemplate{f: f, alreadyPersisted: true} + + o.Created = func() time.Time { return m.Created } + o.Deleted = func() null.Val[time.Time] { return m.Deleted } + o.DistrictID = func() int32 { return m.DistrictID } + o.EmailAddress = func() string { return m.EmailAddress } + + ctx := context.Background() + if m.R.DistrictOrganization != nil { + PublicreportSubscribeEmailMods.WithExistingDistrictOrganization(m.R.DistrictOrganization).Apply(ctx, o) + } + if m.R.EmailAddressEmailContact != nil { + PublicreportSubscribeEmailMods.WithExistingEmailAddressEmailContact(m.R.EmailAddressEmailContact).Apply(ctx, o) + } + + return o +} + +func (f *Factory) NewPublicreportSubscribePhone(mods ...PublicreportSubscribePhoneMod) *PublicreportSubscribePhoneTemplate { + return f.NewPublicreportSubscribePhoneWithContext(context.Background(), mods...) +} + +func (f *Factory) NewPublicreportSubscribePhoneWithContext(ctx context.Context, mods ...PublicreportSubscribePhoneMod) *PublicreportSubscribePhoneTemplate { + o := &PublicreportSubscribePhoneTemplate{f: f} + + if f != nil { + f.basePublicreportSubscribePhoneMods.Apply(ctx, o) + } + + PublicreportSubscribePhoneModSlice(mods).Apply(ctx, o) + + return o +} + +func (f *Factory) FromExistingPublicreportSubscribePhone(m *models.PublicreportSubscribePhone) *PublicreportSubscribePhoneTemplate { + o := &PublicreportSubscribePhoneTemplate{f: f, alreadyPersisted: true} + + o.Created = func() time.Time { return m.Created } + o.Deleted = func() null.Val[time.Time] { return m.Deleted } + o.DistrictID = func() int32 { return m.DistrictID } + o.PhoneE164 = func() string { return m.PhoneE164 } + + ctx := context.Background() + if m.R.DistrictOrganization != nil { + PublicreportSubscribePhoneMods.WithExistingDistrictOrganization(m.R.DistrictOrganization).Apply(ctx, o) + } + if m.R.PhoneE164Phone != nil { + PublicreportSubscribePhoneMods.WithExistingPhoneE164Phone(m.R.PhoneE164Phone).Apply(ctx, o) + } + + return o +} + func (f *Factory) NewRasterColumn(mods ...RasterColumnMod) *RasterColumnTemplate { return f.NewRasterColumnWithContext(context.Background(), mods...) } @@ -4351,6 +4435,22 @@ func (f *Factory) AddBasePublicreportReportLocationMod(mods ...PublicreportRepor f.basePublicreportReportLocationMods = append(f.basePublicreportReportLocationMods, mods...) } +func (f *Factory) ClearBasePublicreportSubscribeEmailMods() { + f.basePublicreportSubscribeEmailMods = nil +} + +func (f *Factory) AddBasePublicreportSubscribeEmailMod(mods ...PublicreportSubscribeEmailMod) { + f.basePublicreportSubscribeEmailMods = append(f.basePublicreportSubscribeEmailMods, mods...) +} + +func (f *Factory) ClearBasePublicreportSubscribePhoneMods() { + f.basePublicreportSubscribePhoneMods = nil +} + +func (f *Factory) AddBasePublicreportSubscribePhoneMod(mods ...PublicreportSubscribePhoneMod) { + f.basePublicreportSubscribePhoneMods = append(f.basePublicreportSubscribePhoneMods, mods...) +} + func (f *Factory) ClearBaseRasterColumnMods() { f.baseRasterColumnMods = nil } diff --git a/db/factory/comms.email_contact.bob.go b/db/factory/comms.email_contact.bob.go index 3f2ca2d2..2f89d433 100644 --- a/db/factory/comms.email_contact.bob.go +++ b/db/factory/comms.email_contact.bob.go @@ -50,6 +50,7 @@ type commsEmailContactR struct { Organizations []*commsEmailContactROrganizationsR EmailAddressNotifyEmailNuisances []*commsEmailContactREmailAddressNotifyEmailNuisancesR EmailAddressNotifyEmailPools []*commsEmailContactREmailAddressNotifyEmailPoolsR + EmailAddressSubscribeEmails []*commsEmailContactREmailAddressSubscribeEmailsR } type commsEmailContactRDestinationEmailLogsR struct { @@ -68,6 +69,10 @@ type commsEmailContactREmailAddressNotifyEmailPoolsR struct { number int o *PublicreportNotifyEmailPoolTemplate } +type commsEmailContactREmailAddressSubscribeEmailsR struct { + number int + o *PublicreportSubscribeEmailTemplate +} // Apply mods to the CommsEmailContactTemplate func (o *CommsEmailContactTemplate) Apply(ctx context.Context, mods ...CommsEmailContactMod) { @@ -129,6 +134,19 @@ func (t CommsEmailContactTemplate) setModelRels(o *models.CommsEmailContact) { } o.R.EmailAddressNotifyEmailPools = rel } + + if t.r.EmailAddressSubscribeEmails != nil { + rel := models.PublicreportSubscribeEmailSlice{} + for _, r := range t.r.EmailAddressSubscribeEmails { + related := r.o.BuildMany(r.number) + for _, rel := range related { + rel.EmailAddress = o.Address // h2 + rel.R.EmailAddressEmailContact = o + } + rel = append(rel, related...) + } + o.R.EmailAddressSubscribeEmails = rel + } } // BuildSetter returns an *models.CommsEmailContactSetter @@ -310,6 +328,26 @@ func (o *CommsEmailContactTemplate) insertOptRels(ctx context.Context, exec bob. } } + isEmailAddressSubscribeEmailsDone, _ := commsEmailContactRelEmailAddressSubscribeEmailsCtx.Value(ctx) + if !isEmailAddressSubscribeEmailsDone && o.r.EmailAddressSubscribeEmails != nil { + ctx = commsEmailContactRelEmailAddressSubscribeEmailsCtx.WithValue(ctx, true) + for _, r := range o.r.EmailAddressSubscribeEmails { + if r.o.alreadyPersisted { + m.R.EmailAddressSubscribeEmails = append(m.R.EmailAddressSubscribeEmails, r.o.Build()) + } else { + rel4, err := r.o.CreateMany(ctx, exec, r.number) + if err != nil { + return err + } + + err = m.AttachEmailAddressSubscribeEmails(ctx, exec, rel4...) + if err != nil { + return err + } + } + } + } + return err } @@ -733,3 +771,51 @@ func (m commsEmailContactMods) WithoutEmailAddressNotifyEmailPools() CommsEmailC o.r.EmailAddressNotifyEmailPools = nil }) } + +func (m commsEmailContactMods) WithEmailAddressSubscribeEmails(number int, related *PublicreportSubscribeEmailTemplate) CommsEmailContactMod { + return CommsEmailContactModFunc(func(ctx context.Context, o *CommsEmailContactTemplate) { + o.r.EmailAddressSubscribeEmails = []*commsEmailContactREmailAddressSubscribeEmailsR{{ + number: number, + o: related, + }} + }) +} + +func (m commsEmailContactMods) WithNewEmailAddressSubscribeEmails(number int, mods ...PublicreportSubscribeEmailMod) CommsEmailContactMod { + return CommsEmailContactModFunc(func(ctx context.Context, o *CommsEmailContactTemplate) { + related := o.f.NewPublicreportSubscribeEmailWithContext(ctx, mods...) + m.WithEmailAddressSubscribeEmails(number, related).Apply(ctx, o) + }) +} + +func (m commsEmailContactMods) AddEmailAddressSubscribeEmails(number int, related *PublicreportSubscribeEmailTemplate) CommsEmailContactMod { + return CommsEmailContactModFunc(func(ctx context.Context, o *CommsEmailContactTemplate) { + o.r.EmailAddressSubscribeEmails = append(o.r.EmailAddressSubscribeEmails, &commsEmailContactREmailAddressSubscribeEmailsR{ + number: number, + o: related, + }) + }) +} + +func (m commsEmailContactMods) AddNewEmailAddressSubscribeEmails(number int, mods ...PublicreportSubscribeEmailMod) CommsEmailContactMod { + return CommsEmailContactModFunc(func(ctx context.Context, o *CommsEmailContactTemplate) { + related := o.f.NewPublicreportSubscribeEmailWithContext(ctx, mods...) + m.AddEmailAddressSubscribeEmails(number, related).Apply(ctx, o) + }) +} + +func (m commsEmailContactMods) AddExistingEmailAddressSubscribeEmails(existingModels ...*models.PublicreportSubscribeEmail) CommsEmailContactMod { + return CommsEmailContactModFunc(func(ctx context.Context, o *CommsEmailContactTemplate) { + for _, em := range existingModels { + o.r.EmailAddressSubscribeEmails = append(o.r.EmailAddressSubscribeEmails, &commsEmailContactREmailAddressSubscribeEmailsR{ + o: o.f.FromExistingPublicreportSubscribeEmail(em), + }) + } + }) +} + +func (m commsEmailContactMods) WithoutEmailAddressSubscribeEmails() CommsEmailContactMod { + return CommsEmailContactModFunc(func(ctx context.Context, o *CommsEmailContactTemplate) { + o.r.EmailAddressSubscribeEmails = nil + }) +} diff --git a/db/factory/comms.phone.bob.go b/db/factory/comms.phone.bob.go index 09bf4ca2..23c43c7d 100644 --- a/db/factory/comms.phone.bob.go +++ b/db/factory/comms.phone.bob.go @@ -52,6 +52,7 @@ type commsPhoneR struct { Organizations []*commsPhoneROrganizationsR PhoneE164NotifyPhoneNuisances []*commsPhoneRPhoneE164NotifyPhoneNuisancesR PhoneE164NotifyPhonePools []*commsPhoneRPhoneE164NotifyPhonePoolsR + PhoneE164SubscribePhones []*commsPhoneRPhoneE164SubscribePhonesR } type commsPhoneRDestinationTextJobsR struct { @@ -78,6 +79,10 @@ type commsPhoneRPhoneE164NotifyPhonePoolsR struct { number int o *PublicreportNotifyPhonePoolTemplate } +type commsPhoneRPhoneE164SubscribePhonesR struct { + number int + o *PublicreportSubscribePhoneTemplate +} // Apply mods to the CommsPhoneTemplate func (o *CommsPhoneTemplate) Apply(ctx context.Context, mods ...CommsPhoneMod) { @@ -165,6 +170,19 @@ func (t CommsPhoneTemplate) setModelRels(o *models.CommsPhone) { } o.R.PhoneE164NotifyPhonePools = rel } + + if t.r.PhoneE164SubscribePhones != nil { + rel := models.PublicreportSubscribePhoneSlice{} + for _, r := range t.r.PhoneE164SubscribePhones { + related := r.o.BuildMany(r.number) + for _, rel := range related { + rel.PhoneE164 = o.E164 // h2 + rel.R.PhoneE164Phone = o + } + rel = append(rel, related...) + } + o.R.PhoneE164SubscribePhones = rel + } } // BuildSetter returns an *models.CommsPhoneSetter @@ -375,6 +393,26 @@ func (o *CommsPhoneTemplate) insertOptRels(ctx context.Context, exec bob.Executo } } + isPhoneE164SubscribePhonesDone, _ := commsPhoneRelPhoneE164SubscribePhonesCtx.Value(ctx) + if !isPhoneE164SubscribePhonesDone && o.r.PhoneE164SubscribePhones != nil { + ctx = commsPhoneRelPhoneE164SubscribePhonesCtx.WithValue(ctx, true) + for _, r := range o.r.PhoneE164SubscribePhones { + if r.o.alreadyPersisted { + m.R.PhoneE164SubscribePhones = append(m.R.PhoneE164SubscribePhones, r.o.Build()) + } else { + rel6, err := r.o.CreateMany(ctx, exec, r.number) + if err != nil { + return err + } + + err = m.AttachPhoneE164SubscribePhones(ctx, exec, rel6...) + if err != nil { + return err + } + } + } + } + return err } @@ -862,3 +900,51 @@ func (m commsPhoneMods) WithoutPhoneE164NotifyPhonePools() CommsPhoneMod { o.r.PhoneE164NotifyPhonePools = nil }) } + +func (m commsPhoneMods) WithPhoneE164SubscribePhones(number int, related *PublicreportSubscribePhoneTemplate) CommsPhoneMod { + return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) { + o.r.PhoneE164SubscribePhones = []*commsPhoneRPhoneE164SubscribePhonesR{{ + number: number, + o: related, + }} + }) +} + +func (m commsPhoneMods) WithNewPhoneE164SubscribePhones(number int, mods ...PublicreportSubscribePhoneMod) CommsPhoneMod { + return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) { + related := o.f.NewPublicreportSubscribePhoneWithContext(ctx, mods...) + m.WithPhoneE164SubscribePhones(number, related).Apply(ctx, o) + }) +} + +func (m commsPhoneMods) AddPhoneE164SubscribePhones(number int, related *PublicreportSubscribePhoneTemplate) CommsPhoneMod { + return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) { + o.r.PhoneE164SubscribePhones = append(o.r.PhoneE164SubscribePhones, &commsPhoneRPhoneE164SubscribePhonesR{ + number: number, + o: related, + }) + }) +} + +func (m commsPhoneMods) AddNewPhoneE164SubscribePhones(number int, mods ...PublicreportSubscribePhoneMod) CommsPhoneMod { + return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) { + related := o.f.NewPublicreportSubscribePhoneWithContext(ctx, mods...) + m.AddPhoneE164SubscribePhones(number, related).Apply(ctx, o) + }) +} + +func (m commsPhoneMods) AddExistingPhoneE164SubscribePhones(existingModels ...*models.PublicreportSubscribePhone) CommsPhoneMod { + return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) { + for _, em := range existingModels { + o.r.PhoneE164SubscribePhones = append(o.r.PhoneE164SubscribePhones, &commsPhoneRPhoneE164SubscribePhonesR{ + o: o.f.FromExistingPublicreportSubscribePhone(em), + }) + } + }) +} + +func (m commsPhoneMods) WithoutPhoneE164SubscribePhones() CommsPhoneMod { + return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) { + o.r.PhoneE164SubscribePhones = nil + }) +} diff --git a/db/factory/organization.bob.go b/db/factory/organization.bob.go index 5cc58818..932b05a5 100644 --- a/db/factory/organization.bob.go +++ b/db/factory/organization.bob.go @@ -93,6 +93,8 @@ type organizationR struct { Nuisances []*organizationRNuisancesR PublicreportPool []*organizationRPublicreportPoolR Quicks []*organizationRQuicksR + DistrictSubscribeEmails []*organizationRDistrictSubscribeEmailsR + DistrictSubscribePhones []*organizationRDistrictSubscribePhonesR User []*organizationRUserR } @@ -251,6 +253,14 @@ type organizationRQuicksR struct { number int o *PublicreportQuickTemplate } +type organizationRDistrictSubscribeEmailsR struct { + number int + o *PublicreportSubscribeEmailTemplate +} +type organizationRDistrictSubscribePhonesR struct { + number int + o *PublicreportSubscribePhoneTemplate +} type organizationRUserR struct { number int o *UserTemplate @@ -765,6 +775,32 @@ func (t OrganizationTemplate) setModelRels(o *models.Organization) { o.R.Quicks = rel } + if t.r.DistrictSubscribeEmails != nil { + rel := models.PublicreportSubscribeEmailSlice{} + for _, r := range t.r.DistrictSubscribeEmails { + related := r.o.BuildMany(r.number) + for _, rel := range related { + rel.DistrictID = o.ID // h2 + rel.R.DistrictOrganization = o + } + rel = append(rel, related...) + } + o.R.DistrictSubscribeEmails = rel + } + + if t.r.DistrictSubscribePhones != nil { + rel := models.PublicreportSubscribePhoneSlice{} + for _, r := range t.r.DistrictSubscribePhones { + related := r.o.BuildMany(r.number) + for _, rel := range related { + rel.DistrictID = o.ID // h2 + rel.R.DistrictOrganization = o + } + rel = append(rel, related...) + } + o.R.DistrictSubscribePhones = rel + } + if t.r.User != nil { rel := models.UserSlice{} for _, r := range t.r.User { @@ -1680,6 +1716,46 @@ func (o *OrganizationTemplate) insertOptRels(ctx context.Context, exec bob.Execu } } + isDistrictSubscribeEmailsDone, _ := organizationRelDistrictSubscribeEmailsCtx.Value(ctx) + if !isDistrictSubscribeEmailsDone && o.r.DistrictSubscribeEmails != nil { + ctx = organizationRelDistrictSubscribeEmailsCtx.WithValue(ctx, true) + for _, r := range o.r.DistrictSubscribeEmails { + if r.o.alreadyPersisted { + m.R.DistrictSubscribeEmails = append(m.R.DistrictSubscribeEmails, r.o.Build()) + } else { + rel39, err := r.o.CreateMany(ctx, exec, r.number) + if err != nil { + return err + } + + err = m.AttachDistrictSubscribeEmails(ctx, exec, rel39...) + if err != nil { + return err + } + } + } + } + + isDistrictSubscribePhonesDone, _ := organizationRelDistrictSubscribePhonesCtx.Value(ctx) + if !isDistrictSubscribePhonesDone && o.r.DistrictSubscribePhones != nil { + ctx = organizationRelDistrictSubscribePhonesCtx.WithValue(ctx, true) + for _, r := range o.r.DistrictSubscribePhones { + if r.o.alreadyPersisted { + m.R.DistrictSubscribePhones = append(m.R.DistrictSubscribePhones, r.o.Build()) + } else { + rel40, err := r.o.CreateMany(ctx, exec, r.number) + if err != nil { + return err + } + + err = m.AttachDistrictSubscribePhones(ctx, exec, rel40...) + if err != nil { + return err + } + } + } + } + isUserDone, _ := organizationRelUserCtx.Value(ctx) if !isUserDone && o.r.User != nil { ctx = organizationRelUserCtx.WithValue(ctx, true) @@ -1687,12 +1763,12 @@ func (o *OrganizationTemplate) insertOptRels(ctx context.Context, exec bob.Execu if r.o.alreadyPersisted { m.R.User = append(m.R.User, r.o.Build()) } else { - rel39, err := r.o.CreateMany(ctx, exec, r.number) + rel41, err := r.o.CreateMany(ctx, exec, r.number) if err != nil { return err } - err = m.AttachUser(ctx, exec, rel39...) + err = m.AttachUser(ctx, exec, rel41...) if err != nil { return err } @@ -4105,6 +4181,102 @@ func (m organizationMods) WithoutQuicks() OrganizationMod { }) } +func (m organizationMods) WithDistrictSubscribeEmails(number int, related *PublicreportSubscribeEmailTemplate) OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + o.r.DistrictSubscribeEmails = []*organizationRDistrictSubscribeEmailsR{{ + number: number, + o: related, + }} + }) +} + +func (m organizationMods) WithNewDistrictSubscribeEmails(number int, mods ...PublicreportSubscribeEmailMod) OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + related := o.f.NewPublicreportSubscribeEmailWithContext(ctx, mods...) + m.WithDistrictSubscribeEmails(number, related).Apply(ctx, o) + }) +} + +func (m organizationMods) AddDistrictSubscribeEmails(number int, related *PublicreportSubscribeEmailTemplate) OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + o.r.DistrictSubscribeEmails = append(o.r.DistrictSubscribeEmails, &organizationRDistrictSubscribeEmailsR{ + number: number, + o: related, + }) + }) +} + +func (m organizationMods) AddNewDistrictSubscribeEmails(number int, mods ...PublicreportSubscribeEmailMod) OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + related := o.f.NewPublicreportSubscribeEmailWithContext(ctx, mods...) + m.AddDistrictSubscribeEmails(number, related).Apply(ctx, o) + }) +} + +func (m organizationMods) AddExistingDistrictSubscribeEmails(existingModels ...*models.PublicreportSubscribeEmail) OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + for _, em := range existingModels { + o.r.DistrictSubscribeEmails = append(o.r.DistrictSubscribeEmails, &organizationRDistrictSubscribeEmailsR{ + o: o.f.FromExistingPublicreportSubscribeEmail(em), + }) + } + }) +} + +func (m organizationMods) WithoutDistrictSubscribeEmails() OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + o.r.DistrictSubscribeEmails = nil + }) +} + +func (m organizationMods) WithDistrictSubscribePhones(number int, related *PublicreportSubscribePhoneTemplate) OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + o.r.DistrictSubscribePhones = []*organizationRDistrictSubscribePhonesR{{ + number: number, + o: related, + }} + }) +} + +func (m organizationMods) WithNewDistrictSubscribePhones(number int, mods ...PublicreportSubscribePhoneMod) OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + related := o.f.NewPublicreportSubscribePhoneWithContext(ctx, mods...) + m.WithDistrictSubscribePhones(number, related).Apply(ctx, o) + }) +} + +func (m organizationMods) AddDistrictSubscribePhones(number int, related *PublicreportSubscribePhoneTemplate) OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + o.r.DistrictSubscribePhones = append(o.r.DistrictSubscribePhones, &organizationRDistrictSubscribePhonesR{ + number: number, + o: related, + }) + }) +} + +func (m organizationMods) AddNewDistrictSubscribePhones(number int, mods ...PublicreportSubscribePhoneMod) OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + related := o.f.NewPublicreportSubscribePhoneWithContext(ctx, mods...) + m.AddDistrictSubscribePhones(number, related).Apply(ctx, o) + }) +} + +func (m organizationMods) AddExistingDistrictSubscribePhones(existingModels ...*models.PublicreportSubscribePhone) OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + for _, em := range existingModels { + o.r.DistrictSubscribePhones = append(o.r.DistrictSubscribePhones, &organizationRDistrictSubscribePhonesR{ + o: o.f.FromExistingPublicreportSubscribePhone(em), + }) + } + }) +} + +func (m organizationMods) WithoutDistrictSubscribePhones() OrganizationMod { + return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { + o.r.DistrictSubscribePhones = nil + }) +} + func (m organizationMods) WithUser(number int, related *UserTemplate) OrganizationMod { return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) { o.r.User = []*organizationRUserR{{ diff --git a/db/factory/publicreport.subscribe_email.bob.go b/db/factory/publicreport.subscribe_email.bob.go new file mode 100644 index 00000000..5e6cde45 --- /dev/null +++ b/db/factory/publicreport.subscribe_email.bob.go @@ -0,0 +1,542 @@ +// Code generated by BobGen psql v0.42.5. DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package factory + +import ( + "context" + "testing" + "time" + + "github.com/Gleipnir-Technology/bob" + models "github.com/Gleipnir-Technology/nidus-sync/db/models" + "github.com/aarondl/opt/null" + "github.com/aarondl/opt/omit" + "github.com/aarondl/opt/omitnull" + "github.com/jaswdr/faker/v2" +) + +type PublicreportSubscribeEmailMod interface { + Apply(context.Context, *PublicreportSubscribeEmailTemplate) +} + +type PublicreportSubscribeEmailModFunc func(context.Context, *PublicreportSubscribeEmailTemplate) + +func (f PublicreportSubscribeEmailModFunc) Apply(ctx context.Context, n *PublicreportSubscribeEmailTemplate) { + f(ctx, n) +} + +type PublicreportSubscribeEmailModSlice []PublicreportSubscribeEmailMod + +func (mods PublicreportSubscribeEmailModSlice) Apply(ctx context.Context, n *PublicreportSubscribeEmailTemplate) { + for _, f := range mods { + f.Apply(ctx, n) + } +} + +// PublicreportSubscribeEmailTemplate is an object representing the database table. +// all columns are optional and should be set by mods +type PublicreportSubscribeEmailTemplate struct { + Created func() time.Time + Deleted func() null.Val[time.Time] + DistrictID func() int32 + EmailAddress func() string + + r publicreportSubscribeEmailR + f *Factory + + alreadyPersisted bool +} + +type publicreportSubscribeEmailR struct { + DistrictOrganization *publicreportSubscribeEmailRDistrictOrganizationR + EmailAddressEmailContact *publicreportSubscribeEmailREmailAddressEmailContactR +} + +type publicreportSubscribeEmailRDistrictOrganizationR struct { + o *OrganizationTemplate +} +type publicreportSubscribeEmailREmailAddressEmailContactR struct { + o *CommsEmailContactTemplate +} + +// Apply mods to the PublicreportSubscribeEmailTemplate +func (o *PublicreportSubscribeEmailTemplate) Apply(ctx context.Context, mods ...PublicreportSubscribeEmailMod) { + for _, mod := range mods { + mod.Apply(ctx, o) + } +} + +// setModelRels creates and sets the relationships on *models.PublicreportSubscribeEmail +// according to the relationships in the template. Nothing is inserted into the db +func (t PublicreportSubscribeEmailTemplate) setModelRels(o *models.PublicreportSubscribeEmail) { + if t.r.DistrictOrganization != nil { + rel := t.r.DistrictOrganization.o.Build() + rel.R.DistrictSubscribeEmails = append(rel.R.DistrictSubscribeEmails, o) + o.DistrictID = rel.ID // h2 + o.R.DistrictOrganization = rel + } + + if t.r.EmailAddressEmailContact != nil { + rel := t.r.EmailAddressEmailContact.o.Build() + rel.R.EmailAddressSubscribeEmails = append(rel.R.EmailAddressSubscribeEmails, o) + o.EmailAddress = rel.Address // h2 + o.R.EmailAddressEmailContact = rel + } +} + +// BuildSetter returns an *models.PublicreportSubscribeEmailSetter +// this does nothing with the relationship templates +func (o PublicreportSubscribeEmailTemplate) BuildSetter() *models.PublicreportSubscribeEmailSetter { + m := &models.PublicreportSubscribeEmailSetter{} + + if o.Created != nil { + val := o.Created() + m.Created = omit.From(val) + } + if o.Deleted != nil { + val := o.Deleted() + m.Deleted = omitnull.FromNull(val) + } + if o.DistrictID != nil { + val := o.DistrictID() + m.DistrictID = omit.From(val) + } + if o.EmailAddress != nil { + val := o.EmailAddress() + m.EmailAddress = omit.From(val) + } + + return m +} + +// BuildManySetter returns an []*models.PublicreportSubscribeEmailSetter +// this does nothing with the relationship templates +func (o PublicreportSubscribeEmailTemplate) BuildManySetter(number int) []*models.PublicreportSubscribeEmailSetter { + m := make([]*models.PublicreportSubscribeEmailSetter, number) + + for i := range m { + m[i] = o.BuildSetter() + } + + return m +} + +// Build returns an *models.PublicreportSubscribeEmail +// Related objects are also created and placed in the .R field +// NOTE: Objects are not inserted into the database. Use PublicreportSubscribeEmailTemplate.Create +func (o PublicreportSubscribeEmailTemplate) Build() *models.PublicreportSubscribeEmail { + m := &models.PublicreportSubscribeEmail{} + + if o.Created != nil { + m.Created = o.Created() + } + if o.Deleted != nil { + m.Deleted = o.Deleted() + } + if o.DistrictID != nil { + m.DistrictID = o.DistrictID() + } + if o.EmailAddress != nil { + m.EmailAddress = o.EmailAddress() + } + + o.setModelRels(m) + + return m +} + +// BuildMany returns an models.PublicreportSubscribeEmailSlice +// Related objects are also created and placed in the .R field +// NOTE: Objects are not inserted into the database. Use PublicreportSubscribeEmailTemplate.CreateMany +func (o PublicreportSubscribeEmailTemplate) BuildMany(number int) models.PublicreportSubscribeEmailSlice { + m := make(models.PublicreportSubscribeEmailSlice, number) + + for i := range m { + m[i] = o.Build() + } + + return m +} + +func ensureCreatablePublicreportSubscribeEmail(m *models.PublicreportSubscribeEmailSetter) { + if !(m.Created.IsValue()) { + val := random_time_Time(nil) + m.Created = omit.From(val) + } + if !(m.DistrictID.IsValue()) { + val := random_int32(nil) + m.DistrictID = omit.From(val) + } + if !(m.EmailAddress.IsValue()) { + val := random_string(nil) + m.EmailAddress = omit.From(val) + } +} + +// insertOptRels creates and inserts any optional the relationships on *models.PublicreportSubscribeEmail +// according to the relationships in the template. +// any required relationship should have already exist on the model +func (o *PublicreportSubscribeEmailTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.PublicreportSubscribeEmail) error { + var err error + + return err +} + +// Create builds a publicreportSubscribeEmail and inserts it into the database +// Relations objects are also inserted and placed in the .R field +func (o *PublicreportSubscribeEmailTemplate) Create(ctx context.Context, exec bob.Executor) (*models.PublicreportSubscribeEmail, error) { + var err error + opt := o.BuildSetter() + ensureCreatablePublicreportSubscribeEmail(opt) + + if o.r.DistrictOrganization == nil { + PublicreportSubscribeEmailMods.WithNewDistrictOrganization().Apply(ctx, o) + } + + var rel0 *models.Organization + + if o.r.DistrictOrganization.o.alreadyPersisted { + rel0 = o.r.DistrictOrganization.o.Build() + } else { + rel0, err = o.r.DistrictOrganization.o.Create(ctx, exec) + if err != nil { + return nil, err + } + } + + opt.DistrictID = omit.From(rel0.ID) + + if o.r.EmailAddressEmailContact == nil { + PublicreportSubscribeEmailMods.WithNewEmailAddressEmailContact().Apply(ctx, o) + } + + var rel1 *models.CommsEmailContact + + if o.r.EmailAddressEmailContact.o.alreadyPersisted { + rel1 = o.r.EmailAddressEmailContact.o.Build() + } else { + rel1, err = o.r.EmailAddressEmailContact.o.Create(ctx, exec) + if err != nil { + return nil, err + } + } + + opt.EmailAddress = omit.From(rel1.Address) + + m, err := models.PublicreportSubscribeEmails.Insert(opt).One(ctx, exec) + if err != nil { + return nil, err + } + + m.R.DistrictOrganization = rel0 + m.R.EmailAddressEmailContact = rel1 + + if err := o.insertOptRels(ctx, exec, m); err != nil { + return nil, err + } + return m, err +} + +// MustCreate builds a publicreportSubscribeEmail and inserts it into the database +// Relations objects are also inserted and placed in the .R field +// panics if an error occurs +func (o *PublicreportSubscribeEmailTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.PublicreportSubscribeEmail { + m, err := o.Create(ctx, exec) + if err != nil { + panic(err) + } + return m +} + +// CreateOrFail builds a publicreportSubscribeEmail and inserts it into the database +// Relations objects are also inserted and placed in the .R field +// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs +func (o *PublicreportSubscribeEmailTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.PublicreportSubscribeEmail { + tb.Helper() + m, err := o.Create(ctx, exec) + if err != nil { + tb.Fatal(err) + return nil + } + return m +} + +// CreateMany builds multiple publicreportSubscribeEmails and inserts them into the database +// Relations objects are also inserted and placed in the .R field +func (o PublicreportSubscribeEmailTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.PublicreportSubscribeEmailSlice, error) { + var err error + m := make(models.PublicreportSubscribeEmailSlice, number) + + for i := range m { + m[i], err = o.Create(ctx, exec) + if err != nil { + return nil, err + } + } + + return m, nil +} + +// MustCreateMany builds multiple publicreportSubscribeEmails and inserts them into the database +// Relations objects are also inserted and placed in the .R field +// panics if an error occurs +func (o PublicreportSubscribeEmailTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.PublicreportSubscribeEmailSlice { + m, err := o.CreateMany(ctx, exec, number) + if err != nil { + panic(err) + } + return m +} + +// CreateManyOrFail builds multiple publicreportSubscribeEmails and inserts them into the database +// Relations objects are also inserted and placed in the .R field +// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs +func (o PublicreportSubscribeEmailTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.PublicreportSubscribeEmailSlice { + tb.Helper() + m, err := o.CreateMany(ctx, exec, number) + if err != nil { + tb.Fatal(err) + return nil + } + return m +} + +// PublicreportSubscribeEmail has methods that act as mods for the PublicreportSubscribeEmailTemplate +var PublicreportSubscribeEmailMods publicreportSubscribeEmailMods + +type publicreportSubscribeEmailMods struct{} + +func (m publicreportSubscribeEmailMods) RandomizeAllColumns(f *faker.Faker) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModSlice{ + PublicreportSubscribeEmailMods.RandomCreated(f), + PublicreportSubscribeEmailMods.RandomDeleted(f), + PublicreportSubscribeEmailMods.RandomDistrictID(f), + PublicreportSubscribeEmailMods.RandomEmailAddress(f), + } +} + +// Set the model columns to this value +func (m publicreportSubscribeEmailMods) Created(val time.Time) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.Created = func() time.Time { return val } + }) +} + +// Set the Column from the function +func (m publicreportSubscribeEmailMods) CreatedFunc(f func() time.Time) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.Created = f + }) +} + +// Clear any values for the column +func (m publicreportSubscribeEmailMods) UnsetCreated() PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.Created = nil + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +func (m publicreportSubscribeEmailMods) RandomCreated(f *faker.Faker) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.Created = func() time.Time { + return random_time_Time(f) + } + }) +} + +// Set the model columns to this value +func (m publicreportSubscribeEmailMods) Deleted(val null.Val[time.Time]) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.Deleted = func() null.Val[time.Time] { return val } + }) +} + +// Set the Column from the function +func (m publicreportSubscribeEmailMods) DeletedFunc(f func() null.Val[time.Time]) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.Deleted = f + }) +} + +// Clear any values for the column +func (m publicreportSubscribeEmailMods) UnsetDeleted() PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.Deleted = nil + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +// The generated value is sometimes null +func (m publicreportSubscribeEmailMods) RandomDeleted(f *faker.Faker) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.Deleted = func() null.Val[time.Time] { + if f == nil { + f = &defaultFaker + } + + val := random_time_Time(f) + return null.From(val) + } + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +// The generated value is never null +func (m publicreportSubscribeEmailMods) RandomDeletedNotNull(f *faker.Faker) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.Deleted = func() null.Val[time.Time] { + if f == nil { + f = &defaultFaker + } + + val := random_time_Time(f) + return null.From(val) + } + }) +} + +// Set the model columns to this value +func (m publicreportSubscribeEmailMods) DistrictID(val int32) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.DistrictID = func() int32 { return val } + }) +} + +// Set the Column from the function +func (m publicreportSubscribeEmailMods) DistrictIDFunc(f func() int32) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.DistrictID = f + }) +} + +// Clear any values for the column +func (m publicreportSubscribeEmailMods) UnsetDistrictID() PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.DistrictID = nil + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +func (m publicreportSubscribeEmailMods) RandomDistrictID(f *faker.Faker) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.DistrictID = func() int32 { + return random_int32(f) + } + }) +} + +// Set the model columns to this value +func (m publicreportSubscribeEmailMods) EmailAddress(val string) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.EmailAddress = func() string { return val } + }) +} + +// Set the Column from the function +func (m publicreportSubscribeEmailMods) EmailAddressFunc(f func() string) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.EmailAddress = f + }) +} + +// Clear any values for the column +func (m publicreportSubscribeEmailMods) UnsetEmailAddress() PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.EmailAddress = nil + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +func (m publicreportSubscribeEmailMods) RandomEmailAddress(f *faker.Faker) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(_ context.Context, o *PublicreportSubscribeEmailTemplate) { + o.EmailAddress = func() string { + return random_string(f) + } + }) +} + +func (m publicreportSubscribeEmailMods) WithParentsCascading() PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(ctx context.Context, o *PublicreportSubscribeEmailTemplate) { + if isDone, _ := publicreportSubscribeEmailWithParentsCascadingCtx.Value(ctx); isDone { + return + } + ctx = publicreportSubscribeEmailWithParentsCascadingCtx.WithValue(ctx, true) + { + + related := o.f.NewOrganizationWithContext(ctx, OrganizationMods.WithParentsCascading()) + m.WithDistrictOrganization(related).Apply(ctx, o) + } + { + + related := o.f.NewCommsEmailContactWithContext(ctx, CommsEmailContactMods.WithParentsCascading()) + m.WithEmailAddressEmailContact(related).Apply(ctx, o) + } + }) +} + +func (m publicreportSubscribeEmailMods) WithDistrictOrganization(rel *OrganizationTemplate) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(ctx context.Context, o *PublicreportSubscribeEmailTemplate) { + o.r.DistrictOrganization = &publicreportSubscribeEmailRDistrictOrganizationR{ + o: rel, + } + }) +} + +func (m publicreportSubscribeEmailMods) WithNewDistrictOrganization(mods ...OrganizationMod) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(ctx context.Context, o *PublicreportSubscribeEmailTemplate) { + related := o.f.NewOrganizationWithContext(ctx, mods...) + + m.WithDistrictOrganization(related).Apply(ctx, o) + }) +} + +func (m publicreportSubscribeEmailMods) WithExistingDistrictOrganization(em *models.Organization) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(ctx context.Context, o *PublicreportSubscribeEmailTemplate) { + o.r.DistrictOrganization = &publicreportSubscribeEmailRDistrictOrganizationR{ + o: o.f.FromExistingOrganization(em), + } + }) +} + +func (m publicreportSubscribeEmailMods) WithoutDistrictOrganization() PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(ctx context.Context, o *PublicreportSubscribeEmailTemplate) { + o.r.DistrictOrganization = nil + }) +} + +func (m publicreportSubscribeEmailMods) WithEmailAddressEmailContact(rel *CommsEmailContactTemplate) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(ctx context.Context, o *PublicreportSubscribeEmailTemplate) { + o.r.EmailAddressEmailContact = &publicreportSubscribeEmailREmailAddressEmailContactR{ + o: rel, + } + }) +} + +func (m publicreportSubscribeEmailMods) WithNewEmailAddressEmailContact(mods ...CommsEmailContactMod) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(ctx context.Context, o *PublicreportSubscribeEmailTemplate) { + related := o.f.NewCommsEmailContactWithContext(ctx, mods...) + + m.WithEmailAddressEmailContact(related).Apply(ctx, o) + }) +} + +func (m publicreportSubscribeEmailMods) WithExistingEmailAddressEmailContact(em *models.CommsEmailContact) PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(ctx context.Context, o *PublicreportSubscribeEmailTemplate) { + o.r.EmailAddressEmailContact = &publicreportSubscribeEmailREmailAddressEmailContactR{ + o: o.f.FromExistingCommsEmailContact(em), + } + }) +} + +func (m publicreportSubscribeEmailMods) WithoutEmailAddressEmailContact() PublicreportSubscribeEmailMod { + return PublicreportSubscribeEmailModFunc(func(ctx context.Context, o *PublicreportSubscribeEmailTemplate) { + o.r.EmailAddressEmailContact = nil + }) +} diff --git a/db/factory/publicreport.subscribe_phone.bob.go b/db/factory/publicreport.subscribe_phone.bob.go new file mode 100644 index 00000000..c0370c54 --- /dev/null +++ b/db/factory/publicreport.subscribe_phone.bob.go @@ -0,0 +1,542 @@ +// Code generated by BobGen psql v0.42.5. DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package factory + +import ( + "context" + "testing" + "time" + + "github.com/Gleipnir-Technology/bob" + models "github.com/Gleipnir-Technology/nidus-sync/db/models" + "github.com/aarondl/opt/null" + "github.com/aarondl/opt/omit" + "github.com/aarondl/opt/omitnull" + "github.com/jaswdr/faker/v2" +) + +type PublicreportSubscribePhoneMod interface { + Apply(context.Context, *PublicreportSubscribePhoneTemplate) +} + +type PublicreportSubscribePhoneModFunc func(context.Context, *PublicreportSubscribePhoneTemplate) + +func (f PublicreportSubscribePhoneModFunc) Apply(ctx context.Context, n *PublicreportSubscribePhoneTemplate) { + f(ctx, n) +} + +type PublicreportSubscribePhoneModSlice []PublicreportSubscribePhoneMod + +func (mods PublicreportSubscribePhoneModSlice) Apply(ctx context.Context, n *PublicreportSubscribePhoneTemplate) { + for _, f := range mods { + f.Apply(ctx, n) + } +} + +// PublicreportSubscribePhoneTemplate is an object representing the database table. +// all columns are optional and should be set by mods +type PublicreportSubscribePhoneTemplate struct { + Created func() time.Time + Deleted func() null.Val[time.Time] + DistrictID func() int32 + PhoneE164 func() string + + r publicreportSubscribePhoneR + f *Factory + + alreadyPersisted bool +} + +type publicreportSubscribePhoneR struct { + DistrictOrganization *publicreportSubscribePhoneRDistrictOrganizationR + PhoneE164Phone *publicreportSubscribePhoneRPhoneE164PhoneR +} + +type publicreportSubscribePhoneRDistrictOrganizationR struct { + o *OrganizationTemplate +} +type publicreportSubscribePhoneRPhoneE164PhoneR struct { + o *CommsPhoneTemplate +} + +// Apply mods to the PublicreportSubscribePhoneTemplate +func (o *PublicreportSubscribePhoneTemplate) Apply(ctx context.Context, mods ...PublicreportSubscribePhoneMod) { + for _, mod := range mods { + mod.Apply(ctx, o) + } +} + +// setModelRels creates and sets the relationships on *models.PublicreportSubscribePhone +// according to the relationships in the template. Nothing is inserted into the db +func (t PublicreportSubscribePhoneTemplate) setModelRels(o *models.PublicreportSubscribePhone) { + if t.r.DistrictOrganization != nil { + rel := t.r.DistrictOrganization.o.Build() + rel.R.DistrictSubscribePhones = append(rel.R.DistrictSubscribePhones, o) + o.DistrictID = rel.ID // h2 + o.R.DistrictOrganization = rel + } + + if t.r.PhoneE164Phone != nil { + rel := t.r.PhoneE164Phone.o.Build() + rel.R.PhoneE164SubscribePhones = append(rel.R.PhoneE164SubscribePhones, o) + o.PhoneE164 = rel.E164 // h2 + o.R.PhoneE164Phone = rel + } +} + +// BuildSetter returns an *models.PublicreportSubscribePhoneSetter +// this does nothing with the relationship templates +func (o PublicreportSubscribePhoneTemplate) BuildSetter() *models.PublicreportSubscribePhoneSetter { + m := &models.PublicreportSubscribePhoneSetter{} + + if o.Created != nil { + val := o.Created() + m.Created = omit.From(val) + } + if o.Deleted != nil { + val := o.Deleted() + m.Deleted = omitnull.FromNull(val) + } + if o.DistrictID != nil { + val := o.DistrictID() + m.DistrictID = omit.From(val) + } + if o.PhoneE164 != nil { + val := o.PhoneE164() + m.PhoneE164 = omit.From(val) + } + + return m +} + +// BuildManySetter returns an []*models.PublicreportSubscribePhoneSetter +// this does nothing with the relationship templates +func (o PublicreportSubscribePhoneTemplate) BuildManySetter(number int) []*models.PublicreportSubscribePhoneSetter { + m := make([]*models.PublicreportSubscribePhoneSetter, number) + + for i := range m { + m[i] = o.BuildSetter() + } + + return m +} + +// Build returns an *models.PublicreportSubscribePhone +// Related objects are also created and placed in the .R field +// NOTE: Objects are not inserted into the database. Use PublicreportSubscribePhoneTemplate.Create +func (o PublicreportSubscribePhoneTemplate) Build() *models.PublicreportSubscribePhone { + m := &models.PublicreportSubscribePhone{} + + if o.Created != nil { + m.Created = o.Created() + } + if o.Deleted != nil { + m.Deleted = o.Deleted() + } + if o.DistrictID != nil { + m.DistrictID = o.DistrictID() + } + if o.PhoneE164 != nil { + m.PhoneE164 = o.PhoneE164() + } + + o.setModelRels(m) + + return m +} + +// BuildMany returns an models.PublicreportSubscribePhoneSlice +// Related objects are also created and placed in the .R field +// NOTE: Objects are not inserted into the database. Use PublicreportSubscribePhoneTemplate.CreateMany +func (o PublicreportSubscribePhoneTemplate) BuildMany(number int) models.PublicreportSubscribePhoneSlice { + m := make(models.PublicreportSubscribePhoneSlice, number) + + for i := range m { + m[i] = o.Build() + } + + return m +} + +func ensureCreatablePublicreportSubscribePhone(m *models.PublicreportSubscribePhoneSetter) { + if !(m.Created.IsValue()) { + val := random_time_Time(nil) + m.Created = omit.From(val) + } + if !(m.DistrictID.IsValue()) { + val := random_int32(nil) + m.DistrictID = omit.From(val) + } + if !(m.PhoneE164.IsValue()) { + val := random_string(nil) + m.PhoneE164 = omit.From(val) + } +} + +// insertOptRels creates and inserts any optional the relationships on *models.PublicreportSubscribePhone +// according to the relationships in the template. +// any required relationship should have already exist on the model +func (o *PublicreportSubscribePhoneTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.PublicreportSubscribePhone) error { + var err error + + return err +} + +// Create builds a publicreportSubscribePhone and inserts it into the database +// Relations objects are also inserted and placed in the .R field +func (o *PublicreportSubscribePhoneTemplate) Create(ctx context.Context, exec bob.Executor) (*models.PublicreportSubscribePhone, error) { + var err error + opt := o.BuildSetter() + ensureCreatablePublicreportSubscribePhone(opt) + + if o.r.DistrictOrganization == nil { + PublicreportSubscribePhoneMods.WithNewDistrictOrganization().Apply(ctx, o) + } + + var rel0 *models.Organization + + if o.r.DistrictOrganization.o.alreadyPersisted { + rel0 = o.r.DistrictOrganization.o.Build() + } else { + rel0, err = o.r.DistrictOrganization.o.Create(ctx, exec) + if err != nil { + return nil, err + } + } + + opt.DistrictID = omit.From(rel0.ID) + + if o.r.PhoneE164Phone == nil { + PublicreportSubscribePhoneMods.WithNewPhoneE164Phone().Apply(ctx, o) + } + + var rel1 *models.CommsPhone + + if o.r.PhoneE164Phone.o.alreadyPersisted { + rel1 = o.r.PhoneE164Phone.o.Build() + } else { + rel1, err = o.r.PhoneE164Phone.o.Create(ctx, exec) + if err != nil { + return nil, err + } + } + + opt.PhoneE164 = omit.From(rel1.E164) + + m, err := models.PublicreportSubscribePhones.Insert(opt).One(ctx, exec) + if err != nil { + return nil, err + } + + m.R.DistrictOrganization = rel0 + m.R.PhoneE164Phone = rel1 + + if err := o.insertOptRels(ctx, exec, m); err != nil { + return nil, err + } + return m, err +} + +// MustCreate builds a publicreportSubscribePhone and inserts it into the database +// Relations objects are also inserted and placed in the .R field +// panics if an error occurs +func (o *PublicreportSubscribePhoneTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.PublicreportSubscribePhone { + m, err := o.Create(ctx, exec) + if err != nil { + panic(err) + } + return m +} + +// CreateOrFail builds a publicreportSubscribePhone and inserts it into the database +// Relations objects are also inserted and placed in the .R field +// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs +func (o *PublicreportSubscribePhoneTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.PublicreportSubscribePhone { + tb.Helper() + m, err := o.Create(ctx, exec) + if err != nil { + tb.Fatal(err) + return nil + } + return m +} + +// CreateMany builds multiple publicreportSubscribePhones and inserts them into the database +// Relations objects are also inserted and placed in the .R field +func (o PublicreportSubscribePhoneTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.PublicreportSubscribePhoneSlice, error) { + var err error + m := make(models.PublicreportSubscribePhoneSlice, number) + + for i := range m { + m[i], err = o.Create(ctx, exec) + if err != nil { + return nil, err + } + } + + return m, nil +} + +// MustCreateMany builds multiple publicreportSubscribePhones and inserts them into the database +// Relations objects are also inserted and placed in the .R field +// panics if an error occurs +func (o PublicreportSubscribePhoneTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.PublicreportSubscribePhoneSlice { + m, err := o.CreateMany(ctx, exec, number) + if err != nil { + panic(err) + } + return m +} + +// CreateManyOrFail builds multiple publicreportSubscribePhones and inserts them into the database +// Relations objects are also inserted and placed in the .R field +// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs +func (o PublicreportSubscribePhoneTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.PublicreportSubscribePhoneSlice { + tb.Helper() + m, err := o.CreateMany(ctx, exec, number) + if err != nil { + tb.Fatal(err) + return nil + } + return m +} + +// PublicreportSubscribePhone has methods that act as mods for the PublicreportSubscribePhoneTemplate +var PublicreportSubscribePhoneMods publicreportSubscribePhoneMods + +type publicreportSubscribePhoneMods struct{} + +func (m publicreportSubscribePhoneMods) RandomizeAllColumns(f *faker.Faker) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModSlice{ + PublicreportSubscribePhoneMods.RandomCreated(f), + PublicreportSubscribePhoneMods.RandomDeleted(f), + PublicreportSubscribePhoneMods.RandomDistrictID(f), + PublicreportSubscribePhoneMods.RandomPhoneE164(f), + } +} + +// Set the model columns to this value +func (m publicreportSubscribePhoneMods) Created(val time.Time) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.Created = func() time.Time { return val } + }) +} + +// Set the Column from the function +func (m publicreportSubscribePhoneMods) CreatedFunc(f func() time.Time) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.Created = f + }) +} + +// Clear any values for the column +func (m publicreportSubscribePhoneMods) UnsetCreated() PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.Created = nil + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +func (m publicreportSubscribePhoneMods) RandomCreated(f *faker.Faker) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.Created = func() time.Time { + return random_time_Time(f) + } + }) +} + +// Set the model columns to this value +func (m publicreportSubscribePhoneMods) Deleted(val null.Val[time.Time]) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.Deleted = func() null.Val[time.Time] { return val } + }) +} + +// Set the Column from the function +func (m publicreportSubscribePhoneMods) DeletedFunc(f func() null.Val[time.Time]) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.Deleted = f + }) +} + +// Clear any values for the column +func (m publicreportSubscribePhoneMods) UnsetDeleted() PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.Deleted = nil + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +// The generated value is sometimes null +func (m publicreportSubscribePhoneMods) RandomDeleted(f *faker.Faker) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.Deleted = func() null.Val[time.Time] { + if f == nil { + f = &defaultFaker + } + + val := random_time_Time(f) + return null.From(val) + } + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +// The generated value is never null +func (m publicreportSubscribePhoneMods) RandomDeletedNotNull(f *faker.Faker) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.Deleted = func() null.Val[time.Time] { + if f == nil { + f = &defaultFaker + } + + val := random_time_Time(f) + return null.From(val) + } + }) +} + +// Set the model columns to this value +func (m publicreportSubscribePhoneMods) DistrictID(val int32) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.DistrictID = func() int32 { return val } + }) +} + +// Set the Column from the function +func (m publicreportSubscribePhoneMods) DistrictIDFunc(f func() int32) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.DistrictID = f + }) +} + +// Clear any values for the column +func (m publicreportSubscribePhoneMods) UnsetDistrictID() PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.DistrictID = nil + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +func (m publicreportSubscribePhoneMods) RandomDistrictID(f *faker.Faker) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.DistrictID = func() int32 { + return random_int32(f) + } + }) +} + +// Set the model columns to this value +func (m publicreportSubscribePhoneMods) PhoneE164(val string) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.PhoneE164 = func() string { return val } + }) +} + +// Set the Column from the function +func (m publicreportSubscribePhoneMods) PhoneE164Func(f func() string) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.PhoneE164 = f + }) +} + +// Clear any values for the column +func (m publicreportSubscribePhoneMods) UnsetPhoneE164() PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.PhoneE164 = nil + }) +} + +// Generates a random value for the column using the given faker +// if faker is nil, a default faker is used +func (m publicreportSubscribePhoneMods) RandomPhoneE164(f *faker.Faker) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(_ context.Context, o *PublicreportSubscribePhoneTemplate) { + o.PhoneE164 = func() string { + return random_string(f) + } + }) +} + +func (m publicreportSubscribePhoneMods) WithParentsCascading() PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) { + if isDone, _ := publicreportSubscribePhoneWithParentsCascadingCtx.Value(ctx); isDone { + return + } + ctx = publicreportSubscribePhoneWithParentsCascadingCtx.WithValue(ctx, true) + { + + related := o.f.NewOrganizationWithContext(ctx, OrganizationMods.WithParentsCascading()) + m.WithDistrictOrganization(related).Apply(ctx, o) + } + { + + related := o.f.NewCommsPhoneWithContext(ctx, CommsPhoneMods.WithParentsCascading()) + m.WithPhoneE164Phone(related).Apply(ctx, o) + } + }) +} + +func (m publicreportSubscribePhoneMods) WithDistrictOrganization(rel *OrganizationTemplate) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) { + o.r.DistrictOrganization = &publicreportSubscribePhoneRDistrictOrganizationR{ + o: rel, + } + }) +} + +func (m publicreportSubscribePhoneMods) WithNewDistrictOrganization(mods ...OrganizationMod) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) { + related := o.f.NewOrganizationWithContext(ctx, mods...) + + m.WithDistrictOrganization(related).Apply(ctx, o) + }) +} + +func (m publicreportSubscribePhoneMods) WithExistingDistrictOrganization(em *models.Organization) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) { + o.r.DistrictOrganization = &publicreportSubscribePhoneRDistrictOrganizationR{ + o: o.f.FromExistingOrganization(em), + } + }) +} + +func (m publicreportSubscribePhoneMods) WithoutDistrictOrganization() PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) { + o.r.DistrictOrganization = nil + }) +} + +func (m publicreportSubscribePhoneMods) WithPhoneE164Phone(rel *CommsPhoneTemplate) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) { + o.r.PhoneE164Phone = &publicreportSubscribePhoneRPhoneE164PhoneR{ + o: rel, + } + }) +} + +func (m publicreportSubscribePhoneMods) WithNewPhoneE164Phone(mods ...CommsPhoneMod) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) { + related := o.f.NewCommsPhoneWithContext(ctx, mods...) + + m.WithPhoneE164Phone(related).Apply(ctx, o) + }) +} + +func (m publicreportSubscribePhoneMods) WithExistingPhoneE164Phone(em *models.CommsPhone) PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) { + o.r.PhoneE164Phone = &publicreportSubscribePhoneRPhoneE164PhoneR{ + o: o.f.FromExistingCommsPhone(em), + } + }) +} + +func (m publicreportSubscribePhoneMods) WithoutPhoneE164Phone() PublicreportSubscribePhoneMod { + return PublicreportSubscribePhoneModFunc(func(ctx context.Context, o *PublicreportSubscribePhoneTemplate) { + o.r.PhoneE164Phone = nil + }) +} diff --git a/db/migrations/00058_district_subscription.sql b/db/migrations/00058_district_subscription.sql new file mode 100644 index 00000000..21f0ede3 --- /dev/null +++ b/db/migrations/00058_district_subscription.sql @@ -0,0 +1,15 @@ +-- +goose Up +CREATE TABLE publicreport.subscribe_email ( + created TIMESTAMP WITHOUT TIME ZONE NOT NULL, + deleted TIMESTAMP WITHOUT TIME ZONE, + district_id INTEGER REFERENCES organization(id), + email_address TEXT NOT NULL REFERENCES comms.email_contact(address), + PRIMARY KEY(district_id, email_address) +); +CREATE TABLE publicreport.subscribe_phone ( + created TIMESTAMP WITHOUT TIME ZONE NOT NULL, + deleted TIMESTAMP WITHOUT TIME ZONE, + district_id INTEGER REFERENCES organization(id), + phone_e164 TEXT NOT NULL REFERENCES comms.phone(e164), + PRIMARY KEY(district_id, phone_e164) +); diff --git a/db/models/bob_joins.bob.go b/db/models/bob_joins.bob.go index d029bf20..3a404754 100644 --- a/db/models/bob_joins.bob.go +++ b/db/models/bob_joins.bob.go @@ -98,6 +98,8 @@ type joins[Q dialect.Joinable] struct { PublicreportPoolImages joinSet[publicreportPoolImageJoins[Q]] PublicreportQuicks joinSet[publicreportQuickJoins[Q]] PublicreportQuickImages joinSet[publicreportQuickImageJoins[Q]] + PublicreportSubscribeEmails joinSet[publicreportSubscribeEmailJoins[Q]] + PublicreportSubscribePhones joinSet[publicreportSubscribePhoneJoins[Q]] Users joinSet[userJoins[Q]] } @@ -177,6 +179,8 @@ func getJoins[Q dialect.Joinable]() joins[Q] { PublicreportPoolImages: buildJoinSet[publicreportPoolImageJoins[Q]](PublicreportPoolImages.Columns, buildPublicreportPoolImageJoins), PublicreportQuicks: buildJoinSet[publicreportQuickJoins[Q]](PublicreportQuicks.Columns, buildPublicreportQuickJoins), PublicreportQuickImages: buildJoinSet[publicreportQuickImageJoins[Q]](PublicreportQuickImages.Columns, buildPublicreportQuickImageJoins), + PublicreportSubscribeEmails: buildJoinSet[publicreportSubscribeEmailJoins[Q]](PublicreportSubscribeEmails.Columns, buildPublicreportSubscribeEmailJoins), + PublicreportSubscribePhones: buildJoinSet[publicreportSubscribePhoneJoins[Q]](PublicreportSubscribePhones.Columns, buildPublicreportSubscribePhoneJoins), Users: buildJoinSet[userJoins[Q]](Users.Columns, buildUserJoins), } } diff --git a/db/models/bob_loaders.bob.go b/db/models/bob_loaders.bob.go index caacacd9..7bea80ed 100644 --- a/db/models/bob_loaders.bob.go +++ b/db/models/bob_loaders.bob.go @@ -83,6 +83,8 @@ type preloaders struct { PublicreportPoolImage publicreportPoolImagePreloader PublicreportQuick publicreportQuickPreloader PublicreportQuickImage publicreportQuickImagePreloader + PublicreportSubscribeEmail publicreportSubscribeEmailPreloader + PublicreportSubscribePhone publicreportSubscribePhonePreloader User userPreloader } @@ -154,6 +156,8 @@ func getPreloaders() preloaders { PublicreportPoolImage: buildPublicreportPoolImagePreloader(), PublicreportQuick: buildPublicreportQuickPreloader(), PublicreportQuickImage: buildPublicreportQuickImagePreloader(), + PublicreportSubscribeEmail: buildPublicreportSubscribeEmailPreloader(), + PublicreportSubscribePhone: buildPublicreportSubscribePhonePreloader(), User: buildUserPreloader(), } } @@ -231,6 +235,8 @@ type thenLoaders[Q orm.Loadable] struct { PublicreportPoolImage publicreportPoolImageThenLoader[Q] PublicreportQuick publicreportQuickThenLoader[Q] PublicreportQuickImage publicreportQuickImageThenLoader[Q] + PublicreportSubscribeEmail publicreportSubscribeEmailThenLoader[Q] + PublicreportSubscribePhone publicreportSubscribePhoneThenLoader[Q] User userThenLoader[Q] } @@ -302,6 +308,8 @@ func getThenLoaders[Q orm.Loadable]() thenLoaders[Q] { PublicreportPoolImage: buildPublicreportPoolImageThenLoader[Q](), PublicreportQuick: buildPublicreportQuickThenLoader[Q](), PublicreportQuickImage: buildPublicreportQuickImageThenLoader[Q](), + PublicreportSubscribeEmail: buildPublicreportSubscribeEmailThenLoader[Q](), + PublicreportSubscribePhone: buildPublicreportSubscribePhoneThenLoader[Q](), User: buildUserThenLoader[Q](), } } diff --git a/db/models/bob_where.bob.go b/db/models/bob_where.bob.go index e6785af6..16a7045c 100644 --- a/db/models/bob_where.bob.go +++ b/db/models/bob_where.bob.go @@ -87,6 +87,8 @@ func Where[Q psql.Filterable]() struct { PublicreportQuicks publicreportQuickWhere[Q] PublicreportQuickImages publicreportQuickImageWhere[Q] PublicreportReportLocations publicreportReportLocationWhere[Q] + PublicreportSubscribeEmails publicreportSubscribeEmailWhere[Q] + PublicreportSubscribePhones publicreportSubscribePhoneWhere[Q] RasterColumns rasterColumnWhere[Q] RasterOverviews rasterOverviewWhere[Q] Sessions sessionWhere[Q] @@ -164,6 +166,8 @@ func Where[Q psql.Filterable]() struct { PublicreportQuicks publicreportQuickWhere[Q] PublicreportQuickImages publicreportQuickImageWhere[Q] PublicreportReportLocations publicreportReportLocationWhere[Q] + PublicreportSubscribeEmails publicreportSubscribeEmailWhere[Q] + PublicreportSubscribePhones publicreportSubscribePhoneWhere[Q] RasterColumns rasterColumnWhere[Q] RasterOverviews rasterOverviewWhere[Q] Sessions sessionWhere[Q] @@ -240,6 +244,8 @@ func Where[Q psql.Filterable]() struct { PublicreportQuicks: buildPublicreportQuickWhere[Q](PublicreportQuicks.Columns), PublicreportQuickImages: buildPublicreportQuickImageWhere[Q](PublicreportQuickImages.Columns), PublicreportReportLocations: buildPublicreportReportLocationWhere[Q](PublicreportReportLocations.Columns), + PublicreportSubscribeEmails: buildPublicreportSubscribeEmailWhere[Q](PublicreportSubscribeEmails.Columns), + PublicreportSubscribePhones: buildPublicreportSubscribePhoneWhere[Q](PublicreportSubscribePhones.Columns), RasterColumns: buildRasterColumnWhere[Q](RasterColumns.Columns), RasterOverviews: buildRasterOverviewWhere[Q](RasterOverviews.Columns), Sessions: buildSessionWhere[Q](Sessions.Columns), diff --git a/db/models/comms.email_contact.bob.go b/db/models/comms.email_contact.bob.go index 1823bc67..5d534c2a 100644 --- a/db/models/comms.email_contact.bob.go +++ b/db/models/comms.email_contact.bob.go @@ -51,6 +51,7 @@ type commsEmailContactR struct { Organizations OrganizationSlice // district_subscription_email.district_subscription_email_email_contact_address_fkeydistrict_subscription_email.district_subscription_email_organization_id_fkey EmailAddressNotifyEmailNuisances PublicreportNotifyEmailNuisanceSlice // publicreport.notify_email_nuisance.notify_email_nuisance_email_address_fkey EmailAddressNotifyEmailPools PublicreportNotifyEmailPoolSlice // publicreport.notify_email_pool.notify_email_pool_email_address_fkey + EmailAddressSubscribeEmails PublicreportSubscribeEmailSlice // publicreport.subscribe_email.subscribe_email_email_address_fkey } func buildCommsEmailContactColumns(alias string) commsEmailContactColumns { @@ -522,6 +523,30 @@ func (os CommsEmailContactSlice) EmailAddressNotifyEmailPools(mods ...bob.Mod[*d )...) } +// EmailAddressSubscribeEmails starts a query for related objects on publicreport.subscribe_email +func (o *CommsEmailContact) EmailAddressSubscribeEmails(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportSubscribeEmailsQuery { + return PublicreportSubscribeEmails.Query(append(mods, + sm.Where(PublicreportSubscribeEmails.Columns.EmailAddress.EQ(psql.Arg(o.Address))), + )...) +} + +func (os CommsEmailContactSlice) EmailAddressSubscribeEmails(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportSubscribeEmailsQuery { + pkAddress := make(pgtypes.Array[string], 0, len(os)) + for _, o := range os { + if o == nil { + continue + } + pkAddress = append(pkAddress, o.Address) + } + PKArgExpr := psql.Select(sm.Columns( + psql.F("unnest", psql.Cast(psql.Arg(pkAddress), "text[]")), + )) + + return PublicreportSubscribeEmails.Query(append(mods, + sm.Where(psql.Group(PublicreportSubscribeEmails.Columns.EmailAddress).OP("IN", PKArgExpr)), + )...) +} + func insertCommsEmailContactDestinationEmailLogs0(ctx context.Context, exec bob.Executor, commsEmailLogs1 []*CommsEmailLogSetter, commsEmailContact0 *CommsEmailContact) (CommsEmailLogSlice, error) { for i := range commsEmailLogs1 { commsEmailLogs1[i].Destination = omit.From(commsEmailContact0.Address) @@ -791,6 +816,74 @@ func (commsEmailContact0 *CommsEmailContact) AttachEmailAddressNotifyEmailPools( return nil } +func insertCommsEmailContactEmailAddressSubscribeEmails0(ctx context.Context, exec bob.Executor, publicreportSubscribeEmails1 []*PublicreportSubscribeEmailSetter, commsEmailContact0 *CommsEmailContact) (PublicreportSubscribeEmailSlice, error) { + for i := range publicreportSubscribeEmails1 { + publicreportSubscribeEmails1[i].EmailAddress = omit.From(commsEmailContact0.Address) + } + + ret, err := PublicreportSubscribeEmails.Insert(bob.ToMods(publicreportSubscribeEmails1...)).All(ctx, exec) + if err != nil { + return ret, fmt.Errorf("insertCommsEmailContactEmailAddressSubscribeEmails0: %w", err) + } + + return ret, nil +} + +func attachCommsEmailContactEmailAddressSubscribeEmails0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribeEmails1 PublicreportSubscribeEmailSlice, commsEmailContact0 *CommsEmailContact) (PublicreportSubscribeEmailSlice, error) { + setter := &PublicreportSubscribeEmailSetter{ + EmailAddress: omit.From(commsEmailContact0.Address), + } + + err := publicreportSubscribeEmails1.UpdateAll(ctx, exec, *setter) + if err != nil { + return nil, fmt.Errorf("attachCommsEmailContactEmailAddressSubscribeEmails0: %w", err) + } + + return publicreportSubscribeEmails1, nil +} + +func (commsEmailContact0 *CommsEmailContact) InsertEmailAddressSubscribeEmails(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribeEmailSetter) error { + if len(related) == 0 { + return nil + } + + var err error + + publicreportSubscribeEmails1, err := insertCommsEmailContactEmailAddressSubscribeEmails0(ctx, exec, related, commsEmailContact0) + if err != nil { + return err + } + + commsEmailContact0.R.EmailAddressSubscribeEmails = append(commsEmailContact0.R.EmailAddressSubscribeEmails, publicreportSubscribeEmails1...) + + for _, rel := range publicreportSubscribeEmails1 { + rel.R.EmailAddressEmailContact = commsEmailContact0 + } + return nil +} + +func (commsEmailContact0 *CommsEmailContact) AttachEmailAddressSubscribeEmails(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribeEmail) error { + if len(related) == 0 { + return nil + } + + var err error + publicreportSubscribeEmails1 := PublicreportSubscribeEmailSlice(related) + + _, err = attachCommsEmailContactEmailAddressSubscribeEmails0(ctx, exec, len(related), publicreportSubscribeEmails1, commsEmailContact0) + if err != nil { + return err + } + + commsEmailContact0.R.EmailAddressSubscribeEmails = append(commsEmailContact0.R.EmailAddressSubscribeEmails, publicreportSubscribeEmails1...) + + for _, rel := range related { + rel.R.EmailAddressEmailContact = commsEmailContact0 + } + + return nil +} + type commsEmailContactWhere[Q psql.Filterable] struct { Address psql.WhereMod[Q, string] Confirmed psql.WhereMod[Q, bool] @@ -867,6 +960,20 @@ func (o *CommsEmailContact) Preload(name string, retrieved any) error { o.R.EmailAddressNotifyEmailPools = rels + for _, rel := range rels { + if rel != nil { + rel.R.EmailAddressEmailContact = o + } + } + return nil + case "EmailAddressSubscribeEmails": + rels, ok := retrieved.(PublicreportSubscribeEmailSlice) + if !ok { + return fmt.Errorf("commsEmailContact cannot load %T as %q", retrieved, name) + } + + o.R.EmailAddressSubscribeEmails = rels + for _, rel := range rels { if rel != nil { rel.R.EmailAddressEmailContact = o @@ -889,6 +996,7 @@ type commsEmailContactThenLoader[Q orm.Loadable] struct { Organizations func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] EmailAddressNotifyEmailNuisances func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] EmailAddressNotifyEmailPools func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] + EmailAddressSubscribeEmails func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } func buildCommsEmailContactThenLoader[Q orm.Loadable]() commsEmailContactThenLoader[Q] { @@ -904,6 +1012,9 @@ func buildCommsEmailContactThenLoader[Q orm.Loadable]() commsEmailContactThenLoa type EmailAddressNotifyEmailPoolsLoadInterface interface { LoadEmailAddressNotifyEmailPools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } + type EmailAddressSubscribeEmailsLoadInterface interface { + LoadEmailAddressSubscribeEmails(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } return commsEmailContactThenLoader[Q]{ DestinationEmailLogs: thenLoadBuilder[Q]( @@ -930,6 +1041,12 @@ func buildCommsEmailContactThenLoader[Q orm.Loadable]() commsEmailContactThenLoa return retrieved.LoadEmailAddressNotifyEmailPools(ctx, exec, mods...) }, ), + EmailAddressSubscribeEmails: thenLoadBuilder[Q]( + "EmailAddressSubscribeEmails", + func(ctx context.Context, exec bob.Executor, retrieved EmailAddressSubscribeEmailsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadEmailAddressSubscribeEmails(ctx, exec, mods...) + }, + ), } } @@ -1197,12 +1314,74 @@ func (os CommsEmailContactSlice) LoadEmailAddressNotifyEmailPools(ctx context.Co return nil } +// LoadEmailAddressSubscribeEmails loads the commsEmailContact's EmailAddressSubscribeEmails into the .R struct +func (o *CommsEmailContact) LoadEmailAddressSubscribeEmails(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + // Reset the relationship + o.R.EmailAddressSubscribeEmails = nil + + related, err := o.EmailAddressSubscribeEmails(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, rel := range related { + rel.R.EmailAddressEmailContact = o + } + + o.R.EmailAddressSubscribeEmails = related + return nil +} + +// LoadEmailAddressSubscribeEmails loads the commsEmailContact's EmailAddressSubscribeEmails into the .R struct +func (os CommsEmailContactSlice) LoadEmailAddressSubscribeEmails(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + publicreportSubscribeEmails, err := os.EmailAddressSubscribeEmails(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, o := range os { + if o == nil { + continue + } + + o.R.EmailAddressSubscribeEmails = nil + } + + for _, o := range os { + if o == nil { + continue + } + + for _, rel := range publicreportSubscribeEmails { + + if !(o.Address == rel.EmailAddress) { + continue + } + + rel.R.EmailAddressEmailContact = o + + o.R.EmailAddressSubscribeEmails = append(o.R.EmailAddressSubscribeEmails, rel) + } + } + + return nil +} + // commsEmailContactC is where relationship counts are stored. type commsEmailContactC struct { DestinationEmailLogs *int64 Organizations *int64 EmailAddressNotifyEmailNuisances *int64 EmailAddressNotifyEmailPools *int64 + EmailAddressSubscribeEmails *int64 } // PreloadCount sets a count in the C struct by name @@ -1220,6 +1399,8 @@ func (o *CommsEmailContact) PreloadCount(name string, count int64) error { o.C.EmailAddressNotifyEmailNuisances = &count case "EmailAddressNotifyEmailPools": o.C.EmailAddressNotifyEmailPools = &count + case "EmailAddressSubscribeEmails": + o.C.EmailAddressSubscribeEmails = &count } return nil } @@ -1229,6 +1410,7 @@ type commsEmailContactCountPreloader struct { Organizations func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader EmailAddressNotifyEmailNuisances func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader EmailAddressNotifyEmailPools func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader + EmailAddressSubscribeEmails func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader } func buildCommsEmailContactCountPreloader() commsEmailContactCountPreloader { @@ -1304,6 +1486,23 @@ func buildCommsEmailContactCountPreloader() commsEmailContactCountPreloader { return psql.Group(psql.Select(subqueryMods...).Expression) }) }, + EmailAddressSubscribeEmails: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader { + return countPreloader[*CommsEmailContact]("EmailAddressSubscribeEmails", func(parent string) bob.Expression { + // Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk) + if parent == "" { + parent = CommsEmailContacts.Alias() + } + + subqueryMods := []bob.Mod[*dialect.SelectQuery]{ + sm.Columns(psql.Raw("count(*)")), + + sm.From(PublicreportSubscribeEmails.Name()), + sm.Where(psql.Quote(PublicreportSubscribeEmails.Alias(), "email_address").EQ(psql.Quote(parent, "address"))), + } + subqueryMods = append(subqueryMods, mods...) + return psql.Group(psql.Select(subqueryMods...).Expression) + }) + }, } } @@ -1312,6 +1511,7 @@ type commsEmailContactCountThenLoader[Q orm.Loadable] struct { Organizations func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] EmailAddressNotifyEmailNuisances func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] EmailAddressNotifyEmailPools func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] + EmailAddressSubscribeEmails func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } func buildCommsEmailContactCountThenLoader[Q orm.Loadable]() commsEmailContactCountThenLoader[Q] { @@ -1327,6 +1527,9 @@ func buildCommsEmailContactCountThenLoader[Q orm.Loadable]() commsEmailContactCo type EmailAddressNotifyEmailPoolsCountInterface interface { LoadCountEmailAddressNotifyEmailPools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } + type EmailAddressSubscribeEmailsCountInterface interface { + LoadCountEmailAddressSubscribeEmails(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } return commsEmailContactCountThenLoader[Q]{ DestinationEmailLogs: countThenLoadBuilder[Q]( @@ -1353,6 +1556,12 @@ func buildCommsEmailContactCountThenLoader[Q orm.Loadable]() commsEmailContactCo return retrieved.LoadCountEmailAddressNotifyEmailPools(ctx, exec, mods...) }, ), + EmailAddressSubscribeEmails: countThenLoadBuilder[Q]( + "EmailAddressSubscribeEmails", + func(ctx context.Context, exec bob.Executor, retrieved EmailAddressSubscribeEmailsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadCountEmailAddressSubscribeEmails(ctx, exec, mods...) + }, + ), } } @@ -1476,12 +1685,43 @@ func (os CommsEmailContactSlice) LoadCountEmailAddressNotifyEmailPools(ctx conte return nil } +// LoadCountEmailAddressSubscribeEmails loads the count of EmailAddressSubscribeEmails into the C struct +func (o *CommsEmailContact) LoadCountEmailAddressSubscribeEmails(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + count, err := o.EmailAddressSubscribeEmails(mods...).Count(ctx, exec) + if err != nil { + return err + } + + o.C.EmailAddressSubscribeEmails = &count + return nil +} + +// LoadCountEmailAddressSubscribeEmails loads the count of EmailAddressSubscribeEmails for a slice +func (os CommsEmailContactSlice) LoadCountEmailAddressSubscribeEmails(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + for _, o := range os { + if err := o.LoadCountEmailAddressSubscribeEmails(ctx, exec, mods...); err != nil { + return err + } + } + + return nil +} + type commsEmailContactJoins[Q dialect.Joinable] struct { typ string DestinationEmailLogs modAs[Q, commsEmailLogColumns] Organizations modAs[Q, organizationColumns] EmailAddressNotifyEmailNuisances modAs[Q, publicreportNotifyEmailNuisanceColumns] EmailAddressNotifyEmailPools modAs[Q, publicreportNotifyEmailPoolColumns] + EmailAddressSubscribeEmails modAs[Q, publicreportSubscribeEmailColumns] } func (j commsEmailContactJoins[Q]) aliasedAs(alias string) commsEmailContactJoins[Q] { @@ -1552,6 +1792,20 @@ func buildCommsEmailContactJoins[Q dialect.Joinable](cols commsEmailContactColum )) } + return mods + }, + }, + EmailAddressSubscribeEmails: modAs[Q, publicreportSubscribeEmailColumns]{ + c: PublicreportSubscribeEmails.Columns, + f: func(to publicreportSubscribeEmailColumns) bob.Mod[Q] { + mods := make(mods.QueryMods[Q], 0, 1) + + { + mods = append(mods, dialect.Join[Q](typ, PublicreportSubscribeEmails.Name().As(to.Alias())).On( + to.EmailAddress.EQ(cols.Address), + )) + } + return mods }, }, diff --git a/db/models/comms.phone.bob.go b/db/models/comms.phone.bob.go index 3a99f731..4e4045e4 100644 --- a/db/models/comms.phone.bob.go +++ b/db/models/comms.phone.bob.go @@ -53,6 +53,7 @@ type commsPhoneR struct { Organizations OrganizationSlice // district_subscription_phone.district_subscription_phone_organization_id_fkeydistrict_subscription_phone.district_subscription_phone_phone_e164_fkey PhoneE164NotifyPhoneNuisances PublicreportNotifyPhoneNuisanceSlice // publicreport.notify_phone_nuisance.notify_phone_nuisance_phone_e164_fkey PhoneE164NotifyPhonePools PublicreportNotifyPhonePoolSlice // publicreport.notify_phone_pool.notify_phone_pool_phone_e164_fkey + PhoneE164SubscribePhones PublicreportSubscribePhoneSlice // publicreport.subscribe_phone.subscribe_phone_phone_e164_fkey } func buildCommsPhoneColumns(alias string) commsPhoneColumns { @@ -550,6 +551,30 @@ func (os CommsPhoneSlice) PhoneE164NotifyPhonePools(mods ...bob.Mod[*dialect.Sel )...) } +// PhoneE164SubscribePhones starts a query for related objects on publicreport.subscribe_phone +func (o *CommsPhone) PhoneE164SubscribePhones(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportSubscribePhonesQuery { + return PublicreportSubscribePhones.Query(append(mods, + sm.Where(PublicreportSubscribePhones.Columns.PhoneE164.EQ(psql.Arg(o.E164))), + )...) +} + +func (os CommsPhoneSlice) PhoneE164SubscribePhones(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportSubscribePhonesQuery { + pkE164 := make(pgtypes.Array[string], 0, len(os)) + for _, o := range os { + if o == nil { + continue + } + pkE164 = append(pkE164, o.E164) + } + PKArgExpr := psql.Select(sm.Columns( + psql.F("unnest", psql.Cast(psql.Arg(pkE164), "text[]")), + )) + + return PublicreportSubscribePhones.Query(append(mods, + sm.Where(psql.Group(PublicreportSubscribePhones.Columns.PhoneE164).OP("IN", PKArgExpr)), + )...) +} + func insertCommsPhoneDestinationTextJobs0(ctx context.Context, exec bob.Executor, commsTextJobs1 []*CommsTextJobSetter, commsPhone0 *CommsPhone) (CommsTextJobSlice, error) { for i := range commsTextJobs1 { commsTextJobs1[i].Destination = omit.From(commsPhone0.E164) @@ -955,6 +980,74 @@ func (commsPhone0 *CommsPhone) AttachPhoneE164NotifyPhonePools(ctx context.Conte return nil } +func insertCommsPhonePhoneE164SubscribePhones0(ctx context.Context, exec bob.Executor, publicreportSubscribePhones1 []*PublicreportSubscribePhoneSetter, commsPhone0 *CommsPhone) (PublicreportSubscribePhoneSlice, error) { + for i := range publicreportSubscribePhones1 { + publicreportSubscribePhones1[i].PhoneE164 = omit.From(commsPhone0.E164) + } + + ret, err := PublicreportSubscribePhones.Insert(bob.ToMods(publicreportSubscribePhones1...)).All(ctx, exec) + if err != nil { + return ret, fmt.Errorf("insertCommsPhonePhoneE164SubscribePhones0: %w", err) + } + + return ret, nil +} + +func attachCommsPhonePhoneE164SubscribePhones0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribePhones1 PublicreportSubscribePhoneSlice, commsPhone0 *CommsPhone) (PublicreportSubscribePhoneSlice, error) { + setter := &PublicreportSubscribePhoneSetter{ + PhoneE164: omit.From(commsPhone0.E164), + } + + err := publicreportSubscribePhones1.UpdateAll(ctx, exec, *setter) + if err != nil { + return nil, fmt.Errorf("attachCommsPhonePhoneE164SubscribePhones0: %w", err) + } + + return publicreportSubscribePhones1, nil +} + +func (commsPhone0 *CommsPhone) InsertPhoneE164SubscribePhones(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribePhoneSetter) error { + if len(related) == 0 { + return nil + } + + var err error + + publicreportSubscribePhones1, err := insertCommsPhonePhoneE164SubscribePhones0(ctx, exec, related, commsPhone0) + if err != nil { + return err + } + + commsPhone0.R.PhoneE164SubscribePhones = append(commsPhone0.R.PhoneE164SubscribePhones, publicreportSubscribePhones1...) + + for _, rel := range publicreportSubscribePhones1 { + rel.R.PhoneE164Phone = commsPhone0 + } + return nil +} + +func (commsPhone0 *CommsPhone) AttachPhoneE164SubscribePhones(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribePhone) error { + if len(related) == 0 { + return nil + } + + var err error + publicreportSubscribePhones1 := PublicreportSubscribePhoneSlice(related) + + _, err = attachCommsPhonePhoneE164SubscribePhones0(ctx, exec, len(related), publicreportSubscribePhones1, commsPhone0) + if err != nil { + return err + } + + commsPhone0.R.PhoneE164SubscribePhones = append(commsPhone0.R.PhoneE164SubscribePhones, publicreportSubscribePhones1...) + + for _, rel := range related { + rel.R.PhoneE164Phone = commsPhone0 + } + + return nil +} + type commsPhoneWhere[Q psql.Filterable] struct { E164 psql.WhereMod[Q, string] IsSubscribed psql.WhereMod[Q, bool] @@ -1057,6 +1150,20 @@ func (o *CommsPhone) Preload(name string, retrieved any) error { o.R.PhoneE164NotifyPhonePools = rels + for _, rel := range rels { + if rel != nil { + rel.R.PhoneE164Phone = o + } + } + return nil + case "PhoneE164SubscribePhones": + rels, ok := retrieved.(PublicreportSubscribePhoneSlice) + if !ok { + return fmt.Errorf("commsPhone cannot load %T as %q", retrieved, name) + } + + o.R.PhoneE164SubscribePhones = rels + for _, rel := range rels { if rel != nil { rel.R.PhoneE164Phone = o @@ -1081,6 +1188,7 @@ type commsPhoneThenLoader[Q orm.Loadable] struct { Organizations func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] PhoneE164NotifyPhoneNuisances func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] PhoneE164NotifyPhonePools func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] + PhoneE164SubscribePhones func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } func buildCommsPhoneThenLoader[Q orm.Loadable]() commsPhoneThenLoader[Q] { @@ -1102,6 +1210,9 @@ func buildCommsPhoneThenLoader[Q orm.Loadable]() commsPhoneThenLoader[Q] { type PhoneE164NotifyPhonePoolsLoadInterface interface { LoadPhoneE164NotifyPhonePools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } + type PhoneE164SubscribePhonesLoadInterface interface { + LoadPhoneE164SubscribePhones(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } return commsPhoneThenLoader[Q]{ DestinationTextJobs: thenLoadBuilder[Q]( @@ -1140,6 +1251,12 @@ func buildCommsPhoneThenLoader[Q orm.Loadable]() commsPhoneThenLoader[Q] { return retrieved.LoadPhoneE164NotifyPhonePools(ctx, exec, mods...) }, ), + PhoneE164SubscribePhones: thenLoadBuilder[Q]( + "PhoneE164SubscribePhones", + func(ctx context.Context, exec bob.Executor, retrieved PhoneE164SubscribePhonesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadPhoneE164SubscribePhones(ctx, exec, mods...) + }, + ), } } @@ -1529,6 +1646,67 @@ func (os CommsPhoneSlice) LoadPhoneE164NotifyPhonePools(ctx context.Context, exe return nil } +// LoadPhoneE164SubscribePhones loads the commsPhone's PhoneE164SubscribePhones into the .R struct +func (o *CommsPhone) LoadPhoneE164SubscribePhones(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + // Reset the relationship + o.R.PhoneE164SubscribePhones = nil + + related, err := o.PhoneE164SubscribePhones(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, rel := range related { + rel.R.PhoneE164Phone = o + } + + o.R.PhoneE164SubscribePhones = related + return nil +} + +// LoadPhoneE164SubscribePhones loads the commsPhone's PhoneE164SubscribePhones into the .R struct +func (os CommsPhoneSlice) LoadPhoneE164SubscribePhones(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + publicreportSubscribePhones, err := os.PhoneE164SubscribePhones(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, o := range os { + if o == nil { + continue + } + + o.R.PhoneE164SubscribePhones = nil + } + + for _, o := range os { + if o == nil { + continue + } + + for _, rel := range publicreportSubscribePhones { + + if !(o.E164 == rel.PhoneE164) { + continue + } + + rel.R.PhoneE164Phone = o + + o.R.PhoneE164SubscribePhones = append(o.R.PhoneE164SubscribePhones, rel) + } + } + + return nil +} + // commsPhoneC is where relationship counts are stored. type commsPhoneC struct { DestinationTextJobs *int64 @@ -1537,6 +1715,7 @@ type commsPhoneC struct { Organizations *int64 PhoneE164NotifyPhoneNuisances *int64 PhoneE164NotifyPhonePools *int64 + PhoneE164SubscribePhones *int64 } // PreloadCount sets a count in the C struct by name @@ -1558,6 +1737,8 @@ func (o *CommsPhone) PreloadCount(name string, count int64) error { o.C.PhoneE164NotifyPhoneNuisances = &count case "PhoneE164NotifyPhonePools": o.C.PhoneE164NotifyPhonePools = &count + case "PhoneE164SubscribePhones": + o.C.PhoneE164SubscribePhones = &count } return nil } @@ -1569,6 +1750,7 @@ type commsPhoneCountPreloader struct { Organizations func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader PhoneE164NotifyPhoneNuisances func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader PhoneE164NotifyPhonePools func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader + PhoneE164SubscribePhones func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader } func buildCommsPhoneCountPreloader() commsPhoneCountPreloader { @@ -1678,6 +1860,23 @@ func buildCommsPhoneCountPreloader() commsPhoneCountPreloader { return psql.Group(psql.Select(subqueryMods...).Expression) }) }, + PhoneE164SubscribePhones: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader { + return countPreloader[*CommsPhone]("PhoneE164SubscribePhones", func(parent string) bob.Expression { + // Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk) + if parent == "" { + parent = CommsPhones.Alias() + } + + subqueryMods := []bob.Mod[*dialect.SelectQuery]{ + sm.Columns(psql.Raw("count(*)")), + + sm.From(PublicreportSubscribePhones.Name()), + sm.Where(psql.Quote(PublicreportSubscribePhones.Alias(), "phone_e164").EQ(psql.Quote(parent, "e164"))), + } + subqueryMods = append(subqueryMods, mods...) + return psql.Group(psql.Select(subqueryMods...).Expression) + }) + }, } } @@ -1688,6 +1887,7 @@ type commsPhoneCountThenLoader[Q orm.Loadable] struct { Organizations func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] PhoneE164NotifyPhoneNuisances func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] PhoneE164NotifyPhonePools func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] + PhoneE164SubscribePhones func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } func buildCommsPhoneCountThenLoader[Q orm.Loadable]() commsPhoneCountThenLoader[Q] { @@ -1709,6 +1909,9 @@ func buildCommsPhoneCountThenLoader[Q orm.Loadable]() commsPhoneCountThenLoader[ type PhoneE164NotifyPhonePoolsCountInterface interface { LoadCountPhoneE164NotifyPhonePools(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } + type PhoneE164SubscribePhonesCountInterface interface { + LoadCountPhoneE164SubscribePhones(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } return commsPhoneCountThenLoader[Q]{ DestinationTextJobs: countThenLoadBuilder[Q]( @@ -1747,6 +1950,12 @@ func buildCommsPhoneCountThenLoader[Q orm.Loadable]() commsPhoneCountThenLoader[ return retrieved.LoadCountPhoneE164NotifyPhonePools(ctx, exec, mods...) }, ), + PhoneE164SubscribePhones: countThenLoadBuilder[Q]( + "PhoneE164SubscribePhones", + func(ctx context.Context, exec bob.Executor, retrieved PhoneE164SubscribePhonesCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadCountPhoneE164SubscribePhones(ctx, exec, mods...) + }, + ), } } @@ -1930,6 +2139,36 @@ func (os CommsPhoneSlice) LoadCountPhoneE164NotifyPhonePools(ctx context.Context return nil } +// LoadCountPhoneE164SubscribePhones loads the count of PhoneE164SubscribePhones into the C struct +func (o *CommsPhone) LoadCountPhoneE164SubscribePhones(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + count, err := o.PhoneE164SubscribePhones(mods...).Count(ctx, exec) + if err != nil { + return err + } + + o.C.PhoneE164SubscribePhones = &count + return nil +} + +// LoadCountPhoneE164SubscribePhones loads the count of PhoneE164SubscribePhones for a slice +func (os CommsPhoneSlice) LoadCountPhoneE164SubscribePhones(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + for _, o := range os { + if err := o.LoadCountPhoneE164SubscribePhones(ctx, exec, mods...); err != nil { + return err + } + } + + return nil +} + type commsPhoneJoins[Q dialect.Joinable] struct { typ string DestinationTextJobs modAs[Q, commsTextJobColumns] @@ -1938,6 +2177,7 @@ type commsPhoneJoins[Q dialect.Joinable] struct { Organizations modAs[Q, organizationColumns] PhoneE164NotifyPhoneNuisances modAs[Q, publicreportNotifyPhoneNuisanceColumns] PhoneE164NotifyPhonePools modAs[Q, publicreportNotifyPhonePoolColumns] + PhoneE164SubscribePhones modAs[Q, publicreportSubscribePhoneColumns] } func (j commsPhoneJoins[Q]) aliasedAs(alias string) commsPhoneJoins[Q] { @@ -2036,6 +2276,20 @@ func buildCommsPhoneJoins[Q dialect.Joinable](cols commsPhoneColumns, typ string )) } + return mods + }, + }, + PhoneE164SubscribePhones: modAs[Q, publicreportSubscribePhoneColumns]{ + c: PublicreportSubscribePhones.Columns, + f: func(to publicreportSubscribePhoneColumns) bob.Mod[Q] { + mods := make(mods.QueryMods[Q], 0, 1) + + { + mods = append(mods, dialect.Join[Q](typ, PublicreportSubscribePhones.Name().As(to.Alias())).On( + to.PhoneE164.EQ(cols.E164), + )) + } + return mods }, }, diff --git a/db/models/organization.bob.go b/db/models/organization.bob.go index 40f482ba..6485672e 100644 --- a/db/models/organization.bob.go +++ b/db/models/organization.bob.go @@ -94,6 +94,8 @@ type organizationR struct { Nuisances PublicreportNuisanceSlice // publicreport.nuisance.nuisance_organization_id_fkey PublicreportPool PublicreportPoolSlice // publicreport.pool.pool_organization_id_fkey Quicks PublicreportQuickSlice // publicreport.quick.quick_organization_id_fkey + DistrictSubscribeEmails PublicreportSubscribeEmailSlice // publicreport.subscribe_email.subscribe_email_district_id_fkey + DistrictSubscribePhones PublicreportSubscribePhoneSlice // publicreport.subscribe_phone.subscribe_phone_district_id_fkey User UserSlice // user_.user__organization_id_fkey } @@ -1521,6 +1523,54 @@ func (os OrganizationSlice) Quicks(mods ...bob.Mod[*dialect.SelectQuery]) Public )...) } +// DistrictSubscribeEmails starts a query for related objects on publicreport.subscribe_email +func (o *Organization) DistrictSubscribeEmails(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportSubscribeEmailsQuery { + return PublicreportSubscribeEmails.Query(append(mods, + sm.Where(PublicreportSubscribeEmails.Columns.DistrictID.EQ(psql.Arg(o.ID))), + )...) +} + +func (os OrganizationSlice) DistrictSubscribeEmails(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportSubscribeEmailsQuery { + pkID := make(pgtypes.Array[int32], 0, len(os)) + for _, o := range os { + if o == nil { + continue + } + pkID = append(pkID, o.ID) + } + PKArgExpr := psql.Select(sm.Columns( + psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")), + )) + + return PublicreportSubscribeEmails.Query(append(mods, + sm.Where(psql.Group(PublicreportSubscribeEmails.Columns.DistrictID).OP("IN", PKArgExpr)), + )...) +} + +// DistrictSubscribePhones starts a query for related objects on publicreport.subscribe_phone +func (o *Organization) DistrictSubscribePhones(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportSubscribePhonesQuery { + return PublicreportSubscribePhones.Query(append(mods, + sm.Where(PublicreportSubscribePhones.Columns.DistrictID.EQ(psql.Arg(o.ID))), + )...) +} + +func (os OrganizationSlice) DistrictSubscribePhones(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportSubscribePhonesQuery { + pkID := make(pgtypes.Array[int32], 0, len(os)) + for _, o := range os { + if o == nil { + continue + } + pkID = append(pkID, o.ID) + } + PKArgExpr := psql.Select(sm.Columns( + psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")), + )) + + return PublicreportSubscribePhones.Query(append(mods, + sm.Where(psql.Group(PublicreportSubscribePhones.Columns.DistrictID).OP("IN", PKArgExpr)), + )...) +} + // User starts a query for related objects on user_ func (o *Organization) User(mods ...bob.Mod[*dialect.SelectQuery]) UsersQuery { return Users.Query(append(mods, @@ -4171,6 +4221,142 @@ func (organization0 *Organization) AttachQuicks(ctx context.Context, exec bob.Ex return nil } +func insertOrganizationDistrictSubscribeEmails0(ctx context.Context, exec bob.Executor, publicreportSubscribeEmails1 []*PublicreportSubscribeEmailSetter, organization0 *Organization) (PublicreportSubscribeEmailSlice, error) { + for i := range publicreportSubscribeEmails1 { + publicreportSubscribeEmails1[i].DistrictID = omit.From(organization0.ID) + } + + ret, err := PublicreportSubscribeEmails.Insert(bob.ToMods(publicreportSubscribeEmails1...)).All(ctx, exec) + if err != nil { + return ret, fmt.Errorf("insertOrganizationDistrictSubscribeEmails0: %w", err) + } + + return ret, nil +} + +func attachOrganizationDistrictSubscribeEmails0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribeEmails1 PublicreportSubscribeEmailSlice, organization0 *Organization) (PublicreportSubscribeEmailSlice, error) { + setter := &PublicreportSubscribeEmailSetter{ + DistrictID: omit.From(organization0.ID), + } + + err := publicreportSubscribeEmails1.UpdateAll(ctx, exec, *setter) + if err != nil { + return nil, fmt.Errorf("attachOrganizationDistrictSubscribeEmails0: %w", err) + } + + return publicreportSubscribeEmails1, nil +} + +func (organization0 *Organization) InsertDistrictSubscribeEmails(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribeEmailSetter) error { + if len(related) == 0 { + return nil + } + + var err error + + publicreportSubscribeEmails1, err := insertOrganizationDistrictSubscribeEmails0(ctx, exec, related, organization0) + if err != nil { + return err + } + + organization0.R.DistrictSubscribeEmails = append(organization0.R.DistrictSubscribeEmails, publicreportSubscribeEmails1...) + + for _, rel := range publicreportSubscribeEmails1 { + rel.R.DistrictOrganization = organization0 + } + return nil +} + +func (organization0 *Organization) AttachDistrictSubscribeEmails(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribeEmail) error { + if len(related) == 0 { + return nil + } + + var err error + publicreportSubscribeEmails1 := PublicreportSubscribeEmailSlice(related) + + _, err = attachOrganizationDistrictSubscribeEmails0(ctx, exec, len(related), publicreportSubscribeEmails1, organization0) + if err != nil { + return err + } + + organization0.R.DistrictSubscribeEmails = append(organization0.R.DistrictSubscribeEmails, publicreportSubscribeEmails1...) + + for _, rel := range related { + rel.R.DistrictOrganization = organization0 + } + + return nil +} + +func insertOrganizationDistrictSubscribePhones0(ctx context.Context, exec bob.Executor, publicreportSubscribePhones1 []*PublicreportSubscribePhoneSetter, organization0 *Organization) (PublicreportSubscribePhoneSlice, error) { + for i := range publicreportSubscribePhones1 { + publicreportSubscribePhones1[i].DistrictID = omit.From(organization0.ID) + } + + ret, err := PublicreportSubscribePhones.Insert(bob.ToMods(publicreportSubscribePhones1...)).All(ctx, exec) + if err != nil { + return ret, fmt.Errorf("insertOrganizationDistrictSubscribePhones0: %w", err) + } + + return ret, nil +} + +func attachOrganizationDistrictSubscribePhones0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribePhones1 PublicreportSubscribePhoneSlice, organization0 *Organization) (PublicreportSubscribePhoneSlice, error) { + setter := &PublicreportSubscribePhoneSetter{ + DistrictID: omit.From(organization0.ID), + } + + err := publicreportSubscribePhones1.UpdateAll(ctx, exec, *setter) + if err != nil { + return nil, fmt.Errorf("attachOrganizationDistrictSubscribePhones0: %w", err) + } + + return publicreportSubscribePhones1, nil +} + +func (organization0 *Organization) InsertDistrictSubscribePhones(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribePhoneSetter) error { + if len(related) == 0 { + return nil + } + + var err error + + publicreportSubscribePhones1, err := insertOrganizationDistrictSubscribePhones0(ctx, exec, related, organization0) + if err != nil { + return err + } + + organization0.R.DistrictSubscribePhones = append(organization0.R.DistrictSubscribePhones, publicreportSubscribePhones1...) + + for _, rel := range publicreportSubscribePhones1 { + rel.R.DistrictOrganization = organization0 + } + return nil +} + +func (organization0 *Organization) AttachDistrictSubscribePhones(ctx context.Context, exec bob.Executor, related ...*PublicreportSubscribePhone) error { + if len(related) == 0 { + return nil + } + + var err error + publicreportSubscribePhones1 := PublicreportSubscribePhoneSlice(related) + + _, err = attachOrganizationDistrictSubscribePhones0(ctx, exec, len(related), publicreportSubscribePhones1, organization0) + if err != nil { + return err + } + + organization0.R.DistrictSubscribePhones = append(organization0.R.DistrictSubscribePhones, publicreportSubscribePhones1...) + + for _, rel := range related { + rel.R.DistrictOrganization = organization0 + } + + return nil +} + func insertOrganizationUser0(ctx context.Context, exec bob.Executor, users1 []*UserSetter, organization0 *Organization) (UserSlice, error) { for i := range users1 { users1[i].OrganizationID = omit.From(organization0.ID) @@ -4819,6 +5005,34 @@ func (o *Organization) Preload(name string, retrieved any) error { } } return nil + case "DistrictSubscribeEmails": + rels, ok := retrieved.(PublicreportSubscribeEmailSlice) + if !ok { + return fmt.Errorf("organization cannot load %T as %q", retrieved, name) + } + + o.R.DistrictSubscribeEmails = rels + + for _, rel := range rels { + if rel != nil { + rel.R.DistrictOrganization = o + } + } + return nil + case "DistrictSubscribePhones": + rels, ok := retrieved.(PublicreportSubscribePhoneSlice) + if !ok { + return fmt.Errorf("organization cannot load %T as %q", retrieved, name) + } + + o.R.DistrictSubscribePhones = rels + + for _, rel := range rels { + if rel != nil { + rel.R.DistrictOrganization = o + } + } + return nil case "User": rels, ok := retrieved.(UserSlice) if !ok { @@ -4900,6 +5114,8 @@ type organizationThenLoader[Q orm.Loadable] struct { Nuisances func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] PublicreportPool func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] Quicks func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] + DistrictSubscribeEmails func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] + DistrictSubscribePhones func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] User func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } @@ -5021,6 +5237,12 @@ func buildOrganizationThenLoader[Q orm.Loadable]() organizationThenLoader[Q] { type QuicksLoadInterface interface { LoadQuicks(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } + type DistrictSubscribeEmailsLoadInterface interface { + LoadDistrictSubscribeEmails(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } + type DistrictSubscribePhonesLoadInterface interface { + LoadDistrictSubscribePhones(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } type UserLoadInterface interface { LoadUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } @@ -5260,6 +5482,18 @@ func buildOrganizationThenLoader[Q orm.Loadable]() organizationThenLoader[Q] { return retrieved.LoadQuicks(ctx, exec, mods...) }, ), + DistrictSubscribeEmails: thenLoadBuilder[Q]( + "DistrictSubscribeEmails", + func(ctx context.Context, exec bob.Executor, retrieved DistrictSubscribeEmailsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadDistrictSubscribeEmails(ctx, exec, mods...) + }, + ), + DistrictSubscribePhones: thenLoadBuilder[Q]( + "DistrictSubscribePhones", + func(ctx context.Context, exec bob.Executor, retrieved DistrictSubscribePhonesLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadDistrictSubscribePhones(ctx, exec, mods...) + }, + ), User: thenLoadBuilder[Q]( "User", func(ctx context.Context, exec bob.Executor, retrieved UserLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { @@ -7691,6 +7925,128 @@ func (os OrganizationSlice) LoadQuicks(ctx context.Context, exec bob.Executor, m return nil } +// LoadDistrictSubscribeEmails loads the organization's DistrictSubscribeEmails into the .R struct +func (o *Organization) LoadDistrictSubscribeEmails(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + // Reset the relationship + o.R.DistrictSubscribeEmails = nil + + related, err := o.DistrictSubscribeEmails(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, rel := range related { + rel.R.DistrictOrganization = o + } + + o.R.DistrictSubscribeEmails = related + return nil +} + +// LoadDistrictSubscribeEmails loads the organization's DistrictSubscribeEmails into the .R struct +func (os OrganizationSlice) LoadDistrictSubscribeEmails(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + publicreportSubscribeEmails, err := os.DistrictSubscribeEmails(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, o := range os { + if o == nil { + continue + } + + o.R.DistrictSubscribeEmails = nil + } + + for _, o := range os { + if o == nil { + continue + } + + for _, rel := range publicreportSubscribeEmails { + + if !(o.ID == rel.DistrictID) { + continue + } + + rel.R.DistrictOrganization = o + + o.R.DistrictSubscribeEmails = append(o.R.DistrictSubscribeEmails, rel) + } + } + + return nil +} + +// LoadDistrictSubscribePhones loads the organization's DistrictSubscribePhones into the .R struct +func (o *Organization) LoadDistrictSubscribePhones(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + // Reset the relationship + o.R.DistrictSubscribePhones = nil + + related, err := o.DistrictSubscribePhones(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, rel := range related { + rel.R.DistrictOrganization = o + } + + o.R.DistrictSubscribePhones = related + return nil +} + +// LoadDistrictSubscribePhones loads the organization's DistrictSubscribePhones into the .R struct +func (os OrganizationSlice) LoadDistrictSubscribePhones(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + publicreportSubscribePhones, err := os.DistrictSubscribePhones(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, o := range os { + if o == nil { + continue + } + + o.R.DistrictSubscribePhones = nil + } + + for _, o := range os { + if o == nil { + continue + } + + for _, rel := range publicreportSubscribePhones { + + if !(o.ID == rel.DistrictID) { + continue + } + + rel.R.DistrictOrganization = o + + o.R.DistrictSubscribePhones = append(o.R.DistrictSubscribePhones, rel) + } + } + + return nil +} + // LoadUser loads the organization's User into the .R struct func (o *Organization) LoadUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { @@ -7792,6 +8148,8 @@ type organizationC struct { Nuisances *int64 PublicreportPool *int64 Quicks *int64 + DistrictSubscribeEmails *int64 + DistrictSubscribePhones *int64 User *int64 } @@ -7878,6 +8236,10 @@ func (o *Organization) PreloadCount(name string, count int64) error { o.C.PublicreportPool = &count case "Quicks": o.C.Quicks = &count + case "DistrictSubscribeEmails": + o.C.DistrictSubscribeEmails = &count + case "DistrictSubscribePhones": + o.C.DistrictSubscribePhones = &count case "User": o.C.User = &count } @@ -7923,6 +8285,8 @@ type organizationCountPreloader struct { Nuisances func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader PublicreportPool func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader Quicks func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader + DistrictSubscribeEmails func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader + DistrictSubscribePhones func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader User func(...bob.Mod[*dialect.SelectQuery]) psql.Preloader } @@ -8580,6 +8944,40 @@ func buildOrganizationCountPreloader() organizationCountPreloader { return psql.Group(psql.Select(subqueryMods...).Expression) }) }, + DistrictSubscribeEmails: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader { + return countPreloader[*Organization]("DistrictSubscribeEmails", func(parent string) bob.Expression { + // Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk) + if parent == "" { + parent = Organizations.Alias() + } + + subqueryMods := []bob.Mod[*dialect.SelectQuery]{ + sm.Columns(psql.Raw("count(*)")), + + sm.From(PublicreportSubscribeEmails.Name()), + sm.Where(psql.Quote(PublicreportSubscribeEmails.Alias(), "district_id").EQ(psql.Quote(parent, "id"))), + } + subqueryMods = append(subqueryMods, mods...) + return psql.Group(psql.Select(subqueryMods...).Expression) + }) + }, + DistrictSubscribePhones: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader { + return countPreloader[*Organization]("DistrictSubscribePhones", func(parent string) bob.Expression { + // Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk) + if parent == "" { + parent = Organizations.Alias() + } + + subqueryMods := []bob.Mod[*dialect.SelectQuery]{ + sm.Columns(psql.Raw("count(*)")), + + sm.From(PublicreportSubscribePhones.Name()), + sm.Where(psql.Quote(PublicreportSubscribePhones.Alias(), "district_id").EQ(psql.Quote(parent, "id"))), + } + subqueryMods = append(subqueryMods, mods...) + return psql.Group(psql.Select(subqueryMods...).Expression) + }) + }, User: func(mods ...bob.Mod[*dialect.SelectQuery]) psql.Preloader { return countPreloader[*Organization]("User", func(parent string) bob.Expression { // Build a correlated subquery: (SELECT COUNT(*) FROM related WHERE fk = parent.pk) @@ -8639,6 +9037,8 @@ type organizationCountThenLoader[Q orm.Loadable] struct { Nuisances func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] PublicreportPool func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] Quicks func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] + DistrictSubscribeEmails func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] + DistrictSubscribePhones func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] User func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] } @@ -8757,6 +9157,12 @@ func buildOrganizationCountThenLoader[Q orm.Loadable]() organizationCountThenLoa type QuicksCountInterface interface { LoadCountQuicks(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } + type DistrictSubscribeEmailsCountInterface interface { + LoadCountDistrictSubscribeEmails(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } + type DistrictSubscribePhonesCountInterface interface { + LoadCountDistrictSubscribePhones(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } type UserCountInterface interface { LoadCountUser(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error } @@ -8990,6 +9396,18 @@ func buildOrganizationCountThenLoader[Q orm.Loadable]() organizationCountThenLoa return retrieved.LoadCountQuicks(ctx, exec, mods...) }, ), + DistrictSubscribeEmails: countThenLoadBuilder[Q]( + "DistrictSubscribeEmails", + func(ctx context.Context, exec bob.Executor, retrieved DistrictSubscribeEmailsCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadCountDistrictSubscribeEmails(ctx, exec, mods...) + }, + ), + DistrictSubscribePhones: countThenLoadBuilder[Q]( + "DistrictSubscribePhones", + func(ctx context.Context, exec bob.Executor, retrieved DistrictSubscribePhonesCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadCountDistrictSubscribePhones(ctx, exec, mods...) + }, + ), User: countThenLoadBuilder[Q]( "User", func(ctx context.Context, exec bob.Executor, retrieved UserCountInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { @@ -10139,6 +10557,66 @@ func (os OrganizationSlice) LoadCountQuicks(ctx context.Context, exec bob.Execut return nil } +// LoadCountDistrictSubscribeEmails loads the count of DistrictSubscribeEmails into the C struct +func (o *Organization) LoadCountDistrictSubscribeEmails(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + count, err := o.DistrictSubscribeEmails(mods...).Count(ctx, exec) + if err != nil { + return err + } + + o.C.DistrictSubscribeEmails = &count + return nil +} + +// LoadCountDistrictSubscribeEmails loads the count of DistrictSubscribeEmails for a slice +func (os OrganizationSlice) LoadCountDistrictSubscribeEmails(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + for _, o := range os { + if err := o.LoadCountDistrictSubscribeEmails(ctx, exec, mods...); err != nil { + return err + } + } + + return nil +} + +// LoadCountDistrictSubscribePhones loads the count of DistrictSubscribePhones into the C struct +func (o *Organization) LoadCountDistrictSubscribePhones(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + count, err := o.DistrictSubscribePhones(mods...).Count(ctx, exec) + if err != nil { + return err + } + + o.C.DistrictSubscribePhones = &count + return nil +} + +// LoadCountDistrictSubscribePhones loads the count of DistrictSubscribePhones for a slice +func (os OrganizationSlice) LoadCountDistrictSubscribePhones(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + for _, o := range os { + if err := o.LoadCountDistrictSubscribePhones(ctx, exec, mods...); err != nil { + return err + } + } + + return nil +} + // LoadCountUser loads the count of User into the C struct func (o *Organization) LoadCountUser(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { if o == nil { @@ -10210,6 +10688,8 @@ type organizationJoins[Q dialect.Joinable] struct { Nuisances modAs[Q, publicreportNuisanceColumns] PublicreportPool modAs[Q, publicreportPoolColumns] Quicks modAs[Q, publicreportQuickColumns] + DistrictSubscribeEmails modAs[Q, publicreportSubscribeEmailColumns] + DistrictSubscribePhones modAs[Q, publicreportSubscribePhoneColumns] User modAs[Q, userColumns] } @@ -10782,6 +11262,34 @@ func buildOrganizationJoins[Q dialect.Joinable](cols organizationColumns, typ st return mods }, }, + DistrictSubscribeEmails: modAs[Q, publicreportSubscribeEmailColumns]{ + c: PublicreportSubscribeEmails.Columns, + f: func(to publicreportSubscribeEmailColumns) bob.Mod[Q] { + mods := make(mods.QueryMods[Q], 0, 1) + + { + mods = append(mods, dialect.Join[Q](typ, PublicreportSubscribeEmails.Name().As(to.Alias())).On( + to.DistrictID.EQ(cols.ID), + )) + } + + return mods + }, + }, + DistrictSubscribePhones: modAs[Q, publicreportSubscribePhoneColumns]{ + c: PublicreportSubscribePhones.Columns, + f: func(to publicreportSubscribePhoneColumns) bob.Mod[Q] { + mods := make(mods.QueryMods[Q], 0, 1) + + { + mods = append(mods, dialect.Join[Q](typ, PublicreportSubscribePhones.Name().As(to.Alias())).On( + to.DistrictID.EQ(cols.ID), + )) + } + + return mods + }, + }, User: modAs[Q, userColumns]{ c: Users.Columns, f: func(to userColumns) bob.Mod[Q] { diff --git a/db/models/publicreport.subscribe_email.bob.go b/db/models/publicreport.subscribe_email.bob.go new file mode 100644 index 00000000..35a245b3 --- /dev/null +++ b/db/models/publicreport.subscribe_email.bob.go @@ -0,0 +1,841 @@ +// Code generated by BobGen psql v0.42.5. DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package models + +import ( + "context" + "fmt" + "io" + "time" + + "github.com/Gleipnir-Technology/bob" + "github.com/Gleipnir-Technology/bob/dialect/psql" + "github.com/Gleipnir-Technology/bob/dialect/psql/dialect" + "github.com/Gleipnir-Technology/bob/dialect/psql/dm" + "github.com/Gleipnir-Technology/bob/dialect/psql/sm" + "github.com/Gleipnir-Technology/bob/dialect/psql/um" + "github.com/Gleipnir-Technology/bob/expr" + "github.com/Gleipnir-Technology/bob/mods" + "github.com/Gleipnir-Technology/bob/orm" + "github.com/Gleipnir-Technology/bob/types/pgtypes" + "github.com/aarondl/opt/null" + "github.com/aarondl/opt/omit" + "github.com/aarondl/opt/omitnull" +) + +// PublicreportSubscribeEmail is an object representing the database table. +type PublicreportSubscribeEmail struct { + Created time.Time `db:"created" ` + Deleted null.Val[time.Time] `db:"deleted" ` + DistrictID int32 `db:"district_id,pk" ` + EmailAddress string `db:"email_address,pk" ` + + R publicreportSubscribeEmailR `db:"-" ` +} + +// PublicreportSubscribeEmailSlice is an alias for a slice of pointers to PublicreportSubscribeEmail. +// This should almost always be used instead of []*PublicreportSubscribeEmail. +type PublicreportSubscribeEmailSlice []*PublicreportSubscribeEmail + +// PublicreportSubscribeEmails contains methods to work with the subscribe_email table +var PublicreportSubscribeEmails = psql.NewTablex[*PublicreportSubscribeEmail, PublicreportSubscribeEmailSlice, *PublicreportSubscribeEmailSetter]("publicreport", "subscribe_email", buildPublicreportSubscribeEmailColumns("publicreport.subscribe_email")) + +// PublicreportSubscribeEmailsQuery is a query on the subscribe_email table +type PublicreportSubscribeEmailsQuery = *psql.ViewQuery[*PublicreportSubscribeEmail, PublicreportSubscribeEmailSlice] + +// publicreportSubscribeEmailR is where relationships are stored. +type publicreportSubscribeEmailR struct { + DistrictOrganization *Organization // publicreport.subscribe_email.subscribe_email_district_id_fkey + EmailAddressEmailContact *CommsEmailContact // publicreport.subscribe_email.subscribe_email_email_address_fkey +} + +func buildPublicreportSubscribeEmailColumns(alias string) publicreportSubscribeEmailColumns { + return publicreportSubscribeEmailColumns{ + ColumnsExpr: expr.NewColumnsExpr( + "created", "deleted", "district_id", "email_address", + ).WithParent("publicreport.subscribe_email"), + tableAlias: alias, + Created: psql.Quote(alias, "created"), + Deleted: psql.Quote(alias, "deleted"), + DistrictID: psql.Quote(alias, "district_id"), + EmailAddress: psql.Quote(alias, "email_address"), + } +} + +type publicreportSubscribeEmailColumns struct { + expr.ColumnsExpr + tableAlias string + Created psql.Expression + Deleted psql.Expression + DistrictID psql.Expression + EmailAddress psql.Expression +} + +func (c publicreportSubscribeEmailColumns) Alias() string { + return c.tableAlias +} + +func (publicreportSubscribeEmailColumns) AliasedAs(alias string) publicreportSubscribeEmailColumns { + return buildPublicreportSubscribeEmailColumns(alias) +} + +// PublicreportSubscribeEmailSetter is used for insert/upsert/update operations +// All values are optional, and do not have to be set +// Generated columns are not included +type PublicreportSubscribeEmailSetter struct { + Created omit.Val[time.Time] `db:"created" ` + Deleted omitnull.Val[time.Time] `db:"deleted" ` + DistrictID omit.Val[int32] `db:"district_id,pk" ` + EmailAddress omit.Val[string] `db:"email_address,pk" ` +} + +func (s PublicreportSubscribeEmailSetter) SetColumns() []string { + vals := make([]string, 0, 4) + if s.Created.IsValue() { + vals = append(vals, "created") + } + if !s.Deleted.IsUnset() { + vals = append(vals, "deleted") + } + if s.DistrictID.IsValue() { + vals = append(vals, "district_id") + } + if s.EmailAddress.IsValue() { + vals = append(vals, "email_address") + } + return vals +} + +func (s PublicreportSubscribeEmailSetter) Overwrite(t *PublicreportSubscribeEmail) { + if s.Created.IsValue() { + t.Created = s.Created.MustGet() + } + if !s.Deleted.IsUnset() { + t.Deleted = s.Deleted.MustGetNull() + } + if s.DistrictID.IsValue() { + t.DistrictID = s.DistrictID.MustGet() + } + if s.EmailAddress.IsValue() { + t.EmailAddress = s.EmailAddress.MustGet() + } +} + +func (s *PublicreportSubscribeEmailSetter) Apply(q *dialect.InsertQuery) { + q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { + return PublicreportSubscribeEmails.BeforeInsertHooks.RunHooks(ctx, exec, s) + }) + + q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { + vals := make([]bob.Expression, 4) + if s.Created.IsValue() { + vals[0] = psql.Arg(s.Created.MustGet()) + } else { + vals[0] = psql.Raw("DEFAULT") + } + + if !s.Deleted.IsUnset() { + vals[1] = psql.Arg(s.Deleted.MustGetNull()) + } else { + vals[1] = psql.Raw("DEFAULT") + } + + if s.DistrictID.IsValue() { + vals[2] = psql.Arg(s.DistrictID.MustGet()) + } else { + vals[2] = psql.Raw("DEFAULT") + } + + if s.EmailAddress.IsValue() { + vals[3] = psql.Arg(s.EmailAddress.MustGet()) + } else { + vals[3] = psql.Raw("DEFAULT") + } + + return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "") + })) +} + +func (s PublicreportSubscribeEmailSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] { + return um.Set(s.Expressions()...) +} + +func (s PublicreportSubscribeEmailSetter) Expressions(prefix ...string) []bob.Expression { + exprs := make([]bob.Expression, 0, 4) + + if s.Created.IsValue() { + exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ + psql.Quote(append(prefix, "created")...), + psql.Arg(s.Created), + }}) + } + + if !s.Deleted.IsUnset() { + exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ + psql.Quote(append(prefix, "deleted")...), + psql.Arg(s.Deleted), + }}) + } + + if s.DistrictID.IsValue() { + exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ + psql.Quote(append(prefix, "district_id")...), + psql.Arg(s.DistrictID), + }}) + } + + if s.EmailAddress.IsValue() { + exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ + psql.Quote(append(prefix, "email_address")...), + psql.Arg(s.EmailAddress), + }}) + } + + return exprs +} + +// FindPublicreportSubscribeEmail retrieves a single record by primary key +// If cols is empty Find will return all columns. +func FindPublicreportSubscribeEmail(ctx context.Context, exec bob.Executor, DistrictIDPK int32, EmailAddressPK string, cols ...string) (*PublicreportSubscribeEmail, error) { + if len(cols) == 0 { + return PublicreportSubscribeEmails.Query( + sm.Where(PublicreportSubscribeEmails.Columns.DistrictID.EQ(psql.Arg(DistrictIDPK))), + sm.Where(PublicreportSubscribeEmails.Columns.EmailAddress.EQ(psql.Arg(EmailAddressPK))), + ).One(ctx, exec) + } + + return PublicreportSubscribeEmails.Query( + sm.Where(PublicreportSubscribeEmails.Columns.DistrictID.EQ(psql.Arg(DistrictIDPK))), + sm.Where(PublicreportSubscribeEmails.Columns.EmailAddress.EQ(psql.Arg(EmailAddressPK))), + sm.Columns(PublicreportSubscribeEmails.Columns.Only(cols...)), + ).One(ctx, exec) +} + +// PublicreportSubscribeEmailExists checks the presence of a single record by primary key +func PublicreportSubscribeEmailExists(ctx context.Context, exec bob.Executor, DistrictIDPK int32, EmailAddressPK string) (bool, error) { + return PublicreportSubscribeEmails.Query( + sm.Where(PublicreportSubscribeEmails.Columns.DistrictID.EQ(psql.Arg(DistrictIDPK))), + sm.Where(PublicreportSubscribeEmails.Columns.EmailAddress.EQ(psql.Arg(EmailAddressPK))), + ).Exists(ctx, exec) +} + +// AfterQueryHook is called after PublicreportSubscribeEmail is retrieved from the database +func (o *PublicreportSubscribeEmail) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { + var err error + + switch queryType { + case bob.QueryTypeSelect: + ctx, err = PublicreportSubscribeEmails.AfterSelectHooks.RunHooks(ctx, exec, PublicreportSubscribeEmailSlice{o}) + case bob.QueryTypeInsert: + ctx, err = PublicreportSubscribeEmails.AfterInsertHooks.RunHooks(ctx, exec, PublicreportSubscribeEmailSlice{o}) + case bob.QueryTypeUpdate: + ctx, err = PublicreportSubscribeEmails.AfterUpdateHooks.RunHooks(ctx, exec, PublicreportSubscribeEmailSlice{o}) + case bob.QueryTypeDelete: + ctx, err = PublicreportSubscribeEmails.AfterDeleteHooks.RunHooks(ctx, exec, PublicreportSubscribeEmailSlice{o}) + } + + return err +} + +// primaryKeyVals returns the primary key values of the PublicreportSubscribeEmail +func (o *PublicreportSubscribeEmail) primaryKeyVals() bob.Expression { + return psql.ArgGroup( + o.DistrictID, + o.EmailAddress, + ) +} + +func (o *PublicreportSubscribeEmail) pkEQ() dialect.Expression { + return psql.Group(psql.Quote("publicreport.subscribe_email", "district_id"), psql.Quote("publicreport.subscribe_email", "email_address")).EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { + return o.primaryKeyVals().WriteSQL(ctx, w, d, start) + })) +} + +// Update uses an executor to update the PublicreportSubscribeEmail +func (o *PublicreportSubscribeEmail) Update(ctx context.Context, exec bob.Executor, s *PublicreportSubscribeEmailSetter) error { + v, err := PublicreportSubscribeEmails.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec) + if err != nil { + return err + } + + o.R = v.R + *o = *v + + return nil +} + +// Delete deletes a single PublicreportSubscribeEmail record with an executor +func (o *PublicreportSubscribeEmail) Delete(ctx context.Context, exec bob.Executor) error { + _, err := PublicreportSubscribeEmails.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec) + return err +} + +// Reload refreshes the PublicreportSubscribeEmail using the executor +func (o *PublicreportSubscribeEmail) Reload(ctx context.Context, exec bob.Executor) error { + o2, err := PublicreportSubscribeEmails.Query( + sm.Where(PublicreportSubscribeEmails.Columns.DistrictID.EQ(psql.Arg(o.DistrictID))), + sm.Where(PublicreportSubscribeEmails.Columns.EmailAddress.EQ(psql.Arg(o.EmailAddress))), + ).One(ctx, exec) + if err != nil { + return err + } + o2.R = o.R + *o = *o2 + + return nil +} + +// AfterQueryHook is called after PublicreportSubscribeEmailSlice is retrieved from the database +func (o PublicreportSubscribeEmailSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { + var err error + + switch queryType { + case bob.QueryTypeSelect: + ctx, err = PublicreportSubscribeEmails.AfterSelectHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeInsert: + ctx, err = PublicreportSubscribeEmails.AfterInsertHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeUpdate: + ctx, err = PublicreportSubscribeEmails.AfterUpdateHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeDelete: + ctx, err = PublicreportSubscribeEmails.AfterDeleteHooks.RunHooks(ctx, exec, o) + } + + return err +} + +func (o PublicreportSubscribeEmailSlice) pkIN() dialect.Expression { + if len(o) == 0 { + return psql.Raw("NULL") + } + + return psql.Group(psql.Quote("publicreport.subscribe_email", "district_id"), psql.Quote("publicreport.subscribe_email", "email_address")).In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { + pkPairs := make([]bob.Expression, len(o)) + for i, row := range o { + pkPairs[i] = row.primaryKeyVals() + } + return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "") + })) +} + +// copyMatchingRows finds models in the given slice that have the same primary key +// then it first copies the existing relationships from the old model to the new model +// and then replaces the old model in the slice with the new model +func (o PublicreportSubscribeEmailSlice) copyMatchingRows(from ...*PublicreportSubscribeEmail) { + for i, old := range o { + for _, new := range from { + if new.DistrictID != old.DistrictID { + continue + } + if new.EmailAddress != old.EmailAddress { + continue + } + new.R = old.R + o[i] = new + break + } + } +} + +// UpdateMod modifies an update query with "WHERE primary_key IN (o...)" +func (o PublicreportSubscribeEmailSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] { + return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) { + q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { + return PublicreportSubscribeEmails.BeforeUpdateHooks.RunHooks(ctx, exec, o) + }) + + q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error { + var err error + switch retrieved := retrieved.(type) { + case *PublicreportSubscribeEmail: + o.copyMatchingRows(retrieved) + case []*PublicreportSubscribeEmail: + o.copyMatchingRows(retrieved...) + case PublicreportSubscribeEmailSlice: + o.copyMatchingRows(retrieved...) + default: + // If the retrieved value is not a PublicreportSubscribeEmail or a slice of PublicreportSubscribeEmail + // then run the AfterUpdateHooks on the slice + _, err = PublicreportSubscribeEmails.AfterUpdateHooks.RunHooks(ctx, exec, o) + } + + return err + })) + + q.AppendWhere(o.pkIN()) + }) +} + +// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)" +func (o PublicreportSubscribeEmailSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] { + return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) { + q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { + return PublicreportSubscribeEmails.BeforeDeleteHooks.RunHooks(ctx, exec, o) + }) + + q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error { + var err error + switch retrieved := retrieved.(type) { + case *PublicreportSubscribeEmail: + o.copyMatchingRows(retrieved) + case []*PublicreportSubscribeEmail: + o.copyMatchingRows(retrieved...) + case PublicreportSubscribeEmailSlice: + o.copyMatchingRows(retrieved...) + default: + // If the retrieved value is not a PublicreportSubscribeEmail or a slice of PublicreportSubscribeEmail + // then run the AfterDeleteHooks on the slice + _, err = PublicreportSubscribeEmails.AfterDeleteHooks.RunHooks(ctx, exec, o) + } + + return err + })) + + q.AppendWhere(o.pkIN()) + }) +} + +func (o PublicreportSubscribeEmailSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals PublicreportSubscribeEmailSetter) error { + if len(o) == 0 { + return nil + } + + _, err := PublicreportSubscribeEmails.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec) + return err +} + +func (o PublicreportSubscribeEmailSlice) DeleteAll(ctx context.Context, exec bob.Executor) error { + if len(o) == 0 { + return nil + } + + _, err := PublicreportSubscribeEmails.Delete(o.DeleteMod()).Exec(ctx, exec) + return err +} + +func (o PublicreportSubscribeEmailSlice) ReloadAll(ctx context.Context, exec bob.Executor) error { + if len(o) == 0 { + return nil + } + + o2, err := PublicreportSubscribeEmails.Query(sm.Where(o.pkIN())).All(ctx, exec) + if err != nil { + return err + } + + o.copyMatchingRows(o2...) + + return nil +} + +// DistrictOrganization starts a query for related objects on organization +func (o *PublicreportSubscribeEmail) DistrictOrganization(mods ...bob.Mod[*dialect.SelectQuery]) OrganizationsQuery { + return Organizations.Query(append(mods, + sm.Where(Organizations.Columns.ID.EQ(psql.Arg(o.DistrictID))), + )...) +} + +func (os PublicreportSubscribeEmailSlice) DistrictOrganization(mods ...bob.Mod[*dialect.SelectQuery]) OrganizationsQuery { + pkDistrictID := make(pgtypes.Array[int32], 0, len(os)) + for _, o := range os { + if o == nil { + continue + } + pkDistrictID = append(pkDistrictID, o.DistrictID) + } + PKArgExpr := psql.Select(sm.Columns( + psql.F("unnest", psql.Cast(psql.Arg(pkDistrictID), "integer[]")), + )) + + return Organizations.Query(append(mods, + sm.Where(psql.Group(Organizations.Columns.ID).OP("IN", PKArgExpr)), + )...) +} + +// EmailAddressEmailContact starts a query for related objects on comms.email_contact +func (o *PublicreportSubscribeEmail) EmailAddressEmailContact(mods ...bob.Mod[*dialect.SelectQuery]) CommsEmailContactsQuery { + return CommsEmailContacts.Query(append(mods, + sm.Where(CommsEmailContacts.Columns.Address.EQ(psql.Arg(o.EmailAddress))), + )...) +} + +func (os PublicreportSubscribeEmailSlice) EmailAddressEmailContact(mods ...bob.Mod[*dialect.SelectQuery]) CommsEmailContactsQuery { + pkEmailAddress := make(pgtypes.Array[string], 0, len(os)) + for _, o := range os { + if o == nil { + continue + } + pkEmailAddress = append(pkEmailAddress, o.EmailAddress) + } + PKArgExpr := psql.Select(sm.Columns( + psql.F("unnest", psql.Cast(psql.Arg(pkEmailAddress), "text[]")), + )) + + return CommsEmailContacts.Query(append(mods, + sm.Where(psql.Group(CommsEmailContacts.Columns.Address).OP("IN", PKArgExpr)), + )...) +} + +func attachPublicreportSubscribeEmailDistrictOrganization0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribeEmail0 *PublicreportSubscribeEmail, organization1 *Organization) (*PublicreportSubscribeEmail, error) { + setter := &PublicreportSubscribeEmailSetter{ + DistrictID: omit.From(organization1.ID), + } + + err := publicreportSubscribeEmail0.Update(ctx, exec, setter) + if err != nil { + return nil, fmt.Errorf("attachPublicreportSubscribeEmailDistrictOrganization0: %w", err) + } + + return publicreportSubscribeEmail0, nil +} + +func (publicreportSubscribeEmail0 *PublicreportSubscribeEmail) InsertDistrictOrganization(ctx context.Context, exec bob.Executor, related *OrganizationSetter) error { + var err error + + organization1, err := Organizations.Insert(related).One(ctx, exec) + if err != nil { + return fmt.Errorf("inserting related objects: %w", err) + } + + _, err = attachPublicreportSubscribeEmailDistrictOrganization0(ctx, exec, 1, publicreportSubscribeEmail0, organization1) + if err != nil { + return err + } + + publicreportSubscribeEmail0.R.DistrictOrganization = organization1 + + organization1.R.DistrictSubscribeEmails = append(organization1.R.DistrictSubscribeEmails, publicreportSubscribeEmail0) + + return nil +} + +func (publicreportSubscribeEmail0 *PublicreportSubscribeEmail) AttachDistrictOrganization(ctx context.Context, exec bob.Executor, organization1 *Organization) error { + var err error + + _, err = attachPublicreportSubscribeEmailDistrictOrganization0(ctx, exec, 1, publicreportSubscribeEmail0, organization1) + if err != nil { + return err + } + + publicreportSubscribeEmail0.R.DistrictOrganization = organization1 + + organization1.R.DistrictSubscribeEmails = append(organization1.R.DistrictSubscribeEmails, publicreportSubscribeEmail0) + + return nil +} + +func attachPublicreportSubscribeEmailEmailAddressEmailContact0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribeEmail0 *PublicreportSubscribeEmail, commsEmailContact1 *CommsEmailContact) (*PublicreportSubscribeEmail, error) { + setter := &PublicreportSubscribeEmailSetter{ + EmailAddress: omit.From(commsEmailContact1.Address), + } + + err := publicreportSubscribeEmail0.Update(ctx, exec, setter) + if err != nil { + return nil, fmt.Errorf("attachPublicreportSubscribeEmailEmailAddressEmailContact0: %w", err) + } + + return publicreportSubscribeEmail0, nil +} + +func (publicreportSubscribeEmail0 *PublicreportSubscribeEmail) InsertEmailAddressEmailContact(ctx context.Context, exec bob.Executor, related *CommsEmailContactSetter) error { + var err error + + commsEmailContact1, err := CommsEmailContacts.Insert(related).One(ctx, exec) + if err != nil { + return fmt.Errorf("inserting related objects: %w", err) + } + + _, err = attachPublicreportSubscribeEmailEmailAddressEmailContact0(ctx, exec, 1, publicreportSubscribeEmail0, commsEmailContact1) + if err != nil { + return err + } + + publicreportSubscribeEmail0.R.EmailAddressEmailContact = commsEmailContact1 + + commsEmailContact1.R.EmailAddressSubscribeEmails = append(commsEmailContact1.R.EmailAddressSubscribeEmails, publicreportSubscribeEmail0) + + return nil +} + +func (publicreportSubscribeEmail0 *PublicreportSubscribeEmail) AttachEmailAddressEmailContact(ctx context.Context, exec bob.Executor, commsEmailContact1 *CommsEmailContact) error { + var err error + + _, err = attachPublicreportSubscribeEmailEmailAddressEmailContact0(ctx, exec, 1, publicreportSubscribeEmail0, commsEmailContact1) + if err != nil { + return err + } + + publicreportSubscribeEmail0.R.EmailAddressEmailContact = commsEmailContact1 + + commsEmailContact1.R.EmailAddressSubscribeEmails = append(commsEmailContact1.R.EmailAddressSubscribeEmails, publicreportSubscribeEmail0) + + return nil +} + +type publicreportSubscribeEmailWhere[Q psql.Filterable] struct { + Created psql.WhereMod[Q, time.Time] + Deleted psql.WhereNullMod[Q, time.Time] + DistrictID psql.WhereMod[Q, int32] + EmailAddress psql.WhereMod[Q, string] +} + +func (publicreportSubscribeEmailWhere[Q]) AliasedAs(alias string) publicreportSubscribeEmailWhere[Q] { + return buildPublicreportSubscribeEmailWhere[Q](buildPublicreportSubscribeEmailColumns(alias)) +} + +func buildPublicreportSubscribeEmailWhere[Q psql.Filterable](cols publicreportSubscribeEmailColumns) publicreportSubscribeEmailWhere[Q] { + return publicreportSubscribeEmailWhere[Q]{ + Created: psql.Where[Q, time.Time](cols.Created), + Deleted: psql.WhereNull[Q, time.Time](cols.Deleted), + DistrictID: psql.Where[Q, int32](cols.DistrictID), + EmailAddress: psql.Where[Q, string](cols.EmailAddress), + } +} + +func (o *PublicreportSubscribeEmail) Preload(name string, retrieved any) error { + if o == nil { + return nil + } + + switch name { + case "DistrictOrganization": + rel, ok := retrieved.(*Organization) + if !ok { + return fmt.Errorf("publicreportSubscribeEmail cannot load %T as %q", retrieved, name) + } + + o.R.DistrictOrganization = rel + + if rel != nil { + rel.R.DistrictSubscribeEmails = PublicreportSubscribeEmailSlice{o} + } + return nil + case "EmailAddressEmailContact": + rel, ok := retrieved.(*CommsEmailContact) + if !ok { + return fmt.Errorf("publicreportSubscribeEmail cannot load %T as %q", retrieved, name) + } + + o.R.EmailAddressEmailContact = rel + + if rel != nil { + rel.R.EmailAddressSubscribeEmails = PublicreportSubscribeEmailSlice{o} + } + return nil + default: + return fmt.Errorf("publicreportSubscribeEmail has no relationship %q", name) + } +} + +type publicreportSubscribeEmailPreloader struct { + DistrictOrganization func(...psql.PreloadOption) psql.Preloader + EmailAddressEmailContact func(...psql.PreloadOption) psql.Preloader +} + +func buildPublicreportSubscribeEmailPreloader() publicreportSubscribeEmailPreloader { + return publicreportSubscribeEmailPreloader{ + DistrictOrganization: func(opts ...psql.PreloadOption) psql.Preloader { + return psql.Preload[*Organization, OrganizationSlice](psql.PreloadRel{ + Name: "DistrictOrganization", + Sides: []psql.PreloadSide{ + { + From: PublicreportSubscribeEmails, + To: Organizations, + FromColumns: []string{"district_id"}, + ToColumns: []string{"id"}, + }, + }, + }, Organizations.Columns.Names(), opts...) + }, + EmailAddressEmailContact: func(opts ...psql.PreloadOption) psql.Preloader { + return psql.Preload[*CommsEmailContact, CommsEmailContactSlice](psql.PreloadRel{ + Name: "EmailAddressEmailContact", + Sides: []psql.PreloadSide{ + { + From: PublicreportSubscribeEmails, + To: CommsEmailContacts, + FromColumns: []string{"email_address"}, + ToColumns: []string{"address"}, + }, + }, + }, CommsEmailContacts.Columns.Names(), opts...) + }, + } +} + +type publicreportSubscribeEmailThenLoader[Q orm.Loadable] struct { + DistrictOrganization func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] + EmailAddressEmailContact func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] +} + +func buildPublicreportSubscribeEmailThenLoader[Q orm.Loadable]() publicreportSubscribeEmailThenLoader[Q] { + type DistrictOrganizationLoadInterface interface { + LoadDistrictOrganization(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } + type EmailAddressEmailContactLoadInterface interface { + LoadEmailAddressEmailContact(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } + + return publicreportSubscribeEmailThenLoader[Q]{ + DistrictOrganization: thenLoadBuilder[Q]( + "DistrictOrganization", + func(ctx context.Context, exec bob.Executor, retrieved DistrictOrganizationLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadDistrictOrganization(ctx, exec, mods...) + }, + ), + EmailAddressEmailContact: thenLoadBuilder[Q]( + "EmailAddressEmailContact", + func(ctx context.Context, exec bob.Executor, retrieved EmailAddressEmailContactLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadEmailAddressEmailContact(ctx, exec, mods...) + }, + ), + } +} + +// LoadDistrictOrganization loads the publicreportSubscribeEmail's DistrictOrganization into the .R struct +func (o *PublicreportSubscribeEmail) LoadDistrictOrganization(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + // Reset the relationship + o.R.DistrictOrganization = nil + + related, err := o.DistrictOrganization(mods...).One(ctx, exec) + if err != nil { + return err + } + + related.R.DistrictSubscribeEmails = PublicreportSubscribeEmailSlice{o} + + o.R.DistrictOrganization = related + return nil +} + +// LoadDistrictOrganization loads the publicreportSubscribeEmail's DistrictOrganization into the .R struct +func (os PublicreportSubscribeEmailSlice) LoadDistrictOrganization(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + organizations, err := os.DistrictOrganization(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, o := range os { + if o == nil { + continue + } + + for _, rel := range organizations { + + if !(o.DistrictID == rel.ID) { + continue + } + + rel.R.DistrictSubscribeEmails = append(rel.R.DistrictSubscribeEmails, o) + + o.R.DistrictOrganization = rel + break + } + } + + return nil +} + +// LoadEmailAddressEmailContact loads the publicreportSubscribeEmail's EmailAddressEmailContact into the .R struct +func (o *PublicreportSubscribeEmail) LoadEmailAddressEmailContact(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + // Reset the relationship + o.R.EmailAddressEmailContact = nil + + related, err := o.EmailAddressEmailContact(mods...).One(ctx, exec) + if err != nil { + return err + } + + related.R.EmailAddressSubscribeEmails = PublicreportSubscribeEmailSlice{o} + + o.R.EmailAddressEmailContact = related + return nil +} + +// LoadEmailAddressEmailContact loads the publicreportSubscribeEmail's EmailAddressEmailContact into the .R struct +func (os PublicreportSubscribeEmailSlice) LoadEmailAddressEmailContact(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + commsEmailContacts, err := os.EmailAddressEmailContact(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, o := range os { + if o == nil { + continue + } + + for _, rel := range commsEmailContacts { + + if !(o.EmailAddress == rel.Address) { + continue + } + + rel.R.EmailAddressSubscribeEmails = append(rel.R.EmailAddressSubscribeEmails, o) + + o.R.EmailAddressEmailContact = rel + break + } + } + + return nil +} + +type publicreportSubscribeEmailJoins[Q dialect.Joinable] struct { + typ string + DistrictOrganization modAs[Q, organizationColumns] + EmailAddressEmailContact modAs[Q, commsEmailContactColumns] +} + +func (j publicreportSubscribeEmailJoins[Q]) aliasedAs(alias string) publicreportSubscribeEmailJoins[Q] { + return buildPublicreportSubscribeEmailJoins[Q](buildPublicreportSubscribeEmailColumns(alias), j.typ) +} + +func buildPublicreportSubscribeEmailJoins[Q dialect.Joinable](cols publicreportSubscribeEmailColumns, typ string) publicreportSubscribeEmailJoins[Q] { + return publicreportSubscribeEmailJoins[Q]{ + typ: typ, + DistrictOrganization: modAs[Q, organizationColumns]{ + c: Organizations.Columns, + f: func(to organizationColumns) bob.Mod[Q] { + mods := make(mods.QueryMods[Q], 0, 1) + + { + mods = append(mods, dialect.Join[Q](typ, Organizations.Name().As(to.Alias())).On( + to.ID.EQ(cols.DistrictID), + )) + } + + return mods + }, + }, + EmailAddressEmailContact: modAs[Q, commsEmailContactColumns]{ + c: CommsEmailContacts.Columns, + f: func(to commsEmailContactColumns) bob.Mod[Q] { + mods := make(mods.QueryMods[Q], 0, 1) + + { + mods = append(mods, dialect.Join[Q](typ, CommsEmailContacts.Name().As(to.Alias())).On( + to.Address.EQ(cols.EmailAddress), + )) + } + + return mods + }, + }, + } +} diff --git a/db/models/publicreport.subscribe_phone.bob.go b/db/models/publicreport.subscribe_phone.bob.go new file mode 100644 index 00000000..a1f3b5df --- /dev/null +++ b/db/models/publicreport.subscribe_phone.bob.go @@ -0,0 +1,841 @@ +// Code generated by BobGen psql v0.42.5. DO NOT EDIT. +// This file is meant to be re-generated in place and/or deleted at any time. + +package models + +import ( + "context" + "fmt" + "io" + "time" + + "github.com/Gleipnir-Technology/bob" + "github.com/Gleipnir-Technology/bob/dialect/psql" + "github.com/Gleipnir-Technology/bob/dialect/psql/dialect" + "github.com/Gleipnir-Technology/bob/dialect/psql/dm" + "github.com/Gleipnir-Technology/bob/dialect/psql/sm" + "github.com/Gleipnir-Technology/bob/dialect/psql/um" + "github.com/Gleipnir-Technology/bob/expr" + "github.com/Gleipnir-Technology/bob/mods" + "github.com/Gleipnir-Technology/bob/orm" + "github.com/Gleipnir-Technology/bob/types/pgtypes" + "github.com/aarondl/opt/null" + "github.com/aarondl/opt/omit" + "github.com/aarondl/opt/omitnull" +) + +// PublicreportSubscribePhone is an object representing the database table. +type PublicreportSubscribePhone struct { + Created time.Time `db:"created" ` + Deleted null.Val[time.Time] `db:"deleted" ` + DistrictID int32 `db:"district_id,pk" ` + PhoneE164 string `db:"phone_e164,pk" ` + + R publicreportSubscribePhoneR `db:"-" ` +} + +// PublicreportSubscribePhoneSlice is an alias for a slice of pointers to PublicreportSubscribePhone. +// This should almost always be used instead of []*PublicreportSubscribePhone. +type PublicreportSubscribePhoneSlice []*PublicreportSubscribePhone + +// PublicreportSubscribePhones contains methods to work with the subscribe_phone table +var PublicreportSubscribePhones = psql.NewTablex[*PublicreportSubscribePhone, PublicreportSubscribePhoneSlice, *PublicreportSubscribePhoneSetter]("publicreport", "subscribe_phone", buildPublicreportSubscribePhoneColumns("publicreport.subscribe_phone")) + +// PublicreportSubscribePhonesQuery is a query on the subscribe_phone table +type PublicreportSubscribePhonesQuery = *psql.ViewQuery[*PublicreportSubscribePhone, PublicreportSubscribePhoneSlice] + +// publicreportSubscribePhoneR is where relationships are stored. +type publicreportSubscribePhoneR struct { + DistrictOrganization *Organization // publicreport.subscribe_phone.subscribe_phone_district_id_fkey + PhoneE164Phone *CommsPhone // publicreport.subscribe_phone.subscribe_phone_phone_e164_fkey +} + +func buildPublicreportSubscribePhoneColumns(alias string) publicreportSubscribePhoneColumns { + return publicreportSubscribePhoneColumns{ + ColumnsExpr: expr.NewColumnsExpr( + "created", "deleted", "district_id", "phone_e164", + ).WithParent("publicreport.subscribe_phone"), + tableAlias: alias, + Created: psql.Quote(alias, "created"), + Deleted: psql.Quote(alias, "deleted"), + DistrictID: psql.Quote(alias, "district_id"), + PhoneE164: psql.Quote(alias, "phone_e164"), + } +} + +type publicreportSubscribePhoneColumns struct { + expr.ColumnsExpr + tableAlias string + Created psql.Expression + Deleted psql.Expression + DistrictID psql.Expression + PhoneE164 psql.Expression +} + +func (c publicreportSubscribePhoneColumns) Alias() string { + return c.tableAlias +} + +func (publicreportSubscribePhoneColumns) AliasedAs(alias string) publicreportSubscribePhoneColumns { + return buildPublicreportSubscribePhoneColumns(alias) +} + +// PublicreportSubscribePhoneSetter is used for insert/upsert/update operations +// All values are optional, and do not have to be set +// Generated columns are not included +type PublicreportSubscribePhoneSetter struct { + Created omit.Val[time.Time] `db:"created" ` + Deleted omitnull.Val[time.Time] `db:"deleted" ` + DistrictID omit.Val[int32] `db:"district_id,pk" ` + PhoneE164 omit.Val[string] `db:"phone_e164,pk" ` +} + +func (s PublicreportSubscribePhoneSetter) SetColumns() []string { + vals := make([]string, 0, 4) + if s.Created.IsValue() { + vals = append(vals, "created") + } + if !s.Deleted.IsUnset() { + vals = append(vals, "deleted") + } + if s.DistrictID.IsValue() { + vals = append(vals, "district_id") + } + if s.PhoneE164.IsValue() { + vals = append(vals, "phone_e164") + } + return vals +} + +func (s PublicreportSubscribePhoneSetter) Overwrite(t *PublicreportSubscribePhone) { + if s.Created.IsValue() { + t.Created = s.Created.MustGet() + } + if !s.Deleted.IsUnset() { + t.Deleted = s.Deleted.MustGetNull() + } + if s.DistrictID.IsValue() { + t.DistrictID = s.DistrictID.MustGet() + } + if s.PhoneE164.IsValue() { + t.PhoneE164 = s.PhoneE164.MustGet() + } +} + +func (s *PublicreportSubscribePhoneSetter) Apply(q *dialect.InsertQuery) { + q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { + return PublicreportSubscribePhones.BeforeInsertHooks.RunHooks(ctx, exec, s) + }) + + q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { + vals := make([]bob.Expression, 4) + if s.Created.IsValue() { + vals[0] = psql.Arg(s.Created.MustGet()) + } else { + vals[0] = psql.Raw("DEFAULT") + } + + if !s.Deleted.IsUnset() { + vals[1] = psql.Arg(s.Deleted.MustGetNull()) + } else { + vals[1] = psql.Raw("DEFAULT") + } + + if s.DistrictID.IsValue() { + vals[2] = psql.Arg(s.DistrictID.MustGet()) + } else { + vals[2] = psql.Raw("DEFAULT") + } + + if s.PhoneE164.IsValue() { + vals[3] = psql.Arg(s.PhoneE164.MustGet()) + } else { + vals[3] = psql.Raw("DEFAULT") + } + + return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "") + })) +} + +func (s PublicreportSubscribePhoneSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] { + return um.Set(s.Expressions()...) +} + +func (s PublicreportSubscribePhoneSetter) Expressions(prefix ...string) []bob.Expression { + exprs := make([]bob.Expression, 0, 4) + + if s.Created.IsValue() { + exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ + psql.Quote(append(prefix, "created")...), + psql.Arg(s.Created), + }}) + } + + if !s.Deleted.IsUnset() { + exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ + psql.Quote(append(prefix, "deleted")...), + psql.Arg(s.Deleted), + }}) + } + + if s.DistrictID.IsValue() { + exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ + psql.Quote(append(prefix, "district_id")...), + psql.Arg(s.DistrictID), + }}) + } + + if s.PhoneE164.IsValue() { + exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{ + psql.Quote(append(prefix, "phone_e164")...), + psql.Arg(s.PhoneE164), + }}) + } + + return exprs +} + +// FindPublicreportSubscribePhone retrieves a single record by primary key +// If cols is empty Find will return all columns. +func FindPublicreportSubscribePhone(ctx context.Context, exec bob.Executor, DistrictIDPK int32, PhoneE164PK string, cols ...string) (*PublicreportSubscribePhone, error) { + if len(cols) == 0 { + return PublicreportSubscribePhones.Query( + sm.Where(PublicreportSubscribePhones.Columns.DistrictID.EQ(psql.Arg(DistrictIDPK))), + sm.Where(PublicreportSubscribePhones.Columns.PhoneE164.EQ(psql.Arg(PhoneE164PK))), + ).One(ctx, exec) + } + + return PublicreportSubscribePhones.Query( + sm.Where(PublicreportSubscribePhones.Columns.DistrictID.EQ(psql.Arg(DistrictIDPK))), + sm.Where(PublicreportSubscribePhones.Columns.PhoneE164.EQ(psql.Arg(PhoneE164PK))), + sm.Columns(PublicreportSubscribePhones.Columns.Only(cols...)), + ).One(ctx, exec) +} + +// PublicreportSubscribePhoneExists checks the presence of a single record by primary key +func PublicreportSubscribePhoneExists(ctx context.Context, exec bob.Executor, DistrictIDPK int32, PhoneE164PK string) (bool, error) { + return PublicreportSubscribePhones.Query( + sm.Where(PublicreportSubscribePhones.Columns.DistrictID.EQ(psql.Arg(DistrictIDPK))), + sm.Where(PublicreportSubscribePhones.Columns.PhoneE164.EQ(psql.Arg(PhoneE164PK))), + ).Exists(ctx, exec) +} + +// AfterQueryHook is called after PublicreportSubscribePhone is retrieved from the database +func (o *PublicreportSubscribePhone) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { + var err error + + switch queryType { + case bob.QueryTypeSelect: + ctx, err = PublicreportSubscribePhones.AfterSelectHooks.RunHooks(ctx, exec, PublicreportSubscribePhoneSlice{o}) + case bob.QueryTypeInsert: + ctx, err = PublicreportSubscribePhones.AfterInsertHooks.RunHooks(ctx, exec, PublicreportSubscribePhoneSlice{o}) + case bob.QueryTypeUpdate: + ctx, err = PublicreportSubscribePhones.AfterUpdateHooks.RunHooks(ctx, exec, PublicreportSubscribePhoneSlice{o}) + case bob.QueryTypeDelete: + ctx, err = PublicreportSubscribePhones.AfterDeleteHooks.RunHooks(ctx, exec, PublicreportSubscribePhoneSlice{o}) + } + + return err +} + +// primaryKeyVals returns the primary key values of the PublicreportSubscribePhone +func (o *PublicreportSubscribePhone) primaryKeyVals() bob.Expression { + return psql.ArgGroup( + o.DistrictID, + o.PhoneE164, + ) +} + +func (o *PublicreportSubscribePhone) pkEQ() dialect.Expression { + return psql.Group(psql.Quote("publicreport.subscribe_phone", "district_id"), psql.Quote("publicreport.subscribe_phone", "phone_e164")).EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { + return o.primaryKeyVals().WriteSQL(ctx, w, d, start) + })) +} + +// Update uses an executor to update the PublicreportSubscribePhone +func (o *PublicreportSubscribePhone) Update(ctx context.Context, exec bob.Executor, s *PublicreportSubscribePhoneSetter) error { + v, err := PublicreportSubscribePhones.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec) + if err != nil { + return err + } + + o.R = v.R + *o = *v + + return nil +} + +// Delete deletes a single PublicreportSubscribePhone record with an executor +func (o *PublicreportSubscribePhone) Delete(ctx context.Context, exec bob.Executor) error { + _, err := PublicreportSubscribePhones.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec) + return err +} + +// Reload refreshes the PublicreportSubscribePhone using the executor +func (o *PublicreportSubscribePhone) Reload(ctx context.Context, exec bob.Executor) error { + o2, err := PublicreportSubscribePhones.Query( + sm.Where(PublicreportSubscribePhones.Columns.DistrictID.EQ(psql.Arg(o.DistrictID))), + sm.Where(PublicreportSubscribePhones.Columns.PhoneE164.EQ(psql.Arg(o.PhoneE164))), + ).One(ctx, exec) + if err != nil { + return err + } + o2.R = o.R + *o = *o2 + + return nil +} + +// AfterQueryHook is called after PublicreportSubscribePhoneSlice is retrieved from the database +func (o PublicreportSubscribePhoneSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error { + var err error + + switch queryType { + case bob.QueryTypeSelect: + ctx, err = PublicreportSubscribePhones.AfterSelectHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeInsert: + ctx, err = PublicreportSubscribePhones.AfterInsertHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeUpdate: + ctx, err = PublicreportSubscribePhones.AfterUpdateHooks.RunHooks(ctx, exec, o) + case bob.QueryTypeDelete: + ctx, err = PublicreportSubscribePhones.AfterDeleteHooks.RunHooks(ctx, exec, o) + } + + return err +} + +func (o PublicreportSubscribePhoneSlice) pkIN() dialect.Expression { + if len(o) == 0 { + return psql.Raw("NULL") + } + + return psql.Group(psql.Quote("publicreport.subscribe_phone", "district_id"), psql.Quote("publicreport.subscribe_phone", "phone_e164")).In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) { + pkPairs := make([]bob.Expression, len(o)) + for i, row := range o { + pkPairs[i] = row.primaryKeyVals() + } + return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "") + })) +} + +// copyMatchingRows finds models in the given slice that have the same primary key +// then it first copies the existing relationships from the old model to the new model +// and then replaces the old model in the slice with the new model +func (o PublicreportSubscribePhoneSlice) copyMatchingRows(from ...*PublicreportSubscribePhone) { + for i, old := range o { + for _, new := range from { + if new.DistrictID != old.DistrictID { + continue + } + if new.PhoneE164 != old.PhoneE164 { + continue + } + new.R = old.R + o[i] = new + break + } + } +} + +// UpdateMod modifies an update query with "WHERE primary_key IN (o...)" +func (o PublicreportSubscribePhoneSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] { + return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) { + q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { + return PublicreportSubscribePhones.BeforeUpdateHooks.RunHooks(ctx, exec, o) + }) + + q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error { + var err error + switch retrieved := retrieved.(type) { + case *PublicreportSubscribePhone: + o.copyMatchingRows(retrieved) + case []*PublicreportSubscribePhone: + o.copyMatchingRows(retrieved...) + case PublicreportSubscribePhoneSlice: + o.copyMatchingRows(retrieved...) + default: + // If the retrieved value is not a PublicreportSubscribePhone or a slice of PublicreportSubscribePhone + // then run the AfterUpdateHooks on the slice + _, err = PublicreportSubscribePhones.AfterUpdateHooks.RunHooks(ctx, exec, o) + } + + return err + })) + + q.AppendWhere(o.pkIN()) + }) +} + +// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)" +func (o PublicreportSubscribePhoneSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] { + return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) { + q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) { + return PublicreportSubscribePhones.BeforeDeleteHooks.RunHooks(ctx, exec, o) + }) + + q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error { + var err error + switch retrieved := retrieved.(type) { + case *PublicreportSubscribePhone: + o.copyMatchingRows(retrieved) + case []*PublicreportSubscribePhone: + o.copyMatchingRows(retrieved...) + case PublicreportSubscribePhoneSlice: + o.copyMatchingRows(retrieved...) + default: + // If the retrieved value is not a PublicreportSubscribePhone or a slice of PublicreportSubscribePhone + // then run the AfterDeleteHooks on the slice + _, err = PublicreportSubscribePhones.AfterDeleteHooks.RunHooks(ctx, exec, o) + } + + return err + })) + + q.AppendWhere(o.pkIN()) + }) +} + +func (o PublicreportSubscribePhoneSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals PublicreportSubscribePhoneSetter) error { + if len(o) == 0 { + return nil + } + + _, err := PublicreportSubscribePhones.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec) + return err +} + +func (o PublicreportSubscribePhoneSlice) DeleteAll(ctx context.Context, exec bob.Executor) error { + if len(o) == 0 { + return nil + } + + _, err := PublicreportSubscribePhones.Delete(o.DeleteMod()).Exec(ctx, exec) + return err +} + +func (o PublicreportSubscribePhoneSlice) ReloadAll(ctx context.Context, exec bob.Executor) error { + if len(o) == 0 { + return nil + } + + o2, err := PublicreportSubscribePhones.Query(sm.Where(o.pkIN())).All(ctx, exec) + if err != nil { + return err + } + + o.copyMatchingRows(o2...) + + return nil +} + +// DistrictOrganization starts a query for related objects on organization +func (o *PublicreportSubscribePhone) DistrictOrganization(mods ...bob.Mod[*dialect.SelectQuery]) OrganizationsQuery { + return Organizations.Query(append(mods, + sm.Where(Organizations.Columns.ID.EQ(psql.Arg(o.DistrictID))), + )...) +} + +func (os PublicreportSubscribePhoneSlice) DistrictOrganization(mods ...bob.Mod[*dialect.SelectQuery]) OrganizationsQuery { + pkDistrictID := make(pgtypes.Array[int32], 0, len(os)) + for _, o := range os { + if o == nil { + continue + } + pkDistrictID = append(pkDistrictID, o.DistrictID) + } + PKArgExpr := psql.Select(sm.Columns( + psql.F("unnest", psql.Cast(psql.Arg(pkDistrictID), "integer[]")), + )) + + return Organizations.Query(append(mods, + sm.Where(psql.Group(Organizations.Columns.ID).OP("IN", PKArgExpr)), + )...) +} + +// PhoneE164Phone starts a query for related objects on comms.phone +func (o *PublicreportSubscribePhone) PhoneE164Phone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery { + return CommsPhones.Query(append(mods, + sm.Where(CommsPhones.Columns.E164.EQ(psql.Arg(o.PhoneE164))), + )...) +} + +func (os PublicreportSubscribePhoneSlice) PhoneE164Phone(mods ...bob.Mod[*dialect.SelectQuery]) CommsPhonesQuery { + pkPhoneE164 := make(pgtypes.Array[string], 0, len(os)) + for _, o := range os { + if o == nil { + continue + } + pkPhoneE164 = append(pkPhoneE164, o.PhoneE164) + } + PKArgExpr := psql.Select(sm.Columns( + psql.F("unnest", psql.Cast(psql.Arg(pkPhoneE164), "text[]")), + )) + + return CommsPhones.Query(append(mods, + sm.Where(psql.Group(CommsPhones.Columns.E164).OP("IN", PKArgExpr)), + )...) +} + +func attachPublicreportSubscribePhoneDistrictOrganization0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribePhone0 *PublicreportSubscribePhone, organization1 *Organization) (*PublicreportSubscribePhone, error) { + setter := &PublicreportSubscribePhoneSetter{ + DistrictID: omit.From(organization1.ID), + } + + err := publicreportSubscribePhone0.Update(ctx, exec, setter) + if err != nil { + return nil, fmt.Errorf("attachPublicreportSubscribePhoneDistrictOrganization0: %w", err) + } + + return publicreportSubscribePhone0, nil +} + +func (publicreportSubscribePhone0 *PublicreportSubscribePhone) InsertDistrictOrganization(ctx context.Context, exec bob.Executor, related *OrganizationSetter) error { + var err error + + organization1, err := Organizations.Insert(related).One(ctx, exec) + if err != nil { + return fmt.Errorf("inserting related objects: %w", err) + } + + _, err = attachPublicreportSubscribePhoneDistrictOrganization0(ctx, exec, 1, publicreportSubscribePhone0, organization1) + if err != nil { + return err + } + + publicreportSubscribePhone0.R.DistrictOrganization = organization1 + + organization1.R.DistrictSubscribePhones = append(organization1.R.DistrictSubscribePhones, publicreportSubscribePhone0) + + return nil +} + +func (publicreportSubscribePhone0 *PublicreportSubscribePhone) AttachDistrictOrganization(ctx context.Context, exec bob.Executor, organization1 *Organization) error { + var err error + + _, err = attachPublicreportSubscribePhoneDistrictOrganization0(ctx, exec, 1, publicreportSubscribePhone0, organization1) + if err != nil { + return err + } + + publicreportSubscribePhone0.R.DistrictOrganization = organization1 + + organization1.R.DistrictSubscribePhones = append(organization1.R.DistrictSubscribePhones, publicreportSubscribePhone0) + + return nil +} + +func attachPublicreportSubscribePhonePhoneE164Phone0(ctx context.Context, exec bob.Executor, count int, publicreportSubscribePhone0 *PublicreportSubscribePhone, commsPhone1 *CommsPhone) (*PublicreportSubscribePhone, error) { + setter := &PublicreportSubscribePhoneSetter{ + PhoneE164: omit.From(commsPhone1.E164), + } + + err := publicreportSubscribePhone0.Update(ctx, exec, setter) + if err != nil { + return nil, fmt.Errorf("attachPublicreportSubscribePhonePhoneE164Phone0: %w", err) + } + + return publicreportSubscribePhone0, nil +} + +func (publicreportSubscribePhone0 *PublicreportSubscribePhone) InsertPhoneE164Phone(ctx context.Context, exec bob.Executor, related *CommsPhoneSetter) error { + var err error + + commsPhone1, err := CommsPhones.Insert(related).One(ctx, exec) + if err != nil { + return fmt.Errorf("inserting related objects: %w", err) + } + + _, err = attachPublicreportSubscribePhonePhoneE164Phone0(ctx, exec, 1, publicreportSubscribePhone0, commsPhone1) + if err != nil { + return err + } + + publicreportSubscribePhone0.R.PhoneE164Phone = commsPhone1 + + commsPhone1.R.PhoneE164SubscribePhones = append(commsPhone1.R.PhoneE164SubscribePhones, publicreportSubscribePhone0) + + return nil +} + +func (publicreportSubscribePhone0 *PublicreportSubscribePhone) AttachPhoneE164Phone(ctx context.Context, exec bob.Executor, commsPhone1 *CommsPhone) error { + var err error + + _, err = attachPublicreportSubscribePhonePhoneE164Phone0(ctx, exec, 1, publicreportSubscribePhone0, commsPhone1) + if err != nil { + return err + } + + publicreportSubscribePhone0.R.PhoneE164Phone = commsPhone1 + + commsPhone1.R.PhoneE164SubscribePhones = append(commsPhone1.R.PhoneE164SubscribePhones, publicreportSubscribePhone0) + + return nil +} + +type publicreportSubscribePhoneWhere[Q psql.Filterable] struct { + Created psql.WhereMod[Q, time.Time] + Deleted psql.WhereNullMod[Q, time.Time] + DistrictID psql.WhereMod[Q, int32] + PhoneE164 psql.WhereMod[Q, string] +} + +func (publicreportSubscribePhoneWhere[Q]) AliasedAs(alias string) publicreportSubscribePhoneWhere[Q] { + return buildPublicreportSubscribePhoneWhere[Q](buildPublicreportSubscribePhoneColumns(alias)) +} + +func buildPublicreportSubscribePhoneWhere[Q psql.Filterable](cols publicreportSubscribePhoneColumns) publicreportSubscribePhoneWhere[Q] { + return publicreportSubscribePhoneWhere[Q]{ + Created: psql.Where[Q, time.Time](cols.Created), + Deleted: psql.WhereNull[Q, time.Time](cols.Deleted), + DistrictID: psql.Where[Q, int32](cols.DistrictID), + PhoneE164: psql.Where[Q, string](cols.PhoneE164), + } +} + +func (o *PublicreportSubscribePhone) Preload(name string, retrieved any) error { + if o == nil { + return nil + } + + switch name { + case "DistrictOrganization": + rel, ok := retrieved.(*Organization) + if !ok { + return fmt.Errorf("publicreportSubscribePhone cannot load %T as %q", retrieved, name) + } + + o.R.DistrictOrganization = rel + + if rel != nil { + rel.R.DistrictSubscribePhones = PublicreportSubscribePhoneSlice{o} + } + return nil + case "PhoneE164Phone": + rel, ok := retrieved.(*CommsPhone) + if !ok { + return fmt.Errorf("publicreportSubscribePhone cannot load %T as %q", retrieved, name) + } + + o.R.PhoneE164Phone = rel + + if rel != nil { + rel.R.PhoneE164SubscribePhones = PublicreportSubscribePhoneSlice{o} + } + return nil + default: + return fmt.Errorf("publicreportSubscribePhone has no relationship %q", name) + } +} + +type publicreportSubscribePhonePreloader struct { + DistrictOrganization func(...psql.PreloadOption) psql.Preloader + PhoneE164Phone func(...psql.PreloadOption) psql.Preloader +} + +func buildPublicreportSubscribePhonePreloader() publicreportSubscribePhonePreloader { + return publicreportSubscribePhonePreloader{ + DistrictOrganization: func(opts ...psql.PreloadOption) psql.Preloader { + return psql.Preload[*Organization, OrganizationSlice](psql.PreloadRel{ + Name: "DistrictOrganization", + Sides: []psql.PreloadSide{ + { + From: PublicreportSubscribePhones, + To: Organizations, + FromColumns: []string{"district_id"}, + ToColumns: []string{"id"}, + }, + }, + }, Organizations.Columns.Names(), opts...) + }, + PhoneE164Phone: func(opts ...psql.PreloadOption) psql.Preloader { + return psql.Preload[*CommsPhone, CommsPhoneSlice](psql.PreloadRel{ + Name: "PhoneE164Phone", + Sides: []psql.PreloadSide{ + { + From: PublicreportSubscribePhones, + To: CommsPhones, + FromColumns: []string{"phone_e164"}, + ToColumns: []string{"e164"}, + }, + }, + }, CommsPhones.Columns.Names(), opts...) + }, + } +} + +type publicreportSubscribePhoneThenLoader[Q orm.Loadable] struct { + DistrictOrganization func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] + PhoneE164Phone func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q] +} + +func buildPublicreportSubscribePhoneThenLoader[Q orm.Loadable]() publicreportSubscribePhoneThenLoader[Q] { + type DistrictOrganizationLoadInterface interface { + LoadDistrictOrganization(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } + type PhoneE164PhoneLoadInterface interface { + LoadPhoneE164Phone(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error + } + + return publicreportSubscribePhoneThenLoader[Q]{ + DistrictOrganization: thenLoadBuilder[Q]( + "DistrictOrganization", + func(ctx context.Context, exec bob.Executor, retrieved DistrictOrganizationLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadDistrictOrganization(ctx, exec, mods...) + }, + ), + PhoneE164Phone: thenLoadBuilder[Q]( + "PhoneE164Phone", + func(ctx context.Context, exec bob.Executor, retrieved PhoneE164PhoneLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error { + return retrieved.LoadPhoneE164Phone(ctx, exec, mods...) + }, + ), + } +} + +// LoadDistrictOrganization loads the publicreportSubscribePhone's DistrictOrganization into the .R struct +func (o *PublicreportSubscribePhone) LoadDistrictOrganization(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + // Reset the relationship + o.R.DistrictOrganization = nil + + related, err := o.DistrictOrganization(mods...).One(ctx, exec) + if err != nil { + return err + } + + related.R.DistrictSubscribePhones = PublicreportSubscribePhoneSlice{o} + + o.R.DistrictOrganization = related + return nil +} + +// LoadDistrictOrganization loads the publicreportSubscribePhone's DistrictOrganization into the .R struct +func (os PublicreportSubscribePhoneSlice) LoadDistrictOrganization(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + organizations, err := os.DistrictOrganization(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, o := range os { + if o == nil { + continue + } + + for _, rel := range organizations { + + if !(o.DistrictID == rel.ID) { + continue + } + + rel.R.DistrictSubscribePhones = append(rel.R.DistrictSubscribePhones, o) + + o.R.DistrictOrganization = rel + break + } + } + + return nil +} + +// LoadPhoneE164Phone loads the publicreportSubscribePhone's PhoneE164Phone into the .R struct +func (o *PublicreportSubscribePhone) LoadPhoneE164Phone(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if o == nil { + return nil + } + + // Reset the relationship + o.R.PhoneE164Phone = nil + + related, err := o.PhoneE164Phone(mods...).One(ctx, exec) + if err != nil { + return err + } + + related.R.PhoneE164SubscribePhones = PublicreportSubscribePhoneSlice{o} + + o.R.PhoneE164Phone = related + return nil +} + +// LoadPhoneE164Phone loads the publicreportSubscribePhone's PhoneE164Phone into the .R struct +func (os PublicreportSubscribePhoneSlice) LoadPhoneE164Phone(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error { + if len(os) == 0 { + return nil + } + + commsPhones, err := os.PhoneE164Phone(mods...).All(ctx, exec) + if err != nil { + return err + } + + for _, o := range os { + if o == nil { + continue + } + + for _, rel := range commsPhones { + + if !(o.PhoneE164 == rel.E164) { + continue + } + + rel.R.PhoneE164SubscribePhones = append(rel.R.PhoneE164SubscribePhones, o) + + o.R.PhoneE164Phone = rel + break + } + } + + return nil +} + +type publicreportSubscribePhoneJoins[Q dialect.Joinable] struct { + typ string + DistrictOrganization modAs[Q, organizationColumns] + PhoneE164Phone modAs[Q, commsPhoneColumns] +} + +func (j publicreportSubscribePhoneJoins[Q]) aliasedAs(alias string) publicreportSubscribePhoneJoins[Q] { + return buildPublicreportSubscribePhoneJoins[Q](buildPublicreportSubscribePhoneColumns(alias), j.typ) +} + +func buildPublicreportSubscribePhoneJoins[Q dialect.Joinable](cols publicreportSubscribePhoneColumns, typ string) publicreportSubscribePhoneJoins[Q] { + return publicreportSubscribePhoneJoins[Q]{ + typ: typ, + DistrictOrganization: modAs[Q, organizationColumns]{ + c: Organizations.Columns, + f: func(to organizationColumns) bob.Mod[Q] { + mods := make(mods.QueryMods[Q], 0, 1) + + { + mods = append(mods, dialect.Join[Q](typ, Organizations.Name().As(to.Alias())).On( + to.ID.EQ(cols.DistrictID), + )) + } + + return mods + }, + }, + PhoneE164Phone: modAs[Q, commsPhoneColumns]{ + c: CommsPhones.Columns, + f: func(to commsPhoneColumns) bob.Mod[Q] { + mods := make(mods.QueryMods[Q], 0, 1) + + { + mods = append(mods, dialect.Join[Q](typ, CommsPhones.Name().As(to.Alias())).On( + to.E164.EQ(cols.PhoneE164), + )) + } + + return mods + }, + }, + } +} diff --git a/platform/report/notification.go b/platform/report/notification.go index 193401f1..b9f1b83c 100644 --- a/platform/report/notification.go +++ b/platform/report/notification.go @@ -7,11 +7,11 @@ import ( "math/big" "strconv" "strings" - //"time" + "time" - //"github.com/aarondl/opt/omit" - //"github.com/aarondl/opt/omitnull" "github.com/Gleipnir-Technology/bob" + "github.com/aarondl/opt/omit" + "github.com/aarondl/opt/omitnull" //"github.com/Gleipnir-Technology/bob/dialect/psql" //"github.com/Gleipnir-Technology/bob/dialect/psql/sm" //"github.com/Gleipnir-Technology/bob/dialect/psql/um" @@ -100,12 +100,41 @@ func RegisterNotificationPhone(ctx context.Context, txn bob.Tx, report_id string return nil } -func RegisterSubscriptionEmail(ctx context.Context, txn bob.Tx, email string) *ErrorWithCode { - log.Warn().Str("email", email).Msg("RegisterSubscription not implemented yet") +func RegisterSubscriptionEmail(ctx context.Context, txn bob.Tx, destination string) *ErrorWithCode { + e := email.EnsureInDB(ctx, destination) + if e != nil { + return newInternalError(e, "Failed to ensure email is in DB") + } + setter := models.PublicreportSubscribeEmailSetter{ + Created: omit.From(time.Now()), + Deleted: omitnull.FromPtr[time.Time](nil), + //DistrictID: omit.FromPtr[int32](nil), + EmailAddress: omit.From(destination), + } + _, err := models.PublicreportSubscribeEmails.Insert(&setter).Exec(ctx, txn) + if err != nil { + log.Error().Err(err).Msg("Failed to save new subscription email row") + return newInternalError(err, "Failed to save new subscription email row") + } + return nil } func RegisterSubscriptionPhone(ctx context.Context, txn bob.Tx, phone text.E164) *ErrorWithCode { - log.Warn().Str("phone", text.PhoneString(phone)).Msg("RegisterSubscription not implemented yet") + e := text.EnsureInDB(ctx, phone) + if e != nil { + return newInternalError(e, "Failed to ensure phone is in DB") + } + setter := models.PublicreportSubscribePhoneSetter{ + Created: omit.From(time.Now()), + Deleted: omitnull.FromPtr[time.Time](nil), + //DistrictID: omitnull.FromPtr[int32](nil), + PhoneE164: omit.From(text.PhoneString(phone)), + } + _, err := models.PublicreportSubscribePhones.Insert(&setter).Exec(ctx, txn) + if err != nil { + log.Error().Err(err).Msg("Failed to save new subscription phone row") + return newInternalError(err, "Failed to save new subscription phone row") + } return nil }