@justair/justair-library 7.1.0 → 7.2.0-alpha.8c8925e
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.
- package/dist/constants/index.d.ts +1 -0
- package/dist/constants/timezones.d.ts +6 -0
- package/dist/constants/timezones.d.ts.map +1 -0
- package/dist/index.d.ts +15 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/models/dataDownloads.d.ts +5 -0
- package/dist/models/dataDownloads.d.ts.map +1 -0
- package/dist/models/tests/dataDownloads.test.d.ts +2 -0
- package/dist/models/tests/dataDownloads.test.d.ts.map +1 -0
- package/dist/utils/dataDownloadRange.d.ts +21 -0
- package/dist/utils/dataDownloadRange.d.ts.map +1 -0
- package/dist/utils/quietHours.d.ts +3 -2
- package/dist/utils/quietHours.d.ts.map +1 -1
- package/dist/utils/tests/dataDownloadRange.test.d.ts +2 -0
- package/dist/utils/tests/dataDownloadRange.test.d.ts.map +1 -0
- package/dist/utils/tests/timezone.test.d.ts +2 -0
- package/dist/utils/tests/timezone.test.d.ts.map +1 -0
- package/dist/utils/timezone.d.ts +3 -0
- package/dist/utils/timezone.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/constants/index.js +1 -0
- package/src/constants/tests/constantsBarrel.test.js +35 -0
- package/src/constants/timezones.js +34 -0
- package/src/index.js +36 -2
- package/src/models/dataDownloads.js +157 -0
- package/src/utils/dataDownloadRange.js +120 -0
- package/src/utils/quietHours.js +8 -33
- package/src/utils/tests/dataDownloadRange.test.js +142 -0
- package/src/utils/tests/timezone.test.js +45 -0
- package/src/utils/timezone.js +38 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timezones.d.ts","sourceRoot":"","sources":["../../src/constants/timezones.js"],"names":[],"mappings":"AAMA,kDAAmD;AAiBnD;;;IAUE"}
|
package/dist/index.d.ts
CHANGED
|
@@ -86,19 +86,31 @@ import { AccountNotification } from "./models/userNotifications.js";
|
|
|
86
86
|
import { NOTIFICATION_KINDS } from "./models/userNotifications.js";
|
|
87
87
|
import { NOTIFICATION_CHANNELS } from "./models/userNotifications.js";
|
|
88
88
|
import { DELIVERY_STATUSES } from "./models/userNotifications.js";
|
|
89
|
+
import { dataDownloadSchema } from "./models/dataDownloads.js";
|
|
90
|
+
import { DataDownloads } from "./models/dataDownloads.js";
|
|
91
|
+
import { AqiDataDownload } from "./models/dataDownloads.js";
|
|
92
|
+
import { SampleDataDownload } from "./models/dataDownloads.js";
|
|
93
|
+
import { MAX_RANGE_DAYS as DATA_DOWNLOAD_MAX_RANGE_DAYS } from "./utils/dataDownloadRange.js";
|
|
94
|
+
import { MAX_RANGE_MS as DATA_DOWNLOAD_MAX_RANGE_MS } from "./utils/dataDownloadRange.js";
|
|
95
|
+
import { DATA_DOWNLOAD_ERRORS } from "./utils/dataDownloadRange.js";
|
|
96
|
+
import { validateDateOrder } from "./utils/dataDownloadRange.js";
|
|
97
|
+
import { validateMaxDateRange } from "./utils/dataDownloadRange.js";
|
|
98
|
+
import { validateDownloadEmail } from "./utils/dataDownloadRange.js";
|
|
99
|
+
import { normalizeDownloadEmail } from "./utils/dataDownloadRange.js";
|
|
89
100
|
import { PARAMETERS } from "./constants/pollutants.js";
|
|
90
101
|
import { HEAVY_METALS } from "./constants/pollutants.js";
|
|
91
102
|
import { ALERT_LEVEL_WEIGHTS } from "./constants/alertLevels.js";
|
|
92
103
|
import { getAlertWeight } from "./constants/alertLevels.js";
|
|
93
104
|
import { splitName } from "./utils/splitName.js";
|
|
94
105
|
import { DEFAULT_QUIET_HOURS } from "./utils/quietHours.js";
|
|
95
|
-
import { DEFAULT_TIMEZONE } from "./
|
|
106
|
+
import { DEFAULT_TIMEZONE } from "./constants/timezones.js";
|
|
107
|
+
import { TIMEZONE_OPTIONS } from "./constants/timezones.js";
|
|
96
108
|
import { QUIET_HOURS_ERRORS } from "./utils/quietHours.js";
|
|
97
109
|
import { isValidTimeString } from "./utils/quietHours.js";
|
|
98
|
-
import { isValidIANATimezone } from "./utils/
|
|
110
|
+
import { isValidIANATimezone } from "./utils/timezone.js";
|
|
99
111
|
import { validateQuietHours } from "./utils/quietHours.js";
|
|
100
112
|
import { getZoneMinutes } from "./utils/quietHours.js";
|
|
101
113
|
import { isInQuietWindow } from "./utils/quietHours.js";
|
|
102
114
|
import { resolveQuietHours } from "./utils/quietHours.js";
|
|
103
|
-
export { Database, adminSchema, Admin, configurationsSchema, Configurations, measurementsSchema, Measurements, monitorRequestsSchema, MonitorRequests, monitorsSchema, Monitors, organizationsSchema, Organizations, usersSchema, Users, COOKIE_CONSENT_CATEGORIES, COOKIE_CONSENT_VALUES, validateCookieConsent, eventsSchema, Events, lightMonitorSchema, LightMonitors, monitorSuppliersSchema, MonitorSuppliers, contextsSchema, Contexts, parametersSchema, Parameters, announcementSchema, Announcements, jobsSchema, Jobs, apiKeySchema, ApiKey, UsageMetrics, usageMetricsSchema, Audit, auditSchema, EventsAudit, eventsAuditSchema, MonitorAudit, monitorAuditSchema, parametersEnum, deploymentTypesEnum, AlertsAudit, Alerts, alertsSchema, alertsAuditSchema, Features, featuresSchema, dataCompletenessSchema, DataCompleteness, networkMetricsSchema, NetworkMetrics, rateOfChangeSchema, RateOfChange, changeStreamCheckpointsSchema, ChangeStreamCheckpoints, sitesSchema, Sites, sampleSitesSchema, SampleSites, sampleSiteAuditSchema, SampleSiteAudit, sampleParametersEnum, samplesSchema, Samples, samplesAuditSchema, SamplesAudit, sampleParameterReferenceConcentrations, passwordResetTokenSchema, PasswordResetToken, monitorEpisodesSchema, MonitorEpisode, userNotificationsSchema, UserNotification, AqiAlertNotification, DeviceOfflineNotification, WeeklyDigestNotification, AccountNotification, NOTIFICATION_KINDS, NOTIFICATION_CHANNELS, DELIVERY_STATUSES, PARAMETERS, HEAVY_METALS, ALERT_LEVEL_WEIGHTS, getAlertWeight, splitName, DEFAULT_QUIET_HOURS, DEFAULT_TIMEZONE, QUIET_HOURS_ERRORS, isValidTimeString, isValidIANATimezone, validateQuietHours, getZoneMinutes, isInQuietWindow, resolveQuietHours };
|
|
115
|
+
export { Database, adminSchema, Admin, configurationsSchema, Configurations, measurementsSchema, Measurements, monitorRequestsSchema, MonitorRequests, monitorsSchema, Monitors, organizationsSchema, Organizations, usersSchema, Users, COOKIE_CONSENT_CATEGORIES, COOKIE_CONSENT_VALUES, validateCookieConsent, eventsSchema, Events, lightMonitorSchema, LightMonitors, monitorSuppliersSchema, MonitorSuppliers, contextsSchema, Contexts, parametersSchema, Parameters, announcementSchema, Announcements, jobsSchema, Jobs, apiKeySchema, ApiKey, UsageMetrics, usageMetricsSchema, Audit, auditSchema, EventsAudit, eventsAuditSchema, MonitorAudit, monitorAuditSchema, parametersEnum, deploymentTypesEnum, AlertsAudit, Alerts, alertsSchema, alertsAuditSchema, Features, featuresSchema, dataCompletenessSchema, DataCompleteness, networkMetricsSchema, NetworkMetrics, rateOfChangeSchema, RateOfChange, changeStreamCheckpointsSchema, ChangeStreamCheckpoints, sitesSchema, Sites, sampleSitesSchema, SampleSites, sampleSiteAuditSchema, SampleSiteAudit, sampleParametersEnum, samplesSchema, Samples, samplesAuditSchema, SamplesAudit, sampleParameterReferenceConcentrations, passwordResetTokenSchema, PasswordResetToken, monitorEpisodesSchema, MonitorEpisode, userNotificationsSchema, UserNotification, AqiAlertNotification, DeviceOfflineNotification, WeeklyDigestNotification, AccountNotification, NOTIFICATION_KINDS, NOTIFICATION_CHANNELS, DELIVERY_STATUSES, dataDownloadSchema, DataDownloads, AqiDataDownload, SampleDataDownload, DATA_DOWNLOAD_MAX_RANGE_DAYS, DATA_DOWNLOAD_MAX_RANGE_MS, DATA_DOWNLOAD_ERRORS, validateDateOrder, validateMaxDateRange, validateDownloadEmail, normalizeDownloadEmail, PARAMETERS, HEAVY_METALS, ALERT_LEVEL_WEIGHTS, getAlertWeight, splitName, DEFAULT_QUIET_HOURS, DEFAULT_TIMEZONE, TIMEZONE_OPTIONS, QUIET_HOURS_ERRORS, isValidTimeString, isValidIANATimezone, validateQuietHours, getZoneMinutes, isInQuietWindow, resolveQuietHours };
|
|
104
116
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"AA4IA;;;iBAEC;yBA5EwB,oBAAoB;qBADxB,gBAAgB;4BAjEF,mBAAmB;sBAAnB,mBAAmB;qCAI/C,4BAA4B;+BAA5B,4BAA4B;mCAM5B,0BAA0B;6BAA1B,0BAA0B;sCAI1B,6BAA6B;gCAA7B,6BAA6B;+BAQ7B,sBAAsB;yBAAtB,sBAAsB;oCACsB,2BAA2B;8BAA3B,2BAA2B;4BAcvE,mBAAmB;sBAAnB,mBAAmB;0CAAnB,mBAAmB;sCAAnB,mBAAmB;sCAAnB,mBAAmB;6BAMnB,oBAAoB;uBAApB,oBAAoB;mCAnBuB,2BAA2B;8BAA3B,2BAA2B;uCAItE,8BAA8B;iCAA9B,8BAA8B;+BACI,sBAAsB;yBAAtB,sBAAsB;iCAClB,wBAAwB;2BAAxB,wBAAwB;mCAcnB,2BAA2B;8BAA3B,2BAA2B;2BAC5C,kBAAkB;qBAAlB,kBAAkB;6BACd,oBAAoB;uBAApB,oBAAoB;6BACR,0BAA0B;mCAA1B,0BAA0B;sBArCpE,0BAA0B;4BAA1B,0BAA0B;4BAiC1B,oBAAoB;kCAApB,oBAAoB;6BArBpB,sBAAsB;mCAAtB,sBAAsB;+BAAtB,sBAAsB;oCAAtB,sBAAsB;4BA+BtB,oBAAoB;uBAApB,oBAAoB;6BAApB,oBAAoB;kCAApB,oBAAoB;yBACc,sBAAsB;+BAAtB,sBAAsB;uCAIxD,8BAA8B;iCAA9B,8BAA8B;qCACgB,4BAA4B;+BAA5B,4BAA4B;mCAChC,0BAA0B;6BAA1B,0BAA0B;8CAIpE,qCAAqC;wCAArC,qCAAqC;4BAkCT,mBAAmB;sBAAnB,mBAAmB;kCAS/C,yBAAyB;4BAAzB,yBAAyB;sCAAzB,yBAAyB;gCAAzB,yBAAyB;qCAAzB,yBAAyB;8BASzB,qBAAqB;wBAArB,qBAAqB;mCAArB,qBAAqB;6BAArB,qBAAqB;uDAArB,qBAAqB;yCACiC,gCAAgC;mCAAhC,gCAAgC;sCA5CtF,6BAA6B;+BAA7B,6BAA6B;wCAW7B,+BAA+B;iCAA/B,+BAA+B;qCAA/B,+BAA+B;0CAA/B,+BAA+B;yCAA/B,+BAA+B;oCAA/B,+BAA+B;mCAA/B,+BAA+B;sCAA/B,+BAA+B;kCAA/B,+BAA+B;mCAuC/B,2BAA2B;8BAA3B,2BAA2B;gCAA3B,2BAA2B;mCAA3B,2BAA2B;+DAe3B,8BAA8B;2DAA9B,8BAA8B;qCAA9B,8BAA8B;kCAA9B,8BAA8B;qCAA9B,8BAA8B;sCAA9B,8BAA8B;uCAA9B,8BAA8B;2BAvEI,2BAA2B;6BAA3B,2BAA2B;oCAChB,4BAA4B;+BAA5B,4BAA4B;0BAiBtD,sBAAsB;oCASzC,uBAAuB;iCAzBqB,0BAA0B;iCAA1B,0BAA0B;mCAyBtE,uBAAuB;kCAAvB,uBAAuB;oCACM,qBAAqB;mCADlD,uBAAuB;+BAAvB,uBAAuB;gCAAvB,uBAAuB;kCAAvB,uBAAuB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataDownloads.d.ts","sourceRoot":"","sources":["../../src/models/dataDownloads.js"],"names":[],"mappings":"AAsBA,qCAmDE;AAkEF,gCAA0E;AAO1E,kCAEG;AAEH,qCAIE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataDownloads.test.d.ts","sourceRoot":"","sources":["../../../src/models/tests/dataDownloads.test.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const MAX_RANGE_DAYS: 31;
|
|
2
|
+
export const MAX_RANGE_MS: number;
|
|
3
|
+
export const DATA_DOWNLOAD_ERRORS: Readonly<{
|
|
4
|
+
END_BEFORE_START: "end_before_start";
|
|
5
|
+
RANGE_TOO_LONG: "range_too_long";
|
|
6
|
+
INVALID_EMAIL: "invalid_email";
|
|
7
|
+
}>;
|
|
8
|
+
export function validateDateOrder({ start, end }?: {}): {
|
|
9
|
+
code: "end_before_start";
|
|
10
|
+
message: string;
|
|
11
|
+
}[];
|
|
12
|
+
export function validateMaxDateRange({ start, end }?: {}): {
|
|
13
|
+
code: "range_too_long";
|
|
14
|
+
message: string;
|
|
15
|
+
}[];
|
|
16
|
+
export function normalizeDownloadEmail(value: any): any;
|
|
17
|
+
export function validateDownloadEmail({ email }?: {}): {
|
|
18
|
+
code: "invalid_email";
|
|
19
|
+
message: string;
|
|
20
|
+
}[];
|
|
21
|
+
//# sourceMappingURL=dataDownloadRange.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataDownloadRange.d.ts","sourceRoot":"","sources":["../../src/utils/dataDownloadRange.js"],"names":[],"mappings":"AAmBA,gCAAiC;AAMjC,kCAAkF;AAKlF;;;;GAIG;AA0BI;;;IAcN;AAMM;;;IAcN;AASM,wDAC0D;AAK1D;;;IAUN"}
|
|
@@ -2,14 +2,12 @@ export const DEFAULT_QUIET_HOURS: Readonly<{
|
|
|
2
2
|
start: "22:00";
|
|
3
3
|
end: "07:00";
|
|
4
4
|
}>;
|
|
5
|
-
export const DEFAULT_TIMEZONE: "America/New_York";
|
|
6
5
|
export const QUIET_HOURS_ERRORS: Readonly<{
|
|
7
6
|
HALF_PAIR: "half_pair";
|
|
8
7
|
FORMAT: "format";
|
|
9
8
|
EQUAL_PAIR: "equal_pair";
|
|
10
9
|
}>;
|
|
11
10
|
export function isValidTimeString(value: any): boolean;
|
|
12
|
-
export function isValidIANATimezone(timeZone: any): boolean;
|
|
13
11
|
export function validateQuietHours({ start, end }?: {}): {
|
|
14
12
|
code: "half_pair";
|
|
15
13
|
message: string;
|
|
@@ -30,4 +28,7 @@ export function resolveQuietHours(user: any): {
|
|
|
30
28
|
timezone: string;
|
|
31
29
|
warnings: string[];
|
|
32
30
|
};
|
|
31
|
+
import { DEFAULT_TIMEZONE } from "../constants/timezones.js";
|
|
32
|
+
import { isValidIANATimezone } from "./timezone.js";
|
|
33
|
+
export { DEFAULT_TIMEZONE, isValidIANATimezone };
|
|
33
34
|
//# sourceMappingURL=quietHours.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quietHours.d.ts","sourceRoot":"","sources":["../../src/utils/quietHours.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"quietHours.d.ts","sourceRoot":"","sources":["../../src/utils/quietHours.js"],"names":[],"mappings":"AA0BA;;;GAGG;AAKH;;;;GAIG;AAII,uDACgD;AAOhD;;;;;;;;;KAsCN;AASM,iEASN;AAKM,uEAaN;AAeM;;;;;;;EA6CN;iCAtKgC,2BAA2B;oCACV,eAAe"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataDownloadRange.test.d.ts","sourceRoot":"","sources":["../../../src/utils/tests/dataDownloadRange.test.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timezone.test.d.ts","sourceRoot":"","sources":["../../../src/utils/tests/timezone.test.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timezone.d.ts","sourceRoot":"","sources":["../../src/utils/timezone.js"],"names":[],"mappings":"AAaO,iDAYN;AAEM,4DAUN"}
|
package/package.json
CHANGED
package/src/constants/index.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import * as barrel from '../index.js';
|
|
7
7
|
import { PARAMETERS, HEAVY_METALS } from '../pollutants.js';
|
|
8
8
|
import { ALERT_LEVEL_WEIGHTS, getAlertWeight } from '../alertLevels.js';
|
|
9
|
+
import { DEFAULT_TIMEZONE, TIMEZONE_OPTIONS } from '../timezones.js';
|
|
9
10
|
|
|
10
11
|
test('barrel re-exports PARAMETERS and HEAVY_METALS unchanged (identity, not copies)', () => {
|
|
11
12
|
expect(barrel.PARAMETERS).toBe(PARAMETERS);
|
|
@@ -44,3 +45,37 @@ test('unknown and legacy statuses fall back to the Good weight (never alert, nev
|
|
|
44
45
|
expect(getAlertWeight(null)).toBe(1);
|
|
45
46
|
expect(getAlertWeight('')).toBe(1);
|
|
46
47
|
});
|
|
48
|
+
|
|
49
|
+
test('barrel exports DEFAULT_TIMEZONE (browser-safe, no Mongoose)', () => {
|
|
50
|
+
expect(barrel.DEFAULT_TIMEZONE).toBe(DEFAULT_TIMEZONE);
|
|
51
|
+
expect(DEFAULT_TIMEZONE).toBe('America/New_York');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// S3 (JA-3245 round 2): curated dropdown options for fixed-set timezone
|
|
55
|
+
// selectors, mirroring JustAir-V2-NextJS-'s useAccountNotifications.js
|
|
56
|
+
// 9-zone list exactly (same order, same IANA ids) so both surfaces share
|
|
57
|
+
// one source instead of each hardcoding its own label -> IANA-id mapping.
|
|
58
|
+
test('barrel exports TIMEZONE_OPTIONS (identity, not a copy)', () => {
|
|
59
|
+
expect(barrel.TIMEZONE_OPTIONS).toBe(TIMEZONE_OPTIONS);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('TIMEZONE_OPTIONS is the curated 9-zone US set, DEFAULT_TIMEZONE among them', () => {
|
|
63
|
+
expect(TIMEZONE_OPTIONS).toEqual([
|
|
64
|
+
{ label: 'Eastern Time (ET)', value: 'America/New_York' },
|
|
65
|
+
{ label: 'Central Time (CT)', value: 'America/Chicago' },
|
|
66
|
+
{ label: 'Mountain Time (MT)', value: 'America/Denver' },
|
|
67
|
+
{ label: 'Mountain Time – Arizona (MT)', value: 'America/Phoenix' },
|
|
68
|
+
{ label: 'Pacific Time (PT)', value: 'America/Los_Angeles' },
|
|
69
|
+
{ label: 'Alaska Time (AKT)', value: 'America/Anchorage' },
|
|
70
|
+
{ label: 'Hawaii Time (HT)', value: 'Pacific/Honolulu' },
|
|
71
|
+
{ label: 'Atlantic Time (AT)', value: 'America/Halifax' },
|
|
72
|
+
{ label: 'Puerto Rico (AT)', value: 'America/Puerto_Rico' },
|
|
73
|
+
]);
|
|
74
|
+
expect(TIMEZONE_OPTIONS.map((tz) => tz.value)).toContain(DEFAULT_TIMEZONE);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('every TIMEZONE_OPTIONS value is a valid IANA timezone identifier', () => {
|
|
78
|
+
for (const { value } of TIMEZONE_OPTIONS) {
|
|
79
|
+
expect(() => new Intl.DateTimeFormat('en-US', { timeZone: value })).not.toThrow();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Mongoose-free timezone defaults, so a browser-safe consumer (e.g.
|
|
2
|
+
// JustAir-Client) can use the same default other domains store server-side.
|
|
3
|
+
// Originally lived in utils/quietHours.js (JA-3054); moved here once a
|
|
4
|
+
// second, unrelated domain (dataDownloads.js, JA-3080) needed it too — see
|
|
5
|
+
// CLAUDE.md's "Note on constants placement".
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_TIMEZONE = "America/New_York";
|
|
8
|
+
|
|
9
|
+
// Curated timezone dropdown options (label + IANA id) for fixed-set
|
|
10
|
+
// timezone selectors — data download's "Set Timezone" field (JA-3080/
|
|
11
|
+
// JA-3245's Figma design) and account notification preferences share the
|
|
12
|
+
// same short, US-centric set rather than every zone Intl recognises.
|
|
13
|
+
//
|
|
14
|
+
// Mirrors JustAir-V2-NextJS-'s useAccountNotifications.js curated 9-zone
|
|
15
|
+
// list exactly (same order, same IANA ids, same English label text from
|
|
16
|
+
// src/locales/en.json) rather than JustAir-Client's older, much larger
|
|
17
|
+
// global alias-list selector (TimeZoneSelector.js) — those two existing
|
|
18
|
+
// frontend lists already diverge, and this one matches the actively
|
|
19
|
+
// developed V2 surface. Labels here are plain strings, not i18n keys: this
|
|
20
|
+
// library has no i18n dependency, so a consumer wanting localized labels
|
|
21
|
+
// resolves them from `value`, the same way it already would for any
|
|
22
|
+
// non-English UI. Neither existing FE list imports this yet — pointing
|
|
23
|
+
// either at it is a follow-up, not part of this PR.
|
|
24
|
+
export const TIMEZONE_OPTIONS = [
|
|
25
|
+
{ label: "Eastern Time (ET)", value: "America/New_York" },
|
|
26
|
+
{ label: "Central Time (CT)", value: "America/Chicago" },
|
|
27
|
+
{ label: "Mountain Time (MT)", value: "America/Denver" },
|
|
28
|
+
{ label: "Mountain Time – Arizona (MT)", value: "America/Phoenix" },
|
|
29
|
+
{ label: "Pacific Time (PT)", value: "America/Los_Angeles" },
|
|
30
|
+
{ label: "Alaska Time (AKT)", value: "America/Anchorage" },
|
|
31
|
+
{ label: "Hawaii Time (HT)", value: "Pacific/Honolulu" },
|
|
32
|
+
{ label: "Atlantic Time (AT)", value: "America/Halifax" },
|
|
33
|
+
{ label: "Puerto Rico (AT)", value: "America/Puerto_Rico" },
|
|
34
|
+
];
|
package/src/index.js
CHANGED
|
@@ -67,6 +67,7 @@ import Database from "./config/db.js"; // Import the new Database class
|
|
|
67
67
|
import CustomLogger from "./config/logger.js";
|
|
68
68
|
import { PARAMETERS, HEAVY_METALS } from "./constants/pollutants.js";
|
|
69
69
|
import { ALERT_LEVEL_WEIGHTS, getAlertWeight } from "./constants/alertLevels.js";
|
|
70
|
+
import { DEFAULT_TIMEZONE, TIMEZONE_OPTIONS } from "./constants/timezones.js";
|
|
70
71
|
import {
|
|
71
72
|
monitorEpisodesSchema,
|
|
72
73
|
MonitorEpisode,
|
|
@@ -85,15 +86,14 @@ import {
|
|
|
85
86
|
import { splitName } from "./utils/splitName.js";
|
|
86
87
|
import {
|
|
87
88
|
DEFAULT_QUIET_HOURS,
|
|
88
|
-
DEFAULT_TIMEZONE,
|
|
89
89
|
QUIET_HOURS_ERRORS,
|
|
90
90
|
isValidTimeString,
|
|
91
|
-
isValidIANATimezone,
|
|
92
91
|
validateQuietHours,
|
|
93
92
|
getZoneMinutes,
|
|
94
93
|
isInQuietWindow,
|
|
95
94
|
resolveQuietHours,
|
|
96
95
|
} from "./utils/quietHours.js";
|
|
96
|
+
import { isValidIANATimezone } from "./utils/timezone.js";
|
|
97
97
|
|
|
98
98
|
// Sites related imports
|
|
99
99
|
import { sitesSchema, Sites } from "./models/sites.js";
|
|
@@ -116,6 +116,27 @@ import {
|
|
|
116
116
|
sampleParameterReferenceConcentrations,
|
|
117
117
|
} from "./models/samples.js";
|
|
118
118
|
import { passwordResetTokenSchema, PasswordResetToken } from "./models/passwordResetToken.js";
|
|
119
|
+
import {
|
|
120
|
+
dataDownloadSchema,
|
|
121
|
+
DataDownloads,
|
|
122
|
+
AqiDataDownload,
|
|
123
|
+
SampleDataDownload,
|
|
124
|
+
} from "./models/dataDownloads.js";
|
|
125
|
+
import {
|
|
126
|
+
// Renamed at the barrel only (JA-3245 round 2, I3) — the root export
|
|
127
|
+
// namespace is shared by every consuming service and already holds
|
|
128
|
+
// domain-scoped names (DATA_DOWNLOAD_ERRORS, DELIVERY_STATUSES,
|
|
129
|
+
// ALERT_LEVEL_WEIGHTS); bare MAX_RANGE_DAYS/MAX_RANGE_MS gave no hint
|
|
130
|
+
// they're data-download-specific. Module-internal name (used by
|
|
131
|
+
// dataDownloadRange.js's own tests and by dataDownloads.js) is unchanged.
|
|
132
|
+
MAX_RANGE_DAYS as DATA_DOWNLOAD_MAX_RANGE_DAYS,
|
|
133
|
+
MAX_RANGE_MS as DATA_DOWNLOAD_MAX_RANGE_MS,
|
|
134
|
+
DATA_DOWNLOAD_ERRORS,
|
|
135
|
+
validateDateOrder,
|
|
136
|
+
validateMaxDateRange,
|
|
137
|
+
validateDownloadEmail,
|
|
138
|
+
normalizeDownloadEmail,
|
|
139
|
+
} from "./utils/dataDownloadRange.js";
|
|
119
140
|
|
|
120
141
|
export function createLoggerInstance({ DATADOG_API_KEY, APPLICATION_NAME }) {
|
|
121
142
|
return new CustomLogger({ DATADOG_API_KEY, APPLICATION_NAME });
|
|
@@ -210,6 +231,18 @@ export {
|
|
|
210
231
|
NOTIFICATION_KINDS,
|
|
211
232
|
NOTIFICATION_CHANNELS,
|
|
212
233
|
DELIVERY_STATUSES,
|
|
234
|
+
// Data downloads (JA-3080)
|
|
235
|
+
dataDownloadSchema,
|
|
236
|
+
DataDownloads,
|
|
237
|
+
AqiDataDownload,
|
|
238
|
+
SampleDataDownload,
|
|
239
|
+
DATA_DOWNLOAD_MAX_RANGE_DAYS,
|
|
240
|
+
DATA_DOWNLOAD_MAX_RANGE_MS,
|
|
241
|
+
DATA_DOWNLOAD_ERRORS,
|
|
242
|
+
validateDateOrder,
|
|
243
|
+
validateMaxDateRange,
|
|
244
|
+
validateDownloadEmail,
|
|
245
|
+
normalizeDownloadEmail,
|
|
213
246
|
// Pollutant constants
|
|
214
247
|
PARAMETERS,
|
|
215
248
|
HEAVY_METALS,
|
|
@@ -221,6 +254,7 @@ export {
|
|
|
221
254
|
// Quiet hours (JA-3054): shared validation + window evaluation
|
|
222
255
|
DEFAULT_QUIET_HOURS,
|
|
223
256
|
DEFAULT_TIMEZONE,
|
|
257
|
+
TIMEZONE_OPTIONS,
|
|
224
258
|
QUIET_HOURS_ERRORS,
|
|
225
259
|
isValidTimeString,
|
|
226
260
|
isValidIANATimezone,
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
import { DEFAULT_TIMEZONE } from "../constants/timezones.js";
|
|
3
|
+
import { isValidIANATimezone } from "../utils/timezone.js";
|
|
4
|
+
import {
|
|
5
|
+
DATA_DOWNLOAD_ERRORS,
|
|
6
|
+
validateDateOrder,
|
|
7
|
+
validateMaxDateRange,
|
|
8
|
+
validateDownloadEmail,
|
|
9
|
+
normalizeDownloadEmail,
|
|
10
|
+
} from "../utils/dataDownloadRange.js";
|
|
11
|
+
|
|
12
|
+
// DataDownloads — one doc per data download request (JA-3080). Written by
|
|
13
|
+
// JustAirService on download (JA-3081); read by the org-level admin
|
|
14
|
+
// communication screen. AQI downloads are monitor-scoped, Sample downloads
|
|
15
|
+
// are site-scoped (samples.js keys samples to SampleSites, not Monitors) —
|
|
16
|
+
// modeled as discriminators per CLAUDE.md's multi-kind pattern rather than
|
|
17
|
+
// a flat schema with a monitorId/siteId pair that's nullable either way.
|
|
18
|
+
//
|
|
19
|
+
// The base model permits a kind-less document (create() without `dataType`)
|
|
20
|
+
// — same property `userNotifications.js`'s base model has. Write through
|
|
21
|
+
// AqiDataDownload/SampleDataDownload, not DataDownloads, so every stored
|
|
22
|
+
// document has the ref field the admin screen needs to render it.
|
|
23
|
+
const dataDownloadSchema = mongoose.Schema(
|
|
24
|
+
{
|
|
25
|
+
orgId: {
|
|
26
|
+
type: mongoose.Types.ObjectId,
|
|
27
|
+
ref: "Organizations",
|
|
28
|
+
required: [true, "orgId is required"],
|
|
29
|
+
},
|
|
30
|
+
dateRangeStart: { type: Date, required: [true, "dateRangeStart is required"] },
|
|
31
|
+
dateRangeEnd: { type: Date, required: [true, "dateRangeEnd is required"] },
|
|
32
|
+
// `default` only applies to `undefined`, not `null` — an explicit
|
|
33
|
+
// `timezone: null` (the normal shape of "didn't pick one" in a JSON
|
|
34
|
+
// intake body) is coerced to DEFAULT_TIMEZONE here rather than left to
|
|
35
|
+
// fail isValidIANATimezone, matching users.js's null-tolerant timezone
|
|
36
|
+
// field in intent (there it's `value === null || isValidIANATimezone`;
|
|
37
|
+
// here the setter resolves it so no reader needs a users.js-style
|
|
38
|
+
// fallback). Setters run on both save() and updateOne()-style query
|
|
39
|
+
// updates, unlike the pre('validate') hook below.
|
|
40
|
+
timezone: {
|
|
41
|
+
type: String,
|
|
42
|
+
default: DEFAULT_TIMEZONE,
|
|
43
|
+
set: (value) => (value === null ? DEFAULT_TIMEZONE : value),
|
|
44
|
+
validate: {
|
|
45
|
+
validator: isValidIANATimezone,
|
|
46
|
+
message: "timezone must be a valid IANA timezone identifier",
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
downloadedAt: { type: Date, required: true, default: Date.now },
|
|
50
|
+
// Format validation delegates to validateDownloadEmail (dataDownloadRange.js)
|
|
51
|
+
// so this and JA-3081's intake endpoint can never enforce two different
|
|
52
|
+
// email rules — but it's a per-field validator, not a pre('validate')
|
|
53
|
+
// hook, so it's still enforced on updateOne()/findOneAndUpdate() calls
|
|
54
|
+
// made with { runValidators: true }, the same as `timezone` above.
|
|
55
|
+
// `type` surfaces as ValidatorError.kind, keeping the stable-code
|
|
56
|
+
// contract intact on this path too.
|
|
57
|
+
email: {
|
|
58
|
+
type: String,
|
|
59
|
+
trim: true,
|
|
60
|
+
lowercase: true,
|
|
61
|
+
default: null,
|
|
62
|
+
set: normalizeDownloadEmail,
|
|
63
|
+
validate: {
|
|
64
|
+
validator: (value) => validateDownloadEmail({ email: value }).length === 0,
|
|
65
|
+
message: "email must be a valid email address",
|
|
66
|
+
type: DATA_DOWNLOAD_ERRORS.INVALID_EMAIL,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
timestamps: true,
|
|
72
|
+
discriminatorKey: "dataType",
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
// Cross-field rule (quiet-hours shape, CLAUDE.md): delegated to
|
|
77
|
+
// validateDateOrder so this schema and JA-3081's write-path validation can
|
|
78
|
+
// never drift. Applies to every kind — the 31-day cap is AQI-only and lives
|
|
79
|
+
// on AqiDataDownload's own schema below, not here. Lives in pre('validate')
|
|
80
|
+
// so create()/save() can't persist an invalid range even if a caller
|
|
81
|
+
// forgets to check. Per-field rules (timezone, email) live on their own
|
|
82
|
+
// fields above, not here — pre('validate') hooks don't run on query
|
|
83
|
+
// updates, so a per-field rule enforced only here would silently stop
|
|
84
|
+
// applying on updateOne()/findOneAndUpdate(). This hook is only for the
|
|
85
|
+
// genuinely cross-field date-order rule.
|
|
86
|
+
//
|
|
87
|
+
// Passes the DATA_DOWNLOAD_ERRORS code as invalidate()'s `kind` argument,
|
|
88
|
+
// not just the message, so a caught ValidationError can be classified via
|
|
89
|
+
// err.errors.<path>.kind without string-matching wording that's free to
|
|
90
|
+
// change.
|
|
91
|
+
dataDownloadSchema.pre("validate", function () {
|
|
92
|
+
const rangeErrors = validateDateOrder({ start: this.dateRangeStart, end: this.dateRangeEnd });
|
|
93
|
+
for (const error of rangeErrors) {
|
|
94
|
+
this.invalidate("dateRangeEnd", error.message, this.dateRangeEnd, error.code);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Org-level admin screen lists a given org's downloads, most recent first,
|
|
99
|
+
// by the domain-meaningful "date of the download" (JA-3080), not createdAt.
|
|
100
|
+
dataDownloadSchema.index({ orgId: 1, downloadedAt: -1 });
|
|
101
|
+
|
|
102
|
+
const aqiDataDownloadSchema = new mongoose.Schema({
|
|
103
|
+
// Figma's download modal (node 4748:16300) titles the AQI variant
|
|
104
|
+
// "Download Data for: {Site Name}". Resolve that label via
|
|
105
|
+
// monitorId -> Monitors.monitorCode, NOT a Monitors -> Sites.name join:
|
|
106
|
+
// Monitors don't carry their own name and monitorCode is JustAirService's
|
|
107
|
+
// existing human-readable identifier for a monitor (already used in
|
|
108
|
+
// admin views and Slack alerts), whereas Monitors.siteId is not
|
|
109
|
+
// guaranteed set — it's only populated for monitors created through
|
|
110
|
+
// POST /devices's "linked device" flow, not plain POST /monitors — so a
|
|
111
|
+
// Sites join would render blank for a meaningful share of monitors.
|
|
112
|
+
// JA-3246/JA-3081 should populate this via monitorId, not siteId.
|
|
113
|
+
monitorId: {
|
|
114
|
+
type: mongoose.Types.ObjectId,
|
|
115
|
+
ref: "Monitors",
|
|
116
|
+
required: [true, "monitorId is required"],
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
aqiDataDownloadSchema.index({ monitorId: 1, downloadedAt: -1 });
|
|
120
|
+
|
|
121
|
+
// AQI-only: the 31-day cap does not apply to Sample downloads. Mongoose
|
|
122
|
+
// merges base + discriminator schema hooks by default (mergeHooks), so this
|
|
123
|
+
// runs alongside (not instead of) the base's validateDateOrder check above.
|
|
124
|
+
aqiDataDownloadSchema.pre("validate", function () {
|
|
125
|
+
const errors = validateMaxDateRange({ start: this.dateRangeStart, end: this.dateRangeEnd });
|
|
126
|
+
for (const error of errors) {
|
|
127
|
+
this.invalidate("dateRangeEnd", error.message, this.dateRangeEnd, error.code);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const sampleDataDownloadSchema = new mongoose.Schema({
|
|
132
|
+
siteId: {
|
|
133
|
+
type: mongoose.Types.ObjectId,
|
|
134
|
+
ref: "SampleSites",
|
|
135
|
+
required: [true, "siteId is required"],
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
sampleDataDownloadSchema.index({ siteId: 1, downloadedAt: -1 });
|
|
139
|
+
|
|
140
|
+
const DataDownloads = mongoose.model("DataDownloads", dataDownloadSchema);
|
|
141
|
+
|
|
142
|
+
// Discriminator model names are namespaced (matches userNotifications.js's
|
|
143
|
+
// aqi_alert/device_offline/weekly_digest) — "AQI"/"Sample" as bare mongoose
|
|
144
|
+
// model-registry keys would collide with any consuming service's own models
|
|
145
|
+
// of those names. { value } keeps the persisted dataType exactly what
|
|
146
|
+
// JA-3245 specifies regardless of the registry name.
|
|
147
|
+
const AqiDataDownload = DataDownloads.discriminator("AqiDataDownload", aqiDataDownloadSchema, {
|
|
148
|
+
value: "AQI",
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const SampleDataDownload = DataDownloads.discriminator(
|
|
152
|
+
"SampleDataDownload",
|
|
153
|
+
sampleDataDownloadSchema,
|
|
154
|
+
{ value: "Sample" }
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
export { dataDownloadSchema, DataDownloads, AqiDataDownload, SampleDataDownload };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Data download date range validation (JA-3080/JA-3245): shared cross-field
|
|
2
|
+
// rules for the requested range on a DataDownloads document. Single source
|
|
3
|
+
// of truth for every consumer — this library's schema enforces these on
|
|
4
|
+
// write, JustAirService's intake endpoint (JA-3081) validates the same
|
|
5
|
+
// request shape before ever constructing a document. Pure functions, no DB,
|
|
6
|
+
// no clock reads. Same shape as quietHours.js's cross-field validator.
|
|
7
|
+
//
|
|
8
|
+
// Two separate rules, deliberately not one function: end-must-not-precede-
|
|
9
|
+
// start applies to every download kind, but the 31-day cap is AQI-only —
|
|
10
|
+
// Sample downloads have no range limit. Compose the one(s) that apply to
|
|
11
|
+
// the kind being validated; don't call validateMaxDateRange for Sample.
|
|
12
|
+
|
|
13
|
+
// Display/messaging only ("range cannot exceed 31 days"). NOT the enforced
|
|
14
|
+
// bound — a consumer clamping a date picker with
|
|
15
|
+
// `MAX_RANGE_DAYS * 24 * 60 * 60 * 1000` builds a stricter cap than the one
|
|
16
|
+
// actually enforced (see MAX_RANGE_MS below) and would reject the exact
|
|
17
|
+
// DST-crossing range the grace exists to allow. Import MAX_RANGE_MS instead
|
|
18
|
+
// for anything that needs the real bound; validateMaxDateRange is the sole
|
|
19
|
+
// authority either way.
|
|
20
|
+
export const MAX_RANGE_DAYS = 31;
|
|
21
|
+
|
|
22
|
+
// DST slack: a calendar-accurate 31-day span crossing a fall-back transition
|
|
23
|
+
// is 745 (not 744) hours long. Allow it rather than rejecting a range the
|
|
24
|
+
// product spec (Figma) explicitly promises as valid. This, not
|
|
25
|
+
// MAX_RANGE_DAYS, is the number validateMaxDateRange actually enforces.
|
|
26
|
+
export const MAX_RANGE_MS = MAX_RANGE_DAYS * 24 * 60 * 60 * 1000 + 60 * 60 * 1000;
|
|
27
|
+
|
|
28
|
+
// Stable error codes for validateDateOrder/validateMaxDateRange/
|
|
29
|
+
// validateDownloadEmail. Consumers filter on `code` — never on message
|
|
30
|
+
// wording, which is free to change.
|
|
31
|
+
export const DATA_DOWNLOAD_ERRORS = Object.freeze({
|
|
32
|
+
END_BEFORE_START: "end_before_start",
|
|
33
|
+
RANGE_TOO_LONG: "range_too_long",
|
|
34
|
+
INVALID_EMAIL: "invalid_email",
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Coerces both ends through `new Date(...)` before comparing/subtracting: a
|
|
38
|
+
// JSON request body (JA-3081's intake endpoint) hands these functions raw
|
|
39
|
+
// strings, not Date instances, and comparing/subtracting un-coerced strings
|
|
40
|
+
// silently produces NaN (`NaN > x` is always false) — the caps would never
|
|
41
|
+
// fire for exactly the caller these functions exist to serve. An unparsable
|
|
42
|
+
// value coerces to an Invalid Date (NaN time); format validation is the
|
|
43
|
+
// schema/type layer's job, not a cross-field rule's, so that's treated the
|
|
44
|
+
// same as "not this validator's concern" and returns null rather than
|
|
45
|
+
// throwing or reporting a spurious range error.
|
|
46
|
+
const toTimes = (start, end) => {
|
|
47
|
+
if (!start || !end) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const startTime = new Date(start).getTime();
|
|
51
|
+
const endTime = new Date(end).getTime();
|
|
52
|
+
if (Number.isNaN(startTime) || Number.isNaN(endTime)) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return { startTime, endTime };
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// Returns an array of { code, message } errors; empty array = valid. Applies
|
|
59
|
+
// to every download kind (AQI and Sample) — the Figma "31 days" cap is
|
|
60
|
+
// AQI-only and is NOT enforced here, see validateMaxDateRange.
|
|
61
|
+
export const validateDateOrder = ({ start, end } = {}) => {
|
|
62
|
+
const times = toTimes(start, end);
|
|
63
|
+
if (!times) {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
if (times.endTime < times.startTime) {
|
|
67
|
+
return [
|
|
68
|
+
{
|
|
69
|
+
code: DATA_DOWNLOAD_ERRORS.END_BEFORE_START,
|
|
70
|
+
message: "dateRangeEnd must not be before dateRangeStart",
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
return [];
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// Returns an array of { code, message } errors; empty array = valid. AQI
|
|
78
|
+
// downloads only — Sample downloads have no range limit. Assumes the caller
|
|
79
|
+
// has already confirmed order via validateDateOrder (a reversed range isn't
|
|
80
|
+
// re-reported here as "too long").
|
|
81
|
+
export const validateMaxDateRange = ({ start, end } = {}) => {
|
|
82
|
+
const times = toTimes(start, end);
|
|
83
|
+
if (!times || times.endTime < times.startTime) {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
if (times.endTime - times.startTime > MAX_RANGE_MS) {
|
|
87
|
+
return [
|
|
88
|
+
{
|
|
89
|
+
code: DATA_DOWNLOAD_ERRORS.RANGE_TOO_LONG,
|
|
90
|
+
message: `date range cannot exceed ${MAX_RANGE_DAYS} days`,
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
return [];
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
98
|
+
|
|
99
|
+
// Email is optional (JA-3080) — a blank form field means "not sharing my
|
|
100
|
+
// email", not "invalid email". Both the schema (as a Mongoose `set`) and
|
|
101
|
+
// JA-3081's intake endpoint (before ever constructing a document) need this
|
|
102
|
+
// same "blank string collapses to null" rule, so it lives here rather than
|
|
103
|
+
// only on the schema.
|
|
104
|
+
export const normalizeDownloadEmail = (value) =>
|
|
105
|
+
typeof value === "string" && value.trim() === "" ? null : value;
|
|
106
|
+
|
|
107
|
+
// Returns an array of { code, message } errors; empty array = valid. null/
|
|
108
|
+
// undefined is valid — call normalizeDownloadEmail first so a blank string
|
|
109
|
+
// reaches this as null instead of failing the pattern check.
|
|
110
|
+
export const validateDownloadEmail = ({ email } = {}) => {
|
|
111
|
+
if (email === null || email === undefined || EMAIL_PATTERN.test(email)) {
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
return [
|
|
115
|
+
{
|
|
116
|
+
code: DATA_DOWNLOAD_ERRORS.INVALID_EMAIL,
|
|
117
|
+
message: "email must be a valid email address",
|
|
118
|
+
},
|
|
119
|
+
];
|
|
120
|
+
};
|
package/src/utils/quietHours.js
CHANGED
|
@@ -16,13 +16,19 @@
|
|
|
16
16
|
// Boundary rule: start is inclusive, end is exclusive — a 22:00–07:00 user
|
|
17
17
|
// is suppressed at 22:00 sharp and receives alerts again at 07:00 sharp.
|
|
18
18
|
|
|
19
|
+
import { DEFAULT_TIMEZONE } from "../constants/timezones.js";
|
|
20
|
+
import { getFormatter, isValidIANATimezone } from "./timezone.js";
|
|
21
|
+
|
|
22
|
+
// Re-exported for existing backend imports — canonical definitions now live
|
|
23
|
+
// in src/constants/timezones.js (Mongoose-free, browser-safe) and
|
|
24
|
+
// ./timezone.js (generic IANA checks, nothing quiet-hours-specific).
|
|
25
|
+
export { DEFAULT_TIMEZONE, isValidIANATimezone };
|
|
26
|
+
|
|
19
27
|
export const DEFAULT_QUIET_HOURS = Object.freeze({
|
|
20
28
|
start: "22:00",
|
|
21
29
|
end: "07:00",
|
|
22
30
|
});
|
|
23
31
|
|
|
24
|
-
export const DEFAULT_TIMEZONE = "America/New_York";
|
|
25
|
-
|
|
26
32
|
// Stable error codes for validateQuietHours. Consumers filter on `code`
|
|
27
33
|
// (e.g. the users schema pre-validate hook re-attaches only cross-field
|
|
28
34
|
// errors) — never on message wording, which is free to change.
|
|
@@ -34,40 +40,9 @@ export const QUIET_HOURS_ERRORS = Object.freeze({
|
|
|
34
40
|
|
|
35
41
|
const HHMM_PATTERN = /^([01]\d|2[0-3]):[0-5]\d$/;
|
|
36
42
|
|
|
37
|
-
// Formatter construction is the expensive part of Intl (~ms); formatToParts
|
|
38
|
-
// on a cached formatter is cheap. The cache also makes validation and
|
|
39
|
-
// evaluation share one authority: a timezone is valid iff Intl accepts it.
|
|
40
|
-
const formatterCache = new Map();
|
|
41
|
-
|
|
42
|
-
const getFormatter = (timeZone) => {
|
|
43
|
-
let formatter = formatterCache.get(timeZone);
|
|
44
|
-
if (formatter === undefined) {
|
|
45
|
-
formatter = new Intl.DateTimeFormat("en-US", {
|
|
46
|
-
timeZone,
|
|
47
|
-
hourCycle: "h23",
|
|
48
|
-
hour: "2-digit",
|
|
49
|
-
minute: "2-digit",
|
|
50
|
-
});
|
|
51
|
-
formatterCache.set(timeZone, formatter);
|
|
52
|
-
}
|
|
53
|
-
return formatter;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
43
|
export const isValidTimeString = (value) =>
|
|
57
44
|
typeof value === "string" && HHMM_PATTERN.test(value);
|
|
58
45
|
|
|
59
|
-
export const isValidIANATimezone = (timeZone) => {
|
|
60
|
-
if (typeof timeZone !== "string" || timeZone.trim() === "") {
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
try {
|
|
64
|
-
getFormatter(timeZone);
|
|
65
|
-
return true;
|
|
66
|
-
} catch {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
46
|
// Returns an array of { code, message } errors; empty array = valid. Codes
|
|
72
47
|
// come from QUIET_HOURS_ERRORS and are the stable API — messages are
|
|
73
48
|
// human-facing and may be reworded freely. Valid states are exactly two:
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MAX_RANGE_DAYS,
|
|
3
|
+
MAX_RANGE_MS,
|
|
4
|
+
DATA_DOWNLOAD_ERRORS,
|
|
5
|
+
validateDateOrder,
|
|
6
|
+
validateMaxDateRange,
|
|
7
|
+
validateDownloadEmail,
|
|
8
|
+
normalizeDownloadEmail,
|
|
9
|
+
} from "../dataDownloadRange.js";
|
|
10
|
+
|
|
11
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
12
|
+
const HOUR_MS = 60 * 60 * 1000;
|
|
13
|
+
const start = new Date("2026-01-01T00:00:00.000Z");
|
|
14
|
+
|
|
15
|
+
// S1: MAX_RANGE_DAYS alone under-describes the enforced bound. A consumer
|
|
16
|
+
// needing the real cap (e.g. to clamp a date picker) should import
|
|
17
|
+
// MAX_RANGE_MS directly rather than recomputing MAX_RANGE_DAYS * 24h and
|
|
18
|
+
// silently building a stricter limit than validateMaxDateRange enforces.
|
|
19
|
+
test("MAX_RANGE_MS is MAX_RANGE_DAYS plus the 1-hour DST grace, not a plain day-to-ms conversion", () => {
|
|
20
|
+
expect(MAX_RANGE_MS).toBe(MAX_RANGE_DAYS * DAY_MS + HOUR_MS);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("validateDateOrder (applies to every download kind)", () => {
|
|
24
|
+
test("returns no errors when start/end are missing", () => {
|
|
25
|
+
expect(validateDateOrder({})).toEqual([]);
|
|
26
|
+
expect(validateDateOrder({ start })).toEqual([]);
|
|
27
|
+
expect(validateDateOrder({ end: start })).toEqual([]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("returns no errors for a valid order", () => {
|
|
31
|
+
const end = new Date(start.getTime() + DAY_MS);
|
|
32
|
+
expect(validateDateOrder({ start, end })).toEqual([]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("flags end before start with END_BEFORE_START", () => {
|
|
36
|
+
const end = new Date(start.getTime() - DAY_MS);
|
|
37
|
+
expect(validateDateOrder({ start, end })).toEqual([
|
|
38
|
+
expect.objectContaining({ code: DATA_DOWNLOAD_ERRORS.END_BEFORE_START }),
|
|
39
|
+
]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("does not flag a range far longer than MAX_RANGE_DAYS — that's validateMaxDateRange's job", () => {
|
|
43
|
+
const end = new Date(start.getTime() + 365 * DAY_MS);
|
|
44
|
+
expect(validateDateOrder({ start, end })).toEqual([]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// JA-3081's intake endpoint hands this raw JSON request-body strings, not
|
|
48
|
+
// Date instances — must not silently misjudge order via NaN-producing
|
|
49
|
+
// string arithmetic (see dataDownloadRange.js's toTimes coercion comment).
|
|
50
|
+
test("flags end before start given as raw date strings", () => {
|
|
51
|
+
expect(
|
|
52
|
+
validateDateOrder({ start: "2026-05-01", end: "2026-01-01" })
|
|
53
|
+
).toEqual([expect.objectContaining({ code: DATA_DOWNLOAD_ERRORS.END_BEFORE_START })]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("returns no errors for an unparsable date value", () => {
|
|
57
|
+
expect(validateDateOrder({ start: "not-a-date", end: start })).toEqual([]);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("validateMaxDateRange (AQI only — Sample downloads have no cap)", () => {
|
|
62
|
+
test("returns no errors when start/end are missing", () => {
|
|
63
|
+
expect(validateMaxDateRange({})).toEqual([]);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("accepts exactly MAX_RANGE_DAYS", () => {
|
|
67
|
+
const end = new Date(start.getTime() + MAX_RANGE_DAYS * DAY_MS);
|
|
68
|
+
expect(validateMaxDateRange({ start, end })).toEqual([]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// The +1h grace exists so a calendar-accurate 31-day span crossing a DST
|
|
72
|
+
// fall-back transition (745 real hours, not 744) still validates — see
|
|
73
|
+
// dataDownloadRange.js's MAX_RANGE_MS comment.
|
|
74
|
+
test("accepts MAX_RANGE_DAYS plus the 1-hour DST grace", () => {
|
|
75
|
+
const end = new Date(start.getTime() + MAX_RANGE_DAYS * DAY_MS + HOUR_MS);
|
|
76
|
+
expect(validateMaxDateRange({ start, end })).toEqual([]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("flags a range longer than MAX_RANGE_DAYS plus the DST grace with RANGE_TOO_LONG", () => {
|
|
80
|
+
const end = new Date(start.getTime() + MAX_RANGE_DAYS * DAY_MS + HOUR_MS + 1);
|
|
81
|
+
expect(validateMaxDateRange({ start, end })).toEqual([
|
|
82
|
+
expect.objectContaining({ code: DATA_DOWNLOAD_ERRORS.RANGE_TOO_LONG }),
|
|
83
|
+
]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("flags a too-long range given as raw date strings (JSON request-body shape)", () => {
|
|
87
|
+
expect(
|
|
88
|
+
validateMaxDateRange({ start: "2026-01-01", end: "2027-06-01" })
|
|
89
|
+
).toEqual([expect.objectContaining({ code: DATA_DOWNLOAD_ERRORS.RANGE_TOO_LONG })]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("does not re-report a reversed range as RANGE_TOO_LONG — validateDateOrder owns that", () => {
|
|
93
|
+
const end = new Date(start.getTime() - DAY_MS);
|
|
94
|
+
expect(validateMaxDateRange({ start, end })).toEqual([]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("returns no errors for an unparsable date value", () => {
|
|
98
|
+
expect(validateMaxDateRange({ start: "not-a-date", end: start })).toEqual([]);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe("normalizeDownloadEmail (blank means 'not shared', not invalid)", () => {
|
|
103
|
+
test("collapses an empty or whitespace-only string to null", () => {
|
|
104
|
+
expect(normalizeDownloadEmail("")).toBeNull();
|
|
105
|
+
expect(normalizeDownloadEmail(" ")).toBeNull();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("passes through a non-blank string unchanged", () => {
|
|
109
|
+
expect(normalizeDownloadEmail("someone@example.com")).toBe("someone@example.com");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("passes through non-string values unchanged — not this function's job to type-check", () => {
|
|
113
|
+
expect(normalizeDownloadEmail(null)).toBeNull();
|
|
114
|
+
expect(normalizeDownloadEmail(undefined)).toBeUndefined();
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe("validateDownloadEmail (email is optional — JA-3080)", () => {
|
|
119
|
+
test("returns no errors when email is null or undefined", () => {
|
|
120
|
+
expect(validateDownloadEmail({})).toEqual([]);
|
|
121
|
+
expect(validateDownloadEmail({ email: null })).toEqual([]);
|
|
122
|
+
expect(validateDownloadEmail({ email: undefined })).toEqual([]);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("returns no errors for a well-formed email", () => {
|
|
126
|
+
expect(validateDownloadEmail({ email: "someone@example.com" })).toEqual([]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("flags a malformed email with INVALID_EMAIL", () => {
|
|
130
|
+
expect(validateDownloadEmail({ email: "not an email" })).toEqual([
|
|
131
|
+
expect.objectContaining({ code: DATA_DOWNLOAD_ERRORS.INVALID_EMAIL }),
|
|
132
|
+
]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Caller is expected to run normalizeDownloadEmail first — an un-normalized
|
|
136
|
+
// blank string is not the same as null and should still fail the pattern.
|
|
137
|
+
test("flags a blank string that hasn't been normalized to null yet", () => {
|
|
138
|
+
expect(validateDownloadEmail({ email: " " })).toEqual([
|
|
139
|
+
expect.objectContaining({ code: DATA_DOWNLOAD_ERRORS.INVALID_EMAIL }),
|
|
140
|
+
]);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { getFormatter, isValidIANATimezone } from '../timezone.js';
|
|
2
|
+
|
|
3
|
+
describe('isValidIANATimezone', () => {
|
|
4
|
+
test.each([
|
|
5
|
+
'America/New_York',
|
|
6
|
+
'America/Detroit',
|
|
7
|
+
'America/Los_Angeles',
|
|
8
|
+
'UTC',
|
|
9
|
+
'Asia/Tokyo',
|
|
10
|
+
])('accepts %s', (tz) => {
|
|
11
|
+
expect(isValidIANATimezone(tz)).toBe(true);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test.each([
|
|
15
|
+
'America/Fake_City',
|
|
16
|
+
'Not/A_Zone',
|
|
17
|
+
'EST GMT',
|
|
18
|
+
'',
|
|
19
|
+
' ',
|
|
20
|
+
])('rejects %p', (tz) => {
|
|
21
|
+
expect(isValidIANATimezone(tz)).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test.each([null, undefined, 5, {}])('rejects non-string %p', (tz) => {
|
|
25
|
+
expect(isValidIANATimezone(tz)).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('getFormatter', () => {
|
|
30
|
+
test('returns a formatter that renders wall-clock time in the given zone', () => {
|
|
31
|
+
const formatter = getFormatter('UTC');
|
|
32
|
+
const parts = formatter.formatToParts(new Date('2026-01-01T13:45:00.000Z'));
|
|
33
|
+
const hour = parts.find((p) => p.type === 'hour').value;
|
|
34
|
+
const minute = parts.find((p) => p.type === 'minute').value;
|
|
35
|
+
expect(`${hour}:${minute}`).toBe('13:45');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('caches the formatter instance per timezone', () => {
|
|
39
|
+
expect(getFormatter('America/New_York')).toBe(getFormatter('America/New_York'));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('throws for an invalid timezone — isValidIANATimezone exists to guard this', () => {
|
|
43
|
+
expect(() => getFormatter('Not/A_Zone')).toThrow();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Generic IANA timezone utilities. No quiet-hours domain logic here (unlike
|
|
2
|
+
// quietHours.js, which imports isValidIANATimezone from this file) so that
|
|
3
|
+
// other consumers of the timezone vocabulary — e.g. dataDownloads.js's
|
|
4
|
+
// `timezone` field (JA-3245) — don't have to reach into an unrelated
|
|
5
|
+
// domain's module to validate one. Re-exported from quietHours.js so
|
|
6
|
+
// existing imports (including the library barrel) keep working.
|
|
7
|
+
|
|
8
|
+
// Formatter construction is the expensive part of Intl (~ms); formatToParts
|
|
9
|
+
// on a cached formatter is cheap. The cache also makes validation and
|
|
10
|
+
// zone-minute lookups (quietHours.js's getZoneMinutes) share one authority:
|
|
11
|
+
// a timezone is valid iff Intl accepts it.
|
|
12
|
+
const formatterCache = new Map();
|
|
13
|
+
|
|
14
|
+
export const getFormatter = (timeZone) => {
|
|
15
|
+
let formatter = formatterCache.get(timeZone);
|
|
16
|
+
if (formatter === undefined) {
|
|
17
|
+
formatter = new Intl.DateTimeFormat("en-US", {
|
|
18
|
+
timeZone,
|
|
19
|
+
hourCycle: "h23",
|
|
20
|
+
hour: "2-digit",
|
|
21
|
+
minute: "2-digit",
|
|
22
|
+
});
|
|
23
|
+
formatterCache.set(timeZone, formatter);
|
|
24
|
+
}
|
|
25
|
+
return formatter;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const isValidIANATimezone = (timeZone) => {
|
|
29
|
+
if (typeof timeZone !== "string" || timeZone.trim() === "") {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
getFormatter(timeZone);
|
|
34
|
+
return true;
|
|
35
|
+
} catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
};
|