update amo repo methods
This commit is contained in:
parent
9a0b7b45cf
commit
8818afa463
@ -12,9 +12,9 @@ type User struct {
|
||||
/* - почта пользователя из амо*/
|
||||
Email string `json:"Email"`
|
||||
/* - роль пользователя в амо*/
|
||||
Role string `json:"Role"`
|
||||
Role int32 `json:"Role"`
|
||||
/* - группы пользователя в амо*/
|
||||
Group []UserGroups `json:"Group"`
|
||||
Group int32 `json:"Group"`
|
||||
/* - флаг мягкого удаления*/
|
||||
Deleted bool `json:"Deleted"`
|
||||
/* - таймштамп создания аккаунта*/
|
||||
|
||||
@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"github.com/sqlc-dev/pqtype"
|
||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen"
|
||||
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -54,6 +54,7 @@ func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *mode
|
||||
AmoID: row.Amoid,
|
||||
Name: row.Name,
|
||||
Email: row.Email,
|
||||
Group: row.Group,
|
||||
Role: row.Role,
|
||||
Createdat: row.Createdat.Time.Unix(),
|
||||
Subdomain: row.Subdomain,
|
||||
@ -61,14 +62,6 @@ func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *mode
|
||||
Country: row.Country,
|
||||
}
|
||||
count = row.TotalCount
|
||||
var group []model.UserGroups
|
||||
if !row.Group.Valid {
|
||||
err := json.Unmarshal(row.Group.RawMessage, &group)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
user.Group = group
|
||||
|
||||
users = append(users, user)
|
||||
}
|
||||
@ -102,37 +95,24 @@ func (r *AmoRepository) GetCurrentAccount(ctx context.Context, accountID string)
|
||||
Name: row.Name,
|
||||
Email: row.Email,
|
||||
Role: row.Role,
|
||||
Group: row.Group,
|
||||
Createdat: row.Createdat.Time.Unix(),
|
||||
Subdomain: row.Subdomain,
|
||||
Amouserid: row.Amouserid,
|
||||
Country: row.Country,
|
||||
}
|
||||
|
||||
var group []model.UserGroups
|
||||
if !row.Group.Valid {
|
||||
err := json.Unmarshal(row.Group.RawMessage, &group)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
user.Group = group
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *AmoRepository) CreateAccount(ctx context.Context, accountID string, userInfo model.User) error {
|
||||
group, err := json.Marshal(userInfo.Group)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.queries.CreateAmoAccount(ctx, sqlcgen.CreateAmoAccountParams{
|
||||
err := r.queries.CreateAmoAccount(ctx, sqlcgen.CreateAmoAccountParams{
|
||||
Accountid: accountID,
|
||||
Amoid: userInfo.AmoID,
|
||||
Name: userInfo.Name,
|
||||
Email: userInfo.Email,
|
||||
Role: userInfo.Role,
|
||||
Group: pqtype.NullRawMessage{RawMessage: group, Valid: len(group) > 0},
|
||||
Group: userInfo.Group,
|
||||
Createdat: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
Subdomain: userInfo.Subdomain,
|
||||
Amouserid: userInfo.Amouserid,
|
||||
@ -146,25 +126,46 @@ func (r *AmoRepository) CreateAccount(ctx context.Context, accountID string, use
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AmoRepository) CheckUsers(ctx context.Context, amouserid int32, user model.User) error {
|
||||
group, err := json.Marshal(user.Group)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = r.queries.CheckUsers(ctx, sqlcgen.CheckUsersParams{
|
||||
Amoid: amouserid,
|
||||
func (r *AmoRepository) CheckMainUser(ctx context.Context, user model.User) error {
|
||||
err := r.queries.CheckMainUser(ctx, sqlcgen.CheckMainUserParams{
|
||||
Name: user.Name,
|
||||
Group: user.Group,
|
||||
Email: user.Email,
|
||||
Role: user.Role,
|
||||
Group: pqtype.NullRawMessage{RawMessage: group, Valid: len(group) > 0},
|
||||
Amoid: user.AmoID,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
func (r *AmoRepository) CheckAndUpdateUsers(ctx context.Context, user model.User) error {
|
||||
err := r.queries.CheckUsers(ctx, sqlcgen.CheckUsersParams{
|
||||
Amoid: user.AmoID,
|
||||
Name: user.Name,
|
||||
Email: user.Email,
|
||||
Role: user.Role,
|
||||
Group: user.Group,
|
||||
Amouserid: user.Amouserid,
|
||||
})
|
||||
|
||||
// чекаем на конфликт
|
||||
if err != nil && strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
|
||||
err = r.queries.UpdateUsers(ctx, sqlcgen.UpdateUsersParams{
|
||||
Amoid: user.AmoID,
|
||||
Name: user.Name,
|
||||
Email: user.Email,
|
||||
Role: user.Role,
|
||||
Group: user.Group,
|
||||
Amouserid: user.Amouserid,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// методы webhook
|
||||
|
||||
Loading…
Reference in New Issue
Block a user