2019-03-02 11:25:10 +01:00
|
|
|
import AbstractService from './abstractService'
|
2022-09-06 11:36:01 +02:00
|
|
|
import TaskModel from '@/models/task'
|
|
|
|
import type {ITask} from '@/modelTypes/ITask'
|
2020-01-30 21:49:00 +01:00
|
|
|
import AttachmentService from './attachment'
|
2020-04-26 01:11:34 +02:00
|
|
|
import LabelService from './label'
|
|
|
|
|
2020-03-02 21:55:22 +01:00
|
|
|
import {formatISO} from 'date-fns'
|
2021-01-17 11:51:07 +01:00
|
|
|
import {colorFromHex} from '@/helpers/color/colorFromHex'
|
2019-03-02 11:25:10 +01:00
|
|
|
|
2020-12-08 15:43:51 +01:00
|
|
|
const parseDate = date => {
|
|
|
|
if (date) {
|
|
|
|
return formatISO(new Date(date))
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:56:31 +02:00
|
|
|
export default class TaskService extends AbstractService<ITask> {
|
2019-03-02 11:25:10 +01:00
|
|
|
constructor() {
|
|
|
|
super({
|
2020-04-12 23:54:46 +02:00
|
|
|
create: '/lists/{listId}',
|
2019-03-10 13:59:17 +01:00
|
|
|
getAll: '/tasks/all',
|
2019-11-24 14:16:24 +01:00
|
|
|
get: '/tasks/{id}',
|
2019-03-02 11:25:10 +01:00
|
|
|
update: '/tasks/{id}',
|
|
|
|
delete: '/tasks/{id}',
|
2020-09-05 22:35:52 +02:00
|
|
|
})
|
2019-03-02 11:25:10 +01:00
|
|
|
}
|
2020-06-21 20:27:39 +02:00
|
|
|
|
2019-03-02 11:25:10 +01:00
|
|
|
modelFactory(data) {
|
|
|
|
return new TaskModel(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeUpdate(model) {
|
2019-04-29 23:41:39 +02:00
|
|
|
return this.processModel(model)
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeCreate(model) {
|
|
|
|
return this.processModel(model)
|
|
|
|
}
|
|
|
|
|
2021-09-11 17:53:03 +02:00
|
|
|
processModel(updatedModel) {
|
|
|
|
const model = { ...updatedModel }
|
2020-04-12 23:54:46 +02:00
|
|
|
|
2021-09-10 14:57:59 +02:00
|
|
|
model.title = model.title?.trim()
|
|
|
|
|
2020-04-12 23:54:46 +02:00
|
|
|
// Ensure that listId is an int
|
|
|
|
model.listId = Number(model.listId)
|
2019-04-29 23:41:39 +02:00
|
|
|
|
2020-02-08 14:16:06 +01:00
|
|
|
// Convert dates into an iso string
|
2020-12-08 15:43:51 +01:00
|
|
|
model.dueDate = parseDate(model.dueDate)
|
|
|
|
model.startDate = parseDate(model.startDate)
|
|
|
|
model.endDate = parseDate(model.endDate)
|
|
|
|
model.doneAt = parseDate(model.doneAt)
|
2020-09-04 22:01:02 +02:00
|
|
|
model.created = formatISO(new Date(model.created))
|
|
|
|
model.updated = formatISO(new Date(model.updated))
|
2019-03-02 11:25:10 +01:00
|
|
|
|
|
|
|
// remove all nulls, these would create empty reminders
|
|
|
|
for (const index in model.reminderDates) {
|
|
|
|
if (model.reminderDates[index] === null) {
|
|
|
|
model.reminderDates.splice(index, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make normal timestamps from js dates
|
2020-06-21 20:27:39 +02:00
|
|
|
if (model.reminderDates.length > 0) {
|
2019-04-29 23:41:39 +02:00
|
|
|
model.reminderDates = model.reminderDates.map(r => {
|
2020-03-08 20:07:21 +01:00
|
|
|
return formatISO(new Date(r))
|
2019-04-29 23:41:39 +02:00
|
|
|
})
|
|
|
|
}
|
2019-03-02 11:25:10 +01:00
|
|
|
|
|
|
|
// Make the repeating amount to seconds
|
|
|
|
let repeatAfterSeconds = 0
|
2021-12-07 21:08:39 +01:00
|
|
|
if (model.repeatAfter !== null && (model.repeatAfter.amount !== null || model.repeatAfter.amount !== 0)) {
|
2019-03-02 11:25:10 +01:00
|
|
|
switch (model.repeatAfter.type) {
|
|
|
|
case 'hours':
|
|
|
|
repeatAfterSeconds = model.repeatAfter.amount * 60 * 60
|
|
|
|
break
|
|
|
|
case 'days':
|
|
|
|
repeatAfterSeconds = model.repeatAfter.amount * 60 * 60 * 24
|
|
|
|
break
|
|
|
|
case 'weeks':
|
|
|
|
repeatAfterSeconds = model.repeatAfter.amount * 60 * 60 * 24 * 7
|
|
|
|
break
|
|
|
|
case 'months':
|
|
|
|
repeatAfterSeconds = model.repeatAfter.amount * 60 * 60 * 24 * 30
|
|
|
|
break
|
|
|
|
case 'years':
|
|
|
|
repeatAfterSeconds = model.repeatAfter.amount * 60 * 60 * 24 * 365
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
model.repeatAfter = repeatAfterSeconds
|
|
|
|
|
2021-01-17 11:51:07 +01:00
|
|
|
model.hexColor = colorFromHex(model.hexColor)
|
2019-04-30 22:18:06 +02:00
|
|
|
|
2019-11-24 14:16:24 +01:00
|
|
|
// Do the same for all related tasks
|
2020-06-21 20:27:39 +02:00
|
|
|
Object.keys(model.relatedTasks).forEach(relationKind => {
|
2020-04-12 23:54:46 +02:00
|
|
|
model.relatedTasks[relationKind] = model.relatedTasks[relationKind].map(t => {
|
2019-11-24 14:16:24 +01:00
|
|
|
return this.processModel(t)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-01-30 21:49:00 +01:00
|
|
|
// Process all attachments to preven parsing errors
|
2020-06-21 20:27:39 +02:00
|
|
|
if (model.attachments.length > 0) {
|
2020-01-30 21:49:00 +01:00
|
|
|
const attachmentService = new AttachmentService()
|
|
|
|
model.attachments.map(a => {
|
|
|
|
return attachmentService.processModel(a)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-26 01:11:34 +02:00
|
|
|
// Preprocess all labels
|
2020-06-21 20:27:39 +02:00
|
|
|
if (model.labels.length > 0) {
|
2020-04-26 01:11:34 +02:00
|
|
|
const labelService = new LabelService()
|
|
|
|
model.labels = model.labels.map(l => labelService.processModel(l))
|
|
|
|
}
|
|
|
|
|
2022-07-21 00:42:36 +02:00
|
|
|
return model as ITask
|
2019-03-02 11:25:10 +01:00
|
|
|
}
|
2020-04-04 18:26:35 +02:00
|
|
|
}
|
|
|
|
|