051976e4a9
Add a POST /desktop/accounts/health-reports endpoint that buffers reports in Redis, dedupes via signature, and publishes change events to a new desktop.account.health RabbitMQ queue. A sink worker drains the queue, and the account service overlays runtime health onto database health when serving desktop accounts. Publish job creation now consults runtime health so a stale DB row no longer blocks publishing once the desktop client reports it healthy. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
141 lines
4.0 KiB
Go
141 lines
4.0 KiB
Go
package transport
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/bootstrap"
|
|
"github.com/geo-platform/tenant-api/internal/shared/auth"
|
|
"github.com/geo-platform/tenant-api/internal/shared/response"
|
|
"github.com/geo-platform/tenant-api/internal/tenant/app"
|
|
"github.com/geo-platform/tenant-api/internal/tenant/repository"
|
|
)
|
|
|
|
type DesktopAccountHandler struct {
|
|
svc *app.DesktopAccountService
|
|
}
|
|
|
|
func NewDesktopAccountHandler(a *bootstrap.App) *DesktopAccountHandler {
|
|
return &DesktopAccountHandler{
|
|
svc: app.NewDesktopAccountService(
|
|
repository.NewDesktopAccountRepository(a.DB),
|
|
repository.NewDesktopClientRepository(a.DB),
|
|
a.Redis,
|
|
a.RabbitMQ,
|
|
),
|
|
}
|
|
}
|
|
|
|
func (h *DesktopAccountHandler) List(c *gin.Context) {
|
|
if clientQuery := c.Query("client"); clientQuery != "" && clientQuery != "self" {
|
|
response.Error(c, response.ErrBadRequest(40081, "invalid_client_query", "client query must be self"))
|
|
return
|
|
}
|
|
|
|
data, err := h.svc.ListByUser(c.Request.Context(), MustDesktopClient(c.Request.Context()))
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *DesktopAccountHandler) TenantList(c *gin.Context) {
|
|
data, err := h.svc.ListForActor(c.Request.Context(), auth.MustActor(c.Request.Context()))
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *DesktopAccountHandler) Upsert(c *gin.Context) {
|
|
var req app.UpsertDesktopAccountRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
|
|
data, err := h.svc.Upsert(c.Request.Context(), MustDesktopClient(c.Request.Context()), req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.SuccessWithStatus(c, 201, data)
|
|
}
|
|
|
|
func (h *DesktopAccountHandler) ReportHealth(c *gin.Context) {
|
|
var req app.ReportDesktopAccountHealthRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
|
|
data, err := h.svc.ReportHealth(c.Request.Context(), MustDesktopClient(c.Request.Context()), req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *DesktopAccountHandler) Patch(c *gin.Context) {
|
|
desktopID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40082, "invalid_desktop_account_id", "desktop account id must be a uuid"))
|
|
return
|
|
}
|
|
|
|
var req app.PatchDesktopAccountRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
|
|
data, err := h.svc.Patch(c.Request.Context(), MustDesktopClient(c.Request.Context()), desktopID, req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *DesktopAccountHandler) Delete(c *gin.Context) {
|
|
desktopID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40082, "invalid_desktop_account_id", "desktop account id must be a uuid"))
|
|
return
|
|
}
|
|
|
|
ifSyncVersion, err := strconv.ParseInt(c.Query("if_sync_version"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40083, "invalid_sync_version", "if_sync_version must be an integer"))
|
|
return
|
|
}
|
|
|
|
data, err := h.svc.Tombstone(c.Request.Context(), MustDesktopClient(c.Request.Context()), desktopID, ifSyncVersion)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *DesktopAccountHandler) RequestDelete(c *gin.Context) {
|
|
desktopID, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40082, "invalid_desktop_account_id", "desktop account id must be a uuid"))
|
|
return
|
|
}
|
|
|
|
actor := auth.MustActor(c.Request.Context())
|
|
data, err := h.svc.RequestDelete(c.Request.Context(), actor, desktopID, c.Query("undo") == "true")
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|