backend-manager 3.0.44 → 3.0.46
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
|
@@ -40,7 +40,8 @@ Usage.prototype.init = function (assistant, options) {
|
|
|
40
40
|
options.app = options.app || Manager.config.app.id;
|
|
41
41
|
options.refetch = typeof options.refetch === 'undefined' ? false : options.refetch;
|
|
42
42
|
options.clear = typeof options.clear === 'undefined' ? false : options.clear;
|
|
43
|
-
options.today = options.today
|
|
43
|
+
options.today = typeof options.today === 'undefined' ? undefined : options.today;
|
|
44
|
+
options.localKey = typeof options.localKey === 'undefined' ? undefined : options.localKey;
|
|
44
45
|
options.log = typeof options.log === 'undefined' ? false : options.log;
|
|
45
46
|
|
|
46
47
|
// Check for required options
|
|
@@ -48,6 +49,9 @@ Usage.prototype.init = function (assistant, options) {
|
|
|
48
49
|
return reject(new Error('Missing required {assistant} parameter'));
|
|
49
50
|
}
|
|
50
51
|
|
|
52
|
+
const localKey = (options.localKey || self.assistant.request.geolocation.ip || '')
|
|
53
|
+
.replace(/[\.:]/g, '_');
|
|
54
|
+
|
|
51
55
|
// Set options
|
|
52
56
|
self.options = options;
|
|
53
57
|
|
|
@@ -57,7 +61,7 @@ Usage.prototype.init = function (assistant, options) {
|
|
|
57
61
|
// Setup storage
|
|
58
62
|
self.storage = Manager.storage({name: 'usage', temporary: true, clear: options.clear, log: options.log});
|
|
59
63
|
|
|
60
|
-
self.paths.user = `users.${
|
|
64
|
+
self.paths.user = `users.${localKey}`;
|
|
61
65
|
self.paths.app = `apps.${options.app}`;
|
|
62
66
|
|
|
63
67
|
// Get storage data
|
|
@@ -189,19 +189,41 @@ Utilities.prototype.get = function (docPath, options) {
|
|
|
189
189
|
const self = this;
|
|
190
190
|
|
|
191
191
|
return new Promise(function(resolve, reject) {
|
|
192
|
+
const Manager = self.Manager;
|
|
193
|
+
const { admin } = Manager.libraries;
|
|
192
194
|
|
|
193
195
|
options = options || {};
|
|
194
196
|
options.maxAge = options.maxAge || (1000 * 60 * 5); // 5 minutes
|
|
197
|
+
options.readTime = typeof options.readTime === 'undefined' ? null : options.readTime;
|
|
198
|
+
options.log = typeof options.log === 'undefined' ? false : options.log;
|
|
195
199
|
|
|
196
|
-
self.cache = self.cache ||
|
|
200
|
+
self.cache = self.cache || Manager.storage({name: 'cache', temporary: true, clear: false});
|
|
197
201
|
|
|
198
202
|
const item = self.cache.get(docPath).value();
|
|
199
203
|
const age = item ? Date.now() - item.time : null;
|
|
200
204
|
|
|
201
|
-
if (
|
|
205
|
+
if (options.readTime) {
|
|
206
|
+
const { Timestamp } = require('firebase-admin/firestore')
|
|
207
|
+
const time = Math.round(new Date(options.readTime).getTime() / 1000 / 60);
|
|
208
|
+
const logg = new Date(time * 1000 * 60);
|
|
209
|
+
|
|
210
|
+
if (options.log) {
|
|
211
|
+
console.log('Read time:', logg);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// loop docs
|
|
215
|
+
admin.firestore().runTransaction(
|
|
216
|
+
updateFunction => updateFunction.get(admin.firestore().doc(docPath)),
|
|
217
|
+
{readOnly: true, readTime: new Timestamp(time * 60, 0)}
|
|
218
|
+
)
|
|
219
|
+
.then(snap => {
|
|
220
|
+
return resolve(snap);
|
|
221
|
+
})
|
|
222
|
+
.catch(e => reject(e));
|
|
223
|
+
} else if (item && age && age < options.maxAge) {
|
|
202
224
|
return resolve(item.doc);
|
|
203
225
|
} else {
|
|
204
|
-
|
|
226
|
+
admin.firestore().doc(docPath)
|
|
205
227
|
.get()
|
|
206
228
|
.then(async (doc) => {
|
|
207
229
|
const data = doc.data();
|