refactor: 移除使用次数扣除逻辑,调整活动类型及记录逻辑
This commit is contained in:
@@ -65,7 +65,6 @@ export class AiCoachController {
|
||||
confirmationData: body.confirmationData,
|
||||
});
|
||||
|
||||
await this.usersService.deductUserUsageCount(userId);
|
||||
|
||||
// 普通流式/非流式响应
|
||||
const readable = result as any;
|
||||
|
||||
@@ -517,6 +517,9 @@ export class AiCoachService {
|
||||
messages.unshift({ role: 'system', content: NUTRITION_ANALYST_PROMPT });
|
||||
}
|
||||
|
||||
// 普通聊天才需要扣减次数
|
||||
await this.usersService.deductUserUsageCount(params.userId);
|
||||
|
||||
return this.generateAIResponse(params.conversationId, params.userId, messages);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,14 @@ import { InjectModel } from '@nestjs/sequelize';
|
||||
import { Op, WhereOptions } from 'sequelize';
|
||||
import { Goal, GoalRepeatType, GoalStatus } from '../models/goal.model';
|
||||
import { GoalTask, TaskStatus } from '../models/goal-task.model';
|
||||
import { CreateGoalTaskDto, UpdateGoalTaskDto, GoalTaskQueryDto, CompleteGoalTaskDto } from '../dto/goal-task.dto';
|
||||
import { UpdateGoalTaskDto, GoalTaskQueryDto, CompleteGoalTaskDto } from '../dto/goal-task.dto';
|
||||
import * as dayjs from 'dayjs';
|
||||
import * as weekOfYear from 'dayjs/plugin/weekOfYear';
|
||||
import * as isoWeek from 'dayjs/plugin/isoWeek';
|
||||
import * as isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
|
||||
import * as isSameOrAfter from 'dayjs/plugin/isSameOrAfter';
|
||||
import { ActivityLevel, ActivityType } from 'src/users/models/user-activity.model';
|
||||
import { UserActivityService } from 'src/users/services/user-activity.service';
|
||||
|
||||
dayjs.extend(weekOfYear);
|
||||
dayjs.extend(isoWeek);
|
||||
@@ -24,6 +26,7 @@ export class GoalTaskService {
|
||||
private readonly goalModel: typeof Goal,
|
||||
@InjectModel(GoalTask)
|
||||
private readonly goalTaskModel: typeof GoalTask,
|
||||
private readonly userActivityService: UserActivityService,
|
||||
) { }
|
||||
|
||||
/**
|
||||
@@ -452,6 +455,21 @@ export class GoalTaskService {
|
||||
|
||||
await task.save();
|
||||
|
||||
try {
|
||||
const today = dayjs().format('YYYY-MM-DD');
|
||||
|
||||
await this.userActivityService.recordActivity(userId, {
|
||||
activityType: ActivityType.GOAL,
|
||||
activityDate: today,
|
||||
level: ActivityLevel.MEDIUM,
|
||||
remark: `完成目标任务: ${task.title}`,
|
||||
});
|
||||
this.logger.log(`记录用户活跃 - 用户: ${userId} 完成目标任务: ${task.title}`);
|
||||
} catch (activityError) {
|
||||
// 记录活跃失败不影响主要业务流程
|
||||
this.logger.error(`记录用户活跃失败: ${activityError.message}`);
|
||||
}
|
||||
|
||||
this.logger.log(`用户 ${userId} 完成任务: ${task.title}, 当前进度: ${task.currentCount}/${task.targetCount}`);
|
||||
|
||||
return this.formatTaskResponse(task);
|
||||
|
||||
@@ -4,7 +4,7 @@ import { User } from './user.model';
|
||||
|
||||
export enum ActivityType {
|
||||
LOGIN = 1, // 登录
|
||||
WORKOUT = 2, // 训练
|
||||
GOAL = 2, // 目标任务完成
|
||||
DIET_RECORD = 3, // 饮食记录
|
||||
WEIGHT_RECORD = 4, // 体重记录
|
||||
PROFILE_UPDATE = 5, // 资料更新
|
||||
@@ -14,7 +14,7 @@ export enum ActivityType {
|
||||
// 活跃类型显示名称映射
|
||||
export const ActivityTypeNames: Record<ActivityType, string> = {
|
||||
[ActivityType.LOGIN]: '登录',
|
||||
[ActivityType.WORKOUT]: '训练',
|
||||
[ActivityType.GOAL]: '目标任务完成',
|
||||
[ActivityType.DIET_RECORD]: '饮食记录',
|
||||
[ActivityType.WEIGHT_RECORD]: '体重记录',
|
||||
[ActivityType.PROFILE_UPDATE]: '资料更新',
|
||||
@@ -77,7 +77,7 @@ export class UserActivity extends SequelizeModel<UserActivityAttributes, UserAct
|
||||
@Column({
|
||||
type: DataType.TINYINT,
|
||||
allowNull: false,
|
||||
comment: '活跃类型:1-登录,2-训练,3-饮食记录,4-体重记录,5-资料更新,6-打卡',
|
||||
comment: '活跃类型:1-登录,2-目标任务完成,3-饮食记录,4-体重记录,5-资料更新,6-打卡',
|
||||
})
|
||||
activityType!: ActivityType;
|
||||
|
||||
|
||||
@@ -40,6 +40,6 @@ import { ActivityLogsModule } from '../activity-logs/activity-logs.module';
|
||||
],
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService, ApplePurchaseService, EncryptionService, AppleAuthService, CosService, UserActivityService],
|
||||
exports: [UsersService, AppleAuthService],
|
||||
exports: [UsersService, AppleAuthService, UserActivityService],
|
||||
})
|
||||
export class UsersModule { }
|
||||
|
||||
Reference in New Issue
Block a user