2024-04-09 15:52:37 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"amocrm/internal/models"
|
2024-04-10 08:54:18 +00:00
|
|
|
amo2 "amocrm/internal/models/amo"
|
|
|
|
"amocrm/internal/tools"
|
2024-04-09 15:52:37 +00:00
|
|
|
"context"
|
2024-04-10 08:54:18 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2024-04-09 15:52:37 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
2024-04-11 09:55:26 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2024-04-10 09:12:22 +00:00
|
|
|
"time"
|
2024-04-09 15:52:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *Repository) UpdateListUsers(ctx context.Context) error {
|
|
|
|
//TODO:IMPLEMENT ME
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) GettingUserFromCash(ctx context.Context) (*models.UserListResp, error) {
|
|
|
|
//TODO:IMPLEMENT ME
|
|
|
|
|
|
|
|
return &models.UserListResp{}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-11 09:55:26 +00:00
|
|
|
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
|
|
|
|
}
|
2024-04-09 15:52:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-11 09:55:26 +00:00
|
|
|
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
|
2024-04-09 15:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repository) CreateAccount(ctx context.Context, accountID string) error {
|
|
|
|
userData := models.User{}
|
|
|
|
userData.ObjID = primitive.NewObjectID()
|
2024-04-10 09:12:22 +00:00
|
|
|
userData.Createdat = time.Now().Unix()
|
2024-04-09 15:52:37 +00:00
|
|
|
userData.Accountid = accountID
|
|
|
|
_, err := r.mdbUser.InsertOne(ctx, userData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-10 08:54:18 +00:00
|
|
|
|
|
|
|
func (r *Repository) UpdateAccount(ctx context.Context, accountID string, userInfo *amo2.AmocrmUserInformation) error {
|
2024-04-11 09:29:17 +00:00
|
|
|
filter := bson.M{"AccountID": accountID}
|
2024-04-10 08:54:18 +00:00
|
|
|
// todo надо как то получить роль и почту
|
|
|
|
update := bson.M{
|
|
|
|
"$set": bson.M{
|
2024-04-11 09:29:17 +00:00
|
|
|
"ID": userInfo.UUID, // uuid аккаунта
|
|
|
|
"Name": userInfo.Name, // имя аккаунта амо
|
|
|
|
"Amocrmid": userInfo.ID, // id аккаунта амо
|
|
|
|
"Amouserid": userInfo.CurrentUserID, // id текущего пользователя в амо
|
|
|
|
"Group": tools.ConvertUserGroups(userInfo), // группы пользователя
|
|
|
|
"Country": userInfo.Country, // страна в настройках амо
|
|
|
|
"Subdomain": userInfo.Subdomain, // поддомен организации
|
2024-04-10 08:54:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.mdbUser.UpdateOne(ctx, filter, update)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|