127 lines
3.3 KiB
Go
127 lines
3.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"amocrm/internal/models"
|
|
"context"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"time"
|
|
)
|
|
|
|
func (r *Repository) UpdateListUsers(ctx context.Context) error {
|
|
//TODO:IMPLEMENT ME
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (r *Repository) GettingUserFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListResp, error) {
|
|
offset := (req.Page - 1) * req.Size
|
|
|
|
totalUsers, err := r.mdbUser.CountDocuments(ctx, bson.M{"Deleted": false})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var users []models.User
|
|
|
|
cursor, err := r.mdbUser.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 user models.User
|
|
if err := cursor.Decode(&user); err != nil {
|
|
return nil, err
|
|
}
|
|
users = append(users, user)
|
|
}
|
|
if err := cursor.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userListResp := &models.UserListResp{
|
|
Count: totalUsers,
|
|
Items: users,
|
|
}
|
|
|
|
return userListResp, nil
|
|
}
|
|
|
|
func (r *Repository) SoftDeleteAccount(ctx context.Context, accountID string) error {
|
|
filter := bson.M{"AccountID": accountID}
|
|
update := bson.M{"$set": bson.M{"Deleted": true}}
|
|
_, err := r.mdbUser.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) GetCurrentAccount(ctx context.Context, accountID string) (*models.User, error) {
|
|
var user models.User
|
|
err := r.mdbUser.FindOne(ctx, bson.M{"AccountID": accountID, "Deleted": false}).Decode(&user)
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, ErrUserNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *Repository) CreateAccount(ctx context.Context, accountID string) error {
|
|
userData := models.User{}
|
|
userData.Createdat = time.Now().Unix()
|
|
userData.Accountid = accountID
|
|
_, err := r.mdbUser.InsertOne(ctx, userData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) UpdateAccount(ctx context.Context, accountID string, userInfo models.User) error {
|
|
filter := bson.M{"AccountID": accountID}
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"ID": userInfo.ID, // uuid аккаунта
|
|
"Name": userInfo.Name, // имя аккаунта амо
|
|
"Amocrmid": userInfo.Amocrmid, // id аккаунта амо
|
|
"Amouserid": userInfo.Amouserid, // id текущего пользователя в амо
|
|
"Country": userInfo.Country, // страна в настройках амо
|
|
"Subdomain": userInfo.Subdomain, // поддомен организации
|
|
"Group": userInfo.Group, // группы пользователя
|
|
"Role": userInfo.Role, // роль
|
|
"Email": userInfo.Email,
|
|
},
|
|
}
|
|
|
|
_, err := r.mdbUser.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) CheckUsers(ctx context.Context, amouserid int, user models.User) error {
|
|
filter := bson.M{"Amouserid": amouserid}
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"Name": user.Name, // имя аккаунта амо
|
|
"Group": user.Group, // группы пользователя
|
|
"Email": user.Email,
|
|
"Role": user.Role,
|
|
},
|
|
}
|
|
_, err := r.mdbUser.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|