feat: PATCH /admin/customers/:id — customer update endpoint
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user