[Regression on closed #541] TeamsService.create and updateSplits are back to one SELECT per member to check existence

未关闭
#730 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
postgresql, typescript

调研方向

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 内容生成。

描述

bug performance

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

  1. Enable DATABASE_LOGGING=true.
  2. POST /teams with 20 distinct valid member userIds.
  3. The log shows 20 separate users SELECTs before the teams INSERT, instead of one.
  4. Same for PATCH /teams/:id/splits with 20 members.

Suggested fix

  • Replace both loops with one batched query and a set difference:
    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(', ')}`);
    
    Note this also reports every missing id at once instead of only the first.
  • Extract it into one private helper (e.g. assertMembersExist(members, manager?)) used by both create and updateSplits, taking an optional EntityManager so it can run inside the same transaction as the write.
  • Add a spec asserting userRepo.find is 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 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

MergeFi/backend 的其他 Issue

查看 MergeFi/backend 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。