2024-04-17 09:30:19 +00:00
|
|
|
package amo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-04-17 17:42:13 +00:00
|
|
|
"encoding/json"
|
2024-04-17 09:30:19 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen"
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
2024-04-20 09:13:57 +00:00
|
|
|
"strings"
|
2024-04-17 18:09:09 +00:00
|
|
|
"time"
|
2024-04-17 09:30:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AmoRepository struct {
|
|
|
|
queries *sqlcgen.Queries
|
|
|
|
pool *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
type Deps struct {
|
|
|
|
Queries *sqlcgen.Queries
|
|
|
|
Pool *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAmoRepository(deps Deps) *AmoRepository {
|
|
|
|
return &AmoRepository{
|
|
|
|
queries: deps.Queries,
|
|
|
|
pool: deps.Pool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// методы пользователя
|
|
|
|
|
2024-04-21 15:21:36 +00:00
|
|
|
func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListResp, error) {
|
2024-04-17 17:42:13 +00:00
|
|
|
rows, err := r.queries.GetUsersWithPagination(ctx, sqlcgen.GetUsersWithPaginationParams{
|
2024-04-22 08:43:59 +00:00
|
|
|
Column2: req.Page,
|
2024-04-21 15:21:36 +00:00
|
|
|
Limit: req.Size,
|
|
|
|
Accountid: accountID,
|
2024-04-17 17:42:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-17 18:09:09 +00:00
|
|
|
var count int64
|
2024-04-17 17:42:13 +00:00
|
|
|
var users []model.User
|
|
|
|
for _, row := range rows {
|
|
|
|
user := model.User{
|
|
|
|
ID: row.ID,
|
|
|
|
Accountid: row.Accountid,
|
|
|
|
AmoID: row.Amoid,
|
|
|
|
Name: row.Name,
|
|
|
|
Email: row.Email,
|
2024-04-20 09:13:57 +00:00
|
|
|
Group: row.Group,
|
2024-04-17 17:42:13 +00:00
|
|
|
Role: row.Role,
|
|
|
|
Createdat: row.Createdat.Time.Unix(),
|
|
|
|
Subdomain: row.Subdomain,
|
|
|
|
Amouserid: row.Amouserid,
|
|
|
|
Country: row.Country,
|
|
|
|
}
|
2024-04-17 18:09:09 +00:00
|
|
|
count = row.TotalCount
|
2024-04-17 17:42:13 +00:00
|
|
|
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:09:09 +00:00
|
|
|
resp := model.UserListResp{
|
|
|
|
Count: count,
|
|
|
|
Items: users,
|
|
|
|
}
|
2024-04-17 17:42:13 +00:00
|
|
|
|
2024-04-17 18:09:09 +00:00
|
|
|
return &resp, nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AmoRepository) SoftDeleteAccount(ctx context.Context, accountID string) error {
|
2024-04-17 18:09:09 +00:00
|
|
|
err := r.queries.SoftDeleteAccount(ctx, accountID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-17 09:30:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AmoRepository) GetCurrentAccount(ctx context.Context, accountID string) (*model.User, error) {
|
2024-04-17 18:09:09 +00:00
|
|
|
row, err := r.queries.GetCurrentAccount(ctx, accountID)
|
2024-04-17 18:15:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user := model.User{
|
|
|
|
ID: row.ID,
|
|
|
|
Accountid: row.Accountid,
|
|
|
|
AmoID: row.Amoid,
|
|
|
|
Name: row.Name,
|
|
|
|
Email: row.Email,
|
|
|
|
Role: row.Role,
|
2024-04-20 09:13:57 +00:00
|
|
|
Group: row.Group,
|
2024-04-17 18:15:20 +00:00
|
|
|
Createdat: row.Createdat.Time.Unix(),
|
|
|
|
Subdomain: row.Subdomain,
|
|
|
|
Amouserid: row.Amouserid,
|
|
|
|
Country: row.Country,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 17:14:12 +00:00
|
|
|
func (r *AmoRepository) CreateAccount(ctx context.Context, accountID string, userInfo model.User) error {
|
2024-04-20 09:13:57 +00:00
|
|
|
err := r.queries.CreateAmoAccount(ctx, sqlcgen.CreateAmoAccountParams{
|
2024-04-17 18:09:09 +00:00
|
|
|
Accountid: accountID,
|
|
|
|
Amoid: userInfo.AmoID,
|
|
|
|
Name: userInfo.Name,
|
|
|
|
Email: userInfo.Email,
|
|
|
|
Role: userInfo.Role,
|
2024-04-20 09:13:57 +00:00
|
|
|
Group: userInfo.Group,
|
2024-04-17 18:09:09 +00:00
|
|
|
Createdat: sql.NullTime{Time: time.Now(), Valid: true},
|
|
|
|
Subdomain: userInfo.Subdomain,
|
|
|
|
Amouserid: userInfo.Amouserid,
|
|
|
|
Country: userInfo.Country,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-17 09:30:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-20 09:13:57 +00:00
|
|
|
func (r *AmoRepository) CheckMainUser(ctx context.Context, user model.User) error {
|
|
|
|
err := r.queries.CheckMainUser(ctx, sqlcgen.CheckMainUserParams{
|
2024-04-17 18:09:09 +00:00
|
|
|
Name: user.Name,
|
2024-04-20 09:13:57 +00:00
|
|
|
Group: user.Group,
|
2024-04-17 18:09:09 +00:00
|
|
|
Email: user.Email,
|
|
|
|
Role: user.Role,
|
2024-04-20 09:13:57 +00:00
|
|
|
Amoid: user.AmoID,
|
2024-04-17 18:09:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-20 09:14:24 +00:00
|
|
|
|
|
|
|
return nil
|
2024-04-20 09:13:57 +00:00
|
|
|
}
|
2024-04-17 09:30:19 +00:00
|
|
|
|
2024-04-20 09:13:57 +00:00
|
|
|
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
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 13:57:43 +00:00
|
|
|
func (r *AmoRepository) GetTokenByID(ctx context.Context, accountID string) (*model.Token, error) {
|
2024-04-21 13:59:53 +00:00
|
|
|
row, err := r.queries.GetTokenById(ctx, accountID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.Token{
|
|
|
|
AccountID: row.Accountid,
|
|
|
|
RefreshToken: row.Refreshtoken,
|
|
|
|
AccessToken: row.Accesstoken,
|
|
|
|
}, nil
|
2024-04-21 13:57:43 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 09:30:19 +00:00
|
|
|
// методы webhook
|
|
|
|
|
|
|
|
func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) error {
|
2024-04-17 18:09:09 +00:00
|
|
|
err := r.queries.CreateWebHook(ctx, sqlcgen.CreateWebHookParams{
|
|
|
|
Accountid: tokens.AccountID,
|
|
|
|
Refreshtoken: tokens.RefreshToken,
|
|
|
|
Accesstoken: tokens.AccessToken,
|
|
|
|
Authcode: tokens.AuthCode,
|
2024-04-19 08:54:50 +00:00
|
|
|
Expiration: time.Unix(tokens.Expiration, 0).In(time.Unix(tokens.CreatedAt, 0).Location()),
|
2024-04-17 18:09:09 +00:00
|
|
|
Createdat: sql.NullTime{Time: time.Unix(tokens.CreatedAt, 0), Valid: true},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-17 09:30:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-21 09:17:18 +00:00
|
|
|
func (r *AmoRepository) WebhookUpdate(ctx context.Context, tokens []model.Token) error {
|
|
|
|
dollar1, err := json.Marshal(tokens)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-17 18:09:09 +00:00
|
|
|
|
2024-04-21 09:17:18 +00:00
|
|
|
err = r.queries.WebhookUpdate(ctx, dollar1)
|
2024-04-17 18:09:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-17 09:30:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// воркер запускается каждые 5 минут, поэтомму ищем токены котторые исекают менее чем через 10 минут отдаем их на обноление
|
|
|
|
func (r *AmoRepository) CheckExpired(ctx context.Context) ([]model.Token, error) {
|
2024-04-17 18:09:09 +00:00
|
|
|
rows, err := r.queries.CheckExpired(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokens []model.Token
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
token := model.Token{
|
|
|
|
AccountID: row.Accountid,
|
|
|
|
AccessToken: row.Accesstoken,
|
|
|
|
RefreshToken: row.Refreshtoken,
|
|
|
|
AuthCode: row.Authcode,
|
|
|
|
Expiration: row.Expiration.Unix(),
|
|
|
|
CreatedAt: row.Createdat.Time.Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens = append(tokens, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens, nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AmoRepository) GetAllTokens(ctx context.Context) ([]model.Token, error) {
|
2024-04-17 18:09:09 +00:00
|
|
|
rows, err := r.queries.GetAllTokens(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokens []model.Token
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
token := model.Token{
|
|
|
|
AccountID: row.Accountid,
|
|
|
|
AccessToken: row.Accesstoken,
|
|
|
|
RefreshToken: row.Refreshtoken,
|
|
|
|
AuthCode: row.Authcode,
|
|
|
|
Expiration: row.Expiration.Unix(),
|
|
|
|
CreatedAt: row.Createdat.Time.Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens = append(tokens, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens, nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AmoRepository) WebhookDelete(ctx context.Context) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// методы pipelines
|
|
|
|
|
2024-04-21 15:21:36 +00:00
|
|
|
func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListPipelinesResp, error) {
|
2024-04-18 11:00:02 +00:00
|
|
|
rows, err := r.queries.GetPipelinesWithPagination(ctx, sqlcgen.GetPipelinesWithPaginationParams{
|
2024-04-21 15:21:36 +00:00
|
|
|
Accountid: accountID,
|
|
|
|
Column2: req.Page,
|
|
|
|
Limit: req.Size,
|
2024-04-18 11:00:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
var pipelines []model.Pipeline
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
count = row.TotalCount
|
|
|
|
pipeline := model.Pipeline{
|
|
|
|
ID: row.ID,
|
|
|
|
Amoid: row.Amoid,
|
|
|
|
AccountID: row.Accountid,
|
|
|
|
Name: row.Name,
|
|
|
|
Isarchive: row.Isarchive,
|
|
|
|
Createdat: row.Createdat.Time.Unix(),
|
|
|
|
}
|
|
|
|
pipelines = append(pipelines, pipeline)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := model.UserListPipelinesResp{
|
|
|
|
Count: count,
|
|
|
|
Items: pipelines,
|
|
|
|
}
|
|
|
|
return &resp, nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 13:15:20 +00:00
|
|
|
func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pipeline) error {
|
2024-04-19 08:19:11 +00:00
|
|
|
dollar1, err := json.Marshal(pipelines)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rows, err := r.queries.CheckPipelines(ctx, dollar1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows != nil {
|
|
|
|
var toUpdate []model.Pipeline
|
|
|
|
for _, row := range rows {
|
|
|
|
to := model.Pipeline{
|
|
|
|
Amoid: row.Amoid,
|
|
|
|
AccountID: row.Accountid,
|
|
|
|
Name: row.Name,
|
|
|
|
Isarchive: row.Isarchive,
|
|
|
|
}
|
|
|
|
toUpdate = append(toUpdate, to)
|
|
|
|
}
|
|
|
|
dollar1, err := json.Marshal(toUpdate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.queries.UpdatePipelines(ctx, dollar1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 13:20:05 +00:00
|
|
|
|
2024-04-17 09:30:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// методы steps
|
|
|
|
|
2024-04-21 15:21:36 +00:00
|
|
|
func (r *AmoRepository) GetStepsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListStepsResp, error) {
|
2024-04-18 11:00:02 +00:00
|
|
|
rows, err := r.queries.GetStepsWithPagination(ctx, sqlcgen.GetStepsWithPaginationParams{
|
2024-04-21 15:21:36 +00:00
|
|
|
Accountid: accountID,
|
|
|
|
Column2: req.Page,
|
|
|
|
Limit: req.Size,
|
2024-04-18 11:00:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
var steps []model.Step
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
count = row.TotalCount
|
|
|
|
step := model.Step{
|
|
|
|
ID: row.ID,
|
|
|
|
Amoid: row.Amoid,
|
|
|
|
Pipelineid: row.Pipelineid,
|
|
|
|
Accountid: row.Accountid,
|
|
|
|
Name: row.Name,
|
|
|
|
Color: row.Color,
|
|
|
|
Createdat: row.Createdat.Time.Unix(),
|
|
|
|
}
|
|
|
|
steps = append(steps, step)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := model.UserListStepsResp{
|
|
|
|
Count: count,
|
|
|
|
Items: steps,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 13:15:20 +00:00
|
|
|
func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) error {
|
2024-04-19 08:19:11 +00:00
|
|
|
dollar1, err := json.Marshal(steps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := r.queries.CheckSteps(ctx, dollar1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows != nil {
|
|
|
|
var toUpdate []model.Step
|
|
|
|
for _, row := range rows {
|
|
|
|
to := model.Step{
|
|
|
|
Amoid: row.Amoid,
|
|
|
|
Pipelineid: row.Pipelineid,
|
|
|
|
Accountid: row.Accountid,
|
|
|
|
Name: row.Name,
|
|
|
|
Color: row.Color,
|
|
|
|
}
|
|
|
|
toUpdate = append(toUpdate, to)
|
|
|
|
}
|
|
|
|
|
|
|
|
dollar1, err := json.Marshal(toUpdate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.queries.UpdateSteps(ctx, dollar1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-17 09:30:19 +00:00
|
|
|
|
2024-04-18 13:20:05 +00:00
|
|
|
return nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// методы tags
|
|
|
|
|
2024-04-21 15:21:36 +00:00
|
|
|
func (r *AmoRepository) GetTagsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListTagsResp, error) {
|
2024-04-18 11:00:02 +00:00
|
|
|
rows, err := r.queries.GetTagsWithPagination(ctx, sqlcgen.GetTagsWithPaginationParams{
|
2024-04-21 15:21:36 +00:00
|
|
|
Accountid: accountID,
|
|
|
|
Column2: req.Page,
|
|
|
|
Limit: req.Size,
|
2024-04-18 11:00:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
var tags []model.Tag
|
|
|
|
for _, row := range rows {
|
|
|
|
count = row.TotalCount
|
|
|
|
|
|
|
|
var entity model.EntityType
|
2024-04-18 20:35:30 +00:00
|
|
|
v := string(row.Entity.([]byte))
|
|
|
|
entity = model.EntityType(v)
|
2024-04-18 11:00:02 +00:00
|
|
|
|
|
|
|
tag := model.Tag{
|
|
|
|
ID: row.ID,
|
|
|
|
Amoid: row.Amoid,
|
|
|
|
Accountid: row.Accountid,
|
|
|
|
Entity: entity,
|
|
|
|
Name: row.Name,
|
|
|
|
Color: &row.Color,
|
|
|
|
Createdat: row.Createdat.Time.Unix(),
|
|
|
|
}
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := model.UserListTagsResp{
|
|
|
|
Count: count,
|
|
|
|
Items: tags,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 12:01:01 +00:00
|
|
|
func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID string) error {
|
2024-04-18 13:54:33 +00:00
|
|
|
column2, err := json.Marshal(tags)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-04-18 11:00:02 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 20:14:38 +00:00
|
|
|
rows, err := r.queries.CheckTags(ctx, sqlcgen.CheckTagsParams{
|
2024-04-18 11:00:02 +00:00
|
|
|
Accountid: tokenID,
|
|
|
|
Column2: column2,
|
|
|
|
})
|
2024-04-17 09:30:19 +00:00
|
|
|
|
2024-04-18 20:14:38 +00:00
|
|
|
if rows != nil {
|
|
|
|
var toUpdate []model.Tag
|
|
|
|
for _, row := range rows {
|
|
|
|
var entity model.EntityType
|
2024-04-18 20:35:30 +00:00
|
|
|
v := string(row.Entity.([]byte))
|
|
|
|
entity = model.EntityType(v)
|
2024-04-18 20:14:38 +00:00
|
|
|
to := model.Tag{
|
|
|
|
Amoid: row.Amoid,
|
2024-04-22 14:48:44 +00:00
|
|
|
Accountid: row.Amoid_2,
|
2024-04-18 20:14:38 +00:00
|
|
|
Entity: entity,
|
|
|
|
Name: row.Name,
|
|
|
|
Color: &row.Color,
|
|
|
|
}
|
|
|
|
toUpdate = append(toUpdate, to)
|
|
|
|
}
|
|
|
|
|
|
|
|
dollar1, err := json.Marshal(toUpdate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.queries.UpdateTags(ctx, dollar1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-18 11:00:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-17 09:30:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// методы fields
|
|
|
|
|
2024-04-21 15:21:36 +00:00
|
|
|
func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListFieldsResp, error) {
|
2024-04-18 11:00:02 +00:00
|
|
|
rows, err := r.queries.GetFieldsWithPagination(ctx, sqlcgen.GetFieldsWithPaginationParams{
|
2024-04-21 15:21:36 +00:00
|
|
|
Accountid: accountID,
|
|
|
|
Column2: req.Page,
|
|
|
|
Limit: req.Size,
|
2024-04-18 11:00:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
var fields []model.Field
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
count = row.TotalCount
|
|
|
|
|
|
|
|
var entity model.EntityType
|
2024-04-18 20:35:30 +00:00
|
|
|
v := string(row.Entity.([]byte))
|
|
|
|
entity = model.EntityType(v)
|
2024-04-18 11:00:02 +00:00
|
|
|
|
|
|
|
field := model.Field{
|
|
|
|
ID: row.ID,
|
|
|
|
Amoid: row.Amoid,
|
|
|
|
Code: row.Code,
|
|
|
|
Accountid: row.Accountid,
|
|
|
|
Name: row.Name,
|
|
|
|
Entity: entity,
|
|
|
|
Type: row.Type,
|
|
|
|
Createdat: row.Createdat.Time.Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
fields = append(fields, field)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := model.UserListFieldsResp{
|
|
|
|
Count: count,
|
|
|
|
Items: fields,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 13:15:20 +00:00
|
|
|
func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, tokenID string) error {
|
2024-04-19 08:19:11 +00:00
|
|
|
column2, err := json.Marshal(fields)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := r.queries.CheckFields(ctx, sqlcgen.CheckFieldsParams{
|
|
|
|
Accountid: tokenID,
|
|
|
|
Column2: column2,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows != nil {
|
|
|
|
var toUpdate []model.Field
|
|
|
|
for _, row := range rows {
|
|
|
|
var entity model.EntityType
|
|
|
|
v := string(row.Entity.([]byte))
|
|
|
|
entity = model.EntityType(v)
|
|
|
|
to := model.Field{
|
|
|
|
Amoid: row.Amoid,
|
|
|
|
Code: row.Code,
|
2024-04-22 14:48:44 +00:00
|
|
|
Accountid: row.Amoid_2,
|
2024-04-19 08:19:11 +00:00
|
|
|
Entity: entity,
|
|
|
|
Type: row.Type,
|
|
|
|
}
|
|
|
|
toUpdate = append(toUpdate, to)
|
|
|
|
}
|
|
|
|
dollar1, err := json.Marshal(toUpdate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.queries.UpdateFields(ctx, dollar1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-04-17 09:30:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// методы rules
|
|
|
|
|
2024-04-23 11:42:13 +00:00
|
|
|
func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error {
|
2024-04-17 09:30:19 +00:00
|
|
|
//TODO:IMPLEMENT ME
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-04-23 11:42:13 +00:00
|
|
|
func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error {
|
2024-04-17 09:30:19 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AmoRepository) GettingQuizRules(ctx context.Context) (*model.Rule, error) {
|
|
|
|
//TODO:IMPLEMENT ME
|
|
|
|
|
|
|
|
return &model.Rule{}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// методы UTMs
|
|
|
|
|
2024-04-22 14:13:18 +00:00
|
|
|
func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq) error {
|
2024-04-22 15:13:59 +00:00
|
|
|
err := r.queries.DeletingUTM(ctx, request.Utms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-17 09:30:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 15:23:54 +00:00
|
|
|
// todo нужно ли тут ограничивать индексом что то
|
2024-04-22 14:50:20 +00:00
|
|
|
func (r *AmoRepository) SavingUserUtm(ctx context.Context, utms []model.UTM, accountID string) (*model.ListSavedIDUTMResp, error) {
|
2024-04-22 15:13:59 +00:00
|
|
|
column2, err := json.Marshal(utms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rows, err := r.queries.SaveUTMs(ctx, sqlcgen.SaveUTMsParams{
|
|
|
|
Accountid: accountID,
|
|
|
|
Column2: column2,
|
|
|
|
})
|
|
|
|
|
|
|
|
var ids []int64
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
ids = append(ids, row.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.ListSavedIDUTMResp{
|
|
|
|
Ids: ids,
|
|
|
|
}, nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.PaginationReq, accountID string, quizID int) (*model.GetListUserUTMResp, error) {
|
2024-04-22 15:13:59 +00:00
|
|
|
rows, err := r.queries.GetUTMsWithPagination(ctx, sqlcgen.GetUTMsWithPaginationParams{
|
|
|
|
Accountid: accountID,
|
|
|
|
Column2: request.Page,
|
|
|
|
Limit: request.Size,
|
2024-04-22 15:16:12 +00:00
|
|
|
Quizid: int32(quizID),
|
2024-04-22 15:13:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
var utmS []model.UTM
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
count = row.TotalCount
|
|
|
|
utm := model.UTM{
|
|
|
|
ID: row.ID,
|
|
|
|
Amofieldid: row.Amofieldid,
|
|
|
|
Quizid: row.Quizid,
|
|
|
|
Accountid: row.Accountid,
|
|
|
|
Name: row.Name,
|
|
|
|
Deleted: row.Deleted,
|
|
|
|
Createdat: row.Createdat.Time.Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
utmS = append(utmS, utm)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.GetListUserUTMResp{
|
|
|
|
Count: count,
|
|
|
|
Items: utmS,
|
|
|
|
}, nil
|
2024-04-17 09:30:19 +00:00
|
|
|
}
|