117 lines
2.8 KiB
Go
117 lines
2.8 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"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"time"
|
|
)
|
|
|
|
func (r *Repository) UpdateListPipelines(ctx context.Context) error {
|
|
//TODO:IMPLEMENT ME
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (r *Repository) GettingPipelinesFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListPipelinesResp, error) {
|
|
offset := (req.Page - 1) * req.Size
|
|
|
|
totalPipelines, err := r.pipelines.CountDocuments(ctx, bson.M{"deleted": false})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var pipelines []models.Pipeline
|
|
|
|
cursor, err := r.pipelines.Find(ctx, bson.M{"deleted": false}, options.Find().SetLimit(int64(req.Size)).SetSkip(int64(offset)))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
|
|
for cursor.Next(ctx) {
|
|
var pipeline models.Pipeline
|
|
if err := cursor.Decode(&pipeline); err != nil {
|
|
return nil, err
|
|
}
|
|
pipelines = append(pipelines, pipeline)
|
|
}
|
|
if err := cursor.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pipelineListResp := &models.UserListPipelinesResp{
|
|
Count: totalPipelines,
|
|
Items: pipelines,
|
|
}
|
|
|
|
return pipelineListResp, 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
|
|
}
|