@justair/justair-library 7.5.0-alpha.0d28a9d → 7.5.0-alpha.477bdf3
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/deviceTypes.d.ts +12 -6
- package/dist/constants/deviceTypes.d.ts.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/models/users.d.ts.map +1 -1
- package/dist/utils/subscribedDevices.d.ts +31 -0
- package/dist/utils/subscribedDevices.d.ts.map +1 -0
- package/dist/utils/tests/subscribedDevices.test.d.ts +2 -0
- package/dist/utils/tests/subscribedDevices.test.d.ts.map +1 -0
- package/dist/utils/tests/subscribedDevicesBarrel.test.d.ts +2 -0
- package/dist/utils/tests/subscribedDevicesBarrel.test.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/constants/deviceTypes.js +16 -3
- package/src/constants/tests/deviceTypes.test.js +5 -0
- package/src/index.js +27 -0
- package/src/models/users.js +38 -11
- package/src/utils/subscribedDevices.js +429 -0
- package/src/utils/tests/subscribedDevices.test.js +665 -0
- package/src/utils/tests/subscribedDevicesBarrel.test.js +42 -0
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
export const DEVICE_TYPE_VALUES:
|
|
1
|
+
export const DEVICE_TYPES: Readonly<{
|
|
2
|
+
MONITOR: "monitor";
|
|
3
|
+
SAMPLE_SITE: "sample-site";
|
|
4
|
+
LINKED_DEVICE: "linked-device";
|
|
5
|
+
}>;
|
|
6
|
+
export const DEVICE_TYPE_VALUES: readonly ("monitor" | "sample-site" | "linked-device")[];
|
|
7
7
|
export function isAqiCapableDeviceType(deviceType: any): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* The device-type vocabulary as a type, so consumers get autocomplete and a
|
|
10
|
+
* compile error on a mis-cased value like "sampleSite" — the exact bug the
|
|
11
|
+
* runtime comments in utils/subscribedDevices.js warn about at length.
|
|
12
|
+
*/
|
|
13
|
+
export type DeviceType = (typeof DEVICE_TYPES)[keyof typeof DEVICE_TYPES];
|
|
8
14
|
//# sourceMappingURL=deviceTypes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deviceTypes.d.ts","sourceRoot":"","sources":["../../src/constants/deviceTypes.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"deviceTypes.d.ts","sourceRoot":"","sources":["../../src/constants/deviceTypes.js"],"names":[],"mappings":"AA0BA;;;;GAIG;AAEH,0FAA6E;AAiBtE,iEAEoC;;;;;;yBAZ9B,CAAA,OAAO,YAAY,EAAC,MAAM,OAAO,YAAY,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -106,6 +106,10 @@ import { getAlertWeight } from "./constants/alertLevels.js";
|
|
|
106
106
|
import { DEVICE_TYPES } from "./constants/deviceTypes.js";
|
|
107
107
|
import { DEVICE_TYPE_VALUES } from "./constants/deviceTypes.js";
|
|
108
108
|
import { isAqiCapableDeviceType } from "./constants/deviceTypes.js";
|
|
109
|
+
import { projectSubscribedMonitors } from "./utils/subscribedDevices.js";
|
|
110
|
+
import { resolveMonitorIdsByUser } from "./utils/subscribedDevices.js";
|
|
111
|
+
import { VISIBLE_SITE_MONITOR_FILTER } from "./utils/subscribedDevices.js";
|
|
112
|
+
import { SUBSCRIBED_DEVICES_ERRORS } from "./utils/subscribedDevices.js";
|
|
109
113
|
import { splitName } from "./utils/splitName.js";
|
|
110
114
|
import { DEFAULT_QUIET_HOURS } from "./utils/quietHours.js";
|
|
111
115
|
import { DEFAULT_TIMEZONE } from "./constants/timezones.js";
|
|
@@ -116,5 +120,5 @@ import { validateQuietHours } from "./utils/quietHours.js";
|
|
|
116
120
|
import { getZoneMinutes } from "./utils/quietHours.js";
|
|
117
121
|
import { isInQuietWindow } from "./utils/quietHours.js";
|
|
118
122
|
import { resolveQuietHours } from "./utils/quietHours.js";
|
|
119
|
-
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, DATA_DOWNLOAD_TYPES, dataDownloadSchema, DataDownloads, AqiDataDownload, SampleDataDownload, DATA_DOWNLOAD_MAX_RANGE_DAYS, DATA_DOWNLOAD_ERRORS, validateDataDownloadDateOrder, validateDataDownloadMaxDateRange, validateDataDownloadEmail, normalizeDataDownloadEmail, validateDataDownloadTimezone, normalizeDataDownloadTimezone, PARAMETERS, HEAVY_METALS, ALERT_LEVEL_WEIGHTS, getAlertWeight, DEVICE_TYPES, DEVICE_TYPE_VALUES, isAqiCapableDeviceType, splitName, DEFAULT_QUIET_HOURS, DEFAULT_TIMEZONE, QUIET_HOURS_ERRORS, isValidTimeString, isValidIANATimezone, validateQuietHours, getZoneMinutes, isInQuietWindow, resolveQuietHours };
|
|
123
|
+
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, DATA_DOWNLOAD_TYPES, dataDownloadSchema, DataDownloads, AqiDataDownload, SampleDataDownload, DATA_DOWNLOAD_MAX_RANGE_DAYS, DATA_DOWNLOAD_ERRORS, validateDataDownloadDateOrder, validateDataDownloadMaxDateRange, validateDataDownloadEmail, normalizeDataDownloadEmail, validateDataDownloadTimezone, normalizeDataDownloadTimezone, PARAMETERS, HEAVY_METALS, ALERT_LEVEL_WEIGHTS, getAlertWeight, DEVICE_TYPES, DEVICE_TYPE_VALUES, isAqiCapableDeviceType, projectSubscribedMonitors, resolveMonitorIdsByUser, VISIBLE_SITE_MONITOR_FILTER, SUBSCRIBED_DEVICES_ERRORS, splitName, DEFAULT_QUIET_HOURS, DEFAULT_TIMEZONE, QUIET_HOURS_ERRORS, isValidTimeString, isValidIANATimezone, validateQuietHours, getZoneMinutes, isInQuietWindow, resolveQuietHours };
|
|
120
124
|
//# 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":"AA+JA;;;iBAEC;yBA/FwB,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;4BA8CT,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;sCAlDtF,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;oCApBF,8BAA8B;mCAiE3D,2BAA2B;8BAA3B,2BAA2B;gCAA3B,2BAA2B;mCAA3B,2BAA2B;+DAsB3B,mCAAmC;qCAAnC,mCAAmC;mEAAnC,mCAAmC;yEAAnC,mCAAmC;mEAAnC,mCAAmC;qEAAnC,mCAAmC;yEAAnC,mCAAmC;2EAAnC,mCAAmC;2BA1FD,2BAA2B;6BAA3B,2BAA2B;oCAChB,4BAA4B;+BAA5B,4BAA4B;6BAOzE,4BAA4B;mCAA5B,4BAA4B;uCAA5B,4BAA4B;0CAgC5B,8BAA8B;wCAA9B,8BAA8B;4CAA9B,8BAA8B;0CAA9B,8BAA8B;0BAhBX,sBAAsB;oCASzC,uBAAuB;iCA/BG,0BAA0B;mCA+BpD,uBAAuB;kCAAvB,uBAAuB;oCACM,qBAAqB;mCADlD,uBAAuB;+BAAvB,uBAAuB;gCAAvB,uBAAuB;kCAAvB,uBAAuB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"users.d.ts","sourceRoot":"","sources":["../../src/models/users.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"users.d.ts","sourceRoot":"","sources":["../../src/models/users.js"],"names":[],"mappings":"AAyFA,8BAgGE;AAiCF,wBAAmD;AAjNnD,iDAA4D;AAC5D,6CAAmD;AAUnD,4DAsBC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const SUBSCRIBED_DEVICES_ERRORS: Readonly<{
|
|
2
|
+
MISSING_SITE_RESOLVER: "missing_site_resolver";
|
|
3
|
+
RESOLVER_RETURNED_NON_ARRAY: "resolver_returned_non_array";
|
|
4
|
+
RESOLVER_RETURNED_NESTED_ARRAYS: "resolver_returned_nested_arrays";
|
|
5
|
+
RESOLVER_RETURNED_DOCUMENTS: "resolver_returned_documents";
|
|
6
|
+
MISSING_COHORT_SITE_RESOLVER: "missing_cohort_site_resolver";
|
|
7
|
+
COHORT_RESOLVER_RETURNED_NON_MAP: "cohort_resolver_returned_non_map";
|
|
8
|
+
COHORT_USER_MISSING_ID: "cohort_user_missing_id";
|
|
9
|
+
}>;
|
|
10
|
+
export const VISIBLE_SITE_MONITOR_FILTER: Readonly<{
|
|
11
|
+
isPrivate: {
|
|
12
|
+
$ne: boolean;
|
|
13
|
+
};
|
|
14
|
+
isActive: true;
|
|
15
|
+
}>;
|
|
16
|
+
export function projectSubscribedMonitors(subscribedDevices?: Array<{
|
|
17
|
+
deviceId: import("mongoose").Types.ObjectId;
|
|
18
|
+
deviceType: import("../constants/deviceTypes.js").DeviceType;
|
|
19
|
+
}>, { resolveSiteMonitorIds }?: {
|
|
20
|
+
resolveSiteMonitorIds?: (siteIds: import("mongoose").Types.ObjectId[]) => Promise<Array<import("mongoose").Types.ObjectId | null | undefined>>;
|
|
21
|
+
}): Promise<import("mongoose").Types.ObjectId[]>;
|
|
22
|
+
export function resolveMonitorIdsByUser(users?: Array<{
|
|
23
|
+
_id: any;
|
|
24
|
+
subscribedDevices?: Array<{
|
|
25
|
+
deviceId: import("mongoose").Types.ObjectId;
|
|
26
|
+
deviceType: import("../constants/deviceTypes.js").DeviceType;
|
|
27
|
+
}>;
|
|
28
|
+
}>, { resolveVisibleSiteMonitors }?: {
|
|
29
|
+
resolveVisibleSiteMonitors?: (siteIds: import("mongoose").Types.ObjectId[]) => Promise<Map<string, import("mongoose").Types.ObjectId[]>>;
|
|
30
|
+
}): Promise<Map<string, import("mongoose").Types.ObjectId[]>>;
|
|
31
|
+
//# sourceMappingURL=subscribedDevices.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscribedDevices.d.ts","sourceRoot":"","sources":["../../src/utils/subscribedDevices.js"],"names":[],"mappings":"AAmEA;;;;;;;;GASG;AAyBH;;;;;GAGG;AAiLI,8DANI,KAAK,CAAC;IAAE,QAAQ,EAAE,OAAO,UAAU,EAAE,KAAK,CAAC,QAAQ,CAAC;IAC1D,UAAU,EAAE,OAAO,6BAA6B,EAAE,UAAU,CAAA;CAAE,CAAC,8BACzD;IAAE,qBAAqB,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,UAAU,EAAE,KAAK,CAAC,QAAQ,EAAE,KAC7E,OAAO,CAAC,KAAK,CAAC,OAAO,UAAU,EAAE,KAAK,CAAC,QAAQ,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC,CAAA;CAAE,GAC9D,OAAO,CAAC,OAAO,UAAU,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CA0BxD;AAkEM,gDAPI,KAAK,CAAC;IAAE,GAAG,EAAE,GAAG,CAAC;IAAC,iBAAiB,CAAC,EAAE,KAAK,CAAC;QAClD,QAAQ,EAAE,OAAO,UAAU,EAAE,KAAK,CAAC,QAAQ,CAAC;QAC5C,UAAU,EAAE,OAAO,6BAA6B,EAAE,UAAU,CAAA;KAAE,CAAC,CAAA;CAAE,CAAC,mCAC5D;IAAE,0BAA0B,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,UAAU,EAAE,KAAK,CAAC,QAAQ,EAAE,KAClF,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,UAAU,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAA;CAAE,GACnD,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,UAAU,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,CA2DrE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscribedDevices.test.d.ts","sourceRoot":"","sources":["../../../src/utils/tests/subscribedDevices.test.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscribedDevicesBarrel.test.d.ts","sourceRoot":"","sources":["../../../src/utils/tests/subscribedDevicesBarrel.test.js"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -19,13 +19,26 @@
|
|
|
19
19
|
//
|
|
20
20
|
// DO NOT rename these values: they are persisted in Users.subscribedDevices.
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
// Frozen because every importer in a process gets this same instance — one
|
|
23
|
+
// consumer reassigning a key, or calling .push()/.sort() on the values array,
|
|
24
|
+
// would silently change the vocabulary (and the Users.subscribedDevices enum
|
|
25
|
+
// derived from it) for every other consumer in that process. Matches
|
|
26
|
+
// DATA_DOWNLOAD_TYPES in dataDownloads.js.
|
|
27
|
+
export const DEVICE_TYPES = Object.freeze({
|
|
23
28
|
MONITOR: "monitor",
|
|
24
29
|
SAMPLE_SITE: "sample-site",
|
|
25
30
|
LINKED_DEVICE: "linked-device",
|
|
26
|
-
};
|
|
31
|
+
});
|
|
27
32
|
|
|
28
|
-
export const DEVICE_TYPE_VALUES = Object.values(DEVICE_TYPES);
|
|
33
|
+
export const DEVICE_TYPE_VALUES = Object.freeze(Object.values(DEVICE_TYPES));
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The device-type vocabulary as a type, so consumers get autocomplete and a
|
|
37
|
+
* compile error on a mis-cased value like "sampleSite" — the exact bug the
|
|
38
|
+
* runtime comments in utils/subscribedDevices.js warn about at length.
|
|
39
|
+
*
|
|
40
|
+
* @typedef {typeof DEVICE_TYPES[keyof typeof DEVICE_TYPES]} DeviceType
|
|
41
|
+
*/
|
|
29
42
|
|
|
30
43
|
// Whether a device of this type can ever report AQI, and therefore whether a
|
|
31
44
|
// subscription to it can contribute to AQI alert delivery.
|
|
@@ -29,6 +29,11 @@ test('DEVICE_TYPE_VALUES covers every type exactly once', () => {
|
|
|
29
29
|
expect(new Set(DEVICE_TYPE_VALUES).size).toBe(DEVICE_TYPE_VALUES.length);
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
+
test('the vocabulary is frozen so no consumer can mutate it for the others', () => {
|
|
33
|
+
expect(Object.isFrozen(DEVICE_TYPES)).toBe(true);
|
|
34
|
+
expect(Object.isFrozen(DEVICE_TYPE_VALUES)).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
|
|
32
37
|
describe('isAqiCapableDeviceType', () => {
|
|
33
38
|
test('monitor and linked-device can report AQI', () => {
|
|
34
39
|
expect(isAqiCapableDeviceType('monitor')).toBe(true);
|
package/src/index.js
CHANGED
|
@@ -100,6 +100,12 @@ import {
|
|
|
100
100
|
resolveQuietHours,
|
|
101
101
|
} from "./utils/quietHours.js";
|
|
102
102
|
import { isValidIANATimezone } from "./utils/timezone.js";
|
|
103
|
+
import {
|
|
104
|
+
projectSubscribedMonitors,
|
|
105
|
+
resolveMonitorIdsByUser,
|
|
106
|
+
VISIBLE_SITE_MONITOR_FILTER,
|
|
107
|
+
SUBSCRIBED_DEVICES_ERRORS,
|
|
108
|
+
} from "./utils/subscribedDevices.js";
|
|
103
109
|
|
|
104
110
|
// Sites related imports
|
|
105
111
|
import { sitesSchema, Sites } from "./models/sites.js";
|
|
@@ -268,6 +274,27 @@ export {
|
|
|
268
274
|
DEVICE_TYPES,
|
|
269
275
|
DEVICE_TYPE_VALUES,
|
|
270
276
|
isAqiCapableDeviceType,
|
|
277
|
+
// subscribedDevices → monitor ids, resolved at READ time (JA-3110 / JA-3335).
|
|
278
|
+
// Users.subscribedDevices is the source of truth; Users.subscribedMonitors is
|
|
279
|
+
// being retired rather than kept in step with it. Nothing should store this
|
|
280
|
+
// result — see the module header for the five bugs a stored copy caused.
|
|
281
|
+
//
|
|
282
|
+
// resolveMonitorIdsByUser — the batch form. One Sites lookup for a whole
|
|
283
|
+
// cohort. What alert runs, org pages and session
|
|
284
|
+
// responses should call.
|
|
285
|
+
// projectSubscribedMonitors — the single-user rule the batch form is built
|
|
286
|
+
// on. Named after the field being retired; it
|
|
287
|
+
// gets renamed when that field is dropped.
|
|
288
|
+
// VISIBLE_SITE_MONITOR_FILTER — the privacy predicate every consumer must
|
|
289
|
+
// apply when turning a Site into monitor ids.
|
|
290
|
+
// Sites.monitorIds is unfiltered; reading it
|
|
291
|
+
// straight through leaks private monitors.
|
|
292
|
+
// SUBSCRIBED_DEVICES_ERRORS — stable `err.code` vocabulary; consumers
|
|
293
|
+
// branch on it, never on message wording.
|
|
294
|
+
projectSubscribedMonitors,
|
|
295
|
+
resolveMonitorIdsByUser,
|
|
296
|
+
VISIBLE_SITE_MONITOR_FILTER,
|
|
297
|
+
SUBSCRIBED_DEVICES_ERRORS,
|
|
271
298
|
// Utilities
|
|
272
299
|
splitName,
|
|
273
300
|
// Quiet hours (JA-3054): shared validation + window evaluation
|
package/src/models/users.js
CHANGED
|
@@ -51,9 +51,37 @@ const validateCookieConsent = (consent) => {
|
|
|
51
51
|
// idempotent. With per-subdocument _ids every push would cast to a brand-new
|
|
52
52
|
// object, `$addToSet` would never match, and the array would accumulate a
|
|
53
53
|
// duplicate on every click of the subscribe button.
|
|
54
|
+
//
|
|
55
|
+
// Note what that dedupe is keyed on: the whole PAIR, not the deviceId. One id
|
|
56
|
+
// stored once as `monitor` and once as `linked-device` is two distinct entries
|
|
57
|
+
// and both survive. A given deviceId must therefore never carry two types —
|
|
58
|
+
// projectSubscribedMonitors would read the `monitor` row as a monitor id and
|
|
59
|
+
// write a Site id into subscribedMonitors, which is precisely the polymorphic
|
|
60
|
+
// id the subscribedMonitors/subscribedDevices split exists to keep out, and it
|
|
61
|
+
// fails silently (the notification service just filters it away). Consumers on
|
|
62
|
+
// the subscribe path should `$pull { deviceId }` before `$addToSet` so a
|
|
63
|
+
// re-subscribe with a corrected type replaces the row instead of adding one.
|
|
64
|
+
//
|
|
65
|
+
// That is two sequential updates, not one. MongoDB rejects `$pull` and
|
|
66
|
+
// `$addToSet` on the same path in a single update document ("would create a
|
|
67
|
+
// conflict at 'subscribedDevices'"), so the pull must be awaited before the
|
|
68
|
+
// add. Between the two writes the user reads as unsubscribed from that device;
|
|
69
|
+
// the window is only worth closing with a transaction if a concurrent read
|
|
70
|
+
// acting on it would be harmful.
|
|
54
71
|
const subscribedDeviceSchema = mongoose.Schema(
|
|
55
72
|
{
|
|
56
73
|
deviceId: { type: mongoose.Types.ObjectId, required: true },
|
|
74
|
+
// `enum` and `required` are document validators: they run on
|
|
75
|
+
// create()/save(), NOT on the `$addToSet` query update the subscribe
|
|
76
|
+
// path uses — `runValidators` defaults to false there. So an
|
|
77
|
+
// updateOne can persist `sampleSite`, `Monitor`, or any other string
|
|
78
|
+
// with no error.
|
|
79
|
+
//
|
|
80
|
+
// This fails silently and loudly nowhere: a bad-cased value makes
|
|
81
|
+
// isAqiCapableDeviceType() return false, so a user who subscribed to a
|
|
82
|
+
// monitor just never receives AQI alerts. Consumers on the subscribe
|
|
83
|
+
// path MUST check the request body against DEVICE_TYPE_VALUES before
|
|
84
|
+
// writing (and/or pass `{ runValidators: true }` to the update).
|
|
57
85
|
deviceType: { type: String, enum: DEVICE_TYPE_VALUES, required: true },
|
|
58
86
|
},
|
|
59
87
|
{ _id: false }
|
|
@@ -75,18 +103,17 @@ const usersSchema = mongoose.Schema(
|
|
|
75
103
|
// Subscribed devices (JA-3110 / JA-3335) — the source of truth for what the
|
|
76
104
|
// user has added to "My Monitors", across all three device types.
|
|
77
105
|
//
|
|
78
|
-
//
|
|
79
|
-
//
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
-
//
|
|
106
|
+
// subscribedMonitors above is its derived AQI-alert projection (monitor ids
|
|
107
|
+
// only). The two must be written together on every subscribe and
|
|
108
|
+
// unsubscribe path, using `projectSubscribedMonitors` rather than a
|
|
109
|
+
// per-consumer reimplementation. It is deliberately not a schema validator:
|
|
110
|
+
// the projection needs a Site lookup, which the schema has no business
|
|
111
|
+
// doing.
|
|
83
112
|
//
|
|
84
|
-
//
|
|
85
|
-
// and
|
|
86
|
-
//
|
|
87
|
-
//
|
|
88
|
-
// polymorphic ids in their own field preserves that invariant for every
|
|
89
|
-
// library consumer.
|
|
113
|
+
// Everything else — the per-device-type rule, the backfill PRECONDITION,
|
|
114
|
+
// the STALENESS gap, and why the field is split at all — is specified once,
|
|
115
|
+
// in the header of src/utils/subscribedDevices.js. Read it there; it is
|
|
116
|
+
// deliberately not summarised here.
|
|
90
117
|
subscribedDevices: [subscribedDeviceSchema],
|
|
91
118
|
currentAlertMode: {
|
|
92
119
|
type: String,
|