amocrm/internal/service/webhook.go

57 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"amocrm/internal/models"
"amocrm/internal/models/amo"
"context"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
"time"
)
type ParamsWebhookCreate struct {
Code string // Authorization 20 минут
Referer string // адрес аккаунта пользователя
State string // строка которая передавалась в соц аус сервисе
FromWidget string
Platform string // ru/global 1/2
}
// todo надо понять как понимать какому юзеру принадлежит токен
func (s *Service) WebhookCreate(ctx context.Context, req ParamsWebhookCreate) error {
// получаем аксес и рефреш токены по коду авторизации
forGetTokens := amo.CreateWebHookReq{
GrantType: "authorization_code",
Code: req.Code,
}
tokens, err := s.amoClient.CreateWebHook(&forGetTokens)
if err != nil {
s.logger.Error("error getting webhook in Service:", zap.Error(err))
return err
}
err = s.repository.WebhookCreate(ctx, models.Token{
ObjID: primitive.NewObjectID(),
RefreshToken: tokens.RefreshToken,
AccessToken: tokens.AccessToken,
AccountID: "todo",
AuthCode: req.Code,
Expiration: time.Now().Unix() + tokens.ExpiresIn,
})
if err != nil {
s.logger.Error("error adding tokens to mongo on service WebhookCreate", zap.Error(err))
return err
}
return nil
}
func (s *Service) WebhookDelete(ctx context.Context) error {
err := s.repository.WebhookDelete(ctx)
if err != nil {
return err
}
return nil
}