fix(count): participant_count 改为 COUNT 重算自愈,修复同秒重复请求导致的 +3 漂移
- 新增 recalcParticipantCount:join/cancel/注销账号后以实时 COUNT 重算,历史漂移下次操作即自愈 - join 满员判定改实时 COUNT(*),不信任可能漂移的缓存计数器 - cancel 加 status 守卫拒绝重复取消,实现幂等 - 本地 E2E 17/17,生产数据已全量重算(8289ef46: 6→3)并部署验证
This commit is contained in:
@@ -1004,12 +1004,24 @@ app.get('/user/stats', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
const recalcParticipantCount = (activityId) => {
|
||||
const now = new Date().toISOString();
|
||||
db.prepare(`
|
||||
UPDATE activities
|
||||
SET participant_count = (SELECT COUNT(*) FROM activity_participants WHERE activity_id = ? AND status != 'cancelled'),
|
||||
updated_at = ?
|
||||
WHERE id = ?
|
||||
`).run(activityId, now, activityId);
|
||||
};
|
||||
|
||||
// 注销账号
|
||||
app.delete('/user/account', (req, res) => {
|
||||
try {
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
const affectedActivities = db.prepare('SELECT DISTINCT activity_id FROM activity_participants WHERE user_id = ?').all(userId).map(r => r.activity_id);
|
||||
db.prepare('DELETE FROM user_companies WHERE user_id = ?').run(userId);
|
||||
db.prepare('DELETE FROM activity_participants WHERE user_id = ?').run(userId);
|
||||
affectedActivities.forEach(recalcParticipantCount);
|
||||
db.prepare('DELETE FROM users WHERE id = ?').run(userId);
|
||||
res.json({ success: true, data: null });
|
||||
} catch (error) {
|
||||
@@ -1394,7 +1406,8 @@ app.post('/activities/:id/join', requireEmail, (req, res) => {
|
||||
return res.json({ success: false, message: '您已参与该活动' });
|
||||
}
|
||||
|
||||
if (activity.participant_count >= activity.max_participants) {
|
||||
const joinedCount = db.prepare("SELECT COUNT(*) as count FROM activity_participants WHERE activity_id = ? AND status != 'cancelled'").get(id).count;
|
||||
if (joinedCount >= activity.max_participants) {
|
||||
return res.json({ success: false, message: '活动人数已满' });
|
||||
}
|
||||
|
||||
@@ -1408,7 +1421,7 @@ app.post('/activities/:id/join', requireEmail, (req, res) => {
|
||||
const participantId = uuidv4();
|
||||
db.prepare("INSERT INTO activity_participants (id, activity_id, user_id, status, joined_at, created_at) VALUES (?, ?, ?, 'joined', ?, ?)").run(participantId, id, userId, now, now);
|
||||
}
|
||||
db.prepare('UPDATE activities SET participant_count = participant_count + 1, updated_at = ? WHERE id = ?').run(now, id);
|
||||
recalcParticipantCount(id);
|
||||
|
||||
res.json({ success: true, data: null });
|
||||
} catch (error) {
|
||||
@@ -1424,13 +1437,13 @@ app.delete('/activities/:id/join', requireEmail, (req, res) => {
|
||||
const userId = req.headers['x-user-id'] || 'user-1';
|
||||
|
||||
const participant = db.prepare('SELECT * FROM activity_participants WHERE activity_id = ? AND user_id = ?').get(id, userId);
|
||||
if (!participant) {
|
||||
if (!participant || participant.status === 'cancelled') {
|
||||
return res.json({ success: false, message: '您未参与该活动' });
|
||||
}
|
||||
|
||||
const now = new Date().toISOString();
|
||||
db.prepare("UPDATE activity_participants SET status = 'cancelled' WHERE id = ?").run(participant.id);
|
||||
db.prepare('UPDATE activities SET participant_count = participant_count - 1, updated_at = ? WHERE id = ?').run(now, id);
|
||||
recalcParticipantCount(id);
|
||||
|
||||
res.json({ success: true, data: null });
|
||||
} catch (error) {
|
||||
@@ -1931,7 +1944,7 @@ app.get('/chaishu/members', (req, res) => {
|
||||
// 会员详情
|
||||
app.get('/chaishu/members/:uid', (req, res) => {
|
||||
try {
|
||||
const m = db.prepare('SELECT * FROM chaishu_members WHERE uid = ?').get(req.params.uid);
|
||||
const m = db.prepare('SELECT m.*, u.id AS bound_user_id, u.nickname AS bound_nickname, u.avatar_url AS bound_avatar, u.position AS bound_position FROM chaishu_members m LEFT JOIN users u ON u.chaishu_uid = m.uid WHERE m.uid = ?').get(req.params.uid);
|
||||
if (!m) {
|
||||
return res.json({ success: false, message: '会员不存在' });
|
||||
}
|
||||
@@ -1944,6 +1957,10 @@ app.get('/chaishu/members/:uid', (req, res) => {
|
||||
description: m.description,
|
||||
shifuName: m.shifu_name, shifuId: m.shifu_id,
|
||||
tudiNames: safeParseJson(m.tudi_names, []), tudiIds: safeParseJson(m.tudi_ids, []),
|
||||
boundUserId: m.bound_user_id || '',
|
||||
boundNickname: m.bound_nickname || '',
|
||||
boundAvatar: m.bound_avatar || '',
|
||||
boundPosition: m.bound_position || '',
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user