2024-09-16 15:14:36 +00:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"go.uber.org/zap"
|
2024-09-20 14:41:33 +00:00
|
|
|
|
"net/url"
|
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/bitrix/internal/models"
|
2024-09-16 15:14:36 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (s *Service) UpdateListUsers(ctx context.Context, accountID string) error {
|
|
|
|
|
message := models.KafkaMessage{
|
|
|
|
|
AccountID: accountID,
|
|
|
|
|
Type: models.UsersUpdate,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := s.producer.ToKafkaUpdate(ctx, message)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.logger.Error("failed to send message to kafka on service update users", zap.Error(err))
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 11:24:30 +00:00
|
|
|
|
func (s *Service) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListBitrixResp, error) {
|
2024-09-16 15:14:36 +00:00
|
|
|
|
response, err := s.repository.BitrixRepo.GettingUserWithPagination(ctx, req, accountID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.logger.Error("error getting users with pagination", zap.Error(err))
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return response, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) SoftDeleteAccount(ctx context.Context, accountID string) error {
|
|
|
|
|
err := s.repository.BitrixRepo.SoftDeleteAccount(ctx, accountID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.logger.Error("error soft delete current account in softDeleteAccount service", zap.Error(err))
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 11:24:30 +00:00
|
|
|
|
func (s *Service) GetCurrentAccount(ctx context.Context, accountID string) (*model.BitrixAccount, error) {
|
2024-09-16 15:14:36 +00:00
|
|
|
|
user, err := s.repository.BitrixRepo.GetCurrentAccount(ctx, accountID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
return nil, pj_errors.ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
s.logger.Error("error getting current account in getCurrentAccount service", zap.Error(err))
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) ConnectAccount(ctx context.Context, accountID string) (*model.ConnectAccountResp, error) {
|
2024-09-20 14:41:33 +00:00
|
|
|
|
state, err := s.encrypt.EncryptStr(accountID)
|
2024-09-16 15:14:36 +00:00
|
|
|
|
if err != nil {
|
2024-09-20 14:41:33 +00:00
|
|
|
|
s.logger.Error("error encrypting account state", zap.Error(err))
|
2024-09-16 15:14:36 +00:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 14:41:33 +00:00
|
|
|
|
oauthURL := url.URL{
|
|
|
|
|
Scheme: "https",
|
2024-10-17 22:00:41 +00:00
|
|
|
|
Host: "b24-s5jg6c.bitrix24.ru", // todo check надо проверить как с дургими доменами работает, потому что сейчас это домен каждого отдельного битрикса
|
2024-09-20 14:41:33 +00:00
|
|
|
|
Path: "/oauth/authorize/",
|
|
|
|
|
RawQuery: url.Values{
|
2025-02-25 10:13:00 +00:00
|
|
|
|
"client_id": {s.config.BitrixIntegrationID},
|
2024-09-20 14:41:33 +00:00
|
|
|
|
"state": {string(state)},
|
|
|
|
|
}.Encode(),
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 15:14:36 +00:00
|
|
|
|
response := model.ConnectAccountResp{
|
2024-09-20 14:41:33 +00:00
|
|
|
|
Link: oauthURL.String(),
|
2024-09-16 15:14:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &response, nil
|
|
|
|
|
}
|