fix: 修复授权唯一性验证不检查地区的bug

授权验证规则:一条推荐线上同一类型授权只能有一个人,不管地区是什么
- 使用 findByUserIdAndRoleType 替代 findByUserIdRoleTypeAndRegion
- 错误信息中显示已存在授权的地区名称

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-12 13:20:53 -08:00
parent 75c49951b7
commit a5089db288
1 changed files with 16 additions and 9 deletions

View File

@ -11,6 +11,12 @@ export interface IReferralRepository {
export class AuthorizationValidatorService {
/**
*
*
*
* 1.
* 2. 线
* - A申请了北京市团队A的下线不能再申请任何城市的市团队
* - A申请了广东省团队A的下线不能再申请任何省份的省团队
*/
async validateAuthorizationRequest(
userId: UserId,
@ -43,40 +49,41 @@ export class AuthorizationValidatorService {
}
}
// 2. 检查团队内唯一性(上下级不能重复
// 2. 检查团队内唯一性(上下级不能重复同类型授权,不管地区
const relationship = await referralRepository.findByUserId(userId)
if (!relationship) {
return ValidationResult.success()
}
// 检查所有上级
// 检查所有上级 - 不检查地区,只检查授权类型
const ancestors = await referralRepository.getAllAncestors(userId)
for (const ancestorId of ancestors) {
const ancestorAuth = await authorizationRepository.findByUserIdRoleTypeAndRegion(
// 使用 findByUserIdAndRoleType 而不是 findByUserIdRoleTypeAndRegion
// 因为一条线下同类型授权只能有一个,不管地区是什么
const ancestorAuth = await authorizationRepository.findByUserIdAndRoleType(
ancestorId,
roleType,
regionCode,
)
if (ancestorAuth && ancestorAuth.status !== AuthorizationStatus.REVOKED) {
return ValidationResult.failure(
`本团队已有人申请该${this.getRoleTypeName(roleType)}授权`,
`本团队上级已有人申请该${this.getRoleTypeName(roleType)}授权${ancestorAuth.regionName || '未知地区'}`,
)
}
}
// 检查所有下级
// 检查所有下级 - 不检查地区,只检查授权类型
const descendants = await referralRepository.getAllDescendants(userId)
for (const descendantId of descendants) {
const descendantAuth = await authorizationRepository.findByUserIdRoleTypeAndRegion(
// 使用 findByUserIdAndRoleType 而不是 findByUserIdRoleTypeAndRegion
const descendantAuth = await authorizationRepository.findByUserIdAndRoleType(
descendantId,
roleType,
regionCode,
)
if (descendantAuth && descendantAuth.status !== AuthorizationStatus.REVOKED) {
return ValidationResult.failure(
`本团队已有人申请该${this.getRoleTypeName(roleType)}授权`,
`本团队下级已有人申请该${this.getRoleTypeName(roleType)}授权${descendantAuth.regionName || '未知地区'}`,
)
}
}