fix: customer auth integration

This commit is contained in:
Samuel Tariku 2025-05-23 18:21:37 +03:00
parent 16768ad924
commit 1be3ffdc3c
8 changed files with 53 additions and 38 deletions

View File

@ -114,7 +114,6 @@ CREATE TABLE IF NOT EXISTS wallets (
CREATE TABLE IF NOT EXISTS customer_wallets ( CREATE TABLE IF NOT EXISTS customer_wallets (
id BIGSERIAL PRIMARY KEY, id BIGSERIAL PRIMARY KEY,
customer_id BIGINT NOT NULL, customer_id BIGINT NOT NULL,
company_id BIGINT NOT NULL,
regular_wallet_id BIGINT NOT NULL, regular_wallet_id BIGINT NOT NULL,
static_wallet_id BIGINT NOT NULL, static_wallet_id BIGINT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

View File

@ -10,11 +10,10 @@ RETURNING *;
-- name: CreateCustomerWallet :one -- name: CreateCustomerWallet :one
INSERT INTO customer_wallets ( INSERT INTO customer_wallets (
customer_id, customer_id,
company_id,
regular_wallet_id, regular_wallet_id,
static_wallet_id static_wallet_id
) )
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3)
RETURNING *; RETURNING *;
-- name: GetAllWallets :many -- name: GetAllWallets :many
SELECT * SELECT *

View File

@ -19,7 +19,6 @@ type CustomerWallet struct {
RegularID int64 RegularID int64
StaticID int64 StaticID int64
CustomerID int64 CustomerID int64
CompanyID int64
} }
type GetCustomerWallet struct { type GetCustomerWallet struct {
ID int64 ID int64
@ -28,7 +27,6 @@ type GetCustomerWallet struct {
StaticID int64 StaticID int64
StaticBalance Currency StaticBalance Currency
CustomerID int64 CustomerID int64
CompanyID int64
RegularUpdatedAt time.Time RegularUpdatedAt time.Time
StaticUpdatedAt time.Time StaticUpdatedAt time.Time
CreatedAt time.Time CreatedAt time.Time
@ -56,7 +54,6 @@ type CreateWallet struct {
type CreateCustomerWallet struct { type CreateCustomerWallet struct {
CustomerID int64 CustomerID int64
CompanyID int64
RegularWalletID int64 RegularWalletID int64
StaticWalletID int64 StaticWalletID int64
} }

View File

@ -36,13 +36,11 @@ func convertDBCustomerWallet(customerWallet dbgen.CustomerWallet) domain.Custome
RegularID: customerWallet.RegularWalletID, RegularID: customerWallet.RegularWalletID,
StaticID: customerWallet.StaticWalletID, StaticID: customerWallet.StaticWalletID,
CustomerID: customerWallet.CustomerID, CustomerID: customerWallet.CustomerID,
CompanyID: customerWallet.CompanyID,
} }
} }
func convertCreateCustomerWallet(customerWallet domain.CreateCustomerWallet) dbgen.CreateCustomerWalletParams { func convertCreateCustomerWallet(customerWallet domain.CreateCustomerWallet) dbgen.CreateCustomerWalletParams {
return dbgen.CreateCustomerWalletParams{ return dbgen.CreateCustomerWalletParams{
CustomerID: customerWallet.CustomerID, CustomerID: customerWallet.CustomerID,
CompanyID: customerWallet.CompanyID,
RegularWalletID: customerWallet.RegularWalletID, RegularWalletID: customerWallet.RegularWalletID,
StaticWalletID: customerWallet.StaticWalletID, StaticWalletID: customerWallet.StaticWalletID,
} }
@ -56,7 +54,6 @@ func convertDBGetCustomerWallet(customerWallet dbgen.GetCustomerWalletRow) domai
StaticID: customerWallet.StaticID, StaticID: customerWallet.StaticID,
StaticBalance: domain.Currency(customerWallet.StaticBalance), StaticBalance: domain.Currency(customerWallet.StaticBalance),
CustomerID: customerWallet.CustomerID, CustomerID: customerWallet.CustomerID,
CompanyID: customerWallet.CompanyID,
RegularUpdatedAt: customerWallet.RegularUpdatedAt.Time, RegularUpdatedAt: customerWallet.RegularUpdatedAt.Time,
StaticUpdatedAt: customerWallet.StaticUpdatedAt.Time, StaticUpdatedAt: customerWallet.StaticUpdatedAt.Time,
CreatedAt: customerWallet.CreatedAt.Time, CreatedAt: customerWallet.CreatedAt.Time,

View File

@ -209,8 +209,21 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
return domain.CreateBetRes{}, ErrBranchIDRequired return domain.CreateBetRes{}, ErrBranchIDRequired
} }
branch, err := s.branchSvc.GetBranchByID(ctx, *req.BranchID)
if err != nil {
return domain.CreateBetRes{}, err
}
// Deduct from wallet:
// TODO: Make this percentage come from the company
var deductedAmount = req.Amount / 10
err = s.walletSvc.DeductFromWallet(ctx, branch.WalletID, domain.ToCurrency(deductedAmount))
if err != nil {
return domain.CreateBetRes{}, err
}
newBet.BranchID = domain.ValidInt64{ newBet.BranchID = domain.ValidInt64{
Value: *req.BranchID, Value: branch.ID,
Valid: true, Valid: true,
} }
newBet.UserID = domain.ValidInt64{ newBet.UserID = domain.ValidInt64{
@ -221,8 +234,19 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
case domain.RoleCustomer: case domain.RoleCustomer:
// Get User Wallet // Get User Wallet
wallet, err := s.walletSvc.GetWalletsByUser(ctx, userID)
if err != nil {
return domain.CreateBetRes{}, err
}
userWallet := wallet[0]
err = s.walletSvc.DeductFromWallet(ctx, userWallet.ID, domain.ToCurrency(req.Amount))
if err != nil {
return domain.CreateBetRes{}, err
}
return domain.CreateBetRes{}, fmt.Errorf("Not yet implemented")
default: default:
return domain.CreateBetRes{}, fmt.Errorf("Unknown Role Type") return domain.CreateBetRes{}, fmt.Errorf("Unknown Role Type")
} }

View File

@ -15,7 +15,7 @@ func (s *Service) CreateWallet(ctx context.Context, wallet domain.CreateWallet)
return s.walletStore.CreateWallet(ctx, wallet) return s.walletStore.CreateWallet(ctx, wallet)
} }
func (s *Service) CreateCustomerWallet(ctx context.Context, customerID int64, companyID int64) (domain.CustomerWallet, error) { func (s *Service) CreateCustomerWallet(ctx context.Context, customerID int64) (domain.CustomerWallet, error) {
regularWallet, err := s.CreateWallet(ctx, domain.CreateWallet{ regularWallet, err := s.CreateWallet(ctx, domain.CreateWallet{
IsWithdraw: true, IsWithdraw: true,
@ -39,7 +39,6 @@ func (s *Service) CreateCustomerWallet(ctx context.Context, customerID int64, co
return s.walletStore.CreateCustomerWallet(ctx, domain.CreateCustomerWallet{ return s.walletStore.CreateCustomerWallet(ctx, domain.CreateCustomerWallet{
CustomerID: customerID, CustomerID: customerID,
CompanyID: companyID,
RegularWalletID: regularWallet.ID, RegularWalletID: regularWallet.ID,
StaticWalletID: staticWallet.ID, StaticWalletID: staticWallet.ID,
}) })
@ -91,8 +90,6 @@ func (s *Service) DeductFromWallet(ctx context.Context, id int64, amount domain.
return s.walletStore.UpdateBalance(ctx, id, wallet.Balance+amount) return s.walletStore.UpdateBalance(ctx, id, wallet.Balance+amount)
} }
func (s *Service) UpdateWalletActive(ctx context.Context, id int64, isActive bool) error { func (s *Service) UpdateWalletActive(ctx context.Context, id int64, isActive bool) error {
return s.walletStore.UpdateWalletActive(ctx, id, isActive) return s.walletStore.UpdateWalletActive(ctx, id, isActive)
} }

View File

@ -150,9 +150,7 @@ func (h *Handler) RegisterUser(c *fiber.Ctx) error {
medium, err := getMedium(req.Email, req.PhoneNumber) medium, err := getMedium(req.Email, req.PhoneNumber)
if err != nil { if err != nil {
h.logger.Error("RegisterUser failed", "error", err) h.logger.Error("RegisterUser failed", "error", err)
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return fiber.NewError(fiber.StatusBadRequest, err.Error())
"error": err.Error(),
})
} }
user.OtpMedium = medium user.OtpMedium = medium
@ -160,24 +158,22 @@ func (h *Handler) RegisterUser(c *fiber.Ctx) error {
newUser, err := h.userSvc.RegisterUser(c.Context(), user) newUser, err := h.userSvc.RegisterUser(c.Context(), user)
if err != nil { if err != nil {
if errors.Is(err, domain.ErrOtpAlreadyUsed) { if errors.Is(err, domain.ErrOtpAlreadyUsed) {
return response.WriteJSON(c, fiber.StatusBadRequest, "Otp already used", nil, nil) return fiber.NewError(fiber.StatusBadRequest, "Otp already used")
} }
if errors.Is(err, domain.ErrOtpExpired) { if errors.Is(err, domain.ErrOtpExpired) {
return response.WriteJSON(c, fiber.StatusBadRequest, "Otp expired", nil, nil) return fiber.NewError(fiber.StatusBadRequest, "Otp expired")
} }
if errors.Is(err, domain.ErrInvalidOtp) { if errors.Is(err, domain.ErrInvalidOtp) {
return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid otp", nil, nil) return fiber.NewError(fiber.StatusBadRequest, "Invalid otp")
} }
if errors.Is(err, domain.ErrOtpNotFound) { if errors.Is(err, domain.ErrOtpNotFound) {
return response.WriteJSON(c, fiber.StatusBadRequest, "User already exist", nil, nil) return fiber.NewError(fiber.StatusBadRequest, "User already exist")
} }
h.logger.Error("RegisterUser failed", "error", err) h.logger.Error("RegisterUser failed", "error", err)
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return fiber.NewError(fiber.StatusInternalServerError, "Unknown Error")
"error": "Internal server error",
})
} }
_, err = h.walletSvc.CreateWallet(c.Context(), domain.CreateWallet{ newWallet, err := h.walletSvc.CreateWallet(c.Context(), domain.CreateWallet{
UserID: newUser.ID, UserID: newUser.ID,
IsWithdraw: true, IsWithdraw: true,
IsBettable: true, IsBettable: true,
@ -194,6 +190,14 @@ func (h *Handler) RegisterUser(c *fiber.Ctx) error {
} }
} }
// TODO: Remove later
err = h.walletSvc.AddToWallet(c.Context(), newWallet.ID, domain.ToCurrency(100.0))
if err != nil {
h.logger.Error("Failed to update wallet for user", "userID", newUser.ID, "error", err)
return fiber.NewError(fiber.StatusInternalServerError, "Failed to update user wallet")
}
return response.WriteJSON(c, fiber.StatusOK, "Registration successful", nil, nil) return response.WriteJSON(c, fiber.StatusOK, "Registration successful", nil, nil)
} }

View File

@ -45,7 +45,6 @@ type CustomerWalletRes struct {
StaticID int64 `json:"static_id" example:"1"` StaticID int64 `json:"static_id" example:"1"`
StaticBalance float32 `json:"static_balance" example:"100.0"` StaticBalance float32 `json:"static_balance" example:"100.0"`
CustomerID int64 `json:"customer_id" example:"1"` CustomerID int64 `json:"customer_id" example:"1"`
CompanyID int64 `json:"company_id" example:"1"`
RegularUpdatedAt time.Time `json:"regular_updated_at"` RegularUpdatedAt time.Time `json:"regular_updated_at"`
StaticUpdatedAt time.Time `json:"static_updated_at"` StaticUpdatedAt time.Time `json:"static_updated_at"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
@ -59,7 +58,6 @@ func convertCustomerWallet(wallet domain.GetCustomerWallet) CustomerWalletRes {
StaticID: wallet.StaticID, StaticID: wallet.StaticID,
StaticBalance: wallet.StaticBalance.Float32(), StaticBalance: wallet.StaticBalance.Float32(),
CustomerID: wallet.CustomerID, CustomerID: wallet.CustomerID,
CompanyID: wallet.CompanyID,
RegularUpdatedAt: wallet.RegularUpdatedAt, RegularUpdatedAt: wallet.RegularUpdatedAt,
StaticUpdatedAt: wallet.StaticUpdatedAt, StaticUpdatedAt: wallet.StaticUpdatedAt,
CreatedAt: wallet.CreatedAt, CreatedAt: wallet.CreatedAt,
@ -249,21 +247,21 @@ func (h *Handler) GetCustomerWallet(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized access") return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized access")
} }
companyID, err := strconv.ParseInt(c.Get("company_id"), 10, 64) // companyID, err := strconv.ParseInt(c.Get("company_id"), 10, 64)
if err != nil { // if err != nil {
h.logger.Error("Invalid company_id header", "value", c.Get("company_id"), "error", err) // h.logger.Error("Invalid company_id header", "value", c.Get("company_id"), "error", err)
return fiber.NewError(fiber.StatusBadRequest, "Invalid company_id") // return fiber.NewError(fiber.StatusBadRequest, "Invalid company_id")
} // }
h.logger.Info("Fetching customer wallet", "userID", userID, "companyID", companyID) h.logger.Info("Fetching customer wallet", "userID", userID)
wallet, err := h.walletSvc.GetCustomerWallet(c.Context(), userID, companyID) wallet, err := h.walletSvc.GetWalletsByUser(c.Context(), userID)
if err != nil { if err != nil {
h.logger.Error("Failed to get customer wallet", "userID", userID, "companyID", companyID, "error", err) h.logger.Error("Failed to get customer wallet", "userID", userID, "error", err)
return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve wallet") return fiber.NewError(fiber.StatusInternalServerError, "Failed to retrieve wallet")
} }
res := convertCustomerWallet(wallet) res := convertWallet(wallet[0])
return response.WriteJSON(c, fiber.StatusOK, "Wallet retrieved successfully", res, nil) return response.WriteJSON(c, fiber.StatusOK, "Wallet retrieved successfully", res, nil)
} }