backend-manager 2.5.34 → 2.5.35

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.35",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -0,0 +1,123 @@
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
+ if (profile.processor === 'paypal') {
30
+ if (resource.status === 'ACTIVE') {
31
+ resolved.status = 'active';
32
+ } else if (resource.status === 'SUSPENDED') {
33
+ resolved.status = 'suspended';
34
+ } else {
35
+ resolved.status = 'cancelled';
36
+ }
37
+ resolved.resource.id = resource.id,
38
+ resolved.expires.timestamp = moment(
39
+ get(resource, 'billing_info.last_payment.time', 0)
40
+ )
41
+ resolved.start.timestamp = moment(
42
+ get(resource, 'start_time', 0)
43
+ )
44
+ } else if (profile.processor === 'chargebee') {
45
+ if (resource.status === 'active') {
46
+ resolved.status = 'active';
47
+ } else if (resource.status === 'paused') {
48
+ resolved.status = 'suspended';
49
+ } else {
50
+ resolved.status = 'cancelled';
51
+ }
52
+ resolved.resource.id = resource.id,
53
+ resolved.expires.timestamp = moment(
54
+ get(resource, 'current_term_start', 0) * 1000
55
+ )
56
+ resolved.start.timestamp = moment(
57
+ get(resource, 'created_at', 0) * 1000
58
+ )
59
+ } else if (profile.processor === 'stripe') {
60
+ if (resource.status === 'active') {
61
+ resolved.status = 'active';
62
+ } else if (resource.status === 'past_due' || resource.status === 'unpaid' || resource.status === 'incomplete' || resource.status === 'incomplete_expired') {
63
+ resolved.status = 'suspended';
64
+ } else {
65
+ resolved.status = 'cancelled';
66
+ }
67
+ resolved.resource.id = resource.id,
68
+ resolved.expires.timestamp = moment(
69
+ get(resource, 'current_period_start', 0) * 1000
70
+ )
71
+ resolved.start.timestamp = moment(
72
+ get(resource, 'start_date', 0) * 1000
73
+ )
74
+ } else if (profile.processor === 'coinbase') {
75
+ // TODO: look in to how to detect a failed payment
76
+ // const completed = resource.confirmed_at;
77
+ const completed = resource.payments.find(p => p.status === 'CONFIRMED');
78
+ resolved.status = 'cancelled';
79
+
80
+ resolved.resource.id = resource.id,
81
+ resolved.expires.timestamp = moment(
82
+ get(resource, 'created_at', 0)
83
+ )
84
+ resolved.start.timestamp = moment(
85
+ get(resource, 'created_at', 0)
86
+ );
87
+
88
+ // SPECIAL:
89
+ // Special coinbase condition: if it was never completed, it was never paid
90
+ if (!completed) {
91
+ resolved.expires.timestamp = moment(0);
92
+ }
93
+ }
94
+
95
+ // Fix expires by adding time to the date of last payment
96
+ if (resolved.status === 'active') {
97
+ resolved.expires.timestamp.add(1, 'year').add(30, 'days');
98
+ } else {
99
+ const freq = profile.details.planFrequency;
100
+ if (freq === 'annually') {
101
+ resolved.expires.timestamp.add(1, 'year');
102
+ } else if (freq === 'monthly') {
103
+ resolved.expires.timestamp.add(1, 'month');
104
+ } else if (freq === 'daily') {
105
+ resolved.expires.timestamp.add(1, 'day');
106
+ }
107
+ }
108
+
109
+ // Fix timestamps
110
+ resolved.expires.timestampUNIX = resolved.expires.timestamp.unix()
111
+ resolved.expires.timestamp = resolved.expires.timestamp.toISOString()
112
+
113
+ resolved.start.timestampUNIX = resolved.start.timestamp.unix()
114
+ resolved.start.timestamp = resolved.start.timestamp.toISOString()
115
+
116
+ // console.log('---resolved', resolved);
117
+
118
+ self.resolved = resolved;
119
+
120
+ return resolved;
121
+ };
122
+
123
+ 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 || {};