fix: customer auth integration
This commit is contained in:
parent
16768ad924
commit
1be3ffdc3c
|
|
@ -114,7 +114,6 @@ CREATE TABLE IF NOT EXISTS wallets (
|
|||
CREATE TABLE IF NOT EXISTS customer_wallets (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
customer_id BIGINT NOT NULL,
|
||||
company_id BIGINT NOT NULL,
|
||||
regular_wallet_id BIGINT NOT NULL,
|
||||
static_wallet_id BIGINT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
|
|
|
|||
|
|
@ -10,11 +10,10 @@ RETURNING *;
|
|||
-- name: CreateCustomerWallet :one
|
||||
INSERT INTO customer_wallets (
|
||||
customer_id,
|
||||
company_id,
|
||||
regular_wallet_id,
|
||||
static_wallet_id
|
||||
)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING *;
|
||||
-- name: GetAllWallets :many
|
||||
SELECT *
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ type CustomerWallet struct {
|
|||
RegularID int64
|
||||
StaticID int64
|
||||
CustomerID int64
|
||||
CompanyID int64
|
||||
}
|
||||
type GetCustomerWallet struct {
|
||||
ID int64
|
||||
|
|
@ -28,7 +27,6 @@ type GetCustomerWallet struct {
|
|||
StaticID int64
|
||||
StaticBalance Currency
|
||||
CustomerID int64
|
||||
CompanyID int64
|
||||
RegularUpdatedAt time.Time
|
||||
StaticUpdatedAt time.Time
|
||||
CreatedAt time.Time
|
||||
|
|
@ -56,7 +54,6 @@ type CreateWallet struct {
|
|||
|
||||
type CreateCustomerWallet struct {
|
||||
CustomerID int64
|
||||
CompanyID int64
|
||||
RegularWalletID int64
|
||||
StaticWalletID int64
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,13 +36,11 @@ func convertDBCustomerWallet(customerWallet dbgen.CustomerWallet) domain.Custome
|
|||
RegularID: customerWallet.RegularWalletID,
|
||||
StaticID: customerWallet.StaticWalletID,
|
||||
CustomerID: customerWallet.CustomerID,
|
||||
CompanyID: customerWallet.CompanyID,
|
||||
}
|
||||
}
|
||||
func convertCreateCustomerWallet(customerWallet domain.CreateCustomerWallet) dbgen.CreateCustomerWalletParams {
|
||||
return dbgen.CreateCustomerWalletParams{
|
||||
CustomerID: customerWallet.CustomerID,
|
||||
CompanyID: customerWallet.CompanyID,
|
||||
RegularWalletID: customerWallet.RegularWalletID,
|
||||
StaticWalletID: customerWallet.StaticWalletID,
|
||||
}
|
||||
|
|
@ -56,7 +54,6 @@ func convertDBGetCustomerWallet(customerWallet dbgen.GetCustomerWalletRow) domai
|
|||
StaticID: customerWallet.StaticID,
|
||||
StaticBalance: domain.Currency(customerWallet.StaticBalance),
|
||||
CustomerID: customerWallet.CustomerID,
|
||||
CompanyID: customerWallet.CompanyID,
|
||||
RegularUpdatedAt: customerWallet.RegularUpdatedAt.Time,
|
||||
StaticUpdatedAt: customerWallet.StaticUpdatedAt.Time,
|
||||
CreatedAt: customerWallet.CreatedAt.Time,
|
||||
|
|
|
|||
|
|
@ -209,8 +209,21 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
|
|||
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{
|
||||
Value: *req.BranchID,
|
||||
Value: branch.ID,
|
||||
Valid: true,
|
||||
}
|
||||
newBet.UserID = domain.ValidInt64{
|
||||
|
|
@ -221,8 +234,19 @@ func (s *Service) PlaceBet(ctx context.Context, req domain.CreateBetReq, userID
|
|||
case domain.RoleCustomer:
|
||||
// Get User Wallet
|
||||
|
||||
|
||||
return domain.CreateBetRes{}, fmt.Errorf("Not yet implemented")
|
||||
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
|
||||
}
|
||||
|
||||
default:
|
||||
return domain.CreateBetRes{}, fmt.Errorf("Unknown Role Type")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ func (s *Service) CreateWallet(ctx context.Context, wallet domain.CreateWallet)
|
|||
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{
|
||||
IsWithdraw: true,
|
||||
|
|
@ -39,7 +39,6 @@ func (s *Service) CreateCustomerWallet(ctx context.Context, customerID int64, co
|
|||
|
||||
return s.walletStore.CreateCustomerWallet(ctx, domain.CreateCustomerWallet{
|
||||
CustomerID: customerID,
|
||||
CompanyID: companyID,
|
||||
RegularWalletID: regularWallet.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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (s *Service) UpdateWalletActive(ctx context.Context, id int64, isActive bool) error {
|
||||
return s.walletStore.UpdateWalletActive(ctx, id, isActive)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -150,9 +150,7 @@ func (h *Handler) RegisterUser(c *fiber.Ctx) error {
|
|||
medium, err := getMedium(req.Email, req.PhoneNumber)
|
||||
if err != nil {
|
||||
h.logger.Error("RegisterUser failed", "error", err)
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
user.OtpMedium = medium
|
||||
|
|
@ -160,24 +158,22 @@ func (h *Handler) RegisterUser(c *fiber.Ctx) error {
|
|||
newUser, err := h.userSvc.RegisterUser(c.Context(), user)
|
||||
if err != nil {
|
||||
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) {
|
||||
return response.WriteJSON(c, fiber.StatusBadRequest, "Otp expired", nil, nil)
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Otp expired")
|
||||
}
|
||||
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) {
|
||||
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)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "Internal server error",
|
||||
})
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Unknown Error")
|
||||
}
|
||||
|
||||
_, err = h.walletSvc.CreateWallet(c.Context(), domain.CreateWallet{
|
||||
newWallet, err := h.walletSvc.CreateWallet(c.Context(), domain.CreateWallet{
|
||||
UserID: newUser.ID,
|
||||
IsWithdraw: 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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,6 @@ type CustomerWalletRes struct {
|
|||
StaticID int64 `json:"static_id" example:"1"`
|
||||
StaticBalance float32 `json:"static_balance" example:"100.0"`
|
||||
CustomerID int64 `json:"customer_id" example:"1"`
|
||||
CompanyID int64 `json:"company_id" example:"1"`
|
||||
RegularUpdatedAt time.Time `json:"regular_updated_at"`
|
||||
StaticUpdatedAt time.Time `json:"static_updated_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
|
|
@ -59,7 +58,6 @@ func convertCustomerWallet(wallet domain.GetCustomerWallet) CustomerWalletRes {
|
|||
StaticID: wallet.StaticID,
|
||||
StaticBalance: wallet.StaticBalance.Float32(),
|
||||
CustomerID: wallet.CustomerID,
|
||||
CompanyID: wallet.CompanyID,
|
||||
RegularUpdatedAt: wallet.RegularUpdatedAt,
|
||||
StaticUpdatedAt: wallet.StaticUpdatedAt,
|
||||
CreatedAt: wallet.CreatedAt,
|
||||
|
|
@ -249,21 +247,21 @@ func (h *Handler) GetCustomerWallet(c *fiber.Ctx) error {
|
|||
return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized access")
|
||||
}
|
||||
|
||||
companyID, err := strconv.ParseInt(c.Get("company_id"), 10, 64)
|
||||
if err != nil {
|
||||
h.logger.Error("Invalid company_id header", "value", c.Get("company_id"), "error", err)
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid company_id")
|
||||
}
|
||||
// companyID, err := strconv.ParseInt(c.Get("company_id"), 10, 64)
|
||||
// if err != nil {
|
||||
// h.logger.Error("Invalid company_id header", "value", c.Get("company_id"), "error", err)
|
||||
// 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 {
|
||||
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")
|
||||
}
|
||||
|
||||
res := convertCustomerWallet(wallet)
|
||||
res := convertWallet(wallet[0])
|
||||
|
||||
return response.WriteJSON(c, fiber.StatusOK, "Wallet retrieved successfully", res, nil)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user