Skip to content

Commit

Permalink
Refactor delivery status notification and enhance event subscriptions
Browse files Browse the repository at this point in the history
Refactor the delivery status notification logic into a separate function for better readability and maintainability. Also, add event subscription for multiple order-related events in the notification service.
  • Loading branch information
mukulmantosh committed Sep 19, 2024
1 parent e086144 commit 2cdcaf6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func main() {

// Notification
notifyService := notification.NewNotificationService(db, env, natServer)

// Subscribe to multiple events.
_ = notifyService.SubscribeNewOrders(wsClients)
_ = notifyService.SubscribeOrderStatus(wsClients)

Expand Down
30 changes: 26 additions & 4 deletions pkg/service/delivery/order_placement.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ func (deliverSrv *DeliveryService) OrderPlacement(ctx context.Context,
if err != nil {
return false, err
}

message := fmt.Sprintf("USER_ID:%d|MESSAGE:The current status of your order is %s", orderInfo.UserID, deliveryStatus)
topic := fmt.Sprintf("orders.status.%d", orderInfo.OrderID)
err = deliverSrv.nats.Pub(topic, []byte(message))
// Notify User.
err = deliverSrv.notifyDeliveryStatusToUser(&orderInfo, deliveryStatus)
if err != nil {
return false, err
}
Expand All @@ -69,3 +67,27 @@ func (deliverSrv *DeliveryService) OrderPlacement(ctx context.Context,
}

}

func (deliverSrv *DeliveryService) notifyDeliveryStatusToUser(order *order.Order, status string) error {
var message string

switch status {
case "delivered":
message = fmt.Sprintf("USER_ID:%d|MESSAGE:Your order no.%d has been successfully %s", order.UserID, order.OrderID, status)
case "failed":
message = fmt.Sprintf("USER_ID:%d|MESSAGE:Your order no.%d has been %s", order.UserID, order.OrderID, status)
case "cancelled":
message = fmt.Sprintf("USER_ID:%d|MESSAGE:Your order no.%d has been %s", order.UserID, order.OrderID, status)
case "on_the_way":
message = fmt.Sprintf("USER_ID:%d|MESSAGE:Your order no.%d is %s", order.UserID, order.OrderID, status)
default:
return fmt.Errorf("invalid status: %s", status)
}

topic := fmt.Sprintf("orders.status.%d", order.OrderID)
err := deliverSrv.nats.Pub(topic, []byte(message))
if err != nil {
return err
}
return nil
}

0 comments on commit 2cdcaf6

Please sign in to comment.