Skip to content

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

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

package db

import (
	"context"

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

const backfillLegacyFeishuInboundReceipts = `-- name: BackfillLegacyFeishuInboundReceipts :execrows
WITH evidence AS MATERIALIZED (
    SELECT installation.id, installation.installed_at, min(binding.created_at) AS received_at
    FROM channel_installation AS installation
    JOIN channel_chat_session_binding AS binding
      ON binding.installation_id = installation.id
     AND binding.channel_type = installation.channel_type
     AND binding.created_at >= installation.installed_at
    WHERE installation.channel_type = 'feishu'
      AND installation.status = 'active'
      AND installation.config->>'binding_generation' IS NULL
      AND installation.config->>'first_message_received_at' IS NULL
    GROUP BY installation.id, installation.installed_at
    ORDER BY installation.id
    LIMIT $1::int
)
UPDATE channel_installation AS installation
SET config = jsonb_set(installation.config, '{first_message_received_at}', to_jsonb(evidence.received_at))
FROM evidence
WHERE installation.id = evidence.id
  AND installation.installed_at = evidence.installed_at
  AND installation.channel_type = 'feishu'
  AND installation.status = 'active'
  AND installation.config->>'binding_generation' IS NULL
  AND installation.config->>'first_message_received_at' IS NULL
`

// Session routes are positive receipt evidence only for the current binding.
// Materialize the candidate timestamp; recheck eligibility after any concurrent
// writer, preserving live receipt writes and never validating a later rebind.
func (q *Queries) BackfillLegacyFeishuInboundReceipts(ctx context.Context, batchSize int32) (int64, error) {
	result, err := q.db.Exec(ctx, backfillLegacyFeishuInboundReceipts, batchSize)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const recordFeishuInboundReceipt = `-- name: RecordFeishuInboundReceipt :exec
UPDATE channel_installation
SET config = jsonb_set(config, '{first_message_received_at}', to_jsonb(now()))
WHERE id = $1
  AND workspace_id = $2
  AND channel_type = 'feishu'
  AND status = 'active'
  AND config->>'app_id' = $3::text
  AND COALESCE(config->>'binding_generation', '') = $4::text
  AND config->>'first_message_received_at' IS NULL
`

type RecordFeishuInboundReceiptParams struct {
	ID                pgtype.UUID `json:"id"`
	WorkspaceID       pgtype.UUID `json:"workspace_id"`
	AppID             string      `json:"app_id"`
	BindingGeneration string      `json:"binding_generation"`
}

// Rebind replaces config with a new generation and no receipt. Legacy configs
// without a generation match only other legacy snapshots, never a new binding.
func (q *Queries) RecordFeishuInboundReceipt(ctx context.Context, arg RecordFeishuInboundReceiptParams) error {
	_, err := q.db.Exec(ctx, recordFeishuInboundReceipt,
		arg.ID,
		arg.WorkspaceID,
		arg.AppID,
		arg.BindingGeneration,
	)
	return err
}

const setFeishuInstallationBotUnionID = `-- name: SetFeishuInstallationBotUnionID :exec
UPDATE channel_installation
SET config = jsonb_set(config, '{bot_union_id}', to_jsonb($1::text)),
    updated_at = now()
WHERE id = $2 AND channel_type = 'feishu'
`

type SetFeishuInstallationBotUnionIDParams struct {
	BotUnionID string      `json:"bot_union_id"`
	ID         pgtype.UUID `json:"id"`
}

// Preserve concurrently recorded receipt evidence and binding generation.
func (q *Queries) SetFeishuInstallationBotUnionID(ctx context.Context, arg SetFeishuInstallationBotUnionIDParams) error {
	_, err := q.db.Exec(ctx, setFeishuInstallationBotUnionID, arg.BotUnionID, arg.ID)
	return err
}