mirror of
https://github.com/marcusDenslow/terminalShop.git
synced 2026-10-25 10:02:21 +01:00
* feat(sca): expire inactive saved cards * fix(sca): address card ttl review feedback - Correct sql typos breaking GET /cards and /view/init (used_id, id_default) - Stop swallowing backfillCardStorageExpiration error in Migrate - Wrap expireStoredCard local writes in a transaction and run stripe detach + audit only after commit, preventing double-fire on retry - Drop redundant ttl refresh in ConvertCart; webhook is authoritative - Rename validation_expired_at metric label to validation_card_expired - Fix CardStorageExpiresAt doc comment and test failure message typos - Add handler-level tests for expireStoredCards (user scoping + cart deref) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * refactor(sca): move card expiration to periodic reconciler Read paths (GetCards, GetViewInit) now filter expired cards from query results instead of detaching them inline, so request handlers no longer block on Stripe API calls. Physical deletion runs in a new ReconcileExpiredCards goroutine on an hourly ticker, mirroring the existing ReconcileOrders / ReconcileUnshipped jobs. Per-card action sites (SetCard, SetDefaultCard, ConvertCart) still expire synchronously since they operate on one row the user is acting on. ViewHandler's stripeKey dependency is dropped now that GetViewInit no longer touches Stripe. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(sca): address card ttl review feedback - guard zero order.CardID before refreshing card storage ttl in payment_intent.succeeded webhook to avoid noisy warn logs on payment paths that did not use a saved card - delete unused Card.RefreshStorageTTL model method; the SQL-only refreshCardStorageTTL helper is the single canonical path - clarify duplicate-card webhook path: storage_expires_at reset is a card-add refresh, not a payment-success refresh, so last_used_at intentionally stays untouched Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * test(sca): cover card ttl read paths and expired-card sync sites --------- Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"terminalShop/pkg/database"
|
|
"terminalShop/pkg/models"
|
|
)
|
|
|
|
// TestGetViewInit_FiltersExpiredCardsAndOrders drives the WHERE + ORDER BY
|
|
// clauses in GetViewInit. a column-name typo in either clause (e.g.
|
|
// id_default instead of is_default) surfaces here as a sqlite error
|
|
func TestGetViewInit_FiltersExpiredCardsAndOrders(t *testing.T) {
|
|
testDB, user := setupCardsExpireTestDB(t)
|
|
defer func() { _ = os.Remove(testDB) }()
|
|
defer database.ResetForTesting()
|
|
|
|
db := database.GetDB()
|
|
now := time.Now()
|
|
past := now.Add(-1 * time.Hour)
|
|
future := now.Add((24 * time.Hour))
|
|
|
|
expired := models.Card{
|
|
UserID: user.ID, StripePaymentID: "pm_expired", Last4: "1111",
|
|
Brand: "Visa", ExpMonth: 1, ExpYear: 2030, StorageExpiresAt: &past,
|
|
}
|
|
freshDefault := models.Card{
|
|
UserID: user.ID, StripePaymentID: "pm_fresh_d", Last4: "2222",
|
|
Brand: "Visa", ExpMonth: 1, ExpYear: 2030, StorageExpiresAt: &future,
|
|
IsDefault: true,
|
|
}
|
|
freshOther := models.Card{
|
|
UserID: user.ID, StripePaymentID: "pm_fresh_o", Last4: "3333",
|
|
Brand: "Visa", ExpMonth: 1, ExpYear: 2030, StorageExpiresAt: &future,
|
|
}
|
|
for _, card := range []*models.Card{&expired, &freshDefault, &freshOther} {
|
|
if err := db.Create(card).Error; err != nil {
|
|
t.Fatalf("seed card: %v", err)
|
|
}
|
|
}
|
|
|
|
handler := NewViewHandler()
|
|
req := authRequest("GET", "/api/v1/view/init", nil, user.ID)
|
|
w := httptest.NewRecorder()
|
|
handler.GetViewInit(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetViewInit status %d, body: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var resp struct {
|
|
Data struct {
|
|
Cards []models.Card `json:"cards"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if len(resp.Data.Cards) != 2 {
|
|
t.Fatalf("want 2 visible cards, got %d", len(resp.Data.Cards))
|
|
}
|
|
if resp.Data.Cards[0].ID != freshDefault.ID {
|
|
t.Fatalf("default card should sort first, got id=%d (want %d)", resp.Data.Cards[0].ID, freshDefault.ID)
|
|
}
|
|
}
|