Yimaru-BackEnd/internal/services/virtualGame/atlas/client.go
Samuel Tariku e49ff366d5 feat: Implement wallet notification system and refactor related services
- Added new notification handling in the wallet service to notify admins when wallet balances are low or insufficient.
- Created a new file for wallet notifications and moved relevant functions from the wallet service to this new file.
- Updated the wallet service to publish wallet events including wallet type.
- Refactored the client code to improve readability and maintainability.
- Enhanced the bet handler to support pagination and status filtering for bets.
- Updated routes and handlers for user search functionality to improve clarity and organization.
- Modified cron job scheduling to comment out unused jobs for clarity.
- Updated the WebSocket broadcast to include wallet type in notifications.
- Adjusted the makefile to include Kafka in the docker-compose setup for local development.
2025-09-25 21:26:24 +03:00

91 lines
2.1 KiB
Go

package atlas
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/SamuelTariku/FortuneBet-Backend/internal/config"
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/wallet"
)
type Client struct {
http *http.Client
BaseURL string
PrivateKey string
CasinoID string
PartnerID string
walletSvc *wallet.Service
}
func NewClient(cfg *config.Config, walletSvc *wallet.Service) *Client {
return &Client{
http: &http.Client{Timeout: 10 * time.Second},
BaseURL: cfg.Atlas.BaseURL,
PrivateKey: cfg.Atlas.SecretKey, // PRIVATE_KEY from Atlas
CasinoID: cfg.Atlas.CasinoID, // provided by Atlas
PartnerID: cfg.Atlas.PartnerID, // aggregator/casino partner_id
walletSvc: walletSvc,
}
}
// Generate timestamp in ms
func nowTimestamp() string {
return fmt.Sprintf("%d", time.Now().UnixMilli())
}
// Signature generator: sha1 hex of (REQUEST_BODY + PRIVATE_KEY + timestamp)
func (c *Client) generateHash(body []byte, timestamp string) string {
plain := string(body) + c.PrivateKey + timestamp
h := sha1.New()
h.Write([]byte(plain))
return hex.EncodeToString(h.Sum(nil))
}
// POST helper
func (c *Client) post(ctx context.Context, path string, body map[string]any, result any) error {
// Add timestamp first
timestamp := nowTimestamp()
body["timestamp"] = timestamp
// Marshal without hash first
tmp, _ := json.Marshal(body)
// Generate hash using original body
hash := c.generateHash(tmp, timestamp)
body["hash"] = hash
fmt.Printf("atlasPost: %v \n", body)
// Marshal final body
data, _ := json.Marshal(body)
req, _ := http.NewRequestWithContext(ctx, "POST", c.BaseURL+path, bytes.NewReader(data))
req.Header.Set("Content-Type", "text/javascript")
// Debug
fmt.Println("Request URL:", c.BaseURL+path)
fmt.Println("Request Body:", string(data))
// Send request
res, err := c.http.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
b, _ := io.ReadAll(res.Body)
if res.StatusCode >= 400 {
return fmt.Errorf("error: %s", string(b))
}
if result != nil {
return json.Unmarshal(b, result)
}
return nil
}