失效映射原先长在 /api/cms/revalidate 的路由体里,所以它只能被外部 HTTP 调用触发。 实测后果:后台四条变更链一个都不刷缓存,8 个页面注释里那句 「CMS 发布/更新时通过 /api/cms/revalidate 主动刷新」是假的 —— s-maxage=3600 下 最长 1 小时前台仍端出已删或过期内容,且没有任何门禁会红。 - 新增 src/lib/cms/revalidation.ts:modelCode→paths 推导做成纯函数 (pathsForModel / tagsForModel / planRevalidation),副作用收在 revalidateContent;同 §5.30「安全抽象必须可导入」。 - 路由改为消费同一份推导(响应契约与去重语义逐字保持,16 套件既有断言未动)。 - items 的 POST/PUT/DELETE、workflow 四个动作、zones 的写与删都接上失效; 判据按 status(草稿不进前台读取路径),slug 变更时旧地址一并刷。 - revalidateContent 是尽力而为:逐条 try + [revalidation-failed] 日志 + failed 回报, 因为数据已落库,刷缓存失败不该把成功的写入连带变成 500; applyRevalidation 仍严格冒泡,保持 /api/cms/revalidate 的 500 语义。 证据:新增 10 例(纯映射 12 例 + 调用点 6 例)。定向 16 套件 193 例 EXIT=0; 负对照把 workflow 的调用点改成注释 ⇒ MUTANT_EXIT=1(4 例转红),还原后 EXIT=0。 lint 定向 0 error。
109 lines
3.4 KiB
TypeScript
109 lines
3.4 KiB
TypeScript
import { NextRequest } from 'next/server';
|
|
import { prisma } from '@/lib/db';
|
|
import { requirePermission } from '@/lib/permissions';
|
|
import { authenticateRequest } from '@/lib/auth';
|
|
import {
|
|
submitForReview,
|
|
approve,
|
|
reject,
|
|
archive,
|
|
type WorkflowAction,
|
|
} from '@/lib/cms/workflow';
|
|
import {
|
|
success,
|
|
notFound,
|
|
unauthorized,
|
|
validationError,
|
|
internalError,
|
|
forbidden,
|
|
} from '@/lib/api-response';
|
|
import { withCrypto } from '@/lib/api-crypto';
|
|
import { parseCmsData } from '@/lib/cms/validate-content-data';
|
|
import { revalidateContent } from '@/lib/cms/revalidation';
|
|
|
|
// 与 items/route.ts 同源:脏 data 列不得让这个已成功的审批动作在返回阶段变成 500(B-4)。
|
|
function parseItem(item: { data: string; [key: string]: unknown }) {
|
|
return { ...item, data: parseCmsData(item.data) };
|
|
}
|
|
|
|
interface WorkflowBody {
|
|
action: WorkflowAction;
|
|
reason?: string;
|
|
}
|
|
|
|
const ACTION_PERMISSION: Record<WorkflowAction, 'update' | 'publish'> = {
|
|
submit: 'update',
|
|
approve: 'publish',
|
|
reject: 'publish',
|
|
archive: 'publish',
|
|
};
|
|
|
|
export const POST = withCrypto(async (
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) => {
|
|
const { id } = await params;
|
|
|
|
try {
|
|
const body = (await request.json()) as WorkflowBody;
|
|
const { action, reason } = body;
|
|
|
|
if (!action) {
|
|
return validationError('缺少 action 参数');
|
|
}
|
|
|
|
if (!['submit', 'approve', 'reject', 'archive'].includes(action)) {
|
|
return validationError(`非法的 action: ${action}`);
|
|
}
|
|
|
|
// Q-8:认证先于存在性探针,避免匿名枚举内容 ID。
|
|
if (!authenticateRequest(request)) return unauthorized();
|
|
|
|
const existing = await prisma.contentItem.findUnique({ where: { id } });
|
|
if (!existing) return notFound('内容不存在');
|
|
|
|
const requiredAction = ACTION_PERMISSION[action];
|
|
const permission = await requirePermission(request, existing.modelCode, requiredAction);
|
|
if ('response' in permission) return permission.response;
|
|
|
|
const user = { userId: permission.user.userId, username: permission.user.username };
|
|
|
|
switch (action) {
|
|
case 'submit':
|
|
await submitForReview(id, user);
|
|
break;
|
|
case 'approve':
|
|
await approve(id, user);
|
|
break;
|
|
case 'reject':
|
|
await reject(id, user, reason);
|
|
break;
|
|
case 'archive':
|
|
await archive(id, user);
|
|
break;
|
|
}
|
|
|
|
// DEC-N5:发布链以前什么都不刷 ——「后台发布后前台最长 1 小时不可见」就是这条缺口。
|
|
// approve 让内容首次出现,reject/archive 让它消失,submit 不改可见性但也一并失效:
|
|
// 多余的一次 revalidatePath 是幂等的,漏一次则是用户可见的旧内容,代价不对称。
|
|
const updated = await prisma.contentItem.findUniqueOrThrow({ where: { id } });
|
|
revalidateContent(existing.modelCode, updated.slug);
|
|
|
|
return success(parseItem(updated));
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
if (error.message.includes('内容不存在')) {
|
|
return notFound(error.message);
|
|
}
|
|
if (error.message.includes('当前状态') || error.message.includes('不允许')) {
|
|
return validationError(error.message);
|
|
}
|
|
if (error.message.includes('无权限')) {
|
|
return forbidden(error.message);
|
|
}
|
|
}
|
|
console.error('Workflow error:', error);
|
|
return internalError();
|
|
}
|
|
});
|