2024-04-09 15:52:37 +00:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-06-20 12:04:41 +00:00
|
|
|
|
"errors"
|
2025-02-27 13:30:52 +00:00
|
|
|
|
"gitea.pena/SQuiz/amocrm/internal/models"
|
|
|
|
|
"gitea.pena/SQuiz/common/pj_errors"
|
2024-04-09 15:52:37 +00:00
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ParamsWebhookCreate struct {
|
|
|
|
|
Code string // Authorization 20 минут
|
|
|
|
|
Referer string // адрес аккаунта пользователя
|
2024-04-19 16:05:42 +00:00
|
|
|
|
AccountID string // строка которая передавалась в соц аус сервисе
|
2024-04-09 15:52:37 +00:00
|
|
|
|
FromWidget string
|
|
|
|
|
Platform string // ru/global 1/2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) WebhookCreate(ctx context.Context, req ParamsWebhookCreate) error {
|
2024-06-18 10:48:07 +00:00
|
|
|
|
_, err := s.GetCurrentAccount(ctx, req.AccountID)
|
2024-06-20 12:04:41 +00:00
|
|
|
|
if err != nil && !errors.Is(err, pj_errors.ErrNotFound) {
|
|
|
|
|
s.logger.Error("error checking current account in amo in webhook create", zap.Error(err))
|
2024-06-18 10:48:07 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-20 12:11:28 +00:00
|
|
|
|
if errors.Is(err, pj_errors.ErrNotFound) {
|
2024-06-18 10:48:07 +00:00
|
|
|
|
message := models.KafkaMessage{
|
|
|
|
|
AccountID: req.AccountID,
|
|
|
|
|
AuthCode: req.Code,
|
|
|
|
|
RefererURL: req.Referer,
|
|
|
|
|
Type: models.UserCreate,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = s.producer.ToKafkaUpdate(ctx, message)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.logger.Error("failed to send message to kafka on service webhook create", zap.Error(err))
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 14:52:55 +00:00
|
|
|
|
message := models.KafkaMessage{
|
2024-06-05 13:27:14 +00:00
|
|
|
|
AccountID: req.AccountID,
|
|
|
|
|
AuthCode: req.Code,
|
|
|
|
|
RefererURL: req.Referer,
|
2024-06-18 10:48:07 +00:00
|
|
|
|
Type: models.UserReLogin,
|
2024-04-09 15:52:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 10:48:07 +00:00
|
|
|
|
err = s.producer.ToKafkaUpdate(ctx, message)
|
2024-04-09 15:52:37 +00:00
|
|
|
|
if err != nil {
|
2024-06-18 10:48:07 +00:00
|
|
|
|
s.logger.Error("failed to send message to kafka on service webhook create, user re-login", zap.Error(err))
|
2024-04-09 15:52:37 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
2024-04-11 15:08:54 +00:00
|
|
|
|
|
2024-04-09 15:52:37 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 17:53:33 +00:00
|
|
|
|
func (s *Service) WebhookDelete(ctx context.Context, amoID int) error {
|
|
|
|
|
err := s.repository.AmoRepo.WebhookDelete(ctx, amoID)
|
2024-04-09 15:52:37 +00:00
|
|
|
|
if err != nil {
|
2024-04-23 17:53:33 +00:00
|
|
|
|
s.logger.Error("error canceled amo integration", zap.Error(err))
|
2024-04-09 15:52:37 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|