@drawbridge/drawbridge-utils 0.0.26 → 0.0.27

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.cjs CHANGED
@@ -31,6 +31,7 @@ __export(index_exports, {
31
31
  formatNumber: () => formatNumber,
32
32
  getPlanFeature: () => getPlanFeature,
33
33
  gigabyte: () => gigabyte,
34
+ incrementUsageTotals: () => incrementUsageTotals,
34
35
  infinite: () => infinite,
35
36
  isInfinite: () => isInfinite,
36
37
  megabyte: () => megabyte,
@@ -309,6 +310,25 @@ var getPlanFeature = (plan, key) => {
309
310
  message: granted ? feature : error
310
311
  };
311
312
  };
313
+ var incrementUsageTotals = async ({ controller, usageId, $inc, session }) => {
314
+ if (!usageId || !$inc) return;
315
+ const safeInc = Object.fromEntries(
316
+ Object.entries($inc).filter(([, value]) => {
317
+ const number = Number(value);
318
+ return Number.isFinite(number) && number !== 0;
319
+ })
320
+ );
321
+ if (Object.keys(safeInc).length === 0) return;
322
+ return controller.update({
323
+ collection: "usage",
324
+ data: { $inc: safeInc },
325
+ options: {
326
+ bypassDocumentValidation: true,
327
+ ...session && { session }
328
+ },
329
+ query: { id: usageId }
330
+ });
331
+ };
312
332
  var percentage = (value1, value2, decimals = 1) => ((value1 || 0) / (value2 || 0) * 100 || 0).toFixed(decimals);
313
333
  var reducers = {
314
334
  fields: (data2 = [], callback = () => ({})) => data2.reduce(
@@ -400,6 +420,7 @@ var currency = (val) => (0, import_currency_codes.code)(val);
400
420
  formatNumber,
401
421
  getPlanFeature,
402
422
  gigabyte,
423
+ incrementUsageTotals,
403
424
  infinite,
404
425
  isInfinite,
405
426
  megabyte,
package/dist/index.d.cts CHANGED
@@ -313,6 +313,44 @@ const getPlanFeature = ( plan, key ) => {
313
313
 
314
314
  };
315
315
 
316
+ // Centralized $inc helper for the per-org usage doc. Replaces inline
317
+ // $inc-with-bypass-validation patterns in drawbridge-api + drawbridge-sync.
318
+ //
319
+ // MongoDB's $inc with 0 / NaN / undefined / null writes the value into the
320
+ // field (zeroing or NaN-ing it) instead of no-op'ing — so we filter the
321
+ // $inc object to keep only finite, non-zero values and callers don't have
322
+ // to remember the guard.
323
+ //
324
+ // `session` is forwarded when present so callers inside a transaction get
325
+ // atomic writes; standalone callers omit it.
326
+ const incrementUsageTotals = async ({ controller, usageId, $inc, session }) => {
327
+
328
+ if( ! usageId || ! $inc ) return;
329
+
330
+ const safeInc = Object.fromEntries(
331
+ Object.entries( $inc ).filter( ([ , value ]) => {
332
+
333
+ const number = Number( value );
334
+
335
+ return Number.isFinite( number ) && number !== 0;
336
+
337
+ })
338
+ );
339
+
340
+ if( Object.keys( safeInc ).length === 0 ) return;
341
+
342
+ return controller.update({
343
+ collection : 'usage',
344
+ data : { $inc : safeInc },
345
+ options : {
346
+ bypassDocumentValidation : true,
347
+ ...( session && { session })
348
+ },
349
+ query : { id : usageId }
350
+ });
351
+
352
+ };
353
+
316
354
  const percentage = ( value1, value2, decimals = 1 ) => ( ( ( ( value1 || 0 ) / ( value2 || 0 ) ) * 100 ) || 0 ).toFixed( decimals );
317
355
 
318
356
  const reducers = {
@@ -411,4 +449,4 @@ const currencies = data.map( ( item ) => ({
411
449
 
412
450
  const currency = ( val ) => code( val );
413
451
 
414
- export { bytesToGB, bytesToMB, capitalize, constants, currencies, currency, expiredPaymentMethod, formatCurrency, formatDateString, formatNumber, getPlanFeature, gigabyte, infinite, isInfinite, megabyte, nanoid, percentage, reducers, regex, shareUrls, urlRoot };
452
+ export { bytesToGB, bytesToMB, capitalize, constants, currencies, currency, expiredPaymentMethod, formatCurrency, formatDateString, formatNumber, getPlanFeature, gigabyte, incrementUsageTotals, infinite, isInfinite, megabyte, nanoid, percentage, reducers, regex, shareUrls, urlRoot };
package/dist/index.d.ts CHANGED
@@ -313,6 +313,44 @@ const getPlanFeature = ( plan, key ) => {
313
313
 
314
314
  };
315
315
 
316
+ // Centralized $inc helper for the per-org usage doc. Replaces inline
317
+ // $inc-with-bypass-validation patterns in drawbridge-api + drawbridge-sync.
318
+ //
319
+ // MongoDB's $inc with 0 / NaN / undefined / null writes the value into the
320
+ // field (zeroing or NaN-ing it) instead of no-op'ing — so we filter the
321
+ // $inc object to keep only finite, non-zero values and callers don't have
322
+ // to remember the guard.
323
+ //
324
+ // `session` is forwarded when present so callers inside a transaction get
325
+ // atomic writes; standalone callers omit it.
326
+ const incrementUsageTotals = async ({ controller, usageId, $inc, session }) => {
327
+
328
+ if( ! usageId || ! $inc ) return;
329
+
330
+ const safeInc = Object.fromEntries(
331
+ Object.entries( $inc ).filter( ([ , value ]) => {
332
+
333
+ const number = Number( value );
334
+
335
+ return Number.isFinite( number ) && number !== 0;
336
+
337
+ })
338
+ );
339
+
340
+ if( Object.keys( safeInc ).length === 0 ) return;
341
+
342
+ return controller.update({
343
+ collection : 'usage',
344
+ data : { $inc : safeInc },
345
+ options : {
346
+ bypassDocumentValidation : true,
347
+ ...( session && { session })
348
+ },
349
+ query : { id : usageId }
350
+ });
351
+
352
+ };
353
+
316
354
  const percentage = ( value1, value2, decimals = 1 ) => ( ( ( ( value1 || 0 ) / ( value2 || 0 ) ) * 100 ) || 0 ).toFixed( decimals );
317
355
 
318
356
  const reducers = {
@@ -411,4 +449,4 @@ const currencies = data.map( ( item ) => ({
411
449
 
412
450
  const currency = ( val ) => code( val );
413
451
 
414
- export { bytesToGB, bytesToMB, capitalize, constants, currencies, currency, expiredPaymentMethod, formatCurrency, formatDateString, formatNumber, getPlanFeature, gigabyte, infinite, isInfinite, megabyte, nanoid, percentage, reducers, regex, shareUrls, urlRoot };
452
+ export { bytesToGB, bytesToMB, capitalize, constants, currencies, currency, expiredPaymentMethod, formatCurrency, formatDateString, formatNumber, getPlanFeature, gigabyte, incrementUsageTotals, infinite, isInfinite, megabyte, nanoid, percentage, reducers, regex, shareUrls, urlRoot };
package/dist/index.js CHANGED
@@ -266,6 +266,25 @@ var getPlanFeature = (plan, key) => {
266
266
  message: granted ? feature : error
267
267
  };
268
268
  };
269
+ var incrementUsageTotals = async ({ controller, usageId, $inc, session }) => {
270
+ if (!usageId || !$inc) return;
271
+ const safeInc = Object.fromEntries(
272
+ Object.entries($inc).filter(([, value]) => {
273
+ const number = Number(value);
274
+ return Number.isFinite(number) && number !== 0;
275
+ })
276
+ );
277
+ if (Object.keys(safeInc).length === 0) return;
278
+ return controller.update({
279
+ collection: "usage",
280
+ data: { $inc: safeInc },
281
+ options: {
282
+ bypassDocumentValidation: true,
283
+ ...session && { session }
284
+ },
285
+ query: { id: usageId }
286
+ });
287
+ };
269
288
  var percentage = (value1, value2, decimals = 1) => ((value1 || 0) / (value2 || 0) * 100 || 0).toFixed(decimals);
270
289
  var reducers = {
271
290
  fields: (data2 = [], callback = () => ({})) => data2.reduce(
@@ -356,6 +375,7 @@ export {
356
375
  formatNumber,
357
376
  getPlanFeature,
358
377
  gigabyte,
378
+ incrementUsageTotals,
359
379
  infinite,
360
380
  isInfinite,
361
381
  megabyte,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "dependencies": {
4
- "@drawbridge/drawbridge-agents": "0.0.9",
4
+ "@drawbridge/drawbridge-agents": "0.0.10",
5
5
  "axios": "1.16.0",
6
6
  "currency-codes": "2.2.0",
7
7
  "nanoid": "3.3.8",
@@ -92,5 +92,5 @@
92
92
  "build": "tsup && npm publish"
93
93
  },
94
94
  "types": "dist/index.d.ts",
95
- "version": "0.0.26"
95
+ "version": "0.0.27"
96
96
  }