53 lines
1.8 KiB
Go
53 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
)
|
|
|
|
// PushSubscriptionService provides business logic for managing push notification tokens.
|
|
// Reference: Chatwoot PushSubscription controller + P2B M8 spec
|
|
type PushSubscriptionService struct {
|
|
pushTokenRepo *repository.PushTokenRepo
|
|
}
|
|
|
|
// NewPushSubscriptionService creates a new PushSubscription service with required dependencies.
|
|
func NewPushSubscriptionService(pushTokenRepo *repository.PushTokenRepo) *PushSubscriptionService {
|
|
return &PushSubscriptionService{
|
|
pushTokenRepo: pushTokenRepo,
|
|
}
|
|
}
|
|
|
|
// ListPushTokens retrieves all push tokens for a user.
|
|
func (s *PushSubscriptionService) ListPushTokens(ctx context.Context, userID uint) ([]model.PushToken, error) {
|
|
return s.pushTokenRepo.ListByUser(ctx, userID)
|
|
}
|
|
|
|
// RegisterPushToken creates a new push token for a user.
|
|
func (s *PushSubscriptionService) RegisterPushToken(ctx context.Context, userID uint, token string, platform string, deviceID string, p256dhKey string, authKey string) (*model.PushToken, error) {
|
|
pt := &model.PushToken{
|
|
UserID: userID,
|
|
Token: token,
|
|
Platform: platform,
|
|
DeviceID: deviceID,
|
|
P256DHKey: p256dhKey,
|
|
AuthKey: authKey,
|
|
}
|
|
err := s.pushTokenRepo.Create(ctx, pt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return pt, nil
|
|
}
|
|
|
|
// RemovePushToken deletes a push token by ID.
|
|
func (s *PushSubscriptionService) RemovePushToken(ctx context.Context, id uint) error {
|
|
return s.pushTokenRepo.Delete(ctx, id)
|
|
}
|
|
|
|
// RemovePushTokenByValue deletes a push token by token string and user ID.
|
|
func (s *PushSubscriptionService) RemovePushTokenByValue(ctx context.Context, token string, userID uint) error {
|
|
return s.pushTokenRepo.DeleteByTokenAndUser(ctx, token, userID)
|
|
} |