@qazuor/qzpay-drizzle 1.3.0 → 1.5.0

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
@@ -1078,7 +1078,7 @@ function mapDrizzleSubscriptionToCore(drizzle3) {
1078
1078
  };
1079
1079
  }
1080
1080
  function mapCoreSubscriptionCreateToDrizzle(input, defaults) {
1081
- return {
1081
+ const insert = {
1082
1082
  id: input.id,
1083
1083
  customerId: input.customerId,
1084
1084
  planId: input.planId,
@@ -1093,6 +1093,17 @@ function mapCoreSubscriptionCreateToDrizzle(input, defaults) {
1093
1093
  metadata: input.metadata ?? {},
1094
1094
  livemode: defaults.livemode
1095
1095
  };
1096
+ if (input.providerSubscriptionIds) {
1097
+ const stripeId = input.providerSubscriptionIds["stripe"];
1098
+ const mpId = input.providerSubscriptionIds["mercadopago"];
1099
+ if (stripeId !== void 0) {
1100
+ insert.stripeSubscriptionId = stripeId;
1101
+ }
1102
+ if (mpId !== void 0) {
1103
+ insert.mpSubscriptionId = mpId;
1104
+ }
1105
+ }
1106
+ return insert;
1096
1107
  }
1097
1108
  function mapCoreSubscriptionUpdateToDrizzle(input) {
1098
1109
  const update = {};
@@ -1120,6 +1131,16 @@ function mapCoreSubscriptionUpdateToDrizzle(input) {
1120
1131
  if (input.trialEnd !== void 0) {
1121
1132
  update.trialEnd = input.trialEnd;
1122
1133
  }
1134
+ if (input.providerSubscriptionIds) {
1135
+ const stripeId = input.providerSubscriptionIds["stripe"];
1136
+ const mpId = input.providerSubscriptionIds["mercadopago"];
1137
+ if (stripeId !== void 0) {
1138
+ update.stripeSubscriptionId = stripeId;
1139
+ }
1140
+ if (mpId !== void 0) {
1141
+ update.mpSubscriptionId = mpId;
1142
+ }
1143
+ }
1123
1144
  return update;
1124
1145
  }
1125
1146
 
@@ -4386,6 +4407,28 @@ var QZPayPromoCodesRepository = class {
4386
4407
  }).where(drizzleOrm.eq(billingPromoCodes.id, id)).returning();
4387
4408
  return firstOrThrow(result, "PromoCode", id);
4388
4409
  }
4410
+ /**
4411
+ * Atomically increment the usage count if (and only if) the promo
4412
+ * code has not reached its `maxUses` limit yet.
4413
+ *
4414
+ * Implemented as a single conditional `UPDATE ... WHERE used_count <
4415
+ * max_uses RETURNING *` statement so it is race-safe under
4416
+ * concurrency. Returns `null` when the increment would exceed the
4417
+ * limit; callers should treat that as "redemption limit reached".
4418
+ *
4419
+ * Promo codes with `maxUses = null` (no limit) always succeed.
4420
+ */
4421
+ async atomicIncrementUsage(id) {
4422
+ const result = await this.db.update(billingPromoCodes).set({
4423
+ usedCount: drizzleOrm.sql`COALESCE(${billingPromoCodes.usedCount}, 0) + 1`
4424
+ }).where(
4425
+ drizzleOrm.and(
4426
+ drizzleOrm.eq(billingPromoCodes.id, id),
4427
+ drizzleOrm.or(drizzleOrm.isNull(billingPromoCodes.maxUses), drizzleOrm.sql`COALESCE(${billingPromoCodes.usedCount}, 0) < ${billingPromoCodes.maxUses}`)
4428
+ )
4429
+ ).returning();
4430
+ return firstOrNull(result);
4431
+ }
4389
4432
  /**
4390
4433
  * Deactivate promo code
4391
4434
  */
@@ -5951,6 +5994,10 @@ var QZPayDrizzleStorageAdapter = class {
5951
5994
  async incrementRedemptions(id) {
5952
5995
  await repo.incrementUsage(id);
5953
5996
  },
5997
+ async atomicIncrementRedemptions(id) {
5998
+ const result = await repo.atomicIncrementUsage(id);
5999
+ return result ? mapDrizzlePromoCodeToCore(result) : null;
6000
+ },
5954
6001
  async list(options) {
5955
6002
  const limit = options?.limit ?? 20;
5956
6003
  const offset = options?.offset ?? 0;