package comms import ( "context" "github.com/Gleipnir-Technology/jet/postgres" "source.gleipnir.technology/Gleipnir/nidus-sync/db" "source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/comms/model" "source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/comms/table" ) func PhoneInsertIfNotExists(ctx context.Context, txn db.Ex, m model.Phone) (model.Phone, error) { inserted := postgres.CTE("inserted") insert := table.Phone.INSERT( table.Phone.AllColumns, ).MODEL(m). ON_CONFLICT(table.Phone.E164).DO_NOTHING(). RETURNING(table.Phone.AllColumns) statement := postgres.WITH(inserted.AS(insert))( postgres.SELECT(inserted.AllColumns()). FROM(inserted). UNION_ALL( postgres.SELECT(table.Phone.AllColumns). FROM(table.Phone). WHERE(postgres.AND( table.Phone.E164.EQ(postgres.String(m.E164)), postgres.NOT(postgres.EXISTS( postgres.SELECT(postgres.STAR).FROM(inserted), )), )), ), ) return db.ExecuteOneTx[model.Phone](ctx, txn, statement) } func PhoneFromE164(ctx context.Context, txn db.Ex, e164 string) (model.Phone, error) { statement := table.Phone.SELECT( table.Phone.AllColumns, ).FROM(table.Phone). WHERE(table.Phone.E164.EQ(postgres.String(e164))) return db.ExecuteOneTx[model.Phone](ctx, txn, statement) }