|
1 | 1 | const moment = require('moment-timezone') |
2 | 2 | const admin = require('firebase-admin') |
| 3 | +const stripe = require('stripe')(process.env.GATEWAY_SK) |
3 | 4 |
|
4 | 5 | const _firebaseConfig = { |
5 | 6 | type: 'service_account', |
@@ -30,46 +31,88 @@ if (!admin.apps.length) { |
30 | 31 | } |
31 | 32 | const firestore = admin.firestore() |
32 | 33 |
|
33 | | -exports.newThisPeriod = data => { |
34 | | - return new Promise((resolve, reject) => { |
35 | | - const trackedRef = firestore |
36 | | - .collection('applications') |
37 | | - .doc(`${data.applicationId}`) |
38 | | - .collection(`${data.collectionId}`) |
39 | | - .doc(`${data.subscription.id}`) |
40 | | - .collection( |
41 | | - `${moment |
42 | | - .unix(data.subscription.current_period_start) |
43 | | - .format('MMDDYYYY')}_${moment |
44 | | - .unix(data.subscription.current_period_end) |
45 | | - .format('MMDDYYYY')}` |
46 | | - ) |
47 | | - .doc(`${data.tracked.id}`) |
| 34 | +module.exports = { |
| 35 | + newThisPeriod(applicationId, collectionId, subscription, tracked) { |
| 36 | + return new Promise((resolve, reject) => { |
| 37 | + const metadata = tracked.metadata |
| 38 | + const trackedRef = firestore |
| 39 | + .collection('applications') |
| 40 | + .doc(`${applicationId}`) |
| 41 | + .collection(`${collectionId}`) |
| 42 | + .doc(`${subscription.id}`) |
| 43 | + .collection( |
| 44 | + `${moment |
| 45 | + .unix(subscription.current_period_start) |
| 46 | + .format('MMDDYYYY')}_${moment |
| 47 | + .unix(subscription.current_period_end) |
| 48 | + .format('MMDDYYYY')}` |
| 49 | + ) |
| 50 | + .doc(`${tracked.id}`) |
48 | 51 |
|
49 | | - return trackedRef |
50 | | - .get() |
51 | | - .then(billingPeriodTrackedRecord => { |
52 | | - // console.log('billingPeriodTrackedRecord',billingPeriodTrackedRecord) |
53 | | - return trackedRef |
54 | | - .set( |
55 | | - { |
56 | | - value: data.tracked.value, |
57 | | - usage: admin.firestore.FieldValue.arrayUnion( |
58 | | - moment() |
59 | | - .utc() |
60 | | - .format() |
61 | | - ) |
62 | | - }, |
63 | | - { merge: true } |
64 | | - ) |
65 | | - .then(() => { |
66 | | - // console.log( |
67 | | - // `trackedRef, newThisPeriod: ${!billingPeriodTrackedRecord.exists}` |
68 | | - // ) |
69 | | - resolve(!billingPeriodTrackedRecord.exists) |
70 | | - }) |
71 | | - .catch(error => reject(error)) |
| 52 | + return trackedRef |
| 53 | + .get() |
| 54 | + .then(billingPeriodTrackedRecord => { |
| 55 | + // console.log('billingPeriodTrackedRecord',billingPeriodTrackedRecord) |
| 56 | + return trackedRef |
| 57 | + .set( |
| 58 | + { |
| 59 | + metadata, |
| 60 | + usage: admin.firestore.FieldValue.arrayUnion( |
| 61 | + moment() |
| 62 | + .utc() |
| 63 | + .format() |
| 64 | + ) |
| 65 | + }, |
| 66 | + { merge: true } |
| 67 | + ) |
| 68 | + .then(() => { |
| 69 | + // console.log( |
| 70 | + // `trackedRef, newThisPeriod: ${!billingPeriodTrackedRecord.exists}` |
| 71 | + // ) |
| 72 | + resolve(!billingPeriodTrackedRecord.exists) |
| 73 | + }) |
| 74 | + .catch(error => reject(error)) |
| 75 | + }) |
| 76 | + .catch(error => reject(error)) |
| 77 | + }) |
| 78 | + }, |
| 79 | + |
| 80 | + createUsageRecords( |
| 81 | + subscriptionItems, |
| 82 | + quantity = 1, |
| 83 | + timestamp = moment().unix() |
| 84 | + ) { |
| 85 | + return new Promise((resolve, reject) => { |
| 86 | + const usageRecords = [] |
| 87 | + subscriptionItems.map(subscriptionItem => { |
| 88 | + usageRecords.push( |
| 89 | + stripe.usageRecords |
| 90 | + .create(subscriptionItem.id, { |
| 91 | + quantity, |
| 92 | + timestamp |
| 93 | + }) |
| 94 | + .then(handleError) |
| 95 | + ) |
72 | 96 | }) |
73 | | - .catch(error => reject(error)) |
| 97 | + |
| 98 | + return Promise.all(usageRecords) |
| 99 | + .then(responses => { |
| 100 | + resolve(responses) |
| 101 | + }) |
| 102 | + .catch(error => reject(error)) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +const handleError = response => { |
| 108 | + return new Promise((resolve, reject) => { |
| 109 | + if (response.error) { |
| 110 | + console.error( |
| 111 | + `handleError, response.error: ${JSON.stringify(response.error)}` |
| 112 | + ) |
| 113 | + reject(response.error) |
| 114 | + } else { |
| 115 | + resolve(response) |
| 116 | + } |
74 | 117 | }) |
75 | 118 | } |
0 commit comments