119 lines
2.6 KiB
Go
119 lines
2.6 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) GettingStepsFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListStepsResp, error) {
|
|
offset := (req.Page - 1) * req.Size
|
|
|
|
totalSteps, err := r.steps.CountDocuments(ctx, bson.M{"deleted": false})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var steps []models.Step
|
|
|
|
cursor, err := r.steps.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 step models.Step
|
|
if err := cursor.Decode(&step); err != nil {
|
|
return nil, err
|
|
}
|
|
steps = append(steps, step)
|
|
}
|
|
if err := cursor.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
stepListResp := &models.UserListStepsResp{
|
|
Count: totalSteps,
|
|
Items: steps,
|
|
}
|
|
|
|
return stepListResp, nil
|
|
}
|
|
|
|
func (r *Repository) UpdateListSteps(ctx context.Context) error {
|
|
//TODO:IMPLEMENT ME
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (r *Repository) CheckSteps(ctx context.Context, accountID string, steps []amo.Statuses) error {
|
|
for _, s := range steps {
|
|
step := models.Step{
|
|
ID: accountID,
|
|
Pipelineid: s.PipelineID,
|
|
Name: s.Name,
|
|
Accountid: s.AccountID,
|
|
Amoid: s.ID,
|
|
Color: s.Color,
|
|
}
|
|
|
|
existingStep, err := r.GetStepByID(ctx, accountID, s.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if existingStep != nil {
|
|
step.UpdateAt = time.Now().Unix()
|
|
err = r.UpdateStep(ctx, &step)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
step.Createdat = time.Now().Unix()
|
|
err = r.InsertStep(ctx, &step)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) GetStepByID(ctx context.Context, accountID string, amoid int) (*models.Step, error) {
|
|
var step models.Step
|
|
filter := bson.M{"id": accountID, "amoid": amoid}
|
|
err := r.steps.FindOne(ctx, filter).Decode(&step)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &step, nil
|
|
}
|
|
|
|
func (r *Repository) UpdateStep(ctx context.Context, step *models.Step) error {
|
|
filter := bson.M{"id": step.ID, "amoid": step.Amoid}
|
|
update := bson.M{"$set": bson.M{
|
|
"accountid": step.Accountid,
|
|
"name": step.Name,
|
|
"updateAt": step.UpdateAt,
|
|
"color": step.Color,
|
|
"pipelineid": step.Pipelineid,
|
|
}}
|
|
_, err := r.steps.UpdateOne(ctx, filter, update)
|
|
return err
|
|
}
|
|
|
|
func (r *Repository) InsertStep(ctx context.Context, step *models.Step) error {
|
|
_, err := r.steps.InsertOne(ctx, step)
|
|
return err
|
|
}
|