From 38dffb7169650edce1acf0d524f3e8e385d118c9 Mon Sep 17 00:00:00 2001 From: Arieflazuardii Date: Mon, 18 Dec 2023 08:44:21 +0700 Subject: [PATCH] feat(transaction): add print receipt fix limit recent transaction and add new feature --- handler/transaction.go | 20 ++++++++++++++++++-- repository/productRepository.go | 3 ++- routes/transaction.go | 1 + utils/response/transactionConvertResponse.go | 4 ++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/handler/transaction.go b/handler/transaction.go index 75b45f1..8c300a7 100644 --- a/handler/transaction.go +++ b/handler/transaction.go @@ -25,6 +25,7 @@ type TransactionHandler interface { GetRecentTransactionsRealtimeHandler(ctx echo.Context) error GetTransactionHandler(ctx echo.Context) error GetTransactionsHandler(ctx echo.Context) error + PrintReceiptTransactionHandler(ctx echo.Context) error GetRecentTransactionsHandler(ctx echo.Context) error GetTransactionMonthlyHandler(ctx echo.Context) error GetTransactionYearlyHandler(ctx echo.Context) error @@ -119,6 +120,21 @@ func (c *TransactionHandlerImpl) GetTransactionHandler(ctx echo.Context) error { return ctx.JSON(http.StatusOK, helpers.SuccessResponse("Successfully Get Data Transaction", response)) } +func (c *TransactionHandlerImpl) PrintReceiptTransactionHandler(ctx echo.Context) error { + transactionInvoice := ctx.Param("invoice") + result, err := c.TransactionService.FindByInvoice(transactionInvoice) + if err != nil { + if strings.Contains(err.Error(), "transaction not found") { + return ctx.JSON(http.StatusNotFound, helpers.ErrorResponse("transaction not found")) + } + logrus.Error(err.Error()) + return ctx.JSON(http.StatusInternalServerError, helpers.ErrorResponse("Get transaction data error")) + } + response := res.TransactionDomainToTransactionResponse(result) + + return ctx.JSON(http.StatusOK, helpers.SuccessResponse("Successfully Get Data Transaction", response)) +} + func (c *TransactionHandlerImpl) GetTransactionsHandler(ctx echo.Context) error { transactions, totalTransactions, err := c.TransactionService.FindAllTransaction() if err != nil { @@ -190,11 +206,11 @@ func (c *TransactionHandlerImpl) GetRecentTransactionsHandler(ctx echo.Context) return ctx.JSON(http.StatusNotFound, helpers.ErrorResponse("transaction not found")) } logrus.Error(err.Error()) - return ctx.JSON(http.StatusInternalServerError, helpers.ErrorResponse("Get all transaction error")) + return ctx.JSON(http.StatusInternalServerError, helpers.ErrorResponse("Get Recent transaction error")) } response := res.ConvertTransactionResponse(transactions) - return ctx.JSON(http.StatusOK, helpers.SuccessResponse("Successfully Get Data Transaction", response)) + return ctx.JSON(http.StatusOK, helpers.SuccessResponse("Successfully Get Data Recent Transaction", response)) } diff --git a/repository/productRepository.go b/repository/productRepository.go index 4bbe087..504bd13 100644 --- a/repository/productRepository.go +++ b/repository/productRepository.go @@ -187,7 +187,8 @@ func (repository *ProductRepositoryImpl) FindBestSellingProduct() ([]domain.Best GROUP BY p.id, p.name, p.image, pd.price, pt.type_name, pd.size ORDER BY - total_quantity DESC; + total_quantity DESC + LIMIT 3; ` result := repository.DB.Raw(query).Preload("ProductDetail", "deleted_at IS NULL").Scan(&bestProduct) if result.Error != nil { diff --git a/routes/transaction.go b/routes/transaction.go index 3a11d05..72950f2 100644 --- a/routes/transaction.go +++ b/routes/transaction.go @@ -33,6 +33,7 @@ func TransactionRoutes(e *echo.Echo, db *gorm.DB, midtransCoreApi midtrans.Midtr transactionGroup.GET("s/cashier", transactionHandler.GetCashierTransactionsHandler, middleware.AuthMiddleware("Cashier")) transactionGroup.GET("s/membership/:id", transactionHandler.GetMembershipTransactionsHandler, middleware.AuthMiddleware("Cashier")) transactionGroup.GET("/:id", transactionHandler.GetTransactionHandler) + transactionGroup.GET("/receipt/:invoice", transactionHandler.PrintReceiptTransactionHandler) transactionGroup.GET("/realtime", transactionHandler.GetTransactionStatusRealtime) transactionGroup.GET("s/recent/realtime", transactionHandler.GetRecentTransactionsRealtimeHandler) transactionGroup.GET("s", transactionHandler.GetTransactionsHandler) diff --git a/utils/response/transactionConvertResponse.go b/utils/response/transactionConvertResponse.go index 95bab9e..b12b009 100644 --- a/utils/response/transactionConvertResponse.go +++ b/utils/response/transactionConvertResponse.go @@ -11,7 +11,7 @@ func TransactionDomainToTransactionResponse(transaction *domain.Transaction) *we updatedAt := transaction.UpdatedAt response := &web.TransactionResponse{ ID: transaction.ID, - CreatedAt: createdAt.Format("2006-01-02 15:04:05"), + CreatedAt: createdAt.Format("2006-January-02 15:04:05"), UpdatedAt: updatedAt.Format("2006-01-02 15:04:05"), Cashier: web.CashierTransactionResponse{ ID: transaction.Cashier.ID, @@ -105,7 +105,7 @@ func TransactionDomainToTransactionResponseNoMembership(transaction *domain.Tran createdAt := transaction.CreatedAt response := &web.TransactionResponse{ ID: transaction.ID, - CreatedAt: createdAt.Format("2006-01-02 15:04:05"), + CreatedAt: createdAt.Format("2006-January-02 15:04:05"), Cashier: web.CashierTransactionResponse{ ID: transaction.Cashier.ID, Fullname: transaction.Cashier.Fullname,