Refactor Notification Management and Enhance Customer Features
- Updated the notification handling logic to utilize a new SDK for sending notifications, improving the flexibility and scalability of the notification system. - Introduced new methods in the notification service for sending notifications to users and customers based on various target audience types, enhancing the notification delivery capabilities. - Added a new endpoint to assign companies to a customer, improving customer management functionalities. - Refactored the customer entity and forms to replace 'CompanyIDs' with 'Companies', ensuring consistency across the data model. - Enhanced API documentation with Swagger comments for the new endpoint and updated notification structures, ensuring clarity for API consumers.
This commit is contained in:
+209
-164
@@ -3,197 +3,242 @@ package notification
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"tm/internal/customer"
|
||||
"tm/internal/user"
|
||||
"tm/pkg/logger"
|
||||
"tm/pkg/notification"
|
||||
"tm/pkg/response"
|
||||
)
|
||||
|
||||
// NotificationService defines the interface for notification business logic
|
||||
type NotificationService interface {
|
||||
Create(ctx context.Context, req *NotificationRequest) (*NotificationResponse, error)
|
||||
Update(ctx context.Context, id string, req *NotificationRequest) (*NotificationResponse, error)
|
||||
Get(ctx context.Context, id string) (*NotificationResponse, error)
|
||||
Search(ctx context.Context, req *SearchForm, pagination *response.Pagination) (*NotificationsResponse, error)
|
||||
Delete(ctx context.Context, id string) error
|
||||
type Service interface {
|
||||
Send(ctx context.Context, req *NotificationRequest) error
|
||||
getUsers(ctx context.Context, userIDs []string) ([]notificationRecipient, error)
|
||||
getCustomers(ctx context.Context, values []string, target string) ([]notificationRecipient, error)
|
||||
}
|
||||
|
||||
// notificationService implements NotificationService interface
|
||||
type notificationService struct {
|
||||
repository NotificationRepository
|
||||
logger logger.Logger
|
||||
sdk notification.SDK
|
||||
userService user.Service
|
||||
customerService customer.Service
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
// NewNotificationService creates a new notification service
|
||||
func NewNotificationService(
|
||||
repository NotificationRepository,
|
||||
sdk notification.SDK,
|
||||
userService user.Service,
|
||||
customerService customer.Service,
|
||||
logger logger.Logger,
|
||||
) NotificationService {
|
||||
) Service {
|
||||
return ¬ificationService{
|
||||
repository: repository,
|
||||
logger: logger,
|
||||
sdk: sdk,
|
||||
userService: userService,
|
||||
customerService: customerService,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Create creates a single notification
|
||||
func (s *notificationService) Create(ctx context.Context, req *NotificationRequest) (*NotificationResponse, error) {
|
||||
s.logger.Info("Creating notification", map[string]interface{}{
|
||||
// Send sends a multiple notification
|
||||
func (s *notificationService) Send(ctx context.Context, req *NotificationRequest) error {
|
||||
s.logger.Info("Sending notification", map[string]interface{}{
|
||||
"recipient": req.Recipient,
|
||||
"type": req.Type,
|
||||
"priority": req.Priority,
|
||||
})
|
||||
|
||||
// Create notification entity
|
||||
notification := &Notification{
|
||||
Recipient: req.Recipient,
|
||||
Tender: req.Tender,
|
||||
Channels: req.Channels,
|
||||
Type: req.Type,
|
||||
Priority: req.Priority,
|
||||
Title: req.Title,
|
||||
Description: req.Description,
|
||||
Link: req.Link,
|
||||
Status: DeliveryStatusPending,
|
||||
Schedule: Schedule{
|
||||
Time: req.Schedule.Time,
|
||||
},
|
||||
}
|
||||
if req.Target == TargetAudienceAllUsers || req.Target == TargetAudienceSpecificUsers {
|
||||
recipients, err := s.getUsers(ctx, req.Recipient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, recipient := range recipients {
|
||||
for _, v := range recipient.DeviceTokens {
|
||||
s.sdk.SendNotification(ctx, ¬ification.NotificationRequest{
|
||||
UserID: recipient.UserID,
|
||||
Title: req.Title,
|
||||
Message: req.Description,
|
||||
Type: string(req.Type),
|
||||
Priority: string(req.Priority),
|
||||
EventType: notification.EventTypePush,
|
||||
Metadata: map[string]any{
|
||||
"tender": req.Tender,
|
||||
},
|
||||
Methods: notification.NotificationMethods{
|
||||
Push: v,
|
||||
},
|
||||
|
||||
// Create notification in repository
|
||||
if err := s.repository.Create(ctx, notification); err != nil {
|
||||
s.logger.Error("Failed to create notification", map[string]interface{}{
|
||||
ScheduledAt: req.ScheduleAt,
|
||||
})
|
||||
}
|
||||
|
||||
if recipient.Email != "" {
|
||||
s.sdk.SendNotification(ctx, ¬ification.NotificationRequest{
|
||||
UserID: recipient.UserID,
|
||||
Title: req.Title,
|
||||
Message: req.Description,
|
||||
Type: string(req.Type),
|
||||
Priority: string(req.Priority),
|
||||
EventType: notification.EventTypeEmail,
|
||||
Metadata: map[string]any{
|
||||
"tender": req.Tender,
|
||||
},
|
||||
Methods: notification.NotificationMethods{
|
||||
Email: recipient.Email,
|
||||
},
|
||||
ScheduledAt: req.ScheduleAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
recipients, err := s.getCustomers(ctx, req.Recipient, string(req.Target))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, recipient := range recipients {
|
||||
for _, v := range recipient.DeviceTokens {
|
||||
s.sdk.SendNotification(ctx, ¬ification.NotificationRequest{
|
||||
UserID: recipient.UserID,
|
||||
Title: req.Title,
|
||||
Message: req.Description,
|
||||
Type: string(req.Type),
|
||||
Priority: string(req.Priority),
|
||||
EventType: notification.EventTypePush,
|
||||
Methods: notification.NotificationMethods{
|
||||
Push: v,
|
||||
},
|
||||
ScheduledAt: req.ScheduleAt,
|
||||
})
|
||||
}
|
||||
|
||||
if recipient.Email != "" {
|
||||
s.sdk.SendNotification(ctx, ¬ification.NotificationRequest{
|
||||
UserID: recipient.UserID,
|
||||
Title: req.Title,
|
||||
Message: req.Description,
|
||||
Type: string(req.Type),
|
||||
Priority: string(req.Priority),
|
||||
EventType: notification.EventTypeEmail,
|
||||
Methods: notification.NotificationMethods{
|
||||
Email: recipient.Email,
|
||||
},
|
||||
ScheduledAt: req.ScheduleAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
s.logger.Info("Notification sent", map[string]interface{}{
|
||||
"recipient": req.Recipient,
|
||||
"error": err.Error(),
|
||||
"channels": req.Channels,
|
||||
})
|
||||
return nil, fmt.Errorf("failed to create notification")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
s.logger.Info("Notification created", map[string]interface{}{
|
||||
"notification_id": notification.ID,
|
||||
"recipient": req.Recipient,
|
||||
"channels": req.Channels,
|
||||
})
|
||||
|
||||
return notification.ToResponse(), nil
|
||||
}
|
||||
|
||||
// Update updates a notification
|
||||
func (s *notificationService) Update(ctx context.Context, id string, req *NotificationRequest) (*NotificationResponse, error) {
|
||||
s.logger.Info("Updating notification", map[string]interface{}{
|
||||
"notification_id": id,
|
||||
})
|
||||
|
||||
notification, err := s.repository.Get(ctx, id)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get notification", map[string]interface{}{
|
||||
"notification_id": id,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, fmt.Errorf("failed to get notification")
|
||||
}
|
||||
|
||||
if notification.Status == DeliveryStatusSent {
|
||||
return nil, fmt.Errorf("notification already sent")
|
||||
}
|
||||
|
||||
// update notification fields
|
||||
notification.Recipient = req.Recipient
|
||||
notification.Tender = req.Tender
|
||||
notification.Channels = req.Channels
|
||||
notification.Type = req.Type
|
||||
notification.Priority = req.Priority
|
||||
notification.Title = req.Title
|
||||
notification.Description = req.Description
|
||||
notification.Link = req.Link
|
||||
notification.Schedule = Schedule{
|
||||
Time: req.Schedule.Time,
|
||||
}
|
||||
notification.Status = DeliveryStatusPending
|
||||
|
||||
// update notification in repository
|
||||
if err := s.repository.Update(ctx, notification); err != nil {
|
||||
s.logger.Error("Failed to update notification", map[string]interface{}{
|
||||
"notification_id": id,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, fmt.Errorf("failed to update notification")
|
||||
}
|
||||
|
||||
s.logger.Info("Notification updated", map[string]interface{}{
|
||||
"notification_id": id,
|
||||
"recipient": req.Recipient,
|
||||
"channels": req.Channels,
|
||||
})
|
||||
|
||||
return notification.ToResponse(), nil
|
||||
}
|
||||
|
||||
// Get gets a notification
|
||||
func (s *notificationService) Get(ctx context.Context, id string) (*NotificationResponse, error) {
|
||||
s.logger.Info("Getting notification", map[string]interface{}{
|
||||
"notification_id": id,
|
||||
})
|
||||
|
||||
notification, err := s.repository.Get(ctx, id)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get notification", map[string]interface{}{
|
||||
"notification_id": id,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, fmt.Errorf("failed to get notification")
|
||||
}
|
||||
|
||||
return notification.ToResponse(), nil
|
||||
}
|
||||
|
||||
// Search searches for notifications
|
||||
func (s *notificationService) Search(ctx context.Context, req *SearchForm, pagination *response.Pagination) (*NotificationsResponse, error) {
|
||||
notifications, total, err := s.repository.Search(ctx, req, pagination)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to search notifications", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, errors.New("failed to search notifications")
|
||||
}
|
||||
|
||||
var notificationResponses []*NotificationResponse
|
||||
for _, notification := range notifications {
|
||||
notificationResponses = append(notificationResponses, notification.ToResponse())
|
||||
}
|
||||
|
||||
return &NotificationsResponse{
|
||||
Notifications: notificationResponses,
|
||||
Meta: pagination.Response(total),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Delete deletes a notification
|
||||
func (s *notificationService) Delete(ctx context.Context, id string) error {
|
||||
s.logger.Info("Deleting notification", map[string]interface{}{
|
||||
"notification_id": id,
|
||||
})
|
||||
|
||||
notification, err := s.repository.Get(ctx, id)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to get notification", map[string]interface{}{
|
||||
"notification_id": id,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return fmt.Errorf("failed to get notification")
|
||||
}
|
||||
|
||||
if notification.Status == DeliveryStatusSent {
|
||||
return fmt.Errorf("notification already sent")
|
||||
}
|
||||
|
||||
err = s.repository.Delete(ctx, id)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to delete notification", map[string]interface{}{
|
||||
"notification_id": id,
|
||||
"error": err.Error(),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *notificationService) getUsers(ctx context.Context, userIDs []string) ([]notificationRecipient, error) {
|
||||
recipients := make([]notificationRecipient, 0)
|
||||
offset := 0
|
||||
for {
|
||||
var users *user.UserListResponse
|
||||
var err error
|
||||
if len(userIDs) > 0 {
|
||||
users, err = s.userService.GetUsersByIDs(ctx, userIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
users, err = s.userService.Search(ctx, &user.SearchUsersForm{}, &response.Pagination{Limit: 100, Offset: offset})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
offset += 100
|
||||
if len(users.Users) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for _, user := range users.Users {
|
||||
recipients = append(recipients, notificationRecipient{
|
||||
UserID: user.ID,
|
||||
Email: user.Email,
|
||||
DeviceTokens: user.DeviceToken,
|
||||
})
|
||||
}
|
||||
|
||||
if len(recipients) == 0 {
|
||||
return nil, errors.New("no users found")
|
||||
}
|
||||
}
|
||||
|
||||
return recipients, nil
|
||||
}
|
||||
|
||||
func (s *notificationService) getCustomers(ctx context.Context, values []string, target string) ([]notificationRecipient, error) {
|
||||
recipients := make([]notificationRecipient, 0)
|
||||
|
||||
if target == string(TargetAudienceSpecificRole) {
|
||||
customers, err := s.customerService.GetCustomersByRole(ctx, values[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, customer := range customers.Customers {
|
||||
recipients = append(recipients, notificationRecipient{
|
||||
UserID: customer.ID,
|
||||
Email: customer.Email,
|
||||
DeviceTokens: customer.DeviceToken,
|
||||
})
|
||||
}
|
||||
} else if target == string(TargetAudienceSpecificCompany) {
|
||||
customers, err := s.customerService.GetCustomersByCompanies(ctx, values)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, customer := range customers.Customers {
|
||||
recipients = append(recipients, notificationRecipient{
|
||||
UserID: customer.ID,
|
||||
Email: customer.Email,
|
||||
DeviceTokens: customer.DeviceToken,
|
||||
})
|
||||
}
|
||||
} else if target == string(TargetAudienceSpecificCustomer) {
|
||||
customers, err := s.customerService.GetCustomersByIDs(ctx, values)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, customer := range customers.Customers {
|
||||
recipients = append(recipients, notificationRecipient{
|
||||
UserID: customer.ID,
|
||||
Email: customer.Email,
|
||||
DeviceTokens: customer.DeviceToken,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
offset := 0
|
||||
for {
|
||||
customers, err := s.customerService.Search(ctx, &customer.SearchCustomersForm{}, &response.Pagination{Limit: 100, Offset: offset})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, customer := range customers.Customers {
|
||||
recipients = append(recipients, notificationRecipient{
|
||||
UserID: customer.ID,
|
||||
Email: customer.Email,
|
||||
DeviceTokens: customer.DeviceToken,
|
||||
})
|
||||
}
|
||||
offset += 100
|
||||
if len(customers.Customers) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(recipients) == 0 {
|
||||
return nil, errors.New("no customers found")
|
||||
}
|
||||
|
||||
return recipients, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user