The key item here is that comms.phone and comms.email are meant to represent a real global namespace, but comms.contact is meant to represent an organization-specific namespace. This means the mapping, comms.contact_phone and comms.contact_email can't key off the global namespace. Otherwise the contact namespace would implicitly be global.
42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
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)
|
|
}
|