Add settins connector view

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2025-04-20 20:00:48 -07:00
parent 85935ac1dc
commit 9aeed6da4a
14 changed files with 1823 additions and 132 deletions

View File

@@ -18,11 +18,13 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"time"
"github.com/getprobo/probo/pkg/connector"
"github.com/getprobo/probo/pkg/crypto/cipher"
"github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
)
@@ -38,8 +40,65 @@ type (
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
Connectors []*Connector
)
func (c *Connectors) LoadWithoutDecryptedConnectionByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor[ConnectorOrderField],
encryptionKey cipher.EncryptionKey,
) error {
q := `
SELECT
id,
organization_id,
name,
type,
encrypted_connection,
created_at,
updated_at
FROM
connectors
WHERE
%s
AND organization_id = @organization_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query connectors: %w", err)
}
connectors, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Connector])
if err != nil {
return fmt.Errorf("cannot collect connectors: %w", err)
}
*c = connectors
return nil
}
func (c *Connector) CursorKey(orderBy ConnectorOrderField) page.CursorKey {
switch orderBy {
case ConnectorOrderFieldCreatedAt:
return page.CursorKey{ID: c.ID, Value: c.CreatedAt}
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (c *Connector) Upsert(
ctx context.Context,
conn pg.Conn,

View File

@@ -0,0 +1,41 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
type (
ConnectorOrderField string
)
const (
ConnectorOrderFieldCreatedAt ConnectorOrderField = "CREATED_AT"
ConnectorOrderFieldName ConnectorOrderField = "NAME"
)
func (p ConnectorOrderField) Column() string {
return string(p)
}
func (p ConnectorOrderField) String() string {
return string(p)
}
func (p ConnectorOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *ConnectorOrderField) UnmarshalText(text []byte) error {
*p = ConnectorOrderField(text)
return nil
}