@justair/justair-library 4.8.18 → 4.8.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justair/justair-library",
3
- "version": "4.8.18",
3
+ "version": "4.8.19",
4
4
  "description": "JustAir Internal Library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -248,6 +248,8 @@ const monitorsSchema = mongoose.Schema(
248
248
  default: [],
249
249
  },
250
250
  applyCorrections: { type: Boolean, default: false },
251
+ // Optional readKey for private API access (e.g. PurpleAir)
252
+ readKey: { type: String, required: false },
251
253
  pausedParameters: {
252
254
  type: [{ parameter: String, timestamp: Date, isPaused: Boolean }],
253
255
  default: [],
@@ -61,6 +61,32 @@ const featureSchema = new Schema({
61
61
  },
62
62
  });
63
63
 
64
+ const computeResetTime = (rateLimitType, baseTime = new Date()) => {
65
+ const resetTime = new Date(baseTime);
66
+
67
+ switch (rateLimitType) {
68
+ case "second":
69
+ resetTime.setSeconds(resetTime.getSeconds() + 1);
70
+ break;
71
+ case "hourly":
72
+ resetTime.setHours(resetTime.getHours() + 1);
73
+ break;
74
+ case "daily":
75
+ resetTime.setDate(resetTime.getDate() + 1);
76
+ break;
77
+ case "weekly":
78
+ resetTime.setDate(resetTime.getDate() + 7);
79
+ break;
80
+ case "monthly":
81
+ resetTime.setMonth(resetTime.getMonth() + 1);
82
+ break;
83
+ default:
84
+ break;
85
+ }
86
+
87
+ return resetTime;
88
+ };
89
+
64
90
  const organizationsSchema = mongoose.Schema(
65
91
  {
66
92
  name: String,
@@ -85,6 +111,54 @@ const organizationsSchema = mongoose.Schema(
85
111
  }
86
112
  );
87
113
 
114
+ organizationsSchema.methods.setRateLimitProperties = function () {
115
+ if (!Array.isArray(this.orgAPIKey)) {
116
+ return;
117
+ }
118
+
119
+ const now = new Date();
120
+
121
+ this.orgAPIKey = this.orgAPIKey.map((apiKeyEntry) => {
122
+ if (!apiKeyEntry || !Array.isArray(apiKeyEntry.rateLimits)) {
123
+ return apiKeyEntry;
124
+ }
125
+
126
+ const updatedRateLimits = apiKeyEntry.rateLimits.map((rateLimit) => {
127
+ if (!rateLimit) {
128
+ return rateLimit;
129
+ }
130
+
131
+ const updatedRateLimit = { ...rateLimit };
132
+
133
+ if (updatedRateLimit.apiCount == null) {
134
+ updatedRateLimit.apiCount = 0;
135
+ }
136
+
137
+ if (!updatedRateLimit.resetTime) {
138
+ updatedRateLimit.resetTime = computeResetTime(
139
+ updatedRateLimit.type,
140
+ now
141
+ );
142
+ }
143
+
144
+ return updatedRateLimit;
145
+ });
146
+
147
+ return {
148
+ ...apiKeyEntry,
149
+ rateLimits: updatedRateLimits,
150
+ };
151
+ });
152
+ };
153
+
154
+ organizationsSchema.pre("save", function (next) {
155
+ if (this.isModified("orgAPIKey")) {
156
+ this.setRateLimitProperties();
157
+ }
158
+
159
+ next();
160
+ });
161
+
88
162
  // Name-based sorting index
89
163
  organizationsSchema.index({ name: 1 });
90
164
  // Connected monitors array queries