Archived
资料文件真产出来了;E04/I01 补到验收线,顺手填了几个「墙拦住却不给路」的坑
**资料文件全链**(用户:审核资料这一步要真产出 word/excel/md/html,前端能渲、点资料名能开)
接手时这一步一个文件都产不出来 —— S3 剧本白纸黑字写着「file_ref 本 MVP 阶段可留空」。
文件是台账那一行的投影,不另存一份:
· 声明 `file:{titleColumn,bodyColumn,formatColumn}`,它同时是授权面(没声明就点不开)
· 现造真字节 —— docx/xlsx 是自己打的 OOXML 包(zip-write:store+CRC32,零依赖)
· 读回来渲 —— 解的就是造出来那份字节,渲得出来才证明文件是真的
· 件上 `data-file-of` → 工作区二级界面(Word 纸面/Excel 网格/渲好的 md/沙箱 html)+ 下载原件
放行落地前把每一份冻进 `sys.task_asset_file`(迁移 0029):归档件点开的必须是签字那一刻
那一份,而资料落在主数据上、试跑单不落正式表 —— 不存这一份,收工后一个都点不开。
工作成果里「产出的资料」自成一摞。Markdown 富文本渲染顺带销账(仍零依赖)。
**Harness**
· 画面按「这一拍办完之后」在办的那一步挑 —— 从前拿拍首的局面决定拍尾的事:
收掉 S4、发出 S5 的卡,而屏上还摆着 S4 的只读件,人无处可填(墙要配路第四次现形)
· 验收「重做」真能退回重做,且打回后台账没动就不许再提交(软硬两段,不锁死单子)
· 立监听的授权面是清单声明;发卡的授权面是调人预算,不是计划项
· 读工具同参重调有兜底;空转写身后还有行动就不废串;finish 的 doneWhen 按写完后的草稿算
**界面**
· 翻件与翻页各归各位(从前二选一,多卡多页时页翻不动);没得翻写「—」不写 0/0
· 工作成果详情能翻份;表宽超出能横滑(E04/I01 抄版式时把 overflow-x 抄丢了)
· 查看面灰底滚过一屏变白 —— 两条独立的路都治了(沙箱内不同色 / 弹性子项不长个)
· 测试台「取不到」不再当成「还活着」
**活动**
E04 关系覆盖、I01 品牌内容各自补到验收线:判据全读基座数、判据指向的东西摆上卡面、
样例满载且名字 ≤14 字;提交前要裁的判据搬进说明书(模型会压拍,压过之后只有说明书还在)。
验收:e2e i01 4/4、e04 3/3 全部 released,资料在办中渲得出、放行后存了档点得开;
门禁 typecheck/lint/depcruise/1628 测全绿。
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EEJX8Z7CHejvwmDQyPQuBX
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* 产出的资料文件**存档** —— 放行那一刻把它冻住。
|
||||
*
|
||||
* 在办期间文件是台账那一行的投影(现造,见 `file-compose.ts`):台账改一格,文件跟着变,
|
||||
* 两处永远一致 —— 那时这样最好。**收工之后不该再变**:归档件说的是「他签字那一刻
|
||||
* 交出去的是什么」,台账后来被下一单改了,归档的那份不能跟着改口。
|
||||
*
|
||||
* 还有一条更硬的理由:资料常落在**主数据**上(如 `content.asset` 品牌内容库),
|
||||
* 而试跑单放行时主数据只留在活动缓存、不落正式表(免得试跑往正式库里塞真资产)——
|
||||
* 不存这一份,试跑单一放行,人在工作成果里就什么都点不开了。
|
||||
*
|
||||
* 一单一行一份,重复放行覆盖(不留两份说法不一的档)。
|
||||
*/
|
||||
import { sql } from 'drizzle-orm';
|
||||
import type { TaskId } from '@contracts';
|
||||
import { composeFile, isFileFormat, type FileFormat } from './file-compose.ts';
|
||||
|
||||
/** 存档里的一份 —— 列表用(不带字节) */
|
||||
export type ArchivedAssetFile = {
|
||||
readonly id: string;
|
||||
readonly address: string;
|
||||
readonly key: Record<string, string>;
|
||||
readonly title: string;
|
||||
readonly format: FileFormat;
|
||||
readonly name: string;
|
||||
readonly size: number;
|
||||
readonly at: string;
|
||||
};
|
||||
|
||||
type Db = { execute(query: unknown): Promise<unknown> };
|
||||
|
||||
function rowsOf<T>(result: unknown): T[] {
|
||||
if (Array.isArray(result)) return result as T[];
|
||||
const rows = (result as { rows?: unknown }).rows;
|
||||
return Array.isArray(rows) ? (rows as T[]) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 主键 → 一个**稳定的**字符串(按列名排序后拼)。
|
||||
* 稳定是要害:存的时候与取的时候拼法不一样,那一行就永远对不上。
|
||||
*/
|
||||
export function keyText(key: Readonly<Record<string, unknown>>): string {
|
||||
return Object.keys(key)
|
||||
.sort()
|
||||
.map((k) => `${k}=${String(key[k] ?? '')}`)
|
||||
.join(',');
|
||||
}
|
||||
|
||||
/** 这一单已存档的资料(不含字节) */
|
||||
export async function listArchived(db: Db, taskId: TaskId): Promise<ArchivedAssetFile[]> {
|
||||
const rows = rowsOf<{
|
||||
id: string;
|
||||
address: string;
|
||||
row_key: unknown;
|
||||
title: string;
|
||||
format: string;
|
||||
name: string;
|
||||
size: number;
|
||||
created_at: unknown;
|
||||
}>(
|
||||
await db.execute(sql`
|
||||
select id, address, row_key, title, format, name, size, created_at
|
||||
from sys.task_asset_file where task_id = ${taskId} order by created_at, name`),
|
||||
);
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
address: r.address,
|
||||
key: (typeof r.row_key === 'object' && r.row_key !== null ? r.row_key : {}) as Record<string, string>,
|
||||
title: r.title,
|
||||
format: isFileFormat(r.format) ? r.format : 'md',
|
||||
name: r.name,
|
||||
size: Number(r.size ?? 0),
|
||||
at: r.created_at instanceof Date ? r.created_at.toISOString() : String(r.created_at),
|
||||
}));
|
||||
}
|
||||
|
||||
/** 取存档里的那一份字节 —— 按 (单, 地址, 主键) 认 */
|
||||
export async function readArchived(
|
||||
db: Db,
|
||||
taskId: TaskId,
|
||||
address: string,
|
||||
key: Readonly<Record<string, string>>,
|
||||
): Promise<{ bytes: Uint8Array; title: string; format: FileFormat; name: string } | undefined> {
|
||||
const rows = rowsOf<{ bytes: unknown; title: string; format: string; name: string }>(
|
||||
await db.execute(sql`
|
||||
select bytes, title, format, name from sys.task_asset_file
|
||||
where task_id = ${taskId} and address = ${address} and row_key = ${JSON.stringify(key)}::jsonb
|
||||
limit 1`),
|
||||
);
|
||||
const one = rows[0];
|
||||
if (one === undefined) return undefined;
|
||||
const raw = one.bytes;
|
||||
/*
|
||||
* **必须拷一份成独立的 Uint8Array** —— pg 取回来的是 Node `Buffer`,
|
||||
* 而 Buffer 挂在一块**共享的 64KB 内存池**上:`buf.slice()` 是视图不是拷贝,
|
||||
* `.buffer` 拿到的是整个池子。直接交出去,一份 3KB 的文件会变成 65536 字节
|
||||
* (后面全是别人的残渣)—— 实测下载三份资料,三份都是 65536。
|
||||
* `Uint8Array.from` 按元素拷,新数组的 buffer 恰好这么长。
|
||||
*/
|
||||
const bytes =
|
||||
raw instanceof Uint8Array
|
||||
? Uint8Array.from(raw)
|
||||
: typeof raw === 'string'
|
||||
? Uint8Array.from(Buffer.from(raw, 'hex'))
|
||||
: undefined;
|
||||
if (bytes === undefined) return undefined;
|
||||
return { bytes, title: one.title, format: isFileFormat(one.format) ? one.format : 'md', name: one.name };
|
||||
}
|
||||
|
||||
/**
|
||||
* 把这一单产出的资料**全部存一份**。放行落地之前调 —— 那时草稿还在、人的修订也已落回,
|
||||
* 存下来的正是他签字那一刻的样子。
|
||||
*
|
||||
* 返回存了几份;一份都没有(活动没声明 `file` 段、或一行都没写)时返回 0,不报错 ——
|
||||
* 大多数活动本来就不产资料文件。
|
||||
*/
|
||||
export async function archiveAssetFiles(input: {
|
||||
readonly db: Db;
|
||||
readonly taskId: TaskId;
|
||||
readonly ids: { next(): string };
|
||||
/** 活动清单声明的台账对象(带 `file` 段的才算) */
|
||||
readonly declarations: ReadonlyArray<{
|
||||
domain: string;
|
||||
object: string;
|
||||
fields: ReadonlyArray<{ name: string; primaryKey?: boolean }>;
|
||||
file?: { titleColumn: string; bodyColumn: string; formatColumn: string } | undefined;
|
||||
}>;
|
||||
/** 本单在该地址写下的行(正式版 + 本单草稿) */
|
||||
readonly rowsOfAddress: (address: { domain: string; object: string }) => Promise<Array<Record<string, unknown>>>;
|
||||
}): Promise<number> {
|
||||
let saved = 0;
|
||||
for (const declared of input.declarations) {
|
||||
const spec = declared.file;
|
||||
if (spec === undefined) continue;
|
||||
const keys = declared.fields.filter((f) => f.primaryKey === true).map((f) => f.name);
|
||||
if (keys.length === 0) continue;
|
||||
|
||||
const rows = await input.rowsOfAddress({ domain: declared.domain, object: declared.object });
|
||||
for (const row of rows) {
|
||||
const markdown = String(row[spec.bodyColumn] ?? '').trim();
|
||||
if (markdown === '') continue; // 没正文的行不是资料,别存一份空壳
|
||||
const key: Record<string, string> = {};
|
||||
for (const k of keys) key[k] = String(row[k] ?? '');
|
||||
if (Object.values(key).some((v) => v === '')) continue; // 主键不全的行认不出来,存了也取不回
|
||||
|
||||
const title = String(row[spec.titleColumn] ?? '').trim() || Object.values(key).join('-');
|
||||
const rawFormat = String(row[spec.formatColumn] ?? '').trim().toLowerCase();
|
||||
const format: FileFormat = isFileFormat(rawFormat) ? rawFormat : 'md';
|
||||
const composed = composeFile({ title, markdown, format });
|
||||
const address = `${declared.domain}.${declared.object}`;
|
||||
|
||||
await input.db.execute(sql`
|
||||
insert into sys.task_asset_file (id, task_id, address, row_key, title, format, name, bytes, size)
|
||||
values (${input.ids.next()}, ${input.taskId}, ${address}, ${JSON.stringify(key)}::jsonb,
|
||||
${title}, ${format}, ${composed.fileName}, ${Buffer.from(composed.bytes)}, ${composed.bytes.length})
|
||||
on conflict (task_id, address, row_key) do update set
|
||||
title = excluded.title, format = excluded.format, name = excluded.name,
|
||||
bytes = excluded.bytes, size = excluded.size, created_at = now()`);
|
||||
saved += 1;
|
||||
}
|
||||
}
|
||||
return saved;
|
||||
}
|
||||
Reference in New Issue
Block a user