updates on the customercontroller and the booking.go updates

This commit is contained in:
2026-09-02 17:52:36 +05:30
parent d12629a1e4
commit 35675d8a9b
4 changed files with 196 additions and 6 deletions

View File

@@ -573,6 +573,52 @@ func CreateCustomerBooking(c *fiber.Ctx) error {
return utils.Created(c, booking)
}
// fillConsignmentFacts attaches the consignment's tracking number and live
// status to each booking that has one.
//
// A booking freezes at Converted_To_Consignment the moment it is collected,
// while the parcel keeps moving on the consignment — so without this a customer
// sees a status that stopped updating the instant their parcel was picked up.
//
// The tracking number matters more: GET /customer/track/:trackingno is keyed on
// it, and nothing else the customer app can read exposes it. Tracking was
// unreachable from the app not because the endpoint was missing but because the
// number never travelled to it.
//
// Batched — one query for the whole page, not one per row.
func fillConsignmentFacts(bookings []models.PickupBooking) {
ids := make([]int, 0, len(bookings))
for _, b := range bookings {
if b.Consignmentid != nil {
ids = append(ids, *b.Consignmentid)
}
}
if len(ids) == 0 {
return
}
var consignments []models.Consignment
if err := db.DB.Select("consignmentid, trackingno, status").
Where("consignmentid IN ?", ids).Find(&consignments).Error; err != nil {
utils.Warn("fillConsignmentFacts: could not load consignments", "error", err)
return
}
byID := make(map[int]models.Consignment, len(consignments))
for _, cn := range consignments {
byID[cn.Consignmentid] = cn
}
for i := range bookings {
if bookings[i].Consignmentid == nil {
continue
}
if cn, ok := byID[*bookings[i].Consignmentid]; ok {
bookings[i].Trackingno = cn.Trackingno
bookings[i].Consignmentstatus = cn.Status
}
}
}
func GetCustomerBookings(c *fiber.Ctx) error {
customerID := c.Locals("userid").(int)
@@ -581,6 +627,8 @@ func GetCustomerBookings(c *fiber.Ctx) error {
return utils.Internal(c, "failed to fetch bookings")
}
fillConsignmentFacts(bookings)
return utils.List(c, bookings, int64(len(bookings)))
}
@@ -596,6 +644,10 @@ func GetCustomerBookingDetails(c *fiber.Ctx) error {
return utils.NotFound(c, "booking not found")
}
one := []models.PickupBooking{booking}
fillConsignmentFacts(one)
booking = one[0]
return utils.OK(c, booking)
}