@tmlmobilidade/dates 20260615.1002.3 → 20260615.1034.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,20 +5,33 @@ import { Dates } from '../../../dates.js';
|
|
|
5
5
|
* Checks if a rule's period criteria matches a specific calendar date.
|
|
6
6
|
* Verifies that at least one of the rule's period IDs is active on this date.
|
|
7
7
|
*/
|
|
8
|
-
function
|
|
8
|
+
function buildActivePeriodDateSet(rule, ctx) {
|
|
9
|
+
const activePeriodDates = new Set();
|
|
10
|
+
if (!rule.year_period_ids?.length)
|
|
11
|
+
return activePeriodDates;
|
|
12
|
+
for (const period of ctx.periods) {
|
|
13
|
+
if (!rule.year_period_ids.includes(period._id))
|
|
14
|
+
continue;
|
|
15
|
+
for (const date of period.dates ?? []) {
|
|
16
|
+
activePeriodDates.add(date);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return activePeriodDates;
|
|
20
|
+
}
|
|
21
|
+
function isInPeriod(rule, key, activePeriodDates) {
|
|
9
22
|
if (!rule.year_period_ids?.length)
|
|
10
23
|
return false;
|
|
11
24
|
const op = keyToYYYYMMDD(key);
|
|
12
|
-
return
|
|
25
|
+
return activePeriodDates.has(op);
|
|
13
26
|
}
|
|
14
27
|
/**
|
|
15
28
|
* Determines if a manual rule applies to a specific calendar date.
|
|
16
29
|
* Checks both period membership and weekday matching.
|
|
17
30
|
*/
|
|
18
|
-
function ruleAppliesToCivilKey(rule, key, ctx) {
|
|
31
|
+
function ruleAppliesToCivilKey(rule, key, ctx, activePeriodDates) {
|
|
19
32
|
const weekday = calendarWeekday(key, ctx.holidays);
|
|
20
33
|
// 1) YearPeriod required
|
|
21
|
-
if (!isInPeriod(rule, key,
|
|
34
|
+
if (!isInPeriod(rule, key, activePeriodDates))
|
|
22
35
|
return false;
|
|
23
36
|
// 2) Weekdays required
|
|
24
37
|
if (!rule.weekdays?.length)
|
|
@@ -52,6 +65,7 @@ export function getManualRuleAffectedDates(rule, ctx) {
|
|
|
52
65
|
// normalize direction (ensure from <= to)
|
|
53
66
|
const from = startKey < endKey ? startKey : endKey;
|
|
54
67
|
const to = startKey < endKey ? endKey : startKey;
|
|
68
|
+
const activePeriodDates = buildActivePeriodDateSet(rule, ctx);
|
|
55
69
|
if (rule.event_id) {
|
|
56
70
|
const event = ctx.events?.find(e => e._id === rule.event_id);
|
|
57
71
|
if (!event?.dates?.length)
|
|
@@ -67,7 +81,7 @@ export function getManualRuleAffectedDates(rule, ctx) {
|
|
|
67
81
|
}
|
|
68
82
|
// If year periods are specified, narrow event dates to matching periods only
|
|
69
83
|
if (rule.year_period_ids?.length) {
|
|
70
|
-
if (!isInPeriod(rule, key,
|
|
84
|
+
if (!isInPeriod(rule, key, activePeriodDates))
|
|
71
85
|
continue;
|
|
72
86
|
}
|
|
73
87
|
// If months are specified, narrow event dates to matching months only
|
|
@@ -85,7 +99,7 @@ export function getManualRuleAffectedDates(rule, ctx) {
|
|
|
85
99
|
const end = datesFromCalendarKey(to);
|
|
86
100
|
while (current.unix_timestamp <= end.unix_timestamp) {
|
|
87
101
|
const key = calendarKey(current);
|
|
88
|
-
if (ruleAppliesToCivilKey(rule, key, ctx)) {
|
|
102
|
+
if (ruleAppliesToCivilKey(rule, key, ctx, activePeriodDates)) {
|
|
89
103
|
affected.push(key);
|
|
90
104
|
}
|
|
91
105
|
current = current.plus({ days: 1 });
|