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
|
|
|
"context"
|
2024-05-24 18:36:50 +00:00
|
|
|
"database/sql"
|
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/model"
|
2024-06-05 08:47:37 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors"
|
2024-04-09 15:52:37 +00:00
|
|
|
)
|
|
|
|
|
2024-04-21 14:52:55 +00:00
|
|
|
func (s *Service) UpdateListUsers(ctx context.Context, accountID string) error {
|
|
|
|
message := models.KafkaMessage{
|
|
|
|
AccountID: accountID,
|
|
|
|
Type: models.UsersUpdate,
|
|
|
|
}
|
2024-04-09 15:52:37 +00:00
|
|
|
|
2024-04-26 16:26:59 +00:00
|
|
|
err := s.producer.ToKafkaUpdate(ctx, message)
|
2024-04-09 15:52:37 +00:00
|
|
|
if err != nil {
|
2024-04-21 14:52:55 +00:00
|
|
|
s.logger.Error("failed to send message to kafka on service update users", zap.Error(err))
|
2024-04-09 15:52:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-04-21 14:52:55 +00:00
|
|
|
|
2024-04-09 15:52:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-21 15:24:13 +00:00
|
|
|
func (s *Service) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListResp, error) {
|
|
|
|
response, err := s.repository.AmoRepo.GettingUserWithPagination(ctx, req, accountID)
|
2024-04-09 15:52:37 +00:00
|
|
|
if err != nil {
|
2024-04-12 16:39:51 +00:00
|
|
|
s.logger.Error("error getting users with pagination", zap.Error(err))
|
2024-04-09 15:52:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2024-04-11 09:55:26 +00:00
|
|
|
func (s *Service) SoftDeleteAccount(ctx context.Context, accountID string) error {
|
2024-04-17 12:21:06 +00:00
|
|
|
err := s.repository.AmoRepo.SoftDeleteAccount(ctx, accountID)
|
2024-04-09 15:52:37 +00:00
|
|
|
if err != nil {
|
2024-04-11 09:55:26 +00:00
|
|
|
s.logger.Error("error soft delete current account in softDeleteAccount service", zap.Error(err))
|
2024-04-09 15:52:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-12 14:34:37 +00:00
|
|
|
func (s *Service) GetCurrentAccount(ctx context.Context, accountID string) (*model.AmoAccount, error) {
|
2024-04-17 12:21:06 +00:00
|
|
|
user, err := s.repository.AmoRepo.GetCurrentAccount(ctx, accountID)
|
2024-04-09 15:52:37 +00:00
|
|
|
if err != nil {
|
2024-05-24 18:36:50 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2024-06-05 08:47:37 +00:00
|
|
|
return nil, pj_errors.ErrNotFound
|
2024-05-24 18:36:50 +00:00
|
|
|
}
|
2024-04-11 09:55:26 +00:00
|
|
|
s.logger.Error("error getting current account in getCurrentAccount service", zap.Error(err))
|
2024-04-09 15:52:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-11 09:55:26 +00:00
|
|
|
|
2024-05-03 09:25:43 +00:00
|
|
|
return user, nil
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 12:21:06 +00:00
|
|
|
func (s *Service) ConnectAccount(ctx context.Context, accountID string) (*model.ConnectAccountResp, error) {
|
2024-04-19 16:05:42 +00:00
|
|
|
link, err := s.socialAuthClient.GenerateAmocrmAuthURL(accountID)
|
2024-04-09 15:52:37 +00:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("error sending request to pena social auth service:", zap.Error(err))
|
2024-04-23 12:22:12 +00:00
|
|
|
return nil, err
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 12:21:06 +00:00
|
|
|
response := model.ConnectAccountResp{
|
2024-04-09 15:52:37 +00:00
|
|
|
Link: link,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &response, nil
|
|
|
|
}
|