nidus-sync/db/factory/comms.phone.bob.go
Eli Ribble 842e6cff43
Move comms work to background goroutine
This is a sort of random checkpoint of work
 * add schema for tracking messages sent to DB
 * add terms of service and privacy policy for RCS compliance
 * standardize some things about background workers
 * update some missing stuff from generated DB code
2026-01-20 17:10:22 +00:00

562 lines
16 KiB
Go

// Code generated by BobGen psql v0.42.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package factory
import (
"context"
"testing"
models "github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/aarondl/opt/omit"
"github.com/jaswdr/faker/v2"
"github.com/stephenafamo/bob"
)
type CommsPhoneMod interface {
Apply(context.Context, *CommsPhoneTemplate)
}
type CommsPhoneModFunc func(context.Context, *CommsPhoneTemplate)
func (f CommsPhoneModFunc) Apply(ctx context.Context, n *CommsPhoneTemplate) {
f(ctx, n)
}
type CommsPhoneModSlice []CommsPhoneMod
func (mods CommsPhoneModSlice) Apply(ctx context.Context, n *CommsPhoneTemplate) {
for _, f := range mods {
f.Apply(ctx, n)
}
}
// CommsPhoneTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type CommsPhoneTemplate struct {
E164 func() string
IsSubscribed func() bool
r commsPhoneR
f *Factory
alreadyPersisted bool
}
type commsPhoneR struct {
SourceEmailLogs []*commsPhoneRSourceEmailLogsR
DestinationSMSLogs []*commsPhoneRDestinationSMSLogsR
SourceSMSLogs []*commsPhoneRSourceSMSLogsR
}
type commsPhoneRSourceEmailLogsR struct {
number int
o *CommsEmailLogTemplate
}
type commsPhoneRDestinationSMSLogsR struct {
number int
o *CommsSMSLogTemplate
}
type commsPhoneRSourceSMSLogsR struct {
number int
o *CommsSMSLogTemplate
}
// Apply mods to the CommsPhoneTemplate
func (o *CommsPhoneTemplate) Apply(ctx context.Context, mods ...CommsPhoneMod) {
for _, mod := range mods {
mod.Apply(ctx, o)
}
}
// setModelRels creates and sets the relationships on *models.CommsPhone
// according to the relationships in the template. Nothing is inserted into the db
func (t CommsPhoneTemplate) setModelRels(o *models.CommsPhone) {
if t.r.SourceEmailLogs != nil {
rel := models.CommsEmailLogSlice{}
for _, r := range t.r.SourceEmailLogs {
related := r.o.BuildMany(r.number)
for _, rel := range related {
rel.Source = o.E164 // h2
rel.R.SourcePhone = o
}
rel = append(rel, related...)
}
o.R.SourceEmailLogs = rel
}
if t.r.DestinationSMSLogs != nil {
rel := models.CommsSMSLogSlice{}
for _, r := range t.r.DestinationSMSLogs {
related := r.o.BuildMany(r.number)
for _, rel := range related {
rel.Destination = o.E164 // h2
rel.R.DestinationPhone = o
}
rel = append(rel, related...)
}
o.R.DestinationSMSLogs = rel
}
if t.r.SourceSMSLogs != nil {
rel := models.CommsSMSLogSlice{}
for _, r := range t.r.SourceSMSLogs {
related := r.o.BuildMany(r.number)
for _, rel := range related {
rel.Source = o.E164 // h2
rel.R.SourcePhone = o
}
rel = append(rel, related...)
}
o.R.SourceSMSLogs = rel
}
}
// BuildSetter returns an *models.CommsPhoneSetter
// this does nothing with the relationship templates
func (o CommsPhoneTemplate) BuildSetter() *models.CommsPhoneSetter {
m := &models.CommsPhoneSetter{}
if o.E164 != nil {
val := o.E164()
m.E164 = omit.From(val)
}
if o.IsSubscribed != nil {
val := o.IsSubscribed()
m.IsSubscribed = omit.From(val)
}
return m
}
// BuildManySetter returns an []*models.CommsPhoneSetter
// this does nothing with the relationship templates
func (o CommsPhoneTemplate) BuildManySetter(number int) []*models.CommsPhoneSetter {
m := make([]*models.CommsPhoneSetter, number)
for i := range m {
m[i] = o.BuildSetter()
}
return m
}
// Build returns an *models.CommsPhone
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use CommsPhoneTemplate.Create
func (o CommsPhoneTemplate) Build() *models.CommsPhone {
m := &models.CommsPhone{}
if o.E164 != nil {
m.E164 = o.E164()
}
if o.IsSubscribed != nil {
m.IsSubscribed = o.IsSubscribed()
}
o.setModelRels(m)
return m
}
// BuildMany returns an models.CommsPhoneSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use CommsPhoneTemplate.CreateMany
func (o CommsPhoneTemplate) BuildMany(number int) models.CommsPhoneSlice {
m := make(models.CommsPhoneSlice, number)
for i := range m {
m[i] = o.Build()
}
return m
}
func ensureCreatableCommsPhone(m *models.CommsPhoneSetter) {
if !(m.E164.IsValue()) {
val := random_string(nil)
m.E164 = omit.From(val)
}
if !(m.IsSubscribed.IsValue()) {
val := random_bool(nil)
m.IsSubscribed = omit.From(val)
}
}
// insertOptRels creates and inserts any optional the relationships on *models.CommsPhone
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *CommsPhoneTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.CommsPhone) error {
var err error
isSourceEmailLogsDone, _ := commsPhoneRelSourceEmailLogsCtx.Value(ctx)
if !isSourceEmailLogsDone && o.r.SourceEmailLogs != nil {
ctx = commsPhoneRelSourceEmailLogsCtx.WithValue(ctx, true)
for _, r := range o.r.SourceEmailLogs {
if r.o.alreadyPersisted {
m.R.SourceEmailLogs = append(m.R.SourceEmailLogs, r.o.Build())
} else {
rel0, err := r.o.CreateMany(ctx, exec, r.number)
if err != nil {
return err
}
err = m.AttachSourceEmailLogs(ctx, exec, rel0...)
if err != nil {
return err
}
}
}
}
isDestinationSMSLogsDone, _ := commsPhoneRelDestinationSMSLogsCtx.Value(ctx)
if !isDestinationSMSLogsDone && o.r.DestinationSMSLogs != nil {
ctx = commsPhoneRelDestinationSMSLogsCtx.WithValue(ctx, true)
for _, r := range o.r.DestinationSMSLogs {
if r.o.alreadyPersisted {
m.R.DestinationSMSLogs = append(m.R.DestinationSMSLogs, r.o.Build())
} else {
rel1, err := r.o.CreateMany(ctx, exec, r.number)
if err != nil {
return err
}
err = m.AttachDestinationSMSLogs(ctx, exec, rel1...)
if err != nil {
return err
}
}
}
}
isSourceSMSLogsDone, _ := commsPhoneRelSourceSMSLogsCtx.Value(ctx)
if !isSourceSMSLogsDone && o.r.SourceSMSLogs != nil {
ctx = commsPhoneRelSourceSMSLogsCtx.WithValue(ctx, true)
for _, r := range o.r.SourceSMSLogs {
if r.o.alreadyPersisted {
m.R.SourceSMSLogs = append(m.R.SourceSMSLogs, r.o.Build())
} else {
rel2, err := r.o.CreateMany(ctx, exec, r.number)
if err != nil {
return err
}
err = m.AttachSourceSMSLogs(ctx, exec, rel2...)
if err != nil {
return err
}
}
}
}
return err
}
// Create builds a commsPhone and inserts it into the database
// Relations objects are also inserted and placed in the .R field
func (o *CommsPhoneTemplate) Create(ctx context.Context, exec bob.Executor) (*models.CommsPhone, error) {
var err error
opt := o.BuildSetter()
ensureCreatableCommsPhone(opt)
m, err := models.CommsPhones.Insert(opt).One(ctx, exec)
if err != nil {
return nil, err
}
if err := o.insertOptRels(ctx, exec, m); err != nil {
return nil, err
}
return m, err
}
// MustCreate builds a commsPhone and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *CommsPhoneTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.CommsPhone {
m, err := o.Create(ctx, exec)
if err != nil {
panic(err)
}
return m
}
// CreateOrFail builds a commsPhone 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 *CommsPhoneTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.CommsPhone {
tb.Helper()
m, err := o.Create(ctx, exec)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// CreateMany builds multiple commsPhones and inserts them into the database
// Relations objects are also inserted and placed in the .R field
func (o CommsPhoneTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.CommsPhoneSlice, error) {
var err error
m := make(models.CommsPhoneSlice, number)
for i := range m {
m[i], err = o.Create(ctx, exec)
if err != nil {
return nil, err
}
}
return m, nil
}
// MustCreateMany builds multiple commsPhones and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o CommsPhoneTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.CommsPhoneSlice {
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
panic(err)
}
return m
}
// CreateManyOrFail builds multiple commsPhones 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 CommsPhoneTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.CommsPhoneSlice {
tb.Helper()
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// CommsPhone has methods that act as mods for the CommsPhoneTemplate
var CommsPhoneMods commsPhoneMods
type commsPhoneMods struct{}
func (m commsPhoneMods) RandomizeAllColumns(f *faker.Faker) CommsPhoneMod {
return CommsPhoneModSlice{
CommsPhoneMods.RandomE164(f),
CommsPhoneMods.RandomIsSubscribed(f),
}
}
// Set the model columns to this value
func (m commsPhoneMods) E164(val string) CommsPhoneMod {
return CommsPhoneModFunc(func(_ context.Context, o *CommsPhoneTemplate) {
o.E164 = func() string { return val }
})
}
// Set the Column from the function
func (m commsPhoneMods) E164Func(f func() string) CommsPhoneMod {
return CommsPhoneModFunc(func(_ context.Context, o *CommsPhoneTemplate) {
o.E164 = f
})
}
// Clear any values for the column
func (m commsPhoneMods) UnsetE164() CommsPhoneMod {
return CommsPhoneModFunc(func(_ context.Context, o *CommsPhoneTemplate) {
o.E164 = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m commsPhoneMods) RandomE164(f *faker.Faker) CommsPhoneMod {
return CommsPhoneModFunc(func(_ context.Context, o *CommsPhoneTemplate) {
o.E164 = func() string {
return random_string(f)
}
})
}
// Set the model columns to this value
func (m commsPhoneMods) IsSubscribed(val bool) CommsPhoneMod {
return CommsPhoneModFunc(func(_ context.Context, o *CommsPhoneTemplate) {
o.IsSubscribed = func() bool { return val }
})
}
// Set the Column from the function
func (m commsPhoneMods) IsSubscribedFunc(f func() bool) CommsPhoneMod {
return CommsPhoneModFunc(func(_ context.Context, o *CommsPhoneTemplate) {
o.IsSubscribed = f
})
}
// Clear any values for the column
func (m commsPhoneMods) UnsetIsSubscribed() CommsPhoneMod {
return CommsPhoneModFunc(func(_ context.Context, o *CommsPhoneTemplate) {
o.IsSubscribed = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m commsPhoneMods) RandomIsSubscribed(f *faker.Faker) CommsPhoneMod {
return CommsPhoneModFunc(func(_ context.Context, o *CommsPhoneTemplate) {
o.IsSubscribed = func() bool {
return random_bool(f)
}
})
}
func (m commsPhoneMods) WithParentsCascading() CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
if isDone, _ := commsPhoneWithParentsCascadingCtx.Value(ctx); isDone {
return
}
ctx = commsPhoneWithParentsCascadingCtx.WithValue(ctx, true)
})
}
func (m commsPhoneMods) WithSourceEmailLogs(number int, related *CommsEmailLogTemplate) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
o.r.SourceEmailLogs = []*commsPhoneRSourceEmailLogsR{{
number: number,
o: related,
}}
})
}
func (m commsPhoneMods) WithNewSourceEmailLogs(number int, mods ...CommsEmailLogMod) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
related := o.f.NewCommsEmailLogWithContext(ctx, mods...)
m.WithSourceEmailLogs(number, related).Apply(ctx, o)
})
}
func (m commsPhoneMods) AddSourceEmailLogs(number int, related *CommsEmailLogTemplate) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
o.r.SourceEmailLogs = append(o.r.SourceEmailLogs, &commsPhoneRSourceEmailLogsR{
number: number,
o: related,
})
})
}
func (m commsPhoneMods) AddNewSourceEmailLogs(number int, mods ...CommsEmailLogMod) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
related := o.f.NewCommsEmailLogWithContext(ctx, mods...)
m.AddSourceEmailLogs(number, related).Apply(ctx, o)
})
}
func (m commsPhoneMods) AddExistingSourceEmailLogs(existingModels ...*models.CommsEmailLog) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
for _, em := range existingModels {
o.r.SourceEmailLogs = append(o.r.SourceEmailLogs, &commsPhoneRSourceEmailLogsR{
o: o.f.FromExistingCommsEmailLog(em),
})
}
})
}
func (m commsPhoneMods) WithoutSourceEmailLogs() CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
o.r.SourceEmailLogs = nil
})
}
func (m commsPhoneMods) WithDestinationSMSLogs(number int, related *CommsSMSLogTemplate) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
o.r.DestinationSMSLogs = []*commsPhoneRDestinationSMSLogsR{{
number: number,
o: related,
}}
})
}
func (m commsPhoneMods) WithNewDestinationSMSLogs(number int, mods ...CommsSMSLogMod) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
related := o.f.NewCommsSMSLogWithContext(ctx, mods...)
m.WithDestinationSMSLogs(number, related).Apply(ctx, o)
})
}
func (m commsPhoneMods) AddDestinationSMSLogs(number int, related *CommsSMSLogTemplate) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
o.r.DestinationSMSLogs = append(o.r.DestinationSMSLogs, &commsPhoneRDestinationSMSLogsR{
number: number,
o: related,
})
})
}
func (m commsPhoneMods) AddNewDestinationSMSLogs(number int, mods ...CommsSMSLogMod) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
related := o.f.NewCommsSMSLogWithContext(ctx, mods...)
m.AddDestinationSMSLogs(number, related).Apply(ctx, o)
})
}
func (m commsPhoneMods) AddExistingDestinationSMSLogs(existingModels ...*models.CommsSMSLog) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
for _, em := range existingModels {
o.r.DestinationSMSLogs = append(o.r.DestinationSMSLogs, &commsPhoneRDestinationSMSLogsR{
o: o.f.FromExistingCommsSMSLog(em),
})
}
})
}
func (m commsPhoneMods) WithoutDestinationSMSLogs() CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
o.r.DestinationSMSLogs = nil
})
}
func (m commsPhoneMods) WithSourceSMSLogs(number int, related *CommsSMSLogTemplate) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
o.r.SourceSMSLogs = []*commsPhoneRSourceSMSLogsR{{
number: number,
o: related,
}}
})
}
func (m commsPhoneMods) WithNewSourceSMSLogs(number int, mods ...CommsSMSLogMod) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
related := o.f.NewCommsSMSLogWithContext(ctx, mods...)
m.WithSourceSMSLogs(number, related).Apply(ctx, o)
})
}
func (m commsPhoneMods) AddSourceSMSLogs(number int, related *CommsSMSLogTemplate) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
o.r.SourceSMSLogs = append(o.r.SourceSMSLogs, &commsPhoneRSourceSMSLogsR{
number: number,
o: related,
})
})
}
func (m commsPhoneMods) AddNewSourceSMSLogs(number int, mods ...CommsSMSLogMod) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
related := o.f.NewCommsSMSLogWithContext(ctx, mods...)
m.AddSourceSMSLogs(number, related).Apply(ctx, o)
})
}
func (m commsPhoneMods) AddExistingSourceSMSLogs(existingModels ...*models.CommsSMSLog) CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
for _, em := range existingModels {
o.r.SourceSMSLogs = append(o.r.SourceSMSLogs, &commsPhoneRSourceSMSLogsR{
o: o.f.FromExistingCommsSMSLog(em),
})
}
})
}
func (m commsPhoneMods) WithoutSourceSMSLogs() CommsPhoneMod {
return CommsPhoneModFunc(func(ctx context.Context, o *CommsPhoneTemplate) {
o.r.SourceSMSLogs = nil
})
}