Docker/Voltaserve/api/service/notification_service.go
2024-04-17 20:22:30 +02:00

46 lines
1.0 KiB
Go

package service
import "voltaserve/repo"
type Notification struct {
Type string `json:"type"`
Body interface{} `json:"body"`
}
type NotificationService struct {
userRepo repo.UserRepo
invitationRepo repo.InvitationRepo
invitationMapper *invitationMapper
}
func NewNotificationService() *NotificationService {
return &NotificationService{
userRepo: repo.NewUserRepo(),
invitationRepo: repo.NewInvitationRepo(),
invitationMapper: newInvitationMapper(),
}
}
func (svc *NotificationService) GetAll(userID string) ([]*Notification, error) {
user, err := svc.userRepo.Find(userID)
if err != nil {
return nil, err
}
invitations, err := svc.invitationRepo.GetIncoming(user.GetEmail())
if err != nil {
return nil, err
}
notifications := make([]*Notification, 0)
for _, inv := range invitations {
v, err := svc.invitationMapper.mapOne(inv, userID)
if err != nil {
return nil, err
}
notifications = append(notifications, &Notification{
Type: "new_invitation",
Body: &v,
})
}
return notifications, nil
}