@qazuor/qzpay-drizzle 1.3.0 → 1.4.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
@@ -4386,6 +4386,28 @@ var QZPayPromoCodesRepository = class {
4386
4386
  }).where(drizzleOrm.eq(billingPromoCodes.id, id)).returning();
4387
4387
  return firstOrThrow(result, "PromoCode", id);
4388
4388
  }
4389
+ /**
4390
+ * Atomically increment the usage count if (and only if) the promo
4391
+ * code has not reached its `maxUses` limit yet.
4392
+ *
4393
+ * Implemented as a single conditional `UPDATE ... WHERE used_count <
4394
+ * max_uses RETURNING *` statement so it is race-safe under
4395
+ * concurrency. Returns `null` when the increment would exceed the
4396
+ * limit; callers should treat that as "redemption limit reached".
4397
+ *
4398
+ * Promo codes with `maxUses = null` (no limit) always succeed.
4399
+ */
4400
+ async atomicIncrementUsage(id) {
4401
+ const result = await this.db.update(billingPromoCodes).set({
4402
+ usedCount: drizzleOrm.sql`COALESCE(${billingPromoCodes.usedCount}, 0) + 1`
4403
+ }).where(
4404
+ drizzleOrm.and(
4405
+ drizzleOrm.eq(billingPromoCodes.id, id),
4406
+ drizzleOrm.or(drizzleOrm.isNull(billingPromoCodes.maxUses), drizzleOrm.sql`COALESCE(${billingPromoCodes.usedCount}, 0) < ${billingPromoCodes.maxUses}`)
4407
+ )
4408
+ ).returning();
4409
+ return firstOrNull(result);
4410
+ }
4389
4411
  /**
4390
4412
  * Deactivate promo code
4391
4413
  */
@@ -5951,6 +5973,10 @@ var QZPayDrizzleStorageAdapter = class {
5951
5973
  async incrementRedemptions(id) {
5952
5974
  await repo.incrementUsage(id);
5953
5975
  },
5976
+ async atomicIncrementRedemptions(id) {
5977
+ const result = await repo.atomicIncrementUsage(id);
5978
+ return result ? mapDrizzlePromoCodeToCore(result) : null;
5979
+ },
5954
5980
  async list(options) {
5955
5981
  const limit = options?.limit ?? 20;
5956
5982
  const offset = options?.offset ?? 0;