2024-04-11 12:45:01 +00:00
|
|
|
package data_updater
|
|
|
|
|
|
|
|
import (
|
|
|
|
"amocrm/internal/repository"
|
|
|
|
"amocrm/pkg/amoClient"
|
|
|
|
"context"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Deps struct {
|
|
|
|
AmoClient *amoClient.Amo
|
|
|
|
Repo *repository.Repository
|
|
|
|
Logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type DataUpdater struct {
|
|
|
|
amoClient *amoClient.Amo
|
|
|
|
repo *repository.Repository
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDataUpdaterWC(deps Deps) *DataUpdater {
|
|
|
|
return &DataUpdater{
|
|
|
|
amoClient: deps.AmoClient,
|
|
|
|
repo: deps.Repo,
|
|
|
|
logger: deps.Logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *DataUpdater) Start(ctx context.Context) {
|
|
|
|
nextStart := calculateTime()
|
|
|
|
ticker := time.NewTicker(time.Second * time.Duration(nextStart))
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
wc.processTasks(ctx)
|
|
|
|
nextStart = calculateTime()
|
|
|
|
ticker.Reset(time.Second * time.Duration(nextStart))
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *DataUpdater) processTasks(ctx context.Context) {
|
2024-04-11 15:08:54 +00:00
|
|
|
// сначала получаем список токенов
|
|
|
|
allTokens, err := wc.repo.GetAllTokens(ctx)
|
|
|
|
if err != nil {
|
|
|
|
wc.logger.Error("error fetch all tokens from mongo:", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, token := range allTokens {
|
|
|
|
pipelines, err := wc.amoClient.GetListPipelines(token.AccessToken)
|
|
|
|
if err != nil {
|
|
|
|
wc.logger.Error("error getting list pipelines:", zap.Error(err))
|
|
|
|
}
|
|
|
|
err = wc.repo.CheckPipelines(ctx, token.AccountID, pipelines.Embedded.Pipelines)
|
|
|
|
if err != nil {
|
|
|
|
wc.logger.Error("error update pipelines in mongo:", zap.Error(err))
|
|
|
|
}
|
2024-04-11 15:50:27 +00:00
|
|
|
|
|
|
|
for _, pipeline := range pipelines.Embedded.Pipelines {
|
|
|
|
steps, err := wc.amoClient.GetListSteps(pipeline.ID, token.AccessToken)
|
|
|
|
if err != nil {
|
|
|
|
wc.logger.Error("error getting list steps pipeline:", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = wc.repo.CheckSteps(ctx, token.AccountID, steps.Embedded.Statuses)
|
|
|
|
if err != nil {
|
|
|
|
wc.logger.Error("error update pipeline steps in mongo:", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 15:08:54 +00:00
|
|
|
}
|
2024-04-11 12:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *DataUpdater) Stop(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|