import { Schema, Document } from 'mongoose' import db from '../service/db' import { customfieldValue } from './customfieldvalue' import { IUUserModel } from './user' import { IUStageModel } from './stage' export interface IUTaskModel extends Document { _id: string content: string note: string accomplished: string _scenariofieldconfigId: string tagIds: Array<{ _id: string name: string } | string> dueDate: string ancestorIds: IUTaskModel[] _executorId: IUUserModel startDate: string priority: number workTime: { totalTime: number usedTime: number } progress: number storyPoint: string rating: number customfields: any _creatorId: IUUserModel created: string isDone: boolean _stageId: IUStageModel uniqueId: number } const task = new Schema( { content: { type: String, default: '', maxlength: 500, get (value) { return value || '' } }, note: { type: String, default: '', maxlength: 10000, get (value) { return value || '' } }, accomplished: { type: Date, default: null }, startDate: { type: Date, default: null }, dueDate: { type: Date, default: null }, priority: { type: Number, default: 0, required: true, validate: { validator (val) { return [0, 1, 2].includes(val) }, message: '{PATH}: {VALUE} not in [0, 1, 2]' } }, source: { type: String, default: 'teambition' }, uniqueId: Number, isDone: { type: Boolean, default: false, set (val) { if (val) { if (!this.accomplished) { this.accomplished = new Date() } } else { this.accomplished = null } return val } }, isDeleted: { type: Boolean, default: false }, isArchived: { type: String, default: null }, created: { type: Date, default: Date.now }, updated: { type: Date, default: Date.now }, recurrence: { type: Schema.Types.Mixed }, _sourceId: { type: Schema.Types.ObjectId, ref: 'Task' }, sourceDate: Date, involveMembers: [ { type: Schema.Types.ObjectId, ref: 'User' } ], visible: { type: String, default: 'members', enum: ['members', 'involves'] }, _creatorId: { type: Schema.Types.ObjectId, ref: 'User', required: true }, _executorId: { type: Schema.Types.ObjectId, ref: 'User' }, _organizationId: { type: Schema.Types.ObjectId, ref: 'Organization', set (val) { if (val) { // 互斥 this._projectId = null this._tasklistId = null this._stageId = null } return val } }, _projectId: { type: Schema.Types.ObjectId, ref: 'Project', set (val) { if (val) { this._organizationId = null } return val } }, _tasklistId: { type: Schema.Types.ObjectId, ref: 'Tasklist' }, _stageId: { type: Schema.Types.ObjectId, ref: 'Stage', default: null }, tagIds: [ { type: Schema.Types.ObjectId, ref: 'Tag' } ], customfields: [customfieldValue], ancestorIds: [ { type: Schema.Types.ObjectId, ref: 'Task' } ], workTime: { totalTime: { type: Number, default: 0, min: 0 }, usedTime: { type: Number, default: 0, min: 0 }, unit: { type: String, default: 'minute' } }, storyPoint: { type: String, default: null }, progress: { type: Number, default: 0 }, rating: { type: Number, default: 0, enum: [0, 1, 2, 3, 4, 5] }, _scenariofieldconfigId: { type: Schema.Types.ObjectId, ref: 'ScenarioFieldConfig' }, _sprintId: { type: Schema.Types.ObjectId, ref: 'Sprint' }, _taskflowstatusId: { type: Schema.Types.ObjectId, ref: 'Taskflowstatus' }, badges: { commentsCount: { type: Number, default: 0 }, attachmentsCount: { type: Number, default: 0 }, likesCount: { type: Number, default: 0 }, objectlinksCount: { type: Number, default: 0 } } }) export const TaskModel = db.model('Task', task)