thread-master/apps/backend/internal/module/radar/usecase/sweep_notify.go

62 lines
1.8 KiB
Go

package usecase
import (
"context"
"fmt"
appnotifDomain "apps/backend/internal/module/appnotif/domain"
"github.com/google/uuid"
)
// AppNotifWriter is satisfied by appnotif usecase for system notifications.
type AppNotifWriter interface {
// InsertSystem creates a one-shot system notification.
InsertSystem(ctx context.Context, ownerUID int64, title, body, refType, refID string) error
}
// AppNotifBridge adapts appnotif.Service-like insert.
type AppNotifBridge struct {
Insert func(ctx context.Context, n *appnotifDomain.Notification) error
}
func (b *AppNotifBridge) InsertSystem(ctx context.Context, ownerUID int64, title, body, refType, refID string) error {
if b == nil || b.Insert == nil {
return nil
}
return b.Insert(ctx, &appnotifDomain.Notification{
ID: uuid.NewString(),
OwnerUID: ownerUID,
Title: title,
Body: body,
Kind: appnotifDomain.KindSystem,
RefType: refType,
RefID: refID,
CreatedAt: appnotifDomain.NowNano(),
})
}
// NotifierFromAppNotif builds SweepNotifier from appnotif bridge.
func NotifierFromAppNotif(w AppNotifWriter) SweepNotifier {
return sweepNotifyFunc(func(ctx context.Context, ownerUID int64, sweepID, watchID, reason string) error {
if w == nil {
return nil
}
title := "雷達巡檢失敗"
body := reason
if body == "" {
body = "今天的雷達巡檢沒有完成,請稍後重試或檢查抓取設定。"
}
return w.InsertSystem(ctx, ownerUID, title, body, "sweep", sweepID)
})
}
type sweepNotifyFunc func(ctx context.Context, ownerUID int64, sweepID, watchID, reason string) error
func (f sweepNotifyFunc) NotifySweepFailed(ctx context.Context, ownerUID int64, sweepID, watchID, reason string) error {
return f(ctx, ownerUID, sweepID, watchID, reason)
}
// Ensure compile-time string for watchID usage in future deep-links.
var _ = fmt.Sprintf