Files
plates-server/src/activity-logs/models/activity-log.model.ts
2025-08-29 10:13:26 +08:00

72 lines
1.9 KiB
TypeScript

import { BelongsTo, Column, DataType, ForeignKey, Index, Model, Table } from 'sequelize-typescript';
import { User } from '../../users/models/user.model';
export enum ActivityEntityType {
USER = 'USER',
USER_PROFILE = 'USER_PROFILE',
USER_WEIGHT_HISTORY = 'USER_WEIGHT_HISTORY',
CHECKIN = 'CHECKIN',
TRAINING_PLAN = 'TRAINING_PLAN',
WORKOUT = 'WORKOUT',
DIET_RECORD = 'DIET_RECORD',
}
export enum ActivityActionType {
CREATE = 'CREATE',
UPDATE = 'UPDATE',
DELETE = 'DELETE',
}
@Table({
tableName: 't_activity_logs',
underscored: true,
})
export class ActivityLog extends Model {
@Column({
type: DataType.UUID,
primaryKey: true,
defaultValue: DataType.UUIDV4,
})
declare id: string;
@ForeignKey(() => User)
@Column({ type: DataType.STRING, allowNull: false, comment: '用户ID' })
declare userId: string;
@BelongsTo(() => User)
declare user?: User;
@Column({
type: DataType.ENUM('USER', 'USER_PROFILE', 'USER_WEIGHT_HISTORY', 'CHECKIN', 'TRAINING_PLAN', 'WORKOUT', 'DIET_RECORD'),
allowNull: false,
comment: '实体类型',
})
declare entityType: ActivityEntityType;
@Column({
type: DataType.ENUM('CREATE', 'UPDATE', 'DELETE'),
allowNull: false,
comment: '动作类型',
})
declare action: ActivityActionType;
@Column({ type: DataType.STRING, allowNull: true, comment: '实体ID' })
declare entityId: string | null;
@Column({ type: DataType.JSON, allowNull: true, comment: '变更详情或前后快照' })
declare changes: Record<string, any> | null;
@Column({ type: DataType.JSON, allowNull: true, comment: '附加信息(来源、设备等)' })
declare metadata: Record<string, any> | null;
@Column({ type: DataType.DATE, defaultValue: DataType.NOW, comment: '发生时间' })
declare createdAt: Date;
@Column({ type: DataType.DATE, defaultValue: DataType.NOW })
declare updatedAt: Date;
}