add methods for update/insert tags in mongo

This commit is contained in:
Pavel 2024-04-12 11:57:19 +03:00
parent fa11f63432
commit 8b1bd1a892
8 changed files with 148 additions and 17 deletions

@ -64,6 +64,7 @@ func Run(ctx context.Context, config initialize.Config, logger *zap.Logger) erro
Tokens: mdb.Collection("tokens"),
Pipelines: mdb.Collection("pipelines"),
Steps: mdb.Collection("steps"),
Tags: mdb.Collection("tags"),
Logger: logger,
})

@ -1,20 +1,24 @@
package models
import "amocrm/internal/models/amo"
type Tag struct {
/* - таймштамп создания тега в нашей системе*/
Createdat int `json:"CreatedAt"`
Createdat int64 `json:"CreatedAt" bson:"createdat"`
// время обновления
UpdateAt int64 `json:"UpdateAt" bson:"updateAt"`
/* - флаг мягкого удаления*/
Deleted bool `json:"Deleted"`
Deleted bool `json:"Deleted" bson:"deleted"`
/* - сущность, к которой принадлежит этот тег. Наверное, стоит сделать через enum в базе*/
Entity string `json:"Entity"`
Entity amo.EntityType `json:"Entity" bson:"entity"`
/* - айдишник в нашей системе*/
ID int `json:"ID"`
ID string `json:"ID" bson:"id"`
/* - название тега в амо*/
Name string `json:"Name"`
Name string `json:"Name" bson:"name"`
/* - связь с аккаунтом в интеграции амо*/
Accountid string `json:"AccountID"`
Accountid int `json:"AccountID" bson:"accountid"`
/* - айдишник тега в амо*/
Amoid int `json:"AmoID"`
Amoid int `json:"AmoID" bson:"amoid"`
/* - цвет тега в амо*/
Color string `json:"Color"`
Color string `json:"Color" bson:"color"`
}

@ -10,6 +10,7 @@ type Deps struct {
Tokens *mongo.Collection
Pipelines *mongo.Collection
Steps *mongo.Collection
Tags *mongo.Collection
Logger *zap.Logger
}
@ -18,6 +19,7 @@ type Repository struct {
tokens *mongo.Collection
pipelines *mongo.Collection
steps *mongo.Collection
tags *mongo.Collection
logger *zap.Logger
}
@ -27,6 +29,7 @@ func NewRepository(deps Deps) *Repository {
tokens: deps.Tokens,
pipelines: deps.Pipelines,
steps: deps.Steps,
tags: deps.Tags,
logger: deps.Logger,
}
}

@ -39,13 +39,13 @@ func (r *Repository) CheckPipelines(ctx context.Context, accountID string, pipel
if existingPipeline != nil {
pipeline.UpdateAt = time.Now().Unix()
err := r.UpdatePipeline(ctx, &pipeline)
err = r.UpdatePipeline(ctx, &pipeline)
if err != nil {
return err
}
} else {
pipeline.Createdat = time.Now().Unix()
err := r.InsertPipeline(ctx, &pipeline)
err = r.InsertPipeline(ctx, &pipeline)
if err != nil {
return err
}
@ -75,7 +75,6 @@ func (r *Repository) UpdatePipeline(ctx context.Context, pipeline *models.Pipeli
"name": pipeline.Name,
"isarchive": pipeline.Isarchive,
"updateAt": pipeline.UpdateAt,
"deleted": pipeline.Deleted,
}}
_, err := r.pipelines.UpdateOne(ctx, filter, update)
return err

@ -40,13 +40,13 @@ func (r *Repository) CheckSteps(ctx context.Context, accountID string, steps []a
if existingStep != nil {
step.UpdateAt = time.Now().Unix()
err := r.UpdateStep(ctx, &step)
err = r.UpdateStep(ctx, &step)
if err != nil {
return err
}
} else {
step.Createdat = time.Now().Unix()
err := r.InsertStep(ctx, &step)
err = r.InsertStep(ctx, &step)
if err != nil {
return err
}
@ -75,7 +75,6 @@ func (r *Repository) UpdateStep(ctx context.Context, step *models.Step) error {
"accountid": step.Accountid,
"name": step.Name,
"updateAt": step.UpdateAt,
"deleted": step.Deleted,
"color": step.Color,
"pipelineid": step.Pipelineid,
}}

@ -2,7 +2,11 @@ package repository
import (
"amocrm/internal/models"
"amocrm/internal/models/amo"
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"time"
)
func (r *Repository) GettingTagsFromCash(ctx context.Context) (*models.UserListTagsResp, error) {
@ -17,3 +21,75 @@ func (r *Repository) UpdateListTags(ctx context.Context) error {
return nil
}
type CheckTagsDeps struct {
AccountID string // id quiz
ID int // id amo
EntityType amo.EntityType
Tags []amo.Tag
}
func (r *Repository) CheckTags(ctx context.Context, deps CheckTagsDeps) error {
for _, t := range deps.Tags {
tag := models.Tag{
ID: deps.AccountID,
Accountid: deps.ID,
Amoid: t.ID,
Name: t.Name,
Color: *t.Color,
Entity: deps.EntityType,
}
existingTag, err := r.GetTagByID(ctx, deps.AccountID, t.ID)
if err != nil {
return err
}
if existingTag != nil {
tag.UpdateAt = time.Now().Unix()
err = r.UpdateTag(ctx, &tag)
if err != nil {
return err
}
} else {
tag.Createdat = time.Now().Unix()
err = r.InsertTag(ctx, &tag)
if err != nil {
return err
}
}
}
return nil
}
func (r *Repository) GetTagByID(ctx context.Context, accountID string, amoid int) (*models.Tag, error) {
var tag models.Tag
filter := bson.M{"id": accountID, "amoid": amoid}
err := r.tags.FindOne(ctx, filter).Decode(&tag)
if err == mongo.ErrNoDocuments {
return nil, nil
}
if err != nil {
return nil, err
}
return &tag, nil
}
func (r *Repository) UpdateTag(ctx context.Context, tag *models.Tag) error {
filter := bson.M{"id": tag.ID, "amoid": tag.Amoid}
update := bson.M{"$set": bson.M{
"accountid": tag.Accountid,
"name": tag.Name,
"updateAt": tag.UpdateAt,
"color": tag.Color,
"entity": tag.Entity,
}}
_, err := r.tags.UpdateOne(ctx, filter, update)
return err
}
func (r *Repository) InsertTag(ctx context.Context, tag *models.Tag) error {
_, err := r.tags.InsertOne(ctx, tag)
return err
}

@ -54,6 +54,7 @@ func (wc *DataUpdater) processTasks(ctx context.Context) {
return
}
for _, token := range allTokens {
accountID := 0
pipelines, err := wc.amoClient.GetListPipelines(token.AccessToken)
if err != nil {
wc.logger.Error("error getting list pipelines:", zap.Error(err))
@ -64,6 +65,7 @@ func (wc *DataUpdater) processTasks(ctx context.Context) {
}
for _, pipeline := range pipelines.Embedded.Pipelines {
accountID = pipeline.AccountID
steps, err := wc.amoClient.GetListSteps(pipeline.ID, token.AccessToken)
if err != nil {
wc.logger.Error("error getting list steps pipeline:", zap.Error(err))
@ -117,7 +119,54 @@ func (wc *DataUpdater) processTasks(ctx context.Context) {
}
}
// todo вставка списков в монгу то есть апдейт
for _, entityType := range entityTypes {
switch entityType {
case amo.LeadsTags:
err := wc.repo.CheckTags(ctx, repository.CheckTagsDeps{
Tags: leadsTags,
AccountID: token.AccountID,
ID: accountID,
EntityType: entityType,
})
if err != nil {
wc.logger.Error("error update leads tags")
continue
}
case amo.ContactsTags:
err := wc.repo.CheckTags(ctx, repository.CheckTagsDeps{
Tags: contactsTags,
AccountID: token.AccountID,
ID: accountID,
EntityType: entityType,
})
if err != nil {
wc.logger.Error("error update contacts tags")
continue
}
case amo.CompaniesTags:
err := wc.repo.CheckTags(ctx, repository.CheckTagsDeps{
Tags: companiesTags,
AccountID: token.AccountID,
ID: accountID,
EntityType: entityType,
})
if err != nil {
wc.logger.Error("error update companies tags")
continue
}
case amo.CustomersTags:
err := wc.repo.CheckTags(ctx, repository.CheckTagsDeps{
Tags: customersTags,
AccountID: token.AccountID,
ID: accountID,
EntityType: entityType,
})
if err != nil {
wc.logger.Error("error update customer tags")
continue
}
}
}
// todo fields
}

@ -595,13 +595,13 @@ components:
description: объект тега из амо
properties:
ID:
type: integer
type: string
description: айдишник в нашей системе
AmoID:
type: integer
description: айдишник тега в амо
AccountID:
type: string
type: integer
description: связь с аккаунтом в интеграции амо
Entity:
type: string