2026-05-15 16:58:28 +00:00
|
|
|
package comms
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-05-19 00:33:14 +00:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2026-05-15 16:58:28 +00:00
|
|
|
|
|
|
|
|
//"github.com/Gleipnir-Technology/bob"
|
2026-05-19 15:33:57 +00:00
|
|
|
"source.gleipnir.technology/Gleipnir/nidus-sync/db"
|
|
|
|
|
//"source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/public/enum"
|
2026-05-15 16:58:28 +00:00
|
|
|
"github.com/Gleipnir-Technology/jet/postgres"
|
2026-05-19 15:33:57 +00:00
|
|
|
"source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/comms/model"
|
|
|
|
|
"source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/comms/table"
|
2026-05-15 16:58:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func ContactInsert(ctx context.Context, txn db.Ex, m model.Contact) (model.Contact, error) {
|
|
|
|
|
statement := table.Contact.INSERT(table.Contact.MutableColumns).
|
|
|
|
|
MODEL(m).
|
|
|
|
|
RETURNING(table.Contact.AllColumns)
|
|
|
|
|
return db.ExecuteOneTx[model.Contact](ctx, txn, statement)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 00:33:14 +00:00
|
|
|
func ContactEmptyForOrganization(ctx context.Context, txn db.Ex, org_id int64) (*model.Contact, error) {
|
|
|
|
|
statement := table.Contact.SELECT(
|
|
|
|
|
table.Contact.AllColumns,
|
|
|
|
|
).FROM(table.Contact).
|
|
|
|
|
WHERE(
|
|
|
|
|
postgres.AND(
|
|
|
|
|
table.Contact.OrganizationID.EQ(postgres.Int(org_id)),
|
|
|
|
|
table.Contact.Name.EQ(postgres.String("")),
|
|
|
|
|
postgres.NOT(
|
|
|
|
|
postgres.EXISTS(
|
|
|
|
|
postgres.SELECT(
|
|
|
|
|
postgres.Int(1),
|
|
|
|
|
).FROM(table.ContactEmail).
|
|
|
|
|
WHERE(table.ContactEmail.ContactID.EQ(table.Contact.ID)),
|
|
|
|
|
)),
|
|
|
|
|
postgres.NOT(
|
|
|
|
|
postgres.EXISTS(
|
|
|
|
|
postgres.SELECT(
|
|
|
|
|
postgres.Int(1),
|
|
|
|
|
).FROM(table.ContactPhone).
|
|
|
|
|
WHERE(table.ContactPhone.ContactID.EQ(table.Contact.ID)),
|
|
|
|
|
)),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
row, err := db.ExecuteOne[model.Contact](ctx, statement)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, db.ErrNoRows) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("query contact: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return &row, nil
|
|
|
|
|
}
|
2026-05-15 16:58:28 +00:00
|
|
|
func ContactFromID(ctx context.Context, txn db.Ex, id int64) (model.Contact, error) {
|
|
|
|
|
statement := table.Contact.SELECT(
|
|
|
|
|
table.Contact.AllColumns,
|
|
|
|
|
).FROM(table.Contact).
|
|
|
|
|
WHERE(table.Contact.ID.EQ(postgres.Int(id)))
|
|
|
|
|
return db.ExecuteOne[model.Contact](ctx, statement)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ContactUpdateName(ctx context.Context, txn db.Ex, id int64, name string) error {
|
|
|
|
|
statement := table.Contact.UPDATE().
|
|
|
|
|
SET(
|
|
|
|
|
table.Contact.Name.SET(postgres.String(name)),
|
|
|
|
|
).
|
|
|
|
|
WHERE(table.Contact.OrganizationID.EQ(postgres.Int(id)))
|
|
|
|
|
return db.ExecuteNoneTx(ctx, txn, statement)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 20:10:14 +00:00
|
|
|
func ContactsFromOrganizationID(ctx context.Context, txn db.Ex, org_id int64) ([]model.Contact, error) {
|
2026-05-15 16:58:28 +00:00
|
|
|
statement := table.Contact.SELECT(
|
|
|
|
|
table.Contact.AllColumns,
|
|
|
|
|
).FROM(table.Contact).
|
2026-05-15 20:10:14 +00:00
|
|
|
WHERE(table.Contact.OrganizationID.EQ(postgres.Int(org_id)))
|
|
|
|
|
return db.ExecuteManyTx[model.Contact](ctx, txn, statement)
|
2026-05-15 16:58:28 +00:00
|
|
|
}
|