updates on the sweeper and eligibility things in the order
This commit is contained in:
@@ -2967,6 +2967,8 @@ func AdminCancelBooking(c *fiber.Ctx) error {
|
||||
"booking_id", booking.Bookingid, "error", err)
|
||||
}
|
||||
|
||||
closeOpenAssignments(booking.Bookingid, "Order cancelled by Doormile operations")
|
||||
|
||||
if booking.Assignedmileruserid != nil {
|
||||
db.DB.Model(&models.MilerProfile{}).
|
||||
Where("userid = ?", *booking.Assignedmileruserid).
|
||||
@@ -3055,6 +3057,8 @@ func AdminBulkCancelBookings(c *fiber.Ctx) error {
|
||||
"booking_id", booking.Bookingid, "error", err)
|
||||
}
|
||||
|
||||
closeOpenAssignments(booking.Bookingid, "Order cancelled by Doormile operations (bulk)")
|
||||
|
||||
if booking.Assignedmileruserid != nil {
|
||||
db.DB.Model(&models.MilerProfile{}).
|
||||
Where("userid = ?", *booking.Assignedmileruserid).
|
||||
@@ -4118,3 +4122,20 @@ func opsActorID(c *fiber.Ctx) *int {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// closeOpenAssignments closes the rider's open assignment on a booking ops
|
||||
// cancelled. Without it the record stayed Assigned/Accepted for good, and
|
||||
// auto-assignment counted it against the rider's cap — enough cancelled
|
||||
// orders and the rider was never offered another. Best effort: the
|
||||
// cancellation itself has already been saved.
|
||||
func closeOpenAssignments(bookingID int, remark string) {
|
||||
if err := db.DB.Model(&models.BookingAssignment{}).
|
||||
Where("bookingid = ? AND assignmentstatus IN ?", bookingID,
|
||||
[]string{constants.AssignmentAssigned, constants.AssignmentAccepted}).
|
||||
Updates(map[string]interface{}{
|
||||
"assignmentstatus": constants.AssignmentCancelled,
|
||||
"remarks": remark,
|
||||
}).Error; err != nil {
|
||||
utils.Error("cancel: could not close the rider's assignment", "booking_id", bookingID, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
35
controllers/cancel_frees_rider_pg_test.go
Normal file
35
controllers/cancel_frees_rider_pg_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"doormile/constants"
|
||||
"doormile/models"
|
||||
)
|
||||
|
||||
// Cancelling an order from the console must close the rider's assignment on
|
||||
// it. It used to stay Assigned/Accepted for good, and auto-assignment counted
|
||||
// it against the rider's cap — enough cancelled orders and the rider was never
|
||||
// offered another. Uses rtoTestDB (consignmentReturn_pg_test.go): skipped
|
||||
// unless REGISTRY_TEST_DSN points at a throwaway database.
|
||||
func TestCancelClosesTheRidersAssignment(t *testing.T) {
|
||||
gdb := rtoTestDB(t)
|
||||
rider := 38
|
||||
must(t, gdb.Create(&models.PickupBooking{Bookingid: 71, Bookingno: "DM-T71", Status: constants.BookingMilerAssigned,
|
||||
Assignedmileruserid: &rider}).Error)
|
||||
must(t, gdb.Create(&models.BookingAssignment{Bookingid: 71, Mileruserid: rider, Assignmentstatus: constants.AssignmentAccepted}).Error)
|
||||
// A closed assignment on the same booking must be left as it is.
|
||||
must(t, gdb.Create(&models.BookingAssignment{Bookingid: 71, Mileruserid: 21, Assignmentstatus: constants.AssignmentRejected}).Error)
|
||||
|
||||
closeOpenAssignments(71, "Order cancelled by Doormile operations")
|
||||
|
||||
var rows []models.BookingAssignment
|
||||
must(t, gdb.Where("bookingid = ?", 71).Order("mileruserid").Find(&rows).Error)
|
||||
got := map[int]string{}
|
||||
for _, r := range rows {
|
||||
got[r.Mileruserid] = r.Assignmentstatus
|
||||
}
|
||||
if got[38] != constants.AssignmentCancelled || got[21] != constants.AssignmentRejected {
|
||||
t.Fatalf("assignments after cancel = %v", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user