后端:
1.阶段 6 CP4/CP5 目录收口与共享边界纯化
- 将 backend 根目录收口为 services、client、gateway、cmd、shared 五个一级目录
- 收拢 bootstrap、inits、infra/kafka、infra/outbox、conv、respond、pkg、middleware,移除根目录旧实现与空目录
- 将 utils 下沉到 services/userauth/internal/auth,将 logic 下沉到 services/schedule/core/planning
- 将迁移期 runtime 桥接实现统一收拢到 services/runtime/{conv,dao,eventsvc,model},删除 shared/legacy 与未再被 import 的旧 service 实现
- 将 gateway/shared/respond 收口为 HTTP/Gin 错误写回适配,shared/respond 仅保留共享错误语义与状态映射
- 将 HTTP IdempotencyMiddleware 与 RateLimitMiddleware 收口到 gateway/middleware
- 将 GormCachePlugin 下沉到 shared/infra/gormcache,将共享 RateLimiter 下沉到 shared/infra/ratelimit,将 agent token budget 下沉到 services/agent/shared
- 删除 InitEino 兼容壳,收缩 cmd/internal/coreinit 仅保留旧组合壳残留域初始化语义
- 更新微服务迁移计划与桌面 checklist,补齐 CP4/CP5 当前切流点、目录终态与验证结果
- 完成 go test ./...、git diff --check 与最终真实 smoke;health、register/login、task/create+get、schedule/today、task-class/list、memory/items、agent chat/meta/timeline/context-stats 全部 200,SSE 合并结果为 CP5_OK 且 [DONE] 只有 1 个
134 lines
4.1 KiB
Go
134 lines
4.1 KiB
Go
package rpc
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"time"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/services/notification/rpc/pb"
|
||
notificationsv "github.com/LoveLosita/smartflow/backend/services/notification/sv"
|
||
contracts "github.com/LoveLosita/smartflow/backend/shared/contracts/notification"
|
||
"github.com/LoveLosita/smartflow/backend/shared/respond"
|
||
)
|
||
|
||
type Handler struct {
|
||
pb.UnimplementedNotificationServer
|
||
svc *notificationsv.Service
|
||
}
|
||
|
||
func NewHandler(svc *notificationsv.Service) *Handler {
|
||
return &Handler{svc: svc}
|
||
}
|
||
|
||
// GetFeishuWebhook 负责把配置查询请求从 gRPC 协议转成内部服务调用。
|
||
//
|
||
// 职责边界:
|
||
// 1. 只做 transport -> service 的参数搬运,不碰 DAO/provider/outbox 细节;
|
||
// 2. 业务错误统一转成 gRPC status,让 client 侧继续使用 `res, err :=`;
|
||
// 3. 成功时只回传业务数据,不在 payload 里塞 status/info。
|
||
func (h *Handler) GetFeishuWebhook(ctx context.Context, req *pb.GetFeishuWebhookRequest) (*pb.ChannelResponse, error) {
|
||
if h == nil || h.svc == nil {
|
||
return nil, grpcErrorFromServiceError(errors.New("notification service dependency not initialized"))
|
||
}
|
||
if req == nil {
|
||
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
||
}
|
||
|
||
resp, err := h.svc.GetFeishuWebhook(ctx, int(req.UserId))
|
||
if err != nil {
|
||
return nil, grpcErrorFromServiceError(err)
|
||
}
|
||
return channelToPB(resp), nil
|
||
}
|
||
|
||
func (h *Handler) SaveFeishuWebhook(ctx context.Context, req *pb.SaveFeishuWebhookRequest) (*pb.ChannelResponse, error) {
|
||
if h == nil || h.svc == nil {
|
||
return nil, grpcErrorFromServiceError(errors.New("notification service dependency not initialized"))
|
||
}
|
||
if req == nil {
|
||
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
||
}
|
||
|
||
resp, err := h.svc.SaveFeishuWebhook(ctx, int(req.UserId), contracts.SaveFeishuWebhookRequest{
|
||
UserID: int(req.UserId),
|
||
Enabled: req.Enabled,
|
||
WebhookURL: req.WebhookUrl,
|
||
AuthType: req.AuthType,
|
||
BearerToken: req.BearerToken,
|
||
})
|
||
if err != nil {
|
||
return nil, grpcErrorFromServiceError(err)
|
||
}
|
||
return channelToPB(resp), nil
|
||
}
|
||
|
||
func (h *Handler) DeleteFeishuWebhook(ctx context.Context, req *pb.DeleteFeishuWebhookRequest) (*pb.StatusResponse, error) {
|
||
if h == nil || h.svc == nil {
|
||
return nil, grpcErrorFromServiceError(errors.New("notification service dependency not initialized"))
|
||
}
|
||
if req == nil {
|
||
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
||
}
|
||
|
||
if err := h.svc.DeleteFeishuWebhook(ctx, int(req.UserId)); err != nil {
|
||
return nil, grpcErrorFromServiceError(err)
|
||
}
|
||
return &pb.StatusResponse{}, nil
|
||
}
|
||
|
||
func (h *Handler) TestFeishuWebhook(ctx context.Context, req *pb.TestFeishuWebhookRequest) (*pb.TestResult, error) {
|
||
if h == nil || h.svc == nil {
|
||
return nil, grpcErrorFromServiceError(errors.New("notification service dependency not initialized"))
|
||
}
|
||
if req == nil {
|
||
return nil, grpcErrorFromServiceError(respond.MissingParam)
|
||
}
|
||
|
||
resp, err := h.svc.TestFeishuWebhook(ctx, int(req.UserId))
|
||
if err != nil {
|
||
return nil, grpcErrorFromServiceError(err)
|
||
}
|
||
return testResultToPB(resp), nil
|
||
}
|
||
|
||
func channelToPB(resp contracts.ChannelResponse) *pb.ChannelResponse {
|
||
return &pb.ChannelResponse{
|
||
Channel: resp.Channel,
|
||
Enabled: resp.Enabled,
|
||
Configured: resp.Configured,
|
||
WebhookUrlMask: resp.WebhookURLMask,
|
||
AuthType: resp.AuthType,
|
||
HasBearerToken: resp.HasBearerToken,
|
||
LastTestStatus: resp.LastTestStatus,
|
||
LastTestError: resp.LastTestError,
|
||
LastTestAtUnixNano: timePtrToUnixNano(resp.LastTestAt),
|
||
}
|
||
}
|
||
|
||
func testResultToPB(resp contracts.TestResult) *pb.TestResult {
|
||
return &pb.TestResult{
|
||
Channel: channelToPB(resp.Channel),
|
||
Status: resp.Status,
|
||
Outcome: resp.Outcome,
|
||
Message: resp.Message,
|
||
TraceId: resp.TraceID,
|
||
SentAtUnixNano: timeToUnixNano(resp.SentAt),
|
||
Skipped: resp.Skipped,
|
||
Provider: resp.Provider,
|
||
}
|
||
}
|
||
|
||
func timePtrToUnixNano(value *time.Time) int64 {
|
||
if value == nil || value.IsZero() {
|
||
return 0
|
||
}
|
||
return value.UnixNano()
|
||
}
|
||
|
||
func timeToUnixNano(value time.Time) int64 {
|
||
if value.IsZero() {
|
||
return 0
|
||
}
|
||
return value.UnixNano()
|
||
}
|