common/repository/amo/amo.go

1085 lines
24 KiB
Go
Raw Normal View History

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-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
}
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-05-03 15:11:57 +00:00
func (r *AmoRepository) CheckAndUpdateUsers(ctx context.Context, users []model.User) error {
dollar1, err := json.Marshal(users)
if err != nil {
return err
}
rows, err := r.queries.CheckUsers(ctx, dollar1)
if err != nil {
return err
}
2024-04-20 09:13:57 +00:00
2024-05-03 15:11:57 +00:00
if rows != nil {
var toUpdate []model.User
for _, row := range rows {
to := model.User{
AmoID: row.Amoid,
Name: row.Name,
Group: row.Group,
Role: row.Role,
Email: row.Email,
Amouserid: row.Amouserid,
}
toUpdate = append(toUpdate, to)
}
dollar1, err := json.Marshal(toUpdate)
if err != nil {
return err
}
err = r.queries.UpdateUsers(ctx, dollar1)
2024-04-20 09:13:57 +00:00
if err != nil {
return err
}
}
2024-05-03 15:11:57 +00:00
return nil
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-05-07 17:29:43 +00:00
func (r *AmoRepository) DeleteUsers(ctx context.Context, ids []int64) error {
err := r.queries.DeleteUsers(ctx, ids)
if err != nil {
return err
}
return nil
}
func (r *AmoRepository) GetUserUsersByID(ctx context.Context, amoUserID int32) ([]model.User, error) {
rows, err := r.queries.GetUserUsersByID(ctx, amoUserID)
if err != nil {
return nil, err
}
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,
Group: row.Group,
Role: row.Role,
Subdomain: row.Subdomain,
Amouserid: row.Amouserid,
Country: row.Country,
}
users = append(users, user)
}
return users, nil
}
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-05-06 11:03:41 +00:00
func (r *AmoRepository) WebhookUpdate(ctx context.Context, token model.Token) error {
err := r.queries.WebhookUpdate(ctx, sqlcgen.WebhookUpdateParams{
2024-05-06 11:08:33 +00:00
Accesstoken: token.AccessToken,
Refreshtoken: token.RefreshToken,
Accountid: token.AccountID,
Column3: token.Expiration,
Column4: token.CreatedAt,
2024-05-06 11:03:41 +00:00
})
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
}
2024-04-23 15:02:15 +00:00
func (r *AmoRepository) WebhookDelete(ctx context.Context, amoID int) error {
2024-04-23 15:06:39 +00:00
err := r.queries.WebhookDelete(ctx, int32(amoID))
if err != nil {
return err
}
2024-04-17 09:30:19 +00:00
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
}
2024-05-07 17:29:43 +00:00
func (r *AmoRepository) DeletePipelines(ctx context.Context, ids []int64) error {
err := r.queries.DeletePipelines(ctx, ids)
if err != nil {
return err
}
return nil
}
func (r *AmoRepository) GetUserPipelinesByID(ctx context.Context, accountID int32) ([]model.Pipeline, error) {
rows, err := r.queries.GetUserPipelinesByID(ctx, accountID)
if err != nil {
return nil, err
}
var pipelines []model.Pipeline
for _, row := range rows {
pipeline := model.Pipeline{
ID: row.ID,
Amoid: row.Amoid,
AccountID: row.Accountid,
Name: row.Name,
Isarchive: row.Isarchive,
}
pipelines = append(pipelines, pipeline)
}
return pipelines, nil
}
2024-04-17 09:30:19 +00:00
// методы steps
2024-05-15 17:49:48 +00:00
func (r *AmoRepository) GetStepsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string, pipelineID int32) (*model.UserListStepsResp, error) {
2024-04-18 11:00:02 +00:00
rows, err := r.queries.GetStepsWithPagination(ctx, sqlcgen.GetStepsWithPaginationParams{
2024-05-15 17:49:48 +00:00
Accountid: accountID,
Column2: req.Page,
Limit: req.Size,
Pipelineid: pipelineID,
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
}
2024-05-07 17:29:43 +00:00
func (r *AmoRepository) DeleteSteps(ctx context.Context, ids []int64) error {
err := r.queries.DeleteSteps(ctx, ids)
if err != nil {
return err
}
return nil
}
func (r *AmoRepository) GetUserStepsByID(ctx context.Context, accountID int32) ([]model.Step, error) {
rows, err := r.queries.GetUserStepsByID(ctx, accountID)
if err != nil {
return nil, err
}
var steps []model.Step
for _, row := range rows {
step := model.Step{
ID: row.ID,
Amoid: row.Amoid,
Pipelineid: row.Pipelineid,
Accountid: row.Accountid,
Name: row.Name,
Color: row.Color,
}
steps = append(steps, step)
}
return steps, 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
}
2024-05-07 17:29:43 +00:00
func (r *AmoRepository) DeleteTags(ctx context.Context, ids []int64) error {
err := r.queries.DeleteTags(ctx, ids)
if err != nil {
return err
}
return nil
}
func (r *AmoRepository) GetUserTagsByID(ctx context.Context, accountID int32) ([]model.Tag, error) {
rows, err := r.queries.GetUserTagsByID(ctx, accountID)
if err != nil {
return nil, err
}
var tags []model.Tag
for _, row := range rows {
var entity model.EntityType
v := string(row.Entity.([]byte))
entity = model.EntityType(v)
tag := model.Tag{
ID: row.ID,
Amoid: row.Amoid,
Accountid: row.Accountid,
Entity: entity,
Name: row.Name,
Color: &row.Color,
}
tags = append(tags, tag)
}
return tags, nil
}
2024-04-17 09:30:19 +00:00
// методы 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
2024-04-28 13:09:35 +00:00
var fieldType model.FieldType
f := string(row.Type.([]byte))
fieldType = model.FieldType(f)
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,
2024-04-28 13:09:35 +00:00
Type: fieldType,
2024-04-18 11:00:02 +00:00
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)
2024-04-28 13:09:35 +00:00
var fieldType model.FieldType
f := string(row.Type.([]byte))
fieldType = model.FieldType(f)
2024-04-19 08:19:11 +00:00
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,
2024-04-28 13:09:35 +00:00
Type: fieldType,
2024-04-29 22:24:22 +00:00
Name: row.Name,
2024-04-19 08:19:11 +00:00
}
toUpdate = append(toUpdate, to)
}
2024-04-29 22:24:22 +00:00
2024-04-19 08:19:11 +00:00
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
}
2024-04-29 21:52:48 +00:00
func (r *AmoRepository) GetUserFieldsByID(ctx context.Context, accountID int32) ([]model.Field, error) {
rows, err := r.queries.GetUserFieldsByID(ctx, accountID)
if err != nil {
return nil, err
}
var fields []model.Field
for _, row := range rows {
var entity model.EntityType
v := string(row.Entity.([]byte))
entity = model.EntityType(v)
var fieldType model.FieldType
f := string(row.Type.([]byte))
fieldType = model.FieldType(f)
field := model.Field{
ID: row.ID,
Amoid: row.Amoid,
Code: row.Code,
Accountid: row.Accountid,
Name: row.Name,
Entity: entity,
Type: fieldType,
}
fields = append(fields, field)
}
return fields, err
}
2024-05-07 17:29:43 +00:00
func (r *AmoRepository) DeleteFields(ctx context.Context, ids []int64) error {
2024-05-07 17:30:31 +00:00
err := r.queries.DeleteFields(ctx, ids)
2024-05-07 17:29:43 +00:00
if err != nil {
return err
}
return nil
}
func (r *AmoRepository) GetFieldByID(ctx context.Context, amoID int32) (*model.Field, error) {
row, err := r.queries.GetFieldByAmoID(ctx, amoID)
if err != nil {
return nil, err
}
var entity model.EntityType
v := string(row.Entity.([]byte))
entity = model.EntityType(v)
var fieldType model.FieldType
f := string(row.Type.([]byte))
fieldType = model.FieldType(f)
return &model.Field{
ID: row.ID,
Amoid: row.Amoid,
Code: row.Code,
Accountid: row.Accountid,
Name: row.Name,
Entity: entity,
Type: fieldType,
}, nil
}
2024-04-17 09:30:19 +00:00
// методы rules
2024-05-02 11:11:38 +00:00
func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error {
2024-04-23 12:04:24 +00:00
jsonFieldRule, err := json.Marshal(request.Fieldsrule)
if err != nil {
2024-05-02 11:11:38 +00:00
return err
2024-04-23 12:04:24 +00:00
}
2024-06-04 15:54:36 +00:00
_, err = r.queries.ChangeQuizSettings(ctx, sqlcgen.ChangeQuizSettingsParams{
2024-04-23 12:04:24 +00:00
Performerid: request.PerformerID,
Pipelineid: request.PipelineID,
Stepid: request.StepID,
2024-05-17 18:54:53 +00:00
//Utms: request.Utms,
Fieldsrule: jsonFieldRule,
Accountid: accountID,
Quizid: int32(quizID),
2024-04-23 12:04:24 +00:00
})
2024-04-17 09:30:19 +00:00
2024-04-23 12:04:24 +00:00
if err != nil {
2024-05-02 11:11:38 +00:00
return err
2024-04-23 12:04:24 +00:00
}
2024-04-17 09:30:19 +00:00
2024-05-02 11:11:38 +00:00
return nil
2024-04-17 09:30:19 +00:00
}
2024-05-02 11:11:38 +00:00
func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error {
2024-04-23 12:04:24 +00:00
jsonFieldRule, err := json.Marshal(request.Fieldsrule)
if err != nil {
2024-05-02 11:11:38 +00:00
return err
2024-04-23 12:04:24 +00:00
}
2024-06-04 15:54:36 +00:00
_, err = r.queries.SetQuizSettings(ctx, sqlcgen.SetQuizSettingsParams{
2024-04-23 12:04:24 +00:00
Performerid: request.PerformerID,
Pipelineid: request.PipelineID,
Stepid: request.StepID,
2024-05-17 18:54:53 +00:00
//Utms: request.Utms,
Fieldsrule: jsonFieldRule,
Accountid: accountID,
Quizid: int32(quizID),
2024-04-23 12:04:24 +00:00
})
2024-04-17 09:30:19 +00:00
2024-04-23 12:04:24 +00:00
if err != nil {
2024-05-02 11:11:38 +00:00
return err
2024-04-23 12:04:24 +00:00
}
2024-04-17 09:30:19 +00:00
2024-05-02 11:11:38 +00:00
return nil
2024-04-17 09:30:19 +00:00
}
2024-04-23 12:04:24 +00:00
func (r *AmoRepository) GettingQuizRules(ctx context.Context, quizID int) (*model.Rule, error) {
row, err := r.queries.GetQuizRule(ctx, int32(quizID))
if err != nil {
return nil, err
}
2024-04-17 09:30:19 +00:00
2024-04-23 12:04:24 +00:00
var fieldsRule model.Fieldsrule
err = json.Unmarshal(row.Fieldsrule, &fieldsRule)
if err != nil {
return nil, err
}
2024-04-17 09:30:19 +00:00
2024-04-23 12:04:24 +00:00
return &model.Rule{
ID: row.ID,
Accountid: row.Accountid,
Quizid: row.Quizid,
Performerid: row.Performerid,
Pipelineid: row.Pipelineid,
Stepid: row.Stepid,
2024-05-17 18:54:53 +00:00
//Utms: row.Utms,
Fieldsrule: fieldsRule,
2024-04-23 12:04:24 +00:00
}, nil
2024-04-17 09:30:19 +00:00
}
2024-05-02 09:40:25 +00:00
func (r *AmoRepository) UpdateFieldRules(ctx context.Context, fieldRules model.Fieldsrule, accountID string, quizID int32) error {
jsonFieldsRule, err := json.Marshal(fieldRules)
if err != nil {
return err
}
err = r.queries.UpdateFieldRules(ctx, sqlcgen.UpdateFieldRulesParams{
Fieldsrule: jsonFieldsRule,
Accountid: accountID,
Quizid: quizID,
})
if err != nil {
return err
}
return nil
}
2024-04-17 09:30:19 +00:00
// методы UTMs
2024-04-22 14:13:18 +00:00
func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq) error {
2024-05-17 18:54:53 +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-05-17 19:03:20 +00:00
//func (r *AmoRepository) SavingUserUtm(ctx context.Context, utms []model.UTM, accountID string) (*model.ListSavedIDUTMResp, error) {
// 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
//
// return nil, nil
//}
//
//func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.PaginationReq, accountID string, quizID int) (*model.GetListUserUTMResp, error) {
// rows, err := r.queries.GetUTMsWithPagination(ctx, sqlcgen.GetUTMsWithPaginationParams{
// Accountid: accountID,
// Column2: request.Page,
// Limit: request.Size,
// Quizid: int32(quizID),
// })
//
// 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
//}
//
//func (r *AmoRepository) GetUtmsByID(ctx context.Context, ids []int32) ([]model.UTM, error) {
// rows, err := r.queries.GetUtmsByID(ctx, ids)
// if err != nil {
// return nil, err
// }
//
// var utmS []model.UTM
// for _, row := range rows {
// utm := model.UTM{
// ID: row.ID,
// Amofieldid: row.Amofieldid,
// Quizid: row.Quizid,
// Accountid: row.Accountid,
// Name: row.Name,
// }
//
// utmS = append(utmS, utm)
// }
//
// return utmS, nil
//}
//
//func (r *AmoRepository) UpdateUTMs(ctx context.Context, utms []model.UTM) error {
// dollar1, err := json.Marshal(utms)
// if err != nil {
// return err
// }
// err = r.queries.UpdateUtms(ctx, dollar1)
//
// if err != nil {
// return err
// }
//
// return nil
//}
//
//func (r *AmoRepository) UpdateUtmsFields(ctx context.Context, ids []int32) error {
// err := r.queries.UpdateUtmsFields(ctx, ids)
// if err != nil {
// return err
// }
//
// return nil
//}
2024-05-04 10:49:49 +00:00
2024-05-04 11:03:52 +00:00
func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model.AmoUsersTrueResults, error) {
2024-05-04 10:49:49 +00:00
rows, err := r.queries.GettingAmoUsersTrueResults(ctx)
if err != nil {
2024-05-04 11:03:52 +00:00
return nil, err
}
var results []model.AmoUsersTrueResults
for _, row := range rows {
var fieldsRule model.Fieldsrule
err = json.Unmarshal(row.Fieldsrule, &fieldsRule)
if err != nil {
return nil, err
}
2024-05-17 19:03:20 +00:00
var utm model.UTMSavingMap
err = json.Unmarshal(row.Utm, &utm)
if err != nil {
return nil, err
}
2024-05-04 11:03:52 +00:00
result := model.AmoUsersTrueResults{
2024-05-17 19:03:20 +00:00
QuizID: row.QuizID,
AnswerID: row.ID,
Result: row.Result.Bool,
QuestionID: row.QuestionID,
Content: row.Content.String,
Session: row.Session.String,
AccessToken: row.Accesstoken,
AmoAccountID: row.Accountid,
UTMs: utm,
2024-05-05 07:47:42 +00:00
FieldsRule: fieldsRule,
PerformerID: row.Performerid,
StepID: row.Stepid,
PipelineID: row.Pipelineid,
PerformerName: row.PerformerName,
2024-05-04 11:03:52 +00:00
}
results = append(results, result)
2024-05-04 10:49:49 +00:00
}
2024-05-04 11:03:52 +00:00
return results, nil
2024-05-04 10:49:49 +00:00
}
2024-05-06 10:01:56 +00:00
2024-05-06 17:43:50 +00:00
type SaveDealAmoDeps struct {
DealID int32
AnswerID int64
Status string
AccessToken string
}
2024-05-06 17:43:50 +00:00
func (r *AmoRepository) SaveDealAmoStatus(ctx context.Context, deps SaveDealAmoDeps) error {
err := r.queries.SettingDealAmoStatus(ctx, sqlcgen.SettingDealAmoStatusParams{
Dealid: deps.DealID,
Answerid: deps.AnswerID,
Status: deps.Status,
Accesstoken: deps.AccessToken,
2024-05-06 10:01:56 +00:00
})
if err != nil {
return err
}
return nil
}
2024-05-06 17:43:50 +00:00
func (r *AmoRepository) UpdatingDealAmoStatus(ctx context.Context, deps SaveDealAmoDeps) error {
err := r.queries.UpdatingDealAmoStatus(ctx, sqlcgen.UpdatingDealAmoStatusParams{
Status: deps.Status,
Accesstoken: deps.AccessToken,
2024-05-06 17:47:36 +00:00
Dealid: deps.DealID,
2024-05-06 17:43:50 +00:00
})
if err != nil {
return err
}
return nil
}