feat(review): 支持多候选稿验收轮次
- 支持每轮提交 1–5 个候选稿并按指定稿验收 - 保留历史轮次只读并兼容单候选稿版本接口 - 同步 SQLite/PostgreSQL schema、迁移验证、测试与项目文档
This commit is contained in:
@@ -1,23 +1,90 @@
|
||||
import sharp from 'sharp';
|
||||
import type { Note, NoteDetail, ImageWithAnnotations, ReviewStatus } from '../../shared/types.js';
|
||||
import type { ImageWithAnnotations, Note, NoteDetail, ReviewStatus } from '../../shared/types.js';
|
||||
import { notesRepository } from '../repositories/notesRepository.js';
|
||||
import { imagesRepository } from '../repositories/imagesRepository.js';
|
||||
import { annotationsRepository } from '../repositories/annotationsRepository.js';
|
||||
import { database, withTransaction } from '../database.js';
|
||||
import { database, databaseDialect, withTransaction, type QueryContext } from '../database.js';
|
||||
import { storeUploadedFile } from '../storage.js';
|
||||
import { recalculateCollectionStatus } from './collectionsService.js';
|
||||
|
||||
export interface UploadedFile { filename: string; originalname?: string; mimetype?: string; path: string }
|
||||
export interface UploadCandidate { candidate_name: string; title: string; description: string; tags: string[]; files: UploadedFile[] }
|
||||
export interface UrlCandidate { candidate_name: string; title: string; description: string; tags: string[]; images: string[] }
|
||||
|
||||
type StoredImage = { url: string; width: number; height: number; storageProvider: 'local' | 'tencent_cos' | 'external'; storageKey: string };
|
||||
type PreparedCandidate = { candidate_name: string; title: string; description: string; tags: string[]; images: StoredImage[] };
|
||||
|
||||
async function readImageSize(filePath: string): Promise<{ width: number; height: number }> {
|
||||
try { const meta = await sharp(filePath).metadata(); return { width: meta.width ?? 0, height: meta.height ?? 0 }; }
|
||||
catch { return { width: 0, height: 0 }; }
|
||||
}
|
||||
|
||||
async function prepareFiles(files: UploadedFile[]) {
|
||||
async function prepareFiles(files: UploadedFile[]): Promise<StoredImage[]> {
|
||||
return Promise.all(files.map(async (file) => ({ ...(await readImageSize(file.path)), ...(await storeUploadedFile(file)) })));
|
||||
}
|
||||
|
||||
function externalImages(images: string[]): StoredImage[] {
|
||||
return images.map((url) => ({ url, width: 0, height: 0, storageProvider: 'external', storageKey: '' }));
|
||||
}
|
||||
|
||||
async function createRoundInTransaction(
|
||||
tx: QueryContext,
|
||||
noteId: number,
|
||||
collectionId: number,
|
||||
candidates: PreparedCandidate[],
|
||||
createdBy: number | undefined,
|
||||
fromStatus: ReviewStatus,
|
||||
): Promise<{ roundId: number; roundNumber: number; firstVersion: number }> {
|
||||
const note = await tx.one<{ active_round_id: number | null }>('SELECT active_round_id FROM notes WHERE id = ?' + (databaseDialect === 'postgres' ? ' FOR NO KEY UPDATE' : ''), [noteId]);
|
||||
if (note?.active_round_id) {
|
||||
await tx.execute("UPDATE work_versions SET candidate_status = 'not_selected', review_status = 'draft' WHERE review_round_id = ? AND candidate_status = 'pending'", [note.active_round_id]);
|
||||
await tx.execute("UPDATE review_rounds SET status = 'completed', completed_at = COALESCE(completed_at, ?) WHERE id = ? AND status = 'reviewing'", [new Date().toISOString(), note.active_round_id]);
|
||||
}
|
||||
const maxima = await tx.one<{ max_version: number | string | null; max_round: number | string | null }>(
|
||||
`SELECT (SELECT MAX(version_number) FROM work_versions WHERE note_id = ?) AS max_version,
|
||||
(SELECT MAX(round_number) FROM review_rounds WHERE note_id = ?) AS max_round`,
|
||||
[noteId, noteId],
|
||||
);
|
||||
const firstVersion = Number(maxima?.max_version ?? 0) + 1;
|
||||
const roundNumber = Number(maxima?.max_round ?? 0) + 1;
|
||||
const roundId = await tx.insertId(
|
||||
"INSERT INTO review_rounds (note_id, round_number, status, created_by) VALUES (?, ?, 'reviewing', ?)",
|
||||
[noteId, roundNumber, createdBy ?? null],
|
||||
);
|
||||
|
||||
for (let index = 0; index < candidates.length; index += 1) {
|
||||
const candidate = candidates[index];
|
||||
const versionNumber = firstVersion + index;
|
||||
await tx.execute(
|
||||
"INSERT INTO work_versions (note_id, version_number, title, description, tags, review_status, review_round_id, candidate_name, candidate_status, created_by) VALUES (?, ?, ?, ?, ?, 'pending', ?, ?, 'pending', ?)",
|
||||
[noteId, versionNumber, candidate.title, candidate.description, JSON.stringify(candidate.tags), roundId, candidate.candidate_name, createdBy ?? null],
|
||||
);
|
||||
await imagesRepository.createMany(noteId, candidate.images, versionNumber, tx);
|
||||
await tx.execute(
|
||||
"INSERT INTO review_events (note_id, version_number, event_type, from_status, to_status, actor_name, actor_role) VALUES (?, ?, 'submitted', ?, 'pending', ?, 'operator')",
|
||||
[noteId, versionNumber, fromStatus, '工作台'],
|
||||
);
|
||||
}
|
||||
|
||||
const first = candidates[0];
|
||||
await tx.execute(
|
||||
"UPDATE notes SET title = ?, description = ?, tags = ?, version_number = ?, active_round_id = ?, approved_version_number = NULL, review_status = 'pending' WHERE id = ?",
|
||||
[first.title, first.description, JSON.stringify(first.tags), firstVersion, roundId, noteId],
|
||||
);
|
||||
await recalculateCollectionStatus(collectionId, tx);
|
||||
return { roundId, roundNumber, firstVersion };
|
||||
}
|
||||
|
||||
async function prepareUploadCandidates(candidates: UploadCandidate[]): Promise<PreparedCandidate[]> {
|
||||
return Promise.all(candidates.map(async (candidate) => ({
|
||||
candidate_name: candidate.candidate_name,
|
||||
title: candidate.title,
|
||||
description: candidate.description,
|
||||
tags: candidate.tags,
|
||||
images: await prepareFiles(candidate.files),
|
||||
})));
|
||||
}
|
||||
|
||||
export const notesService = {
|
||||
async list(query: { sort?: string; order?: string; q?: string; collectionId?: number; status?: ReviewStatus; tag?: string; projectId?: number; groupId?: number; externalId?: string }) {
|
||||
return notesRepository.list({ sort: query.sort === 'annotations' ? 'annotations' : 'created_at', order: query.order === 'asc' ? 'asc' : 'desc', q: query.q, collectionId: query.collectionId, status: query.status, tag: query.tag, projectId: query.projectId, groupId: query.groupId, externalId: query.externalId });
|
||||
@@ -34,13 +101,16 @@ export const notesService = {
|
||||
const images = await imagesRepository.listByNote(id, note.version_number);
|
||||
const workContext = await database.one<{ project_id: number; project_name: string; slug: string; collection_id: number; collection_name: string; collection_status: NoteDetail['collection']['status'] }>(`SELECT p.id AS project_id, p.name AS project_name, p.slug, c.id AS collection_id, c.name AS collection_name, c.status AS collection_status FROM notes n JOIN collections c ON c.id = n.collection_id JOIN projects p ON p.id = c.project_id WHERE n.id = ?`, [id]);
|
||||
if (!workContext) return null;
|
||||
const versionRows = await database.all<Array<Omit<NoteDetail['versions'][number], 'tags'> & { tags: string }>[number]>('SELECT version_number, title, description, tags, review_status, created_at FROM work_versions WHERE note_id = ? ORDER BY version_number DESC', [id]);
|
||||
const versionRows = await database.all<Array<Omit<NoteDetail['versions'][number], 'tags'> & { tags: string }>[number]>(`SELECT v.version_number, v.title, v.description, v.tags, v.review_status, v.review_round_id,
|
||||
v.candidate_name, v.candidate_status, v.created_at, r.round_number, r.status AS round_status, r.selected_version_number
|
||||
FROM work_versions v JOIN review_rounds r ON r.id = v.review_round_id
|
||||
WHERE v.note_id = ? ORDER BY r.round_number DESC, v.version_number ASC`, [id]);
|
||||
const result: NoteDetail = {
|
||||
...note,
|
||||
images: [] as ImageWithAnnotations[],
|
||||
text_annotations: await database.all<NoteDetail['text_annotations'][number]>('SELECT id, note_id, version_number, target, content, author_name, status, created_at FROM text_annotations WHERE note_id = ? AND version_number = ? ORDER BY id ASC', [id, note.version_number]),
|
||||
comments: await database.all<NoteDetail['comments'][number]>('SELECT * FROM work_comments WHERE note_id = ? ORDER BY id ASC', [id]),
|
||||
versions: versionRows.map((item) => ({ ...item, version_number: Number(item.version_number), tags: JSON.parse(item.tags || '[]') as string[] })),
|
||||
versions: versionRows.map((item) => ({ ...item, version_number: Number(item.version_number), review_round_id: Number(item.review_round_id), round_number: Number(item.round_number), selected_version_number: item.selected_version_number == null ? null : Number(item.selected_version_number), tags: JSON.parse(item.tags || '[]') as string[] })),
|
||||
review_events: await database.all<NoteDetail['review_events'][number]>('SELECT * FROM review_events WHERE note_id = ? ORDER BY id DESC', [id]),
|
||||
project: { id: Number(workContext.project_id), name: workContext.project_name, slug: workContext.slug },
|
||||
collection: { id: Number(workContext.collection_id), name: workContext.collection_name, status: workContext.collection_status },
|
||||
@@ -52,10 +122,8 @@ export const notesService = {
|
||||
async create(title: string, description: string, files: UploadedFile[], collectionId: number, tags: string[], externalId: string | null = null): Promise<Note> {
|
||||
const prepared = await prepareFiles(files);
|
||||
const noteId = await withTransaction(async (tx) => {
|
||||
const id = await tx.insertId('INSERT INTO notes (external_id, title, description, collection_id, tags, review_status) VALUES (?, ?, ?, ?, ?, ?)', [externalId, title, description, collectionId, JSON.stringify(tags), 'pending']);
|
||||
await imagesRepository.createMany(id, prepared, 1, tx);
|
||||
await tx.execute("INSERT INTO work_versions (note_id, version_number, title, description, tags, review_status) VALUES (?, 1, ?, ?, ?, 'pending')", [id, title, description, JSON.stringify(tags)]);
|
||||
await recalculateCollectionStatus(collectionId, tx);
|
||||
const id = await tx.insertId("INSERT INTO notes (external_id, title, description, collection_id, tags, review_status) VALUES (?, ?, ?, ?, ?, 'pending')", [externalId, title, description, collectionId, JSON.stringify(tags)]);
|
||||
await createRoundInTransaction(tx, id, collectionId, [{ candidate_name: '方案 A', title, description, tags, images: prepared }], undefined, 'draft');
|
||||
return id;
|
||||
});
|
||||
return (await notesRepository.findById(noteId))!;
|
||||
@@ -63,10 +131,8 @@ export const notesService = {
|
||||
|
||||
async createFromUrls(title: string, description: string, imageUrls: string[], collectionId: number, tags: string[], externalId: string | null = null): Promise<Note> {
|
||||
const noteId = await withTransaction(async (tx) => {
|
||||
const id = await tx.insertId('INSERT INTO notes (external_id, title, description, collection_id, tags, review_status) VALUES (?, ?, ?, ?, ?, ?)', [externalId, title, description, collectionId, JSON.stringify(tags), 'pending']);
|
||||
await imagesRepository.createMany(id, imageUrls.map((url) => ({ url, width: 0, height: 0, storageProvider: 'external' as const, storageKey: '' })), 1, tx);
|
||||
await tx.execute("INSERT INTO work_versions (note_id, version_number, title, description, tags, review_status) VALUES (?, 1, ?, ?, ?, 'pending')", [id, title, description, JSON.stringify(tags)]);
|
||||
await recalculateCollectionStatus(collectionId, tx);
|
||||
const id = await tx.insertId("INSERT INTO notes (external_id, title, description, collection_id, tags, review_status) VALUES (?, ?, ?, ?, ?, 'pending')", [externalId, title, description, collectionId, JSON.stringify(tags)]);
|
||||
await createRoundInTransaction(tx, id, collectionId, [{ candidate_name: '方案 A', title, description, tags, images: externalImages(imageUrls) }], undefined, 'draft');
|
||||
return id;
|
||||
});
|
||||
return (await notesRepository.findById(noteId))!;
|
||||
@@ -76,35 +142,30 @@ export const notesService = {
|
||||
return notesRepository.findByExternalId(collectionId, externalId);
|
||||
},
|
||||
|
||||
async createVersion(id: number, title: string, description: string, files: UploadedFile[], tags: string[], createdBy?: number): Promise<Note> {
|
||||
async createReviewRound(id: number, candidates: UploadCandidate[], createdBy?: number): Promise<Note> {
|
||||
const current = await notesRepository.findById(id);
|
||||
if (!current) throw new Error('作品不存在');
|
||||
const nextVersion = current.version_number + 1;
|
||||
const prepared = await prepareFiles(files);
|
||||
await withTransaction(async (tx) => {
|
||||
await tx.execute("UPDATE notes SET title = ?, description = ?, tags = ?, version_number = ?, review_status = 'pending' WHERE id = ?", [title, description, JSON.stringify(tags), nextVersion, id]);
|
||||
await tx.execute("INSERT INTO work_versions (note_id, version_number, title, description, tags, review_status, created_by) VALUES (?, ?, ?, ?, ?, 'pending', ?)", [id, nextVersion, title, description, JSON.stringify(tags), createdBy ?? null]);
|
||||
await imagesRepository.createMany(id, prepared, nextVersion, tx);
|
||||
await tx.execute("INSERT INTO review_events (note_id, version_number, event_type, from_status, to_status, actor_name, actor_role) VALUES (?, ?, 'submitted', ?, 'pending', ?, 'operator')", [id, nextVersion, current.review_status, '工作台']);
|
||||
await recalculateCollectionStatus(current.collection_id, tx);
|
||||
});
|
||||
const prepared = await prepareUploadCandidates(candidates);
|
||||
await withTransaction((tx) => createRoundInTransaction(tx, id, current.collection_id, prepared, createdBy, current.review_status));
|
||||
return (await notesRepository.findById(id))!;
|
||||
},
|
||||
|
||||
async createVersionFromUrls(id: number, title: string, description: string, imageUrls: string[], tags: string[], createdBy?: number): Promise<Note> {
|
||||
async createReviewRoundFromUrls(id: number, candidates: UrlCandidate[], createdBy?: number): Promise<Note> {
|
||||
const current = await notesRepository.findById(id);
|
||||
if (!current) throw new Error('作品不存在');
|
||||
const nextVersion = current.version_number + 1;
|
||||
await withTransaction(async (tx) => {
|
||||
await tx.execute("UPDATE notes SET title = ?, description = ?, tags = ?, version_number = ?, review_status = 'pending' WHERE id = ?", [title, description, JSON.stringify(tags), nextVersion, id]);
|
||||
await tx.execute("INSERT INTO work_versions (note_id, version_number, title, description, tags, review_status, created_by) VALUES (?, ?, ?, ?, ?, 'pending', ?)", [id, nextVersion, title, description, JSON.stringify(tags), createdBy ?? null]);
|
||||
await imagesRepository.createMany(id, imageUrls.map((url) => ({ url, width: 0, height: 0, storageProvider: 'external' as const, storageKey: '' })), nextVersion, tx);
|
||||
await tx.execute("INSERT INTO review_events (note_id, version_number, event_type, from_status, to_status, actor_name, actor_role) VALUES (?, ?, 'submitted', ?, 'pending', ?, 'operator')", [id, nextVersion, current.review_status, '工作台']);
|
||||
await recalculateCollectionStatus(current.collection_id, tx);
|
||||
});
|
||||
const prepared = candidates.map((candidate) => ({ ...candidate, images: externalImages(candidate.images) }));
|
||||
await withTransaction((tx) => createRoundInTransaction(tx, id, current.collection_id, prepared, createdBy, current.review_status));
|
||||
return (await notesRepository.findById(id))!;
|
||||
},
|
||||
|
||||
async createVersion(id: number, title: string, description: string, files: UploadedFile[], tags: string[], createdBy?: number): Promise<Note> {
|
||||
return this.createReviewRound(id, [{ candidate_name: '方案 A', title, description, tags, files }], createdBy);
|
||||
},
|
||||
|
||||
async createVersionFromUrls(id: number, title: string, description: string, imageUrls: string[], tags: string[], createdBy?: number): Promise<Note> {
|
||||
return this.createReviewRoundFromUrls(id, [{ candidate_name: '方案 A', title, description, tags, images: imageUrls }], createdBy);
|
||||
},
|
||||
|
||||
async remove(id: number) {
|
||||
return withTransaction(async (tx) => {
|
||||
const note = await tx.one<{ collection_id: number }>('SELECT collection_id FROM notes WHERE id = ?', [id]);
|
||||
@@ -114,12 +175,17 @@ export const notesService = {
|
||||
return removed;
|
||||
});
|
||||
},
|
||||
|
||||
async setStatus(id: number, status: ReviewStatus) {
|
||||
return withTransaction(async (tx) => {
|
||||
const note = await tx.one<{ collection_id: number; version_number: number }>('SELECT collection_id, version_number FROM notes WHERE id = ?', [id]);
|
||||
const note = await tx.one<{ collection_id: number; active_round_id: number | null }>('SELECT collection_id, active_round_id FROM notes WHERE id = ?', [id]);
|
||||
if (!note) return false;
|
||||
await tx.execute('UPDATE notes SET review_status = ? WHERE id = ?', [status, id]);
|
||||
await tx.execute('UPDATE work_versions SET review_status = ? WHERE note_id = ? AND version_number = ?', [status, id, note.version_number]);
|
||||
if (note.active_round_id) {
|
||||
const candidateStatus = status === 'draft' ? 'draft' : 'pending';
|
||||
await tx.execute("UPDATE work_versions SET review_status = ?, candidate_status = ? WHERE review_round_id = ? AND candidate_status NOT IN ('selected', 'not_selected')", [status, candidateStatus, note.active_round_id]);
|
||||
await tx.execute('UPDATE review_rounds SET status = ?, selected_version_number = NULL, completed_at = NULL WHERE id = ?', [status === 'draft' ? 'draft' : 'reviewing', note.active_round_id]);
|
||||
}
|
||||
await recalculateCollectionStatus(Number(note.collection_id), tx);
|
||||
return true;
|
||||
});
|
||||
|
||||
58
api/services/reviewService.ts
Normal file
58
api/services/reviewService.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import type { ReviewStatus } from '../../shared/types.js';
|
||||
import { databaseDialect, withTransaction, type QueryContext } from '../database.js';
|
||||
import { recalculateCollectionStatus } from './collectionsService.js';
|
||||
|
||||
export class ReviewDecisionError extends Error {
|
||||
constructor(public statusCode: number, message: string) { super(message); }
|
||||
}
|
||||
|
||||
export interface CandidateDecisionInput {
|
||||
noteId: number;
|
||||
versionNumber: number;
|
||||
projectId: number;
|
||||
decision: 'approved' | 'changes_requested';
|
||||
reason: string;
|
||||
actorName: string;
|
||||
actorRole: 'client';
|
||||
}
|
||||
|
||||
export async function decideCandidateInTransaction(tx: QueryContext, input: CandidateDecisionInput) {
|
||||
const candidate = await tx.one<{
|
||||
review_round_id: number; candidate_status: string; review_status: ReviewStatus;
|
||||
collection_id: number; active_round_id: number | null; round_status: string;
|
||||
title: string; description: string; tags: string;
|
||||
}>(`SELECT v.review_round_id,v.candidate_status,v.review_status,v.title,v.description,v.tags,
|
||||
n.collection_id,n.active_round_id,r.status AS round_status
|
||||
FROM work_versions v JOIN notes n ON n.id=v.note_id JOIN collections c ON c.id=n.collection_id
|
||||
JOIN review_rounds r ON r.id=v.review_round_id
|
||||
WHERE v.note_id=? AND v.version_number=? AND c.project_id=?${databaseDialect === 'postgres' ? ' FOR NO KEY UPDATE' : ''}`,
|
||||
[input.noteId, input.versionNumber, input.projectId]);
|
||||
if (!candidate) throw new ReviewDecisionError(404, '候选稿不存在');
|
||||
if (Number(candidate.active_round_id) !== Number(candidate.review_round_id) || candidate.round_status !== 'reviewing') {
|
||||
throw new ReviewDecisionError(409, '历史验收轮次为只读状态');
|
||||
}
|
||||
if (!['pending', 'changes_requested'].includes(candidate.candidate_status)) {
|
||||
throw new ReviewDecisionError(409, '该候选稿当前不能重复验收');
|
||||
}
|
||||
|
||||
if (input.decision === 'approved') {
|
||||
await tx.execute("UPDATE work_versions SET candidate_status='not_selected', review_status='draft' WHERE review_round_id=? AND version_number!=?", [candidate.review_round_id, input.versionNumber]);
|
||||
await tx.execute("UPDATE work_versions SET candidate_status='selected', review_status='approved' WHERE note_id=? AND version_number=?", [input.noteId, input.versionNumber]);
|
||||
await tx.execute("UPDATE review_rounds SET status='completed', selected_version_number=?, completed_at=? WHERE id=?", [input.versionNumber, new Date().toISOString(), candidate.review_round_id]);
|
||||
await tx.execute("UPDATE notes SET title=?,description=?,tags=?,version_number=?,approved_version_number=?,review_status='approved' WHERE id=?", [candidate.title, candidate.description, candidate.tags, input.versionNumber, input.versionNumber, input.noteId]);
|
||||
} else {
|
||||
await tx.execute("UPDATE work_versions SET candidate_status='changes_requested', review_status='changes_requested' WHERE note_id=? AND version_number=?", [input.noteId, input.versionNumber]);
|
||||
const remaining = await tx.one<{ count: number | string }>("SELECT COUNT(*) AS count FROM work_versions WHERE review_round_id=? AND candidate_status='pending'", [candidate.review_round_id]);
|
||||
const workStatus: ReviewStatus = Number(remaining?.count ?? 0) > 0 ? 'pending' : 'changes_requested';
|
||||
await tx.execute('UPDATE notes SET title=?,description=?,tags=?,version_number=?,review_status=? WHERE id=?', [candidate.title, candidate.description, candidate.tags, input.versionNumber, workStatus, input.noteId]);
|
||||
}
|
||||
|
||||
await tx.execute('INSERT INTO review_events (note_id,version_number,event_type,from_status,to_status,reason,actor_name,actor_role) VALUES (?,?,?,?,?,?,?,?)', [input.noteId, input.versionNumber, input.decision, candidate.review_status, input.decision, input.reason, input.actorName, input.actorRole]);
|
||||
if (input.reason) await tx.execute("INSERT INTO work_comments (note_id,content,author_name,author_role) VALUES (?,?,?,'client')", [input.noteId, input.reason, input.actorName]);
|
||||
await recalculateCollectionStatus(Number(candidate.collection_id), tx);
|
||||
return { success: true as const, status: input.decision, version_number: input.versionNumber };
|
||||
}
|
||||
|
||||
export async function decideCandidate(input: CandidateDecisionInput) {
|
||||
return withTransaction((tx) => decideCandidateInTransaction(tx, input));
|
||||
}
|
||||
Reference in New Issue
Block a user