115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
package transaction
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
)
|
|
|
|
func (s *Service) CreateShopDeposit(ctx context.Context, userID int64, role domain.Role, req domain.ShopDepositReq) (domain.ShopDeposit, error) {
|
|
var branchID int64
|
|
var companyID int64
|
|
var senderID int64
|
|
if role == domain.RoleAdmin || role == domain.RoleBranchManager || role == domain.RoleSuperAdmin {
|
|
if req.BranchID == nil {
|
|
// h.logger.Error("CashoutReq Branch ID is required for this user role")
|
|
return domain.ShopDeposit{}, ErrBranchRequiredForRole
|
|
}
|
|
branch, err := s.branchSvc.GetBranchByID(ctx, *req.BranchID)
|
|
if err != nil {
|
|
// h.logger.Error("CashoutReq no branches")
|
|
return domain.ShopDeposit{}, ErrInvalidBetID
|
|
}
|
|
|
|
branchID = branch.ID
|
|
companyID = branch.CompanyID
|
|
senderID = branch.WalletID
|
|
} else if role == domain.RoleCashier {
|
|
branch, err := s.branchSvc.GetBranchByCashier(ctx, userID)
|
|
if err != nil {
|
|
// h.logger.Error("CashoutReq failed, branch id invalid")
|
|
return domain.ShopDeposit{}, ErrInvalidBranchID
|
|
}
|
|
branchID = branch.ID
|
|
companyID = branch.CompanyID
|
|
senderID = branch.WalletID
|
|
} else {
|
|
return domain.ShopDeposit{}, ErrCustomerRoleNotAuthorized
|
|
}
|
|
|
|
customerWallet, err := s.walletSvc.GetCustomerWallet(ctx, req.CustomerID)
|
|
|
|
if err != nil {
|
|
return domain.ShopDeposit{}, err
|
|
}
|
|
|
|
transfer, err := s.walletSvc.TransferToWallet(ctx,
|
|
senderID, customerWallet.RegularID, domain.ToCurrency(req.Amount), domain.TRANSFER_DIRECT,
|
|
domain.ValidInt64{Value: userID, Valid: true},
|
|
fmt.Sprintf("Transferred %v from customer wallet deposit", req.Amount),
|
|
)
|
|
|
|
newTransaction, err := s.CreateShopTransaction(ctx, domain.CreateShopTransaction{
|
|
Amount: domain.Currency(req.Amount),
|
|
BranchID: branchID,
|
|
CompanyID: companyID,
|
|
UserID: userID,
|
|
Type: domain.TRANSACTION_DEPOSIT,
|
|
FullName: req.FullName,
|
|
PhoneNumber: req.PhoneNumber,
|
|
PaymentOption: req.PaymentOption,
|
|
BankCode: domain.ValidString{
|
|
Value: req.BankCode,
|
|
Valid: req.BankCode != "",
|
|
},
|
|
BeneficiaryName: domain.ValidString{
|
|
Value: req.BeneficiaryName,
|
|
Valid: req.BeneficiaryName != "",
|
|
},
|
|
AccountName: domain.ValidString{
|
|
Value: req.AccountName,
|
|
Valid: req.AccountName != "",
|
|
},
|
|
AccountNumber: domain.ValidString{
|
|
Value: req.AccountNumber,
|
|
Valid: req.AccountNumber != "",
|
|
},
|
|
ReferenceNumber: domain.ValidString{
|
|
Value: req.ReferenceNumber,
|
|
Valid: req.ReferenceNumber != "",
|
|
},
|
|
Verified: false,
|
|
ApprovedBy: domain.ValidInt64{
|
|
Value: userID,
|
|
Valid: true,
|
|
},
|
|
})
|
|
|
|
if err != nil {
|
|
return domain.ShopDeposit{}, err
|
|
}
|
|
|
|
return s.transactionStore.CreateShopDeposit(ctx, domain.CreateShopDeposit{
|
|
ShopTransactionID: newTransaction.ID,
|
|
CustomerID: req.CustomerID,
|
|
WalletTransferID: transfer.ID,
|
|
})
|
|
}
|
|
|
|
// func (s *Service) CreateShopDeposit(ctx context.Context, deposit domain.CreateShopDeposit) (domain.ShopDeposit, error) {
|
|
// return s.transactionStore.CreateShopDeposit(ctx, deposit)
|
|
// }
|
|
|
|
func (s *Service) GetAllShopDeposit(ctx context.Context, filter domain.ShopDepositFilter) ([]domain.ShopDepositDetail, error) {
|
|
return s.transactionStore.GetAllShopDeposit(ctx, filter)
|
|
}
|
|
|
|
func (s *Service) GetShopDepositByID(ctx context.Context, id int64) (domain.ShopDepositDetail, error) {
|
|
return s.transactionStore.GetShopDepositByID(ctx, id)
|
|
}
|
|
|
|
func (s *Service) GetShopDepositByShopTransactionID(ctx context.Context, shopTransactionID int64) (domain.ShopDepositDetail, error) {
|
|
return s.transactionStore.GetShopDepositByShopTransactionID(ctx, shopTransactionID)
|
|
}
|