2024-04-09 15:52:37 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2024-04-21 14:52:55 +00:00
|
|
|
"amocrm/internal/models"
|
2024-04-09 15:52:37 +00:00
|
|
|
"amocrm/pkg/amoClient"
|
|
|
|
pena_social_auth "amocrm/pkg/pena-social-auth"
|
2024-04-21 14:52:55 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/twmb/franz-go/pkg/kgo"
|
2024-04-09 15:52:37 +00:00
|
|
|
"go.uber.org/zap"
|
2024-04-17 12:21:06 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
|
2024-04-09 15:52:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Deps struct {
|
2024-04-17 12:21:06 +00:00
|
|
|
Repository *dal.AmoDal
|
2024-04-09 15:52:37 +00:00
|
|
|
Logger *zap.Logger
|
|
|
|
SocialAuthClient *pena_social_auth.Client
|
|
|
|
AmoClient *amoClient.Amo
|
2024-04-21 14:52:55 +00:00
|
|
|
KafkaClient *kgo.Client
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
2024-04-17 12:21:06 +00:00
|
|
|
repository *dal.AmoDal
|
2024-04-09 15:52:37 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
socialAuthClient *pena_social_auth.Client
|
|
|
|
amoClient *amoClient.Amo
|
2024-04-21 14:52:55 +00:00
|
|
|
kafkaClient *kgo.Client
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewService(deps Deps) *Service {
|
|
|
|
return &Service{
|
|
|
|
repository: deps.Repository,
|
|
|
|
logger: deps.Logger,
|
|
|
|
socialAuthClient: deps.SocialAuthClient,
|
|
|
|
amoClient: deps.AmoClient,
|
2024-04-21 14:52:55 +00:00
|
|
|
kafkaClient: deps.KafkaClient,
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-21 14:52:55 +00:00
|
|
|
|
|
|
|
func (s *Service) ToKafkaUpdate(ctx context.Context, message models.KafkaMessage) error {
|
|
|
|
bytes, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("error marshal message to kafka", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
responses := s.kafkaClient.ProduceSync(ctx, &kgo.Record{Value: bytes})
|
|
|
|
for _, response := range responses {
|
|
|
|
if response.Err != nil {
|
|
|
|
s.logger.Error("failed to send message on update kafka", zap.Error(response.Err))
|
|
|
|
return response.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|