feat: PATCH /admin/customers/:id — customer update endpoint

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-09 12:10:49 +05:30
parent d7c0f35ffe
commit 5a2da49c35
2 changed files with 67 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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)