package restricted import ( "strconv" "git.ma-al.com/goc_daniel/b2b/app/model" "git.ma-al.com/goc_daniel/b2b/app/service/orderService" "git.ma-al.com/goc_daniel/b2b/app/utils/i18n" "git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor" "git.ma-al.com/goc_daniel/b2b/app/utils/logger" "git.ma-al.com/goc_daniel/b2b/app/utils/nullable" "git.ma-al.com/goc_daniel/b2b/app/utils/query/query_params" "git.ma-al.com/goc_daniel/b2b/app/utils/response" "git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors" "github.com/gofiber/fiber/v3" ) type OrdersHandler struct { ordersService *orderService.OrderService } func NewOrdersHandler() *OrdersHandler { ordersService := orderService.New() return &OrdersHandler{ ordersService: ordersService, } } func OrdersHandlerRoutes(r fiber.Router) fiber.Router { handler := NewOrdersHandler() r.Get("/list", handler.ListOrders) r.Post("/place-new-order", handler.PlaceNewOrder) r.Post("/change-order-address", handler.ChangeOrderAddress) r.Get("/change-order-status", handler.ChangeOrderStatus) return r } // when a user (not admin) wants to list orders, we automatically append filter to only view his orders. // we base permissions and user based on target user only. func (h *OrdersHandler) ListOrders(c fiber.Ctx) error { user, ok := localeExtractor.GetCustomer(c) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } paging, filters, err := query_params.ParseFilters[model.CustomerOrder](c, columnMappingListOrders) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } list, err := h.ordersService.Find(user, paging, filters) if err != nil { logger.Error("failed to list orders", "handler", "OrdersHandler.ListOrders", "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(&list.Items, int(list.Count), i18n.T_(c, response.Message_OK))) } var columnMappingListOrders map[string]string = map[string]string{ "order_id": "b2b_customer_orders.order_id", "user_id": "b2b_customer_orders.user_id", "name": "b2b_customer_orders.name", "country_id": "b2b_customer_orders.country_id", "status": "b2b_customer_orders.status", } func (h *OrdersHandler) PlaceNewOrder(c fiber.Ctx) error { userID, ok := localeExtractor.GetUserID(c) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } cart_id_attribute := c.Query("cart_id") cart_id, err := strconv.Atoi(cart_id_attribute) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } country_id_attribute := c.Query("country_id") country_id, err := strconv.Atoi(country_id_attribute) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } address_info := string(c.Body()) if address_info == "" { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } name := c.Query("name") err = h.ordersService.PlaceNewOrder(userID, uint(cart_id), name, uint(country_id), address_info) if err != nil { logger.Error("failed to place order", "handler", "OrdersHandler.PlaceNewOrder", "user_id", userID, "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK))) } // we base permissions and user based on target user only. func (h *OrdersHandler) ChangeOrderAddress(c fiber.Ctx) error { user, ok := localeExtractor.GetCustomer(c) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } order_id_attribute := c.Query("order_id") order_id, err := strconv.Atoi(order_id_attribute) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } country_id_attribute := c.Query("country_id") country_id, err := strconv.Atoi(country_id_attribute) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } address_info := string(c.Body()) if address_info == "" { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } err = h.ordersService.ChangeOrderAddress(user, uint(order_id), uint(country_id), address_info) if err != nil { logger.Error("failed to change order address", "handler", "OrdersHandler.ChangeOrderAddress", "order_id", order_id, "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK))) } // we base permissions and user based on target user only. // TODO: well, permissions and all that. func (h *OrdersHandler) ChangeOrderStatus(c fiber.Ctx) error { user, ok := localeExtractor.GetCustomer(c) if !ok { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody))) } order_id_attribute := c.Query("order_id") order_id, err := strconv.Atoi(order_id_attribute) if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) } status := c.Query("status") err = h.ordersService.ChangeOrderStatus(user, uint(order_id), status) if err != nil { logger.Error("failed to change order status", "handler", "OrdersHandler.ChangeOrderStatus", "order_id", order_id, "error", err.Error(), ) return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK))) }