|
1 | 1 | import { PouchDBCollection } from './PouchDBCollection.js' |
2 | | -import { findLast, sortBy } from 'lodash' |
| 2 | +import { findLast, sortBy, dropWhile } from 'lodash' |
3 | 3 | import { DateTime } from 'luxon' |
4 | 4 | import { toDateTime } from '@/helpers/dates.js' |
5 | 5 |
|
@@ -34,53 +34,34 @@ export class EventsCollection extends PouchDBCollection { |
34 | 34 | * @param {any} start |
35 | 35 | * @param {any} end |
36 | 36 | */ |
37 | | - async getInterval(userId, start, end) { |
| 37 | + async getInterval(userId, start, end, { groupEvents = true } = {}) { |
38 | 38 | start = toDateTime(start) |
39 | 39 | end = toDateTime(end) |
| 40 | + |
| 41 | + // Charge les évènements jusqu'à 7 jours avant le début de la plage |
40 | 42 | let events = await this.findUserEvents(userId, start.minus({ days: 7 }), end) |
41 | 43 |
|
42 | 44 | if (!events.length) { |
43 | 45 | return [] |
44 | 46 | } |
45 | 47 |
|
46 | | - const startISO = start.toISO() |
47 | | - const endISO = end.toISO() |
| 48 | + // Les évènements sont triés par date croissante avec les rotations avant les SV |
| 49 | + events = dropWhile(events, evt => toDateTime(evt.end) < start) |
48 | 50 |
|
49 | | - // possibilité d'optimiser en utilisant une boucle s'arrêtant dès que evt.start >= start |
50 | | - let hasOverlap |
51 | | - events = events.filter(evt => { |
52 | | - if (evt.tag === 'rotation' && evt.start < startISO && evt.end >= startISO) { |
53 | | - hasOverlap = evt |
54 | | - // console.log('isRotation before') |
55 | | - return true |
56 | | - } |
57 | | - if (evt.end >= startISO) { |
58 | | - // console.log('end after start') |
59 | | - return true |
60 | | - } |
61 | | - if (hasOverlap) { |
62 | | - // console.log('is duty in before start') |
63 | | - return evt?.rotationId === hasOverlap._id |
64 | | - } |
65 | | - return false |
66 | | - }) |
67 | | - |
68 | | - const hasOverlapEnd = findLast(events, evt => evt.tag === 'rotation' && evt.end > endISO) |
69 | | - if (hasOverlapEnd) { // Ajouter les SV manquants |
70 | | - const lastSvs = await this.findUserEvents(userId, end, hasOverlapEnd.end, { inclusive_start: false }) |
71 | | - .filter(evt => { |
72 | | - return evt.rotationId === hasOverlapEnd._id |
73 | | - }) |
| 51 | + const rotationOverlappingEnd = findLast(events, evt => evt.tag === 'rotation' && toDateTime(evt.end) > end) |
| 52 | + if (rotationOverlappingEnd) { // Ajouter les SV manquants |
| 53 | + const lastSvs = (await this.findUserEvents(userId, end, rotationOverlappingEnd.end, { inclusive_start: false })) |
| 54 | + .filter(evt => evt.rotationId === rotationOverlappingEnd._id) |
74 | 55 | .toArray() |
75 | | - if (lastSvs && lastSvs.length) { |
| 56 | + if (lastSvs?.length) { |
76 | 57 | events.push(...lastSvs) |
77 | 58 | } |
78 | 59 | } |
79 | 60 |
|
80 | | - const groupedEvents = this.constructor.groupEvents(events) |
81 | | - console.log(`%cEvents.getInterval%c : ${userId}`, 'color:teal', 'color:inherit', groupedEvents, hasOverlap, hasOverlapEnd) |
| 61 | + events = groupEvents ? this.constructor.groupEvents(events) : events |
| 62 | + console.log(`%cEvents.getInterval%c : ${userId}-${start.toISO()}-${end.toISO()}`, 'color:teal', 'color:inherit', events) |
82 | 63 |
|
83 | | - return groupedEvents |
| 64 | + return events |
84 | 65 | } |
85 | 66 |
|
86 | 67 | // Liste les évènements dont le début est compris entre 'start' et 'end' |
|
0 commit comments