refactor(login_core): 消除代码重复,提取手机号查找为私有方法

范围:src/core/login_core/
- 提取手机号查找逻辑为 findUserByPhone() 私有方法
- 添加 isPhoneExists() 私有方法检查手机号是否存在
- 消除 login、validateUserUniqueness、sendPasswordResetCode 等方法中的重复代码
- 测试文件添加文件头注释,清理未使用的 UsersService 导入
- 更新版本号 1.1.0 -> 1.1.1
This commit is contained in:
moyin
2026-01-15 14:13:48 +08:00
parent 519394645a
commit 4f18f0fec6
2 changed files with 69 additions and 17 deletions

View File

@@ -1,8 +1,42 @@
/**
* 登录核心模块测试套件
*
* 功能描述:
* - 测试LoginCoreModule的模块配置和依赖注入
* - 验证服务提供者的正确注册和实例化
* - 确保JWT配置的正确加载和访问
* - 测试模块导出和依赖关系
*
* 测试覆盖范围:
* - 模块定义:模块实例化和配置验证
* - 服务提供者LoginCoreService和ConfigService的注入
* - JWT配置JWT密钥和过期时间的配置访问
* - 模块依赖:依赖模块的正确导入
* - 模块导出:服务的正确导出和可用性
*
* 测试策略:
* - 单元测试:独立测试模块配置
* - Mock测试模拟所有外部依赖服务
* - 配置测试:验证配置项的正确读取
*
* 依赖模块:
* - Jest: 测试框架和Mock功能
* - NestJS Testing: 提供测试模块和依赖注入
*
* 最近修改:
* - 2026-01-15: 代码规范优化 - 清理未使用的导入(UsersService) (修改者: moyin)
* - 2026-01-15: 代码规范优化 - 添加文件头注释 (修改者: moyin)
*
* @author moyin
* @version 1.0.2
* @since 2025-12-17
* @lastModified 2026-01-15
*/
import { Test, TestingModule } from '@nestjs/testing';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { LoginCoreService } from './login_core.service';
import { UsersService } from '../db/users/users.service';
import { EmailService } from '../utils/email/email.service';
import { VerificationService } from '../utils/verification/verification.service';

View File

@@ -12,16 +12,16 @@
* - 为business层提供可复用的服务
*
* 最近修改:
* - 2026-01-15: 代码规范优化 - 提取手机号查找为私有方法消除重复代码 (修改者: moyin)
* - 2026-01-12: 代码规范优化 - 提取魔法数字为常量,拆分过长方法,消除代码重复 (修改者: moyin)
* - 2026-01-12: 代码规范优化 - 添加LoginCoreService类注释完善类职责和方法说明 (修改者: moyin)
* - 2026-01-12: 代码规范优化 - 处理TODO项移除短信发送相关的TODO注释 (修改者: moyin)
* - 2025-01-07: 代码规范优化 - 清理未使用的导入(EmailSendResult, crypto)
* - 2025-01-07: 代码规范优化 - 修复常量命名(saltRounds -> SALT_ROUNDS)
*
* @author moyin
* @version 1.1.0
* @version 1.1.1
* @since 2025-12-17
* @lastModified 2026-01-12
* @lastModified 2026-01-15
*/
import { Injectable, UnauthorizedException, ConflictException, NotFoundException, BadRequestException, ForbiddenException, Inject } from '@nestjs/common';
@@ -243,8 +243,7 @@ export class LoginCoreService {
// 如果邮箱未找到,尝试手机号查找(简单验证)
if (!user && this.isPhoneNumber(identifier)) {
const users = await this.usersService.findAll();
user = users.find((u: Users) => u.phone === identifier) || null;
user = await this.findUserByPhone(identifier);
}
// 用户不存在
@@ -340,9 +339,8 @@ export class LoginCoreService {
// 检查手机号是否已存在
if (phone) {
const users = await this.usersService.findAll();
const existingPhone = users.find((u: Users) => u.phone === phone);
if (existingPhone) {
const phoneExists = await this.isPhoneExists(phone);
if (phoneExists) {
throw new ConflictException('手机号已存在');
}
}
@@ -555,8 +553,7 @@ export class LoginCoreService {
throw new BadRequestException('邮箱未验证,无法重置密码');
}
} else if (this.isPhoneNumber(identifier)) {
const users = await this.usersService.findAll();
user = users.find((u: Users) => u.phone === identifier) || null;
user = await this.findUserByPhone(identifier);
}
if (!user) {
@@ -616,8 +613,7 @@ export class LoginCoreService {
if (this.isEmail(identifier)) {
user = await this.usersService.findByEmail(identifier);
} else if (this.isPhoneNumber(identifier)) {
const users = await this.usersService.findAll();
user = users.find((u: Users) => u.phone === identifier) || null;
user = await this.findUserByPhone(identifier);
}
if (!user) {
@@ -847,6 +843,30 @@ export class LoginCoreService {
return phoneRegex.test(str.replace(/\s/g, ''));
}
/**
* 通过手机号查找用户
*
* @param phone 手机号
* @returns 用户信息或null
* @private
*/
private async findUserByPhone(phone: string): Promise<Users | null> {
const users = await this.usersService.findAll();
return users.find((u: Users) => u.phone === phone) || null;
}
/**
* 检查手机号是否已存在
*
* @param phone 手机号
* @returns 是否存在
* @private
*/
private async isPhoneExists(phone: string): Promise<boolean> {
const user = await this.findUserByPhone(phone);
return user !== null;
}
/**
* 验证码登录
*
@@ -888,8 +908,7 @@ export class LoginCoreService {
}
} else if (this.isPhoneNumber(identifier)) {
// 手机号登录
const users = await this.usersService.findAll();
user = users.find((u: Users) => u.phone === identifier) || null;
user = await this.findUserByPhone(identifier);
verificationType = VerificationCodeType.SMS_VERIFICATION;
} else {
throw new BadRequestException('请提供有效的邮箱或手机号');
@@ -964,8 +983,7 @@ export class LoginCoreService {
throw new BadRequestException('邮箱未验证,无法使用验证码登录');
}
} else if (this.isPhoneNumber(identifier)) {
const users = await this.usersService.findAll();
user = users.find((u: Users) => u.phone === identifier) || null;
user = await this.findUserByPhone(identifier);
verificationType = VerificationCodeType.SMS_VERIFICATION;
} else {
throw new BadRequestException('请提供有效的邮箱或手机号');