Add giftcard management
This commit is contained in:
@@ -29,10 +29,19 @@ type GiftCard struct {
|
||||
RedeemedBy *string `json:"redeemed_by,omitempty"`
|
||||
}
|
||||
|
||||
type UserBalance struct {
|
||||
UserID string `json:"user_id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Balance float64 `json:"balance"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type GiftCardSummary struct {
|
||||
TotalUnclaimed float64 `json:"total_unclaimed"`
|
||||
TotalUserBalances float64 `json:"total_user_balances"`
|
||||
GiftCards []GiftCard `json:"gift_cards"`
|
||||
TotalUnclaimed float64 `json:"total_unclaimed"`
|
||||
TotalUserBalances float64 `json:"total_user_balances"`
|
||||
GiftCards []GiftCard `json:"gift_cards"`
|
||||
UserBalances []UserBalance `json:"user_balances"`
|
||||
}
|
||||
|
||||
type CreateGiftCardRequest struct {
|
||||
@@ -136,6 +145,37 @@ func GetGiftCards(w http.ResponseWriter, r *http.Request) {
|
||||
summary.GiftCards = append(summary.GiftCards, gc)
|
||||
}
|
||||
|
||||
summary.UserBalances = []UserBalance{}
|
||||
ubRows, err := db.DB.Query(ctx, `
|
||||
SELECT b.user_id, u.n_first_name || ' ' || u.n_last_name AS name, u.email, b.balance, b.updated_at
|
||||
FROM user_giftcard_balances b
|
||||
JOIN users u ON b.user_id = u.id
|
||||
ORDER BY b.updated_at DESC
|
||||
`)
|
||||
if err != nil {
|
||||
log.Printf("Failed to query user balances: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer ubRows.Close()
|
||||
|
||||
for ubRows.Next() {
|
||||
var ub UserBalance
|
||||
err = ubRows.Scan(
|
||||
&ub.UserID,
|
||||
&ub.Name,
|
||||
&ub.Email,
|
||||
&ub.Balance,
|
||||
&ub.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("Failed to scan user balance: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
summary.UserBalances = append(summary.UserBalances, ub)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(summary)
|
||||
}
|
||||
@@ -155,8 +195,16 @@ func CreateGiftCard(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := db.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Failed to begin transaction: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
var gc GiftCard
|
||||
err := db.DB.QueryRow(ctx, `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO gift_cards (total_funds_added, amount_remaining, created_by)
|
||||
VALUES ($1, $1, $2)
|
||||
RETURNING id, total_funds_added, amount_remaining, created_by, created_at
|
||||
@@ -173,6 +221,23 @@ func CreateGiftCard(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Create an 'on_the_house' payment record for financial tracking
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO payments (payment_type, payment_method, status, amount, created_by, created_at, updated_at)
|
||||
VALUES ('full', 'on_the_house', 'completed', $1, $2, NOW(), NOW())
|
||||
`, req.Amount, adminID)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create payment record for gift card: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
log.Printf("Failed to commit transaction: %v", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(gc)
|
||||
|
||||
Reference in New Issue
Block a user