peppa/packages/core/src/project-trash.ts

53 lines
3.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { m as tr } from "@peppa/shared/i18n";
import { projects, type Db } from "@peppa/db";
import { and, eq, isNotNull, lt } from "drizzle-orm";
import { PeppaError } from "./errors.ts";
import { logEvent } from "./audit.ts";
import { isSuperadmin } from "./monitor.ts";
import { getUser } from "./users.ts";
import type { Project } from "./projects.ts";
/** PEP-163: a deleted project stays with the superadmin this many days, then goes for good. */
export function projectTrashDays(): number {
const n = Number(process.env.PEPPA_PROJECT_TRASH_DAYS);
return Number.isFinite(n) && n > 0 ? n : 7;
}
const DAY_MS = 86_400_000;
/** Whole days left before the purge (0 — the next watchdog tick takes it). */
export function trashDaysLeft(p: Pick<Project, "deletedAt">, now = new Date()): number {
if (!p.deletedAt) return projectTrashDays();
return Math.max(0, Math.ceil((p.deletedAt.getTime() + projectTrashDays() * DAY_MS - now.getTime()) / DAY_MS));
}
/** PEP-181: whether the delete button is shown to this viewer — owner or superadmin, not the platform project, not already deleted. */
export function mayDeleteProject(p: Pick<Project, "key" | "ownerId" | "deletedAt">, userId: number, superadmin: boolean): boolean {
return !p.deletedAt && p.key !== (process.env.PEPPA_PLATFORM_KEY || "PEP") && (p.ownerId === userId || superadmin);
}
/** Marks the project deleted: only its owner or a superadmin; the platform project never. */
export async function markProjectDeleted(db: Db, project: Project, userId: number): Promise<void> {
if (project.key === (process.env.PEPPA_PLATFORM_KEY || "PEP")) throw new PeppaError(tr("Этот проект удалить нельзя"));
const user = await getUser(db, userId);
if (project.ownerId !== userId && !(await isSuperadmin(db, user))) throw new PeppaError(tr("Удалить проект может только его владелец"));
if (project.deletedAt) return;
await db.update(projects).set({ deletedAt: new Date(), deletedBy: userId }).where(eq(projects.id, project.id));
await logEvent(db, { projectId: project.id, type: "project_deleted", payload: { by: userId } });
}
/** Superadmin brings a deleted project back. */
export async function restoreProject(db: Db, projectId: number, userId: number): Promise<void> {
if (!(await isSuperadmin(db, await getUser(db, userId)))) throw new PeppaError(tr("Не найдено"));
const [p] = await db.update(projects).set({ deletedAt: null, deletedBy: null }).where(and(eq(projects.id, projectId), isNotNull(projects.deletedAt))).returning();
if (!p) throw new PeppaError(tr("Не найдено"));
await logEvent(db, { projectId, type: "project_restored", payload: { by: userId } });
}
/** Removes projects deleted longer than the trash period ago. Returns their keys. */
export async function purgeDeletedProjects(db: Db, now = new Date()): Promise<string[]> {
const cutoff = new Date(now.getTime() - projectTrashDays() * DAY_MS);
const rows = await db.delete(projects).where(and(isNotNull(projects.deletedAt), lt(projects.deletedAt, cutoff))).returning({ key: projects.key });
return rows.map((r) => r.key);
}