|
1 | | -import { RRULE } from "@core/constants/core.constants"; |
| 1 | +import uniqby from "lodash.uniqby"; |
2 | 2 | import { Categories_Event, Schema_Event } from "@core/types/event.types"; |
3 | 3 | import dayjs, { Dayjs } from "@core/util/date/dayjs"; |
4 | 4 | import { |
@@ -32,63 +32,63 @@ export const getSomedayEventCategory = ( |
32 | 32 | return Categories_Event.SOMEDAY_WEEK; |
33 | 33 | }; |
34 | 34 |
|
| 35 | +export function eventsBetweenDates( |
| 36 | + events: Schema_SomedayEvent[], |
| 37 | + start: Dayjs, |
| 38 | + end: Dayjs, |
| 39 | +): Schema_SomedayEvent[] { |
| 40 | + return events.filter((event) => { |
| 41 | + return dayjs(event.startDate).isBetween(start, end, null, "[]"); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
35 | 45 | export const categorizeSomedayEvents = ( |
36 | | - somedayEvents: Schema_SomedayEventsColumn["events"], |
| 46 | + events: Schema_SomedayEventsColumn["events"], |
37 | 47 | weekDates: { start: Dayjs; end: Dayjs }, |
38 | 48 | ): Schema_SomedayEventsColumn => { |
39 | 49 | const { start: weekStart, end: weekEnd } = weekDates; |
40 | | - |
41 | | - let events = Object.values(somedayEvents) as Schema_SomedayEvent[]; |
42 | | - |
43 | | - events = validateSomedayEvents(events); |
44 | | - |
45 | | - const sortedEvents = events.sort((a, b) => a.order - b.order); |
46 | | - |
47 | | - const weekIds: string[] = []; |
48 | | - const monthIds: string[] = []; |
49 | | - |
50 | | - sortedEvents.forEach((e) => { |
51 | | - const eventStart = dayjs(e.startDate); |
52 | | - const eventEnd = dayjs(e.endDate); |
53 | | - const isWeek = |
54 | | - eventStart.isSameOrAfter(weekStart) && eventEnd.isSameOrBefore(weekEnd); |
55 | | - |
56 | | - if (isWeek) { |
57 | | - const isMonthRepeat = e?.recurrence?.rule?.includes(RRULE.MONTH); |
58 | | - if (!isMonthRepeat) { |
59 | | - weekIds.push(e._id!); |
60 | | - return; |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - const isFutureWeekThisMonth = e?.recurrence?.rule?.includes(RRULE.WEEK); |
65 | | - if (isFutureWeekThisMonth) { |
66 | | - return; |
67 | | - } |
68 | | - |
69 | | - const monthStart = weekStart.startOf("month"); |
70 | | - const monthEnd = weekStart.endOf("month"); |
71 | | - const isMonth = eventStart.isBetween(monthStart, monthEnd, null, "[]"); |
72 | | - |
73 | | - if (isMonth) { |
74 | | - monthIds.push(e._id!); |
75 | | - } |
76 | | - }); |
| 50 | + const monthStart = weekStart.startOf("month"); |
| 51 | + const monthEnd = weekStart.endOf("month"); |
| 52 | + const _events = Object.values(events) as Schema_SomedayEvent[]; |
| 53 | + const somedayEvents = validateSomedayEvents(_events); |
| 54 | + const _weekEvents = eventsBetweenDates(somedayEvents, weekStart, weekEnd); |
| 55 | + const _monthEvents = eventsBetweenDates(somedayEvents, monthStart, monthEnd); |
| 56 | + const weekEvents = uniqby(_weekEvents, (e) => e.recurrence?.eventId ?? e._id); |
| 57 | + |
| 58 | + const otherMonthEvents = _monthEvents.filter( |
| 59 | + ({ _id, recurrence }) => |
| 60 | + !weekEvents.some( |
| 61 | + (e) => |
| 62 | + e._id === _id || |
| 63 | + (typeof e.recurrence?.eventId === "string" && |
| 64 | + e.recurrence?.eventId === recurrence?.eventId), |
| 65 | + ), |
| 66 | + ); |
| 67 | + |
| 68 | + const monthEvents = uniqby( |
| 69 | + otherMonthEvents, |
| 70 | + (e) => e.recurrence?.eventId ?? e._id, |
| 71 | + ); |
77 | 72 |
|
78 | 73 | const sortedData = { |
79 | 74 | columns: { |
80 | 75 | [COLUMN_WEEK]: { |
81 | 76 | id: `${COLUMN_WEEK}`, |
82 | | - eventIds: weekIds, |
| 77 | + eventIds: weekEvents |
| 78 | + .sort((a, b) => a.order - b.order) |
| 79 | + .map((e) => e._id!), |
83 | 80 | }, |
84 | 81 | [COLUMN_MONTH]: { |
85 | 82 | id: `${COLUMN_MONTH}`, |
86 | | - eventIds: monthIds, |
| 83 | + eventIds: monthEvents |
| 84 | + .sort((a, b) => a.order - b.order) |
| 85 | + .map((e) => e._id!), |
87 | 86 | }, |
88 | 87 | }, |
89 | 88 | columnOrder: [COLUMN_WEEK, COLUMN_MONTH], |
90 | | - events: somedayEvents, |
| 89 | + events, |
91 | 90 | }; |
| 91 | + |
92 | 92 | return sortedData; |
93 | 93 | }; |
94 | 94 |
|
|
0 commit comments