diff --git a/db/migrations/000007_setting_data.up.sql b/db/migrations/000007_setting_data.up.sql index adbc6c2..f69156b 100644 --- a/db/migrations/000007_setting_data.up.sql +++ b/db/migrations/000007_setting_data.up.sql @@ -4,8 +4,7 @@ VALUES ('max_number_of_outcomes', '30'), ('bet_amount_limit', '100000'), ('daily_ticket_limit', '50'), ('total_winnings_limit', '1000000'), - ('amount_for_bet_referral', '1000000') - ('cashback_amount_cap', '1000') - ON CONFLICT (key) DO + ('amount_for_bet_referral', '1000000'), + ('cashback_amount_cap', '1000') ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value; \ No newline at end of file diff --git a/db/query/location.sql b/db/query/location.sql new file mode 100644 index 0000000..01d117d --- /dev/null +++ b/db/query/location.sql @@ -0,0 +1,7 @@ +-- name: GetAllBranchLocations :many +SELECT * +FROM branch_locations +WHERE ( + value ILIKE '%' || sqlc.narg('query') || '%' + OR sqlc.narg('query') IS NULL + ); \ No newline at end of file diff --git a/gen/db/location.sql.go b/gen/db/location.sql.go new file mode 100644 index 0000000..008aa61 --- /dev/null +++ b/gen/db/location.sql.go @@ -0,0 +1,41 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.29.0 +// source: location.sql + +package dbgen + +import ( + "context" + + "github.com/jackc/pgx/v5/pgtype" +) + +const GetAllBranchLocations = `-- name: GetAllBranchLocations :many +SELECT key, value +FROM branch_locations +WHERE ( + value ILIKE '%' || $1 || '%' + OR $1 IS NULL + ) +` + +func (q *Queries) GetAllBranchLocations(ctx context.Context, query pgtype.Text) ([]BranchLocation, error) { + rows, err := q.db.Query(ctx, GetAllBranchLocations, query) + if err != nil { + return nil, err + } + defer rows.Close() + var items []BranchLocation + for rows.Next() { + var i BranchLocation + if err := rows.Scan(&i.Key, &i.Value); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/domain/branch.go b/internal/domain/branch.go index 610af17..8064410 100644 --- a/internal/domain/branch.go +++ b/internal/domain/branch.go @@ -11,6 +11,11 @@ type Branch struct { IsSelfOwned bool } +type BranchLocation struct { + Key string `json:"key" example:"addis_ababa" ` + Name string `json:"name" example:"Addis Ababa"` +} + type BranchFilter struct { CompanyID ValidInt64 IsActive ValidBool diff --git a/internal/repository/location.go b/internal/repository/location.go new file mode 100644 index 0000000..d2c958b --- /dev/null +++ b/internal/repository/location.go @@ -0,0 +1,30 @@ +package repository + +import ( + "context" + + "github.com/SamuelTariku/FortuneBet-Backend/internal/domain" + "github.com/jackc/pgx/v5/pgtype" +) + + +func (s *Store) GetAllBranchLocations (ctx context.Context, query domain.ValidString) ([]domain.BranchLocation, error) { + locations, err := s.queries.GetAllBranchLocations(ctx, pgtype.Text{ + String: query.Value, + Valid: query.Valid, + }) + + if err != nil { + return nil, err + } + + var result []domain.BranchLocation = make([]domain.BranchLocation, 0, len(locations)) + + for _, location := range locations { + result = append(result, domain.BranchLocation{ + Key: location.Key, + Name: location.Value, + }) + } + return result, nil +} \ No newline at end of file diff --git a/internal/services/bet/service.go b/internal/services/bet/service.go index a3952db..891d8a5 100644 --- a/internal/services/bet/service.go +++ b/internal/services/bet/service.go @@ -1033,7 +1033,7 @@ func (s *Service) ProcessBetCashback(ctx context.Context) error { ) continue } - cashbackAmount := math.Min(float64(settingsList.CashbackAmountCap), float64(calculateCashbackAmount(bet.Amount.Float32(), bet.TotalOdds))) + cashbackAmount := math.Min(float64(settingsList.CashbackAmountCap.Float32()), float64(calculateCashbackAmount(bet.Amount.Float32(), bet.TotalOdds))) _, err = s.walletSvc.AddToWallet(ctx, wallets.StaticID, domain.ToCurrency(float32(cashbackAmount)), domain.ValidInt64{}, domain.TRANSFER_DIRECT, domain.PaymentDetails{}, fmt.Sprintf("cashback amount of %f added to users static wallet", cashbackAmount)) diff --git a/internal/services/branch/branch_locations.go b/internal/services/branch/branch_locations.go new file mode 100644 index 0000000..ad25f33 --- /dev/null +++ b/internal/services/branch/branch_locations.go @@ -0,0 +1,11 @@ +package branch + +import ( + "context" + + "github.com/SamuelTariku/FortuneBet-Backend/internal/domain" +) + +func (s *Service) GetAllBranchLocations(ctx context.Context, query domain.ValidString) ([]domain.BranchLocation, error) { + return s.branchStore.GetAllBranchLocations(ctx, query) +} diff --git a/internal/services/branch/port.go b/internal/services/branch/port.go index 3f242c8..8b17ae1 100644 --- a/internal/services/branch/port.go +++ b/internal/services/branch/port.go @@ -29,4 +29,6 @@ type BranchStore interface { GetAllCompaniesBranch(ctx context.Context) ([]domain.Company, error) GetBranchesByCompany(ctx context.Context, companyID int64) ([]domain.Branch, error) + + GetAllBranchLocations(ctx context.Context, query domain.ValidString) ([]domain.BranchLocation, error) } diff --git a/internal/services/branch/service.go b/internal/services/branch/service.go index eccb764..9e4f641 100644 --- a/internal/services/branch/service.go +++ b/internal/services/branch/service.go @@ -78,3 +78,4 @@ func (s *Service) GetAllCompaniesBranch(ctx context.Context) ([]domain.Company, func (s *Service) GetBranchesByCompany(ctx context.Context, companyID int64) ([]domain.Branch, error) { return s.branchStore.GetBranchesByCompany(ctx, companyID) } + diff --git a/internal/web_server/handlers/branch_handler.go b/internal/web_server/handlers/branch_handler.go index 4f42491..d085a38 100644 --- a/internal/web_server/handlers/branch_handler.go +++ b/internal/web_server/handlers/branch_handler.go @@ -619,6 +619,39 @@ func (h *Handler) GetBranchOperations(c *fiber.Ctx) error { return response.WriteJSON(c, fiber.StatusOK, "Branch Operations retrieved successfully", result, nil) } + +// GetAllBranchLocations godoc +// @Summary Gets all branch locations +// @Description Gets all branch locations +// @Tags branch +// @Accept json +// @Produce json +// @Success 200 {array} domain.BranchLocation +// @Failure 400 {object} response.APIResponse +// @Failure 500 {object} response.APIResponse +// @Router /api/v1/branchLocation [get] +func (h *Handler) GetAllBranchLocations(c *fiber.Ctx) error { + + searchQuery := c.Query("query") + searchString := domain.ValidString{ + Value: searchQuery, + Valid: searchQuery != "", + } + + locations, err := h.branchSvc.GetAllBranchLocations(c.Context(), searchString) + + if err != nil { + h.mongoLoggerSvc.Error("Failed to get branch locations", + zap.Int("status_code", fiber.StatusInternalServerError), + zap.Error(err), + zap.Time("timestamp", time.Now()), + ) + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + return response.WriteJSON(c, fiber.StatusOK, "Branch Location successfully fetched", locations, nil) +} + // GetBranchCashiers godoc // @Summary Gets branch cashiers // @Description Gets branch cashiers diff --git a/internal/web_server/routes.go b/internal/web_server/routes.go index d5bf75a..ba725df 100644 --- a/internal/web_server/routes.go +++ b/internal/web_server/routes.go @@ -171,9 +171,10 @@ func (a *App) initAppRoutes() { groupV1.Put("/branch/:id/set-active", a.authMiddleware, h.UpdateBranchStatus) groupV1.Put("/branch/:id/set-inactive", a.authMiddleware, h.UpdateBranchStatus) groupV1.Delete("/branch/:id", a.authMiddleware, h.DeleteBranch) + groupV1.Get("/search/branch", a.authMiddleware, h.SearchBranch) - // /branch/search - // branch/wallet + groupV1.Get("/branchLocation", a.authMiddleware, h.GetAllBranchLocations) + groupV1.Get("/branch/:id/cashiers", a.authMiddleware, h.GetBranchCashiers) groupV1.Get("/branchCashier", a.authMiddleware, h.GetBranchForCashier)