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

@@ -0,0 +1,146 @@
package controllers
import (
"net/http"
"strconv"
"github.com/gofiber/fiber/v2"
"nearle/models"
"nearle/services"
)
type DeliverySlotController struct {
service services.DeliverySlotService
}
func NewDeliverySlotController(service services.DeliverySlotService) *DeliverySlotController {
return &DeliverySlotController{service: service}
}
/*
GET /v1/web/deliveryslots?tenantid&locationid
Everything a branch has configured, active or not, for the console's editor.
A branch that has set nothing returns an empty list — see the note on Available
about why that is never an error.
*/
func (ctl *DeliverySlotController) ListDeliverySlots(c *fiber.Ctx) error {
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
locationID, _ := strconv.Atoi(c.Query("locationid"))
if tenantID <= 0 {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "tenantid is required",
"status": false,
})
}
slots, err := ctl.service.ListForBranch(tenantID, locationID)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": slots,
})
}
/*
PUT /v1/web/deliveryslots
The branch's windows, all three together rather than one at a time: they are
edited as a set on one screen, and sending them together is what lets the
service reject the whole edit when one row is wrong instead of applying part of
it.
A business objection — an unreadable time, a window ending before it starts,
the same key twice — is 409 and not 500. It is an answer about the request, and
the message is written to be shown to the person who typed it.
*/
func (ctl *DeliverySlotController) SaveDeliverySlots(c *fiber.Ctx) error {
var req struct {
Tenantid int `json:"tenantid"`
Locationid int `json:"locationid"`
Slots []models.DeliverySlots `json:"slots"`
}
if err := c.BodyParser(&req); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
if req.Tenantid <= 0 {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "tenantid is required",
"status": false,
})
}
if err := ctl.service.Save(req.Tenantid, req.Locationid, req.Slots); err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"code": http.StatusConflict,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Successfully Updated",
"status": true,
})
}
/*
GET /v1/mob/deliveryslots/available?tenantid&locationid
What the shopper may pick, already filtered and dated. The app renders this list
and does no time arithmetic of its own — see the service for why one clock has
to be authoritative.
An empty list is 200 with `details: []`, NOT an error. It means this branch has
set no windows, which is the state every shop is in today, and the app is
required to fall back to ordering without one. Returning 404 here would turn an
ordinary shop into a broken one.
*/
func (ctl *DeliverySlotController) AvailableDeliverySlots(c *fiber.Ctx) error {
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
locationID, _ := strconv.Atoi(c.Query("locationid"))
if tenantID <= 0 {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "tenantid is required",
"status": false,
})
}
slots, err := ctl.service.Available(tenantID, locationID)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": slots,
})
}

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,