delivery slot api creation

This commit is contained in:
2026-10-06 17:35:51 +05:30
parent f67cbad79a
commit 03ae7d310b
12 changed files with 1137 additions and 104 deletions

View File

@@ -16,117 +16,126 @@ import (
type OrderController struct {
orderService services.OrderService
// Asked whether a chosen delivery window is still open. Held here rather
// than reimplemented, so the app's list and this check can never disagree
// about where a window ends.
deliverySlotService services.DeliverySlotService
}
func NewOrderController(orderService services.OrderService) *OrderController {
return &OrderController{orderService: orderService}
func NewOrderController(
orderService services.OrderService,
deliverySlotService services.DeliverySlotService,
) *OrderController {
return &OrderController{
orderService: orderService,
deliverySlotService: deliverySlotService,
}
}
func (ctl *OrderController) GetOrders(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(c.Query("tenantid"))
pid, _ := strconv.Atoi(c.Query("partnerid"))
cid, _ := strconv.Atoi(c.Query("customerid"))
mid, _ := strconv.Atoi(c.Query("moduleid"))
aid, _ := strconv.Atoi(c.Query("applocationid"))
uid, _ := strconv.Atoi(c.Query("appuserid"))
lid, _ := strconv.Atoi(c.Query("locationid"))
configid, _ := strconv.Atoi(c.Query("configid"))
tid, _ := strconv.Atoi(c.Query("tenantid"))
pid, _ := strconv.Atoi(c.Query("partnerid"))
cid, _ := strconv.Atoi(c.Query("customerid"))
mid, _ := strconv.Atoi(c.Query("moduleid"))
aid, _ := strconv.Atoi(c.Query("applocationid"))
uid, _ := strconv.Atoi(c.Query("appuserid"))
lid, _ := strconv.Atoi(c.Query("locationid"))
configid, _ := strconv.Atoi(c.Query("configid"))
stat := c.Query("status")
fdate := c.Query("fromdate")
tdate := c.Query("todate")
keyword := c.Query("keyword")
stat := c.Query("status")
fdate := c.Query("fromdate")
tdate := c.Query("todate")
keyword := c.Query("keyword")
pageno, _ := strconv.Atoi(c.Query("pageno"))
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
pageno, _ := strconv.Atoi(c.Query("pageno"))
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
if pageno <= 0 {
pageno = 1
}
if pagesize <= 0 {
pagesize = 10
}
if pageno <= 0 {
pageno = 1
}
if pagesize <= 0 {
pagesize = 10
}
// Build dynamic query struct
query := models.DeliveryQuery{
Tenantid: tid,
Partnerid: pid,
Customerid: cid,
Moduleid: mid,
Applocationid: aid,
Locationid: lid,
UserID: uid,
Appuserid: uid,
Configid: configid,
Fromdate: fdate,
ToDate: tdate,
Status: stat,
Keyword: keyword,
Pageno: pageno,
Pagesize: pagesize,
}
// Build dynamic query struct
query := models.DeliveryQuery{
Tenantid: tid,
Partnerid: pid,
Customerid: cid,
Moduleid: mid,
Applocationid: aid,
Locationid: lid,
UserID: uid,
Appuserid: uid,
Configid: configid,
Fromdate: fdate,
ToDate: tdate,
Status: stat,
Keyword: keyword,
Pageno: pageno,
Pagesize: pagesize,
}
var (
orders []models.OrderInfo
err error
)
var (
orders []models.OrderInfo
err error
)
// --------------------------
// 🔥 DYNAMIC ROUTING LOGIC
// --------------------------
// --------------------------
// 🔥 DYNAMIC ROUTING LOGIC
// --------------------------
if tid != 0 && lid != 0 {
// ⭐ Both tenant & location → special handler
orders, err = ctl.orderService.GetTenantLocationOrders(query)
if tid != 0 && lid != 0 {
// ⭐ Both tenant & location → special handler
orders, err = ctl.orderService.GetTenantLocationOrders(query)
} else if tid != 0 {
// Tenant only
orders, err = ctl.orderService.GetTenantOrders(query)
} else if tid != 0 {
// Tenant only
orders, err = ctl.orderService.GetTenantOrders(query)
} else if pid != 0 {
// Partner
orders, err = ctl.orderService.GetPartnerOrders(stat, fdate, tdate, pid, pageno, pagesize, keyword)
} else if pid != 0 {
// Partner
orders, err = ctl.orderService.GetPartnerOrders(stat, fdate, tdate, pid, pageno, pagesize, keyword)
} else if cid != 0 {
// Customer
orders, err = ctl.orderService.GetCustomerOrders(stat, fdate, tdate, cid, mid, pageno, pagesize, keyword)
} else if cid != 0 {
// Customer
orders, err = ctl.orderService.GetCustomerOrders(stat, fdate, tdate, cid, mid, pageno, pagesize, keyword)
} else if aid != 0 {
// App-location orders
orders, err = ctl.orderService.GetAdminOrders(stat, fdate, tdate, aid, pageno, pagesize, keyword)
} else if aid != 0 {
// App-location orders
orders, err = ctl.orderService.GetAdminOrders(stat, fdate, tdate, aid, pageno, pagesize, keyword)
} else if uid != 0 {
// User orders
orders, err = ctl.orderService.GetUserOrders(stat, fdate, tdate, uid, pageno, pagesize, keyword)
} else if uid != 0 {
// User orders
orders, err = ctl.orderService.GetUserOrders(stat, fdate, tdate, uid, pageno, pagesize, keyword)
} else {
// No scoping id supplied (tenantid/partnerid/customerid/applocationid/appuserid).
// Refuse instead of silently returning every order in the database.
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": http.StatusBadRequest,
"message": "At least one of tenantid, partnerid, customerid, applocationid or appuserid is required",
})
}
} else {
// No scoping id supplied (tenantid/partnerid/customerid/applocationid/appuserid).
// Refuse instead of silently returning every order in the database.
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": http.StatusBadRequest,
"message": "At least one of tenantid, partnerid, customerid, applocationid or appuserid is required",
})
}
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Success",
"details": orders,
})
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Success",
"details": orders,
})
}
func (ctl *OrderController) GetOrderSummary(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(c.Query("tenantid"))
pid, _ := strconv.Atoi(c.Query("partnerid"))
@@ -160,7 +169,6 @@ func (ctl *OrderController) GetOrderSummary(c *fiber.Ctx) error {
})
}
func (ctl *OrderController) GetlocationOrderSummary(c *fiber.Ctx) error {
tenantIDStr := c.Query("tenantid")
tenantID, _ := strconv.Atoi(tenantIDStr)
@@ -350,6 +358,29 @@ func (ctl *OrderController) CreateOrderv3(c *fiber.Ctx) error {
data.Deliverytime = time.Now().Format("2006-01-02 15:04:05")
}
// The chosen delivery window, re-decided here.
//
// The app sends back what /v1/mob/deliveryslots/available handed it, but
// that group carries NO SESSION — anything arriving is a claim, not a fact.
// The common case is innocent and still has to be caught: a shopper leaves
// the checkout screen open while the window closes, then taps pay.
//
// 409 rather than 400: the request was well formed and was true when it was
// built. The message is written to be shown to the shopper as-is.
//
// An order naming NO window passes straight through. That is every order
// placed before this shipped and every order from a branch that has set no
// windows, and it must stay ordinary.
if err := ctl.deliverySlotService.ValidateForOrder(
data.Tenantid, data.Locationid, data.Deliveryslotid, data.Deliveryslotdate,
); err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"code": http.StatusConflict,
"message": err.Error(),
"status": false,
})
}
// An order that does not state its config is an APP order, because that is
// the only kind this endpoint takes.
//
@@ -592,7 +623,7 @@ func (ctl *OrderController) GetTimeSeries(c *fiber.Ctx) error {
"status": false,
})
}
if granularity == "" {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,