74 lines
2.8 KiB
Go
74 lines
2.8 KiB
Go
package comms
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
//"github.com/Gleipnir-Technology/bob"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
//"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/public/enum"
|
|
"github.com/Gleipnir-Technology/jet/postgres"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/model"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/gen/nidus-sync/comms/table"
|
|
)
|
|
|
|
func ContactPhoneInsert(ctx context.Context, txn db.Ex, m model.ContactPhone) (model.ContactPhone, error) {
|
|
statement := table.ContactPhone.INSERT(table.ContactPhone.MutableColumns).
|
|
MODEL(m).
|
|
RETURNING(table.ContactPhone.AllColumns)
|
|
return db.ExecuteOneTx[model.ContactPhone](ctx, txn, statement)
|
|
}
|
|
|
|
func ContactPhoneFromE164(ctx context.Context, txn db.Ex, e164 string) (model.ContactPhone, error) {
|
|
statement := table.ContactPhone.SELECT(
|
|
table.ContactPhone.AllColumns,
|
|
).FROM(table.ContactPhone).
|
|
WHERE(table.ContactPhone.E164.EQ(postgres.String(e164)))
|
|
return db.ExecuteOneTx[model.ContactPhone](ctx, txn, statement)
|
|
}
|
|
|
|
func ContactPhoneUpdateConfirmedMessageID(ctx context.Context, txn db.Ex, e164 string, message_id *int32) error {
|
|
statement := table.ContactPhone.UPDATE().
|
|
SET(table.ContactPhone.ConfirmedMessageID.SET(postgres.IntExp(postgres.NULL))).
|
|
WHERE(table.ContactPhone.E164.EQ(postgres.String(e164)))
|
|
return db.ExecuteNoneTx(ctx, txn, statement)
|
|
}
|
|
func ContactPhoneUpdateStopMessageID(ctx context.Context, txn db.Ex, e164 string, message_id *int32) error {
|
|
/*
|
|
m := model.ContactPhone{}
|
|
m.StopMessageID = message_id
|
|
statement := table.ContactPhone.UPDATE(
|
|
table.ContactPhone.StopMessageID,
|
|
).MODEL(m).
|
|
WHERE(table.ContactPhone.E164.EQ(postgres.String(e164)))
|
|
*/
|
|
statement := table.ContactPhone.UPDATE().
|
|
SET(table.ContactPhone.StopMessageID.SET(postgres.IntExp(postgres.NULL))).
|
|
WHERE(table.ContactPhone.E164.EQ(postgres.String(e164)))
|
|
return db.ExecuteNoneTx(ctx, txn, statement)
|
|
}
|
|
func ContactPhoneByContactIDs(ctx context.Context, txn db.Ex, contact_ids []int64) (result map[int64][]model.ContactPhone, err error) {
|
|
sql_ids := make([]postgres.Expression, len(contact_ids))
|
|
for i, contact_id := range contact_ids {
|
|
sql_ids[i] = postgres.Int(contact_id)
|
|
}
|
|
statement := table.ContactPhone.SELECT(
|
|
table.ContactPhone.AllColumns,
|
|
).FROM(table.ContactPhone).
|
|
WHERE(table.ContactPhone.ContactID.IN(sql_ids...))
|
|
rows, err := db.ExecuteManyTx[model.ContactPhone](ctx, txn, statement)
|
|
if err != nil {
|
|
return result, fmt.Errorf("query by contact IDs: %w", err)
|
|
}
|
|
result = make(map[int64][]model.ContactPhone, len(contact_ids))
|
|
for _, contact_id := range contact_ids {
|
|
result[contact_id] = make([]model.ContactPhone, 0)
|
|
}
|
|
for _, row := range rows {
|
|
id := int64(row.ContactID)
|
|
cur := result[id]
|
|
cur = append(cur, row)
|
|
result[id] = cur
|
|
}
|
|
return result, nil
|
|
}
|