diff --git a/go.mod b/go.mod index 91ace95..928f89b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module latipe-order-service-v2 -go 1.20 +go 1.21 + +toolchain go1.22.2 require ( github.com/Pacific73/gorm-cache v1.1.1 diff --git a/internal/infrastructure/persistence/order/gorm.go b/internal/infrastructure/persistence/order/gorm.go index 8336bc4..ffb4859 100644 --- a/internal/infrastructure/persistence/order/gorm.go +++ b/internal/infrastructure/persistence/order/gorm.go @@ -101,6 +101,7 @@ func (g GormRepository) FindAll(ctx context.Context, query *pagable.Query) ([]en Preload("OrderItem"). Preload("Delivery"). Where(whereState). + Order("created_at desc"). Limit(query.GetLimit()).Offset(query.GetOffset()). Find(&orders).Error }, ctx) @@ -152,6 +153,7 @@ func (g GormRepository) FindOrderByStoreID(ctx context.Context, storeId string, Where("orders.store_id=?", storeId). Where(whereState). Where(likeState). + Order("created_at desc"). Limit(query.GetLimit()).Offset(query.GetOffset()). Find(&orders).Error diff --git a/internal/services/commands/orderCmd/order_service.go b/internal/services/commands/orderCmd/order_service.go index a86d664..7bcc37a 100644 --- a/internal/services/commands/orderCmd/order_service.go +++ b/internal/services/commands/orderCmd/order_service.go @@ -13,6 +13,7 @@ import ( "latipe-order-service-v2/internal/domain/dto/order/store" "latipe-order-service-v2/internal/domain/entities/order" "latipe-order-service-v2/internal/domain/msgDTO" + "latipe-order-service-v2/internal/infrastructure/adapter/deliveryserv" "latipe-order-service-v2/internal/infrastructure/adapter/storeserv" dto2 "latipe-order-service-v2/internal/infrastructure/adapter/storeserv/dto" voucherConst "latipe-order-service-v2/internal/infrastructure/adapter/vouchersev/dto" @@ -41,7 +42,8 @@ type orderCommandService struct { deliveryGrpc deliverygrpc.DeliveryServiceClient userGrpc usergrpc.UserServiceClient //rest call - storeServ storeserv.Service + storeServ storeserv.Service + deliveryServ deliveryserv.Service } func NewOrderCommandService(cfg *config.Config, orderRepo order.OrderRepository, @@ -49,7 +51,7 @@ func NewOrderCommandService(cfg *config.Config, orderRepo order.OrderRepository, cacheEngine *cacheV9.CacheEngine, publisher *publishMsg.PublisherTransactionMessage, voucherGrpc vouchergrpc.VoucherServiceClient, productGrpc productgrpc.ProductServiceClient, deliveryGrpc deliverygrpc.DeliveryServiceClient, userGrpc usergrpc.UserServiceClient, notifyPub *publishMsg.NotificationMessagePublisher, - storeServ storeserv.Service) OrderCommandUsecase { + storeServ storeserv.Service, deliveryServ deliveryserv.Service) OrderCommandUsecase { return orderCommandService{ orderRepo: orderRepo, commissionRepo: commissionRepo, @@ -61,6 +63,7 @@ func NewOrderCommandService(cfg *config.Config, orderRepo order.OrderRepository, productGrpc: productGrpc, userGrpc: userGrpc, storeServ: storeServ, + deliveryServ: deliveryServ, notifyPub: notifyPub, } } @@ -358,6 +361,7 @@ func (o orderCommandService) StoreUpdateOrderStatus(ctx context.Context, dto *st } var bodyContent string + deliMessage := false switch dto.Status { case order.ORDER_PREPARED: @@ -365,6 +369,7 @@ func (o orderCommandService) StoreUpdateOrderStatus(ctx context.Context, dto *st return err } + deliMessage = true bodyContent = fmt.Sprintf("Xin chào, đơn hàng của bạn đã được cửa hàng xác nhận #[%v]", orderDAO.OrderID) case order.ORDER_CANCEL_BY_STORE: if err := o.orderRepo.UpdateStatus(ctx, orderDAO.OrderID, order.ORDER_CANCEL_BY_STORE, @@ -377,13 +382,21 @@ func (o orderCommandService) StoreUpdateOrderStatus(ctx context.Context, dto *st return errors.OrderCannotUpdate } - userNoti := msgDTO.NewNotificationMessage(msgDTO.NOTIFY_USER, orderDAO.UserId, "[Latipe] Thông báo tình trạng đơn hàng", bodyContent, orderDAO.Thumbnail) - //send message to user + userNoti := msgDTO.NewNotificationMessage(msgDTO.NOTIFY_USER, orderDAO.UserId, "[Latipe] Thông báo tình trạng đơn hàng", bodyContent, orderDAO.Thumbnail) if err := o.notifyPub.NotifyToUser(userNoti); err != nil { - return err + log.Error(err) + } + // send to deli + if deliMessage { + var err error + deliNoti := msgDTO.NewNotificationMessage(msgDTO.NOTIFY_USER, orderDAO.Delivery.DeliveryId, "[Latipe] Thông báo tình trạng đơn hàng", + fmt.Sprintf("Bạn có đơn hàng chờ vận chuyển [%v]", strings.ToUpper(orderDAO.OrderID)), + orderDAO.Thumbnail) + if err = o.notifyPub.NotifyToUser(deliNoti); err != nil { + log.Error(err) + } } - return nil } diff --git a/internal/wire_gen.go b/internal/wire_gen.go index 4ba6d0b..1449e0b 100644 --- a/internal/wire_gen.go +++ b/internal/wire_gen.go @@ -12,7 +12,6 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/adaptor" "github.com/gofiber/fiber/v2/middleware/basicauth" - "github.com/gofiber/fiber/v2/middleware/cors" healthcheck2 "github.com/gofiber/fiber/v2/middleware/healthcheck" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/monitor" @@ -78,11 +77,11 @@ func New() (*Server, error) { userServiceClient := usergrpc.NewUserServiceClientGRPCImpl(configConfig) notificationMessagePublisher := publisher.NewNotificationMessagePublisher(configConfig, connection) service := storeserv.NewStoreServiceAdapter(configConfig) - orderCommandUsecase := orderCmd.NewOrderCommandService(configConfig, orderRepository, commissionRepository, cacheV9CacheEngine, publisherTransactionMessage, voucherServiceClient, productServiceClient, deliveryServiceClient, userServiceClient, notificationMessagePublisher, service) + deliveryservService := deliveryserv.NewDeliServHttpAdapter(configConfig) + orderCommandUsecase := orderCmd.NewOrderCommandService(configConfig, orderRepository, commissionRepository, cacheV9CacheEngine, publisherTransactionMessage, voucherServiceClient, productServiceClient, deliveryServiceClient, userServiceClient, notificationMessagePublisher, service, deliveryservService) orderQueryUsecase := orderQuery.NewOrderQueryService(orderRepository) orderApiHandler := order2.NewOrderHandler(orderCommandUsecase, orderQueryUsecase) authservService := authserv.NewAuthServHttpAdapter(configConfig) - deliveryservService := deliveryserv.NewDeliServHttpAdapter(configConfig) authenticationMiddleware := auth.NewAuthMiddleware(authservService, service, deliveryservService, configConfig, cacheV9CacheEngine) middlewareMiddleware := middleware.NewMiddleware(authenticationMiddleware) adminOrderRouter := adminRouter.NewAdminOrderRouter(orderApiHandler, middlewareMiddleware) @@ -126,12 +125,6 @@ func NewServer( recoverConfig := recover2.ConfigDefault app.Use(recover2.New(recoverConfig)) - app.Use(cors.New(cors.Config{ - AllowOrigins: "http://127.0.0.1:5500, http://127.0.0.1:5173, http://localhost:5500, http://localhost:5173", - AllowHeaders: "*", - AllowMethods: "*", - })) - basicAuthConfig := basicauth.Config{ Users: map[string]string{ cfg.Metrics.Username: cfg.Metrics.Password,