diff --git a/backend/docs/docs.go b/backend/docs/docs.go index 08b0111f..c9896522 100644 --- a/backend/docs/docs.go +++ b/backend/docs/docs.go @@ -571,6 +571,13 @@ const docTemplate = `{ "in": "path", "required": true }, + { + "type": "string", + "description": "Transaction type (e,g, '0', '1')", + "name": "transaction_type", + "in": "path", + "required": true + }, { "type": "string", "description": "Time range for the query (e.g., '24h', ", diff --git a/backend/docs/swagger.json b/backend/docs/swagger.json index 7c9cf654..f5d7da30 100644 --- a/backend/docs/swagger.json +++ b/backend/docs/swagger.json @@ -559,6 +559,13 @@ "in": "path", "required": true }, + { + "type": "string", + "description": "Transaction type (e,g, '0', '1')", + "name": "transaction_type", + "in": "path", + "required": true + }, { "type": "string", "description": "Time range for the query (e.g., '24h', ", diff --git a/backend/docs/swagger.yaml b/backend/docs/swagger.yaml index 164d22ed..cb56a49b 100644 --- a/backend/docs/swagger.yaml +++ b/backend/docs/swagger.yaml @@ -805,6 +805,11 @@ paths: name: asset_id required: true type: integer + - description: Transaction type (e,g, '0', '1') + in: path + name: transaction_type + required: true + type: string - description: 'Time range for the query (e.g., ''24h'', ' in: path name: time_range diff --git a/backend/internal/controller/http/v1/log_transaction.go b/backend/internal/controller/http/v1/log_transaction.go index 6aa0faef..850ad0cd 100644 --- a/backend/internal/controller/http/v1/log_transaction.go +++ b/backend/internal/controller/http/v1/log_transaction.go @@ -26,7 +26,7 @@ func newLogTransactionsRoutes(handler *gin.RouterGroup, w usecase.WalletUseCase, h.GET("/assets/:asset_id/:time_range", r.getLogTransactionsByAssetID) h.GET("/user/:user_id/:time_range", r.getLogTransactionsByUserID) h.GET("/transaction_type/:transaction_type_id/:time_range", r.getLogTransactionsByTransactionTypeID) - h.GET("/assets/:asset_id/sum/:time_range/:time_frame", r.sumAmountsByAssetID) + h.GET("/assets/:asset_id/type/:transaction_type_id/sum/:time_range/:time_frame", r.sumAmountsByAssetID) h.GET("/assets/sum/:time_range/:time_frame", r.sumAmountsForAllAssets) } } @@ -142,6 +142,7 @@ func (r *logTransactionsRoutes) getLogTransactionsByTransactionTypeID(c *gin.Con // @Accept json // @Produce json // @Param asset_id path int true "Asset ID" +// @Param transaction_type path string true "Transaction type (e,g, '0', '1')" // @Param time_range path string true "Time range for the query (e.g., '24h', "7d")" // @Param time_frame path string false "Time frame for the query (e.g., '1h')". // @Security ApiKeyAuth @@ -151,9 +152,20 @@ func (r *logTransactionsRoutes) getLogTransactionsByTransactionTypeID(c *gin.Con // @Router /log_transactions/asset/{asset_id}/sum/{time_range}/{time_frame} [get] func (r *logTransactionsRoutes) sumAmountsByAssetID(c *gin.Context) { assetIDStr := c.Param("asset_id") + transactionTypeStr := c.Param("transaction_type_id") timeRange := c.Param("time_range") timeFrame := c.Param("time_frame") + if transactionTypeStr == "" { + transactionTypeStr = "0" + } + + // Convert transactionTypeStr to integer + transactionType, err := strconv.Atoi(transactionTypeStr) + if err != nil { + errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid transaction type: %s", err.Error()), err) + return + } duration, err := time.ParseDuration(timeFrame) if err != nil { errorResponse(c, http.StatusBadRequest, "Invalid time_frame format", err) @@ -166,7 +178,7 @@ func (r *logTransactionsRoutes) sumAmountsByAssetID(c *gin.Context) { return } - sum, err := r.l.SumLogTransactionsByAssetID(assetID, timeRange, duration) + sum, err := r.l.SumLogTransactionsByAssetID(assetID, timeRange, duration, transactionType) if err != nil { errorResponse(c, http.StatusInternalServerError, fmt.Sprintf("error getting log transactions: %s", err.Error()), err) return diff --git a/backend/internal/usecase/interfaces.go b/backend/internal/usecase/interfaces.go index d4e80017..8f193ab3 100644 --- a/backend/internal/usecase/interfaces.go +++ b/backend/internal/usecase/interfaces.go @@ -85,7 +85,7 @@ type ( GetLogTransactionsByAssetID(assetID int, timeRange string) ([]entity.LogTransaction, error) GetLogTransactionsByUserID(userID int, timeRange string) ([]entity.LogTransaction, error) GetLogTransactionsByTransactionTypeID(transactionTypeID int, timeRange string) ([]entity.LogTransaction, error) - SumLogTransactionsByAssetID(assetID int, timeRange string, timeFrame time.Duration) (entity.SumLogTransaction, error) + SumLogTransactionsByAssetID(assetID int, timeRange string, timeFrame time.Duration, transactionType int) (entity.SumLogTransaction, error) SumLogTransactions(timeRange string, timeFrame time.Duration) ([]entity.SumLogTransaction, error) } ) diff --git a/backend/internal/usecase/log_transaction.go b/backend/internal/usecase/log_transaction.go index 1f6628fe..60777760 100644 --- a/backend/internal/usecase/log_transaction.go +++ b/backend/internal/usecase/log_transaction.go @@ -62,8 +62,8 @@ func (l *LogTransactionUseCase) SumLogTransactions(timeRange string, timeFrame t return sum, nil } -func (l *LogTransactionUseCase) SumLogTransactionsByAssetID(assetID int, timeRange string, duration time.Duration) (entity.SumLogTransaction, error) { - sum, err := l.lRepo.SumLogTransactionsByAssetID(assetID, timeRange, duration) +func (l *LogTransactionUseCase) SumLogTransactionsByAssetID(assetID int, timeRange string, duration time.Duration, transactionType int) (entity.SumLogTransaction, error) { + sum, err := l.lRepo.SumLogTransactionsByAssetID(assetID, timeRange, duration, transactionType) if err != nil { return entity.SumLogTransaction{}, err } diff --git a/backend/internal/usecase/repo/log_transaction_postgres.go b/backend/internal/usecase/repo/log_transaction_postgres.go index dccb7a39..f731fc48 100644 --- a/backend/internal/usecase/repo/log_transaction_postgres.go +++ b/backend/internal/usecase/repo/log_transaction_postgres.go @@ -44,7 +44,7 @@ func (repo *LogTransactionRepo) GetLogTransactionsByTransactionTypeID(transactio return getLogTransactions(repo, timeRange, "WHERE transaction_type_id = $2") } -func (repo *LogTransactionRepo) SumLogTransactionsByAssetID(assetID int, timeRange string, timeFrame time.Duration) (entity.SumLogTransaction, error) { +func (repo *LogTransactionRepo) SumLogTransactionsByAssetID(assetID int, timeRange string, timeFrame time.Duration, transactionType int) (entity.SumLogTransaction, error) { dateFilter, err := getDateFilter(timeRange) if err != nil { return entity.SumLogTransaction{}, err @@ -53,18 +53,33 @@ func (repo *LogTransactionRepo) SumLogTransactionsByAssetID(assetID int, timeRan timeFrameUnit := getTimeFrame(timeFrame) timeFrameSeconds := timeFrame.Seconds() - query := ` + baseQuery := ` SELECT a.id, a.name, a.code, a.asset_type, SUM(lt.amount), DATE_TRUNC($3, TIMESTAMP 'epoch' + INTERVAL '1 second' * floor(EXTRACT(EPOCH FROM lt.date)/$4) * $4) as dateFrame FROM logtransactions AS lt JOIN asset AS a ON lt.asset_id = a.id - WHERE lt.date >= $1 AND lt.asset_id = $2 - GROUP BY a.id, dateFrame + ` + + var whereClause string + var queryArgs []interface{} + + if transactionType == 0 { + whereClause = "WHERE lt.date >= $1 AND lt.asset_id = $2" + queryArgs = append(queryArgs, dateFilter, assetID, timeFrameUnit, timeFrameSeconds) + } else { + whereClause = "WHERE lt.date >= $1 AND lt.asset_id = $2 AND lt.transaction_type_id = $5" + queryArgs = append(queryArgs, dateFilter, assetID, timeFrameUnit, timeFrameSeconds, transactionType) + } + + groupAndOrderClause := ` + GROUP BY a.id, dateFrame ORDER BY a.id, dateFrame; - ` + ` + + query := baseQuery + whereClause + groupAndOrderClause - rows, err := repo.Db.Query(query, dateFilter, assetID, timeFrameUnit, timeFrameSeconds) + rows, err := repo.Db.Query(query, queryArgs...) if err != nil { return entity.SumLogTransaction{}, err } @@ -198,7 +213,7 @@ func getDateFilter(timeRange string) (time.Time, error) { if value == 1 { return time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentTime.Location()), nil } - // For other values, adjust the date accordingly. + targetDate := currentTime.AddDate(0, 0, -value+1) return time.Date(targetDate.Year(), targetDate.Month(), targetDate.Day(), 0, 0, 0, 0, targetDate.Location()), nil default: