@skylord123/node-red-pebble-timeline 1.0.0 → 1.0.1

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": "@skylord123/node-red-pebble-timeline",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Node-RED nodes for interacting with the Pebble Timeline API",
5
5
  "keywords": [
6
6
  "node-red",
@@ -288,7 +288,7 @@ module.exports = function(RED) {
288
288
  node.status({fill: "green", shape: "dot", text: "OK"});
289
289
 
290
290
  // Store the pin in our local storage
291
- storePin(pin);
291
+ storePin(pin, timelineToken);
292
292
 
293
293
  // Prepare the output message
294
294
  msg.payload = {
@@ -336,8 +336,15 @@ module.exports = function(RED) {
336
336
  });
337
337
 
338
338
  // Helper to store a pin in local storage
339
- function storePin(pin) {
340
- const timelineToken = tokenOverride || configNode.credentials.timelineToken;
339
+ function storePin(pin, timelineToken) {
340
+ // Ensure we have a valid token
341
+ if (!timelineToken) {
342
+ node.warn("Cannot store pin: No valid timeline token provided");
343
+ return;
344
+ }
345
+
346
+ // Convert token to string to ensure it can be used as an object key
347
+ timelineToken = String(timelineToken);
341
348
 
342
349
  // Initialize the token's pins array if it doesn't exist
343
350
  if (!pinsData[timelineToken]) {
@@ -368,21 +375,40 @@ module.exports = function(RED) {
368
375
  function cleanupOldPins() {
369
376
  const oneMonthAgo = new Date();
370
377
  oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1);
378
+ let changed = false;
371
379
 
372
380
  // Iterate through all tokens
373
381
  Object.keys(pinsData).forEach(token => {
382
+ // Make sure the token's data is an array
383
+ if (!Array.isArray(pinsData[token])) {
384
+ pinsData[token] = [];
385
+ return;
386
+ }
387
+
374
388
  // Filter out pins older than 1 month
375
389
  const initialCount = pinsData[token].length;
376
390
  pinsData[token] = pinsData[token].filter(pin => {
377
- const storedDate = new Date(pin._stored);
378
- return storedDate >= oneMonthAgo;
391
+ // Make sure pin has _stored property
392
+ if (!pin || !pin._stored) return false;
393
+
394
+ try {
395
+ const storedDate = new Date(pin._stored);
396
+ return storedDate >= oneMonthAgo;
397
+ } catch (e) {
398
+ // If date parsing fails, remove the pin
399
+ return false;
400
+ }
379
401
  });
380
402
 
381
403
  // Log if pins were removed
382
404
  if (pinsData[token].length < initialCount) {
383
405
  node.debug(`Removed ${initialCount - pinsData[token].length} old pins for token ${token.substring(0, 8)}...`);
406
+ changed = true;
384
407
  }
385
408
  });
409
+
410
+ // No need to save here as the calling function will save the file
411
+ return changed;
386
412
  }
387
413
 
388
414
  // Apply node configuration to the pin
@@ -102,7 +102,7 @@ module.exports = function(RED) {
102
102
  node.status({fill: "green", shape: "dot", text: "Pin deleted"});
103
103
 
104
104
  // Remove the pin from our local storage
105
- removePin(pinId);
105
+ removePin(pinId, timelineToken);
106
106
 
107
107
  // Prepare the output message
108
108
  msg.payload = {
@@ -133,8 +133,15 @@ module.exports = function(RED) {
133
133
  });
134
134
 
135
135
  // Helper to remove a pin from local storage
136
- function removePin(pinId) {
137
- const timelineToken = node.tokenOverride || configNode.credentials.timelineToken;
136
+ function removePin(pinId, timelineToken) {
137
+ // Ensure we have a valid token
138
+ if (!timelineToken) {
139
+ node.warn("Cannot remove pin: No valid timeline token provided");
140
+ return;
141
+ }
142
+
143
+ // Convert token to string to ensure it can be used as an object key
144
+ timelineToken = String(timelineToken);
138
145
 
139
146
  // Check if this token has any pins
140
147
  if (!pinsData[timelineToken]) {
@@ -145,8 +152,7 @@ module.exports = function(RED) {
145
152
  const initialCount = pinsData[timelineToken].length;
146
153
  pinsData[timelineToken] = pinsData[timelineToken].filter(p => p.id !== pinId);
147
154
 
148
- // Clean up old pins (older than 1 month) from all tokens
149
- cleanupOldPins();
155
+ // Note: Cleanup of old pins is handled in the add node
150
156
 
151
157
  // Only write if we actually removed something
152
158
  if (pinsData[timelineToken].length !== initialCount) {
@@ -158,27 +164,6 @@ module.exports = function(RED) {
158
164
  }
159
165
  }
160
166
 
161
- // Helper to clean up pins older than 1 month
162
- function cleanupOldPins() {
163
- const oneMonthAgo = new Date();
164
- oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1);
165
-
166
- // Iterate through all tokens
167
- Object.keys(pinsData).forEach(token => {
168
- // Filter out pins older than 1 month
169
- const initialCount = pinsData[token].length;
170
- pinsData[token] = pinsData[token].filter(pin => {
171
- const storedDate = new Date(pin._stored);
172
- return storedDate >= oneMonthAgo;
173
- });
174
-
175
- // Log if pins were removed
176
- if (pinsData[token].length < initialCount) {
177
- node.debug(`Removed ${initialCount - pinsData[token].length} old pins for token ${token.substring(0, 8)}...`);
178
- }
179
- });
180
- }
181
-
182
167
  node.on('close', function() {
183
168
  // Clean up any resources
184
169
  });
@@ -105,11 +105,20 @@ module.exports = function(RED) {
105
105
  }
106
106
 
107
107
  // Get the timeline token to use
108
- const timelineToken = tokenOverride || configNode.credentials.timelineToken;
108
+ let timelineToken = tokenOverride || configNode.credentials.timelineToken;
109
+
110
+ // Ensure we have a valid token
111
+ if (!timelineToken) {
112
+ node.warn("No valid timeline token provided");
113
+ timelineToken = "default"; // Use a default key to avoid errors
114
+ }
115
+
116
+ // Convert token to string to ensure it can be used as an object key
117
+ timelineToken = String(timelineToken);
109
118
 
110
119
  // Get pins for this token only
111
120
  let pins = [];
112
- if (pinsData[timelineToken]) {
121
+ if (pinsData[timelineToken] && Array.isArray(pinsData[timelineToken])) {
113
122
  pins = pinsData[timelineToken];
114
123
  }
115
124
 
@@ -134,8 +143,7 @@ module.exports = function(RED) {
134
143
  return include;
135
144
  });
136
145
 
137
- // Clean up old pins (older than 1 month) from all tokens
138
- cleanupOldPins(pinsData);
146
+ // Note: Cleanup of old pins is handled in the add node
139
147
 
140
148
  // Create output message
141
149
  msg.payload = filteredPins;
@@ -152,38 +160,6 @@ module.exports = function(RED) {
152
160
  });
153
161
  });
154
162
 
155
- // Helper to clean up pins older than 1 month
156
- function cleanupOldPins(pinsData) {
157
- const oneMonthAgo = new Date();
158
- oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1);
159
- let changed = false;
160
-
161
- // Iterate through all tokens
162
- Object.keys(pinsData).forEach(token => {
163
- // Filter out pins older than 1 month
164
- const initialCount = pinsData[token].length;
165
- pinsData[token] = pinsData[token].filter(pin => {
166
- const storedDate = new Date(pin._stored);
167
- return storedDate >= oneMonthAgo;
168
- });
169
-
170
- // Log if pins were removed
171
- if (pinsData[token].length < initialCount) {
172
- node.debug(`Removed ${initialCount - pinsData[token].length} old pins for token ${token.substring(0, 8)}...`);
173
- changed = true;
174
- }
175
- });
176
-
177
- // Save the updated pins data if any pins were removed
178
- if (changed) {
179
- try {
180
- fs.writeFileSync(pinsFile, JSON.stringify(pinsData, null, 2));
181
- } catch (error) {
182
- node.warn(`Error saving pins to file: ${error.message}`);
183
- }
184
- }
185
- }
186
-
187
163
  node.on('close', function() {
188
164
  // Clean up any resources
189
165
  });