Download api/v1/fa/handler.go from Mrkiti/Mexa: direct link, hf CLI and curl.
- Browser
- Download file 4.49 kB
-
https://huggingface.co/spaces/Mrkiti/Mexa/resolve/main/api/v1/fa/handler.go
- Command line
-
hf download hf://spaces/Mrkiti/Mexa/api/v1/fa/handler.go
-
curl -L -o handler.go https://huggingface.co/spaces/Mrkiti/Mexa/resolve/main/api/v1/fa/handler.go
4.49 kB
| package fa | |
| import ( | |
| "net/http" | |
| "github.com/gin-gonic/gin" | |
| "mexa/api/v1/db" | |
| "mexa/middleware" | |
| ) | |
| // fa/handler.go β mpesa_transactions (migration 012), commissions (migration 050), events (migration 048) | |
| // RegisterRoutes β mounts all FA routes β read only throughout | |
| func RegisterRoutes(rg *gin.RouterGroup) { | |
| f := rg.Group("/fa", | |
| middleware.RequireAuth(), | |
| middleware.RequireRole("FA"), | |
| ) | |
| { | |
| f.GET("/payouts", Payouts) | |
| f.GET("/commissions", Commissions) | |
| f.GET("/reconciliation", Reconciliation) | |
| f.GET("/reports", Reports) | |
| } | |
| } | |
| // Payouts β GET /api/v1/fa/payouts β read only | |
| // mpesa_transactions β migration 012 | |
| // status CHECK = PENDING|SUCCESS|FAILED|TIMEOUT (uppercase per migration 012) | |
| func Payouts(c *gin.Context) { | |
| ctx := c.Request.Context() | |
| rows, err := db.DB.QueryContext(ctx, ` | |
| SELECT id, event_id, mymeza_id, amount_kes, status, created_at | |
| FROM mpesa_transactions | |
| ORDER BY created_at DESC | |
| LIMIT 100 | |
| `) | |
| if err != nil { | |
| c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load payouts"}) | |
| return | |
| } | |
| defer rows.Close() | |
| type Row struct { | |
| ID string `json:"id"` | |
| EventID string `json:"event_id"` | |
| MyMezaID string `json:"mymeza_id"` | |
| AmountKES float64 `json:"amount_kes"` | |
| Status string `json:"status"` | |
| CreatedAt string `json:"created_at"` | |
| } | |
| results := []Row{} | |
| for rows.Next() { | |
| var r Row | |
| if err := rows.Scan(&r.ID, &r.EventID, &r.MyMezaID, &r.AmountKES, &r.Status, &r.CreatedAt); err != nil { | |
| continue | |
| } | |
| results = append(results, r) | |
| } | |
| c.JSON(http.StatusOK, results) | |
| } | |
| // Commissions β GET /api/v1/fa/commissions β read only | |
| // commissions β migration 050 | |
| // status CHECK = pending|paid|cancelled (lowercase per migration 050) | |
| func Commissions(c *gin.Context) { | |
| ctx := c.Request.Context() | |
| rows, err := db.DB.QueryContext(ctx, ` | |
| SELECT id, event_id, recipient_id, recipient_role, amount_kes, status, created_at | |
| FROM commissions | |
| ORDER BY created_at DESC | |
| LIMIT 100 | |
| `) | |
| if err != nil { | |
| c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load commissions"}) | |
| return | |
| } | |
| defer rows.Close() | |
| type Row struct { | |
| ID string `json:"id"` | |
| EventID string `json:"event_id"` | |
| RecipientID string `json:"recipient_id"` | |
| RecipientRole string `json:"recipient_role"` | |
| AmountKES float64 `json:"amount_kes"` | |
| Status string `json:"status"` | |
| CreatedAt string `json:"created_at"` | |
| } | |
| results := []Row{} | |
| for rows.Next() { | |
| var r Row | |
| if err := rows.Scan(&r.ID, &r.EventID, &r.RecipientID, &r.RecipientRole, &r.AmountKES, &r.Status, &r.CreatedAt); err != nil { | |
| continue | |
| } | |
| results = append(results, r) | |
| } | |
| c.JSON(http.StatusOK, results) | |
| } | |
| // Reconciliation β GET /api/v1/fa/reconciliation β read only | |
| // mpesa_transactions (migration 012), events (migration 048) | |
| // events.state CHECK = CREATED|IN_TRANSIT|AT_HUB|DELIVERED|COMPLETED|CANCELLED | |
| // DISPUTED removed β not a valid events state per migration 048 | |
| func Reconciliation(c *gin.Context) { | |
| ctx := c.Request.Context() | |
| var totalTransactions, completedEvents int | |
| var totalAmountKES float64 | |
| db.DB.QueryRowContext(ctx, `SELECT COUNT(*) FROM mpesa_transactions`).Scan(&totalTransactions) | |
| db.DB.QueryRowContext(ctx, `SELECT COUNT(*) FROM events WHERE state = 'COMPLETED'`).Scan(&completedEvents) | |
| db.DB.QueryRowContext(ctx, `SELECT COALESCE(SUM(amount_kes), 0) FROM mpesa_transactions WHERE status = 'SUCCESS'`).Scan(&totalAmountKES) | |
| c.JSON(http.StatusOK, gin.H{ | |
| "total_transactions": totalTransactions, | |
| "completed_events": completedEvents, | |
| "total_amount_kes": totalAmountKES, | |
| }) | |
| } | |
| // Reports β GET /api/v1/fa/reports β read only | |
| // mpesa_transactions β migration 012 | |
| // status CHECK = PENDING|SUCCESS|FAILED|TIMEOUT (uppercase per migration 012) | |
| func Reports(c *gin.Context) { | |
| ctx := c.Request.Context() | |
| var pendingPayouts, failedTransactions int | |
| var pendingAmountKES float64 | |
| db.DB.QueryRowContext(ctx, `SELECT COUNT(*) FROM mpesa_transactions WHERE status = 'PENDING'`).Scan(&pendingPayouts) | |
| db.DB.QueryRowContext(ctx, `SELECT COUNT(*) FROM mpesa_transactions WHERE status = 'FAILED'`).Scan(&failedTransactions) | |
| db.DB.QueryRowContext(ctx, `SELECT COALESCE(SUM(amount_kes), 0) FROM mpesa_transactions WHERE status = 'PENDING'`).Scan(&pendingAmountKES) | |
| c.JSON(http.StatusOK, gin.H{ | |
| "pending_payouts": pendingPayouts, | |
| "failed_transactions": failedTransactions, | |
| "pending_amount_kes": pendingAmountKES, | |
| }) | |
| } | |