182 lines
5.6 KiB
Go
182 lines
5.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/ports"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
// Interface for creating new shop transaction store
|
|
func NewTransactionStore(s *Store) ports.TransactionStore { return s }
|
|
|
|
func (s *Store) CreateShopTransaction(ctx context.Context, transaction domain.CreateShopTransaction) (domain.ShopTransaction, error) {
|
|
|
|
newTransaction, err := s.queries.CreateShopTransaction(ctx, domain.ConvertCreateShopTransaction(transaction))
|
|
if err != nil {
|
|
return domain.ShopTransaction{}, err
|
|
}
|
|
return domain.ConvertDBShopTransaction(newTransaction), err
|
|
}
|
|
|
|
func (s *Store) GetShopTransactionByID(ctx context.Context, id int64) (domain.ShopTransactionDetail, error) {
|
|
transaction, err := s.queries.GetShopTransactionByID(ctx, id)
|
|
if err != nil {
|
|
return domain.ShopTransactionDetail{}, err
|
|
}
|
|
|
|
return domain.ConvertDBShopTransactionDetail(transaction), nil
|
|
}
|
|
|
|
func (s *Store) GetAllShopTransactions(ctx context.Context, filter domain.ShopTransactionFilter) ([]domain.ShopTransactionDetail, error) {
|
|
transaction, err := s.queries.GetAllShopTransactions(ctx, dbgen.GetAllShopTransactionsParams{
|
|
BranchID: filter.BranchID.ToPG(),
|
|
CompanyID: filter.CompanyID.ToPG(),
|
|
UserID: filter.UserID.ToPG(),
|
|
Query: filter.Query.ToPG(),
|
|
CreatedBefore: filter.CreatedBefore.ToPG(),
|
|
CreatedAfter: filter.CreatedAfter.ToPG(),
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result []domain.ShopTransactionDetail = make([]domain.ShopTransactionDetail, 0, len(transaction))
|
|
for _, tAction := range transaction {
|
|
result = append(result, domain.ConvertDBShopTransactionDetail(tAction))
|
|
}
|
|
return result, nil
|
|
}
|
|
func (s *Store) GetShopTransactionByBranch(ctx context.Context, id int64) ([]domain.ShopTransactionDetail, error) {
|
|
transaction, err := s.queries.GetShopTransactionByBranch(ctx, id)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result []domain.ShopTransactionDetail = make([]domain.ShopTransactionDetail, 0, len(transaction))
|
|
for _, ticket := range transaction {
|
|
result = append(result, domain.ConvertDBShopTransactionDetail(ticket))
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Store) UpdateShopTransactionVerified(ctx context.Context, id int64, verified bool, approvedBy int64) error {
|
|
err := s.queries.UpdateShopTransactionVerified(ctx, dbgen.UpdateShopTransactionVerifiedParams{
|
|
ID: id,
|
|
ApprovedBy: pgtype.Int8{
|
|
Int64: approvedBy,
|
|
Valid: true,
|
|
},
|
|
Verified: verified,
|
|
})
|
|
return err
|
|
}
|
|
|
|
// GetTransactionTotals returns total deposits and withdrawals
|
|
func (s *Store) GetTransactionTotals(ctx context.Context, filter domain.ReportFilter) (deposits, withdrawals domain.Currency, err error) {
|
|
query := `SELECT
|
|
COALESCE(SUM(CASE WHEN type = 1 THEN amount ELSE 0 END), 0) as deposits,
|
|
COALESCE(SUM(CASE WHEN type = 0 THEN amount ELSE 0 END), 0) as withdrawals
|
|
FROM shop_transactions`
|
|
|
|
args := []interface{}{}
|
|
argPos := 1
|
|
|
|
if filter.CompanyID.Valid {
|
|
query += fmt.Sprintf(" WHERE company_id = $%d", argPos)
|
|
args = append(args, filter.CompanyID.Value)
|
|
argPos++
|
|
} else if filter.BranchID.Valid {
|
|
query += fmt.Sprintf(" WHERE branch_id = $%d", argPos)
|
|
args = append(args, filter.BranchID.Value)
|
|
argPos++
|
|
} else if filter.UserID.Valid {
|
|
query += fmt.Sprintf(" WHERE user_id = $%d", argPos)
|
|
args = append(args, filter.UserID.Value)
|
|
argPos++
|
|
}
|
|
|
|
if filter.StartTime.Valid {
|
|
query += fmt.Sprintf(" AND %screated_at >= $%d", func() string {
|
|
if len(args) == 0 {
|
|
return ""
|
|
}
|
|
return " "
|
|
}(), argPos)
|
|
args = append(args, filter.StartTime.Value)
|
|
argPos++
|
|
}
|
|
if filter.EndTime.Valid {
|
|
query += fmt.Sprintf(" AND created_at <= $%d", argPos)
|
|
args = append(args, filter.EndTime.Value)
|
|
argPos++
|
|
}
|
|
|
|
row := s.conn.QueryRow(ctx, query, args...)
|
|
err = row.Scan(&deposits, &withdrawals)
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("failed to get transaction totals: %w", err)
|
|
}
|
|
return deposits, withdrawals, nil
|
|
}
|
|
|
|
// GetBranchTransactionTotals returns transaction totals by branch
|
|
func (s *Store) GetBranchTransactionTotals(ctx context.Context, filter domain.ReportFilter) (map[int64]domain.BranchTransactions, error) {
|
|
query := `SELECT
|
|
branch_id,
|
|
COALESCE(SUM(CASE WHEN type = 1 THEN amount ELSE 0 END), 0) as deposits,
|
|
COALESCE(SUM(CASE WHEN type = 0 THEN amount ELSE 0 END), 0) as withdrawals
|
|
FROM shop_transactions`
|
|
|
|
args := []interface{}{}
|
|
argPos := 1
|
|
|
|
if filter.CompanyID.Valid {
|
|
query += fmt.Sprintf(" WHERE company_id = $%d", argPos)
|
|
args = append(args, filter.CompanyID.Value)
|
|
argPos++
|
|
}
|
|
if filter.StartTime.Valid {
|
|
query += fmt.Sprintf(" AND %screated_at >= $%d", func() string {
|
|
if len(args) == 0 {
|
|
return " WHERE "
|
|
}
|
|
return " AND "
|
|
}(), argPos)
|
|
args = append(args, filter.StartTime.Value)
|
|
argPos++
|
|
}
|
|
if filter.EndTime.Valid {
|
|
query += fmt.Sprintf(" AND created_at <= $%d", argPos)
|
|
args = append(args, filter.EndTime.Value)
|
|
argPos++
|
|
}
|
|
|
|
query += " GROUP BY branch_id"
|
|
|
|
rows, err := s.conn.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to query branch transaction totals: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
totals := make(map[int64]domain.BranchTransactions)
|
|
for rows.Next() {
|
|
var branchID int64
|
|
var transactions domain.BranchTransactions
|
|
if err := rows.Scan(&branchID, &transactions.Deposits, &transactions.Withdrawals); err != nil {
|
|
return nil, fmt.Errorf("failed to scan branch transaction totals: %w", err)
|
|
}
|
|
totals[branchID] = transactions
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("rows error: %w", err)
|
|
}
|
|
return totals, nil
|
|
}
|