112 lines · 4.3 KB
import { env } from "cloudflare:workers";
import { all, first } from "./db";
import { canCreateInAccount, visibleReposClause } from "./repos";
import { viewerFromSessionCookie } from "./auth";
import { limitBrowse } from "./limits";
import { HttpError, forbidden, notFoundError } from "./errors";
import type { Viewer } from "~/lib/types";

const LIVE_PATH = /^\/projects\/([^/]+)\/(\d+)\/live$/;

export type ProjectRow = {
  id: string;
  owner_id: string;
  number: number;
  title: string;
  description: string;
  visibility: "public" | "private";
  closed: number;
  created_by: string;
  created_at: number;
  updated_at: number;
};

export type ProjectFieldRow = {
  id: string;
  project_id: string;
  name: string;
  kind: "text" | "number" | "date" | "single_select";
  options: string;
  position: number;
};

export type ProjectAccess = { project: ProjectRow; ownerName: string; canWrite: boolean };

export async function loadProjectAccess(
  ownerName: string,
  number: number,
  viewer: Viewer | null,
): Promise<ProjectAccess> {
  const row = await first<ProjectRow & { owner_name: string }>(