fix: fixed card and address list having duplicate items, and a checkout flow infinate loop condition

This commit is contained in:
2026-04-26 11:59:24 +02:00
parent e0ff8f5f76
commit 7bebdeebed
3 changed files with 15 additions and 1 deletions

View File

@@ -141,6 +141,12 @@ func (h *AddressHandler) CreateAddress(w http.ResponseWriter, r *http.Request) {
return
}
// remove existing addres with the same street addres
var existing models.Address
if err := db.Where("user_id = ? AND street = ?", userID, address.Street).First(&existing).Error; err == nil {
db.Delete(&existing)
}
if err := db.Create(&address).Error; err != nil {
utils.RespondError(w, http.StatusInternalServerError, "DATABASE_ERROR", "failed to create address", nil)
return

View File

@@ -248,6 +248,13 @@ func (h *CardHandler) SaveCard(w http.ResponseWriter, r *http.Request) {
return
}
// remove existing local card with the same last4 + brand + expiry
var existingCard models.Card
if err := db.Where("user_id = ? AND last4 = ? AND brand = ? AND exp_month = ? AND exp_year = ?",
user.ID, pm.Card.Last4, string(pm.Card.Brand), pm.Card.ExpMonth, pm.Card.ExpYear).First(&existingCard).Error; err == nil {
db.Delete(&existingCard)
}
// Persist only the safe card metadata — no raw card data is stored.
card := models.Card{
UserID: user.ID,