@drawbridge/drawbridge-stripe 0.1.30 → 0.1.31

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/dist/index.d.mts CHANGED
@@ -12,6 +12,9 @@ module.exports = {
12
12
  fx : lib.fx({
13
13
  request
14
14
  }),
15
+ reconcile : lib.reconcile({
16
+ stripe
17
+ }),
15
18
  stripe,
16
19
  subscription : lib.subscription({
17
20
  stripe
package/dist/index.d.ts CHANGED
@@ -12,6 +12,9 @@ module.exports = {
12
12
  fx : lib.fx({
13
13
  request
14
14
  }),
15
+ reconcile : lib.reconcile({
16
+ stripe
17
+ }),
15
18
  stripe,
16
19
  subscription : lib.subscription({
17
20
  stripe
package/dist/index.js CHANGED
@@ -152,6 +152,105 @@ var require_fx = __commonJS({
152
152
  }
153
153
  });
154
154
 
155
+ // lib/reconcile.js
156
+ var require_reconcile = __commonJS({
157
+ "lib/reconcile.js"(exports2, module2) {
158
+ module2.exports = ({
159
+ stripe: stripe2
160
+ }) => {
161
+ return async ({
162
+ db,
163
+ organization,
164
+ stripeSubscriptionId,
165
+ subscription
166
+ }) => {
167
+ if (!db || !stripeSubscriptionId || !subscription) {
168
+ return null;
169
+ }
170
+ ;
171
+ const open = await stripe2.invoices.list({
172
+ limit: 100,
173
+ status: "open",
174
+ subscription: stripeSubscriptionId
175
+ });
176
+ const openInvoices = (open == null ? void 0 : open.data) || [];
177
+ const openStripeIds = openInvoices.map((invoice) => invoice.id);
178
+ const locals = await db.aggregate({
179
+ collection: "invoice",
180
+ pipeline: [
181
+ {
182
+ $match: {
183
+ organization: organization == null ? void 0 : organization.id,
184
+ subscription,
185
+ $or: [
186
+ {
187
+ status: {
188
+ $in: ["open", "incomplete"]
189
+ }
190
+ },
191
+ {
192
+ stripeInvoiceId: {
193
+ $in: openStripeIds
194
+ }
195
+ }
196
+ ]
197
+ }
198
+ }
199
+ ]
200
+ });
201
+ await Promise.all(locals.map(async (invoice) => {
202
+ const outstanding = openStripeIds.includes(invoice.stripeInvoiceId);
203
+ const marked = ["open", "incomplete"].includes(invoice.status);
204
+ if (outstanding === marked) {
205
+ return;
206
+ }
207
+ ;
208
+ const liveInvoice = await stripe2.invoices.retrieve(invoice.stripeInvoiceId);
209
+ await db.update({
210
+ collection: "invoice",
211
+ data: {
212
+ $set: {
213
+ "amount.due": liveInvoice == null ? void 0 : liveInvoice.amount_due,
214
+ "amount.paid": liveInvoice == null ? void 0 : liveInvoice.amount_paid,
215
+ "amount.remaining": liveInvoice == null ? void 0 : liveInvoice.amount_remaining,
216
+ status: liveInvoice == null ? void 0 : liveInvoice.status
217
+ }
218
+ },
219
+ options: {
220
+ bypassDocumentValidation: true
221
+ },
222
+ query: {
223
+ id: invoice.id
224
+ }
225
+ });
226
+ }));
227
+ const localByStripeId = Object.fromEntries(locals.map((invoice) => [invoice.stripeInvoiceId, invoice]));
228
+ const invoiceErrors = openInvoices.map((invoice) => {
229
+ const local = localByStripeId[invoice.id];
230
+ return {
231
+ message: "Invoice #" + ((local == null ? void 0 : local.number) || invoice.number) + " is outstanding.",
232
+ reference: local == null ? void 0 : local.id,
233
+ source: "stripe.reconcile",
234
+ type: "invoice"
235
+ };
236
+ });
237
+ const liveSubscription = await stripe2.subscriptions.retrieve(stripeSubscriptionId);
238
+ const canceled = Boolean(liveSubscription == null ? void 0 : liveSubscription.canceled_at);
239
+ const suspended = Boolean((organization == null ? void 0 : organization.status) === "suspended");
240
+ let status = suspended ? "suspended" : canceled ? "unsubscribed" : liveSubscription == null ? void 0 : liveSubscription.status;
241
+ if (openInvoices.length > 0) {
242
+ status = "incomplete";
243
+ }
244
+ ;
245
+ return {
246
+ invoiceErrors,
247
+ status
248
+ };
249
+ };
250
+ };
251
+ }
252
+ });
253
+
155
254
  // lib/subscription.js
156
255
  var require_subscription = __commonJS({
157
256
  "lib/subscription.js"(exports2, module2) {
@@ -900,9 +999,11 @@ var require_subscription = __commonJS({
900
999
  var require_lib = __commonJS({
901
1000
  "lib/index.js"(exports2, module2) {
902
1001
  var fx = require_fx();
1002
+ var reconcile = require_reconcile();
903
1003
  var subscription = require_subscription();
904
1004
  module2.exports = {
905
1005
  fx,
1006
+ reconcile,
906
1007
  subscription
907
1008
  };
908
1009
  }
@@ -921,6 +1022,9 @@ module.exports = {
921
1022
  fx: lib.fx({
922
1023
  request
923
1024
  }),
1025
+ reconcile: lib.reconcile({
1026
+ stripe
1027
+ }),
924
1028
  stripe,
925
1029
  subscription: lib.subscription({
926
1030
  stripe
package/dist/index.mjs CHANGED
@@ -158,6 +158,105 @@ var require_fx = __commonJS({
158
158
  }
159
159
  });
160
160
 
161
+ // lib/reconcile.js
162
+ var require_reconcile = __commonJS({
163
+ "lib/reconcile.js"(exports, module) {
164
+ module.exports = ({
165
+ stripe
166
+ }) => {
167
+ return async ({
168
+ db,
169
+ organization,
170
+ stripeSubscriptionId,
171
+ subscription
172
+ }) => {
173
+ if (!db || !stripeSubscriptionId || !subscription) {
174
+ return null;
175
+ }
176
+ ;
177
+ const open = await stripe.invoices.list({
178
+ limit: 100,
179
+ status: "open",
180
+ subscription: stripeSubscriptionId
181
+ });
182
+ const openInvoices = (open == null ? void 0 : open.data) || [];
183
+ const openStripeIds = openInvoices.map((invoice) => invoice.id);
184
+ const locals = await db.aggregate({
185
+ collection: "invoice",
186
+ pipeline: [
187
+ {
188
+ $match: {
189
+ organization: organization == null ? void 0 : organization.id,
190
+ subscription,
191
+ $or: [
192
+ {
193
+ status: {
194
+ $in: ["open", "incomplete"]
195
+ }
196
+ },
197
+ {
198
+ stripeInvoiceId: {
199
+ $in: openStripeIds
200
+ }
201
+ }
202
+ ]
203
+ }
204
+ }
205
+ ]
206
+ });
207
+ await Promise.all(locals.map(async (invoice) => {
208
+ const outstanding = openStripeIds.includes(invoice.stripeInvoiceId);
209
+ const marked = ["open", "incomplete"].includes(invoice.status);
210
+ if (outstanding === marked) {
211
+ return;
212
+ }
213
+ ;
214
+ const liveInvoice = await stripe.invoices.retrieve(invoice.stripeInvoiceId);
215
+ await db.update({
216
+ collection: "invoice",
217
+ data: {
218
+ $set: {
219
+ "amount.due": liveInvoice == null ? void 0 : liveInvoice.amount_due,
220
+ "amount.paid": liveInvoice == null ? void 0 : liveInvoice.amount_paid,
221
+ "amount.remaining": liveInvoice == null ? void 0 : liveInvoice.amount_remaining,
222
+ status: liveInvoice == null ? void 0 : liveInvoice.status
223
+ }
224
+ },
225
+ options: {
226
+ bypassDocumentValidation: true
227
+ },
228
+ query: {
229
+ id: invoice.id
230
+ }
231
+ });
232
+ }));
233
+ const localByStripeId = Object.fromEntries(locals.map((invoice) => [invoice.stripeInvoiceId, invoice]));
234
+ const invoiceErrors = openInvoices.map((invoice) => {
235
+ const local = localByStripeId[invoice.id];
236
+ return {
237
+ message: "Invoice #" + ((local == null ? void 0 : local.number) || invoice.number) + " is outstanding.",
238
+ reference: local == null ? void 0 : local.id,
239
+ source: "stripe.reconcile",
240
+ type: "invoice"
241
+ };
242
+ });
243
+ const liveSubscription = await stripe.subscriptions.retrieve(stripeSubscriptionId);
244
+ const canceled = Boolean(liveSubscription == null ? void 0 : liveSubscription.canceled_at);
245
+ const suspended = Boolean((organization == null ? void 0 : organization.status) === "suspended");
246
+ let status = suspended ? "suspended" : canceled ? "unsubscribed" : liveSubscription == null ? void 0 : liveSubscription.status;
247
+ if (openInvoices.length > 0) {
248
+ status = "incomplete";
249
+ }
250
+ ;
251
+ return {
252
+ invoiceErrors,
253
+ status
254
+ };
255
+ };
256
+ };
257
+ }
258
+ });
259
+
161
260
  // lib/subscription.js
162
261
  var require_subscription = __commonJS({
163
262
  "lib/subscription.js"(exports, module) {
@@ -906,9 +1005,11 @@ var require_subscription = __commonJS({
906
1005
  var require_lib = __commonJS({
907
1006
  "lib/index.js"(exports, module) {
908
1007
  var fx = require_fx();
1008
+ var reconcile = require_reconcile();
909
1009
  var subscription = require_subscription();
910
1010
  module.exports = {
911
1011
  fx,
1012
+ reconcile,
912
1013
  subscription
913
1014
  };
914
1015
  }
@@ -929,6 +1030,9 @@ var require_index = __commonJS({
929
1030
  fx: lib.fx({
930
1031
  request
931
1032
  }),
1033
+ reconcile: lib.reconcile({
1034
+ stripe
1035
+ }),
932
1036
  stripe,
933
1037
  subscription: lib.subscription({
934
1038
  stripe
package/package.json CHANGED
@@ -26,5 +26,5 @@
26
26
  "build": "tsup ./index.js && npm publish"
27
27
  },
28
28
  "types": "dist/index.d.ts",
29
- "version": "0.1.30"
29
+ "version": "0.1.31"
30
30
  }