Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions backend/internal/admin/terminal_sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"time"

"github.com/shopspring/decimal"
jsonwrite "unicard-go/backend/internal/pkg/handler"
)

Expand Down Expand Up @@ -127,7 +128,9 @@ func (h *Handler) TerminalSimTransactionHandler(w http.ResponseWriter, r *http.R
}

serviceFee := req.Amount * (commissionRate / 100.0)
loyaltyPoints := req.Amount * 0.002 // 0.2% reward points

amountDec := decimal.NewFromFloat(req.Amount)
loyaltyPoints := amountDec.Mul(decimal.NewFromFloat(0.002))

// 3. Process Transaction (Start TX)
tx, err := h.DB.Begin()
Expand Down Expand Up @@ -178,9 +181,9 @@ func (h *Handler) TerminalSimTransactionHandler(w http.ResponseWriter, r *http.R
}

_, err = tx.Exec(`
INSERT INTO transactions (transaction_id, card_number, merchant_id, terminal_id, transaction_type, amount, service_fee, processed_by)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`, transactionID, req.CardNumber, req.MerchantID, terminalID, dbTransactionType, req.Amount, serviceFee, processedBy)
INSERT INTO transactions (transaction_id, card_number, merchant_id, terminal_id, transaction_type, amount, service_fee, processed_by, points_earned)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, transactionID, req.CardNumber, req.MerchantID, terminalID, dbTransactionType, req.Amount, serviceFee, processedBy, loyaltyPoints)

if err != nil {
// If `merchant_id` is not nullable and causes error or id doesn't auto-increment
Expand Down
8 changes: 5 additions & 3 deletions backend/internal/pkg/structs/struct.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package structure

import "github.com/shopspring/decimal"

// CardData struct represents the data required to create a new card
type CardData struct {
CardUID string `json:"card_uid" db:"card_uid" validate:"required"`
Expand Down Expand Up @@ -63,8 +65,8 @@ type DashboardUser struct {
UserID string `db:"user_id" json:"user_id,omitempty"`
Username string `db:"username" json:"username"`
Name string `db:"name" json:"name"`
Balance float64 `db:"balance" json:"balance"`
LoyaltyPoints int `db:"loyalty_points" json:"loyalty_points"`
AccountType string `db:"account_type" json:"account_type"`
Balance float64 `db:"balance" json:"balance"`
LoyaltyPoints decimal.Decimal `db:"loyalty_points" json:"loyalty_points"`
AccountType string `db:"account_type" json:"account_type"`
RecentTransactions []Transaction `json:"transactions"` // Add recent transactions to the dashboard response
}
76 changes: 38 additions & 38 deletions backend/internal/user/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,43 @@ import (
"net/http"
"strings"
"time"

"github.com/shopspring/decimal"
jsonwrite "unicard-go/backend/internal/pkg/handler"
)

type Transaction struct {
TransactionID string `json:"transaction_id"`
TerminalID string `json:"terminal_id"`
Date string `json:"date" db:"date"`
Time string `json:"time"`
Description string `json:"description" db:"description"`
Type string `json:"type" db:"transaction_type"`
Amount float64 `json:"amount" db:"transaction_amount"`
Status string `json:"status" db:"status"`
MerchantName string `json:"merchant_name"`
MerchantID string `json:"merchant_id"`
ServiceFee float64 `json:"service_fee"`
PointsEarned int `json:"points_earned"`
TransactionID string `json:"transaction_id"`
TerminalID string `json:"terminal_id"`
Date string `json:"date" db:"date"`
Time string `json:"time"`
Description string `json:"description" db:"description"`
Type string `json:"type" db:"transaction_type"`
Amount float64 `json:"amount" db:"transaction_amount"`
Status string `json:"status" db:"status"`
MerchantName string `json:"merchant_name"`
MerchantID string `json:"merchant_id"`
ServiceFee float64 `json:"service_fee"`
PointsEarned decimal.Decimal `json:"points_earned"`
}

// DashboardUser info struct for the user dashboard view
type DashboardUser struct {
ID int `json:"id,omitempty" db:"id"`
UserID string `json:"user_id,omitempty" db:"user_id"`
Username string `json:"username" db:"username"`
Name string `json:"name" db:"name"`
Email string `json:"email" db:"email"`
PendingEmail string `json:"pending_email"`
Phone string `json:"phone" db:"phone"`
Initials string `json:"initials"`
Balance float64 `json:"balance" db:"balance"`
LoyaltyPoints float64 `json:"loyalty_points" db:"loyalty_points"`
AccountType string `db:"account_type" json:"account_type"`
CardNumber string `json:"card_number"`
CardExpiry string `json:"card_expiry"`
CardStatus string `json:"card_status"`
RecentTransactions []Transaction `json:"recent_transactions"` // Add recent transactions to the dashboard response
ID int `json:"id,omitempty" db:"id"`
UserID string `json:"user_id,omitempty" db:"user_id"`
Username string `json:"username" db:"username"`
Name string `json:"name" db:"name"`
Email string `json:"email" db:"email"`
PendingEmail string `json:"pending_email"`
Phone string `json:"phone" db:"phone"`
Initials string `json:"initials"`
Balance float64 `json:"balance" db:"balance"`
LoyaltyPoints decimal.Decimal `json:"loyalty_points" db:"loyalty_points"`
AccountType string `db:"account_type" json:"account_type"`
CardNumber string `json:"card_number"`
CardExpiry string `json:"card_expiry"`
CardStatus string `json:"card_status"`
RecentTransactions []Transaction `json:"recent_transactions"` // Add recent transactions to the dashboard response
}

func (h *Handler) DashboardView(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -79,7 +81,7 @@ func (h *Handler) DashboardHandler(w http.ResponseWriter, r *http.Request) {
phone string
userType string
balance float64
loyaltyPoints float64
loyaltyPoints decimal.Decimal
cardNumber string
expiryDate string
cardStatus string
Expand Down Expand Up @@ -143,7 +145,7 @@ func (h *Handler) DashboardHandler(w http.ResponseWriter, r *http.Request) {

// Fetch recent transactions
txnQuery := `
SELECT t.transaction_id, t.description, t.created_at, t.transaction_type, t.amount, t.terminal_id, t.status, m.business_name, m.merchant_id
SELECT t.transaction_id, t.description, t.created_at, COALESCE(t.transaction_type, ''), t.amount, COALESCE(t.terminal_id, ''), COALESCE(t.status, ''), m.business_name, m.merchant_id, COALESCE(t.points_earned, 0)
FROM transactions t
JOIN cards c ON t.card_number = c.card_number
JOIN users u ON c.user_id = u.user_id
Expand All @@ -163,6 +165,7 @@ func (h *Handler) DashboardHandler(w http.ResponseWriter, r *http.Request) {
var description sql.NullString
var businessName sql.NullString
var merchantId sql.NullString
var pointsEarned decimal.Decimal
if err := rows.Scan(
&t.TransactionID,
&description,
Expand All @@ -173,6 +176,7 @@ func (h *Handler) DashboardHandler(w http.ResponseWriter, r *http.Request) {
&t.Status,
&businessName,
&merchantId,
&pointsEarned,
); err != nil {
fmt.Printf("Error scanning transaction row: %v\n", err)
continue
Expand All @@ -181,14 +185,14 @@ func (h *Handler) DashboardHandler(w http.ResponseWriter, r *http.Request) {
t.Time = formatTime(createdAt)
if description.Valid {
t.Description = description.String
} else {
t.Description = ""
}

if businessName.Valid {
if businessName.Valid && businessName.String != "" {
t.MerchantName = businessName.String
} else if description.Valid {
t.MerchantName = description.String
} else {
t.MerchantName = "System"
t.MerchantName = t.Description
}

if merchantId.Valid {
Expand All @@ -198,11 +202,7 @@ func (h *Handler) DashboardHandler(w http.ResponseWriter, r *http.Request) {
}

t.ServiceFee = 0.00
if strings.ToLower(t.Type) == "payment" && t.Amount >= 100 {
t.PointsEarned = int(t.Amount / 100)
} else {
t.PointsEarned = 0
}
t.PointsEarned = pointsEarned

transactions = append(transactions, t)
}
Expand Down
Loading