102 lines
2.0 KiB
Go
102 lines
2.0 KiB
Go
// backend\operation_assignees.go
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type operationAssigneeUser struct {
|
|
ID string `json:"id"`
|
|
Username string `json:"username"`
|
|
DisplayName string `json:"displayName"`
|
|
Email string `json:"email"`
|
|
Avatar string `json:"avatar"`
|
|
Unit string `json:"unit"`
|
|
Group string `json:"group"`
|
|
}
|
|
|
|
func (s *Server) handleListOperationAssignees(w http.ResponseWriter, r *http.Request) {
|
|
unit := strings.TrimSpace(r.URL.Query().Get("unit"))
|
|
if unit == "" {
|
|
unit = "TEG"
|
|
}
|
|
|
|
leaders, err := s.listOperationAssigneeUsers(r.Context(), unit, "operation-leader")
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "Einsatzleiter konnten nicht geladen werden")
|
|
return
|
|
}
|
|
|
|
officers, err := s.listOperationAssigneeUsers(r.Context(), unit, "operation-officer")
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "Einsatzbeamte konnten nicht geladen werden")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"leaders": leaders,
|
|
"officers": officers,
|
|
})
|
|
}
|
|
|
|
func (s *Server) listOperationAssigneeUsers(
|
|
ctx context.Context,
|
|
unit string,
|
|
group string,
|
|
) ([]operationAssigneeUser, error) {
|
|
rows, err := s.db.Query(
|
|
ctx,
|
|
`
|
|
SELECT
|
|
id::TEXT,
|
|
COALESCE(username, ''),
|
|
COALESCE(display_name, ''),
|
|
email,
|
|
COALESCE(avatar, ''),
|
|
COALESCE(unit, ''),
|
|
COALESCE(user_group, '')
|
|
FROM users
|
|
WHERE upper(COALESCE(unit, '')) = upper($1)
|
|
AND COALESCE(user_group, '') = $2
|
|
ORDER BY
|
|
lower(COALESCE(NULLIF(display_name, ''), NULLIF(username, ''), email)) ASC,
|
|
lower(email) ASC
|
|
`,
|
|
unit,
|
|
group,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
users := []operationAssigneeUser{}
|
|
|
|
for rows.Next() {
|
|
var user operationAssigneeUser
|
|
|
|
if err := rows.Scan(
|
|
&user.ID,
|
|
&user.Username,
|
|
&user.DisplayName,
|
|
&user.Email,
|
|
&user.Avatar,
|
|
&user.Unit,
|
|
&user.Group,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
users = append(users, user)
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return users, nil
|
|
}
|