prelearning.ts
4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import type { FastifyPluginAsync } from 'fastify';
import type {
PrelearningActionPayload,
PrelearningActionRequest,
PrelearningActionResponse,
PrelearningActionType,
PrelearningBuriedPointPayload,
PrelearningBuriedPointRequest,
PrelearningBuriedPointResponse,
PrelearningApiError,
PrelearningInitRequest,
PrelearningInitResponse,
} from '../../packages/contracts/src/index.js';
import { createErrorResponse, AppError } from '../lib/errors.js';
import {
createPrismaPrelearningRepository,
initializePrelearningSession,
performPrelearningAction,
recordPrelearningBuriedPoint,
type PrelearningRepository,
} from '../features/prelearning.js';
export interface RouteOptions {
createRepository?: (repository: PrelearningRepository) => PrelearningRepository;
}
export const prelearningRoute: FastifyPluginAsync<RouteOptions> = async (app, options) => {
const baseRepository = createPrismaPrelearningRepository(app.prisma);
const repository = options?.createRepository ? options.createRepository(baseRepository) : baseRepository;
app.post('/api/prelearning/init', async (request, reply) => {
const body = request.body as PrelearningInitRequest | undefined;
if (!body?.payload) {
const error = new AppError('PRELEARNING_INIT_PAYLOAD_INVALID', '初始化参数缺失', 400, {
missingFields: ['payload'],
});
const response: PrelearningApiError = createErrorResponse(
error.code,
error.message,
request.id,
error.details,
);
reply.status(error.statusCode).send(response);
return;
}
try {
const result: PrelearningInitResponse = await initializePrelearningSession(repository, body.payload);
reply.send(result);
} catch (error) {
if (error instanceof AppError) {
const response: PrelearningApiError = createErrorResponse(
error.code,
error.message,
request.id,
error.details,
);
reply.status(error.statusCode).send(response);
return;
}
throw error;
}
});
// 处理预学习页底部动作:先保存当前学习进度,再按 actionType 返回下一步页面数据。
// learn_next 会尝试切换到同活动下一个未学习套餐/商品;complete/view_other 会返回完成或查看其他案例状态。
app.post('/api/prelearning/action', async (request, reply) => {
const body = request.body as PrelearningActionRequest | undefined;
if (!body?.payload || !body.actionType) {
const error = new AppError('PRELEARNING_ACTION_PAYLOAD_INVALID', '学习动作参数缺失', 400, {
missingFields: [
...(!body?.actionType ? ['actionType'] : []),
...(!body?.payload ? ['payload'] : []),
],
});
const response: PrelearningApiError = createErrorResponse(
error.code,
error.message,
request.id,
error.details,
);
reply.status(error.statusCode).send(response);
return;
}
try {
const result: PrelearningActionResponse = await performPrelearningAction(
repository,
body.payload as PrelearningActionPayload,
body.actionType as PrelearningActionType,
);
reply.send(result);
} catch (error) {
if (error instanceof AppError) {
const response: PrelearningApiError = createErrorResponse(
error.code,
error.message,
request.id,
error.details,
);
reply.status(error.statusCode).send(response);
return;
}
throw error;
}
});
app.post('/api/prelearning/buried-point', async (request, reply) => {
const body = request.body as PrelearningBuriedPointRequest | undefined;
if (!body?.payload) {
const error = new AppError('PRELEARNING_BURIED_POINT_PAYLOAD_INVALID', '埋点参数缺失', 400, {
missingFields: ['payload'],
});
const response: PrelearningApiError = createErrorResponse(
error.code,
error.message,
request.id,
error.details,
);
reply.status(error.statusCode).send(response);
return;
}
try {
const result: PrelearningBuriedPointResponse = await recordPrelearningBuriedPoint(
repository,
body.payload as PrelearningBuriedPointPayload,
);
reply.send(result);
} catch (error) {
if (error instanceof AppError) {
const response: PrelearningApiError = createErrorResponse(
error.code,
error.message,
request.id,
error.details,
);
reply.status(error.statusCode).send(response);
return;
}
request.log.warn({ err: error }, 'prelearning buried point persistence failed');
reply.send({
stored: false,
duplicate: false,
} satisfies PrelearningBuriedPointResponse);
}
});
};