Skip to content

Commit

Permalink
Add order validation and status update functionality
Browse files Browse the repository at this point in the history
Introduce a new order validation method to ensure orders are handled properly by delivery partners, including checks for acceptance time and cancellation status. Also, add an updateOrderStatus function to handle status changes in both orders and deliveries.
  • Loading branch information
mukulmantosh committed Sep 20, 2024
1 parent a3b919d commit 6ae38eb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/service/delivery/order_placement.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import (
"time"
)

func (deliverSrv *DeliveryService) updateOrderStatus(ctx context.Context, orderID int64, status string) error {
_, err := deliverSrv.db.Update(ctx, "orders", database.Filter{"order_status": status},
database.Filter{"order_id": orderID})
if err != nil {
return err
}
_, err = deliverSrv.db.Update(ctx, "deliveries", database.Filter{"delivery_status": status},
database.Filter{"order_id": orderID})
return nil
}

func (deliverSrv *DeliveryService) OrderPlacement(ctx context.Context,
deliveryPersonID int64, orderID int64, deliveryStatus string) (bool, error) {
var orderInfo order.Order
Expand All @@ -22,6 +33,12 @@ func (deliverSrv *DeliveryService) OrderPlacement(ctx context.Context,
return false, err
}

// Perform generic validation.
_, err = deliverSrv.orderValidation(ctx, &orderInfo, deliveryPersonID)
if err != nil {
return false, err
}

invalidStatuses := map[string]bool{
"cancelled": true,
"completed": true,
Expand Down
51 changes: 51 additions & 0 deletions pkg/service/delivery/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package delivery

import (
"Go_Food_Delivery/pkg/database/models/order"
"context"
"errors"
"time"
)

func (deliverSrv *DeliveryService) orderValidation(ctx context.Context, order *order.Order, deliveryPersonID int64) (bool, error) {
orderPlacedTime := order.CreatedAt
currentTime := time.Now()

var deliveryExists int64
var deliveryCancelCount int64
err := deliverSrv.db.Raw(ctx, &deliveryExists,
`SELECT COUNT(*) FROM deliveries WHERE order_id=? AND delivery_person_id=? AND delivery_status='on_the_way';`,
order.OrderID, deliveryPersonID)
if err != nil {
return false, err
}

err = deliverSrv.db.Raw(ctx, &deliveryCancelCount,
`SELECT COUNT(*) FROM deliveries WHERE order_id=? AND delivery_person_id=? AND delivery_status='cancelled';`,
order.OrderID, deliveryPersonID)
if err != nil {
return false, err
}

// If the order remains unclaimed by any delivery partner for more than 5 minutes, it will be canceled.
if order.OrderStatus == "in_progress" {
if currentTime.Sub(orderPlacedTime) > 5*time.Minute {
_ = deliverSrv.updateOrderStatus(ctx, order.OrderID, "cancelled")
return false, errors.New("this order was not accepted by the restaurant " +
"for more than 5 minutes. It has been cancelled")
}
}

//If a delivery partner accepts an order, it should no longer be visible to other delivery partners.
if order.OrderStatus == "on_the_way" && deliveryExists == 0 {
return false, errors.New("this order was already accepted by another delivery partner")
}
// If a delivery partner cancels an order, it should no longer be visible to them but will remain visible to other delivery partners.
if deliveryCancelCount == 1 {
return false, errors.New("you won't be able to accept this order again. It has been cancelled")
}

// If a delivery partner cancels three or more orders within an hour of accepting them, they will be blocked from receiving orders for three hours.

return true, nil
}

0 comments on commit 6ae38eb

Please sign in to comment.