feat: 在用户资料中添加活动水平字段

- 更新用户资料相关逻辑,新增活动水平字段,支持用户在更新资料时提供活动水平信息。
- 修改相关DTO和模型,确保活动水平字段的有效性和数据一致性。
- 更新用户响应数据结构,包含活动水平信息,提升用户体验和数据完整性。
This commit is contained in:
richarjiang
2025-08-27 10:00:18 +08:00
parent a8c67ceb17
commit 79aa300aa1
4 changed files with 34 additions and 6 deletions

View File

@@ -52,6 +52,10 @@ export class UpdateUserDto {
@ApiProperty({ description: '身高(厘米)', example: 168 }) @ApiProperty({ description: '身高(厘米)', example: 168 })
height?: number; height?: number;
@IsOptional()
@ApiProperty({ description: '活动水平1-5的枚举值', example: 3, minimum: 1, maximum: 5 })
activityLevel?: number;
} }
export class UpdateUserResponseDto { export class UpdateUserResponseDto {

View File

@@ -25,7 +25,7 @@ export interface UserWithPurchaseStatus {
maxUsageCount: number; maxUsageCount: number;
favoriteTopicCount: number; favoriteTopicCount: number;
isVip: boolean; isVip: boolean;
profile?: Pick<UserProfile, 'dailyStepsGoal' | 'dailyCaloriesGoal' | 'pilatesPurposes' | 'weight' | 'height'>; profile?: Pick<UserProfile, 'dailyStepsGoal' | 'dailyCaloriesGoal' | 'pilatesPurposes' | 'weight' | 'height' | 'activityLevel'>;
} }
export class UserResponseDto implements BaseResponseDto<UserWithPurchaseStatus> { export class UserResponseDto implements BaseResponseDto<UserWithPurchaseStatus> {

View File

@@ -1,6 +1,14 @@
import { BelongsTo, Column, DataType, ForeignKey, Model, Table } from 'sequelize-typescript'; import { BelongsTo, Column, DataType, ForeignKey, Model, Table } from 'sequelize-typescript';
import { User } from './user.model'; import { User } from './user.model';
export enum ActivityLevel {
LEVEL_1 = 1,
LEVEL_2 = 2,
LEVEL_3 = 3,
LEVEL_4 = 4,
LEVEL_5 = 5,
}
@Table({ @Table({
tableName: 't_user_profile', tableName: 't_user_profile',
underscored: true, underscored: true,
@@ -53,6 +61,17 @@ export class UserProfile extends Model {
}) })
declare height: number | null; declare height: number | null;
@Column({
type: DataType.INTEGER,
allowNull: true,
comment: '活动水平1-5的枚举值',
validate: {
min: 1,
max: 5,
},
})
declare activityLevel: ActivityLevel | null;
@Column({ @Column({
type: DataType.DATE, type: DataType.DATE,
defaultValue: DataType.NOW, defaultValue: DataType.NOW,
@@ -64,6 +83,4 @@ export class UserProfile extends Model {
defaultValue: DataType.NOW, defaultValue: DataType.NOW,
}) })
declare updatedAt: Date; declare updatedAt: Date;
} }

View File

@@ -109,6 +109,7 @@ export class UsersService {
pilatesPurposes: profile?.pilatesPurposes, pilatesPurposes: profile?.pilatesPurposes,
weight: profile?.weight, weight: profile?.weight,
height: profile?.height, height: profile?.height,
activityLevel: profile?.activityLevel,
} }
this.logger.log(`getProfile returnData: ${JSON.stringify(returnData, null, 2)}`); this.logger.log(`getProfile returnData: ${JSON.stringify(returnData, null, 2)}`);
@@ -178,7 +179,7 @@ export class UsersService {
// 更新用户昵称、头像 // 更新用户昵称、头像
async updateUser(updateUserDto: UpdateUserDto): Promise<UpdateUserResponseDto> { async updateUser(updateUserDto: UpdateUserDto): Promise<UpdateUserResponseDto> {
const { userId, name, avatar, gender, birthDate, dailyStepsGoal, dailyCaloriesGoal, pilatesPurposes, weight, height } = updateUserDto; const { userId, name, avatar, gender, birthDate, dailyStepsGoal, dailyCaloriesGoal, pilatesPurposes, weight, height, activityLevel } = updateUserDto;
this.logger.log(`updateUser: ${JSON.stringify(updateUserDto, null, 2)}`); this.logger.log(`updateUser: ${JSON.stringify(updateUserDto, null, 2)}`);
@@ -214,7 +215,7 @@ export class UsersService {
await user.save(); await user.save();
// 更新或创建扩展信息 // 更新或创建扩展信息
if (dailyStepsGoal !== undefined || dailyCaloriesGoal !== undefined || pilatesPurposes !== undefined || weight !== undefined || height !== undefined) { if (dailyStepsGoal !== undefined || dailyCaloriesGoal !== undefined || pilatesPurposes !== undefined || weight !== undefined || height !== undefined || activityLevel !== undefined) {
const [profile] = await this.userProfileModel.findOrCreate({ const [profile] = await this.userProfileModel.findOrCreate({
where: { userId }, where: { userId },
defaults: { userId }, defaults: { userId },
@@ -235,6 +236,10 @@ export class UsersService {
profile.height = height; profile.height = height;
profileChanges.height = height; profileChanges.height = height;
} }
if (activityLevel !== undefined) {
profile.activityLevel = activityLevel;
profileChanges.activityLevel = activityLevel;
}
await profile.save(); await profile.save();
} }
@@ -646,6 +651,7 @@ export class UsersService {
pilatesPurposes: profileForLogin.pilatesPurposes, pilatesPurposes: profileForLogin.pilatesPurposes,
weight: profileForLogin.weight, weight: profileForLogin.weight,
height: profileForLogin.height, height: profileForLogin.height,
activityLevel: profileForLogin.activityLevel,
} : undefined, } : undefined,
}; };
@@ -857,6 +863,7 @@ export class UsersService {
pilatesPurposes: profileForGuest.pilatesPurposes, pilatesPurposes: profileForGuest.pilatesPurposes,
weight: profileForGuest.weight, weight: profileForGuest.weight,
height: profileForGuest.height, height: profileForGuest.height,
activityLevel: profileForGuest.activityLevel,
} : undefined, } : undefined,
}; };