Files
crosscode-platform/apps/website/backend/src/admin/previewLinks.ts
T
joshuaandClaude Opus 5.5 84c5b0c08e feat(website): the admin, with drafts, preview, publish and restore
Admin API and screens: dashboard and setup checklist, template and
style, pages and blocks, ministries, leadership, media library with
the storage limit, publish and history. Pending changes with who and
when, discard one, one Publish, restore any version (each version
keeps the working copy's rows, never sent to visitors). Preview on
the site's own addresses with a bar (widths, back, publish); shared
preview links for 7 days or until revoked. Roles checked, same-origin
changes only. Publish refreshes the public pages at once
(WEBSITE_CONTENT_CACHE_SECONDS). E2E on a production build: the
admin's flows and axe on every screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 12:30:29 +02:00

43 lines
2.3 KiB
TypeScript

/**
* Shared previews (WEBSITE-1.0-PLAN §4): a link for someone without an account (the pastor), that
* shows the working copy as it is when opened, for 7 days or until revoked. It can't edit or
* publish, and its pages are `noindex`. Only the token's hash is stored; the link is shown once.
*/
import type { Ctx } from '../context.js';
import { ApiError } from '../http.js';
import { audit } from '../lib/audit.js';
import { hashToken, randomToken } from '../lib/crypto.js';
export const PREVIEW_DAYS = 7;
export async function createPreviewLink(ctx: Ctx, by: { id: string; role: string }) {
const token = randomToken();
const now = ctx.now();
const row = await ctx.db.previewLink.create({ data: { tokenHash: hashToken(token), createdBy: by.id, createdAt: now, expiresAt: new Date(now.getTime() + PREVIEW_DAYS * 864e5) } });
await audit(ctx, { actorId: by.id, actorRole: by.role, action: 'preview_link_created', targetType: 'preview_link', targetId: row.id });
return { id: row.id, url: `${ctx.config.publicUrl}/preview/${token}`, expiresAt: row.expiresAt.toISOString() };
}
export async function listPreviewLinks(ctx: Ctx) {
const now = ctx.now();
const rows = await ctx.db.previewLink.findMany({ orderBy: { createdAt: 'desc' } });
const names = new Map((await ctx.db.websiteUser.findMany()).map((u) => [u.id, u.name]));
return rows.map((r) => ({
id: r.id, createdAt: r.createdAt.toISOString(), expiresAt: r.expiresAt.toISOString(), by: names.get(r.createdBy) ?? null,
active: !r.revokedAt && r.expiresAt > now, revokedAt: r.revokedAt?.toISOString() ?? null,
}));
}
export async function revokePreviewLink(ctx: Ctx, id: string, by: { id: string; role: string }) {
const row = await ctx.db.previewLink.findUnique({ where: { id } });
if (!row) throw new ApiError(404, 'NOT_FOUND', 'That preview link doesn\'t exist.');
await ctx.db.previewLink.update({ where: { id }, data: { revokedAt: ctx.now() } });
await audit(ctx, { actorId: by.id, actorRole: by.role, action: 'preview_link_revoked', targetType: 'preview_link', targetId: id });
}
export async function previewTokenValid(ctx: Ctx, token: string | undefined): Promise<boolean> {
if (!token || token.length > 200) return false;
const row = await ctx.db.previewLink.findUnique({ where: { tokenHash: hashToken(token) } });
return !!row && !row.revokedAt && row.expiresAt > ctx.now();
}