diff --git a/controllers/adminController.go b/controllers/adminController.go index daa7fcb..1e0e1ea 100644 --- a/controllers/adminController.go +++ b/controllers/adminController.go @@ -516,6 +516,72 @@ func GetAdminCustomers(c *fiber.Ctx) error { }) } +func UpdateAdminCustomer(c *fiber.Ctx) error { + id, err := strconv.Atoi(c.Params("id")) + if err != nil { + return utils.BadRequest(c, "invalid customer id") + } + + var customer models.AppCustomer + if err := db.DB.First(&customer, id).Error; err != nil { + return utils.NotFound(c, "customer not found") + } + + req := new(struct { + Name string `json:"name"` + Phone string `json:"phone"` + Email string `json:"email"` + }) + if err := c.BodyParser(req); err != nil { + return utils.BadRequest(c, "invalid request body") + } + + if req.Phone != "" { + if len(req.Phone) != 10 || strings.IndexFunc(req.Phone, func(r rune) bool { return r < '0' || r > '9' }) != -1 { + return utils.BadRequest(c, "phone must be 10 digits") + } + customer.Phone = req.Phone + } + + if req.Name != "" { + name := strings.TrimSpace(req.Name) + parts := strings.SplitN(name, " ", 2) + customer.Firstname = parts[0] + if len(parts) > 1 { + customer.Lastname = parts[1] + } else { + customer.Lastname = "" + } + } + + if req.Email != "" { + customer.Email = req.Email + } + + customer.Updatedat = time.Now() + + if err := db.DB.Model(&models.AppCustomer{}).Where("appcustomerid = ?", customer.Appcustomerid). + Updates(map[string]interface{}{ + "firstname": customer.Firstname, + "lastname": customer.Lastname, + "phone": customer.Phone, + "email": customer.Email, + "updatedat": customer.Updatedat, + }).Error; err != nil { + return utils.Internal(c, "failed to update customer") + } + + return c.JSON(fiber.Map{ + "success": true, + "data": fiber.Map{ + "appcustomerid": customer.Appcustomerid, + "name": strings.TrimSpace(customer.Firstname + " " + customer.Lastname), + "phone": customer.Phone, + "email": customer.Email, + }, + }) +} + func CreateTenantCustomer(c *fiber.Ctx) error { req := new(dto.TenantCustomerCreateRequest) if err := c.BodyParser(req); err != nil { diff --git a/routes/routes.go b/routes/routes.go index ae442bb..8d15f1f 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -164,6 +164,7 @@ func RegisterRoutes(app *fiber.App, cfg *config.Config) { // B2C App customers adminAuth.Get("/customers", controllers.GetAdminCustomers) + adminAuth.Patch("/customers/:id", controllers.UpdateAdminCustomer) // Hubs adminAuth.Get("/hubs", controllers.GetHubs)