fix: miler assignment lifecycle, pickup hub resolution, and hyperlocal delivery

Bugs found during live Coimbatore testing against production:

- GetMilerAssignments returned every assignment ever made to a miler with
  no status filter, so weeks-old Rejected assignments still showed up as
  actionable in the app. Now filters to Assigned/Accepted only.
- AcceptMilerAssignment overwrote assignmentstatus unconditionally, letting
  a stale Rejected assignment be silently reactivated (and pushing its
  booking back to Pickup_Scheduled). Now 400s unless currently Assigned,
  reporting the actual status.
- RejectMilerAssignment read `reason` from the query string instead of the
  JSON body, contradicting the API contract and every sibling endpoint.
- BookingPickupComplete resolved the origin hub via db.First(&hub) with no
  Where clause — i.e. the lowest hub ID in the table, unrelated to the
  booking or miler. Now uses the miler's own MilerProfile.Hubid, falling
  back to the old behaviour with a warning only when unassigned.
- No code path ever set a consignment to Out_for_Delivery, making
  MilerDeliverConsignment unreachable. BookingPickupComplete now goes
  straight to Out_for_Delivery when pickup and delivery pincodes share a
  3-digit postal-area prefix (same-miler hyperlocal), reusing the
  hubPincodePrefix convention. Cross-hub still lands at Inwarded_at_Hub.

Also allow https://app.doormile.com in CORS, and drop a stray Windows-path
log file that was committed by accident.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Suriya
2026-07-27 11:04:01 +05:30
parent 6b3c95c259
commit a2b9268189
3 changed files with 39 additions and 45 deletions

View File

@@ -283,7 +283,9 @@ func GetMilerAssignments(c *fiber.Ctx) error {
milerUserID := c.Locals("userid").(int)
var assignments []models.BookingAssignment
if err := db.DB.Where("mileruserid = ?", milerUserID).Order("assignedat DESC").Find(&assignments).Error; err != nil {
if err := db.DB.Where("mileruserid = ? AND assignmentstatus IN ?", milerUserID,
[]string{constants.AssignmentAssigned, constants.AssignmentAccepted}).
Order("assignedat DESC").Find(&assignments).Error; err != nil {
return utils.Internal(c, "failed to fetch assignments")
}
@@ -326,6 +328,11 @@ func AcceptMilerAssignment(c *fiber.Ctx) error {
return utils.NotFound(c, "assignment not found")
}
if assignment.Assignmentstatus != constants.AssignmentAssigned {
tx.Rollback()
return utils.BadRequest(c, fmt.Sprintf("assignment is not pending acceptance (current status: %s)", assignment.Assignmentstatus))
}
now := time.Now()
assignment.Assignmentstatus = constants.AssignmentAccepted
assignment.Acceptedat = &now
@@ -367,6 +374,16 @@ func RejectMilerAssignment(c *fiber.Ctx) error {
return utils.BadRequest(c, "invalid assignment ID")
}
var req struct {
Reason string `json:"reason"`
}
if err := c.BodyParser(&req); err != nil {
return utils.BadRequest(c, "invalid request body")
}
if req.Reason == "" {
req.Reason = "Rejected by rider"
}
tx := db.DB.Begin()
var ba models.BookingAssignment
@@ -376,7 +393,7 @@ func RejectMilerAssignment(c *fiber.Ctx) error {
}
ba.Assignmentstatus = constants.AssignmentRejected
ba.Remarks = c.Query("reason", "Rejected by rider")
ba.Remarks = req.Reason
tx.Save(&ba)
var booking models.PickupBooking
@@ -581,9 +598,23 @@ func BookingPickupComplete(c *fiber.Ctx) error {
trackingNo := generateTrackingNo()
var defaultHubID *int
var hub models.Hub
if tx.First(&hub).Error == nil {
defaultHubID = &hub.Hubid
if profile.Hubid != nil {
defaultHubID = profile.Hubid
} else {
utils.Warn("BookingPickupComplete: miler has no assigned hub, falling back to first hub row", "miler_user_id", milerUserID, "booking_id", bookingID)
var hub models.Hub
if tx.First(&hub).Error == nil {
defaultHubID = &hub.Hubid
}
}
// Hyperlocal shortcut: pickup and delivery in the same postal area mean
// no hub-to-hub tripsheet leg is needed, so the same miler goes straight
// to final-mile delivery instead of parking the consignment at the hub.
consignmentStatus := constants.ConsignmentInwardedAtHub
if len(booking.Pickuppincode) >= 3 && len(booking.Deliverypincode) >= 3 &&
booking.Pickuppincode[:3] == booking.Deliverypincode[:3] {
consignmentStatus = constants.ConsignmentOutForDelivery
}
consignment := models.Consignment{
@@ -602,7 +633,7 @@ func BookingPickupComplete(c *fiber.Ctx) error {
Volumetricweight: totalChargeable - totalDead,
Chargeableweight: totalChargeable,
Paymentmode: "Prepaid",
Status: constants.ConsignmentInwardedAtHub,
Status: consignmentStatus,
Estimateddeliveryat: nil,
Createdby: milerUserID,
Originhubid: defaultHubID,
@@ -632,7 +663,7 @@ func BookingPickupComplete(c *fiber.Ctx) error {
Consignmentid: consignment.Consignmentid,
Hubid: defaultHubID,
Userid: &milerUserID,
Eventstatus: constants.ConsignmentInwardedAtHub,
Eventstatus: consignmentStatus,
Remarks: "Package collected by miler and converted to consignment",
}
tx.Create(&history)