feat(notification): add dashboard notification center
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package event_handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"cs-agent/internal/events"
|
||||
"cs-agent/internal/pkg/dto/request"
|
||||
"cs-agent/internal/pkg/eventbus"
|
||||
"cs-agent/internal/services"
|
||||
|
||||
"github.com/mlogclub/simple/common/strs"
|
||||
)
|
||||
|
||||
func init() {
|
||||
eventbus.
|
||||
Register[events.TicketAssignedEvent]().
|
||||
Subscribe(handleTicketAssignedInAppNotification)
|
||||
eventbus.
|
||||
Register[events.ConversationAssignedEvent]().
|
||||
Subscribe(handleConversationAssignedInAppNotification)
|
||||
}
|
||||
|
||||
func handleTicketAssignedInAppNotification(ctx context.Context, event events.TicketAssignedEvent) error {
|
||||
if event.TicketID <= 0 || event.ToUserID <= 0 {
|
||||
return nil
|
||||
}
|
||||
ticket := services.TicketService.Get(event.TicketID)
|
||||
if ticket == nil {
|
||||
return nil
|
||||
}
|
||||
content := fmt.Sprintf("工单 %s 已指派给你", strs.DefaultIfBlank(ticket.TicketNo, fmt.Sprintf("#%d", ticket.ID)))
|
||||
if title := strings.TrimSpace(ticket.Title); title != "" {
|
||||
content = content + "\n" + title
|
||||
}
|
||||
if reason := strings.TrimSpace(event.Reason); reason != "" {
|
||||
content = content + "\n指派原因: " + reason
|
||||
}
|
||||
_, err := services.NotificationService.CreateAndPush(request.CreateNotificationRequest{
|
||||
RecipientUserID: event.ToUserID,
|
||||
Title: "工单指派提醒",
|
||||
Content: content,
|
||||
NotificationType: "ticket_assigned",
|
||||
BizType: "ticket",
|
||||
BizID: ticket.ID,
|
||||
ActionURL: fmt.Sprintf("/dashboard/tickets/%d", ticket.ID),
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("create ticket assigned in-app notification failed", "error", err, "ticketId", event.TicketID, "toUserId", event.ToUserID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleConversationAssignedInAppNotification(ctx context.Context, event events.ConversationAssignedEvent) error {
|
||||
if event.ConversationID <= 0 || event.ToUserID <= 0 {
|
||||
return nil
|
||||
}
|
||||
conversation := services.ConversationService.Get(event.ConversationID)
|
||||
if conversation == nil {
|
||||
return nil
|
||||
}
|
||||
content := fmt.Sprintf("会话 #%d 已分配给你", conversation.ID)
|
||||
if summary := strings.TrimSpace(services.ConversationService.BuildConversationSummary(conversation)); summary != "" {
|
||||
content = content + "\n" + summary
|
||||
}
|
||||
if reason := strings.TrimSpace(event.Reason); reason != "" {
|
||||
content = content + "\n分配原因: " + reason
|
||||
}
|
||||
_, err := services.NotificationService.CreateAndPush(request.CreateNotificationRequest{
|
||||
RecipientUserID: event.ToUserID,
|
||||
Title: conversationAssignedNotifyTitle(event.AssignType),
|
||||
Content: content,
|
||||
NotificationType: "conversation_assigned",
|
||||
BizType: "conversation",
|
||||
BizID: conversation.ID,
|
||||
ActionURL: fmt.Sprintf("/dashboard/conversations?conversationId=%d", conversation.ID),
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("create conversation assigned in-app notification failed", "error", err, "conversationId", event.ConversationID, "toUserId", event.ToUserID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package event_handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"cs-agent/internal/events"
|
||||
"cs-agent/internal/models"
|
||||
"cs-agent/internal/pkg/enums"
|
||||
"cs-agent/internal/repositories"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/mlogclub/simple/sqls"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
func TestTicketAssignedInAppNotification(t *testing.T) {
|
||||
setupNotificationEventHandlerTestDB(t)
|
||||
|
||||
ticket := &models.Ticket{
|
||||
TicketNo: "TK202604280001",
|
||||
Title: "退款处理",
|
||||
Source: enums.TicketSourceManual,
|
||||
Status: enums.TicketStatusOpen,
|
||||
CurrentAssigneeID: 11,
|
||||
AuditFields: models.AuditFields{
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
},
|
||||
}
|
||||
if err := repositories.TicketRepository.Create(sqls.DB(), ticket); err != nil {
|
||||
t.Fatalf("create ticket error = %v", err)
|
||||
}
|
||||
|
||||
if err := handleTicketAssignedInAppNotification(context.Background(), events.TicketAssignedEvent{
|
||||
TicketID: ticket.ID,
|
||||
FromUserID: 0,
|
||||
ToUserID: 11,
|
||||
OperatorID: 1,
|
||||
Reason: "需要人工跟进",
|
||||
}); err != nil {
|
||||
t.Fatalf("handler error = %v", err)
|
||||
}
|
||||
|
||||
list := repositories.NotificationRepository.Find(sqls.DB(), sqls.NewCnd().Eq("recipient_user_id", 11))
|
||||
if len(list) != 1 {
|
||||
t.Fatalf("expected 1 notification, got %d", len(list))
|
||||
}
|
||||
got := list[0]
|
||||
if got.NotificationType != "ticket_assigned" || got.BizType != "ticket" || got.BizID != ticket.ID {
|
||||
t.Fatalf("unexpected notification: %+v", got)
|
||||
}
|
||||
if got.ActionURL != "/dashboard/tickets/1" {
|
||||
t.Fatalf("unexpected action url: %q", got.ActionURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConversationAssignedInAppNotification(t *testing.T) {
|
||||
setupNotificationEventHandlerTestDB(t)
|
||||
|
||||
conversation := &models.Conversation{
|
||||
CustomerName: "张三",
|
||||
Status: enums.IMConversationStatusActive,
|
||||
CurrentAssigneeID: 22,
|
||||
AuditFields: models.AuditFields{
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
},
|
||||
}
|
||||
if err := repositories.ConversationRepository.Create(sqls.DB(), conversation); err != nil {
|
||||
t.Fatalf("create conversation error = %v", err)
|
||||
}
|
||||
|
||||
if err := handleConversationAssignedInAppNotification(context.Background(), events.ConversationAssignedEvent{
|
||||
ConversationID: conversation.ID,
|
||||
FromUserID: 0,
|
||||
ToUserID: 22,
|
||||
OperatorID: 1,
|
||||
Reason: "自动分配",
|
||||
AssignType: events.ConversationAssignTypeAutoAssign,
|
||||
}); err != nil {
|
||||
t.Fatalf("handler error = %v", err)
|
||||
}
|
||||
|
||||
list := repositories.NotificationRepository.Find(sqls.DB(), sqls.NewCnd().Eq("recipient_user_id", 22))
|
||||
if len(list) != 1 {
|
||||
t.Fatalf("expected 1 notification, got %d", len(list))
|
||||
}
|
||||
got := list[0]
|
||||
if got.NotificationType != "conversation_assigned" || got.BizType != "conversation" || got.BizID != conversation.ID {
|
||||
t.Fatalf("unexpected notification: %+v", got)
|
||||
}
|
||||
if got.ActionURL != "/dashboard/conversations?conversationId=1" {
|
||||
t.Fatalf("unexpected action url: %q", got.ActionURL)
|
||||
}
|
||||
}
|
||||
|
||||
func setupNotificationEventHandlerTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: "t_",
|
||||
SingularTable: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
sqlDB, err := db.DB()
|
||||
if err == nil {
|
||||
_ = sqlDB.Close()
|
||||
}
|
||||
})
|
||||
if err := db.AutoMigrate(&models.Notification{}, &models.Ticket{}, &models.Conversation{}); err != nil {
|
||||
t.Fatalf("auto migrate error = %v", err)
|
||||
}
|
||||
sqls.SetDB(db)
|
||||
return db
|
||||
}
|
||||
Reference in New Issue
Block a user