feat(ai-coach,checkins): 实现软删除功能

在AI教练和打卡模块中添加deleted字段,将物理删除改为软删除:
- 在AiConversation和AiMessage模型中添加deleted布尔字段
- 在Checkin模型中添加deleted字段
- 更新所有查询条件添加deleted: false过滤
- 修改删除操作为标记deleted: true而非物理删除
- 在打卡服务中添加重复记录检查逻辑
This commit is contained in:
richarjiang
2025-08-14 19:16:57 +08:00
parent 9ad65b19fd
commit 812ac5c21e
5 changed files with 58 additions and 10 deletions

View File

@@ -54,7 +54,7 @@ export class AiCoachService {
buildChatHistory = async (userId: string, conversationId: string) => {
const history = await AiMessage.findAll({
where: { userId, conversationId },
where: { userId, conversationId, deleted: false },
order: [['created_at', 'ASC']],
});
@@ -128,7 +128,7 @@ export class AiCoachService {
const pageSize = Math.min(50, Math.max(1, params.pageSize || 20));
const offset = (page - 1) * pageSize;
const { rows, count } = await AiConversation.findAndCountAll({
where: { userId },
where: { userId, deleted: false },
order: [['last_message_at', 'DESC']],
offset,
limit: pageSize,
@@ -147,10 +147,10 @@ export class AiCoachService {
}
async getConversationDetail(userId: string, conversationId: string) {
const conv = await AiConversation.findOne({ where: { id: conversationId, userId } });
const conv = await AiConversation.findOne({ where: { id: conversationId, userId, deleted: false } });
if (!conv) return null;
const messages = await AiMessage.findAll({
where: { userId, conversationId },
where: { userId, conversationId, deleted: false },
order: [['created_at', 'ASC']],
});
return {
@@ -163,10 +163,10 @@ export class AiCoachService {
}
async deleteConversation(userId: string, conversationId: string): Promise<boolean> {
const conv = await AiConversation.findOne({ where: { id: conversationId, userId } });
const conv = await AiConversation.findOne({ where: { id: conversationId, userId, deleted: false } });
if (!conv) return false;
await AiMessage.destroy({ where: { userId, conversationId } });
await AiConversation.destroy({ where: { id: conversationId, userId } });
await AiMessage.update({ deleted: true }, { where: { userId, conversationId } });
await AiConversation.update({ deleted: true }, { where: { id: conversationId, userId } });
return true;
}

View File

@@ -46,6 +46,13 @@ export class AiConversation extends Model {
})
declare updatedAt: Date;
@Column({
type: DataType.BOOLEAN,
allowNull: false,
defaultValue: false,
})
declare deleted: boolean;
@HasMany(() => AiMessage)
declare messages?: AiMessage[];
}

View File

@@ -65,6 +65,13 @@ export class AiMessage extends Model {
})
declare updatedAt: Date;
@Column({
type: DataType.BOOLEAN,
allowNull: false,
defaultValue: false,
})
declare deleted: boolean;
@BelongsTo(() => AiConversation)
declare conversation?: AiConversation;
}