93 lines
3.4 KiB
Go
93 lines
3.4 KiB
Go
package transaction
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/bet"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/branch"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/services/wallet"
|
|
)
|
|
|
|
var (
|
|
ErrBranchRequiredForRole = errors.New("branch_id is required to be passed for this role")
|
|
ErrInvalidBranchID = errors.New("invalid branch id")
|
|
ErrUnauthorizedCompanyID = errors.New("unauthorized company id")
|
|
ErrUnauthorizedBranchManager = errors.New("unauthorized branch manager")
|
|
ErrCustomerRoleNotAuthorized = errors.New("customer role not authorized")
|
|
)
|
|
|
|
type Service struct {
|
|
transactionStore TransactionStore
|
|
branchSvc branch.Service
|
|
betSvc bet.Service
|
|
walletSvc wallet.Service
|
|
}
|
|
|
|
func NewService(transactionStore TransactionStore, branchSvc branch.Service, betSvc bet.Service, walletSvc wallet.Service) *Service {
|
|
return &Service{
|
|
transactionStore: transactionStore,
|
|
branchSvc: branchSvc,
|
|
betSvc: betSvc,
|
|
walletSvc: walletSvc,
|
|
}
|
|
}
|
|
|
|
func (s *Service) CreateShopTransaction(ctx context.Context, transaction domain.CreateShopTransaction) (domain.ShopTransaction, error) {
|
|
return s.transactionStore.CreateShopTransaction(ctx, transaction)
|
|
}
|
|
func (s *Service) GetShopTransactionByID(ctx context.Context, id int64) (domain.ShopTransactionDetail, error) {
|
|
return s.transactionStore.GetShopTransactionByID(ctx, id)
|
|
}
|
|
func (s *Service) GetAllShopTransactions(ctx context.Context, filter domain.ShopTransactionFilter) ([]domain.ShopTransactionDetail, error) {
|
|
return s.transactionStore.GetAllShopTransactions(ctx, filter)
|
|
}
|
|
func (s *Service) GetShopTransactionByBranch(ctx context.Context, id int64) ([]domain.ShopTransactionDetail, error) {
|
|
return s.transactionStore.GetShopTransactionByBranch(ctx, id)
|
|
}
|
|
func (s *Service) UpdateShopTransactionVerified(ctx context.Context, id int64, verified bool, approvedBy int64, approverName string) error {
|
|
return s.transactionStore.UpdateShopTransactionVerified(ctx, id, verified, approvedBy, approverName)
|
|
}
|
|
|
|
func (s *Service) GetBranchByRole(ctx context.Context, branchID *int64, role domain.Role, userID int64, userCompanyID domain.ValidInt64) (*int64, *int64, error) {
|
|
// var branchID int64
|
|
// var companyID int64
|
|
|
|
if role == domain.RoleAdmin || role == domain.RoleBranchManager || role == domain.RoleSuperAdmin {
|
|
if branchID == nil {
|
|
// h.logger.Error("CashoutReq Branch ID is required for this user role")
|
|
return nil, nil, ErrBranchRequiredForRole
|
|
}
|
|
branch, err := s.branchSvc.GetBranchByID(ctx, *branchID)
|
|
if err != nil {
|
|
// h.logger.Error("CashoutReq no branches")
|
|
return nil, nil, ErrInvalidBetID
|
|
}
|
|
|
|
// Check if the user has access to the company
|
|
if role != domain.RoleSuperAdmin {
|
|
if !userCompanyID.Valid || userCompanyID.Value != branch.CompanyID {
|
|
return nil, nil, ErrUnauthorizedCompanyID
|
|
}
|
|
}
|
|
|
|
if role == domain.RoleBranchManager {
|
|
if branch.BranchManagerID != userID {
|
|
return nil, nil, ErrUnauthorizedBranchManager
|
|
}
|
|
}
|
|
|
|
return &branch.ID, &branch.CompanyID, nil
|
|
} else if role == domain.RoleCashier {
|
|
branch, err := s.branchSvc.GetBranchByCashier(ctx, userID)
|
|
if err != nil {
|
|
// h.logger.Error("CashoutReq failed, branch id invalid")
|
|
return nil, nil, ErrInvalidBranchID
|
|
}
|
|
return &branch.ID, &branch.CompanyID, nil
|
|
} else {
|
|
return nil, nil, ErrCustomerRoleNotAuthorized
|
|
}
|
|
}
|