- Removed detailed event routes from the API. - Added new report request routes for creating and fetching report requests. - Introduced new domain models for report requests, including metadata and status handling. - Implemented report request processing logic, including CSV generation for event interval reports. - Enhanced company statistics handling with new domain models and service methods. - Updated repository interfaces and implementations to support new report functionalities. - Added error handling and logging for report file operations and notifications.
86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package bonus
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
)
|
|
|
|
type SendBonusNotificationParam struct {
|
|
BonusID int64
|
|
UserID int64
|
|
Type domain.BonusType
|
|
Amount domain.Currency
|
|
SendEmail bool
|
|
SendSMS bool
|
|
}
|
|
|
|
func shouldSend(channel domain.DeliveryChannel, sendEmail, sendSMS bool) bool {
|
|
switch {
|
|
case channel == domain.DeliveryChannelEmail && sendEmail:
|
|
return true
|
|
case channel == domain.DeliveryChannelSMS && sendSMS:
|
|
return true
|
|
case channel == domain.DeliveryChannelInApp:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (s *Service) SendBonusNotification(ctx context.Context, param SendBonusNotificationParam) error {
|
|
|
|
var (
|
|
headline string
|
|
message string
|
|
)
|
|
|
|
switch param.Type {
|
|
case domain.WelcomeBonus:
|
|
headline = "You've been awarded a welcome bonus!"
|
|
message = fmt.Sprintf(
|
|
"Congratulations! A you've been given %.2f as a welcome bonus for you to bet on.",
|
|
param.Amount.Float32(),
|
|
)
|
|
default:
|
|
return fmt.Errorf("unsupported bonus type: %v", param.Type)
|
|
}
|
|
for _, channel := range []domain.DeliveryChannel{
|
|
domain.DeliveryChannelInApp,
|
|
domain.DeliveryChannelEmail,
|
|
domain.DeliveryChannelSMS,
|
|
} {
|
|
if !shouldSend(channel, param.SendEmail, param.SendSMS) {
|
|
continue
|
|
}
|
|
|
|
raw, _ := json.Marshal(map[string]any{
|
|
"bonus_id": param.BonusID,
|
|
"type": param.Type,
|
|
})
|
|
|
|
n := &domain.Notification{
|
|
RecipientID: param.UserID,
|
|
DeliveryStatus: domain.DeliveryStatusPending,
|
|
IsRead: false,
|
|
Type: domain.NOTIFICATION_TYPE_BONUS_AWARDED,
|
|
Level: domain.NotificationLevelSuccess,
|
|
Reciever: domain.NotificationRecieverSideCustomer,
|
|
DeliveryChannel: channel,
|
|
Payload: domain.NotificationPayload{
|
|
Headline: headline,
|
|
Message: message,
|
|
},
|
|
Priority: 2,
|
|
Metadata: raw,
|
|
}
|
|
if err := s.notificationSvc.SendNotification(ctx, n); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|