2022-10-19 13:18:34 +00:00
|
|
|
import {PRIORITIES, type Priority} from '@/constants/priorities'
|
|
|
|
import {SECONDS_A_DAY, SECONDS_A_HOUR, SECONDS_A_MONTH, SECONDS_A_WEEK, SECONDS_A_YEAR} from '@/constants/date'
|
2022-07-20 22:42:36 +00:00
|
|
|
|
2022-08-04 18:57:43 +00:00
|
|
|
import type {ITask} from '@/modelTypes/ITask'
|
|
|
|
import type {ILabel} from '@/modelTypes/ILabel'
|
|
|
|
import type {IUser} from '@/modelTypes/IUser'
|
|
|
|
import type {IAttachment} from '@/modelTypes/IAttachment'
|
2022-11-13 21:04:57 +00:00
|
|
|
import type {IProject} from '@/modelTypes/IProject'
|
2022-08-04 18:57:43 +00:00
|
|
|
import type {ISubscription} from '@/modelTypes/ISubscription'
|
|
|
|
import type {IBucket} from '@/modelTypes/IBucket'
|
|
|
|
|
|
|
|
import type {IRepeatAfter} from '@/types/IRepeatAfter'
|
2022-10-19 13:18:34 +00:00
|
|
|
import type {IRelationKind} from '@/types/IRelationKind'
|
2022-08-04 18:57:43 +00:00
|
|
|
import {TASK_REPEAT_MODES, type IRepeatMode} from '@/types/IRepeatMode'
|
2021-04-14 08:24:07 +00:00
|
|
|
|
2021-02-21 15:13:58 +00:00
|
|
|
import {parseDateOrNull} from '@/helpers/parseDateOrNull'
|
2022-08-04 18:57:43 +00:00
|
|
|
|
|
|
|
import AbstractModel from './abstractModel'
|
|
|
|
import LabelModel from './label'
|
|
|
|
import UserModel from './user'
|
|
|
|
import AttachmentModel from './attachment'
|
|
|
|
import SubscriptionModel from './subscription'
|
2023-03-11 18:20:07 +00:00
|
|
|
import type {ITaskReminder} from '@/modelTypes/ITaskReminder'
|
|
|
|
import TaskReminderModel from '@/models/taskReminder'
|
2023-06-09 11:19:47 +00:00
|
|
|
import {periodToSeconds, secondsToPeriod} from '@/helpers/time/period'
|
2020-12-08 14:43:51 +00:00
|
|
|
|
2022-06-16 16:11:42 +00:00
|
|
|
export const TASK_DEFAULT_COLOR = '#1973ff'
|
2021-11-22 19:03:27 +00:00
|
|
|
|
2022-10-02 10:28:57 +00:00
|
|
|
export function getHexColor(hexColor: string): string {
|
2022-08-04 18:57:43 +00:00
|
|
|
if (hexColor === '' || hexColor === '#') {
|
|
|
|
return TASK_DEFAULT_COLOR
|
|
|
|
}
|
|
|
|
|
|
|
|
return hexColor
|
2022-07-20 22:42:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 13:18:34 +00:00
|
|
|
/**
|
|
|
|
* Parses `repeatAfterSeconds` into a usable js object.
|
|
|
|
*/
|
|
|
|
export function parseRepeatAfter(repeatAfterSeconds: number): IRepeatAfter {
|
2023-06-09 11:19:47 +00:00
|
|
|
|
|
|
|
const period = secondsToPeriod(repeatAfterSeconds)
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: period.unit,
|
|
|
|
amount: period.amount,
|
2022-10-19 13:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 09:36:01 +00:00
|
|
|
export default class TaskModel extends AbstractModel<ITask> implements ITask {
|
2022-08-14 10:15:45 +00:00
|
|
|
id = 0
|
|
|
|
title = ''
|
|
|
|
description = ''
|
|
|
|
done = false
|
|
|
|
doneAt: Date | null = null
|
|
|
|
priority: Priority = PRIORITIES.UNSET
|
|
|
|
labels: ILabel[] = []
|
|
|
|
assignees: IUser[] = []
|
|
|
|
|
|
|
|
dueDate: Date | null = 0
|
|
|
|
startDate: Date | null = 0
|
|
|
|
endDate: Date | null = 0
|
2022-08-04 18:57:43 +00:00
|
|
|
repeatAfter: number | IRepeatAfter = 0
|
2022-08-14 10:15:45 +00:00
|
|
|
repeatFromCurrentDate = false
|
2022-08-04 18:57:43 +00:00
|
|
|
repeatMode: IRepeatMode = TASK_REPEAT_MODES.REPEAT_MODE_DEFAULT
|
2023-03-28 05:51:46 +00:00
|
|
|
/* Make sure to not return reminderDates to the server.
|
|
|
|
The server currently supports both reminderDates (old API) and reminder (new API) and assumes the old logic
|
|
|
|
if it still receives reminderDates.
|
|
|
|
This line and reminderDates attributes will be removed after https://kolaente.dev/vikunja/api/pulls/1448 was merged.
|
|
|
|
*/
|
2023-03-11 18:20:07 +00:00
|
|
|
reminderDates = null
|
|
|
|
reminders: ITaskReminder[] = []
|
2022-08-14 10:15:45 +00:00
|
|
|
parentTaskId: ITask['id'] = 0
|
|
|
|
hexColor = ''
|
|
|
|
percentDone = 0
|
2022-09-15 10:52:38 +00:00
|
|
|
relatedTasks: Partial<Record<IRelationKind, ITask[]>> = {}
|
2022-08-14 10:15:45 +00:00
|
|
|
attachments: IAttachment[] = []
|
2022-10-17 11:14:07 +00:00
|
|
|
coverImageAttachmentId: IAttachment['id'] = null
|
2022-08-14 10:15:45 +00:00
|
|
|
identifier = ''
|
|
|
|
index = 0
|
|
|
|
isFavorite = false
|
|
|
|
subscription: ISubscription = null
|
2022-11-01 13:27:35 +00:00
|
|
|
coverImageAttachmentId: IAttachment['id'] = null
|
2022-08-14 10:15:45 +00:00
|
|
|
|
|
|
|
position = 0
|
|
|
|
kanbanPosition = 0
|
|
|
|
|
|
|
|
createdBy: IUser = UserModel
|
|
|
|
created: Date = null
|
|
|
|
updated: Date = null
|
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
projectId: IProject['id'] = 0
|
2022-08-14 10:15:45 +00:00
|
|
|
bucketId: IBucket['id'] = 0
|
2022-06-23 01:22:21 +00:00
|
|
|
|
2022-09-15 10:52:38 +00:00
|
|
|
constructor(data: Partial<ITask> = {}) {
|
2022-08-14 10:15:45 +00:00
|
|
|
super()
|
|
|
|
this.assignData(data)
|
2019-11-24 13:16:24 +00:00
|
|
|
|
|
|
|
this.id = Number(this.id)
|
2021-09-10 12:57:59 +00:00
|
|
|
this.title = this.title?.trim()
|
2022-02-13 18:57:12 +00:00
|
|
|
this.doneAt = parseDateOrNull(this.doneAt)
|
|
|
|
|
|
|
|
this.labels = this.labels
|
|
|
|
.map(l => new LabelModel(l))
|
|
|
|
.sort((f, s) => f.title > s.title ? 1 : -1)
|
|
|
|
|
|
|
|
// Parse the assignees into user models
|
|
|
|
this.assignees = this.assignees.map(a => {
|
|
|
|
return new UserModel(a)
|
|
|
|
})
|
|
|
|
|
2021-02-21 15:13:58 +00:00
|
|
|
this.dueDate = parseDateOrNull(this.dueDate)
|
|
|
|
this.startDate = parseDateOrNull(this.startDate)
|
|
|
|
this.endDate = parseDateOrNull(this.endDate)
|
2019-03-02 10:25:10 +00:00
|
|
|
|
2022-02-13 18:57:12 +00:00
|
|
|
// Parse the repeat after into something usable
|
2022-10-19 13:18:34 +00:00
|
|
|
this.repeatAfter = parseRepeatAfter(this.repeatAfter as number)
|
2022-02-13 18:57:12 +00:00
|
|
|
|
2023-03-11 18:20:07 +00:00
|
|
|
this.reminders = this.reminders.map(r => new TaskReminderModel(r))
|
2022-02-13 18:57:12 +00:00
|
|
|
|
2021-04-18 17:16:53 +00:00
|
|
|
if (this.hexColor !== '' && this.hexColor.substring(0, 1) !== '#') {
|
2019-04-30 20:18:06 +00:00
|
|
|
this.hexColor = '#' + this.hexColor
|
|
|
|
}
|
2019-10-28 21:45:37 +00:00
|
|
|
|
2022-09-15 10:52:38 +00:00
|
|
|
// Convert all subtasks to task models
|
2020-06-27 17:04:30 +00:00
|
|
|
Object.keys(this.relatedTasks).forEach(relationKind => {
|
2020-04-12 21:54:46 +00:00
|
|
|
this.relatedTasks[relationKind] = this.relatedTasks[relationKind].map(t => {
|
2019-10-28 21:45:37 +00:00
|
|
|
return new TaskModel(t)
|
|
|
|
})
|
|
|
|
})
|
2019-11-24 13:16:24 +00:00
|
|
|
|
|
|
|
// Make all attachments to attachment models
|
2022-02-13 18:57:12 +00:00
|
|
|
this.attachments = this.attachments.map(a => new AttachmentModel(a))
|
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
// Set the task identifier to empty if the project does not have one
|
2020-06-27 17:04:30 +00:00
|
|
|
if (this.identifier === `-${this.index}`) {
|
2020-05-16 11:14:57 +00:00
|
|
|
this.identifier = ''
|
|
|
|
}
|
|
|
|
|
2021-02-21 15:13:58 +00:00
|
|
|
if (typeof this.subscription !== 'undefined' && this.subscription !== null) {
|
2021-02-14 19:18:51 +00:00
|
|
|
this.subscription = new SubscriptionModel(this.subscription)
|
|
|
|
}
|
|
|
|
|
2022-02-13 18:57:12 +00:00
|
|
|
this.createdBy = new UserModel(this.createdBy)
|
2020-02-08 13:16:06 +00:00
|
|
|
this.created = new Date(this.created)
|
|
|
|
this.updated = new Date(this.updated)
|
2022-02-13 18:57:12 +00:00
|
|
|
|
2022-11-13 21:04:57 +00:00
|
|
|
this.projectId = Number(this.projectId)
|
2019-03-02 10:25:10 +00:00
|
|
|
}
|
2020-06-27 17:04:30 +00:00
|
|
|
|
2021-02-21 15:13:58 +00:00
|
|
|
getTextIdentifier() {
|
2021-04-14 08:24:07 +00:00
|
|
|
if (this.identifier === '') {
|
2021-02-21 15:13:58 +00:00
|
|
|
return `#${this.index}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.identifier
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:16:53 +00:00
|
|
|
getHexColor() {
|
2022-08-04 18:57:43 +00:00
|
|
|
return getHexColor(this.hexColor)
|
2021-04-18 17:16:53 +00:00
|
|
|
}
|
2020-04-04 16:26:35 +00:00
|
|
|
}
|
|
|
|
|