技术环境:退货与退款模块 — OrderFlow 平台
Brownfield 项目。 现有技术栈是基线。新代码必须融入 已建立的模式。下方未列出的选择,应遵循现有代码库 — 没有正当理由不得引入新模式。
现有技术栈(必须保留)
| 层级 | 当前技术 | 版本 | 说明 |
|---|---|---|---|
| 语言 | TypeScript | 5.x | 严格模式。不要引入 JavaScript 文件。 |
| 运行时 | Node.js | 20.x LTS | |
| API 框架 | Express | 4.x | 所有现有服务都使用 Express。不要引入 Fastify 或 Koa。 |
| 数据库 | PostgreSQL | 15 | 通过 pg 和 node-postgres 使用。无 ORM — 使用带类型 query helper 的原始 SQL。 |
| 基础设施 | AWS ECS Fargate | — | 服务以 Docker container 部署。所有 infra 使用 CDK。 |
| 消息总线 | Amazon SQS | — | notification-service 用于异步电子邮件派发。 |
| 认证 | AWS Cognito | — | JWT token 在 API Gateway 校验。不要构建新的 auth layer。 |
| 包管理器 | npm | 10.x | 不要引入 yarn 或 pnpm。 |
| 测试框架 | Jest | 29.x | 配合 ts-jest。所有测试与源码并列放在 __tests__/ 中。 |
| Linter / formatter | ESLint + Prettier | — | 配置文件位于 repo root。不要修改它们。 |
要新增的内容(本模块新增)
- 一个新的
returns-service,遵循与order-service相同的结构 - 新 PostgreSQL 表:
return_requests、return_items、return_status_history - 面向客户退货表单和运营仪表板的新 React 组件
- 这些新增内容不得修改现有表或服务契约
保持不变的内容
order-service、payment-service、notification-service— 不要修改这些服务- 现有 PostgreSQL 表 — 仅允许增量 migration(新表、新表上的新列)
notification-serviceAPI 契约 — 按文档调用它,不要扩展它- 现有 CDK stack — 为
returns-service添加新 stack,不要编辑现有 stack - 前端设计系统组件 — 使用现有组件,不要创建替代品
要移除 / 不要引入的内容
| 禁止项 | 原因 | 替代方案 |
|---|---|---|
| ORM(TypeORM、Prisma、Sequelize) | 现有代码库使用带类型 helper 的原始 SQL。引入 ORM 会造成不一致。 | node-postgres 与带类型 query function,匹配现有模式 |
| Axios | 项目使用原生 fetch(Node 20 内置)。 | fetch |
| 任何新的 CSS framework | 现有前端使用 Tailwind CSS。 | Tailwind CSS、现有设计系统组件 |
| 新的状态管理库 | 现有前端使用 React Context + useReducer。 | React Context + useReducer |
| 新的 test runner(Vitest、Mocha) | 项目全程使用 Jest。 | Jest |
| 独立 auth service 或 middleware | Auth 通过 Cognito JWT 在 API Gateway 处理。 | 与其他服务一致,校验 Authorization header 中传入的 JWT |
安全基础
- 认证:Cognito JWT 在 API Gateway 校验。服务接收
x-user-id和x-user-roleheader — 信任这些 header,不要在服务内重新校验 JWT - 授权:运营仪表板 endpoint 要求
role === 'operations'— 检查这个 header - 输入校验:处理前使用 Zod schema 校验所有 request body
- PII:退货请求包含客户姓名和地址 — 不要记录这些字段
- Secret:数据库凭据和服务 URL 通过 AWS Secrets Manager 提供,与现有服务一致
示例代码模式
遵循现有代码库中的这些模式。不要发明替代方案。
服务 endpoint(Express route handler):
import { Router, Request, Response } from 'express';
import { z } from 'zod';
import { createReturnRequest } from '../domain/returns';
import { AppError } from '../errors';
const router = Router();
const CreateReturnSchema = z.object({
orderId: z.string().uuid(),
items: z.array(z.object({ orderItemId: z.string().uuid(), reason: z.string().min(1) })).min(1),
});
router.post('/returns', async (req: Request, res: Response) => {
const parsed = CreateReturnSchema.safeParse(req.body);
if (!parsed.success) {
return res.status(400).json({ error: 'VALIDATION_ERROR', details: parsed.error.flatten() });
}
try {
const result = await createReturnRequest(parsed.data, req.headers['x-user-id'] as string);
return res.status(201).json(result);
} catch (err) {
if (err instanceof AppError) {
return res.status(err.statusCode).json({ error: err.code, message: err.message });
}
throw err;
}
});
export default router;
数据库 query function:
import { pool } from '../db/pool';
export interface ReturnRequest {
id: string;
orderId: string;
customerId: string;
status: 'submitted' | 'approved' | 'rejected' | 'refunded';
createdAt: Date;
}
export async function getReturnRequestById(id: string): Promise<ReturnRequest | null> {
const { rows } = await pool.query<ReturnRequest>(
'SELECT id, order_id AS "orderId", customer_id AS "customerId", status, created_at AS "createdAt" FROM return_requests WHERE id = $1',
[id]
);
return rows[0] ?? null;
}
Jest 测试:
import { getReturnRequestById } from '../db/return-requests';
import { pool } from '../db/pool';
jest.mock('../db/pool');
const mockQuery = pool.query as jest.Mock;
describe('getReturnRequestById', () => {
it('returns the request when found', async () => {
mockQuery.mockResolvedValueOnce({ rows: [{ id: 'abc', orderId: '123', status: 'submitted' }] });
const result = await getReturnRequestById('abc');
expect(result?.id).toBe('abc');
});
it('returns null when not found', async () => {
mockQuery.mockResolvedValueOnce({ rows: [] });
const result = await getReturnRequestById('missing');
expect(result).toBeNull();
});
});