Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
6.5 KB
Raw Blame
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: composio.sql

package db

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const getUserComposioConnection = `-- name: GetUserComposioConnection :one
SELECT id, user_id, toolkit_slug, auth_config_id, connected_account_id, composio_user_id, status, connected_at, last_used_at, created_at, updated_at, workspace_id FROM user_composio_connection
WHERE id = $1 AND user_id = $2
`

type GetUserComposioConnectionParams struct {
	ID     pgtype.UUID `json:"id"`
	UserID pgtype.UUID `json:"user_id"`
}

// Owner-scoped lookup: a connection can only be read by the user who owns it,
// so one user cannot disconnect another's account by guessing the UUID.
func (q *Queries) GetUserComposioConnection(ctx context.Context, arg GetUserComposioConnectionParams) (UserComposioConnection, error) {
	row := q.db.QueryRow(ctx, getUserComposioConnection, arg.ID, arg.UserID)
	var i UserComposioConnection
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.ToolkitSlug,
		&i.AuthConfigID,
		&i.ConnectedAccountID,
		&i.ComposioUserID,
		&i.Status,
		&i.ConnectedAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}

const listActiveUserComposioConnections = `-- name: ListActiveUserComposioConnections :many
SELECT id, user_id, toolkit_slug, auth_config_id, connected_account_id, composio_user_id, status, connected_at, last_used_at, created_at, updated_at, workspace_id FROM user_composio_connection
WHERE user_id = $1 AND status = 'active'
  AND (workspace_id = $2::uuid OR workspace_id IS NULL)
ORDER BY (workspace_id IS NULL), connected_at DESC
`

type ListActiveUserComposioConnectionsParams struct {
	UserID      pgtype.UUID `json:"user_id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

// A member's active personal accounts in one workspace, plus the ones made
// before accounts belonged to a workspace. This workspace's own come first,
// so a reader keeping the first row per app prefers them.
func (q *Queries) ListActiveUserComposioConnections(ctx context.Context, arg ListActiveUserComposioConnectionsParams) ([]UserComposioConnection, error) {
	rows, err := q.db.Query(ctx, listActiveUserComposioConnections, arg.UserID, arg.WorkspaceID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []UserComposioConnection{}
	for rows.Next() {
		var i UserComposioConnection
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.ToolkitSlug,
			&i.AuthConfigID,
			&i.ConnectedAccountID,
			&i.ComposioUserID,
			&i.Status,
			&i.ConnectedAt,
			&i.LastUsedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.WorkspaceID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listAllActiveUserComposioConnections = `-- name: ListAllActiveUserComposioConnections :many
SELECT id, user_id, toolkit_slug, auth_config_id, connected_account_id, composio_user_id, status, connected_at, last_used_at, created_at, updated_at, workspace_id FROM user_composio_connection
WHERE user_id = $1 AND status = 'active'
ORDER BY connected_at DESC
`

// Every active personal account of a member, in any workspace.
func (q *Queries) ListAllActiveUserComposioConnections(ctx context.Context, userID pgtype.UUID) ([]UserComposioConnection, error) {
	rows, err := q.db.Query(ctx, listAllActiveUserComposioConnections, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []UserComposioConnection{}
	for rows.Next() {
		var i UserComposioConnection
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.ToolkitSlug,
			&i.AuthConfigID,
			&i.ConnectedAccountID,
			&i.ComposioUserID,
			&i.Status,
			&i.ConnectedAt,
			&i.LastUsedAt,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.WorkspaceID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markUserComposioConnectionRevoked = `-- name: MarkUserComposioConnectionRevoked :exec
UPDATE user_composio_connection
SET status = 'revoked', updated_at = now()
WHERE id = $1 AND user_id = $2
`

type MarkUserComposioConnectionRevokedParams struct {
	ID     pgtype.UUID `json:"id"`
	UserID pgtype.UUID `json:"user_id"`
}

// Idempotent: re-running on an already-revoked row is a no-op write. Scoped to
// the owner for defense-in-depth.
func (q *Queries) MarkUserComposioConnectionRevoked(ctx context.Context, arg MarkUserComposioConnectionRevokedParams) error {
	_, err := q.db.Exec(ctx, markUserComposioConnectionRevoked, arg.ID, arg.UserID)
	return err
}

const upsertUserComposioConnection = `-- name: UpsertUserComposioConnection :one

INSERT INTO user_composio_connection (
    user_id, toolkit_slug, auth_config_id, connected_account_id, composio_user_id, status, workspace_id
) VALUES (
    $1, $2, $3, $4, $5, 'active', $6::uuid
)
ON CONFLICT (user_id, connected_account_id) DO UPDATE SET
    toolkit_slug     = EXCLUDED.toolkit_slug,
    auth_config_id   = EXCLUDED.auth_config_id,
    composio_user_id = EXCLUDED.composio_user_id,
    status           = 'active',
    updated_at       = now()
RETURNING id, user_id, toolkit_slug, auth_config_id, connected_account_id, composio_user_id, status, connected_at, last_used_at, created_at, updated_at, workspace_id
`

type UpsertUserComposioConnectionParams struct {
	UserID             pgtype.UUID `json:"user_id"`
	ToolkitSlug        string      `json:"toolkit_slug"`
	AuthConfigID       string      `json:"auth_config_id"`
	ConnectedAccountID string      `json:"connected_account_id"`
	ComposioUserID     string      `json:"composio_user_id"`
	WorkspaceID        pgtype.UUID `json:"workspace_id"`
}

// =====================
// User Composio Connection
// =====================
// Idempotent on (user_id, connected_account_id): a duplicate callback for the
// same connected account re-activates the row instead of inserting a second
// one. connected_at is preserved on conflict (first-connect time); updated_at
// moves so the reactivation is observable.
func (q *Queries) UpsertUserComposioConnection(ctx context.Context, arg UpsertUserComposioConnectionParams) (UserComposioConnection, error) {
	row := q.db.QueryRow(ctx, upsertUserComposioConnection,
		arg.UserID,
		arg.ToolkitSlug,
		arg.AuthConfigID,
		arg.ConnectedAccountID,
		arg.ComposioUserID,
		arg.WorkspaceID,
	)
	var i UserComposioConnection
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.ToolkitSlug,
		&i.AuthConfigID,
		&i.ConnectedAccountID,
		&i.ComposioUserID,
		&i.Status,
		&i.ConnectedAt,
		&i.LastUsedAt,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.WorkspaceID,
	)
	return i, err
}