@justair/justair-library 7.5.0-alpha.ed6e6e7 → 7.6.0-alpha.2ec581a

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,4 +5,11 @@ export const DEVICE_TYPES: Readonly<{
5
5
  }>;
6
6
  export const DEVICE_TYPE_VALUES: readonly ("monitor" | "sample-site" | "linked-device")[];
7
7
  export function isAqiCapableDeviceType(deviceType: any): boolean;
8
+ export const AQI_CAPABLE_DEVICE_TYPES: readonly ("monitor" | "sample-site" | "linked-device")[];
9
+ /**
10
+ * The device-type vocabulary as a type, so consumers get autocomplete and a
11
+ * compile error on a mis-cased value like "sampleSite" — the exact bug the
12
+ * runtime comments in utils/subscribedDevices.js warn about at length.
13
+ */
14
+ export type DeviceType = (typeof DEVICE_TYPES)[keyof typeof DEVICE_TYPES];
8
15
  //# sourceMappingURL=deviceTypes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"deviceTypes.d.ts","sourceRoot":"","sources":["../../src/constants/deviceTypes.js"],"names":[],"mappings":"AA0BA;;;;GAIG;AAEH,0FAA6E;AAStE,iEAEoC"}
1
+ {"version":3,"file":"deviceTypes.d.ts","sourceRoot":"","sources":["../../src/constants/deviceTypes.js"],"names":[],"mappings":"AA0BA;;;;GAIG;AAEH,0FAA6E;AAiBtE,iEAEoC;AAiB3C,gGAEE;;;;;;yBA/BW,CAAA,OAAO,YAAY,EAAC,MAAM,OAAO,YAAY,CAAC"}
package/dist/index.d.ts CHANGED
@@ -106,7 +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 { AQI_CAPABLE_DEVICE_TYPES } from "./constants/deviceTypes.js";
109
110
  import { projectSubscribedMonitors } from "./utils/subscribedDevices.js";
111
+ import { resolveSubscribedMonitorIdsByUser } from "./utils/subscribedDevices.js";
112
+ import { VISIBLE_SITE_MONITOR_FILTER } from "./utils/subscribedDevices.js";
110
113
  import { SUBSCRIBED_DEVICES_ERRORS } from "./utils/subscribedDevices.js";
111
114
  import { splitName } from "./utils/splitName.js";
112
115
  import { DEFAULT_QUIET_HOURS } from "./utils/quietHours.js";
@@ -118,5 +121,5 @@ import { validateQuietHours } from "./utils/quietHours.js";
118
121
  import { getZoneMinutes } from "./utils/quietHours.js";
119
122
  import { isInQuietWindow } from "./utils/quietHours.js";
120
123
  import { resolveQuietHours } from "./utils/quietHours.js";
121
- 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, SUBSCRIBED_DEVICES_ERRORS, splitName, DEFAULT_QUIET_HOURS, DEFAULT_TIMEZONE, QUIET_HOURS_ERRORS, isValidTimeString, isValidIANATimezone, validateQuietHours, getZoneMinutes, isInQuietWindow, resolveQuietHours };
124
+ 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, AQI_CAPABLE_DEVICE_TYPES, projectSubscribedMonitors, resolveSubscribedMonitorIdsByUser, VISIBLE_SITE_MONITOR_FILTER, SUBSCRIBED_DEVICES_ERRORS, splitName, DEFAULT_QUIET_HOURS, DEFAULT_TIMEZONE, QUIET_HOURS_ERRORS, isValidTimeString, isValidIANATimezone, validateQuietHours, getZoneMinutes, isInQuietWindow, resolveQuietHours };
122
125
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"AA6JA;;;iBAEC;yBA7FwB,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,sBA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CAAmD;AAUnD,4DAsBC"}
1
+ {"version":3,"file":"users.d.ts","sourceRoot":"","sources":["../../src/models/users.js"],"names":[],"mappings":"AA6FA,8BA0GE;AAiCF,wBAAmD;AA/NnD,iDAA4D;AAC5D,6CAAmD;AAUnD,4DA0BC"}
@@ -1,12 +1,31 @@
1
1
  export const SUBSCRIBED_DEVICES_ERRORS: Readonly<{
2
2
  MISSING_SITE_RESOLVER: "missing_site_resolver";
3
3
  RESOLVER_RETURNED_NON_ARRAY: "resolver_returned_non_array";
4
+ RESOLVER_RETURNED_NESTED_ARRAYS: "resolver_returned_nested_arrays";
4
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: Readonly<{
12
+ $ne: true;
13
+ }>;
14
+ isActive: true;
5
15
  }>;
6
16
  export function projectSubscribedMonitors(subscribedDevices?: Array<{
7
17
  deviceId: import("mongoose").Types.ObjectId;
8
- deviceType: string;
18
+ deviceType: import("../constants/deviceTypes.js").DeviceType;
9
19
  }>, { resolveSiteMonitorIds }?: {
10
20
  resolveSiteMonitorIds?: (siteIds: import("mongoose").Types.ObjectId[]) => Promise<Array<import("mongoose").Types.ObjectId | null | undefined>>;
11
21
  }): Promise<import("mongoose").Types.ObjectId[]>;
22
+ export function resolveSubscribedMonitorIdsByUser(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[]>>;
12
31
  //# sourceMappingURL=subscribedDevices.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"subscribedDevices.d.ts","sourceRoot":"","sources":["../../src/utils/subscribedDevices.js"],"names":[],"mappings":"AA4EA;;;;GAIG;AAwII,8DALI,KAAK,CAAC;IAAE,QAAQ,EAAE,OAAO,UAAU,EAAE,KAAK,CAAC,QAAQ,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC,8BAC1E;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"}
1
+ {"version":3,"file":"subscribedDevices.d.ts","sourceRoot":"","sources":["../../src/utils/subscribedDevices.js"],"names":[],"mappings":"AA0FA;;;;;;;;GASG;AA+CH;;;;;GAGG;AA6LI,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;AA0EM,0DAPI,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,CAmErE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justair/justair-library",
3
- "version": "7.5.0-alpha.ed6e6e7",
3
+ "version": "7.6.0-alpha.2ec581a",
4
4
  "description": "JustAir Internal Library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -32,6 +32,14 @@ export const DEVICE_TYPES = Object.freeze({
32
32
 
33
33
  export const DEVICE_TYPE_VALUES = Object.freeze(Object.values(DEVICE_TYPES));
34
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
+ */
42
+
35
43
  // Whether a device of this type can ever report AQI, and therefore whether a
36
44
  // subscription to it can contribute to AQI alert delivery.
37
45
  //
@@ -42,3 +50,22 @@ export const DEVICE_TYPE_VALUES = Object.freeze(Object.values(DEVICE_TYPES));
42
50
  export const isAqiCapableDeviceType = (deviceType) =>
43
51
  deviceType === DEVICE_TYPES.MONITOR ||
44
52
  deviceType === DEVICE_TYPES.LINKED_DEVICE;
53
+
54
+ // The same predicate in the shape a Mongo query wants, so a cohort read can
55
+ // leave out rows that could never alert instead of loading and discarding
56
+ // them:
57
+ //
58
+ // { "subscribedDevices.deviceType": { $in: AQI_CAPABLE_DEVICE_TYPES } }
59
+ //
60
+ // Derived from isAqiCapableDeviceType's own answer rather than hand-listed, so
61
+ // the two cannot disagree if a fourth device type ever arrives. Frozen for the
62
+ // same reason DEVICE_TYPE_VALUES is: every importer in a process shares this
63
+ // instance, and one `.push()` would silently widen the audience for all of
64
+ // them.
65
+ //
66
+ // Carries the same asymmetry the predicate does: `linked-device` being in this
67
+ // list is necessary and NOT sufficient — a Site only actually alerts when it
68
+ // links at least one monitor, which no query on deviceType can tell you.
69
+ export const AQI_CAPABLE_DEVICE_TYPES = Object.freeze(
70
+ DEVICE_TYPE_VALUES.filter(isAqiCapableDeviceType),
71
+ );
@@ -8,12 +8,14 @@ import {
8
8
  DEVICE_TYPES,
9
9
  DEVICE_TYPE_VALUES,
10
10
  isAqiCapableDeviceType,
11
+ AQI_CAPABLE_DEVICE_TYPES,
11
12
  } from '../deviceTypes.js';
12
13
 
13
14
  test('barrel exports the device-type constants', () => {
14
15
  expect(barrel.DEVICE_TYPES).toBe(DEVICE_TYPES);
15
16
  expect(barrel.DEVICE_TYPE_VALUES).toBe(DEVICE_TYPE_VALUES);
16
17
  expect(barrel.isAqiCapableDeviceType).toBe(isAqiCapableDeviceType);
18
+ expect(barrel.AQI_CAPABLE_DEVICE_TYPES).toBe(AQI_CAPABLE_DEVICE_TYPES);
17
19
  });
18
20
 
19
21
  test('DEVICE_TYPES values match the strings the devices API emits', () => {
@@ -51,3 +53,23 @@ describe('isAqiCapableDeviceType', () => {
51
53
  expect(isAqiCapableDeviceType('')).toBe(false);
52
54
  });
53
55
  });
56
+
57
+ describe('AQI_CAPABLE_DEVICE_TYPES', () => {
58
+ test('is the $in form of isAqiCapableDeviceType, in vocabulary order', () => {
59
+ expect(AQI_CAPABLE_DEVICE_TYPES).toEqual(['monitor', 'linked-device']);
60
+ });
61
+
62
+ test('agrees with the predicate for every device type', () => {
63
+ // Derived from the predicate rather than hand-listed, so this pins that
64
+ // the derivation stays wired up if a fourth device type is added.
65
+ DEVICE_TYPE_VALUES.forEach((deviceType) => {
66
+ expect(AQI_CAPABLE_DEVICE_TYPES.includes(deviceType)).toBe(
67
+ isAqiCapableDeviceType(deviceType),
68
+ );
69
+ });
70
+ });
71
+
72
+ test('is frozen — every importer shares this instance', () => {
73
+ expect(Object.isFrozen(AQI_CAPABLE_DEVICE_TYPES)).toBe(true);
74
+ });
75
+ });
package/src/index.js CHANGED
@@ -57,7 +57,10 @@ import {
57
57
  DataCompleteness,
58
58
  dataCompletenessSchema,
59
59
  } from "./models/dataCompleteness.js";
60
- import { NetworkMetrics, networkMetricsSchema } from "./models/networkMetrics.js";
60
+ import {
61
+ NetworkMetrics,
62
+ networkMetricsSchema,
63
+ } from "./models/networkMetrics.js";
61
64
  import { RateOfChange, rateOfChangeSchema } from "./models/rateOfChange.js";
62
65
  import {
63
66
  ChangeStreamCheckpoints,
@@ -66,13 +69,17 @@ import {
66
69
  import Database from "./config/db.js"; // Import the new Database class
67
70
  import CustomLogger from "./config/logger.js";
68
71
  import { PARAMETERS, HEAVY_METALS } from "./constants/pollutants.js";
69
- import { ALERT_LEVEL_WEIGHTS, getAlertWeight } from "./constants/alertLevels.js";
72
+ import {
73
+ ALERT_LEVEL_WEIGHTS,
74
+ getAlertWeight,
75
+ } from "./constants/alertLevels.js";
70
76
  import { DEFAULT_TIMEZONE } from "./constants/timezones.js";
71
77
  import { DATA_DOWNLOAD_TYPES } from "./constants/dataDownloads.js";
72
78
  import {
73
79
  DEVICE_TYPES,
74
80
  DEVICE_TYPE_VALUES,
75
81
  isAqiCapableDeviceType,
82
+ AQI_CAPABLE_DEVICE_TYPES,
76
83
  } from "./constants/deviceTypes.js";
77
84
  import {
78
85
  monitorEpisodesSchema,
@@ -102,6 +109,8 @@ import {
102
109
  import { isValidIANATimezone } from "./utils/timezone.js";
103
110
  import {
104
111
  projectSubscribedMonitors,
112
+ resolveSubscribedMonitorIdsByUser,
113
+ VISIBLE_SITE_MONITOR_FILTER,
105
114
  SUBSCRIBED_DEVICES_ERRORS,
106
115
  } from "./utils/subscribedDevices.js";
107
116
 
@@ -125,7 +134,10 @@ import {
125
134
  SamplesAudit,
126
135
  sampleParameterReferenceConcentrations,
127
136
  } from "./models/samples.js";
128
- import { passwordResetTokenSchema, PasswordResetToken } from "./models/passwordResetToken.js";
137
+ import {
138
+ passwordResetTokenSchema,
139
+ PasswordResetToken,
140
+ } from "./models/passwordResetToken.js";
129
141
  import {
130
142
  dataDownloadSchema,
131
143
  DataDownloads,
@@ -272,12 +284,34 @@ export {
272
284
  DEVICE_TYPES,
273
285
  DEVICE_TYPE_VALUES,
274
286
  isAqiCapableDeviceType,
275
- // subscribedDevices → subscribedMonitors projection (JA-3110 / JA-3335).
276
- // Every consumer that writes a subscription recomputes the AQI audience with
277
- // this instead of re-implementing the per-device-type rule.
278
- // SUBSCRIBED_DEVICES_ERRORS is the stable `err.code` vocabulary for the
279
- // throw above; consumers branch on it rather than on message wording.
287
+ // The query form of isAqiCapableDeviceType, for narrowing a Users read to
288
+ // rows that could actually alert:
289
+ // { "subscribedDevices.deviceType": { $in: AQI_CAPABLE_DEVICE_TYPES } }
290
+ AQI_CAPABLE_DEVICE_TYPES,
291
+ // subscribedDevices → monitor ids, resolved at READ time (JA-3110 / JA-3335).
292
+ // Users.subscribedDevices is the source of truth; Users.subscribedMonitors is
293
+ // being retired rather than kept in step with it. Nothing should store this
294
+ // result — see the module header for the five bugs a stored copy caused.
295
+ //
296
+ // Both resolvers are only correct for users whose subscribedDevices has been
297
+ // backfilled from subscribedMonitors — read the PRECONDITION block in the
298
+ // module header before pointing a service at either of them.
299
+ //
300
+ // resolveSubscribedMonitorIdsByUser — the batch form. One Sites lookup for
301
+ // a whole cohort. What alert runs, org pages and
302
+ // session responses should call.
303
+ // projectSubscribedMonitors — the single-user rule the batch form is built
304
+ // on. Named after the field being retired; it
305
+ // gets renamed when that field is dropped.
306
+ // VISIBLE_SITE_MONITOR_FILTER — the privacy predicate every consumer must
307
+ // apply when turning a Site into monitor ids.
308
+ // Sites.monitorIds is unfiltered; reading it
309
+ // straight through leaks private monitors.
310
+ // SUBSCRIBED_DEVICES_ERRORS — stable `err.code` vocabulary; consumers
311
+ // branch on it, never on message wording.
280
312
  projectSubscribedMonitors,
313
+ resolveSubscribedMonitorIdsByUser,
314
+ VISIBLE_SITE_MONITOR_FILTER,
281
315
  SUBSCRIBED_DEVICES_ERRORS,
282
316
  // Utilities
283
317
  splitName,
@@ -15,11 +15,15 @@ const cookieConsentSchemaDefinition = COOKIE_CONSENT_CATEGORIES.reduce(
15
15
  definition[category] = { type: String, enum: COOKIE_CONSENT_VALUES };
16
16
  return definition;
17
17
  },
18
- {}
18
+ {},
19
19
  );
20
20
 
21
21
  const validateCookieConsent = (consent) => {
22
- if (typeof consent !== "object" || consent === null || Array.isArray(consent)) {
22
+ if (
23
+ typeof consent !== "object" ||
24
+ consent === null ||
25
+ Array.isArray(consent)
26
+ ) {
23
27
  return "cookie_consent must be an object";
24
28
  }
25
29
 
@@ -34,7 +38,7 @@ const validateCookieConsent = (consent) => {
34
38
  }
35
39
  if (!COOKIE_CONSENT_VALUES.includes(value)) {
36
40
  return `Invalid value for ${category}: must be one of ${COOKIE_CONSENT_VALUES.join(
37
- ", "
41
+ ", ",
38
42
  )}`;
39
43
  }
40
44
  }
@@ -84,7 +88,7 @@ const subscribedDeviceSchema = mongoose.Schema(
84
88
  // writing (and/or pass `{ runValidators: true }` to the update).
85
89
  deviceType: { type: String, enum: DEVICE_TYPE_VALUES, required: true },
86
90
  },
87
- { _id: false }
91
+ { _id: false },
88
92
  );
89
93
 
90
94
  const usersSchema = mongoose.Schema(
@@ -99,21 +103,31 @@ const usersSchema = mongoose.Schema(
99
103
  organizations: [{ type: mongoose.Types.ObjectId, ref: "Organizations" }],
100
104
  alertPhone: String,
101
105
  alertCount: { type: Number, default: 0 },
106
+ // BEING RETIRED (JA-3110 / JA-3335). This was a denormalized copy of the
107
+ // AQI-alert audience, rewritten on every subscribe and unsubscribe. Do not
108
+ // write it from new code and do not keep it in step with anything: the
109
+ // audience is now RESOLVED AT READ TIME from subscribedDevices below, and
110
+ // the field is dropped once every consumer has moved off it. Five distinct
111
+ // silent-failure bugs traced back to maintaining this copy; they are
112
+ // enumerated in the header of src/utils/subscribedDevices.js.
113
+ //
114
+ // It is still read (and still the correct thing to read) for any user
115
+ // whose subscribedDevices has not been backfilled yet — the same header's
116
+ // PRECONDITION block has the ordering.
102
117
  subscribedMonitors: [{ type: mongoose.Types.ObjectId, ref: "Monitors" }],
103
- // Subscribed devices (JA-3110 / JA-3335) — the source of truth for what the
118
+ // Subscribed devices (JA-3110 / JA-3335) — the SOURCE OF TRUTH for what the
104
119
  // user has added to "My Monitors", across all three device types.
105
120
  //
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.
121
+ // The monitor ids this implies are derived on read, never stored:
122
+ // `resolveSubscribedMonitorIdsByUser` for a cohort,
123
+ // `projectSubscribedMonitors` for one user, both exported from
124
+ // src/index.js. Deliberately not a schema validator — the derivation needs
125
+ // a Site lookup, which the schema has no business doing.
112
126
  //
113
127
  // 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.
128
+ // the privacy obligation, and why the field is split at all — is specified
129
+ // once, in the header of src/utils/subscribedDevices.js. Read it there; it
130
+ // is deliberately not summarised here.
117
131
  subscribedDevices: [subscribedDeviceSchema],
118
132
  currentAlertMode: {
119
133
  type: String,
@@ -182,7 +196,7 @@ const usersSchema = mongoose.Schema(
182
196
  },
183
197
  {
184
198
  timestamps: true,
185
- }
199
+ },
186
200
  );
187
201
 
188
202
  // Cross-field quiet-hours rules (both-or-neither, start ≠ end) delegated to