[Regression on closed #541] TeamsService.create and updateSplits are back to one SELECT per member to check existence
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 75/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- postgresql, typescript
- 领域
- backend, databases, performance, testing
调研方向
Start in src/teams/teams.service.ts at create and updateSplits, where each member is checked with a separate findOne. Read the related team service spec and inspect how repository queries and transactions are used in both methods. Replace the repeated checks with a shared batched existence check, then add a spec confirming one user query for a multi-member payload and run the team service tests.
由索引模型根据 Issue 内容生成。
描述
Location — src/teams/teams.service.ts:28-35 (create) and src/teams/teams.service.ts:80-87 (updateSplits).
Problem — The per-member existence loop closed as #541 is present again, duplicated verbatim in both methods:
// Verify all member userIds exist
for (const member of dto.members) {
const userExists = await this.userRepo.findOne({
where: { id: member.userId },
});
if (!userExists) {
throw new NotFoundException(`User ${member.userId} not found`);
}
}
Each iteration is an awaited round trip, so the queries are strictly sequential: a 20-member team issues 20 serial SELECT * FROM users WHERE id = $1 LIMIT 1 statements before any write happens. findOne with no select also hydrates the full User row (including payout address and other PII columns) when only the row's existence is needed.
Why it matters — Request latency scales linearly with member count at full network round-trip cost, on a write path that then holds a transaction open. It is also a needless source of partial-failure windows: by the time member 20 is checked, member 1 may already have been deleted, so the FK can still reject the insert (see the non-transactional delete/insert problem in updateSplits). And the same five lines now exist in two places, so the next fix has to be applied twice — exactly how this regressed.
Reproduction / failure path
- Enable
DATABASE_LOGGING=true. POST /teamswith 20 distinct valid memberuserIds.- The log shows 20 separate
usersSELECTs before theteamsINSERT, instead of one. - Same for
PATCH /teams/:id/splitswith 20 members.
Suggested fix
- Replace both loops with one batched query and a set difference:
Note this also reports every missing id at once instead of only the first.const ids = members.map((m) => m.userId); const found = await this.userRepo.find({ where: { id: In(ids) }, select: { id: true } }); const missing = ids.filter((id) => !found.some((u) => u.id === id)); if (missing.length) throw new NotFoundException(`User(s) not found: ${missing.join(', ')}`); - Extract it into one private helper (e.g.
assertMembersExist(members, manager?)) used by bothcreateandupdateSplits, taking an optionalEntityManagerso it can run inside the same transaction as the write. - Add a spec asserting
userRepo.findis called exactly once for a multi-member payload, so a future merge cannot quietly reintroduce the loop.
- 主要语言
- TypeScript
- 星标
- 2
- 派生
- 90
- 平均合并
- 31 分钟
- 30 天内合并 PR
- 20
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
MergeFi/backend 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
bug security
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
bug testing
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
bug security
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
相似的 Issue
-
Codex使用可 コード
难度 2/5 1-3 小时 新手友好度 72/100
ryuki-moro/Nicomaru#218 ·
维护者通常 3 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
sveltejs/svelte-eslint-parser#1008 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
silverbucket/inbox-rs#351 ·
维护者通常 1 天内回复
-
[Bug]: The H2 series' and the H3C's fan 3 is labelled "Chamber Fan", Bambu calls it the exhaust fan未关闭area:ui
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
bug front P2
难度 2/5 1-3 小时 新手友好度 86/100
invoicerr-app/invoicerr#964 ·
维护者通常 1 天内回复