97 lines
2.8 KiB
Go
97 lines
2.8 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"chat-agent-system/internal/auth"
|
|
)
|
|
|
|
type userProfilePatch struct {
|
|
Name *string `json:"name,omitempty"`
|
|
Email *string `json:"email,omitempty"`
|
|
Department *string `json:"department,omitempty"`
|
|
Locale *string `json:"locale,omitempty"`
|
|
Timezone *string `json:"timezone,omitempty"`
|
|
}
|
|
|
|
// handleMeProfile GET/PATCH /api/v1/me/profile
|
|
func (s *Server) handleMeProfile(w http.ResponseWriter, r *http.Request) {
|
|
user, ok := s.requirePrincipal(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
resp := map[string]any{
|
|
"id": user.ID, "username": user.Username, "name": user.Name,
|
|
"email": user.Email, "locale": user.Locale, "timezone": user.Timezone,
|
|
"department": user.Department, "employee_id": user.EmployeeID, "position": user.Position,
|
|
}
|
|
if user.ProfileUpdatedBy > 0 {
|
|
resp["profile_updated_by"] = user.ProfileUpdatedBy
|
|
resp["profile_updated_at"] = user.ProfileUpdatedAt
|
|
if admin, err := s.authSvc().GetUserByID(r.Context(), user.ProfileUpdatedBy); err == nil && admin != nil {
|
|
name := strings.TrimSpace(admin.Name)
|
|
if name == "" {
|
|
name = admin.Username
|
|
}
|
|
resp["profile_updated_by_name"] = name
|
|
}
|
|
}
|
|
writeJSONResponse(w, http.StatusOK, resp)
|
|
case http.MethodPatch:
|
|
var patch userProfilePatch
|
|
if err := json.NewDecoder(r.Body).Decode(&patch); err != nil {
|
|
writeSafeError(w, r, http.StatusBadRequest, "invalid body")
|
|
return
|
|
}
|
|
saved, err := s.authSvc().UpdateUserProfile(r.Context(), user.ID, auth.ProfilePatch{
|
|
Name: patch.Name,
|
|
Email: patch.Email,
|
|
Department: patch.Department,
|
|
Locale: patch.Locale,
|
|
Timezone: patch.Timezone,
|
|
})
|
|
if err != nil {
|
|
writeDomainError(w, r, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
writeJSONResponse(w, http.StatusOK, saved)
|
|
default:
|
|
writeSafeError(w, r, http.StatusMethodNotAllowed, "method not allowed")
|
|
}
|
|
}
|
|
|
|
// handleMePreferences GET/PUT /api/v1/me/preferences
|
|
func (s *Server) handleMePreferences(w http.ResponseWriter, r *http.Request) {
|
|
user, ok := s.requirePrincipal(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
prefs, err := s.authSvc().GetUserPreferences(r.Context(), user.ID)
|
|
if err != nil {
|
|
writeDomainError(w, r, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
writeJSONResponse(w, http.StatusOK, prefs)
|
|
case http.MethodPut:
|
|
var body map[string]any
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
writeSafeError(w, r, http.StatusBadRequest, "invalid body")
|
|
return
|
|
}
|
|
prefs, err := s.authSvc().SaveUserPreferences(r.Context(), user.ID, body)
|
|
if err != nil {
|
|
writeDomainError(w, r, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
writeJSONResponse(w, http.StatusOK, prefs)
|
|
default:
|
|
writeSafeError(w, r, http.StatusMethodNotAllowed, "method not allowed")
|
|
}
|
|
}
|