backend-manager 3.2.172 → 3.2.173
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 +3 -3
- package/src/cli/cli.js +27 -11
- package/dist/cli/cli.js +0 -1534
- package/dist/manager/functions/core/actions/api/admin/backup.js +0 -338
- package/dist/manager/functions/core/actions/api/admin/create-post.js +0 -388
- package/dist/manager/functions/core/actions/api/admin/cron.js +0 -37
- package/dist/manager/functions/core/actions/api/admin/database-read.js +0 -35
- package/dist/manager/functions/core/actions/api/admin/database-write.js +0 -39
- package/dist/manager/functions/core/actions/api/admin/edit-post.js +0 -158
- package/dist/manager/functions/core/actions/api/admin/firestore-query.js +0 -165
- package/dist/manager/functions/core/actions/api/admin/firestore-read.js +0 -38
- package/dist/manager/functions/core/actions/api/admin/firestore-write.js +0 -54
- package/dist/manager/functions/core/actions/api/admin/get-stats.js +0 -269
- package/dist/manager/functions/core/actions/api/admin/payment-processor.js +0 -57
- package/dist/manager/functions/core/actions/api/admin/run-hook.js +0 -95
- package/dist/manager/functions/core/actions/api/admin/send-notification.js +0 -197
- package/dist/manager/functions/core/actions/api/admin/sync-users.js +0 -125
- package/dist/manager/functions/core/actions/api/admin/templates/post.html +0 -16
- package/dist/manager/functions/core/actions/api/firebase/get-providers.js +0 -102
- package/dist/manager/functions/core/actions/api/general/emails/general:download-app-link.js +0 -21
- package/dist/manager/functions/core/actions/api/general/fetch-post.js +0 -99
- package/dist/manager/functions/core/actions/api/general/generate-uuid.js +0 -41
- package/dist/manager/functions/core/actions/api/general/send-email.js +0 -112
- package/dist/manager/functions/core/actions/api/handler/create-post.js +0 -146
- package/dist/manager/functions/core/actions/api/special/setup-electron-manager-client.js +0 -103
- package/dist/manager/functions/core/actions/api/template.js +0 -33
- package/dist/manager/functions/core/actions/api/test/authenticate.js +0 -22
- package/dist/manager/functions/core/actions/api/test/create-test-accounts.js +0 -27
- package/dist/manager/functions/core/actions/api/test/lab.js +0 -55
- package/dist/manager/functions/core/actions/api/test/redirect.js +0 -26
- package/dist/manager/functions/core/actions/api/test/webhook.js +0 -30
- package/dist/manager/functions/core/actions/api/user/create-custom-token.js +0 -32
- package/dist/manager/functions/core/actions/api/user/delete.js +0 -68
- package/dist/manager/functions/core/actions/api/user/get-active-sessions.js +0 -45
- package/dist/manager/functions/core/actions/api/user/get-subscription-info.js +0 -49
- package/dist/manager/functions/core/actions/api/user/oauth2/discord.js +0 -114
- package/dist/manager/functions/core/actions/api/user/oauth2/google.js +0 -99
- package/dist/manager/functions/core/actions/api/user/oauth2.js +0 -476
- package/dist/manager/functions/core/actions/api/user/regenerate-api-keys.js +0 -54
- package/dist/manager/functions/core/actions/api/user/resolve.js +0 -32
- package/dist/manager/functions/core/actions/api/user/sign-out-all-sessions.js +0 -118
- package/dist/manager/functions/core/actions/api/user/sign-up copy.js +0 -544
- package/dist/manager/functions/core/actions/api/user/sign-up.js +0 -99
- package/dist/manager/functions/core/actions/api/user/submit-feedback.js +0 -96
- package/dist/manager/functions/core/actions/api/user/validate-settings.js +0 -86
- package/dist/manager/functions/core/actions/api.js +0 -354
- package/dist/manager/functions/core/actions/create-post-handler.js +0 -184
- package/dist/manager/functions/core/actions/generate-uuid.js +0 -62
- package/dist/manager/functions/core/actions/sign-up-handler.js +0 -205
- package/dist/manager/functions/core/admin/create-post.js +0 -206
- package/dist/manager/functions/core/admin/firestore-write.js +0 -72
- package/dist/manager/functions/core/admin/get-stats.js +0 -218
- package/dist/manager/functions/core/admin/query.js +0 -198
- package/dist/manager/functions/core/admin/send-notification.js +0 -206
- package/dist/manager/functions/core/cron/daily/ghostii-auto-publisher.js +0 -377
- package/dist/manager/functions/core/cron/daily/reset-usage.js +0 -197
- package/dist/manager/functions/core/cron/daily.js +0 -114
- package/dist/manager/functions/core/events/auth/before-create.js +0 -124
- package/dist/manager/functions/core/events/auth/before-signin.js +0 -62
- package/dist/manager/functions/core/events/auth/on-create copy.js +0 -121
- package/dist/manager/functions/core/events/auth/on-create.js +0 -564
- package/dist/manager/functions/core/events/auth/on-delete.js +0 -72
- package/dist/manager/functions/core/events/firestore/on-subscription.js +0 -107
- package/dist/manager/functions/test/authenticate.js +0 -38
- package/dist/manager/functions/test/create-test-accounts.js +0 -144
- package/dist/manager/functions/test/webhook.js +0 -37
- package/dist/manager/functions/wrappers/mailchimp/addToList.js +0 -25
- package/dist/manager/helpers/analytics copy.js +0 -217
- package/dist/manager/helpers/analytics.js +0 -467
- package/dist/manager/helpers/api-manager.js +0 -324
- package/dist/manager/helpers/assistant.js +0 -1043
- package/dist/manager/helpers/metadata.js +0 -32
- package/dist/manager/helpers/middleware.js +0 -154
- package/dist/manager/helpers/roles.js +0 -69
- package/dist/manager/helpers/settings.js +0 -158
- package/dist/manager/helpers/subscription-resolver-new.js +0 -828
- package/dist/manager/helpers/subscription-resolver.js +0 -842
- package/dist/manager/helpers/usage.js +0 -381
- package/dist/manager/helpers/user.js +0 -198
- package/dist/manager/helpers/utilities.js +0 -292
- package/dist/manager/index.js +0 -1076
- package/dist/manager/libraries/openai.js +0 -460
- package/dist/manager/routes/restart/index.js +0 -52
- package/dist/manager/routes/test/index.js +0 -43
- package/dist/manager/schemas/restart.js +0 -13
- package/dist/manager/schemas/test.js +0 -13
- package/dist/require.js +0 -3
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usage
|
|
3
|
-
* Meant to check and update usage for a user
|
|
4
|
-
* Uses the ITWCW apps/{app}/products/{product}/limits/{metric} to check limits
|
|
5
|
-
* Stores usage in the user's firestore document OR in local/temp storage if no user
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const fetch = require('wonderful-fetch');
|
|
9
|
-
const moment = require('moment');
|
|
10
|
-
const _ = require('lodash');
|
|
11
|
-
const hcaptcha = require('hcaptcha');
|
|
12
|
-
|
|
13
|
-
function Usage(m) {
|
|
14
|
-
const self = this;
|
|
15
|
-
|
|
16
|
-
self.Manager = m;
|
|
17
|
-
|
|
18
|
-
self.user = null;
|
|
19
|
-
self.app = null;
|
|
20
|
-
self.options = null;
|
|
21
|
-
self.assistant = null;
|
|
22
|
-
self.storage = null;
|
|
23
|
-
|
|
24
|
-
self.paths = {
|
|
25
|
-
user: '',
|
|
26
|
-
app: '',
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
self.initialized = false;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
Usage.prototype.init = function (assistant, options) {
|
|
33
|
-
const self = this;
|
|
34
|
-
|
|
35
|
-
return new Promise(async function(resolve, reject) {
|
|
36
|
-
const Manager = self.Manager;
|
|
37
|
-
|
|
38
|
-
// Set options
|
|
39
|
-
options = options || {};
|
|
40
|
-
options.app = options.app || Manager.config.app.id;
|
|
41
|
-
options.refetch = typeof options.refetch === 'undefined' ? false : options.refetch;
|
|
42
|
-
options.clear = typeof options.clear === 'undefined' ? false : options.clear;
|
|
43
|
-
options.today = typeof options.today === 'undefined' ? undefined : options.today;
|
|
44
|
-
options.key = typeof options.key === 'undefined' ? undefined : options.key;
|
|
45
|
-
options.unauthenticatedMode = typeof options.unauthenticatedMode === 'undefined' ? 'firestore' : options.unauthenticatedMode;
|
|
46
|
-
options.whitelistKeys = options.whitelistKeys || [];
|
|
47
|
-
options.log = typeof options.log === 'undefined' ? false : options.log;
|
|
48
|
-
|
|
49
|
-
// Check for required options
|
|
50
|
-
if (!assistant) {
|
|
51
|
-
return reject(new Error('Missing required {assistant} parameter'));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Add BEM to whitelist keys
|
|
55
|
-
options.whitelistKeys.push(
|
|
56
|
-
Manager?.config?.backend_manager?.key
|
|
57
|
-
|| process.env.BACKEND_MANAGER_KEY
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
// Set options
|
|
61
|
-
self.options = options;
|
|
62
|
-
|
|
63
|
-
// Set assistant
|
|
64
|
-
self.assistant = assistant;
|
|
65
|
-
|
|
66
|
-
// Setup storage
|
|
67
|
-
self.storage = Manager.storage({name: 'usage', temporary: true, clear: options.clear, log: options.log});
|
|
68
|
-
|
|
69
|
-
// Set local key
|
|
70
|
-
self.key = (options.key || self.assistant.request.geolocation.ip || '')
|
|
71
|
-
.replace(/[\.:]/g, '_');
|
|
72
|
-
|
|
73
|
-
// Set paths
|
|
74
|
-
self.paths.user = `users.${self.key}`;
|
|
75
|
-
self.paths.app = `apps.${options.app}`;
|
|
76
|
-
|
|
77
|
-
// Get storage data
|
|
78
|
-
const appLastFetched = moment(self.storage.get(`${self.paths.app}.lastFetched`, 0).value());
|
|
79
|
-
const diff = moment().diff(appLastFetched, 'hours');
|
|
80
|
-
|
|
81
|
-
// Authenticate user (user will be resolved as well)
|
|
82
|
-
self.user = await assistant.authenticate();
|
|
83
|
-
|
|
84
|
-
self.useUnauthenticatedStorage = !self.user.auth.uid || self.options.key;
|
|
85
|
-
|
|
86
|
-
// Load usage with temporary if unauthenticated
|
|
87
|
-
if (self.useUnauthenticatedStorage) {
|
|
88
|
-
let foundUsage;
|
|
89
|
-
|
|
90
|
-
if (options.unauthenticatedMode === 'firestore') {
|
|
91
|
-
// TODO: Make it request using .where() query so it doesnt use a read if it doesnt have to
|
|
92
|
-
foundUsage = await Manager.libraries.admin.firestore().doc(`temporary/${self.key}`)
|
|
93
|
-
.get()
|
|
94
|
-
.then((r) => _.get(r.data(), 'usage'))
|
|
95
|
-
.catch((e) => {
|
|
96
|
-
assistant.errorify(`Usage.init(): Error fetching usage data: ${e}`, {code: 500, sentry: true});
|
|
97
|
-
});
|
|
98
|
-
} else {
|
|
99
|
-
foundUsage = self.storage.get(`${self.paths.user}.usage`, {}).value();
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
self.user.usage = foundUsage ? foundUsage : self.user.usage;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Log
|
|
106
|
-
self.log(`Usage.init(): Checking if usage data needs to be fetched (${diff} hours)...`);
|
|
107
|
-
|
|
108
|
-
// Get app data to get plan limits using cached data if possible
|
|
109
|
-
if (diff > 1 || options.refetch) {
|
|
110
|
-
await fetch('https://us-central1-itw-creative-works.cloudfunctions.net/getApp', {
|
|
111
|
-
method: 'post',
|
|
112
|
-
response: 'json',
|
|
113
|
-
body: {
|
|
114
|
-
id: options.app,
|
|
115
|
-
},
|
|
116
|
-
})
|
|
117
|
-
.then((json) => {
|
|
118
|
-
// Write data and last fetched to storage
|
|
119
|
-
self.storage.set(`${self.paths.app}.data`, json).write();
|
|
120
|
-
self.storage.set(`${self.paths.app}.lastFetched`, new Date().toISOString()).write();
|
|
121
|
-
})
|
|
122
|
-
.catch(e => {
|
|
123
|
-
assistant.errorify(`Usage.init(): Error fetching app data: ${e}`, {code: 500, sentry: true});
|
|
124
|
-
})
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Get app data
|
|
128
|
-
self.app = self.storage.get(`${self.paths.app}.data`, {}).value();
|
|
129
|
-
|
|
130
|
-
// Check for app data
|
|
131
|
-
if (!self.app) {
|
|
132
|
-
return reject(new Error('Usage.init(): No app data found'));
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Log
|
|
136
|
-
self.log(`Usage.init(): Got app data`, self.app);
|
|
137
|
-
self.log(`Usage.init(): Got user`, self.user);
|
|
138
|
-
|
|
139
|
-
// Set initialized to true
|
|
140
|
-
self.initialized = true;
|
|
141
|
-
|
|
142
|
-
// Resolve
|
|
143
|
-
return resolve(self);
|
|
144
|
-
});
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
Usage.prototype.validate = function (name, options) {
|
|
148
|
-
const self = this;
|
|
149
|
-
|
|
150
|
-
return new Promise(async function(resolve, reject) {
|
|
151
|
-
const Manager = self.Manager;
|
|
152
|
-
const assistant = self.assistant;
|
|
153
|
-
|
|
154
|
-
// Set options
|
|
155
|
-
options = options || {};
|
|
156
|
-
options.useCaptchaResponse = typeof options.useCaptchaResponse === 'undefined' ? true : options.useCaptchaResponse;
|
|
157
|
-
options.log = typeof options.log === 'undefined' ? true : options.log;
|
|
158
|
-
options.throw = typeof options.throw === 'undefined' ? false : options.throw;
|
|
159
|
-
|
|
160
|
-
// Check for required options
|
|
161
|
-
const period = self.getUsage(name);
|
|
162
|
-
const allowed = self.getLimit(name);
|
|
163
|
-
|
|
164
|
-
// Log (independent of options.log because this is important)
|
|
165
|
-
if (options.log) {
|
|
166
|
-
assistant.log(`Usage.validate(): Checking ${period}/${allowed} for ${name}...`);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Reject function
|
|
170
|
-
function _reject() {
|
|
171
|
-
reject(
|
|
172
|
-
assistant.errorify(`You have exceeded your ${name} usage limit of ${period}/${allowed}.`, {code: 429})
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// Dev mode throw
|
|
177
|
-
if (options.throw) {
|
|
178
|
-
return _reject();
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Check if they have a white list key
|
|
182
|
-
const hasWhitelistKey = self.options.whitelistKeys.some((key) => key && key === self?.user?.api?.privateKey);
|
|
183
|
-
if (hasWhitelistKey) {
|
|
184
|
-
self.log(`Usage.validate(): Whitelist key found for ${name}`);
|
|
185
|
-
|
|
186
|
-
return resolve(true);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// If they are under the limit, resolve
|
|
190
|
-
if (period < allowed) {
|
|
191
|
-
self.log(`Usage.validate(): Valid for ${name}`);
|
|
192
|
-
|
|
193
|
-
return resolve(true);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// If they are using captcha, attempt to resolve
|
|
197
|
-
const captchaResponse = assistant.request.data['h-captcha-response'];
|
|
198
|
-
if (captchaResponse && options.useCaptchaResponse) {
|
|
199
|
-
self.log(`Usage.validate(): Checking captcha response`, captchaResponse);
|
|
200
|
-
|
|
201
|
-
const captchaResult = await hcaptcha.verify(process.env.HCAPTCHA_SECRET, captchaResponse)
|
|
202
|
-
.then((data) => data)
|
|
203
|
-
.catch((e) => e);
|
|
204
|
-
|
|
205
|
-
// If the captcha is valid, resolve
|
|
206
|
-
if (!captchaResult || captchaResult instanceof Error || !captchaResult.success) {
|
|
207
|
-
return reject(
|
|
208
|
-
assistant.errorify(`Captcha verification failed.`, {code: 400})
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// Otherwise, they are over the limit, reject
|
|
214
|
-
return _reject();
|
|
215
|
-
});
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
Usage.prototype.increment = function (name, value, options) {
|
|
219
|
-
const self = this;
|
|
220
|
-
const Manager = self.Manager;
|
|
221
|
-
const assistant = self.assistant;
|
|
222
|
-
|
|
223
|
-
// Set name
|
|
224
|
-
name = name || 'requests';
|
|
225
|
-
|
|
226
|
-
// Set value
|
|
227
|
-
value = typeof value === 'undefined' ? 1 : value;
|
|
228
|
-
|
|
229
|
-
// Set options
|
|
230
|
-
options = options || {};
|
|
231
|
-
options.id = options.id || null;
|
|
232
|
-
|
|
233
|
-
// Update total and period
|
|
234
|
-
['total', 'period', 'last'].forEach((key) => {
|
|
235
|
-
const resolved = `usage.${name}.${key}`;
|
|
236
|
-
const existing = _.get(self.user, resolved, 0);
|
|
237
|
-
|
|
238
|
-
if (key === 'last') {
|
|
239
|
-
const now = moment(
|
|
240
|
-
typeof self.options.today === 'undefined' ? new Date() : self.options.today
|
|
241
|
-
);
|
|
242
|
-
|
|
243
|
-
_.set(self.user, resolved, {
|
|
244
|
-
id: options.id,
|
|
245
|
-
timestamp: now.toISOString(),
|
|
246
|
-
timestampUNIX: now.unix(),
|
|
247
|
-
});
|
|
248
|
-
} else {
|
|
249
|
-
_.set(self.user, resolved, existing + value);
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
// Log the updated user
|
|
254
|
-
self.log(`Usage.init(): Incremented ${name} for user`, self.user);
|
|
255
|
-
|
|
256
|
-
return self;
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
Usage.prototype.set = function (name, value) {
|
|
260
|
-
const self = this;
|
|
261
|
-
const Manager = self.Manager;
|
|
262
|
-
const assistant = self.assistant;
|
|
263
|
-
|
|
264
|
-
// Set name
|
|
265
|
-
name = name || 'requests';
|
|
266
|
-
|
|
267
|
-
// Set value
|
|
268
|
-
value = typeof value === 'undefined' ? 0 : value;
|
|
269
|
-
|
|
270
|
-
// Update total and period
|
|
271
|
-
const resolved = `usage.${name}.period`;
|
|
272
|
-
|
|
273
|
-
// Set the value
|
|
274
|
-
_.set(self.user, resolved, value);
|
|
275
|
-
|
|
276
|
-
// Log the updated user
|
|
277
|
-
self.log(`Usage.init(): Set ${name} for user`, self.user);
|
|
278
|
-
|
|
279
|
-
return self;
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
Usage.prototype.getUsage = function (name) {
|
|
283
|
-
const self = this;
|
|
284
|
-
const Manager = self.Manager;
|
|
285
|
-
const assistant = self.assistant;
|
|
286
|
-
|
|
287
|
-
// Get usage
|
|
288
|
-
if (name) {
|
|
289
|
-
return _.get(self.user, `usage.${name}.period`, 0);
|
|
290
|
-
} else {
|
|
291
|
-
return self.user.usage;
|
|
292
|
-
}
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
Usage.prototype.getLimit = function (name) {
|
|
296
|
-
const self = this;
|
|
297
|
-
const Manager = self.Manager;
|
|
298
|
-
const assistant = self.assistant;
|
|
299
|
-
|
|
300
|
-
// Get key
|
|
301
|
-
const key = `products.${self.options.app}-${self.user.plan.id}.limits`;
|
|
302
|
-
|
|
303
|
-
// Get limit
|
|
304
|
-
if (name) {
|
|
305
|
-
return _.get(self.app, `${key}.${name}`, 0);
|
|
306
|
-
} else {
|
|
307
|
-
return _.get(self.app, key, {});
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
Usage.prototype.update = function () {
|
|
312
|
-
const self = this;
|
|
313
|
-
|
|
314
|
-
return new Promise(async function(resolve, reject) {
|
|
315
|
-
const Manager = self.Manager;
|
|
316
|
-
const assistant = self.assistant;
|
|
317
|
-
|
|
318
|
-
// Write self.user to firestore or local if no user or if key is set
|
|
319
|
-
if (self.useUnauthenticatedStorage) {
|
|
320
|
-
if (self.options.unauthenticatedMode === 'firestore') {
|
|
321
|
-
Manager.libraries.admin.firestore().doc(`temporary/${self.key}`)
|
|
322
|
-
.set({
|
|
323
|
-
usage: self.user.usage,
|
|
324
|
-
}, {merge: true})
|
|
325
|
-
.then(() => {
|
|
326
|
-
self.log(`Usage.update(): Updated user.usage in firestore`, self.user.usage);
|
|
327
|
-
|
|
328
|
-
return resolve(self.user.usage);
|
|
329
|
-
})
|
|
330
|
-
.catch(e => {
|
|
331
|
-
return reject(assistant.errorify(e, {code: 500, sentry: true}));
|
|
332
|
-
});
|
|
333
|
-
} else {
|
|
334
|
-
self.storage.set(`${self.paths.user}.usage`, self.user.usage).write();
|
|
335
|
-
|
|
336
|
-
self.log(`Usage.update(): Updated user.usage in local storage`, self.user.usage);
|
|
337
|
-
|
|
338
|
-
return resolve(self.user.usage);
|
|
339
|
-
}
|
|
340
|
-
} else {
|
|
341
|
-
Manager.libraries.admin.firestore().doc(`users/${self.user.auth.uid}`)
|
|
342
|
-
.set({
|
|
343
|
-
usage: self.user.usage,
|
|
344
|
-
}, {merge: true})
|
|
345
|
-
.then(() => {
|
|
346
|
-
self.log(`Usage.update(): Updated user.usage in firestore`, self.user.usage);
|
|
347
|
-
|
|
348
|
-
return resolve(self.user.usage);
|
|
349
|
-
})
|
|
350
|
-
.catch(e => {
|
|
351
|
-
return reject(assistant.errorify(e, {code: 500, sentry: true}));
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
});
|
|
355
|
-
};
|
|
356
|
-
|
|
357
|
-
Usage.prototype.addWhitelistKeys = function (keys) {
|
|
358
|
-
const self = this;
|
|
359
|
-
|
|
360
|
-
const options = self.options;
|
|
361
|
-
|
|
362
|
-
// Make keys and array if not already
|
|
363
|
-
keys = Array.isArray(keys) ? keys : [keys];
|
|
364
|
-
|
|
365
|
-
// Add keys to whitelist
|
|
366
|
-
options.whitelistKeys = options.whitelistKeys.concat(keys);
|
|
367
|
-
|
|
368
|
-
// Log
|
|
369
|
-
return self;
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
Usage.prototype.log = function () {
|
|
373
|
-
const self = this;
|
|
374
|
-
|
|
375
|
-
// Log
|
|
376
|
-
if (self.options.log) {
|
|
377
|
-
self.assistant.log(...arguments);
|
|
378
|
-
}
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
module.exports = Usage;
|
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
const _ = require('lodash');
|
|
2
|
-
const uuid4 = require('uuid').v4;
|
|
3
|
-
const shortid = require('shortid');
|
|
4
|
-
const powertools = require('node-powertools');
|
|
5
|
-
const UIDGenerator = require('uid-generator');
|
|
6
|
-
const uidgen = new UIDGenerator(256);
|
|
7
|
-
|
|
8
|
-
function User(Manager, settings, options) {
|
|
9
|
-
const self = this;
|
|
10
|
-
|
|
11
|
-
self.Manager = Manager;
|
|
12
|
-
|
|
13
|
-
settings = settings || {};
|
|
14
|
-
options = options || {};
|
|
15
|
-
|
|
16
|
-
options.defaults = typeof options.defaults === 'undefined' ? true : options.defaults;
|
|
17
|
-
options.prune = typeof options.prune === 'undefined' ? false : options.prune;
|
|
18
|
-
|
|
19
|
-
const now = powertools.timestamp(new Date(), {output: 'string'});
|
|
20
|
-
const nowUNIX = powertools.timestamp(now, {output: 'unix'});
|
|
21
|
-
const oldDate = powertools.timestamp(new Date(0), {output: 'string'})
|
|
22
|
-
const oldDateUNIX = powertools.timestamp(oldDate, {output: 'unix'});
|
|
23
|
-
|
|
24
|
-
const defaults = options.defaults;
|
|
25
|
-
|
|
26
|
-
self.properties = {
|
|
27
|
-
auth: {
|
|
28
|
-
uid: _.get(settings, 'auth.uid', null),
|
|
29
|
-
email: _.get(settings, 'auth.email', null),
|
|
30
|
-
temporary: _.get(settings, 'auth.temporary', defaults ? false : null),
|
|
31
|
-
},
|
|
32
|
-
plan: {
|
|
33
|
-
id: _.get(settings, 'plan.id', defaults ? 'basic' : null), // intro | basic | advanced | premium
|
|
34
|
-
status: _.get(settings, 'plan.status', defaults ? 'cancelled' : null), // active | suspended | cancelled
|
|
35
|
-
expires: {
|
|
36
|
-
timestamp: _.get(settings, 'plan.expires.timestamp', defaults ? oldDate : null),
|
|
37
|
-
timestampUNIX: _.get(settings, 'plan.expires.timestampUNIX', defaults ? oldDateUNIX : null),
|
|
38
|
-
},
|
|
39
|
-
trial: {
|
|
40
|
-
activated: _.get(settings, 'plan.trial.activated', defaults ? false : null),
|
|
41
|
-
expires: {
|
|
42
|
-
timestamp: _.get(settings, 'plan.trial.expires.timestamp', defaults ? oldDate : null),
|
|
43
|
-
timestampUNIX: _.get(settings, 'plan.trial.expires.timestampUNIX', defaults ? oldDateUNIX : null),
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
limits: {
|
|
47
|
-
// devices: _.get(settings, 'plan.limits.devices', null),
|
|
48
|
-
},
|
|
49
|
-
payment: {
|
|
50
|
-
processor: _.get(settings, 'plan.payment.processor', null), // paypal | stripe | chargebee, etc
|
|
51
|
-
orderId: _.get(settings, 'plan.payment.orderId', null), // xxx-xxx-xxx
|
|
52
|
-
resourceId: _.get(settings, 'plan.payment.resourceId', null), // x-xxxxxx
|
|
53
|
-
frequency: _.get(settings, 'plan.payment.frequency', null), // monthly || annually
|
|
54
|
-
active: _.get(settings, 'plan.payment.active', defaults ? false : null), // true | false
|
|
55
|
-
startDate: {
|
|
56
|
-
timestamp: _.get(settings, 'plan.payment.startDate.timestamp', defaults ? now : null), // x-xxxxxx
|
|
57
|
-
timestampUNIX: _.get(settings, 'plan.payment.startDate.timestampUNIX', defaults ? nowUNIX : null), // x-xxxxxx
|
|
58
|
-
},
|
|
59
|
-
updatedBy: {
|
|
60
|
-
event: {
|
|
61
|
-
name: _.get(settings, 'plan.payment.updatedBy.event.name', null), // x-xxxxxx
|
|
62
|
-
id: _.get(settings, 'plan.payment.updatedBy.event.id', null), // x-xxxxxx
|
|
63
|
-
},
|
|
64
|
-
date: {
|
|
65
|
-
timestamp: _.get(settings, 'plan.payment.updatedBy.date.timestamp', defaults ? now : null), // x-xxxxxx
|
|
66
|
-
timestampUNIX: _.get(settings, 'plan.payment.updatedBy.date.timestampUNIX', defaults ? nowUNIX : null), // x-xxxxxx
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
roles: {
|
|
72
|
-
admin: _.get(settings, 'roles.admin', defaults ? false : null),
|
|
73
|
-
betaTester: _.get(settings, 'roles.betaTester', defaults ? false : null),
|
|
74
|
-
developer: _.get(settings, 'roles.developer', defaults ? false : null),
|
|
75
|
-
},
|
|
76
|
-
affiliate: {
|
|
77
|
-
code: _.get(settings, 'affiliate.code', defaults ? self.Manager.Utilities().randomId({size: 7}) : null),
|
|
78
|
-
referrals: _.get(settings, 'affiliate.referrals', []),
|
|
79
|
-
referrer: _.get(settings, 'affiliate.referrer', null),
|
|
80
|
-
},
|
|
81
|
-
activity: {
|
|
82
|
-
lastActivity: {
|
|
83
|
-
timestamp: _.get(settings, 'activity.lastActivity.timestamp', defaults ? now : null),
|
|
84
|
-
timestampUNIX: _.get(settings, 'activity.lastActivity.timestampUNIX', defaults ? nowUNIX : null),
|
|
85
|
-
},
|
|
86
|
-
created: {
|
|
87
|
-
timestamp: _.get(settings, 'activity.created.timestamp', defaults ? now : null),
|
|
88
|
-
timestampUNIX: _.get(settings, 'activity.created.timestampUNIX', defaults ? nowUNIX : null),
|
|
89
|
-
},
|
|
90
|
-
geolocation: {
|
|
91
|
-
ip: _.get(settings, 'activity.geolocation.ip', defaults ? '' : null),
|
|
92
|
-
continent: _.get(settings, 'activity.geolocation.continent', defaults ? '' : null),
|
|
93
|
-
country: _.get(settings, 'activity.geolocation.country', defaults ? '' : null),
|
|
94
|
-
region: _.get(settings, 'activity.geolocation.region', defaults ? '' : null),
|
|
95
|
-
city: _.get(settings, 'activity.geolocation.city', defaults ? '' : null),
|
|
96
|
-
latitude: _.get(settings, 'activity.geolocation.latitude', defaults ? 0 : null),
|
|
97
|
-
longitude: _.get(settings, 'activity.geolocation.longitude', defaults ? 0 : null),
|
|
98
|
-
},
|
|
99
|
-
client: {
|
|
100
|
-
userAgent: _.get(settings, 'activity.client.userAgent', defaults ? '' : null),
|
|
101
|
-
language: _.get(settings, 'activity.client.language', defaults ? '' : null),
|
|
102
|
-
platform: _.get(settings, 'activity.client.platform', defaults ? '' : null),
|
|
103
|
-
mobile: _.get(settings, 'activity.client.mobile', defaults ? null : null),
|
|
104
|
-
},
|
|
105
|
-
},
|
|
106
|
-
api: {
|
|
107
|
-
clientId: _.get(settings, 'api.clientId', defaults ? `${uuid4()}` : null),
|
|
108
|
-
privateKey: _.get(settings, 'api.privateKey', defaults ? `${uidgen.generateSync()}` : null),
|
|
109
|
-
},
|
|
110
|
-
usage: {
|
|
111
|
-
requests: {
|
|
112
|
-
period: _.get(settings, 'usage.requests.period', defaults ? 0 : null),
|
|
113
|
-
total: _.get(settings, 'usage.requests.total', defaults ? 0 : null),
|
|
114
|
-
last: {
|
|
115
|
-
id: _.get(settings, 'usage.requests.last.id', defaults ? '' : null),
|
|
116
|
-
timestamp: _.get(settings, 'usage.requests.last.timestamp', defaults ? oldDate : null),
|
|
117
|
-
timestampUNIX: _.get(settings, 'usage.requests.last.timestampUNIX', defaults ? oldDateUNIX : null),
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
personal: {
|
|
122
|
-
birthday: {
|
|
123
|
-
timestamp: _.get(settings, 'personal.birthday.timestamp', defaults ? oldDate : null),
|
|
124
|
-
timestampUNIX: _.get(settings, 'personal.birthday.timestampUNIX', defaults ? oldDateUNIX : null),
|
|
125
|
-
},
|
|
126
|
-
gender: _.get(settings, 'personal.gender', defaults ? '' : null),
|
|
127
|
-
location: {
|
|
128
|
-
country: _.get(settings, 'personal.location.country', defaults ? '' : null),
|
|
129
|
-
region: _.get(settings, 'personal.location.region', defaults ? '' : null),
|
|
130
|
-
city: _.get(settings, 'personal.location.city', defaults ? '' : null),
|
|
131
|
-
},
|
|
132
|
-
name: {
|
|
133
|
-
first: _.get(settings, 'personal.name.first', defaults ? '' : null),
|
|
134
|
-
last: _.get(settings, 'personal.name.last', defaults ? '' : null),
|
|
135
|
-
},
|
|
136
|
-
telephone: {
|
|
137
|
-
countryCode: _.get(settings, 'personal.telephone.countryCode', defaults ? 0 : null),
|
|
138
|
-
national: _.get(settings, 'personal.telephone.national', defaults ? 0 : null),
|
|
139
|
-
},
|
|
140
|
-
},
|
|
141
|
-
oauth2: {
|
|
142
|
-
// updated: {
|
|
143
|
-
// timestamp: _.get(settings, 'oauth2.updated.timestamp', defaults ? oldDate : null),
|
|
144
|
-
// timestampUNIX: _.get(settings, 'oauth2.updated.timestampUNIX', defaults ? oldDateUNIX : null),
|
|
145
|
-
// },
|
|
146
|
-
},
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (options.prune) {
|
|
150
|
-
self.properties = pruneObject(self.properties);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
self.resolve = function (options) {
|
|
154
|
-
options = options || {};
|
|
155
|
-
options.defaultPlan = options.defaultPlan || 'basic';
|
|
156
|
-
const planId = _.get(self.properties, 'plan.id', options.defaultPlan);
|
|
157
|
-
const premiumExpire = _.get(self.properties, 'plan.expires.timestamp', 0);
|
|
158
|
-
|
|
159
|
-
let difference = ((new Date(premiumExpire).getTime() - new Date().getTime())/(24*3600*1000));
|
|
160
|
-
// console.log('---difference', difference);
|
|
161
|
-
if (difference <= -1) {
|
|
162
|
-
_.set(self.properties, 'plan.id', options.defaultPlan);
|
|
163
|
-
// console.log('---REVERTED TO BASIC BECAUSE EXPIRED');
|
|
164
|
-
} else {
|
|
165
|
-
// console.log('---ITS FINE');
|
|
166
|
-
}
|
|
167
|
-
return self;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
self.merge = function (userObject) {
|
|
171
|
-
self.properties = _.merge({}, self.properties, userObject)
|
|
172
|
-
return self;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return self;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
// https://stackoverflow.com/a/26202058/7305269
|
|
180
|
-
function pruneObject(obj) {
|
|
181
|
-
return function prune(current) {
|
|
182
|
-
_.forOwn(current, function (value, key) {
|
|
183
|
-
if (_.isUndefined(value) || _.isNull(value) || _.isNaN(value) ||
|
|
184
|
-
(_.isObject(value) && _.isEmpty(prune(value)))) {
|
|
185
|
-
|
|
186
|
-
delete current[key];
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
// remove any leftover undefined values from the delete
|
|
190
|
-
// operation on an array
|
|
191
|
-
if (_.isArray(current)) _.pull(current, undefined);
|
|
192
|
-
|
|
193
|
-
return current;
|
|
194
|
-
|
|
195
|
-
}(_.cloneDeep(obj)); // Do not modify the original object, create a clone instead
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
module.exports = User;
|