@blamejs/blamejs-shop 0.4.61 → 0.4.62
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/CHANGELOG.md +2 -0
- package/lib/asset-manifest.json +1 -1
- package/lib/operator-approvals.js +16 -2
- package/lib/payment-retries.js +35 -0
- package/lib/plan-changes.js +21 -2
- package/lib/seller-signup.js +37 -6
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,8 @@ upgrading across more than a few patches at a time.
|
|
|
8
8
|
|
|
9
9
|
## v0.4.x
|
|
10
10
|
|
|
11
|
+
- v0.4.62 (2026-06-19) — **Money and approval actions can't double-fire under concurrent calls.** Closes a set of concurrency races in money- and approval-critical paths where the action read a row, checked its status in application code, and then performed an irreversible side-effect (retry a card, execute an approved action, register a vendor, bill a subscription change) before an unconditional update committed — so two concurrent calls could both pass the check and both perform the side-effect. Each now claims the transition with a single conditional update and performs the side-effect only when that claim wins. A scheduled payment retry is claimed out of the due set before the processor is called, so an overlapping scheduler tick can't retry the same card twice; a payment outcome is claimed by its attempt number before the attempt is recorded, so a redelivered webhook can't double-count it. Executing an approved operator request claims the approved-to-executed transition before returning success, so the underlying money move runs once. Approving a seller application claims the in-review-to-approved transition before registering the vendor (rolling back to in-review if registration fails), so one application can't create two vendors. Applying scheduled subscription plan changes claims each pending change before updating the plan and recording its proration invoice, and stamps a deterministic idempotency key on the invoice, so an overlapping or retried scheduler run can't bill the proration twice. Behavior for a single sequential caller is unchanged. **Fixed:** *Scheduled payment retries and recorded outcomes claim before acting* — The retry scheduler now claims each due enrollment (nulling its next-retry time, conditional on the value the tick observed) before composing the processor retry, so two overlapping ticks can't both retry the same card for one scheduled slot — only the claim winner calls the processor. Recording a retry outcome claims the enrollment by bumping its attempt count conditionally on the observed value before writing the attempt row, so a redelivered webhook (or a manual record racing it) can't write a duplicate attempt or double-count toward the cap; the loser returns the already-advanced row. · *Executing an approved request runs the action once* — markExecuted now flips the request from approved to executed with a conditional update (WHERE id = ? AND status = 'approved') and returns success only when that update changes one row; a concurrent second call is refused with a typed error before the application runs the underlying action (a large refund, vendor payout, or bulk operation) a second time. · *Approving a seller application creates one vendor* — approveApplication now claims the in-review-to-approved transition first and registers the vendor only when the claim wins, so two concurrent approvals can't both register a vendor for one application. If vendor registration fails after the claim, the application is rolled back to in-review so it can be approved again rather than stranded as approved with no vendor. · *Applying scheduled plan changes bills the proration once* — applyScheduledChanges now claims each pending change (WHERE id = ? AND status = 'pending') before updating the subscription plan and recording the proration invoice, so an overlapping or retried scheduler run skips an already-applied change instead of billing it twice. Both the scheduled and the immediate plan-change paths now stamp a deterministic idempotency key on the proration invoice, so even a re-run that reached the billing call would dedupe against the unique invoice id rather than write a second charge. A regression test asserts a second apply pass records no additional invoice.
|
|
12
|
+
|
|
11
13
|
- v0.4.61 (2026-06-19) — **Scheduled and dispatched sends no longer double-fire under concurrent ticks.** Closes a class of concurrency races in the scheduled-send and dispatch paths where a tick read a row, checked its status or schedule cursor in application code, and then performed the send (and its ledger write) before an unconditional update committed — so two overlapping ticks (a cron overrun, two workers, or a manual tick racing the schedule) could both pass the check and both send. Each path now claims the row with a single conditional update — flipping the status, or advancing the schedule cursor, only when the row is still in the expected state — and performs the send only when that claim wins; the losing tick changes zero rows and skips. The affected sends: an email campaign drain (the whole audience could be mailed twice), a queued push notification, a wishlist digest email and its sent-ledger row, a reorder reminder, a survey response record, and a dunning step's reminder email / subscription action. Behavior for a single (sequential) caller is unchanged; only the concurrent double-fire is removed. Where a send fails after the claim wins, the schedule cursor is rolled back so the row is retried on the next tick rather than skipped. **Fixed:** *Email campaign sends claim the campaign before draining* — sendNow, the scheduled dispatch tick, and the consent-gated broadcast drain now flip the campaign from scheduled to sending with a conditional update (WHERE slug = ? AND status = 'scheduled') and drain only when that update changes exactly one row. Previously the status check ran in application code before an unconditional flip, so two concurrent sends could both drain and mail the entire audience twice. The losing caller refuses (a typed send-race error) or, for the tick, skips the campaign. · *Push, wishlist-digest, and reorder-reminder ticks claim each row before sending* — Each per-row advance is now the atomic claim: a queued push notification advances to sent/failed only via WHERE id = ? AND status = 'queued'; a wishlist digest and a reorder reminder advance their schedule cursor with WHERE id = ? AND next_*_at = <the observed value>. The external send (and, for the digest, its sent-ledger row) runs only when the claim changes one row, so two overlapping ticks can't both dispatch the same row. A send that fails after winning the reminder claim rolls the cursor back so the row retries next tick. · *Survey responses and dunning steps gate their side-effect on an atomic claim* — Submitting a survey response now flips the invitation from issued to responded with WHERE id = ? AND status = 'issued' and inserts the response row only when that claim wins (a lost race returns a typed already-responded error), so a double-submit can't record two responses for one invitation. A dunning tick now claims the enrollment with WHERE id = ? AND status = 'active' AND next_action_at = <the observed value> before executing the step (the reminder email / in-app notification / pause or cancel), so overlapping dunning ticks can't send a step twice; the subsequent state transitions carry the same status guard.
|
|
12
14
|
|
|
13
15
|
- v0.4.60 (2026-06-19) — **Free-shipping discount rules now actually waive the shipping charge.** Fixes a checkout defect that made a free_shipping auto-discount rule a complete no-op on the amount charged: the discount engine correctly flagged the order as free-shipping-eligible, but checkout dropped that flag and still billed the customer the full selected shipping rate. A matched free_shipping rule now zeroes the charged shipping in the quote and the confirmed order (the selected rate is still shown so the customer sees what was waived), so the payment is for merchandise and tax only. The rule is also now reserved against its redemption like every other auto-discount, so a free_shipping rule with a usage cap is enforced instead of applying without limit. Subtotal discounts are unaffected — free shipping reduces the shipping line, not the subtotal, and tax (computed on the post-discount subtotal) is unchanged. **Fixed:** *free_shipping auto-discount waives the charged shipping* — A matching free_shipping rule was flagged by the auto-discount engine but never applied to the amount charged — checkout excluded it from the discount (correct, since free shipping is not a subtotal reduction) but then charged the full shipping rate, so the rule saved the customer nothing. The quote and the confirmed order now set the charged shipping to zero when a free_shipping rule matches; the selected shipping service and its rate are still surfaced so the storefront can show the waived amount. The free_shipping rule is included in the order's applied auto-discounts and reserved against its redemption pre-charge, so a capped free-shipping rule is enforced and recorded the same way a percentage or amount-off rule is. A checkout integration test now asserts the charged total, the confirmed order total, and the payment amount all exclude shipping when free shipping applies.
|
package/lib/asset-manifest.json
CHANGED
|
@@ -724,11 +724,25 @@ function create(opts) {
|
|
|
724
724
|
}
|
|
725
725
|
|
|
726
726
|
var ts = _monotonicTs();
|
|
727
|
-
|
|
727
|
+
// Atomic claim: gate the approved->executed transition on the row
|
|
728
|
+
// still being 'approved'. The JS check above only refuses a
|
|
729
|
+
// sequential re-execute; two concurrent markExecuted calls would
|
|
730
|
+
// both read 'approved', both pass that check, and both return
|
|
731
|
+
// success — so the application would run the underlying money move
|
|
732
|
+
// (a large refund / vendor payout / bulk catalog action) TWICE. The
|
|
733
|
+
// `AND status = 'approved'` predicate lets exactly one caller win
|
|
734
|
+
// the transition; the loser is refused before its side-effect runs.
|
|
735
|
+
var claim = await query(
|
|
728
736
|
"UPDATE approval_requests SET status = 'executed', executed_by = ?1, " +
|
|
729
|
-
"executed_at = ?2, result_json = ?3, updated_at = ?2 WHERE id = ?4",
|
|
737
|
+
"executed_at = ?2, result_json = ?3, updated_at = ?2 WHERE id = ?4 AND status = 'approved'",
|
|
730
738
|
[executedBy, ts, resultJson, requestId],
|
|
731
739
|
);
|
|
740
|
+
if (Number(claim.rowCount || 0) !== 1) {
|
|
741
|
+
var raced = new Error("operatorApprovals.markExecuted: request " +
|
|
742
|
+
JSON.stringify(requestId) + " is no longer approved — execution refused");
|
|
743
|
+
raced.code = "APPROVAL_EXECUTE_RACE";
|
|
744
|
+
throw raced;
|
|
745
|
+
}
|
|
732
746
|
|
|
733
747
|
await _audit("approval.execute", executedBy, requestId,
|
|
734
748
|
{ status: req.status },
|
package/lib/payment-retries.js
CHANGED
|
@@ -587,6 +587,23 @@ function create(opts) {
|
|
|
587
587
|
var advanced = [];
|
|
588
588
|
for (var i = 0; i < due.rows.length; i += 1) {
|
|
589
589
|
var enrollment = due.rows[i];
|
|
590
|
+
// Atomic claim: take this enrollment out of the due set (null its
|
|
591
|
+
// next_retry_at) gated on the value THIS tick observed, BEFORE any
|
|
592
|
+
// processor work. Two overlapping ticks (cron overrun / two
|
|
593
|
+
// workers / a manual tick racing the cron) both snapshot the same
|
|
594
|
+
// active+due row; only the one whose conditional UPDATE matches
|
|
595
|
+
// one row proceeds to _tickAdvance, which composes
|
|
596
|
+
// paymentHandle.retryIntent (the real card-charge retry). The
|
|
597
|
+
// loser changes zero rows and skips, so the customer's card is
|
|
598
|
+
// retried exactly once per scheduled slot. _tickAdvance re-stamps
|
|
599
|
+
// next_retry_at to the true next slot (or a terminal NULL).
|
|
600
|
+
var claim = await query(
|
|
601
|
+
"UPDATE payment_retry_enrollments SET next_retry_at = NULL " +
|
|
602
|
+
"WHERE id = ?1 AND status = 'active' AND next_retry_at = ?2",
|
|
603
|
+
[enrollment.id, enrollment.next_retry_at],
|
|
604
|
+
);
|
|
605
|
+
if (Number(claim.rowCount || 0) !== 1) continue; // another tick claimed it
|
|
606
|
+
|
|
590
607
|
var policy = await _getPolicy(enrollment.policy_slug);
|
|
591
608
|
if (!policy) {
|
|
592
609
|
// Policy went missing between enrollment + tick — close
|
|
@@ -645,6 +662,24 @@ function create(opts) {
|
|
|
645
662
|
var attemptCount = Number(enrollment.attempt_count);
|
|
646
663
|
var nextAttempt = attemptCount + 1;
|
|
647
664
|
|
|
665
|
+
// Atomic claim: bump attempt_count gated on the value we observed,
|
|
666
|
+
// BEFORE writing the attempt ledger row. The status check above
|
|
667
|
+
// only refuses a SEQUENTIAL re-record once the row is terminal;
|
|
668
|
+
// two concurrent recordRetryOutcome calls while still active (a
|
|
669
|
+
// webhook redelivery, or a manual record racing the webhook) would
|
|
670
|
+
// both read attempt_count=N, both write a duplicate attempt row,
|
|
671
|
+
// and both double-count toward the cap. Only the caller whose
|
|
672
|
+
// UPDATE matches one row proceeds; a loser returns the
|
|
673
|
+
// already-advanced row idempotently.
|
|
674
|
+
var outcomeClaim = await query(
|
|
675
|
+
"UPDATE payment_retry_enrollments SET attempt_count = ?1 " +
|
|
676
|
+
"WHERE id = ?2 AND status = 'active' AND attempt_count = ?3",
|
|
677
|
+
[nextAttempt, enrollment.id, attemptCount],
|
|
678
|
+
);
|
|
679
|
+
if (Number(outcomeClaim.rowCount || 0) !== 1) {
|
|
680
|
+
return await _refetchEnrollment(enrollment.id);
|
|
681
|
+
}
|
|
682
|
+
|
|
648
683
|
await _writeAttempt(enrollment.id, nextAttempt, succeeded, succeeded ? null : newFailureCode);
|
|
649
684
|
|
|
650
685
|
if (succeeded) {
|
package/lib/plan-changes.js
CHANGED
|
@@ -382,6 +382,10 @@ function create(opts) {
|
|
|
382
382
|
period_end: sub.current_period_end,
|
|
383
383
|
amount_minor: net,
|
|
384
384
|
currency: proposed.currency.toUpperCase(),
|
|
385
|
+
// Deterministic idempotency key keyed on this change row so
|
|
386
|
+
// a re-applied change dedupes against the UNIQUE
|
|
387
|
+
// processor_invoice_id instead of double-billing.
|
|
388
|
+
processor_invoice_id: "planchange:" + id,
|
|
385
389
|
});
|
|
386
390
|
} catch (_e) {
|
|
387
391
|
// Drop-silent — by design. The billing handle is an
|
|
@@ -468,10 +472,21 @@ function create(opts) {
|
|
|
468
472
|
var executed = [];
|
|
469
473
|
for (var i = 0; i < due.rows.length; i += 1) {
|
|
470
474
|
var row = due.rows[i];
|
|
471
|
-
|
|
472
|
-
|
|
475
|
+
// Atomic claim: flip pending -> executed gated on the status.
|
|
476
|
+
// Two overlapping scheduler runs (cron overrun, retry, two
|
|
477
|
+
// workers) both SELECT the same due row; WITHOUT this guard both
|
|
478
|
+
// would update the subscription plan AND record a proration
|
|
479
|
+
// invoice — billing the customer twice for one change. Only the
|
|
480
|
+
// caller whose UPDATE matches one row proceeds; the loser skips.
|
|
481
|
+
// (The synchronous executeChange path is already protected by its
|
|
482
|
+
// plan_id claim; this is the batch path's equivalent.)
|
|
483
|
+
var claim = await query(
|
|
484
|
+
"UPDATE subscription_plan_changes SET status = 'executed', executed_at = ?1 " +
|
|
485
|
+
"WHERE id = ?2 AND status = 'pending'",
|
|
473
486
|
[now, row.id],
|
|
474
487
|
);
|
|
488
|
+
if (Number(claim.rowCount || 0) !== 1) continue; // another run claimed it
|
|
489
|
+
|
|
475
490
|
await query(
|
|
476
491
|
"UPDATE subscriptions SET plan_id = ?1, updated_at = ?2 WHERE id = ?3",
|
|
477
492
|
[row.to_plan_id, now, row.subscription_id],
|
|
@@ -492,6 +507,10 @@ function create(opts) {
|
|
|
492
507
|
period_end: periodEnd,
|
|
493
508
|
amount_minor: net,
|
|
494
509
|
currency: String(row.currency).toUpperCase(),
|
|
510
|
+
// Deterministic idempotency key — this change applies once,
|
|
511
|
+
// so a re-run dedupes against the UNIQUE processor_invoice_id
|
|
512
|
+
// rather than writing a second proration invoice.
|
|
513
|
+
processor_invoice_id: "planchange:" + row.id,
|
|
495
514
|
});
|
|
496
515
|
} catch (_e) {
|
|
497
516
|
// Drop-silent — see executeChange.
|
package/lib/seller-signup.js
CHANGED
|
@@ -842,6 +842,29 @@ function create(opts) {
|
|
|
842
842
|
throw notReady;
|
|
843
843
|
}
|
|
844
844
|
|
|
845
|
+
// Atomic claim FIRST: flip in_review -> approved gated on the
|
|
846
|
+
// status. The JS checks above only refuse a sequential re-approve;
|
|
847
|
+
// two concurrent approvals would both read 'in_review', both pass,
|
|
848
|
+
// and both call registerVendor — which mints a fresh vendor row
|
|
849
|
+
// each time, leaving a DUPLICATE vendor for one application. The
|
|
850
|
+
// `AND status = 'in_review'` predicate lets exactly one caller win;
|
|
851
|
+
// the loser is refused before registering anything. vendor_slug is
|
|
852
|
+
// stamped in a second UPDATE after the (winner-only) registration.
|
|
853
|
+
var ts = _now();
|
|
854
|
+
var claim = await query(
|
|
855
|
+
"UPDATE seller_applications " +
|
|
856
|
+
"SET status = 'approved', approved_at = ?1, approved_by = ?2, updated_at = ?1 " +
|
|
857
|
+
"WHERE id = ?3 AND status = 'in_review'",
|
|
858
|
+
[ts, approvedBy, applicationId],
|
|
859
|
+
);
|
|
860
|
+
if (Number(claim.rowCount || 0) !== 1) {
|
|
861
|
+
var raced = new Error(
|
|
862
|
+
"seller-signup.approveApplication: refused — application is no longer in_review (concurrent approval)"
|
|
863
|
+
);
|
|
864
|
+
raced.code = "APPLICATION_NOT_READY";
|
|
865
|
+
throw raced;
|
|
866
|
+
}
|
|
867
|
+
|
|
845
868
|
// Compose vendors.registerVendor — the operator-facing v1
|
|
846
869
|
// defaults a fresh marketplace seller to bank_transfer payout +
|
|
847
870
|
// 70% commission split; the operator tunes both via
|
|
@@ -860,6 +883,17 @@ function create(opts) {
|
|
|
860
883
|
status: "active",
|
|
861
884
|
});
|
|
862
885
|
} catch (e) {
|
|
886
|
+
// Roll the claim back so the application returns to in_review and
|
|
887
|
+
// can be re-approved — the winning claim must not strand the app
|
|
888
|
+
// in 'approved' with no vendor behind it.
|
|
889
|
+
try {
|
|
890
|
+
await query(
|
|
891
|
+
"UPDATE seller_applications " +
|
|
892
|
+
"SET status = 'in_review', approved_at = NULL, approved_by = NULL, updated_at = ?1 " +
|
|
893
|
+
"WHERE id = ?2 AND status = 'approved'",
|
|
894
|
+
[_now(), applicationId],
|
|
895
|
+
);
|
|
896
|
+
} catch (_e2) { /* drop-silent — the registerVendor error is the caller's signal */ }
|
|
863
897
|
// Surface the vendors error with our typed-code wrapper so
|
|
864
898
|
// operators can distinguish a vendors-layer refusal from an
|
|
865
899
|
// application-layer refusal.
|
|
@@ -872,13 +906,10 @@ function create(opts) {
|
|
|
872
906
|
throw wrapped;
|
|
873
907
|
}
|
|
874
908
|
|
|
875
|
-
|
|
909
|
+
// Stamp the registered vendor slug onto the now-approved application.
|
|
876
910
|
await query(
|
|
877
|
-
"UPDATE seller_applications "
|
|
878
|
-
|
|
879
|
-
" vendor_slug = ?3, updated_at = ?1 " +
|
|
880
|
-
"WHERE id = ?4",
|
|
881
|
-
[ts, approvedBy, vendor.slug, applicationId],
|
|
911
|
+
"UPDATE seller_applications SET vendor_slug = ?1, updated_at = ?2 WHERE id = ?3",
|
|
912
|
+
[vendor.slug, _now(), applicationId],
|
|
882
913
|
);
|
|
883
914
|
|
|
884
915
|
return {
|
package/package.json
CHANGED