updates on the admincontroler page

This commit is contained in:
2026-09-10 15:55:40 +05:30
parent 86ae2ab41e
commit 17bd316e4d
4 changed files with 196 additions and 1 deletions

View File

@@ -2062,6 +2062,41 @@ func AssignMilerVehicle(c *fiber.Ctx) error {
// BOOKINGS MANAGEMENT
// --------------------
// bookingDestinationCounts is one row of the grouped bookingdestinations
// aggregate: how many destinations a booking carries and how many packages they
// add up to across all of them.
type bookingDestinationCounts struct {
Bookingid int `gorm:"column:bookingid"`
Destinationcount int `gorm:"column:destinationcount"`
Totalpackagecount int `gorm:"column:totalpackagecount"`
}
// applyDestinationCounts writes the grouped counts onto the bookings they
// belong to. A booking with no bookingdestinations rows — every
// console-created booking, and every booking predating the customer app —
// keeps the 0/0 zero value, which is the correct answer and the one the
// console distinguishes from a single-destination booking.
//
// Split out from the handler because it is the whole of the mapping logic and
// the only part that can be tested without a database.
func applyDestinationCounts(bookings []models.PickupBooking, counts []bookingDestinationCounts) {
if len(bookings) == 0 || len(counts) == 0 {
return
}
byBooking := make(map[int]bookingDestinationCounts, len(counts))
for _, row := range counts {
byBooking[row.Bookingid] = row
}
for i := range bookings {
row, ok := byBooking[bookings[i].Bookingid]
if !ok {
continue
}
bookings[i].Destinationcount = row.Destinationcount
bookings[i].Totalpackagecount = row.Totalpackagecount
}
}
func GetAdminBookings(c *fiber.Ctx) error {
pageno := max(1, c.QueryInt("pageno", 1))
pagesize := min(100, max(1, c.QueryInt("pagesize", 20)))
@@ -2113,6 +2148,27 @@ func GetAdminBookings(c *fiber.Ctx) error {
}
}
// How many destinations each booking carries, and the packages summed across
// them. A customer-app pickup is ONE booking with N destinations, and the
// console needs to render "3 destinations · 4 packages" on the collapsed row.
// The full array is deliberately NOT preloaded here: the console drains up to
// 12 pages of 100 bookings and only opens one row at a time, so the array is
// payload the list never reads. Batched exactly like the consignment status
// above — one grouped query for the whole page, never one per row.
bookingIDs := make([]int, 0, len(bookings))
for _, b := range bookings {
bookingIDs = append(bookingIDs, b.Bookingid)
}
if len(bookingIDs) > 0 {
var counts []bookingDestinationCounts
db.DB.Model(&models.BookingDestination{}).
Select("bookingid, COUNT(*) AS destinationcount, COALESCE(SUM(packagecount), 0) AS totalpackagecount").
Where("bookingid IN ?", bookingIDs).
Group("bookingid").
Scan(&counts)
applyDestinationCounts(bookings, counts)
}
pages := int(math.Ceil(float64(total) / float64(pagesize)))
return c.JSON(fiber.Map{
@@ -2595,7 +2651,23 @@ func AdminBulkCreateBookings(c *fiber.Ctx) error {
func GetAdminBookingDetails(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
var booking models.PickupBooking
q := scopeToOwnTenant(c, db.DB.Preload("Parcels").Preload("ServiceOptions").Preload("Payments"), "tenantid")
// Destinations is the customer-app half of the booking: one pickup carries N
// of them, each with its own consignment, tracking number and stage once the
// miler completes pickup (cxPickupFanout.go). The relation has been declared
// on the model since the customer app shipped and nothing preloaded it, which
// is the entire reason the console could only ever show one drop.
//
// Ordered by seq ascending and never by anything else: seq is the
// customer-facing position and the {index} in
// PATCH /customer/bookings/{ref}/destinations/{index}, so the order the
// console renders has to be the order the customer addresses.
q := scopeToOwnTenant(c, db.DB.
Preload("Parcels").
Preload("ServiceOptions").
Preload("Payments").
Preload("Destinations", func(d *gorm.DB) *gorm.DB {
return d.Order("seq ASC")
}), "tenantid")
if err := q.First(&booking, id).Error; err != nil {
return utils.NotFound(c, "booking not found")
}