-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtask-repeat-cfg.effects.ts
483 lines (447 loc) · 18.1 KB
/
task-repeat-cfg.effects.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import {
concatMap,
delay,
filter,
first,
map,
mergeMap,
take,
tap,
withLatestFrom,
} from 'rxjs/operators';
import { Action, select, Store } from '@ngrx/store';
import {
addTaskRepeatCfgToTask,
deleteTaskRepeatCfg,
deleteTaskRepeatCfgs,
updateTaskRepeatCfg,
updateTaskRepeatCfgs,
upsertTaskRepeatCfg,
} from './task-repeat-cfg.actions';
import { selectTaskRepeatCfgFeatureState } from './task-repeat-cfg.reducer';
import { PersistenceService } from '../../../core/persistence/persistence.service';
import { Task, TaskArchive, TaskCopy } from '../../tasks/task.model';
import { addSubTask, updateTask } from '../../tasks/store/task.actions';
import { TaskService } from '../../tasks/task.service';
import { TaskRepeatCfgService } from '../task-repeat-cfg.service';
import {
DEFAULT_TASK_REPEAT_CFG,
TaskRepeatCfg,
TaskRepeatCfgCopy,
TaskRepeatCfgState,
} from '../task-repeat-cfg.model';
import { Observable, forkJoin, from, merge, of } from 'rxjs';
import { setActiveWorkContext } from '../../work-context/store/work-context.actions';
import { SyncTriggerService } from '../../../imex/sync/sync-trigger.service';
import { SyncProviderService } from '../../../imex/sync/sync-provider.service';
import { sortRepeatableTaskCfgs } from '../sort-repeatable-task-cfg';
import { MatDialog } from '@angular/material/dialog';
import { DialogConfirmComponent } from '../../../ui/dialog-confirm/dialog-confirm.component';
import { T } from '../../../t.const';
import { Update } from '@ngrx/entity';
import { getDateTimeFromClockString } from '../../../util/get-date-time-from-clock-string';
import { isToday } from '../../../util/is-today.util';
import { DateService } from 'src/app/core/date/date.service';
import { getWorklogStr } from 'src/app/util/get-work-log-str';
import { getTaskById } from '../../tasks/store/task.reducer.util';
@Injectable()
export class TaskRepeatCfgEffects {
updateTaskRepeatCfgs$: any = createEffect(
() =>
this._actions$.pipe(
ofType(
addTaskRepeatCfgToTask,
updateTaskRepeatCfg,
updateTaskRepeatCfgs,
upsertTaskRepeatCfg,
deleteTaskRepeatCfg,
deleteTaskRepeatCfgs,
),
withLatestFrom(this._store$.pipe(select(selectTaskRepeatCfgFeatureState))),
tap(this._saveToLs.bind(this)),
),
{ dispatch: false },
);
/**
* Updates the repeatCfg of a task, if the task was updated.
*/
updateRepeatCfgWhenTaskUpdates: any = createEffect(
() =>
this._actions$.pipe(
ofType(updateTask),
tap(async (aAction) => {
const allTasks = await this._taskService.allTasks$.pipe(first()).toPromise();
const task = allTasks.find((aTask) => aTask.id === aAction.task.id)!;
if (task.repeatCfgId !== null) {
const repeatCfgForTask = await this._taskRepeatCfgService
.getTaskRepeatCfgById$(task.repeatCfgId)
.pipe(first())
.toPromise();
const taskChanges = aAction.task.changes;
// TODO: is there a better way to do this? Is there anything missing?
const repeatCfgChanges: Partial<TaskRepeatCfgCopy> = {
projectId: taskChanges.projectId ?? repeatCfgForTask.projectId,
title: taskChanges.title ?? repeatCfgForTask.title,
tagIds: taskChanges.tagIds ?? repeatCfgForTask.tagIds,
notes: taskChanges.notes ?? repeatCfgForTask.notes,
};
// TODO: Do we need to do this for all instances??
this._taskRepeatCfgService.updateTaskRepeatCfg(
task.repeatCfgId,
repeatCfgChanges,
);
}
}),
),
{ dispatch: false }, // Question: What exactly does this do?
);
/**
* When a main task is made repeatable, this function checks if there are subtasks.
* If that is the case, a repeat-cfg gets added for each subtask, too.
*/
addTaskRepeatCfgForSubTasksOf: any = createEffect(
() =>
this._actions$.pipe(
ofType(addTaskRepeatCfgToTask),
tap(async (aAction) => {
// TODO: is there an easier way to get to the parent task?
const allTasks = await this._taskService.allTasks$.pipe(first()).toPromise();
const parentTask = allTasks.find((aTask) => aTask.id === aAction.taskId);
const parentTaskRepeatCfg = aAction.taskRepeatCfg;
if (parentTask !== undefined && parentTask.subTaskIds.length > 0) {
for (const aSubTaskId of parentTask.subTaskIds) {
const task = allTasks.find((aTask) => aTask.id === aSubTaskId)!;
const repeatCfg = {
...parentTaskRepeatCfg,
// TODO: anything missing in this list that should not be overwritten by the parent?
title: task.title,
notes: task.notes,
defaultEstimate: task.timeEstimate, // is this correct?
parentId: parentTask.repeatCfgId,
};
this._taskRepeatCfgService.addTaskRepeatCfgToTask(
task.id,
task.projectId,
repeatCfg,
);
}
}
}),
),
{ dispatch: false }, // Question: What exactly does this do?
);
/**
* When adding a sub task, this function checks if the parent is a repeatable task and therefore the sub-task also has to be.
* If that is the case, a repeat-cfg gets added for each subtask, too.
*/
addTaskRepeatCfgForSubTask: any = createEffect(
() =>
this._actions$.pipe(
ofType(addSubTask),
tap(async (aAction) => {
const task = aAction.task;
// we only want to continue if the task doesn't already have a repeatCfgId
if (task.repeatCfgId === null) {
console.log('A TASKKK', task);
// TODO: is there an easier way to get to the parent task?
const allTasks = await this._taskService.allTasks$.pipe(first()).toPromise();
const parentTask = allTasks.find((aTask) => aTask.id === aAction.parentId);
console.log('PARENT TASK', parentTask);
if (parentTask !== undefined && parentTask.repeatCfgId !== null) {
const parentRepeatCfg = await this._taskRepeatCfgService
.getTaskRepeatCfgById$(parentTask.repeatCfgId)
.pipe(first())
.toPromise();
const repeatCfg = {
...parentRepeatCfg,
// TODO: anything missing in this list that should not be overwritten by the parent?
title: task.title,
notes: task.notes || undefined,
defaultEstimate: task.timeEstimate, // is this correct?
parentId: parentRepeatCfg.id,
};
this._taskRepeatCfgService.addTaskRepeatCfgToTask(
task.id,
task.projectId,
repeatCfg,
);
}
}
}),
),
{ dispatch: false }, // Question: What exactly does this do?
);
private triggerRepeatableTaskCreation$ = merge(
this._syncTriggerService.afterInitialSyncDoneAndDataLoadedInitially$,
this._actions$.pipe(
ofType(setActiveWorkContext),
concatMap(() => this._syncProviderService.afterCurrentSyncDoneOrSyncDisabled$),
),
).pipe(
// make sure everything has settled
delay(1000),
);
createRepeatableTasks: any = createEffect(() =>
this.triggerRepeatableTaskCreation$.pipe(
concatMap(
() =>
this._taskRepeatCfgService
.getRepeatTableTasksDueForDay$(
Date.now() - this._dateService.startOfNextDayDiff,
)
.pipe(first()),
// ===> taskRepeatCfgs scheduled for today and not yet created already
),
filter((taskRepeatCfgs) => taskRepeatCfgs && !!taskRepeatCfgs.length),
withLatestFrom(this._taskService.currentTaskId$),
// existing tasks with sub tasks are loaded, because need to move them to the archive
mergeMap(([taskRepeatCfgs, currentTaskId]) => {
// we only want to work with parent tasks, so filter out sub tasks
const parentTasksRepeatCfgs = taskRepeatCfgs.filter(
(aTask) => aTask.parentId === null,
);
// NOTE sorting here is important
const sorted = parentTasksRepeatCfgs.sort(sortRepeatableTaskCfgs);
return from(sorted).pipe(
mergeMap((taskRepeatCfg: TaskRepeatCfg) =>
this._taskRepeatCfgService.getActionsForTaskRepeatCfg(
taskRepeatCfg,
currentTaskId,
Date.now() - this._dateService.startOfNextDayDiff,
),
),
concatMap((actionsForRepeatCfg) => from(actionsForRepeatCfg)),
);
}),
),
);
removeConfigIdFromTaskStateTasks$: any = createEffect(() =>
this._actions$.pipe(
ofType(deleteTaskRepeatCfg),
concatMap(({ id }) => this._taskService.getTasksByRepeatCfgId$(id).pipe(take(1))),
filter((tasks) => tasks && !!tasks.length),
concatMap((value: TaskCopy[], index) => {
const tasks: Readonly<TaskCopy>[] = value;
const allSubIds = tasks.flatMap((aTask) => aTask.subTaskIds);
return this._taskService
.getByIdsLive$(allSubIds)
.pipe(map((aAllSubTasks: TaskCopy[]) => ({ aAllSubTasks, tasks })));
}),
mergeMap(({ aAllSubTasks, tasks }) => {
return [...aAllSubTasks, ...tasks].map((task) =>
updateTask({
task: {
id: task.id,
changes: { repeatCfgId: null },
},
}),
);
}),
),
);
removeConfigIdFromTaskArchiveTasks$: any = createEffect(
() =>
this._actions$.pipe(
ofType(deleteTaskRepeatCfg),
tap(async ({ id }) => {
const subTasks = await this._taskRepeatCfgService
.getTaskRepeatCfgsByParentId$(id)
.pipe(first())
.toPromise();
if (subTasks.length > 0) {
const subTaskIds = subTasks.map((aTask) => aTask.id);
// remove repeat cfgs from sub tasks
for (const aId of subTaskIds) {
this._removeRepeatCfgFromArchiveTasks(aId);
}
}
// remove repeat cfg from main task
this._removeRepeatCfgFromArchiveTasks(id);
}),
),
{ dispatch: false },
);
checkToUpdateAllTaskInstances: any = createEffect(
() =>
this._actions$.pipe(
ofType(updateTaskRepeatCfg),
filter(({ isAskToUpdateAllTaskInstances }) => !!isAskToUpdateAllTaskInstances),
concatMap(({ taskRepeatCfg }) => {
const id = taskRepeatCfg.id as string;
return forkJoin([
of(taskRepeatCfg),
this._taskService.getTasksByRepeatCfgId$(id).pipe(first()),
this._taskService.getArchiveTasksForRepeatCfgId(id),
]);
}),
concatMap(([{ id, changes }, todayTasks, archiveTasks]) => {
if (todayTasks.length + archiveTasks.length === 0) {
return of(false);
}
// NOTE: there will always be at least on instance, since we're editing it
return this._matDialog
.open(DialogConfirmComponent, {
restoreFocus: true,
data: {
okTxt: T.F.TASK_REPEAT.D_CONFIRM_UPDATE_INSTANCES.OK,
cancelTxt: T.F.TASK_REPEAT.D_CONFIRM_UPDATE_INSTANCES.CANCEL,
message: T.F.TASK_REPEAT.D_CONFIRM_UPDATE_INSTANCES.MSG,
translateParams: { tasksNr: todayTasks.length + archiveTasks.length },
},
})
.afterClosed()
.pipe(
withLatestFrom(
this._taskRepeatCfgService.getTaskRepeatCfgById$(id as string),
),
tap(([isConfirm, completeCfg]) => {
if (isConfirm) {
console.log(changes);
console.log(todayTasks, archiveTasks);
// NOTE: keep in mind that it's very likely that there will be only one task for today
// TODO update reminders if given
todayTasks.forEach((task) => {
// NOTE: projects can't be updated from the dialog,
// if (
// typeof changes.projectId === 'string' &&
// task.projectId !== changes.projectId
// ) {
// const withSubTasks = await this._taskService
// .getByIdWithSubTaskData$(task.id)
// .pipe(take(1))
// .toPromise();
// this._taskService.moveToProject(withSubTasks, changes.projectId);
// }
if (
(changes.startTime || changes.remindAt) &&
completeCfg.remindAt &&
completeCfg.startTime &&
isToday(task.created)
) {
const dateTime = getDateTimeFromClockString(
completeCfg.startTime as string,
new Date(),
);
if (task.reminderId) {
this._taskService.reScheduleTask({
task,
plannedAt: dateTime,
remindCfg: completeCfg.remindAt,
isMoveToBacklog: false,
});
} else {
this._taskService.scheduleTask(
task,
dateTime,
completeCfg.remindAt,
);
}
}
if (changes.tagIds) {
this._taskService.updateTags(task, changes.tagIds, task.tagIds);
}
if (changes.title || changes.notes) {
this._taskService.update(task.id, {
...(changes.title
? {
title: changes.title,
}
: {}),
...(changes.notes
? {
notes: changes.notes,
}
: {}),
});
}
if (
typeof changes.defaultEstimate === 'number' &&
task.subTaskIds.length === 0
) {
this._taskService.update(task.id, {
timeEstimate: changes.defaultEstimate,
});
}
});
const archiveUpdates: Update<TaskCopy>[] = archiveTasks.map((task) => {
const changesForArchiveTask: Partial<TaskCopy> = {};
// NOTE: projects can't be updated from the dialog
// if (
// typeof changes.projectId === 'string' &&
// task.projectId !== changes.projectId
// ) {
// changesForArchiveTask.projectId = changes.projectId;
// // TODO also update sub tasks
// if (task.subTaskIds) {
// task.subTaskIds.forEach((subTaskId) => {
// this._taskService.updateArchiveTask(
// subTaskId,
// changesForArchiveTask,
// );
// });
// }
// }
if (changes.tagIds) {
changesForArchiveTask.tagIds = changes.tagIds;
}
if (changes.title) {
changesForArchiveTask.title = changes.title;
}
if (changes.notes) {
changesForArchiveTask.notes = changes.notes;
}
if (
typeof changes.defaultEstimate === 'number' &&
task.subTaskIds.length === 0
) {
changesForArchiveTask.timeEstimate = changes.defaultEstimate;
}
console.log('updateArchiveTask', changesForArchiveTask);
return { id: task.id, changes: changesForArchiveTask };
});
this._taskService.updateArchiveTasks(archiveUpdates);
}
}),
);
}),
),
{ dispatch: false },
);
constructor(
private _actions$: Actions,
private _taskService: TaskService,
private _store$: Store<any>,
private _persistenceService: PersistenceService,
private _dateService: DateService,
private _taskRepeatCfgService: TaskRepeatCfgService,
private _syncTriggerService: SyncTriggerService,
private _syncProviderService: SyncProviderService,
private _matDialog: MatDialog,
) {}
private _saveToLs([action, taskRepeatCfgState]: [Action, TaskRepeatCfgState]): void {
this._persistenceService.taskRepeatCfg.saveState(taskRepeatCfgState, {
isSyncModelChange: true,
});
}
private _removeRepeatCfgFromArchiveTasks(repeatConfigId: string): void {
this._persistenceService.taskArchive.loadState().then((taskArchive: TaskArchive) => {
// if not yet initialized for project
if (!taskArchive) {
return;
}
const newState = { ...taskArchive };
const ids = newState.ids as string[];
const tasksWithRepeatCfgId = ids
.map((id) => newState.entities[id] as Task)
.filter((task) => task.repeatCfgId === repeatConfigId);
if (tasksWithRepeatCfgId && tasksWithRepeatCfgId.length) {
tasksWithRepeatCfgId.forEach((task: any) => (task.repeatCfgId = null));
this._persistenceService.taskArchive.saveState(newState, {
isSyncModelChange: true,
});
}
});
}
}