82 lines
2.5 KiB
Go
82 lines
2.5 KiB
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
"database/sql"
|
||
"go.uber.org/zap"
|
||
"net/url"
|
||
"penahub.gitlab.yandexcloud.net/backend/quiz/bitrix/internal/models"
|
||
"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
|
||
}
|
||
|
||
func (s *Service) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListBitrixResp, error) {
|
||
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
|
||
}
|
||
|
||
func (s *Service) GetCurrentAccount(ctx context.Context, accountID string) (*model.BitrixAccount, error) {
|
||
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) {
|
||
state, err := s.encrypt.EncryptStr(accountID)
|
||
if err != nil {
|
||
s.logger.Error("error encrypting account state", zap.Error(err))
|
||
return nil, err
|
||
}
|
||
|
||
oauthURL := url.URL{
|
||
Scheme: "https",
|
||
Host: "b24-s5jg6c.bitrix24.ru", // todo check надо проверить как с дургими доменами работает, потому что сейчас это домен каждого отдельного битрикса
|
||
Path: "/oauth/authorize/",
|
||
RawQuery: url.Values{
|
||
"client_id": {s.config.BitrixIntegrationID},
|
||
"state": {string(state)},
|
||
}.Encode(),
|
||
}
|
||
|
||
response := model.ConnectAccountResp{
|
||
Link: oauthURL.String(),
|
||
}
|
||
|
||
return &response, nil
|
||
}
|