From b7af24591037c9a1bedf882d32526578b5718c0e Mon Sep 17 00:00:00 2001 From: Suriya Date: Tue, 21 Jul 2026 09:25:30 +0530 Subject: [PATCH] store wise orders change --- controllers/deliveriesController.go | 8 ++++++ controllers/orderController.go | 33 +++++++++++++++++++++-- controllers/partnerController.go | 19 ++++++++++++- controllers/userController.go | 8 ++++++ repositories/catalogueRepository.go | 25 ++++++++++++++++++ repositories/orderRepository.go | 41 +++++++++++++++++------------ 6 files changed, 114 insertions(+), 20 deletions(-) diff --git a/controllers/deliveriesController.go b/controllers/deliveriesController.go index ddb4297..81991ce 100644 --- a/controllers/deliveriesController.go +++ b/controllers/deliveriesController.go @@ -209,6 +209,14 @@ func (ctl *DeliveriesController) GetDeliveries(c *fiber.Ctx) error { stat := c.Query("status") keyword := c.Query("keyword") + if pid == 0 && tid == 0 && uid == 0 && cid == 0 && aid == 0 && aud == 0 { + return c.Status(http.StatusBadRequest).JSON(fiber.Map{ + "code": http.StatusBadRequest, + "message": "At least one of tenantid, partnerid, customerid, applocationid, userid or appuserid is required", + "status": false, + }) + } + info := models.DeliveryQuery{ Partnerid: pid, Tenantid: tid, diff --git a/controllers/orderController.go b/controllers/orderController.go index d03e6ba..8395a58 100644 --- a/controllers/orderController.go +++ b/controllers/orderController.go @@ -100,8 +100,13 @@ func (ctl *OrderController) GetOrders(c *fiber.Ctx) error { orders, err = ctl.orderService.GetUserOrders(stat, fdate, tdate, uid, pageno, pagesize, keyword) } else { - // All orders - orders, err = ctl.orderService.GetAllOrders(stat, fdate, tdate, pageno, pagesize, keyword) + // 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 { @@ -129,6 +134,14 @@ func (ctl *OrderController) GetOrderSummary(c *fiber.Ctx) error { fdate := c.Query("fromdate") tdate := c.Query("todate") + if tid == 0 && pid == 0 && cid == 0 && lid == 0 { + return c.Status(http.StatusBadRequest).JSON(fiber.Map{ + "code": http.StatusBadRequest, + "message": "At least one of tenantid, partnerid, customerid or locationid is required", + "status": false, + }) + } + data, err := ctl.orderService.GetOrderSummary(tid, pid, cid, lid, fdate, tdate) if err != nil { return c.Status(http.StatusConflict).JSON(fiber.Map{ @@ -151,6 +164,14 @@ func (ctl *OrderController) GetlocationOrderSummary(c *fiber.Ctx) error { tenantIDStr := c.Query("tenantid") tenantID, _ := strconv.Atoi(tenantIDStr) + if tenantID == 0 { + return c.Status(http.StatusBadRequest).JSON(fiber.Map{ + "status": false, + "code": http.StatusBadRequest, + "message": "tenantid is required", + }) + } + data, err := ctl.orderService.GetLocationOrderSummary(tenantID) if err != nil { return c.Status(http.StatusInternalServerError).JSON(fiber.Map{ @@ -336,6 +357,14 @@ func (ctl *OrderController) GetCustomerOrders(c *fiber.Ctx) error { } offset := (pageNo - 1) * pageSize + if customerID == "" { + return c.Status(http.StatusBadRequest).JSON(fiber.Map{ + "code": http.StatusBadRequest, + "status": false, + "message": "customerid is required", + }) + } + orders, err := ctl.orderService.GetCustomerOrdersv3(customerID, tenantID, moduleID, fromDate, toDate, orderStatus, keyword, pageSize, offset) if err != nil { log.Println("GetCustomerOrders error:", err) diff --git a/controllers/partnerController.go b/controllers/partnerController.go index 66cd560..08bd5f4 100644 --- a/controllers/partnerController.go +++ b/controllers/partnerController.go @@ -21,6 +21,15 @@ func (ctl *PartnerController) GetActiveRiders(c *fiber.Ctx) error { aid, _ := strconv.Atoi(c.Query("applocationid")) uid, _ := strconv.Atoi(c.Query("userid")) tid, _ := strconv.Atoi(c.Query("tenantid")) + + if pid == 0 && aid == 0 && uid == 0 && tid == 0 { + return c.Status(http.StatusBadRequest).JSON(fiber.Map{ + "status": false, + "code": http.StatusBadRequest, + "message": "At least one of tenantid, partnerid, applocationid or userid is required", + }) + } + result, err := ctl.partnerService.GetActiveRiders(pid, aid, uid, tid) if err != nil { @@ -113,7 +122,15 @@ func (ctl *PartnerController) GetRiderLogs(c *fiber.Ctx) error { pid, _ := strconv.Atoi(c.Query("partnerid")) aid, _ := strconv.Atoi(c.Query("applocationid")) fdate := c.Query("fromdate") - tdate := c.Query("fromdate") + tdate := c.Query("todate") + + if pid == 0 && aid == 0 { + return c.Status(http.StatusBadRequest).JSON(fiber.Map{ + "status": false, + "code": http.StatusBadRequest, + "message": "At least one of partnerid or applocationid is required", + }) + } data, err := ctl.partnerService.GetRiderLogs(pid, aid, fdate, tdate) diff --git a/controllers/userController.go b/controllers/userController.go index acce3dd..57d048b 100644 --- a/controllers/userController.go +++ b/controllers/userController.go @@ -25,6 +25,14 @@ func (ctl *UserController) GetAllUsers(c *fiber.Ctx) error { pagesize, _ := strconv.Atoi(c.Query("pagesize", "10")) keyword := c.Query("keyword", "") + if tenantID == 0 { + return c.Status(http.StatusBadRequest).JSON(fiber.Map{ + "code": http.StatusBadRequest, + "message": "tenantid is required", + "status": false, + }) + } + users, err := ctl.userService.GetAllUsers(roleID, tenantID, pageno, pagesize, keyword) if err != nil { return c.Status(http.StatusInternalServerError).JSON(fiber.Map{ diff --git a/repositories/catalogueRepository.go b/repositories/catalogueRepository.go index 7063dd4..3bca09c 100644 --- a/repositories/catalogueRepository.go +++ b/repositories/catalogueRepository.go @@ -26,6 +26,11 @@ var catalogueBrandTables = map[string]string{ var ErrUnknownBrand = errors.New("unknown brand") +// ErrCatalogueDBUnavailable is returned by every method below when the +// catalogue DB connection wasn't configured (db.CatalogueDB is nil), so +// callers get a normal error instead of a nil-pointer panic. +var ErrCatalogueDBUnavailable = errors.New("catalogue database is not configured") + // catalogueProductColumns casts the text[] columns to text: GORM's raw // scan-into-struct silently drops slice-kind destination fields, so they // are read as text here and parsed into []string in scanProductRow. @@ -108,6 +113,10 @@ func tableForBrand(brand string) (string, error) { } func (r *catalogueRepository) GetBrands() ([]models.CatalogueBrand, error) { + if r.db == nil { + return nil, ErrCatalogueDBUnavailable + } + var brands []models.CatalogueBrand for brand, table := range catalogueBrandTables { @@ -122,6 +131,10 @@ func (r *catalogueRepository) GetBrands() ([]models.CatalogueBrand, error) { } func (r *catalogueRepository) GetCategories(brand string) ([]string, error) { + if r.db == nil { + return nil, ErrCatalogueDBUnavailable + } + table, err := tableForBrand(brand) if err != nil { return nil, err @@ -141,6 +154,10 @@ func (r *catalogueRepository) GetCategories(brand string) ([]string, error) { // everything, then let the store owner choose" — brand/category/keyword are // optional narrowing filters on top of that, not prerequisites. func (r *catalogueRepository) GetProducts(brand, category, keyword string, pageno, pagesize int) ([]models.CatalogueProduct, int64, error) { + if r.db == nil { + return nil, 0, ErrCatalogueDBUnavailable + } + if pagesize <= 0 { pagesize = 20 } @@ -253,6 +270,10 @@ func catalogueWhereClause(category, keyword string) (string, []interface{}) { } func (r *catalogueRepository) GetProductBySKU(brand, sku string) (*models.CatalogueProduct, error) { + if r.db == nil { + return nil, ErrCatalogueDBUnavailable + } + table, err := tableForBrand(brand) if err != nil { return nil, err @@ -276,6 +297,10 @@ func (r *catalogueRepository) GetProductBySKU(brand, sku string) (*models.Catalo } func (r *catalogueRepository) GetProductByID(brand string, id int64) (*models.CatalogueProduct, error) { + if r.db == nil { + return nil, ErrCatalogueDBUnavailable + } + table, err := tableForBrand(brand) if err != nil { return nil, err diff --git a/repositories/orderRepository.go b/repositories/orderRepository.go index 46f79ac..a52535b 100644 --- a/repositories/orderRepository.go +++ b/repositories/orderRepository.go @@ -4,7 +4,6 @@ import ( "fmt" "log" "nearle/models" - "strconv" "time" "gorm.io/gorm" @@ -599,39 +598,47 @@ func (r *orderRepository) GetAllOrders(stat, fdate, tdate string, pageno, pagesi func (r *orderRepository) GetOrderSummary(tid, pid, cid, lid int, fdate, tdate string) ([]models.Ordersummarydaily, error) { var data []models.Ordersummarydaily - var q1 string // Base SELECT const base = ` - SELECT + SELECT COUNT(*) AS total, SUM(CASE WHEN o.orderstatus = 'created' THEN 1 ELSE 0 END) AS created, SUM(CASE WHEN o.orderstatus = 'pending' THEN 1 ELSE 0 END) AS pending, SUM(CASE WHEN o.orderstatus = 'processing' THEN 1 ELSE 0 END) AS processing, SUM(CASE WHEN o.orderstatus = 'delivered' THEN 1 ELSE 0 END) AS delivered, SUM(CASE WHEN o.orderstatus = 'cancelled' THEN 1 ELSE 0 END) AS cancelled, - t.tenantid, + t.tenantid, t.tenantname FROM orders o INNER JOIN tenants t ON o.tenantid = t.tenantid ` - // Apply filters - if tid != 0 { - q1 = base + " WHERE o.configid = 1 AND o.tenantid = " + strconv.Itoa(tid) - } else if pid != 0 { - q1 = base + " WHERE o.configid = 1 AND o.partnerid = " + strconv.Itoa(pid) - } else if cid != 0 { - q1 = base + " WHERE o.configid = 1 AND o.customerid = " + strconv.Itoa(cid) - } else if lid != 0 { - q1 = base + " WHERE o.configid = 1 AND o.locationid = " + strconv.Itoa(lid) - } else { - q1 = base + " WHERE o.configid = 1" + var params []interface{} + var q1 string + + // Apply filters (at least one scoping id is required — enforced by the caller) + switch { + case tid != 0: + q1 = base + " WHERE o.configid = 1 AND o.tenantid = ?" + params = append(params, tid) + case pid != 0: + q1 = base + " WHERE o.configid = 1 AND o.partnerid = ?" + params = append(params, pid) + case cid != 0: + q1 = base + " WHERE o.configid = 1 AND o.customerid = ?" + params = append(params, cid) + case lid != 0: + q1 = base + " WHERE o.configid = 1 AND o.locationid = ?" + params = append(params, lid) + default: + return nil, fmt.Errorf("at least one of tenantid, partnerid, customerid or locationid is required") } // Date filter if fdate != "" && tdate != "" { - q1 += " AND o.orderdate::date BETWEEN '" + fdate + "' AND '" + tdate + "'" + q1 += " AND o.orderdate::date BETWEEN ? AND ?" + params = append(params, fdate, tdate) } // Group by tenant @@ -640,7 +647,7 @@ func (r *orderRepository) GetOrderSummary(tid, pid, cid, lid int, fdate, tdate s // Debug fmt.Println("Executing GetOrderSummary query:", q1) - if err := r.db.Raw(q1).Scan(&data).Error; err != nil { + if err := r.db.Raw(q1, params...).Scan(&data).Error; err != nil { return nil, err }