87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
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) UpdateListPipelines(ctx context.Context) error {
|
|
//TODO:IMPLEMENT ME
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (r *Repository) GettingPipelinesFromCash(ctx context.Context) (*models.UserListPipelinesResp, error) {
|
|
//TODO:IMPLEMENT ME
|
|
|
|
return &models.UserListPipelinesResp{}, nil
|
|
}
|
|
|
|
func (r *Repository) CheckPipelines(ctx context.Context, accountID string, pipelines []amo.Pipeline) error {
|
|
for _, p := range pipelines {
|
|
pipeline := models.Pipeline{
|
|
ID: accountID,
|
|
AccountID: p.AccountID,
|
|
Amoid: p.ID,
|
|
Name: p.Name,
|
|
Isarchive: p.IsArchive,
|
|
}
|
|
|
|
existingPipeline, err := r.GetPipelineByID(ctx, accountID, p.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if existingPipeline != nil {
|
|
pipeline.UpdateAt = time.Now().Unix()
|
|
err = r.UpdatePipeline(ctx, &pipeline)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
pipeline.Createdat = time.Now().Unix()
|
|
err = r.InsertPipeline(ctx, &pipeline)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) GetPipelineByID(ctx context.Context, accountID string, amoid int) (*models.Pipeline, error) {
|
|
var pipeline models.Pipeline
|
|
filter := bson.M{"id": accountID, "amoid": amoid}
|
|
err := r.pipelines.FindOne(ctx, filter).Decode(&pipeline)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pipeline, nil
|
|
}
|
|
|
|
func (r *Repository) UpdatePipeline(ctx context.Context, pipeline *models.Pipeline) error {
|
|
filter := bson.M{"id": pipeline.ID, "amoid": pipeline.Amoid}
|
|
update := bson.M{"$set": bson.M{
|
|
"accountID": pipeline.AccountID,
|
|
"name": pipeline.Name,
|
|
"isarchive": pipeline.Isarchive,
|
|
"updateAt": pipeline.UpdateAt,
|
|
}}
|
|
_, err := r.pipelines.UpdateOne(ctx, filter, update)
|
|
return err
|
|
}
|
|
|
|
func (r *Repository) InsertPipeline(ctx context.Context, pipeline *models.Pipeline) error {
|
|
_, err := r.pipelines.InsertOne(ctx, pipeline)
|
|
return err
|
|
}
|