backend-manager 2.5.73 → 2.5.75

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": "backend-manager",
3
- "version": "2.5.73",
3
+ "version": "2.5.75",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -15,6 +15,21 @@ Module.prototype.main = function () {
15
15
  return new Promise(async function(resolve, reject) {
16
16
  self.Api.resolveUser({adminRequired: true})
17
17
  .then(async (user) => {
18
+ // Get auth user from firebase
19
+ const authUser = await self.libraries.admin.auth().getUser(user.auth.uid).catch(e => e);
20
+
21
+ if (authUser instanceof Error) {
22
+ return reject(assistant.errorManager(`Failed to get auth user: ${authUser}`, {code: 500, sentry: false, send: false, log: false}).error)
23
+ }
24
+
25
+ // Difference in hours
26
+ const diff = (Date.now() - new Date(authUser.metadata.creationTime)) / 36e5;
27
+
28
+ // If the user is not new, reject
29
+ if (diff > 0.5) {
30
+ return reject(assistant.errorManager(`User is not new.`, {code: 400, sentry: false, send: false, log: false}).error)
31
+ }
32
+
18
33
  await self.signUp({
19
34
  auth: {
20
35
  uid: _.get(user, 'auth.uid', null),
@@ -16,6 +16,7 @@ SubscriptionResolver.prototype.resolve = function (options) {
16
16
 
17
17
  const resolved = {
18
18
  status: 'cancelled',
19
+ frequency: 'monthly',
19
20
  resource: {
20
21
  id: '',
21
22
  },
@@ -32,18 +33,41 @@ SubscriptionResolver.prototype.resolve = function (options) {
32
33
  },
33
34
  trial: {
34
35
  active: false,
36
+ daysLeft: 0,
35
37
  }
36
38
  }
37
39
 
40
+ // Set
38
41
  const profile = self.profile;
39
42
  const resource = self.resource;
40
43
 
44
+ // Set defaults
45
+ profile.details = profile.details || {};
46
+ profile.details.planFrequency = profile.details.planFrequency || null;
47
+
48
+ // Set
41
49
  options = options || {};
42
50
 
51
+ // Set provider if not set
52
+ if (!profile.processor) {
53
+ if (resource.billing_info) {
54
+ profile.processor = 'paypal';
55
+ } else if (resource.billing_period_unit) {
56
+ profile.processor = 'chargebee';
57
+ } else if (resource.customer) {
58
+ profile.processor = 'stripe';
59
+ } else if (resource.addresses) {
60
+ profile.processor = 'coinbase';
61
+ } else {
62
+ throw new Error('Unable to determine subscription provider');
63
+ }
64
+ }
65
+
66
+ // Log if requested
43
67
  if (options.log) {
44
68
  console.log('profile', profile);
45
69
  console.log('resource', resource);
46
- }
70
+ }
47
71
 
48
72
  // Process differently based on each provider
49
73
  if (profile.processor === 'paypal') {
@@ -75,28 +99,46 @@ SubscriptionResolver.prototype.resolve = function (options) {
75
99
  // Set resource ID
76
100
  resolved.resource.id = resource.id;
77
101
 
102
+ // Set start
103
+ resolved.start.timestamp = moment(
104
+ get(resource, 'start_time', 0)
105
+ )
106
+
107
+ // Set expiration
108
+ resolved.expires.timestamp = moment(
109
+ get(resource, 'billing_info.last_payment.time', 0)
110
+ )
111
+
78
112
  // Get trial
79
- const trialTenure = get(resource, 'billing_info.cycle_executions', []).find((cycle) => cycle.tenure_type === 'TRIAL');
80
- const regularTenure = get(resource, 'billing_info.cycle_executions', []).find((cycle) => cycle.tenure_type === 'REGULAR');
113
+ const trialTenure = get(resource, 'plan.billing_cycles', []).find((cycle) => cycle.tenure_type === 'TRIAL');
114
+ const regularTenure = get(resource, 'plan.billing_cycles', []).find((cycle) => cycle.tenure_type === 'REGULAR');
81
115
 
82
116
  // Resolve trial
83
- if (trialTenure && regularTenure && regularTenure.cycles_completed === 0) {
117
+ if (
118
+ resolved.status === 'active'
119
+ && (trialTenure && regularTenure && regularTenure.total_cycles === 0)
120
+ ) {
84
121
  resolved.trial.active = true;
85
122
 
86
- // Set expiration and start
123
+ // Set expiration
87
124
  resolved.expires.timestamp = moment(
88
125
  get(resource, 'billing_info.next_billing_time', 0)
89
126
  )
90
- } else {
91
- // Set expiration and start
92
- resolved.expires.timestamp = moment(
93
- get(resource, 'billing_info.last_payment.time', 0)
94
- )
95
127
  }
96
128
 
97
- resolved.start.timestamp = moment(
98
- get(resource, 'start_time', 0)
99
- )
129
+ // Resolve frequency
130
+ const unit = regularTenure.frequency.interval_unit;
131
+ if (unit === 'YEAR') {
132
+ resolved.frequency = 'annually';
133
+ } else if (unit === 'MONTH') {
134
+ resolved.frequency = 'monthly';
135
+ } else if (unit === 'WEEK') {
136
+ resolved.frequency = 'weekly';
137
+ } else if (unit === 'DAY') {
138
+ resolved.frequency = 'daily';
139
+ } else {
140
+ throw new Error('Unknown frequency');
141
+ }
100
142
 
101
143
  // Set completed
102
144
  if (resource.plan_id) {
@@ -120,28 +162,43 @@ SubscriptionResolver.prototype.resolve = function (options) {
120
162
  // Set resource ID
121
163
  resolved.resource.id = resource.id;
122
164
 
165
+ // Set start
166
+ resolved.start.timestamp = moment(
167
+ get(resource, 'created_at', 0) * 1000
168
+ )
169
+
170
+ // Set expiration
171
+ resolved.expires.timestamp = moment(
172
+ (
173
+ get(resource, 'current_term_start', 0)
174
+ ) * 1000
175
+ )
176
+
123
177
  // Get trial
124
178
  if (resource.status === 'in_trial') {
125
179
  resolved.trial.active = true;
126
180
 
127
- // Set expiration and start
181
+ // Set expiration
128
182
  resolved.expires.timestamp = moment(
129
183
  (
130
184
  get(resource, 'trial_end', 0)
131
185
  ) * 1000
132
186
  )
133
- } else {
134
- // Set expiration and start
135
- resolved.expires.timestamp = moment(
136
- (
137
- get(resource, 'current_term_start', 0)
138
- ) * 1000
139
- )
140
187
  }
141
188
 
142
- resolved.start.timestamp = moment(
143
- get(resource, 'created_at', 0) * 1000
144
- )
189
+ // Resolve frequency
190
+ const unit = resource.billing_period_unit;
191
+ if (unit === 'year') {
192
+ resolved.frequency = 'annually';
193
+ } else if (unit === 'month') {
194
+ resolved.frequency = 'monthly';
195
+ } else if (unit === 'week') {
196
+ resolved.frequency = 'weekly';
197
+ } else if (unit === 'day') {
198
+ resolved.frequency = 'daily';
199
+ } else {
200
+ throw new Error('Unknown frequency');
201
+ }
145
202
 
146
203
  // Set completed
147
204
  if (true) {
@@ -157,7 +214,7 @@ SubscriptionResolver.prototype.resolve = function (options) {
157
214
  // Set status
158
215
  if (['trialing', 'active'].includes(resource.status)) {
159
216
  resolved.status = 'active';
160
- } if (['past_due', 'unpaid'].includes(resource.status)) {
217
+ } else if (['past_due', 'unpaid'].includes(resource.status)) {
161
218
  resolved.status = 'suspended';
162
219
  } else {
163
220
  resolved.status = 'cancelled';
@@ -166,26 +223,41 @@ SubscriptionResolver.prototype.resolve = function (options) {
166
223
  // Set resource ID
167
224
  resolved.resource.id = resource.id;
168
225
 
226
+ // Set start
227
+ resolved.start.timestamp = moment(
228
+ get(resource, 'start_date', 0) * 1000
229
+ );
230
+
231
+ // Set expiration
232
+ resolved.expires.timestamp = moment(
233
+ get(resource, 'current_period_start', 0) * 1000
234
+ );
235
+
169
236
  // Get trial
170
237
  if (resource.status === 'trialing') {
171
238
  resolved.trial.active = true;
172
239
 
173
- // Set expiration and start
240
+ // Set expiration
174
241
  resolved.expires.timestamp = moment(
175
242
  (
176
243
  get(resource, 'trial_end', 0)
177
244
  ) * 1000
178
- )
179
- } else {
180
- // Set expiration and start
181
- resolved.expires.timestamp = moment(
182
- get(resource, 'current_period_start', 0) * 1000
183
- );
245
+ )
184
246
  }
185
247
 
186
- resolved.start.timestamp = moment(
187
- get(resource, 'start_date', 0) * 1000
188
- );
248
+ // Resolve frequency @@@
249
+ const unit = resource.plan.interval;
250
+ if (unit === 'year') {
251
+ resolved.frequency = 'annually';
252
+ } else if (unit === 'month') {
253
+ resolved.frequency = 'monthly';
254
+ } else if (unit === 'week') {
255
+ resolved.frequency = 'weekly';
256
+ } else if (unit === 'day') {
257
+ resolved.frequency = 'daily';
258
+ } else {
259
+ throw new Error('Unknown frequency');
260
+ }
189
261
 
190
262
  // Set completed
191
263
  if (resource.object === 'subscription') {
@@ -201,37 +273,56 @@ SubscriptionResolver.prototype.resolve = function (options) {
201
273
  // Set resource ID
202
274
  resolved.resource.id = resource.id;
203
275
 
276
+ // Set start
277
+ resolved.start.timestamp = moment(
278
+ get(resource, 'created_at', 0)
279
+ );
280
+
281
+ // Set expiration
282
+ resolved.expires.timestamp = moment(
283
+ get(resource, 'created_at', 0)
284
+ );
285
+
204
286
  // Get trial
205
287
  if (true) {
206
288
  resolved.trial.active = false;
207
289
  }
208
290
 
209
- // Set expiration and start
210
- resolved.expires.timestamp = moment(
211
- get(resource, 'created_at', 0)
212
- );
213
- resolved.start.timestamp = moment(
214
- get(resource, 'created_at', 0)
215
- );
291
+ // Resolve frequency
292
+ const unit = profile.details.planFrequency;
293
+ if (unit) {
294
+ resolved.frequency = unit;
295
+ } else {
296
+ throw new Error('Unknown frequency');
297
+ }
216
298
 
217
299
  // Set completed
218
300
  if (true) {
219
301
  resolved.payment.completed = !!resource.payments.find(p => p.status === 'CONFIRMED');
220
302
  }
303
+ } else {
304
+ throw new Error('Unknown processor');
221
305
  }
222
306
 
223
307
  // Fix expiry by adding time to the date of last payment
224
308
  if (resolved.status === 'active') {
309
+ // Set days left
310
+ if (resolved.trial.active) {
311
+ resolved.trial.daysLeft = resolved.expires.timestamp.diff(moment(), 'days');
312
+ }
313
+
314
+ // Set expiration
225
315
  resolved.expires.timestamp.add(1, 'year').add(30, 'days');
226
316
  } else {
227
317
  // If trial, it's already set to the trial end above
228
318
  if (!resolved.trial.active) {
229
- const freq = profile.details.planFrequency || 'monthly';
230
- if (freq === 'annually') {
319
+ if (resolved.frequency === 'annually') {
231
320
  resolved.expires.timestamp.add(1, 'year');
232
- } else if (freq === 'monthly') {
321
+ } else if (resolved.frequency === 'monthly') {
233
322
  resolved.expires.timestamp.add(1, 'month');
234
- } else if (freq === 'daily') {
323
+ } else if (resolved.frequency === 'weekly') {
324
+ resolved.expires.timestamp.add(1, 'week');
325
+ } else if (resolved.frequency === 'daily') {
235
326
  resolved.expires.timestamp.add(1, 'day');
236
327
  }
237
328
  }
@@ -243,12 +334,16 @@ SubscriptionResolver.prototype.resolve = function (options) {
243
334
  }
244
335
 
245
336
  // Fix timestamps
246
- resolved.expires.timestampUNIX = resolved.expires.timestamp.unix()
247
- resolved.expires.timestamp = resolved.expires.timestamp.toISOString()
337
+ resolved.expires.timestampUNIX = resolved.expires.timestamp.unix();
338
+ resolved.expires.timestamp = resolved.expires.timestamp.toISOString();
248
339
 
249
- resolved.start.timestampUNIX = resolved.start.timestamp.unix()
250
- resolved.start.timestamp = resolved.start.timestamp.toISOString()
340
+ resolved.start.timestampUNIX = resolved.start.timestamp.unix();
341
+ resolved.start.timestamp = resolved.start.timestamp.toISOString();
251
342
 
343
+ // Fix trial days
344
+ resolved.trial.daysLeft = resolved.trial.daysLeft < 0 ? 0 : resolved.trial.daysLeft;
345
+
346
+ // Log if needed
252
347
  if (options.log) {
253
348
  console.log('resolved', resolved);
254
349
  }