2026-04-19 14:18:20 +08:00
|
|
|
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),
|
2026-04-20 09:52:48 +08:00
|
|
|
repository.NewDesktopClientRepository(a.DB),
|
|
|
|
|
a.Redis,
|
2026-04-27 21:33:36 +08:00
|
|
|
a.RabbitMQ,
|
2026-04-19 14:18:20 +08:00
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 09:52:48 +08:00
|
|
|
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()))
|
2026-04-19 14:18:20 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 21:33:36 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
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)
|
|
|
|
|
}
|