forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix materializeTask logic to work better and improve retrieveTasks an…
…d delayTasks logic b00tc4mp#426
- Loading branch information
Showing
16 changed files
with
719 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
staff/miguel-arias/project/api/logic/retrieveTasks.2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { validate, errors } from 'com' | ||
const { SystemError, NotFoundError } = errors | ||
|
||
import { addDay, weekStart, weekEnd, dayEnd } from '@formkit/tempo' | ||
|
||
import { Home, Task } from '../data/models.js' | ||
|
||
function retrieveTasks(homeId, week) { | ||
validate.id(homeId, 'home id') | ||
|
||
return (async () => { | ||
|
||
let home | ||
try { | ||
home = await Home.findById(homeId).lean() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!home) | ||
throw new NotFoundError('home not found') | ||
|
||
const referenceDate = addDay(new Date(), week * 7) | ||
|
||
const startOfCurrentWeek = weekStart(referenceDate, 1) | ||
|
||
const endOfCurrentWeek = weekEnd(referenceDate, 1) | ||
|
||
// traer todas las tareas | ||
let tasks | ||
try { | ||
tasks = await Task.find({ home: homeId }).populate('template', '-__v').select('-__v').sort({ date: 1 }).lean() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
let tasksAndEchoes = [] | ||
|
||
tasks.forEach(task => { | ||
if (task.done === true) { | ||
if (task.date >= startOfCurrentWeek && task.date <= endOfCurrentWeek) { | ||
tasksAndEchoes.push({ ...task }) | ||
} | ||
} | ||
|
||
if (task.done === false) { | ||
const existingEcho = tasksAndEchoes.find(echo => echo._id ? echo._id === task.oldId : null) | ||
|
||
if (existingEcho) { | ||
const index = tasksAndEchoes.indexOf(existingEcho) | ||
tasksAndEchoes.splice(index, 1) | ||
} | ||
|
||
if (task.date >= startOfCurrentWeek && task.date <= endOfCurrentWeek) | ||
tasksAndEchoes.push({ ...task }) | ||
|
||
let currentDate = new Date(task.date) | ||
let idCounter = 0 | ||
|
||
while (currentDate <= endOfCurrentWeek) { | ||
currentDate = addDay(currentDate, task.template.periodicity) | ||
if (currentDate >= startOfCurrentWeek && currentDate <= endOfCurrentWeek) { | ||
const newTask = { ...task, date: new Date(currentDate), assignee: '', _id: task._id + '_' + idCounter, done: false } | ||
|
||
if (newTask.date >= task.date) { | ||
tasksAndEchoes.push(newTask) | ||
idCounter++ | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
// de cada tarea quiero que se haga un date = date x periodicity mientras date <= endOfCurrentWeek | ||
// si task.date >= startOfCurrentWeek && task.date <= endOfCurrentWeek, la pusheo en un nuevo array | ||
// devuelvo este nuevo array | ||
|
||
/* let tasks | ||
try { | ||
tasks = await Task.find({ home: homeId, date: { $gte: startOfCurrentWeek, $lte: endOfCurrentWeek } }).populate('template', '-__v').select('-__v').sort({ date: 1 }).lean() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} */ | ||
|
||
tasksAndEchoes.forEach(task => { | ||
task.id = task._id.toString() | ||
delete task._id | ||
|
||
task.date = dayEnd(task.date) | ||
}) | ||
|
||
tasksAndEchoes.sort((a, b) => a.date - b.date) | ||
|
||
/* let duplicatedTasks = tasks.map(task => ({ ...task, date: new Date(task.date) })) | ||
let newTasks = [] | ||
duplicatedTasks.forEach(task => { | ||
task.date = addDay(new Date(task.date), task.template.periodicity) | ||
task.assignee = '' | ||
//if (task.date >= startOfCurrentWeek && task.date <= endOfCurrentWeek) | ||
newTasks.push(task) | ||
}) | ||
const allTasks = [...tasks, ...newTasks].sort((a, b) => a.date - b.date); | ||
return allTasks */ | ||
|
||
return tasksAndEchoes | ||
})() | ||
} | ||
|
||
export default retrieveTasks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.