mirror of
https://github.com/certimate-go/certimate.git
synced 2026-06-22 21:05:48 +08:00
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package notify
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/certimate-go/certimate/internal/domain/dtos"
|
|
)
|
|
|
|
const (
|
|
notifyTestSubject = "[Certimate] Notification Testing"
|
|
notifyTestMessage = "Welcome to use Certimate!"
|
|
)
|
|
|
|
type NotifyService struct {
|
|
accessRepo accessRepository
|
|
}
|
|
|
|
func NewNotifyService(accessRepo accessRepository) *NotifyService {
|
|
return &NotifyService{
|
|
accessRepo: accessRepo,
|
|
}
|
|
}
|
|
|
|
func (n *NotifyService) TestPush(ctx context.Context, req *dtos.NotifyTestPushReq) (*dtos.NotifyTestPushResp, error) {
|
|
accessConfig := make(map[string]any)
|
|
if access, err := n.accessRepo.GetById(ctx, req.AccessId); err != nil {
|
|
return nil, fmt.Errorf("failed to get access #%s record: %w", req.AccessId, err)
|
|
} else {
|
|
if access.Reserve != "notif" {
|
|
return nil, fmt.Errorf("access #%s is not available for notification", req.AccessId)
|
|
}
|
|
|
|
accessConfig = access.Config
|
|
}
|
|
|
|
notifier := NewClient()
|
|
notifyReq := &SendNotificationRequest{
|
|
Provider: req.Provider,
|
|
ProviderAccessConfig: accessConfig,
|
|
ProviderExtendedConfig: make(map[string]any),
|
|
Subject: notifyTestSubject,
|
|
Message: notifyTestMessage,
|
|
}
|
|
if _, err := notifier.SendNotification(ctx, notifyReq); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &dtos.NotifyTestPushResp{}, nil
|
|
}
|