后端: 1. 阶段 4 active-scheduler 服务边界落地,新增 `cmd/active-scheduler`、`services/active_scheduler`、`shared/contracts/activescheduler` 和 active-scheduler port,迁移 dry-run、trigger、preview、confirm zrpc 能力 2. active-scheduler outbox consumer、relay、retry loop 和 due job scanner 迁入独立服务入口,gateway `/active-schedule/*` 改为通过 zrpc client 调用 3. gateway 目录收口为 `gateway/api` + `gateway/client`,统一归档 userauth、notification、active-scheduler 的 HTTP 门面和 zrpc client 4. 将旧 `backend/active_scheduler` 领域核心下沉到 `services/active_scheduler/core`,清退旧根目录活跃实现,并补充 active-scheduler 启动期跨域依赖表检查 5. 调整单体启动与 outbox 归属,`cmd/all` 不再启动 active-scheduler workflow、scanner 或 handler 文档: 1. 更新微服务迁移计划,将阶段 4 active-scheduler 标记为首轮收口完成,并明确下一阶段进入 schedule / task / course / task-class
129 lines
4.0 KiB
Go
129 lines
4.0 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/respond"
|
||
contracts "github.com/LoveLosita/smartflow/backend/shared/contracts/notification"
|
||
"github.com/LoveLosita/smartflow/backend/shared/ports"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
const notificationAPITimeout = 8 * time.Second
|
||
|
||
// NotificationAPI 承载当前用户的外部通知通道配置接口。
|
||
//
|
||
// 职责边界:
|
||
// 1. 只负责从 JWT 上下文取得当前 user_id、绑定请求体并调用 notification zrpc client;
|
||
// 2. 不直接读写 user_notification_channels,避免 API 层绕过 webhook 校验和脱敏规则;
|
||
// 3. 不参与主动调度、notification_records 状态机和 outbox 消费。
|
||
type NotificationAPI struct {
|
||
client ports.NotificationCommandClient
|
||
}
|
||
|
||
func NewNotificationAPI(client ports.NotificationCommandClient) *NotificationAPI {
|
||
return &NotificationAPI{client: client}
|
||
}
|
||
|
||
type saveFeishuWebhookRequest struct {
|
||
Enabled *bool `json:"enabled"`
|
||
WebhookURL string `json:"webhook_url" binding:"required"`
|
||
AuthType string `json:"auth_type"`
|
||
BearerToken string `json:"bearer_token"`
|
||
}
|
||
|
||
// GetFeishuWebhook 查询当前用户的飞书 Webhook 触发器配置。
|
||
func (api *NotificationAPI) GetFeishuWebhook(c *gin.Context) {
|
||
if api == nil || api.client == nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(nilServiceError("通知通道 service 未初始化")))
|
||
return
|
||
}
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), notificationAPITimeout)
|
||
defer cancel()
|
||
|
||
channel, err := api.client.GetFeishuWebhook(ctx, contracts.GetFeishuWebhookRequest{
|
||
UserID: c.GetInt("user_id"),
|
||
})
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, channel))
|
||
}
|
||
|
||
// SaveFeishuWebhook 幂等保存当前用户的飞书 Webhook 触发器配置。
|
||
func (api *NotificationAPI) SaveFeishuWebhook(c *gin.Context) {
|
||
if api == nil || api.client == nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(nilServiceError("通知通道 service 未初始化")))
|
||
return
|
||
}
|
||
|
||
var req saveFeishuWebhookRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
enabled := true
|
||
if req.Enabled != nil {
|
||
enabled = *req.Enabled
|
||
}
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), notificationAPITimeout)
|
||
defer cancel()
|
||
|
||
channel, err := api.client.SaveFeishuWebhook(ctx, contracts.SaveFeishuWebhookRequest{
|
||
UserID: c.GetInt("user_id"),
|
||
Enabled: enabled,
|
||
WebhookURL: req.WebhookURL,
|
||
AuthType: req.AuthType,
|
||
BearerToken: req.BearerToken,
|
||
})
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, channel))
|
||
}
|
||
|
||
// DeleteFeishuWebhook 删除当前用户的飞书 Webhook 触发器配置。
|
||
func (api *NotificationAPI) DeleteFeishuWebhook(c *gin.Context) {
|
||
if api == nil || api.client == nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(nilServiceError("通知通道 service 未初始化")))
|
||
return
|
||
}
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), notificationAPITimeout)
|
||
defer cancel()
|
||
|
||
if err := api.client.DeleteFeishuWebhook(ctx, contracts.DeleteFeishuWebhookRequest{
|
||
UserID: c.GetInt("user_id"),
|
||
}); err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, gin.H{"deleted": true}))
|
||
}
|
||
|
||
// TestFeishuWebhook 发送一条最小业务 JSON 到当前用户配置的飞书 Webhook。
|
||
func (api *NotificationAPI) TestFeishuWebhook(c *gin.Context) {
|
||
if api == nil || api.client == nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(nilServiceError("通知通道 service 未初始化")))
|
||
return
|
||
}
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), notificationAPITimeout)
|
||
defer cancel()
|
||
|
||
result, err := api.client.TestFeishuWebhook(ctx, contracts.TestFeishuWebhookRequest{
|
||
UserID: c.GetInt("user_id"),
|
||
})
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, result))
|
||
}
|