backend-manager 2.5.34 → 2.5.36

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.34",
3
+ "version": "2.5.36",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -0,0 +1,126 @@
1
+ const moment = require('moment');
2
+ const { get } = require('lodash');
3
+
4
+ function SubscriptionResolver(profile, resource) {
5
+ const self = this;
6
+
7
+ self.profile = profile;
8
+ self.resource = resource;
9
+ }
10
+
11
+ SubscriptionResolver.prototype.resolve = function () {
12
+ const self = this;
13
+
14
+ const resolved = {
15
+ status: '',
16
+ resource: {
17
+ id: '',
18
+ },
19
+ expires: {
20
+ timestamp: moment(0),
21
+ timestampUNIX: moment(0),
22
+ },
23
+ start: {
24
+ timestamp: moment(0),
25
+ timestampUNIX: moment(0),
26
+ },
27
+ }
28
+
29
+ const profile = self.profile;
30
+ const resource = self.resource;
31
+
32
+ if (profile.processor === 'paypal') {
33
+ if (resource.status === 'ACTIVE') {
34
+ resolved.status = 'active';
35
+ } else if (resource.status === 'SUSPENDED') {
36
+ resolved.status = 'suspended';
37
+ } else {
38
+ resolved.status = 'cancelled';
39
+ }
40
+ resolved.resource.id = resource.id,
41
+ resolved.expires.timestamp = moment(
42
+ get(resource, 'billing_info.last_payment.time', 0)
43
+ )
44
+ resolved.start.timestamp = moment(
45
+ get(resource, 'start_time', 0)
46
+ )
47
+ } else if (profile.processor === 'chargebee') {
48
+ if (resource.status === 'active') {
49
+ resolved.status = 'active';
50
+ } else if (resource.status === 'paused') {
51
+ resolved.status = 'suspended';
52
+ } else {
53
+ resolved.status = 'cancelled';
54
+ }
55
+ resolved.resource.id = resource.id,
56
+ resolved.expires.timestamp = moment(
57
+ get(resource, 'current_term_start', 0) * 1000
58
+ )
59
+ resolved.start.timestamp = moment(
60
+ get(resource, 'created_at', 0) * 1000
61
+ )
62
+ } else if (profile.processor === 'stripe') {
63
+ if (resource.status === 'active') {
64
+ resolved.status = 'active';
65
+ } else if (resource.status === 'past_due' || resource.status === 'unpaid' || resource.status === 'incomplete' || resource.status === 'incomplete_expired') {
66
+ resolved.status = 'suspended';
67
+ } else {
68
+ resolved.status = 'cancelled';
69
+ }
70
+ resolved.resource.id = resource.id,
71
+ resolved.expires.timestamp = moment(
72
+ get(resource, 'current_period_start', 0) * 1000
73
+ )
74
+ resolved.start.timestamp = moment(
75
+ get(resource, 'start_date', 0) * 1000
76
+ )
77
+ } else if (profile.processor === 'coinbase') {
78
+ // TODO: look in to how to detect a failed payment
79
+ // const completed = resource.confirmed_at;
80
+ const completed = resource.payments.find(p => p.status === 'CONFIRMED');
81
+ resolved.status = 'cancelled';
82
+
83
+ resolved.resource.id = resource.id,
84
+ resolved.expires.timestamp = moment(
85
+ get(resource, 'created_at', 0)
86
+ )
87
+ resolved.start.timestamp = moment(
88
+ get(resource, 'created_at', 0)
89
+ );
90
+
91
+ // SPECIAL:
92
+ // Special coinbase condition: if it was never completed, it was never paid
93
+ if (!completed) {
94
+ resolved.expires.timestamp = moment(0);
95
+ }
96
+ }
97
+
98
+ // Fix expires by adding time to the date of last payment
99
+ if (resolved.status === 'active') {
100
+ resolved.expires.timestamp.add(1, 'year').add(30, 'days');
101
+ } else {
102
+ const freq = profile.details.planFrequency;
103
+ if (freq === 'annually') {
104
+ resolved.expires.timestamp.add(1, 'year');
105
+ } else if (freq === 'monthly') {
106
+ resolved.expires.timestamp.add(1, 'month');
107
+ } else if (freq === 'daily') {
108
+ resolved.expires.timestamp.add(1, 'day');
109
+ }
110
+ }
111
+
112
+ // Fix timestamps
113
+ resolved.expires.timestampUNIX = resolved.expires.timestamp.unix()
114
+ resolved.expires.timestamp = resolved.expires.timestamp.toISOString()
115
+
116
+ resolved.start.timestampUNIX = resolved.start.timestamp.unix()
117
+ resolved.start.timestamp = resolved.start.timestamp.toISOString()
118
+
119
+ // console.log('---resolved', resolved);
120
+
121
+ self.resolved = resolved;
122
+
123
+ return resolved;
124
+ };
125
+
126
+ module.exports = SubscriptionResolver;
@@ -582,6 +582,12 @@ Manager.prototype.Roles = function () {
582
582
  return new self.libraries.Roles(self, ...arguments);
583
583
  };
584
584
 
585
+ Manager.prototype.SubscriptionResolver = function () {
586
+ const self = this;
587
+ self.libraries.SubscriptionResolver = self.libraries.SubscriptionResolver || require('./helpers/subscription-resolver.js');
588
+ return new self.libraries.SubscriptionResolver(self, ...arguments);
589
+ };
590
+
585
591
  Manager.prototype.storage = function (options) {
586
592
  const self = this;
587
593
  options = options || {};