2019-03-02 11:25:10 +01:00
|
|
|
import AbstractService from './abstractService'
|
|
|
|
import TaskModel from '../models/task'
|
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'
|
2019-03-02 11:25:10 +01:00
|
|
|
|
|
|
|
export default class TaskService extends AbstractService {
|
|
|
|
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}',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
processModel(model) {
|
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-03-08 20:07:21 +01:00
|
|
|
model.dueDate = model.dueDate === null ? null : formatISO(new Date(model.dueDate))
|
|
|
|
model.startDate = model.startDate === null ? null : formatISO(new Date(model.startDate))
|
|
|
|
model.endDate = model.endDate === null ? null : formatISO(new Date(model.endDate))
|
2020-03-02 21:55:22 +01:00
|
|
|
model.created = formatISO(model.created)
|
|
|
|
model.updated = formatISO(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
|
2019-04-29 23:41:39 +02:00
|
|
|
if(model.reminderDates.length > 0) {
|
|
|
|
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
|
|
|
|
if (model.repeatAfter.amount !== null || model.repeatAfter.amount !== 0) {
|
|
|
|
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
|
|
|
|
|
2019-04-30 22:18:06 +02:00
|
|
|
if (model.hexColor.substring(0, 1) === '#') {
|
|
|
|
model.hexColor = model.hexColor.substring(1, 7)
|
|
|
|
}
|
|
|
|
|
2019-11-24 14:16:24 +01:00
|
|
|
// Do the same for all related tasks
|
2020-04-12 23:54:46 +02:00
|
|
|
Object.keys(model.relatedTasks).forEach(relationKind => {
|
|
|
|
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
|
|
|
|
if(model.attachments.length > 0) {
|
|
|
|
const attachmentService = new AttachmentService()
|
|
|
|
model.attachments.map(a => {
|
|
|
|
return attachmentService.processModel(a)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-26 01:11:34 +02:00
|
|
|
// Preprocess all labels
|
|
|
|
if(model.labels.length > 0) {
|
|
|
|
const labelService = new LabelService()
|
|
|
|
model.labels = model.labels.map(l => labelService.processModel(l))
|
|
|
|
}
|
|
|
|
|
2019-03-02 11:25:10 +01:00
|
|
|
return model
|
|
|
|
}
|
2020-04-04 18:26:35 +02:00
|
|
|
}
|
|
|
|
|