e307a048d1
Adds an ai_point_usage_logs ledger and a reserve/refund/complete pipeline that charges AI points for article selection optimize, template analyze /title/outline, and KOL prompt generate/optimize. Pending reservations are reconciled when the kol-assist worker and template-assist tasks finish or fail, so points refund automatically on errors. Workspace now exposes the AI quota status and a paginated usage ledger. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package transport
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/bootstrap"
|
|
"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 WorkspaceHandler struct {
|
|
svc *app.WorkspaceService
|
|
}
|
|
|
|
func NewWorkspaceHandler(a *bootstrap.App) *WorkspaceHandler {
|
|
return &WorkspaceHandler{
|
|
svc: app.NewWorkspaceService(
|
|
repository.NewWorkspaceRepository(a.DB),
|
|
repository.NewCachedTemplateRepository(
|
|
repository.NewTemplateRepository(a.DB),
|
|
a.Cache,
|
|
),
|
|
repository.NewQuotaRepository(a.DB),
|
|
repository.NewAIPointUsageRepository(a.DB),
|
|
).WithCache(a.Cache),
|
|
}
|
|
}
|
|
|
|
func (h *WorkspaceHandler) Overview(c *gin.Context) {
|
|
data, err := h.svc.Overview(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *WorkspaceHandler) RecentArticles(c *gin.Context) {
|
|
data, err := h.svc.RecentArticles(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *WorkspaceHandler) QuotaSummary(c *gin.Context) {
|
|
data, err := h.svc.QuotaSummary(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *WorkspaceHandler) AIPointUsageLogs(c *gin.Context) {
|
|
page := parseIntQuery(c.Query("page"), 1)
|
|
pageSize := parseIntQuery(c.Query("page_size"), 20)
|
|
if c.Query("page") == "" && c.Query("page_size") == "" && c.Query("limit") != "" {
|
|
limit := parseIntQuery(c.Query("limit"), 20)
|
|
offset := parseIntQuery(c.Query("offset"), 0)
|
|
if limit > 0 {
|
|
pageSize = limit
|
|
page = offset/limit + 1
|
|
}
|
|
}
|
|
|
|
data, err := h.svc.AIPointUsageLogs(
|
|
c.Request.Context(),
|
|
page,
|
|
pageSize,
|
|
)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *WorkspaceHandler) TemplateCards(c *gin.Context) {
|
|
data, err := h.svc.TemplateCards(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *WorkspaceHandler) KolCards(c *gin.Context) {
|
|
data, err := h.svc.KolCards(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|