Files
plates-server/src/users/models/user-badge.model.ts
richarjiang 1c033cd801 feat(badges): 更新勋章系统,支持UUID作为勋章ID类型
feat(challenges): 优化进度报告,添加睡眠挑战勋章授予逻辑
fix(push-notifications): 修复推送测试服务初始化返回值问题
2025-11-15 23:25:02 +08:00

105 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Column, Model, Table, DataType, ForeignKey, BelongsTo } from 'sequelize-typescript';
import { User } from './user.model';
import { BadgeConfig } from './badge-config.model';
export enum BadgeSource {
CHALLENGE = 'challenge',
MANUAL = 'manual',
SYSTEM = 'system',
}
@Table({
tableName: 't_user_badges',
underscored: true,
timestamps: true,
indexes: [
{
unique: true,
fields: ['user_id', 'badge_code'],
name: 'unique_user_badge',
},
{
fields: ['user_id'],
},
{
fields: ['badge_code'],
},
{
fields: ['awarded_at'],
},
],
})
export class UserBadge extends Model {
@Column({
type: DataType.CHAR(36),
defaultValue: DataType.UUIDV4,
primaryKey: true,
})
declare id: string;
@ForeignKey(() => User)
@Column({
type: DataType.STRING,
allowNull: false,
comment: '用户ID',
})
declare userId: string;
@ForeignKey(() => BadgeConfig)
@Column({
type: DataType.STRING(64),
allowNull: false,
comment: '勋章代码',
})
declare badgeCode: string;
@Column({
type: DataType.DATE,
allowNull: false,
defaultValue: DataType.NOW,
comment: '获得时间',
})
declare awardedAt: Date;
@Column({
type: DataType.ENUM(...Object.values(BadgeSource)),
allowNull: false,
defaultValue: BadgeSource.SYSTEM,
comment: '授予来源',
})
declare source: BadgeSource;
@Column({
type: DataType.STRING(128),
allowNull: true,
comment: '来源ID如挑战ID',
})
declare sourceId: string;
@Column({
type: DataType.JSON,
allowNull: true,
comment: '额外元数据',
})
declare metadata: Record<string, any>;
@Column({
type: DataType.BOOLEAN,
allowNull: false,
defaultValue: false,
comment: '是否已展示过客户端展示勋章获得动画后设置为true',
})
declare isShow: boolean;
@Column({
type: DataType.DATE,
defaultValue: DataType.NOW,
})
declare createdAt: Date;
@BelongsTo(() => User)
declare user: User;
@BelongsTo(() => BadgeConfig, 'badgeCode')
declare badge: BadgeConfig;
}