From 79aa300aa1a5ed4180b33d567bbc7a1b1bff087e Mon Sep 17 00:00:00 2001 From: richarjiang Date: Wed, 27 Aug 2025 10:00:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8=E7=94=A8=E6=88=B7=E8=B5=84?= =?UTF-8?q?=E6=96=99=E4=B8=AD=E6=B7=BB=E5=8A=A0=E6=B4=BB=E5=8A=A8=E6=B0=B4?= =?UTF-8?q?=E5=B9=B3=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新用户资料相关逻辑,新增活动水平字段,支持用户在更新资料时提供活动水平信息。 - 修改相关DTO和模型,确保活动水平字段的有效性和数据一致性。 - 更新用户响应数据结构,包含活动水平信息,提升用户体验和数据完整性。 --- src/users/dto/update-user.dto.ts | 4 ++++ src/users/dto/user-response.dto.ts | 2 +- src/users/models/user-profile.model.ts | 23 ++++++++++++++++++++--- src/users/users.service.ts | 11 +++++++++-- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/users/dto/update-user.dto.ts b/src/users/dto/update-user.dto.ts index 4824c22..c3d8cc2 100644 --- a/src/users/dto/update-user.dto.ts +++ b/src/users/dto/update-user.dto.ts @@ -52,6 +52,10 @@ export class UpdateUserDto { @ApiProperty({ description: '身高(厘米)', example: 168 }) height?: number; + @IsOptional() + @ApiProperty({ description: '活动水平(1-5的枚举值)', example: 3, minimum: 1, maximum: 5 }) + activityLevel?: number; + } export class UpdateUserResponseDto { diff --git a/src/users/dto/user-response.dto.ts b/src/users/dto/user-response.dto.ts index ce7a976..00cacad 100644 --- a/src/users/dto/user-response.dto.ts +++ b/src/users/dto/user-response.dto.ts @@ -25,7 +25,7 @@ export interface UserWithPurchaseStatus { maxUsageCount: number; favoriteTopicCount: number; isVip: boolean; - profile?: Pick; + profile?: Pick; } export class UserResponseDto implements BaseResponseDto { diff --git a/src/users/models/user-profile.model.ts b/src/users/models/user-profile.model.ts index 2d82017..71f39ca 100644 --- a/src/users/models/user-profile.model.ts +++ b/src/users/models/user-profile.model.ts @@ -1,6 +1,14 @@ import { BelongsTo, Column, DataType, ForeignKey, Model, Table } from 'sequelize-typescript'; import { User } from './user.model'; +export enum ActivityLevel { + LEVEL_1 = 1, + LEVEL_2 = 2, + LEVEL_3 = 3, + LEVEL_4 = 4, + LEVEL_5 = 5, +} + @Table({ tableName: 't_user_profile', underscored: true, @@ -53,6 +61,17 @@ export class UserProfile extends Model { }) declare height: number | null; + @Column({ + type: DataType.INTEGER, + allowNull: true, + comment: '活动水平(1-5的枚举值)', + validate: { + min: 1, + max: 5, + }, + }) + declare activityLevel: ActivityLevel | null; + @Column({ type: DataType.DATE, defaultValue: DataType.NOW, @@ -64,6 +83,4 @@ export class UserProfile extends Model { defaultValue: DataType.NOW, }) declare updatedAt: Date; -} - - +} \ No newline at end of file diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 19afa6b..71bd708 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -109,6 +109,7 @@ export class UsersService { pilatesPurposes: profile?.pilatesPurposes, weight: profile?.weight, height: profile?.height, + activityLevel: profile?.activityLevel, } this.logger.log(`getProfile returnData: ${JSON.stringify(returnData, null, 2)}`); @@ -178,7 +179,7 @@ export class UsersService { // 更新用户昵称、头像 async updateUser(updateUserDto: UpdateUserDto): Promise { - 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)}`); @@ -214,7 +215,7 @@ export class UsersService { 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({ where: { userId }, defaults: { userId }, @@ -235,6 +236,10 @@ export class UsersService { profile.height = height; profileChanges.height = height; } + if (activityLevel !== undefined) { + profile.activityLevel = activityLevel; + profileChanges.activityLevel = activityLevel; + } await profile.save(); } @@ -646,6 +651,7 @@ export class UsersService { pilatesPurposes: profileForLogin.pilatesPurposes, weight: profileForLogin.weight, height: profileForLogin.height, + activityLevel: profileForLogin.activityLevel, } : undefined, }; @@ -857,6 +863,7 @@ export class UsersService { pilatesPurposes: profileForGuest.pilatesPurposes, weight: profileForGuest.weight, height: profileForGuest.height, + activityLevel: profileForGuest.activityLevel, } : undefined, };