import { getModelForClass, modelOptions, mongoose, prop, Severity } from '@typegoose/typegoose' import { dbconn } from 'decorators/dbconn' import { Base, TimeStamps } from '@typegoose/typegoose/lib/defaultClasses' import { BaseModule } from './Base' export enum TaskTypeEnum { ONCE = 1, DAILY = 2, } @modelOptions({ schemaOptions: { _id: false }, options: { allowMixed: Severity.ALLOW } }) export class TaskCfg { @prop() id: string @prop() task: string @prop() title: string @prop({ enum: TaskTypeEnum, default: TaskTypeEnum.ONCE }) type: TaskTypeEnum @prop({ default: 1 }) repeat: number @prop() desc: string @prop({ type: mongoose.Schema.Types.Mixed }) category: any @prop({ default: false }) autoclaim: boolean @prop({ type: () => [String], default: [] }) pretasks: string[] @prop() score: number @prop({ default: true }) show: boolean @prop({ type: mongoose.Schema.Types.Mixed }) cfg: any @prop() start?: string @prop() end?: string @prop({ type: mongoose.Schema.Types.Mixed }) params: any @prop({ default: true }) checkChain: boolean public isStart() { const now = Date.now() if (this.start) { let start = new Date(this.start).getTime() if (now < start) { return false } } return true } public isEnd() { const now = Date.now() if (this.end) { let end = new Date(this.end).getTime() if (now > end) { return true } } return false } public isVaild() { return this.isStart() && !this.isEnd() } } export interface ActivityInfoClass extends TimeStamps {} @dbconn() @modelOptions({ schemaOptions: { collection: 'activity_info', timestamps: true } }) export class ActivityInfoClass extends BaseModule { @prop() public _id: string @prop({ required: true }) public name: string @prop() public description: string @prop({ type: () => [TaskCfg], default: [] }) public tasks: TaskCfg[] @prop({ required: false }) public pause: boolean @prop() public startTime: number @prop() public endTime: number @prop() public comment?: string public isVaild() { const now = Date.now() if (this.startTime) { if (now < this.startTime) { return false } } if (this.endTime) { if (now > this.endTime) { return false } } return true } public toJson() { let result = super.toJson() let tasks = [] const now = Date.now() for (let task of this.tasks) { if (task.show && task.isStart()) { tasks.push({ id: task.id, task: task.task, title: task.title, desc: task.desc, type: task.type, repeat: task.repeat, pretasks: task.pretasks, score: task.score, category: task.category, autoclaim: task.autoclaim, cfg: task.cfg, end: task.isEnd(), }) } } result.tasks = tasks return result } } export const ActivityInfo = getModelForClass(ActivityInfoClass, { existingConnection: ActivityInfoClass.db })