backend-manager 2.0.19 → 2.0.20
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
|
@@ -13,9 +13,11 @@ let sampleUser = {
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
authenticated: false,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
_APIManager: {
|
|
17
|
+
meta: {
|
|
18
|
+
lastStatsReset: new Date(),
|
|
19
|
+
lastUserFetch: new Date(),
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}
|
|
21
23
|
|
|
@@ -42,7 +44,7 @@ ApiManager.prototype.init = function (options) {
|
|
|
42
44
|
options.plans = options.plans || {};
|
|
43
45
|
|
|
44
46
|
// await self.Manager.libraries.admin.firestore
|
|
45
|
-
// options.plans.basic = options.plans.basic || {requests:
|
|
47
|
+
// options.plans.basic = options.plans.basic || {requests: 100};
|
|
46
48
|
|
|
47
49
|
options.maxUsersStored = options.maxUsersStored || 10000;
|
|
48
50
|
options.refetchInterval = options.refetchInterval || 60;
|
|
@@ -89,9 +91,18 @@ ApiManager.prototype.init = function (options) {
|
|
|
89
91
|
|
|
90
92
|
ApiManager.prototype._createNewUser = function (authenticatedUser, planId, persistentData, isRefetch, apiKey) {
|
|
91
93
|
const self = this;
|
|
94
|
+
const _APIManager_default = {
|
|
95
|
+
stats: {
|
|
96
|
+
requests: 0,
|
|
97
|
+
},
|
|
98
|
+
meta: {
|
|
99
|
+
lastStatsReset: new Date(),
|
|
100
|
+
lastUserFetch: new Date(),
|
|
101
|
+
},
|
|
102
|
+
providedAPIKey: apiKey,
|
|
103
|
+
}
|
|
92
104
|
persistentData = persistentData || {};
|
|
93
|
-
persistentData.
|
|
94
|
-
persistentData._stats = persistentData._stats || {};
|
|
105
|
+
persistentData._APIManager = persistentData._APIManager || merge({}, _APIManager_default);
|
|
95
106
|
|
|
96
107
|
let newUser = {
|
|
97
108
|
api: get(authenticatedUser, 'api', {}),
|
|
@@ -99,36 +110,43 @@ ApiManager.prototype._createNewUser = function (authenticatedUser, planId, persi
|
|
|
99
110
|
plan: {
|
|
100
111
|
id: planId,
|
|
101
112
|
limits: {
|
|
102
|
-
requests: get(authenticatedUser, 'plan.limits.requests', get(self.options, `plans.${planId}.limits.requests`, 93)),
|
|
103
113
|
}
|
|
104
114
|
},
|
|
105
115
|
authenticated: authenticatedUser.authenticated,
|
|
106
116
|
ip: authenticatedUser.ip,
|
|
107
117
|
country: authenticatedUser.country,
|
|
108
|
-
|
|
109
|
-
requests: 0,
|
|
110
|
-
},
|
|
111
|
-
_meta: {
|
|
112
|
-
lastStatsReset: new Date(),
|
|
113
|
-
lastUserFetch: new Date(),
|
|
114
|
-
},
|
|
115
|
-
_providedAPIKey: apiKey,
|
|
118
|
+
_APIManager: merge({}, _APIManager_default),
|
|
116
119
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
120
|
+
|
|
121
|
+
// Setup newUser
|
|
122
|
+
const currentPlan = get(self.options, `plans.${planId}.limits`, {})
|
|
123
|
+
Object.keys(currentPlan)
|
|
124
|
+
.forEach((id, i) => {
|
|
125
|
+
// console.log('----id', id);
|
|
126
|
+
// console.log('======currentPlan[id]', currentPlan[id]);
|
|
127
|
+
newUser.plan.limits[id] = get(authenticatedUser, `plan.limits.${id}`, currentPlan[id])
|
|
128
|
+
// const product = data.products[id]
|
|
129
|
+
// options.plans[product.planId] = {}
|
|
130
|
+
// options.plans[product.planId].limits = product.limits || {};
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
// console.log('-----MIN', moment().diff(moment(persistentData._APIManager.meta.lastStatsReset), 'minutes', true), self.options.resetInterval);
|
|
136
|
+
if (moment().diff(moment(persistentData._APIManager.meta.lastStatsReset), 'minutes', true) < self.options.resetInterval) {
|
|
137
|
+
newUser._APIManager.meta.lastStatsReset = persistentData._APIManager.meta.lastStatsReset || newUser._APIManager.meta.lastStatsReset;
|
|
138
|
+
newUser._APIManager.meta.lastUserFetch = persistentData._APIManager.meta.lastUserFetch || newUser._APIManager.meta.lastUserFetch;
|
|
139
|
+
Object.keys(persistentData._APIManager.stats)
|
|
122
140
|
.forEach((key, i) => {
|
|
123
|
-
newUser.
|
|
141
|
+
newUser._APIManager.stats[key] = persistentData._APIManager.stats[key];
|
|
124
142
|
});
|
|
125
143
|
} else {
|
|
126
144
|
// console.log('---RESSET INTERVAL REACHED');
|
|
127
|
-
newUser.
|
|
145
|
+
newUser._APIManager.meta.lastUserFetch = persistentData._APIManager.meta.lastUserFetch;
|
|
128
146
|
}
|
|
129
147
|
|
|
130
148
|
if (isRefetch) {
|
|
131
|
-
newUser.
|
|
149
|
+
newUser._APIManager.meta.lastUserFetch = new Date();
|
|
132
150
|
}
|
|
133
151
|
|
|
134
152
|
return newUser;
|
|
@@ -145,14 +163,14 @@ ApiManager.prototype.getUser = async function (assistant) {
|
|
|
145
163
|
if (apiKey) {
|
|
146
164
|
newUser = self.userList.filter(user => user.api.privateKey === apiKey);
|
|
147
165
|
if (newUser[0]) {
|
|
148
|
-
if (newUser.length > 1 || moment().diff(moment(newUser[0].
|
|
166
|
+
if (newUser.length > 1 || moment().diff(moment(newUser[0]._APIManager.meta.lastUserFetch), 'minutes', true) > self.options.refetchInterval) {
|
|
149
167
|
// console.log('----REFETCHING');
|
|
150
|
-
persistentData = {set: true,
|
|
168
|
+
persistentData = {set: true, _APIManager: merge({}, newUser[0]._APIManager)};
|
|
151
169
|
|
|
152
170
|
self.userList = self.userList.filter(user => user.api.privateKey !== apiKey)
|
|
153
171
|
newUser = null;
|
|
154
172
|
} else {
|
|
155
|
-
persistentData = {set: true,
|
|
173
|
+
persistentData = {set: true, _APIManager: merge({}, newUser[0]._APIManager)};
|
|
156
174
|
|
|
157
175
|
newUser = newUser[0];
|
|
158
176
|
}
|
|
@@ -178,10 +196,10 @@ ApiManager.prototype.getUser = async function (assistant) {
|
|
|
178
196
|
let existingUser = self.userList.find(user => user.auth.uid === workingUID);
|
|
179
197
|
if (existingUser) {
|
|
180
198
|
// console.log('---actually does exist so setting');
|
|
181
|
-
// console.log('----1111 MIN lastUserFetch', moment().diff(moment(existingUser.
|
|
182
|
-
persistentData = !persistentData.set ? {set: true,
|
|
199
|
+
// console.log('----1111 MIN lastUserFetch', moment().diff(moment(existingUser._APIManager.meta.lastUserFetch), 'minutes', true), self.options.refetchInterval);
|
|
200
|
+
persistentData = !persistentData.set ? {set: true, _APIManager: merge({}, existingUser._APIManager)} : persistentData;
|
|
183
201
|
// console.log('----persistentData 2', persistentData);
|
|
184
|
-
if (moment().diff(moment(existingUser.
|
|
202
|
+
if (moment().diff(moment(existingUser._APIManager.meta.lastUserFetch), 'minutes', true) > self.options.refetchInterval) {
|
|
185
203
|
// console.log('----REFETCHING');
|
|
186
204
|
self.userList = self.userList.filter(user => user.auth.uid !== workingUID)
|
|
187
205
|
existingUser = self._createNewUser(authenticatedUser, planId, persistentData, true, apiKey);
|
|
@@ -205,17 +223,40 @@ ApiManager.prototype.getUser = async function (assistant) {
|
|
|
205
223
|
|
|
206
224
|
function _getUserStat(self, user, stat, def) {
|
|
207
225
|
const isWhitelistedAPIKey = self.options.whitelistedAPIKeys.includes(
|
|
208
|
-
get(user, `api.privateKey`, get(user, `
|
|
226
|
+
get(user, `api.privateKey`, get(user, `_APIManager.providedAPIKey`))
|
|
209
227
|
);
|
|
210
228
|
// console.log('----user', user);
|
|
211
229
|
// console.log('----isWhitelistedAPIKey', isWhitelistedAPIKey);
|
|
212
230
|
return {
|
|
213
|
-
current: !isWhitelistedAPIKey ? get(user, `
|
|
231
|
+
current: !isWhitelistedAPIKey ? get(user, `_APIManager.stats.${stat}`, typeof def !== 'undefined' ? def : 0) : 0,
|
|
214
232
|
limit: !isWhitelistedAPIKey ? get(user, `plan.limits.${stat}`, typeof def !== 'undefined' ? def : 0) : Infinity,
|
|
215
233
|
}
|
|
216
234
|
}
|
|
217
235
|
|
|
218
|
-
ApiManager.prototype.
|
|
236
|
+
ApiManager.prototype.isUserOverStat = function (user, stat, def, frame) {
|
|
237
|
+
const self = this;
|
|
238
|
+
if (!user || !stat) {
|
|
239
|
+
throw new Error('<user> and <stat> required')
|
|
240
|
+
}
|
|
241
|
+
const result = self.getUserStat(user, stat, def);
|
|
242
|
+
frame = frame || 'daily';
|
|
243
|
+
let limit = result.limit;
|
|
244
|
+
// console.log('---result', result);
|
|
245
|
+
// console.log('---typeof result.current', typeof result.current);
|
|
246
|
+
// console.log('----limit', limit);
|
|
247
|
+
if (typeof result.limit === 'number') {
|
|
248
|
+
if (frame === 'daily') {
|
|
249
|
+
limit = Math.floor(result.limit / 31);
|
|
250
|
+
}
|
|
251
|
+
// console.log('----limit', limit);
|
|
252
|
+
// console.log('-----result.current < limit', result.current < limit);
|
|
253
|
+
return limit >= result.current;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
ApiManager.prototype.getUserStat = function (user, stat, def, ) {
|
|
219
260
|
const self = this;
|
|
220
261
|
if (!user || !stat) {
|
|
221
262
|
throw new Error('<user> and <stat> required')
|
|
@@ -228,7 +269,7 @@ ApiManager.prototype.incrementUserStat = function (user, stat, amount) {
|
|
|
228
269
|
if (!user || !stat) {
|
|
229
270
|
throw new Error('<user> and <stat> required')
|
|
230
271
|
}
|
|
231
|
-
set(user, `
|
|
272
|
+
set(user, `_APIManager.stats.${stat}`, get(user, `_APIManager.stats.${stat}`, 0) + amount)
|
|
232
273
|
return _getUserStat(self, user, stat, 0);
|
|
233
274
|
}
|
|
234
275
|
|