Skip to content

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

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

package db

import (
	"context"

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

const archiveProject = `-- name: ArchiveProject :one
UPDATE project SET
    archived_at = now(),
    archived_by_type = $3,
    archived_by_id = $4,
    updated_at = now()
WHERE id = $1 AND workspace_id = $2 AND archived_at IS NULL
RETURNING id, workspace_id, title, description, icon, status, lead_type, lead_id, created_at, updated_at, priority, start_date, due_date, workflow_id, creator_type, creator_id, visibility, archived_at, archived_by_type, archived_by_id, folder_id
`

type ArchiveProjectParams struct {
	ID             pgtype.UUID `json:"id"`
	WorkspaceID    pgtype.UUID `json:"workspace_id"`
	ArchivedByType pgtype.Text `json:"archived_by_type"`
	ArchivedByID   pgtype.UUID `json:"archived_by_id"`
}

// Guarded on archived_at IS NULL so a repeated archive keeps the first
// actor and time; no row means it was already archived (or is gone).
func (q *Queries) ArchiveProject(ctx context.Context, arg ArchiveProjectParams) (Project, error) {
	row := q.db.QueryRow(ctx, archiveProject,
		arg.ID,
		arg.WorkspaceID,
		arg.ArchivedByType,
		arg.ArchivedByID,
	)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Title,
		&i.Description,
		&i.Icon,
		&i.Status,
		&i.LeadType,
		&i.LeadID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Priority,
		&i.StartDate,
		&i.DueDate,
		&i.WorkflowID,
		&i.CreatorType,
		&i.CreatorID,
		&i.Visibility,
		&i.ArchivedAt,
		&i.ArchivedByType,
		&i.ArchivedByID,
		&i.FolderID,
	)
	return i, err
}

const countIssuesByProject = `-- name: CountIssuesByProject :one
SELECT count(*) FROM issue
WHERE project_id = $1
`

func (q *Queries) CountIssuesByProject(ctx context.Context, projectID pgtype.UUID) (int64, error) {
	row := q.db.QueryRow(ctx, countIssuesByProject, projectID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createProject = `-- name: CreateProject :one
INSERT INTO project (
    workspace_id, title, description, icon, status,
    lead_type, lead_id, priority, start_date, due_date, workflow_id,
    creator_type, creator_id
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
) RETURNING id, workspace_id, title, description, icon, status, lead_type, lead_id, created_at, updated_at, priority, start_date, due_date, workflow_id, creator_type, creator_id, visibility, archived_at, archived_by_type, archived_by_id, folder_id
`

type CreateProjectParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	Title       string      `json:"title"`
	Description pgtype.Text `json:"description"`
	Icon        pgtype.Text `json:"icon"`
	Status      string      `json:"status"`
	LeadType    pgtype.Text `json:"lead_type"`
	LeadID      pgtype.UUID `json:"lead_id"`
	Priority    string      `json:"priority"`
	StartDate   pgtype.Date `json:"start_date"`
	DueDate     pgtype.Date `json:"due_date"`
	WorkflowID  pgtype.UUID `json:"workflow_id"`
	CreatorType pgtype.Text `json:"creator_type"`
	CreatorID   pgtype.UUID `json:"creator_id"`
}

func (q *Queries) CreateProject(ctx context.Context, arg CreateProjectParams) (Project, error) {
	row := q.db.QueryRow(ctx, createProject,
		arg.WorkspaceID,
		arg.Title,
		arg.Description,
		arg.Icon,
		arg.Status,
		arg.LeadType,
		arg.LeadID,
		arg.Priority,
		arg.StartDate,
		arg.DueDate,
		arg.WorkflowID,
		arg.CreatorType,
		arg.CreatorID,
	)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Title,
		&i.Description,
		&i.Icon,
		&i.Status,
		&i.LeadType,
		&i.LeadID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Priority,
		&i.StartDate,
		&i.DueDate,
		&i.WorkflowID,
		&i.CreatorType,
		&i.CreatorID,
		&i.Visibility,
		&i.ArchivedAt,
		&i.ArchivedByType,
		&i.ArchivedByID,
		&i.FolderID,
	)
	return i, err
}

const deleteProject = `-- name: DeleteProject :exec
DELETE FROM project WHERE id = $1 AND workspace_id = $2
`

type DeleteProjectParams struct {
	ID          pgtype.UUID `json:"id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

// Defense-in-depth: workspace_id is a SQL-layer tenant guard. See DeleteIssue.
func (q *Queries) DeleteProject(ctx context.Context, arg DeleteProjectParams) error {
	_, err := q.db.Exec(ctx, deleteProject, arg.ID, arg.WorkspaceID)
	return err
}

const getProjectInWorkspace = `-- name: GetProjectInWorkspace :one
SELECT id, workspace_id, title, description, icon, status, lead_type, lead_id, created_at, updated_at, priority, start_date, due_date, workflow_id, creator_type, creator_id, visibility, archived_at, archived_by_type, archived_by_id, folder_id FROM project
WHERE id = $1 AND workspace_id = $2
`

type GetProjectInWorkspaceParams struct {
	ID          pgtype.UUID `json:"id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

func (q *Queries) GetProjectInWorkspace(ctx context.Context, arg GetProjectInWorkspaceParams) (Project, error) {
	row := q.db.QueryRow(ctx, getProjectInWorkspace, arg.ID, arg.WorkspaceID)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Title,
		&i.Description,
		&i.Icon,
		&i.Status,
		&i.LeadType,
		&i.LeadID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Priority,
		&i.StartDate,
		&i.DueDate,
		&i.WorkflowID,
		&i.CreatorType,
		&i.CreatorID,
		&i.Visibility,
		&i.ArchivedAt,
		&i.ArchivedByType,
		&i.ArchivedByID,
		&i.FolderID,
	)
	return i, err
}

const getProjectIssueStats = `-- name: GetProjectIssueStats :many
SELECT project_id,
       count(*)::bigint AS total_count,
       count(*) FILTER (WHERE status = ANY($1::text[]))::bigint AS done_count,
       count(*) FILTER (WHERE status = ANY($2::text[]))::bigint AS completed_count,
       count(*) FILTER (WHERE status = ANY($3::text[]))::bigint AS started_count,
       max(GREATEST(updated_at, last_activity_at))::timestamptz AS last_issue_activity_at
FROM issue
WHERE workspace_id = $4::uuid
  AND project_id = ANY($5::uuid[])
GROUP BY project_id
`

type GetProjectIssueStatsParams struct {
	TerminalStatusKeys  []string      `json:"terminal_status_keys"`
	CompletedStatusKeys []string      `json:"completed_status_keys"`
	StartedStatusKeys   []string      `json:"started_status_keys"`
	WorkspaceID         pgtype.UUID   `json:"workspace_id"`
	ProjectIds          []pgtype.UUID `json:"project_ids"`
}

type GetProjectIssueStatsRow struct {
	ProjectID           pgtype.UUID        `json:"project_id"`
	TotalCount          int64              `json:"total_count"`
	DoneCount           int64              `json:"done_count"`
	CompletedCount      int64              `json:"completed_count"`
	StartedCount        int64              `json:"started_count"`
	LastIssueActivityAt pgtype.Timestamptz `json:"last_issue_activity_at"`
}

// done_count counts every terminal issue (done and closed categories) and
// keeps that meaning for older clients; completed_count is the done category
// alone, so cancelled = done_count - completed_count.
func (q *Queries) GetProjectIssueStats(ctx context.Context, arg GetProjectIssueStatsParams) ([]GetProjectIssueStatsRow, error) {
	rows, err := q.db.Query(ctx, getProjectIssueStats,
		arg.TerminalStatusKeys,
		arg.CompletedStatusKeys,
		arg.StartedStatusKeys,
		arg.WorkspaceID,
		arg.ProjectIds,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetProjectIssueStatsRow{}
	for rows.Next() {
		var i GetProjectIssueStatsRow
		if err := rows.Scan(
			&i.ProjectID,
			&i.TotalCount,
			&i.DoneCount,
			&i.CompletedCount,
			&i.StartedCount,
			&i.LastIssueActivityAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listIssueStatusChangesSince = `-- name: ListIssueStatusChangesSince :many
SELECT issue_id, details, created_at
FROM activity_log
WHERE workspace_id = $1::uuid
  AND issue_id = ANY($2::uuid[])
  AND action = 'status_changed'
  AND created_at >= $3::timestamptz
ORDER BY created_at, id
`

type ListIssueStatusChangesSinceParams struct {
	WorkspaceID pgtype.UUID        `json:"workspace_id"`
	IssueIds    []pgtype.UUID      `json:"issue_ids"`
	Since       pgtype.Timestamptz `json:"since"`
}

type ListIssueStatusChangesSinceRow struct {
	IssueID   pgtype.UUID        `json:"issue_id"`
	Details   []byte             `json:"details"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
}

func (q *Queries) ListIssueStatusChangesSince(ctx context.Context, arg ListIssueStatusChangesSinceParams) ([]ListIssueStatusChangesSinceRow, error) {
	rows, err := q.db.Query(ctx, listIssueStatusChangesSince, arg.WorkspaceID, arg.IssueIds, arg.Since)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListIssueStatusChangesSinceRow{}
	for rows.Next() {
		var i ListIssueStatusChangesSinceRow
		if err := rows.Scan(&i.IssueID, &i.Details, &i.CreatedAt); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listProjectActivity = `-- name: ListProjectActivity :many
SELECT a.id, a.issue_id, a.actor_type, a.actor_id, a.action, a.details, a.created_at,
       i.number AS issue_number, i.title AS issue_title, i.status AS issue_status
FROM activity_log a
JOIN issue i ON i.id = a.issue_id
WHERE i.workspace_id = $1::uuid
  AND i.project_id = $2::uuid
  AND a.action = ANY($3::text[])
  AND (
      $4::timestamptz IS NULL
      OR (a.created_at, a.id) < ($4::timestamptz, $5::uuid)
  )
ORDER BY a.created_at DESC, a.id DESC
LIMIT $6
`

type ListProjectActivityParams struct {
	WorkspaceID pgtype.UUID        `json:"workspace_id"`
	ProjectID   pgtype.UUID        `json:"project_id"`
	Actions     []string           `json:"actions"`
	BeforeAt    pgtype.Timestamptz `json:"before_at"`
	BeforeID    pgtype.UUID        `json:"before_id"`
	RowLimit    int32              `json:"row_limit"`
}

type ListProjectActivityRow struct {
	ID          pgtype.UUID        `json:"id"`
	IssueID     pgtype.UUID        `json:"issue_id"`
	ActorType   pgtype.Text        `json:"actor_type"`
	ActorID     pgtype.UUID        `json:"actor_id"`
	Action      string             `json:"action"`
	Details     []byte             `json:"details"`
	CreatedAt   pgtype.Timestamptz `json:"created_at"`
	IssueNumber int32              `json:"issue_number"`
	IssueTitle  string             `json:"issue_title"`
	IssueStatus string             `json:"issue_status"`
}

// The newest activity on a project's issues, newest first, keyset-paged on
// (created_at, id). Only the given actions are read, so autosaves and run
// bookkeeping stay out of the project feed. A zero cursor reads from the top.
func (q *Queries) ListProjectActivity(ctx context.Context, arg ListProjectActivityParams) ([]ListProjectActivityRow, error) {
	rows, err := q.db.Query(ctx, listProjectActivity,
		arg.WorkspaceID,
		arg.ProjectID,
		arg.Actions,
		arg.BeforeAt,
		arg.BeforeID,
		arg.RowLimit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListProjectActivityRow{}
	for rows.Next() {
		var i ListProjectActivityRow
		if err := rows.Scan(
			&i.ID,
			&i.IssueID,
			&i.ActorType,
			&i.ActorID,
			&i.Action,
			&i.Details,
			&i.CreatedAt,
			&i.IssueNumber,
			&i.IssueTitle,
			&i.IssueStatus,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listProjectProgressIssues = `-- name: ListProjectProgressIssues :many
SELECT id, status, assignee_type, assignee_id, created_at,
       ARRAY(
           SELECT il.label_id FROM issue_to_label il
           WHERE il.issue_id = issue.id
           ORDER BY il.label_id
       )::uuid[] AS label_ids
FROM issue
WHERE workspace_id = $1::uuid
  AND project_id = $2::uuid
`

type ListProjectProgressIssuesParams struct {
	WorkspaceID pgtype.UUID `json:"workspace_id"`
	ProjectID   pgtype.UUID `json:"project_id"`
}

type ListProjectProgressIssuesRow struct {
	ID           pgtype.UUID        `json:"id"`
	Status       string             `json:"status"`
	AssigneeType pgtype.Text        `json:"assignee_type"`
	AssigneeID   pgtype.UUID        `json:"assignee_id"`
	CreatedAt    pgtype.Timestamptz `json:"created_at"`
	LabelIds     []pgtype.UUID      `json:"label_ids"`
}

func (q *Queries) ListProjectProgressIssues(ctx context.Context, arg ListProjectProgressIssuesParams) ([]ListProjectProgressIssuesRow, error) {
	rows, err := q.db.Query(ctx, listProjectProgressIssues, arg.WorkspaceID, arg.ProjectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListProjectProgressIssuesRow{}
	for rows.Next() {
		var i ListProjectProgressIssuesRow
		if err := rows.Scan(
			&i.ID,
			&i.Status,
			&i.AssigneeType,
			&i.AssigneeID,
			&i.CreatedAt,
			&i.LabelIds,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listProjects = `-- name: ListProjects :many
SELECT id, workspace_id, title, description, icon, status, lead_type, lead_id, created_at, updated_at, priority, start_date, due_date, workflow_id, creator_type, creator_id, visibility, archived_at, archived_by_type, archived_by_id, folder_id FROM project
WHERE workspace_id = $1
  AND ($2::text IS NULL OR status = $2)
  AND ($3::text IS NULL OR priority = $3)
  AND ($4::boolean OR archived_at IS NULL)
ORDER BY created_at DESC
`

type ListProjectsParams struct {
	WorkspaceID     pgtype.UUID `json:"workspace_id"`
	Status          pgtype.Text `json:"status"`
	Priority        pgtype.Text `json:"priority"`
	IncludeArchived bool        `json:"include_archived"`
}

func (q *Queries) ListProjects(ctx context.Context, arg ListProjectsParams) ([]Project, error) {
	rows, err := q.db.Query(ctx, listProjects,
		arg.WorkspaceID,
		arg.Status,
		arg.Priority,
		arg.IncludeArchived,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Project{}
	for rows.Next() {
		var i Project
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceID,
			&i.Title,
			&i.Description,
			&i.Icon,
			&i.Status,
			&i.LeadType,
			&i.LeadID,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Priority,
			&i.StartDate,
			&i.DueDate,
			&i.WorkflowID,
			&i.CreatorType,
			&i.CreatorID,
			&i.Visibility,
			&i.ArchivedAt,
			&i.ArchivedByType,
			&i.ArchivedByID,
			&i.FolderID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const lockProjectForChatSessionCreate = `-- name: LockProjectForChatSessionCreate :one
SELECT id FROM project
WHERE id = $1 AND workspace_id = $2
FOR KEY SHARE
`

type LockProjectForChatSessionCreateParams struct {
	ID          pgtype.UUID `json:"id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

// Conflicts with project deletion so a chat session cannot commit a soft
// project reference after the delete transaction has swept existing sessions.
func (q *Queries) LockProjectForChatSessionCreate(ctx context.Context, arg LockProjectForChatSessionCreateParams) (pgtype.UUID, error) {
	row := q.db.QueryRow(ctx, lockProjectForChatSessionCreate, arg.ID, arg.WorkspaceID)
	var id pgtype.UUID
	err := row.Scan(&id)
	return id, err
}

const lockProjectForDelete = `-- name: LockProjectForDelete :one
SELECT id FROM project
WHERE id = $1 AND workspace_id = $2
FOR UPDATE
`

type LockProjectForDeleteParams struct {
	ID          pgtype.UUID `json:"id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

// Serializes project deletion with chat-session creation. The handler locks,
// clears every soft chat reference, and deletes the project in one transaction.
func (q *Queries) LockProjectForDelete(ctx context.Context, arg LockProjectForDeleteParams) (pgtype.UUID, error) {
	row := q.db.QueryRow(ctx, lockProjectForDelete, arg.ID, arg.WorkspaceID)
	var id pgtype.UUID
	err := row.Scan(&id)
	return id, err
}

const restoreProject = `-- name: RestoreProject :one
UPDATE project SET
    archived_at = NULL,
    archived_by_type = NULL,
    archived_by_id = NULL,
    updated_at = now()
WHERE id = $1 AND workspace_id = $2 AND archived_at IS NOT NULL
RETURNING id, workspace_id, title, description, icon, status, lead_type, lead_id, created_at, updated_at, priority, start_date, due_date, workflow_id, creator_type, creator_id, visibility, archived_at, archived_by_type, archived_by_id, folder_id
`

type RestoreProjectParams struct {
	ID          pgtype.UUID `json:"id"`
	WorkspaceID pgtype.UUID `json:"workspace_id"`
}

func (q *Queries) RestoreProject(ctx context.Context, arg RestoreProjectParams) (Project, error) {
	row := q.db.QueryRow(ctx, restoreProject, arg.ID, arg.WorkspaceID)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Title,
		&i.Description,
		&i.Icon,
		&i.Status,
		&i.LeadType,
		&i.LeadID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Priority,
		&i.StartDate,
		&i.DueDate,
		&i.WorkflowID,
		&i.CreatorType,
		&i.CreatorID,
		&i.Visibility,
		&i.ArchivedAt,
		&i.ArchivedByType,
		&i.ArchivedByID,
		&i.FolderID,
	)
	return i, err
}

const updateProject = `-- name: UpdateProject :one
UPDATE project SET
    title = COALESCE($2, title),
    description = CASE WHEN $3::boolean THEN $4 ELSE description END,
    icon = CASE WHEN $5::boolean THEN $6 ELSE icon END,
    status = COALESCE($7, status),
    priority = COALESCE($8, priority),
    lead_type = CASE WHEN $9::boolean THEN $10 ELSE lead_type END,
    lead_id = CASE WHEN $11::boolean THEN $12 ELSE lead_id END,
    start_date = CASE WHEN $13::boolean THEN $14 ELSE start_date END,
    due_date = CASE WHEN $15::boolean THEN $16 ELSE due_date END,
    updated_at = now()
WHERE id = $1
RETURNING id, workspace_id, title, description, icon, status, lead_type, lead_id, created_at, updated_at, priority, start_date, due_date, workflow_id, creator_type, creator_id, visibility, archived_at, archived_by_type, archived_by_id, folder_id
`

type UpdateProjectParams struct {
	ID             pgtype.UUID `json:"id"`
	Title          pgtype.Text `json:"title"`
	DescriptionSet bool        `json:"description_set"`
	Description    pgtype.Text `json:"description"`
	IconSet        bool        `json:"icon_set"`
	Icon           pgtype.Text `json:"icon"`
	Status         pgtype.Text `json:"status"`
	Priority       pgtype.Text `json:"priority"`
	LeadTypeSet    bool        `json:"lead_type_set"`
	LeadType       pgtype.Text `json:"lead_type"`
	LeadIDSet      bool        `json:"lead_id_set"`
	LeadID         pgtype.UUID `json:"lead_id"`
	StartDateSet   bool        `json:"start_date_set"`
	StartDate      pgtype.Date `json:"start_date"`
	DueDateSet     bool        `json:"due_date_set"`
	DueDate        pgtype.Date `json:"due_date"`
}

func (q *Queries) UpdateProject(ctx context.Context, arg UpdateProjectParams) (Project, error) {
	row := q.db.QueryRow(ctx, updateProject,
		arg.ID,
		arg.Title,
		arg.DescriptionSet,
		arg.Description,
		arg.IconSet,
		arg.Icon,
		arg.Status,
		arg.Priority,
		arg.LeadTypeSet,
		arg.LeadType,
		arg.LeadIDSet,
		arg.LeadID,
		arg.StartDateSet,
		arg.StartDate,
		arg.DueDateSet,
		arg.DueDate,
	)
	var i Project
	err := row.Scan(
		&i.ID,
		&i.WorkspaceID,
		&i.Title,
		&i.Description,
		&i.Icon,
		&i.Status,
		&i.LeadType,
		&i.LeadID,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Priority,
		&i.StartDate,
		&i.DueDate,
		&i.WorkflowID,
		&i.CreatorType,
		&i.CreatorID,
		&i.Visibility,
		&i.ArchivedAt,
		&i.ArchivedByType,
		&i.ArchivedByID,
		&i.FolderID,
	)
	return i, err
}