perf: 优化饮食记录代码
This commit is contained in:
@@ -8,6 +8,7 @@ export enum ActivityEntityType {
|
||||
CHECKIN = 'CHECKIN',
|
||||
TRAINING_PLAN = 'TRAINING_PLAN',
|
||||
WORKOUT = 'WORKOUT',
|
||||
DIET_RECORD = 'DIET_RECORD',
|
||||
|
||||
}
|
||||
|
||||
@@ -38,7 +39,7 @@ export class ActivityLog extends Model {
|
||||
declare user?: User;
|
||||
|
||||
@Column({
|
||||
type: DataType.ENUM('USER', 'USER_PROFILE', 'USER_WEIGHT_HISTORY', 'CHECKIN', 'TRAINING_PLAN', 'WORKOUT'),
|
||||
type: DataType.ENUM('USER', 'USER_PROFILE', 'USER_WEIGHT_HISTORY', 'CHECKIN', 'TRAINING_PLAN', 'WORKOUT', 'DIET_RECORD'),
|
||||
allowNull: false,
|
||||
comment: '实体类型',
|
||||
})
|
||||
|
||||
@@ -3,7 +3,7 @@ import { InjectModel } from '@nestjs/sequelize';
|
||||
import { Op, Transaction } from 'sequelize';
|
||||
import { Sequelize } from 'sequelize-typescript';
|
||||
import { UserDietHistory } from '../users/models/user-diet-history.model';
|
||||
import { ActivityLog } from '../activity-logs/models/activity-log.model';
|
||||
import { ActivityActionType, ActivityEntityType, ActivityLog } from '../activity-logs/models/activity-log.model';
|
||||
import { CreateDietRecordDto, UpdateDietRecordDto, GetDietHistoryQueryDto, DietRecordResponseDto, DietHistoryResponseDto, NutritionSummaryDto } from '../users/dto/diet-record.dto';
|
||||
import { DietRecordSource } from '../users/models/user-diet-history.model';
|
||||
import { ResponseCode } from '../base.dto';
|
||||
@@ -26,6 +26,7 @@ export class DietRecordsService {
|
||||
async addDietRecord(userId: string, createDto: CreateDietRecordDto): Promise<DietRecordResponseDto> {
|
||||
const t = await this.sequelize.transaction();
|
||||
try {
|
||||
this.logger.log(`addDietRecord - userId: ${userId}, createDto: ${JSON.stringify(createDto)}`);
|
||||
// 创建饮食记录
|
||||
const dietRecord = await this.userDietHistoryModel.create({
|
||||
userId,
|
||||
@@ -52,8 +53,10 @@ export class DietRecordsService {
|
||||
// 记录活动日志
|
||||
await this.activityLogModel.create({
|
||||
userId,
|
||||
action: 'diet_record_added',
|
||||
details: {
|
||||
action: ActivityActionType.CREATE,
|
||||
entityType: ActivityEntityType.DIET_RECORD,
|
||||
entityId: dietRecord.id.toString(),
|
||||
changes: {
|
||||
recordId: dietRecord.id,
|
||||
foodName: createDto.foodName,
|
||||
mealType: createDto.mealType,
|
||||
@@ -160,11 +163,13 @@ export class DietRecordsService {
|
||||
// 记录活动日志
|
||||
await this.activityLogModel.create({
|
||||
userId,
|
||||
action: 'diet_record_updated',
|
||||
details: {
|
||||
action: ActivityActionType.UPDATE,
|
||||
entityType: ActivityEntityType.DIET_RECORD,
|
||||
entityId: record.id.toString(),
|
||||
changes: {
|
||||
recordId: record.id,
|
||||
foodName: record.foodName,
|
||||
changes: updateDto,
|
||||
updates: updateDto,
|
||||
},
|
||||
}, { transaction: t });
|
||||
|
||||
@@ -199,8 +204,10 @@ export class DietRecordsService {
|
||||
// 记录活动日志
|
||||
await this.activityLogModel.create({
|
||||
userId,
|
||||
action: 'diet_record_deleted',
|
||||
details: {
|
||||
action: ActivityActionType.DELETE,
|
||||
entityType: ActivityEntityType.DIET_RECORD,
|
||||
entityId: record.id.toString(),
|
||||
changes: {
|
||||
recordId: record.id,
|
||||
foodName: record.foodName,
|
||||
mealType: record.mealType,
|
||||
|
||||
Reference in New Issue
Block a user