Security - Express console had no tenant scoping at all: LoginAdmin hardcoded tenantid 0 into every JWT and none of the 85 admin handlers filtered by tenant, so any client given a console login would read every other client's bookings, customers, pricing and reports. Adds DoormileAuth.Tenantid (nil = Doormile staff, unrestricted; set = client, scoped), emits it in the token, and scopes reads, guards writes and pins tenantid on create. - Miler telemetry (/miler/logs, /miler/status, /miler/consignments/logs) took userid from the request body, letting any authenticated rider write another rider's status and GPS trail — data the dispatch layer reasons over. Identity now comes from the token. - POST /miler/reset-pin was unauthenticated and overwrote a PIN given only a phone number, so reset-pin + verify-pin took over any rider account. Now requires admin/manager/executive auth. Correctness - Date ranges compared the container's UTC clock against timestamps the DB writes as IST wall-clock (DSN sets TimeZone=Asia/Kolkata), so "today so far" ended 5h30m in the past and silently dropped everything created after noon IST from every report. Sets TZ in the image and adds utils.DBNow/DBToday, which stay correct regardless of container timezone. - CreateMiler never set Configid, so console-created riders got the column default of 1 while LoginMiler looks up configid 1001 — every such rider was unable to log in, reported as "no miler account found". - Delivery wrote no consignment history row, so a tracking timeline never showed the parcel arriving. Features - Delivery OTP is now real (crypto/rand, issued to the receiver, verified and cleared on delivery) but opt-in per client via Tenant.Requiredeliveryotp, defaulting off — friction worth it for a courier parcel, not a food order. - Express bookings accept pickuplocationid, so the console can name a client site (a DailyGrubs kitchen) instead of retyping its address; validated against the tenant and carried through to the consignment. - TenantLocation.Locationname, miler tenantid/hubid, Nagercoil (629) opened. - PUT /miler/availability accepts both "status" and "availabilitystatus", and /miler/location no longer drops speed/heading — both were contract mismatches against the doc the Flutter dev was given. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
682 lines
21 KiB
Go
682 lines
21 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"doormile/constants"
|
|
"doormile/db"
|
|
"doormile/internal/notify"
|
|
"doormile/models"
|
|
"doormile/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// --------------------
|
|
// DUTY MANAGEMENT
|
|
// --------------------
|
|
|
|
func MilerStartDuty(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
|
|
var req struct {
|
|
Lat float64 `json:"lat"`
|
|
Lon float64 `json:"lon"`
|
|
}
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return utils.BadRequest(c, "invalid request body")
|
|
}
|
|
|
|
midnight := todayMidnight()
|
|
var existing models.MilerDutyLog
|
|
if err := db.DB.Where("userid = ? AND onduty = ? AND logoutat IS NULL AND loginat >= ?", milerUserID, true, midnight).
|
|
First(&existing).Error; err == nil {
|
|
return utils.BadRequest(c, "already on duty, end current duty first")
|
|
}
|
|
|
|
now := time.Now()
|
|
dutyLog := models.MilerDutyLog{
|
|
Userid: milerUserID,
|
|
Loginat: now,
|
|
Onduty: true,
|
|
Startlat: req.Lat,
|
|
Startlon: req.Lon,
|
|
Lastlat: req.Lat,
|
|
Lastlon: req.Lon,
|
|
}
|
|
if err := db.DB.Create(&dutyLog).Error; err != nil {
|
|
return utils.Internal(c, "failed to start duty")
|
|
}
|
|
|
|
db.DB.Model(&models.MilerProfile{}).Where("userid = ?", milerUserID).Updates(map[string]interface{}{
|
|
"availabilitystatus": constants.MilerAvailable,
|
|
"currentlatitude": req.Lat,
|
|
"currentlongitude": req.Lon,
|
|
"lastlocationupdatedat": now,
|
|
})
|
|
db.DB.Model(&models.AppUser{}).Where("userid = ?", milerUserID).Update("onduty", 1)
|
|
|
|
return utils.OK(c, fiber.Map{
|
|
"dutylogid": dutyLog.Dutylogid,
|
|
"loginat": dutyLog.Loginat,
|
|
})
|
|
}
|
|
|
|
func MilerEndDuty(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
|
|
var req struct {
|
|
Lat float64 `json:"lat"`
|
|
Lon float64 `json:"lon"`
|
|
}
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return utils.BadRequest(c, "invalid request body")
|
|
}
|
|
|
|
var dutyLog models.MilerDutyLog
|
|
if err := db.DB.Where("userid = ? AND onduty = ? AND logoutat IS NULL", milerUserID, true).
|
|
Order("loginat DESC").First(&dutyLog).Error; err != nil {
|
|
return utils.BadRequest(c, "not currently on duty")
|
|
}
|
|
|
|
var activeAssignments int64
|
|
db.DB.Model(&models.BookingAssignment{}).
|
|
Where("mileruserid = ? AND assignmentstatus IN ?", milerUserID, []string{constants.AssignmentAssigned, constants.AssignmentAccepted}).
|
|
Count(&activeAssignments)
|
|
if activeAssignments > 0 {
|
|
return utils.BadRequest(c, "cannot go off duty with active assignments")
|
|
}
|
|
|
|
now := time.Now()
|
|
dutyLog.Logoutat = &now
|
|
dutyLog.Onduty = false
|
|
dutyLog.Lastlat = req.Lat
|
|
dutyLog.Lastlon = req.Lon
|
|
if err := db.DB.Save(&dutyLog).Error; err != nil {
|
|
return utils.Internal(c, "failed to end duty")
|
|
}
|
|
|
|
db.DB.Model(&models.MilerProfile{}).Where("userid = ?", milerUserID).Update("availabilitystatus", constants.MilerOffline)
|
|
db.DB.Model(&models.AppUser{}).Where("userid = ?", milerUserID).Update("onduty", 0)
|
|
|
|
return utils.OK(c, fiber.Map{
|
|
"dutylogid": dutyLog.Dutylogid,
|
|
"logoutat": dutyLog.Logoutat,
|
|
"totalkms": dutyLog.Totalkms,
|
|
})
|
|
}
|
|
|
|
func MilerGetDutyStatus(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
midnight := todayMidnight()
|
|
|
|
var dutyLog models.MilerDutyLog
|
|
if err := db.DB.Where("userid = ? AND onduty = ? AND logoutat IS NULL AND loginat >= ?", milerUserID, true, midnight).
|
|
Order("loginat DESC").First(&dutyLog).Error; err != nil {
|
|
return utils.OK(c, fiber.Map{"onduty": false})
|
|
}
|
|
|
|
var profile models.MilerProfile
|
|
db.DB.Where("userid = ?", milerUserID).First(&profile)
|
|
|
|
return utils.OK(c, fiber.Map{
|
|
"onduty": true,
|
|
"dutylogid": dutyLog.Dutylogid,
|
|
"loginat": dutyLog.Loginat,
|
|
"totalkms": dutyLog.Totalkms,
|
|
"availabilitystatus": profile.Availabilitystatus,
|
|
})
|
|
}
|
|
|
|
// --------------------
|
|
// BREAK MANAGEMENT
|
|
// --------------------
|
|
|
|
func MilerStartBreak(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
|
|
var req struct {
|
|
Breaktype string `json:"breaktype"`
|
|
}
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return utils.BadRequest(c, "invalid request body")
|
|
}
|
|
if req.Breaktype == "" {
|
|
req.Breaktype = "Personal"
|
|
}
|
|
|
|
var dutyLog models.MilerDutyLog
|
|
if err := db.DB.Where("userid = ? AND onduty = ? AND logoutat IS NULL", milerUserID, true).
|
|
Order("loginat DESC").First(&dutyLog).Error; err != nil {
|
|
return utils.BadRequest(c, "not on duty")
|
|
}
|
|
|
|
var openBreak models.MilerBreakLog
|
|
if err := db.DB.Where("userid = ? AND endat IS NULL", milerUserID).First(&openBreak).Error; err == nil {
|
|
return utils.BadRequest(c, "break already active")
|
|
}
|
|
|
|
breakLog := models.MilerBreakLog{
|
|
Userid: milerUserID,
|
|
Dutylogid: dutyLog.Dutylogid,
|
|
Breaktype: req.Breaktype,
|
|
Startat: time.Now(),
|
|
}
|
|
if err := db.DB.Create(&breakLog).Error; err != nil {
|
|
return utils.Internal(c, "failed to start break")
|
|
}
|
|
|
|
db.DB.Model(&models.MilerProfile{}).Where("userid = ?", milerUserID).Update("availabilitystatus", constants.MilerBreak)
|
|
|
|
return utils.OK(c, fiber.Map{
|
|
"breaklogid": breakLog.Breaklogid,
|
|
"startat": breakLog.Startat,
|
|
})
|
|
}
|
|
|
|
func MilerEndBreak(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
|
|
var breakLog models.MilerBreakLog
|
|
if err := db.DB.Where("userid = ? AND endat IS NULL", milerUserID).
|
|
Order("startat DESC").First(&breakLog).Error; err != nil {
|
|
return utils.BadRequest(c, "no active break")
|
|
}
|
|
|
|
now := time.Now()
|
|
breakLog.Endat = &now
|
|
if err := db.DB.Save(&breakLog).Error; err != nil {
|
|
return utils.Internal(c, "failed to end break")
|
|
}
|
|
|
|
db.DB.Model(&models.MilerProfile{}).Where("userid = ?", milerUserID).Update("availabilitystatus", constants.MilerAvailable)
|
|
|
|
return utils.OK(c, fiber.Map{
|
|
"breaklogid": breakLog.Breaklogid,
|
|
"endat": breakLog.Endat,
|
|
})
|
|
}
|
|
|
|
// --------------------
|
|
// MILER'S OWN BOOKINGS
|
|
// --------------------
|
|
|
|
func MilerGetMyBookings(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
status := c.Query("status")
|
|
dateStr := c.Query("date")
|
|
|
|
query := db.DB.Where("assignedmileruserid = ?", milerUserID)
|
|
if status != "" && status != "All" {
|
|
query = query.Where("status = ?", status)
|
|
}
|
|
if dateStr != "" {
|
|
if date, err := time.Parse("2006-01-02", dateStr); err == nil {
|
|
query = query.Where("createdat >= ? AND createdat < ?", date, date.Add(24*time.Hour))
|
|
}
|
|
}
|
|
|
|
var bookings []models.PickupBooking
|
|
if err := query.Preload("Parcels").Preload("ServiceOptions").Order("createdat DESC").Find(&bookings).Error; err != nil {
|
|
return utils.Internal(c, "failed to fetch bookings")
|
|
}
|
|
|
|
response := make([]fiber.Map, 0, len(bookings))
|
|
for _, b := range bookings {
|
|
var customer models.AppCustomer
|
|
db.DB.Where("appcustomerid = ?", b.Appcustomerid).First(&customer)
|
|
|
|
response = append(response, fiber.Map{
|
|
"bookingid": b.Bookingid,
|
|
"bookingreference": b.Bookingno,
|
|
"status": b.Status,
|
|
"pickupaddress": b.Pickupaddress,
|
|
"pickuplatitude": b.Pickuplatitude,
|
|
"pickuplongitude": b.Pickuplongitude,
|
|
"deliveryaddress": b.Deliveryaddress,
|
|
"deliverylatitude": b.Deliverylatitude,
|
|
"deliverylongitude": b.Deliverylongitude,
|
|
"customername": strings.TrimSpace(customer.Firstname + " " + customer.Lastname),
|
|
"customerphone": customer.Phone,
|
|
"parcels": b.Parcels,
|
|
"serviceoptions": b.ServiceOptions,
|
|
"createdat": b.Createdat,
|
|
})
|
|
}
|
|
|
|
return utils.List(c, response, int64(len(response)))
|
|
}
|
|
|
|
// --------------------
|
|
// DELIVERY CONFIRMATION
|
|
// --------------------
|
|
|
|
func MilerDeliverConsignment(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
consignmentID, err := strconv.Atoi(c.Params("id"))
|
|
if err != nil {
|
|
return utils.BadRequest(c, "invalid consignment ID")
|
|
}
|
|
|
|
var req struct {
|
|
Otp string `json:"otp"`
|
|
Deliveredtoname string `json:"deliveredtoname"`
|
|
Photourl string `json:"photourl"`
|
|
Receiversignatureurl string `json:"receiversignatureurl"`
|
|
Lat float64 `json:"lat"`
|
|
Lon float64 `json:"lon"`
|
|
}
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return utils.BadRequest(c, "invalid request body")
|
|
}
|
|
if req.Deliveredtoname == "" {
|
|
return utils.BadRequest(c, "deliveredtoname is required")
|
|
}
|
|
|
|
var consignment models.Consignment
|
|
if err := db.DB.First(&consignment, consignmentID).Error; err != nil {
|
|
return utils.NotFound(c, "consignment not found")
|
|
}
|
|
|
|
var booking models.PickupBooking
|
|
if err := db.DB.Where("consignmentid = ? AND assignedmileruserid = ?", consignment.Consignmentid, milerUserID).
|
|
First(&booking).Error; err != nil {
|
|
return utils.NotFound(c, "assigned consignment not found")
|
|
}
|
|
|
|
if consignment.Status != constants.ConsignmentOutForDelivery {
|
|
return utils.BadRequest(c, "consignment is not out for delivery")
|
|
}
|
|
|
|
// An OTP is only present when the client asked for one (Tenant.Requiredeliveryotp),
|
|
// so an empty one means this delivery was never meant to need a code — that
|
|
// covers food clients like DailyGrubs as well as parcels already in the
|
|
// network from before OTPs existed, which would otherwise be unclosable.
|
|
if consignment.Deliveryotp != "" {
|
|
if req.Otp == "" {
|
|
return utils.BadRequest(c, "otp is required for this delivery")
|
|
}
|
|
if req.Otp != consignment.Deliveryotp {
|
|
return utils.BadRequest(c, "incorrect delivery OTP")
|
|
}
|
|
}
|
|
|
|
tx := db.DB.Begin()
|
|
|
|
proof := models.DeliveryProof{
|
|
Consignmentid: consignment.Consignmentid,
|
|
Deliveredat: time.Now(),
|
|
Deliveredtoname: req.Deliveredtoname,
|
|
Receiversignatureurl: req.Receiversignatureurl,
|
|
Photourl: req.Photourl,
|
|
Otpverified: consignment.Deliveryotp != "",
|
|
Geolatitude: req.Lat,
|
|
Geolongitude: req.Lon,
|
|
Createdby: milerUserID,
|
|
}
|
|
if err := tx.Create(&proof).Error; err != nil {
|
|
tx.Rollback()
|
|
return utils.Internal(c, "failed to record delivery proof")
|
|
}
|
|
|
|
consignment.Status = constants.ConsignmentDelivered
|
|
// Cleared once redeemed so the same code can't close out a second attempt.
|
|
consignment.Deliveryotp = ""
|
|
consignment.Updatedat = time.Now()
|
|
if err := tx.Save(&consignment).Error; err != nil {
|
|
tx.Rollback()
|
|
return utils.Internal(c, "failed to mark consignment delivered")
|
|
}
|
|
|
|
// Every other state change on a consignment writes a history row; delivery
|
|
// did not, so a customer following the tracking timeline never saw the
|
|
// parcel arrive — it just stopped at Out_for_Delivery.
|
|
deliveredEvent := models.ConsignmentHistory{
|
|
Consignmentid: consignment.Consignmentid,
|
|
Hubid: consignment.Currenthubid,
|
|
Userid: &milerUserID,
|
|
Eventstatus: constants.ConsignmentDelivered,
|
|
Remarks: fmt.Sprintf("Delivered to %s at (%.5f, %.5f)",
|
|
req.Deliveredtoname, req.Lat, req.Lon),
|
|
}
|
|
if err := tx.Create(&deliveredEvent).Error; err != nil {
|
|
tx.Rollback()
|
|
return utils.Internal(c, "failed to record delivery history")
|
|
}
|
|
|
|
if err := tx.Model(&models.BookingAssignment{}).
|
|
Where("bookingid = ? AND mileruserid = ?", booking.Bookingid, milerUserID).
|
|
Updates(map[string]interface{}{
|
|
"assignmentstatus": constants.AssignmentCompleted,
|
|
"completedat": time.Now(),
|
|
}).Error; err != nil {
|
|
tx.Rollback()
|
|
return utils.Internal(c, "failed to close assignment")
|
|
}
|
|
|
|
if err := tx.Commit().Error; err != nil {
|
|
return utils.Internal(c, "failed to confirm delivery")
|
|
}
|
|
|
|
if db.Js != nil {
|
|
payload := map[string]interface{}{
|
|
"bookingid": booking.Bookingid,
|
|
"outcome": "on_time",
|
|
"consignmentid": consignment.Consignmentid,
|
|
"mileruserid": milerUserID,
|
|
}
|
|
if data, err := json.Marshal(payload); err == nil {
|
|
if _, err := db.Js.Publish("booking.outcome", data); err != nil {
|
|
utils.Warn("MilerDeliverConsignment: NATS publish failed", "consignment_id", consignment.Consignmentid, "error", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
var customer models.AppCustomer
|
|
if err := db.DB.Where("appcustomerid = ?", booking.Appcustomerid).First(&customer).Error; err == nil && customer.Devicetoken != "" {
|
|
if notifyErr := notify.SendToDevice(
|
|
customer.Devicetoken,
|
|
"Parcel Delivered",
|
|
"Your parcel has been delivered!",
|
|
map[string]string{
|
|
"booking_id": strconv.Itoa(booking.Bookingid),
|
|
"consignment_id": strconv.Itoa(consignment.Consignmentid),
|
|
},
|
|
); notifyErr != nil {
|
|
utils.Warn("FCM: failed to notify customer on delivery", "booking_id", booking.Bookingid, "error", notifyErr)
|
|
}
|
|
}
|
|
|
|
return c.JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "delivery confirmed",
|
|
"data": fiber.Map{
|
|
"proofid": proof.Proofid,
|
|
"deliveredat": proof.Deliveredat,
|
|
},
|
|
})
|
|
}
|
|
|
|
// MilerSkipDelivery records a failed/incomplete final-mile delivery attempt
|
|
// (customer unavailable, gate locked, wrong address, etc.) without closing
|
|
// out the consignment — the miler still holds the parcel, the consignment
|
|
// stays Out_for_Delivery, and the attempt is logged for a retry. This is the
|
|
// "skipped" half of the old system's 8-in-1 status endpoint; MilerDeliverConsignment
|
|
// above is the "delivered" half, and MilerCancelAssignment (milerController.go)
|
|
// is the pre-pickup "cancelled"/"rejected" half.
|
|
func MilerSkipDelivery(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
consignmentID, err := strconv.Atoi(c.Params("id"))
|
|
if err != nil {
|
|
return utils.BadRequest(c, "invalid consignment ID")
|
|
}
|
|
|
|
var req struct {
|
|
Reason string `json:"reason"`
|
|
Lat float64 `json:"lat"`
|
|
Lon float64 `json:"lon"`
|
|
}
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return utils.BadRequest(c, "invalid request body")
|
|
}
|
|
if req.Reason == "" {
|
|
return utils.BadRequest(c, "reason is required")
|
|
}
|
|
|
|
var consignment models.Consignment
|
|
if err := db.DB.First(&consignment, consignmentID).Error; err != nil {
|
|
return utils.NotFound(c, "consignment not found")
|
|
}
|
|
|
|
var booking models.PickupBooking
|
|
if err := db.DB.Where("consignmentid = ? AND assignedmileruserid = ?", consignment.Consignmentid, milerUserID).
|
|
First(&booking).Error; err != nil {
|
|
return utils.NotFound(c, "assigned consignment not found")
|
|
}
|
|
|
|
if consignment.Status != constants.ConsignmentOutForDelivery {
|
|
return utils.BadRequest(c, "consignment is not out for delivery")
|
|
}
|
|
|
|
tx := db.DB.Begin()
|
|
|
|
consignment.Attemptcount += 1
|
|
consignment.Updatedat = time.Now()
|
|
if err := tx.Save(&consignment).Error; err != nil {
|
|
tx.Rollback()
|
|
return utils.Internal(c, "failed to record skipped attempt")
|
|
}
|
|
|
|
history := models.ConsignmentHistory{
|
|
Consignmentid: consignment.Consignmentid,
|
|
Userid: &milerUserID,
|
|
Eventstatus: "Delivery_Skipped",
|
|
Remarks: fmt.Sprintf("Attempt %d skipped at (%.5f, %.5f): %s", consignment.Attemptcount, req.Lat, req.Lon, req.Reason),
|
|
}
|
|
if err := tx.Create(&history).Error; err != nil {
|
|
tx.Rollback()
|
|
return utils.Internal(c, "failed to log skipped attempt")
|
|
}
|
|
|
|
if err := tx.Commit().Error; err != nil {
|
|
return utils.Internal(c, "failed to commit skipped attempt")
|
|
}
|
|
|
|
// After 3 failed attempts, flag it for hub attention rather than leaving
|
|
// it silently retrying forever.
|
|
if consignment.Attemptcount >= 3 {
|
|
exception := models.ConsignmentException{
|
|
Consignmentid: consignment.Consignmentid,
|
|
Reportedbyuserid: &milerUserID,
|
|
Exceptiontype: constants.ExceptionUndeliverable,
|
|
Severity: "Medium",
|
|
Description: fmt.Sprintf("3 delivery attempts failed. Last reason: %s", req.Reason),
|
|
}
|
|
db.DB.Create(&exception)
|
|
}
|
|
|
|
return utils.OK(c, fiber.Map{
|
|
"consignmentid": consignment.Consignmentid,
|
|
"attemptcount": consignment.Attemptcount,
|
|
"status": consignment.Status,
|
|
})
|
|
}
|
|
|
|
// --------------------
|
|
// EARNINGS
|
|
// --------------------
|
|
|
|
type milerEarningsSummary struct {
|
|
CompletedStops int64 `gorm:"column:completed_stops"`
|
|
TotalKms float64 `gorm:"column:total_kms"`
|
|
TotalEarnings float64 `gorm:"column:total_earnings"`
|
|
TotalBonus int `gorm:"column:total_bonus"`
|
|
}
|
|
|
|
func MilerGetEarnings(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
period := c.Query("period", "daily")
|
|
|
|
var start, end time.Time
|
|
now := time.Now()
|
|
|
|
switch period {
|
|
case "weekly":
|
|
start = todayMidnight().Add(-6 * 24 * time.Hour)
|
|
end = todayMidnight().Add(24 * time.Hour)
|
|
case "monthly":
|
|
start = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
|
end = start.AddDate(0, 1, 0)
|
|
default:
|
|
period = "daily"
|
|
day := todayMidnight()
|
|
if dateStr := c.Query("date"); dateStr != "" {
|
|
if parsed, err := time.Parse("2006-01-02", dateStr); err == nil {
|
|
day = parsed
|
|
}
|
|
}
|
|
start = day
|
|
end = day.Add(24 * time.Hour)
|
|
}
|
|
|
|
var summary milerEarningsSummary
|
|
db.DB.Model(&models.BookingAssignment{}).
|
|
Where("mileruserid = ? AND assignmentstatus = ? AND completedat >= ? AND completedat < ?",
|
|
milerUserID, constants.AssignmentCompleted, start, end).
|
|
Select("COUNT(*) as completed_stops, COALESCE(SUM(riderkms),0) as total_kms, COALESCE(SUM(ridercharges),0) as total_earnings, COALESCE(SUM(bonuspoints),0) as total_bonus").
|
|
Scan(&summary)
|
|
|
|
var breakdown []models.BookingAssignment
|
|
db.DB.Where("mileruserid = ? AND assignmentstatus = ? AND completedat >= ? AND completedat < ?",
|
|
milerUserID, constants.AssignmentCompleted, start, end).
|
|
Order("completedat DESC").Find(&breakdown)
|
|
|
|
breakdownResp := make([]fiber.Map, 0, len(breakdown))
|
|
for _, a := range breakdown {
|
|
breakdownResp = append(breakdownResp, fiber.Map{
|
|
"bookingid": a.Bookingid,
|
|
"riderkms": a.Riderkms,
|
|
"ridercharges": a.Ridercharges,
|
|
"bonuspoints": a.Bonuspoints,
|
|
"completedat": a.Completedat,
|
|
})
|
|
}
|
|
|
|
data := fiber.Map{
|
|
"period": period,
|
|
"completed_stops": summary.CompletedStops,
|
|
"total_kms": summary.TotalKms,
|
|
"total_earnings": summary.TotalEarnings,
|
|
"total_bonus": summary.TotalBonus,
|
|
"breakdown": breakdownResp,
|
|
}
|
|
if period == "daily" {
|
|
data["date"] = start.Format("2006-01-02")
|
|
}
|
|
|
|
return utils.OK(c, data)
|
|
}
|
|
|
|
// --------------------
|
|
// NOTIFICATIONS (synthetic — no dedicated table yet)
|
|
// --------------------
|
|
|
|
// MilerGetNotifications generates notifications from real assignment events,
|
|
// mirroring GetHubNotifications' synthetic-notification pattern.
|
|
func MilerGetNotifications(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
midnight := todayMidnight()
|
|
last24h := time.Now().Add(-24 * time.Hour)
|
|
|
|
var notifications []notificationEntry
|
|
|
|
var assigned []models.BookingAssignment
|
|
db.DB.Where("mileruserid = ? AND assignedat >= ?", milerUserID, midnight).Find(&assigned)
|
|
for _, a := range assigned {
|
|
notifications = append(notifications, notificationEntry{
|
|
Title: fmt.Sprintf("New pickup assigned — Booking #%d", a.Bookingid),
|
|
Type: "assignment",
|
|
Time: a.Assignedat,
|
|
})
|
|
}
|
|
|
|
var completed []models.BookingAssignment
|
|
db.DB.Where("mileruserid = ? AND assignmentstatus = ? AND completedat >= ?", milerUserID, constants.AssignmentCompleted, midnight).Find(&completed)
|
|
for _, a := range completed {
|
|
t := a.Assignedat
|
|
if a.Completedat != nil {
|
|
t = *a.Completedat
|
|
}
|
|
notifications = append(notifications, notificationEntry{
|
|
Title: fmt.Sprintf("Booking #%d completed", a.Bookingid),
|
|
Type: "completed",
|
|
Time: t,
|
|
})
|
|
}
|
|
|
|
var rejected []models.BookingAssignment
|
|
db.DB.Where("mileruserid = ? AND assignmentstatus = ? AND assignedat >= ?", milerUserID, constants.AssignmentRejected, last24h).Find(&rejected)
|
|
for _, a := range rejected {
|
|
notifications = append(notifications, notificationEntry{
|
|
Title: fmt.Sprintf("Assignment rejected — Booking #%d", a.Bookingid),
|
|
Type: "rejected",
|
|
Time: a.Assignedat,
|
|
})
|
|
}
|
|
|
|
sort.Slice(notifications, func(i, j int) bool { return notifications[i].Time.After(notifications[j].Time) })
|
|
if len(notifications) > 20 {
|
|
notifications = notifications[:20]
|
|
}
|
|
|
|
response := make([]fiber.Map, 0, len(notifications))
|
|
for i, n := range notifications {
|
|
response = append(response, fiber.Map{
|
|
"id": i + 1,
|
|
"title": n.Title,
|
|
"type": n.Type,
|
|
"time": humanizeRelativeTime(n.Time),
|
|
"read": false,
|
|
})
|
|
}
|
|
|
|
return utils.List(c, response, int64(len(response)))
|
|
}
|
|
|
|
// MilerMarkNotificationRead is a stub until a notifications table with
|
|
// read-state exists — always succeeds without persisting anything, matching
|
|
// MarkNotificationRead's hub-console equivalent.
|
|
func MilerMarkNotificationRead(c *fiber.Ctx) error {
|
|
return c.JSON(fiber.Map{"success": true})
|
|
}
|
|
|
|
// --------------------
|
|
// SUPPORT
|
|
// --------------------
|
|
|
|
func MilerCreateTicket(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
|
|
var req struct {
|
|
Subject string `json:"subject"`
|
|
Description string `json:"description"`
|
|
}
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return utils.BadRequest(c, "invalid request body")
|
|
}
|
|
if req.Subject == "" || req.Description == "" {
|
|
return utils.BadRequest(c, "subject and description are required")
|
|
}
|
|
|
|
ticket := models.MilerSupportTicket{
|
|
Userid: milerUserID,
|
|
Subject: req.Subject,
|
|
Description: req.Description,
|
|
Status: "Open",
|
|
}
|
|
if err := db.DB.Create(&ticket).Error; err != nil {
|
|
return utils.Internal(c, "failed to create support ticket")
|
|
}
|
|
|
|
return utils.Created(c, fiber.Map{
|
|
"ticketid": ticket.Ticketid,
|
|
"status": ticket.Status,
|
|
})
|
|
}
|
|
|
|
func MilerGetTickets(c *fiber.Ctx) error {
|
|
milerUserID := c.Locals("userid").(int)
|
|
|
|
var tickets []models.MilerSupportTicket
|
|
if err := db.DB.Where("userid = ?", milerUserID).Order("createdat DESC").Find(&tickets).Error; err != nil {
|
|
return utils.Internal(c, "failed to fetch support tickets")
|
|
}
|
|
|
|
return utils.List(c, tickets, int64(len(tickets)))
|
|
}
|