From 6dbce0725df3f66a7831c3e90ff7a163d9518d4c Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Sun, 8 Jun 2025 13:55:10 +0300 Subject: [PATCH] more report cards --- cmd/main.go | 29 ++++++++++++++++-------- db/migrations/000001_fortune.up.sql | 3 ++- db/migrations/000002_notification.up.sql | 1 + internal/domain/mongoLogs.go | 12 ++++++++++ internal/logger/mongoLogger/handler.go | 14 ++---------- internal/repository/bet.go | 8 +++---- internal/repository/transaction.go | 8 +++---- 7 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 internal/domain/mongoLogs.go diff --git a/cmd/main.go b/cmd/main.go index 5a717ed..36861b8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -121,11 +121,11 @@ func main() { notificationSvc := notificationservice.New(notificationRepo, logger, cfg) - var betStore bet.BetStore - var walletStore wallet.WalletStore - var transactionStore transaction.TransactionStore - var branchStore branch.BranchStore - var userStore user.UserStore + // var betStore bet.BetStore + // var walletStore wallet.WalletStore + // var transactionStore transaction.TransactionStore + // var branchStore branch.BranchStore + // var userStore user.UserStore var notificationStore notificationservice.NotificationStore walletSvc := wallet.NewService( @@ -173,14 +173,23 @@ func main() { ) reportSvc := report.NewService( - betStore, - walletStore, - transactionStore, - branchStore, - userStore, + bet.BetStore(store), // Must implement BetStore + wallet.WalletStore(store), // Must implement WalletStore + transaction.TransactionStore(store), + branch.BranchStore(store), + user.UserStore(store), logger, ) + // reportSvc := report.NewService( + // betStore, + // walletStore, + // transactionStore, + // branchStore, + // userStore, + // logger, + // ) + walletMonitorSvc := monitor.NewService( *walletSvc, *branchSvc, diff --git a/db/migrations/000001_fortune.up.sql b/db/migrations/000001_fortune.up.sql index 1628f2d..5c13e03 100644 --- a/db/migrations/000001_fortune.up.sql +++ b/db/migrations/000001_fortune.up.sql @@ -138,7 +138,7 @@ CREATE TABLE IF NOT EXISTS transactions ( branch_id BIGINT NOT NULL, company_id BIGINT, cashier_id BIGINT, - cashier_name VARCHAR(255)L, + cashier_name VARCHAR(255), bet_id BIGINT, number_of_outcomes BIGINT, type BIGINT, @@ -162,6 +162,7 @@ CREATE TABLE IF NOT EXISTS branches ( id BIGSERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, location VARCHAR(255) NOT NULL, + is_active BOOLEAN NOT NULL DEFAULT false, wallet_id BIGINT NOT NULL, branch_manager_id BIGINT NOT NULL, company_id BIGINT NOT NULL, diff --git a/db/migrations/000002_notification.up.sql b/db/migrations/000002_notification.up.sql index 31c47fe..364de0c 100644 --- a/db/migrations/000002_notification.up.sql +++ b/db/migrations/000002_notification.up.sql @@ -44,3 +44,4 @@ CREATE INDEX idx_notifications_recipient_id ON notifications (recipient_id); CREATE INDEX idx_notifications_timestamp ON notifications (timestamp); CREATE INDEX idx_notifications_type ON notifications (type); + diff --git a/internal/domain/mongoLogs.go b/internal/domain/mongoLogs.go new file mode 100644 index 0000000..6f1cc2c --- /dev/null +++ b/internal/domain/mongoLogs.go @@ -0,0 +1,12 @@ +package domain + +type LogEntry struct { + Level string `json:"level" bson:"level"` + Message string `json:"message" bson:"message"` + Timestamp string `json:"timestamp" bson:"timestamp"` + Fields map[string]interface{} `json:"fields" bson:"fields"` + Caller string `json:"caller" bson:"caller"` + Stack string `json:"stacktrace" bson:"stacktrace"` + Service string `json:"service" bson:"service"` + Env string `json:"env" bson:"env"` +} diff --git a/internal/logger/mongoLogger/handler.go b/internal/logger/mongoLogger/handler.go index 81aa893..0aeb15f 100644 --- a/internal/logger/mongoLogger/handler.go +++ b/internal/logger/mongoLogger/handler.go @@ -3,23 +3,13 @@ package mongoLogger import ( "context" + "github.com/SamuelTariku/FortuneBet-Backend/internal/domain" "github.com/gofiber/fiber/v2" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) -type LogEntry struct { - Level string `json:"level" bson:"level"` - Message string `json:"message" bson:"message"` - Timestamp string `json:"timestamp" bson:"timestamp"` - Fields map[string]interface{} `json:"fields" bson:"fields"` - Caller string `json:"caller" bson:"caller"` - Stack string `json:"stacktrace" bson:"stacktrace"` - Service string `json:"service" bson:"service"` - Env string `json:"env" bson:"env"` -} - func GetLogsHandler(appCtx context.Context) fiber.Handler { return func(c *fiber.Ctx) error { client, err := mongo.Connect(appCtx, options.Client().ApplyURI("mongodb://root:secret@mongo:27017/?authSource=admin")) @@ -37,7 +27,7 @@ func GetLogsHandler(appCtx context.Context) fiber.Handler { } defer cursor.Close(appCtx) - var logs []LogEntry + var logs []domain.LogEntry if err := cursor.All(appCtx, &logs); err != nil { return fiber.NewError(fiber.StatusInternalServerError, "Cursor decoding error: "+err.Error()) } diff --git a/internal/repository/bet.go b/internal/repository/bet.go index f372ca1..23e97b7 100644 --- a/internal/repository/bet.go +++ b/internal/repository/bet.go @@ -309,10 +309,10 @@ func (s *Store) GetBetSummary(ctx context.Context, filter domain.ReportFilter) ( ) { query := `SELECT COALESCE(SUM(amount), 0) as total_stakes, - COUNT(*) as total_bets, - SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) as active_bets, - SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as total_wins, - SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as total_losses, + COALESCE(COUNT(*), 0) as total_bets, + COALESCE(SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END), 0) as active_bets, + COALESCE(SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END), 0) as total_wins, + COALESCE(SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END), 0) as total_losses, COALESCE(SUM(CASE WHEN status = 1 THEN amount * total_odds ELSE 0 END), 0) as win_balance FROM bets` diff --git a/internal/repository/transaction.go b/internal/repository/transaction.go index fc4826f..e2008b8 100644 --- a/internal/repository/transaction.go +++ b/internal/repository/transaction.go @@ -144,8 +144,8 @@ func (s *Store) UpdateTransactionVerified(ctx context.Context, id int64, verifie // GetTransactionTotals returns total deposits and withdrawals func (s *Store) GetTransactionTotals(ctx context.Context, filter domain.ReportFilter) (deposits, withdrawals domain.Currency, err error) { query := `SELECT - COALESCE(SUM(CASE WHEN type = 'deposit' THEN amount ELSE 0 END), 0) as deposits, - COALESCE(SUM(CASE WHEN type = 'withdrawal' THEN amount ELSE 0 END), 0) as withdrawals + COALESCE(SUM(CASE WHEN type = 1 THEN amount ELSE 0 END), 0) as deposits, + COALESCE(SUM(CASE WHEN type = 0 THEN amount ELSE 0 END), 0) as withdrawals FROM transactions` args := []interface{}{} @@ -193,8 +193,8 @@ func (s *Store) GetTransactionTotals(ctx context.Context, filter domain.ReportFi func (s *Store) GetBranchTransactionTotals(ctx context.Context, filter domain.ReportFilter) (map[int64]domain.BranchTransactions, error) { query := `SELECT branch_id, - COALESCE(SUM(CASE WHEN type = 'deposit' THEN amount ELSE 0 END), 0) as deposits, - COALESCE(SUM(CASE WHEN type = 'withdrawal' THEN amount ELSE 0 END), 0) as withdrawals + COALESCE(SUM(CASE WHEN type = 1 THEN amount ELSE 0 END), 0) as deposits, + COALESCE(SUM(CASE WHEN type = 0 THEN amount ELSE 0 END), 0) as withdrawals FROM transactions` args := []interface{}{}