26 lines
1.2 KiB
TypeScript
26 lines
1.2 KiB
TypeScript
import { m } from "@peppa/shared/i18n";
|
|
import type { Db } from "@peppa/db";
|
|
import { listSessions, markProjectDeleted, projectForUser, purgeDeletedProjects, type Project } from "@peppa/core";
|
|
import { stopSession } from "./sessions.ts";
|
|
|
|
/**
|
|
* PEP-163: the owner deletes a project — its agents stop, it disappears from the people's lists
|
|
* and stays with the superadmin for PEPPA_PROJECT_TRASH_DAYS (7). The repository is not touched.
|
|
*/
|
|
export async function deleteProject(db: Db, userId: number, projectId: number): Promise<{ stopped: number }> {
|
|
const project: Project = await projectForUser(db, userId, projectId);
|
|
await markProjectDeleted(db, project, userId);
|
|
let stopped = 0;
|
|
for (const s of await listSessions(db, { active: true, projectId })) {
|
|
await stopSession(db, s.id).catch(() => {});
|
|
stopped++;
|
|
}
|
|
return { stopped };
|
|
}
|
|
|
|
/** Watchdog: projects whose week is over go for good. */
|
|
export async function projectTrashTick(db: Db, opts: { now?: Date; log?: (line: string) => void } = {}): Promise<void> {
|
|
const keys = await purgeDeletedProjects(db, opts.now ?? new Date());
|
|
for (const key of keys) opts.log?.(m("Проект {key} удалён навсегда: срок хранения вышел", { key }));
|
|
}
|