From e00c3373cb450308e433750c88ed1682f5c75aef Mon Sep 17 00:00:00 2001 From: cclohmar Date: Sat, 30 May 2026 12:33:48 +0000 Subject: [PATCH] feat: replace abstract exchange rate with sample-based conversion - Instead of entering a hard-to-calculate rate like 0.00773, users now enter a real sample (e.g. receipt=1000 KES, claimed=7.73 USD) - The system computes the rate automatically: 7.73 / 1000 = 0.00773 - Users can get the sample values from their payment app notification - Much more intuitive, especially for currencies with small exchange rates --- internal/handlers/events.go | 17 ++++++++++---- templates/dashboard.html | 46 +++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/internal/handlers/events.go b/internal/handlers/events.go index fd77d81..da24c4e 100644 --- a/internal/handlers/events.go +++ b/internal/handlers/events.go @@ -98,13 +98,22 @@ func (h *EventHandler) CreateEvent(w http.ResponseWriter, r *http.Request) { baseCurrency := r.FormValue("base_currency") if baseCurrency == "" { - baseCurrency = "EUR" + baseCurrency = "USD" } + // Compute exchange rate from user-provided sample: + // e.g. receipt=1000 KES, claimed=7.73 USD → rate = 7.73 / 1000 = 0.00773 exchangeRate := 1.0 - if v := r.FormValue("exchange_rate"); v != "" { - if parsed, err := strconv.ParseFloat(v, 64); err == nil && parsed > 0 { - exchangeRate = parsed + sampleReceipt := r.FormValue("sample_receipt_amount") + sampleClaim := r.FormValue("sample_claim_amount") + if sampleReceipt != "" && sampleClaim != "" { + sampleReceiptVal, err1 := strconv.ParseFloat(sampleReceipt, 64) + sampleClaimVal, err2 := strconv.ParseFloat(sampleClaim, 64) + if err1 == nil && err2 == nil && sampleReceiptVal > 0 && sampleClaimVal > 0 { + exchangeRate = sampleClaimVal / sampleReceiptVal + log.Printf("INFO [%s] handlers: CreateEvent: computed rate %.6f from sample %s %s → %.2f %s", + time.Now().Format(time.RFC3339), exchangeRate, + r.FormValue("sample_receipt_currency"), sampleReceipt, sampleClaimVal, baseCurrency) } } diff --git a/templates/dashboard.html b/templates/dashboard.html index 3f43480..a46391a 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -39,18 +39,54 @@ -
- - +
+
+
+ 💱 Conversion Sample +
+

+ Enter a real receipt amount and what you were actually charged in your claim currency. + The exchange rate is calculated automatically. Check your payment app/bank notification for the exact converted amount. +

+
+
+ + +
+
+ + +
+
+
+
+ + +