import { Injectable, NotFoundException } from '@nestjs/common'; import { PageView } from '@dorfteich/shared'; import { User } from '@prisma/client'; import { PinoLogger } from 'nestjs-pino'; import { ClockService } from '../common/clock.service'; import { PagesService } from '../pages/pages.service'; import { PermissionService } from '../permissions/permission.service'; import { PrismaService } from '../prisma/prisma.service'; import { QuotaService } from '../quotas/quota.service'; import { InstanceSettingsService } from '../settings/instance-settings.service'; import { FileStorageService } from '../files/file-storage.service'; const MS_PER_DAY = 24 * 60 * 60 * 1000; /** * Page trash: soft delete (already done by `PagesService.softDelete`, * issue #23), list/restore/purge-single, and the scheduled purge job * (issue #31, ADR 0013). Deleting a page never touches its content or * files — only `purgePage` does, so restore is always intact by * construction as long as purge hasn't run yet. */ @Injectable() export class TrashService { constructor( private readonly prisma: PrismaService, private readonly permissions: PermissionService, private readonly pages: PagesService, private readonly settings: InstanceSettingsService, private readonly quotas: QuotaService, private readonly storage: FileStorageService, private readonly clock: ClockService, private readonly logger: PinoLogger, ) { this.logger.setContext(TrashService.name); } /** A pond's trash: the trashed pages the user could edit — trash access is * write capability (ADR 0013), resolved per page (issue #52). */ async list(user: User, pondId: string): Promise { const pond = await this.prisma.pond.findFirst({ where: { id: pondId, deletedAt: null } }); if (!pond) throw new NotFoundException(); const pages = await this.prisma.page.findMany({ where: { pondId, deletedAt: { not: null } }, orderBy: { deletedAt: 'desc' }, }); const editable = await this.permissions.filterPages( user, pondId, pages.map((page) => ({ id: page.id })), 'write', ); return pages.filter((page) => editable.has(page.id)).map((page) => this.pages.viewOf(page)); } async restore(user: User, id: string): Promise { const page = await this.prisma.page.findFirst({ where: { id } }); if (!page || !page.deletedAt) throw new NotFoundException(); const restored = await this.prisma.page.update({ where: { id }, data: { deletedAt: null, deletedBy: null }, }); this.logger.info({ pageId: id, userId: user.id }, 'audit: page restored from trash'); return this.pages.viewOf(restored); } /** Manual "purge single" (scope's third trash endpoint) — bypasses retention. */ async purgeNow(user: User, id: string): Promise { const page = await this.prisma.page.findFirst({ where: { id } }); if (!page || !page.deletedAt) throw new NotFoundException(); await this.purgePage(id); this.logger.info({ pageId: id, userId: user.id }, 'audit: page purged from trash'); } /** Scheduled job entry point (registered with SchedulerService, trash.module.ts). */ async purgeDuePages(): Promise { const retentionDays = await this.settings.get('trash.retentionDays'); const cutoff = new Date(this.clock.now().getTime() - retentionDays * MS_PER_DAY); const due = await this.prisma.page.findMany({ where: { deletedAt: { lte: cutoff } }, select: { id: true }, }); for (const { id } of due) { await this.purgePage(id); } } /** * Deletes state, content cache, files, and (once M3 exists) versions for * one page — the fixed set of things `#31`'s scope names. Version rows * are a placeholder: `page_versions` doesn't exist until M3 (#33–#42). */ private async purgePage(pageId: string): Promise { const page = await this.prisma.page.findUnique({ where: { id: pageId } }); if (!page) return; // already gone — e.g. a manual purge raced the job const attachments = await this.prisma.attachment.findMany({ where: { pageId } }); for (const attachment of attachments) { await this.storage.delete(attachment.pondId, attachment.id); await this.quotas.release(attachment.pondId, attachment.sizeBytes); } await this.prisma.attachment.deleteMany({ where: { pageId } }); await this.prisma.pageContentCache.deleteMany({ where: { pageId } }); await this.prisma.pageUpdate.deleteMany({ where: { pageId } }); await this.prisma.page.delete({ where: { id: pageId } }); this.logger.info({ pageId }, 'audit: page purged (retention)'); } }