From b73bae19666260e30fec5508dca6f27d9b817bd6 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 12:30:19 +0300 Subject: [PATCH 001/187] init amo repo in common repo --- dal/dal.go | 46 +++++++- model/amo.go | 188 +++++++++++++++++++++++++++++++ model/amoReq.go | 23 ++++ model/amoResp.go | 72 ++++++++++++ repository/amo/amo.go | 255 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 582 insertions(+), 2 deletions(-) create mode 100644 model/amo.go create mode 100644 model/amoReq.go create mode 100644 model/amoResp.go create mode 100644 repository/amo/amo.go diff --git a/dal/dal.go b/dal/dal.go index c64e4bc..3acc8a5 100644 --- a/dal/dal.go +++ b/dal/dal.go @@ -12,6 +12,7 @@ import ( "github.com/minio/minio-go/v7" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/account" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/amo" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/answer" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/question" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/quiz" @@ -51,8 +52,8 @@ func New(ctx context.Context, cred string, minioClient *minio.Client) (*DAL, err queries := sqlcgen.New(pool) accountRepo := account.NewAccountRepository(account.Deps{ - Queries: queries, - Pool: pool, + Queries: queries, + Pool: pool, }) storerAnswer := &answer.StorerAnswer{} @@ -130,3 +131,44 @@ func (d *DAL) Init() error { } return nil } + +type AmoDal struct { + conn *sql.DB + queries *sqlcgen.Queries + AmoRepo *amo.AmoRepository +} + +func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { + pool, err := sql.Open("postgres", cred) + if err != nil { + return nil, err + } + + timeoutCtx, cancel := context.WithTimeout(ctx, time.Second) + defer cancel() + + if err := pool.PingContext(timeoutCtx); err != nil { + return nil, err + } + + queries := sqlcgen.New(pool) + + amoRepo := amo.NewAmoRepository(amo.Deps{ + Queries: queries, + Pool: pool, + }) + + return &AmoDal{ + conn: pool, + queries: queries, + AmoRepo: amoRepo, + }, nil +} + +func (d *AmoDal) Close(ctx context.Context) error { + err := d.conn.Close() + if err != nil { + return err + } + return nil +} diff --git a/model/amo.go b/model/amo.go new file mode 100644 index 0000000..74e268a --- /dev/null +++ b/model/amo.go @@ -0,0 +1,188 @@ +package model + +type User struct { + /* - айдишник в нашей системе Primary Key*/ + ID int `json:"ID"` + /* - id пользователя из токена в нашей системе*/ + Accountid string `json:"AccountID"` + /* - айдишник пользователя в амо*/ + AmoID int `json:"AmocrmID"` + /* - имя аккаунта в амо*/ + Name string `json:"Name"` + /* - почта пользователя из амо*/ + Email string `json:"Email"` + /* - роль пользователя в амо*/ + Role string `json:"Role"` + /* - группы пользователя в амо*/ + Group []UserGroups `json:"Group"` + /* - флаг мягкого удаления*/ + Deleted bool `json:"Deleted"` + /* - таймштамп создания аккаунта*/ + Createdat int64 `json:"CreatedAt"` + /* - поддомен организации в амо*/ + Subdomain string `json:"Subdomain"` + /* - айдишник пользвателя, который подключал интеграцию*/ + Amouserid int `json:"AmoUserID"` + /* - страна указанная в настройках амо*/ + Country string `json:"Country"` +} + +type UserGroups struct { + ID int `json:"id" bson:"id"` + Name string `json:"name" bson:"name"` + UUID interface{} `json:"uuid" bson:"uuid"` +} + +type Token struct { + AccountID string `json:"account_id"` // id в квизе + RefreshToken string `json:"refresh_token"` // 80 дней + AccessToken string `json:"access_token"` // 20 минут + AuthCode string `json:"auth_code"` + Expiration int64 `json:"expiration"` // таймшамп времени когда кончится AccessToken + CreatedAt int64 `json:"created_at"` // таймшамп времени создания, нужен для отслеживания 80 дней +} + +type Pipeline struct { + // айдишник в нашей системе Primary Key + ID int `json:"ID"` + /* - айдишник воронки в амо*/ + Amoid int `json:"AmoID"` + /* - связь с аккаунтом в интеграции амо id аккаунта в амо*/ + AccountID int `json:"AccountID"` + /* - название воронки в амо*/ + Name string `json:"Name"` + /* - флаг архивной воронки в амо*/ + Isarchive bool `json:"IsArchive"` + /* - флаг мягкого удаления*/ + Deleted bool `json:"Deleted"` + /* - таймштамп создания воронки в нашей системе*/ + Createdat int64 `json:"CreatedAt"` + // время обновления + UpdateAt int64 `json:"UpdateAt"` +} + +type Step struct { + /* - айдишник в нашей системе Primary Key*/ + ID int `json:"ID"` + /* - айдишник шага воронки в амо*/ + Amoid int `json:"AmoID"` + /* - айдишник воронки в амо*/ + Pipelineid int `json:"PipelineID"` + /* - связь с аккаунтом в интеграции амо id в амо*/ + Accountid int `json:"AccountID"` + /* - название воронки в амо*/ + Name string `json:"Name"` + /* - цвет шага в амо*/ + Color string `json:"Color"` + /* - флаг мягкого удаления*/ + Deleted bool `json:"Deleted"` + /* - таймштамп создания воронки в нашей системе*/ + Createdat int64 `json:"CreatedAt"` + // время обновления + UpdateAt int64 `json:"UpdateAt"` +} + +type Tag struct { + /* - айдишник в нашей системе Primary Key*/ + ID int `json:"ID"` + /* - айдишник тега в амо*/ + Amoid int `json:"AmoID"` + /* - связь с аккаунтом в интеграции амо id аккаунта в амо*/ + Accountid int `json:"AccountID"` + /* - сущность, к которой принадлежит этот тег. Наверное, стоит сделать через enum в базе*/ + Entity EntityType `json:"Entity"` + /* - название тега в амо*/ + Name string `json:"Name"` + /* - цвет тега в амо*/ + Color *string `json:"Color"` + /* - флаг мягкого удаления*/ + Deleted bool `json:"Deleted"` + /* - таймштамп создания тега в нашей системе*/ + Createdat int64 `json:"CreatedAt"` + // время обновления + UpdateAt int64 `json:"UpdateAt"` +} + +type Field struct { + /* - айдишник в нашей системе Primary Key*/ + ID int `json:"ID"` + /* - айдишник кастомного поля в амо*/ + Amoid int `json:"AmoID"` + /* - кодовое слово в амо*/ + Code string `json:"Code"` + /* - связь с аккаунтом в интеграции амо id аккаунта в амо*/ + Accountid int `json:"AccountID"` + /* - название воронки в амо*/ + Name string `json:"Name"` + /* - тип сущности в амо, для которой это кастомное поле*/ + Entity EntityType `json:"Entity"` + /* - тип поля https://www.amocrm.ru/developers/content/crm_platform/custom-fields#%D0%94%D0%BE%D1%81%D1%82%D1%83%D0%BF%D0%BD%D1%8B%D0%B5-%D1%82%D0%B8%D0%BF%D1%8B-%D0%BF%D0%BE%D0%BB%D0%B5%D0%B9*/ + Type string `json:"Type"` + /* - флаг мягкого удаления*/ + Deleted bool `json:"Deleted"` + /* - таймштамп создания воронки в нашей системе*/ + Createdat int64 `json:"CreatedAt"` + // время обновления + UpdateAt int64 `json:"UpdateAt"` +} + +type EntityType string + +const ( + LeadsTags EntityType = "leads" + ContactsTags EntityType = "contacts" + CompaniesTags EntityType = "companies" + CustomersTags EntityType = "customers" +) + +type Rule struct { + /* - айдишник в нашей системе*/ + ID int `json:"ID"` + /* - связь с аккаунтом в интеграции амо id в амо*/ + Accountid int `json:"AccountID"` + /* - айдишник опроса*/ + Quizid int `json:"QuizID"` + /* - айдишник ответственного за сделку*/ + Performerid int `json:"PerformerID"` + /* - айдишник воронки*/ + Pipelineid int `json:"PipelineID"` + /* - айдишник этапа*/ + Stepid int `json:"StepID"` + /* - список UTM для этого опроса*/ + Utms []int `json:"UTMs"` + /* - правила заполнения полей сущностей в амо*/ + Fieldsrule Fieldsrule `json:"FieldsRule"` + /* - флаг мягкого удаления*/ + Deleted bool `json:"Deleted"` + /* - таймштамп создания воронки в нашей системе*/ + Createdat int `json:"CreatedAt"` +} + +type Fieldsrule struct { + Lead []FieldRule `json:"Lead"` + Contact []FieldRule `json:"Contact"` + Company []FieldRule `json:"Company"` + Customer []FieldRule `json:"Customer"` +} + +type FieldRule struct { + /* - сопоставление айдишника вопроса полю, которое будет заполняться ответом. соответственно QuestionID это айдишник вопроса. это я так мэпу пытался записать*/ + Questionid map[int]int `json:"QuestionID"` // ключ id вопроса значение id астомного поля +} + +type UTM struct { + /* - айдишник в нашей системе Primary Key*/ + ID int `json:"ID" bson:"ID"` + /* - айдишник кастомного поля в амо*/ + Amofieldid int `json:"AmoFieldID"` + /* - айдишник квиза*/ + Quizid int `json:"QuizID"` + /* - связь с аккаунтом в интеграции амо id амо*/ + Accountid int `json:"AccountID"` + /* - название тега в амо*/ + Name string `json:"Name"` + /* - флаг мягкого удаления*/ + Deleted bool `json:"Deleted"` + /* - таймштамп создания тега в нашей системе*/ + Createdat int64 `json:"CreatedAt"` +} diff --git a/model/amoReq.go b/model/amoReq.go new file mode 100644 index 0000000..859dfcc --- /dev/null +++ b/model/amoReq.go @@ -0,0 +1,23 @@ +package model + +type ListDeleteUTMIDsReq struct { + /* - список айдишников utm которые удалить*/ + Utms []string `json:"utms"` +} + +type PaginationReq struct { + /* - указание страницы пагинации. Если страница не указана, применять 0*/ + Page int `json:"page"` + /* - указание размера страницы пагинации. По умолчанию применять 25*/ + Size int `json:"size"` +} + +type RulesReq struct { + /* - ID квиза*/ + ID string `json:"ID"` +} + +type SaveUserListUTMReq struct { + /* - список utm для сохранения. сохранять только те, которых в этом аккаунте ещё нет*/ + Utms []UTM `json:"utms"` +} diff --git a/model/amoResp.go b/model/amoResp.go new file mode 100644 index 0000000..bcf81b2 --- /dev/null +++ b/model/amoResp.go @@ -0,0 +1,72 @@ +package model + +type ConnectAccountResp struct { + /* - ссылка для авторизации в амо*/ + Link string `json:"link"` +} + +type GetCurrentAccountResp struct { + /* - айдишник в нашей системе Primary Key*/ + ID int `json:"ID"` + /* - имя аккаунта в амо*/ + Name string `json:"Name"` + /* - поддомен организации в амо*/ + Subdomain string `json:"Subdomain"` + /* - id пользователя из токена в нашей системе*/ + Accountid string `json:"AccountID"` + /* - айдишник пользвателя, который подключал интеграцию*/ + Amouserid int `json:"AmoUserID"` + /* - связь с аккаунтом в амо*/ + Amocrmid int `json:"AmocrmID"` + /* - страна указанная в настройках амо*/ + Country string `json:"Country"` + /* - таймштамп создания аккаунта*/ + Createdat int64 `json:"CreatedAt"` +} + +type GetListUserUTMResp struct { + /* - общее количество юзеров, которые у нас закешированы для этого пользователя*/ + Count int `json:"count"` + /* - список юзеров, которые были закешированы нашим сервисом*/ + Items []UTM `json:"items"` +} + +type ListSavedIDUTMResp struct { + /* - список айдишников сохранённых меток*/ + Ids []string `json:"IDs"` +} + +type UserListFieldsResp struct { + /* - общее количество кастомных полей, которые у нас закешированы для этого пользователя*/ + Count int `json:"count"` + /* - список кастомных полей, которые были закешированы нашим сервисом*/ + Items []Field `json:"items"` +} + +type UserListPipelinesResp struct { + /* - общее количество воронок, которые у нас закешированы для этого пользователя*/ + Count int `json:"count"` + /* - список воронок, которые были закешированы нашим сервисом*/ + Items []Pipeline `json:"items"` +} + +type UserListResp struct { + /* - общее количество юзеров, которые у нас закешированы для этого пользователя*/ + Count int `json:"count"` + /* - список юзеров, которые были закешированы нашим сервисом*/ + Items []User `json:"items"` +} + +type UserListStepsResp struct { + /* - список шагов воронок, которые были закешированы нашим сервисом*/ + Items []Step `json:"items"` + /* - общее количество шагов воронок, которые у нас закешированы для этого пользователя*/ + Count int `json:"count"` +} + +type UserListTagsResp struct { + /* - общее количество тегов, которые у нас закешированы для этого пользователя*/ + Count int64 `json:"count"` + /* - список тегов, которые были закешированы нашим сервисом*/ + Items []Tag `json:"items"` +} diff --git a/repository/amo/amo.go b/repository/amo/amo.go new file mode 100644 index 0000000..ce39133 --- /dev/null +++ b/repository/amo/amo.go @@ -0,0 +1,255 @@ +package amo + +import ( + "context" + "database/sql" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" +) + +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, + } +} + +// методы пользователя + +func (r *AmoRepository) UpdateListUsers(ctx context.Context) error { + //TODO:IMPLEMENT ME + + return nil + +} + +func (r *AmoRepository) GettingUserFromCash(ctx context.Context, req *model.PaginationReq) (*model.UserListResp, error) { + return nil, nil +} + +func (r *AmoRepository) SoftDeleteAccount(ctx context.Context, accountID string) error { + return nil +} + +func (r *AmoRepository) GetCurrentAccount(ctx context.Context, accountID string) (*model.User, error) { + return nil, nil +} + +func (r *AmoRepository) CreateAccount(ctx context.Context, accountID string) error { + return nil +} + +func (r *AmoRepository) UpdateAccount(ctx context.Context, accountID string, userInfo model.User) error { + return nil +} + +func (r *AmoRepository) CheckUsers(ctx context.Context, amouserid int, user model.User) error { + + return nil +} + +// методы webhook + +func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) error { + return nil +} + +func (r *AmoRepository) WebhookUpdate(ctx context.Context, tokens model.Token) error { + return nil +} + +// воркер запускается каждые 5 минут, поэтомму ищем токены котторые исекают менее чем через 10 минут отдаем их на обноление +func (r *AmoRepository) CheckExpired(ctx context.Context) ([]model.Token, error) { + return nil, nil +} + +func (r *AmoRepository) GetAllTokens(ctx context.Context) ([]model.Token, error) { + return nil, nil +} + +func (r *AmoRepository) WebhookDelete(ctx context.Context) error { + //TODO:IMPLEMENT ME + + return nil + +} + +// методы pipelines + +func (r *AmoRepository) UpdateListPipelines(ctx context.Context) error { + //TODO:IMPLEMENT ME + + return nil + +} + +func (r *AmoRepository) GettingPipelinesFromCash(ctx context.Context, req *model.PaginationReq) (*model.UserListPipelinesResp, error) { + return nil, nil +} + +func (r *AmoRepository) CheckPipelines(ctx context.Context, accountID string, pipelines []model.Pipeline) error { + return nil +} + +func (r *AmoRepository) GetPipelineByID(ctx context.Context, accountID string, amoid int) (*model.Pipeline, error) { + return nil, nil +} + +func (r *AmoRepository) UpdatePipeline(ctx context.Context, pipeline *model.Pipeline) error { + return nil +} + +func (r *AmoRepository) InsertPipeline(ctx context.Context, pipeline *model.Pipeline) error { + return nil +} + +// методы steps + +func (r *AmoRepository) GettingStepsFromCash(ctx context.Context, req *model.PaginationReq) (*model.UserListStepsResp, error) { + return nil, nil +} + +func (r *AmoRepository) UpdateListSteps(ctx context.Context) error { + //TODO:IMPLEMENT ME + + return nil + +} + +func (r *AmoRepository) CheckSteps(ctx context.Context, accountID string, steps []model.Step) error { + + return nil +} + +func (r *AmoRepository) GetStepByID(ctx context.Context, accountID string, amoid int) (*model.Step, error) { + return nil, nil +} + +func (r *AmoRepository) UpdateStep(ctx context.Context, step *model.Step) error { + return nil +} + +func (r *AmoRepository) InsertStep(ctx context.Context, step *model.Step) error { + return nil +} + +// методы tags + +func (r *AmoRepository) GettingTagsFromCash(ctx context.Context, req *model.PaginationReq) (*model.UserListTagsResp, error) { + return nil, nil +} + +func (r *AmoRepository) UpdateListTags(ctx context.Context) error { + //TODO:IMPLEMENT ME + + return nil + +} + +type CheckTagsDeps struct { + AccountID string // id quiz + ID int // id amo + EntityType model.EntityType + Tags []model.Tag +} + +func (r *AmoRepository) CheckTags(ctx context.Context, deps CheckTagsDeps) error { + + return nil +} + +func (r *AmoRepository) GetTagByID(ctx context.Context, accountID string, amoid int, entity model.EntityType) (*model.Tag, error) { + return nil, nil +} + +func (r *AmoRepository) UpdateTag(ctx context.Context, tag *model.Tag) error { + return nil +} + +func (r *AmoRepository) InsertTag(ctx context.Context, tag *model.Tag) error { + return nil +} + +// методы fields + +func (r *AmoRepository) GettingFieldsFromCash(ctx context.Context, req *model.PaginationReq) (*model.UserListFieldsResp, error) { + return nil, nil +} + +func (r *AmoRepository) UpdateListCustom(ctx context.Context) error { + //TODO:IMPLEMENT ME + + return nil + +} + +type CheckFieldsDeps struct { + AccountID string // id quiz + ID int // id amo + EntityType model.EntityType + Fields []model.Field +} + +func (r *AmoRepository) CheckFields(ctx context.Context, deps CheckFieldsDeps) error { + + return nil +} + +func (r *AmoRepository) GetFieldByID(ctx context.Context, accountID string, amoid int, entity model.EntityType) (*model.Field, error) { + return nil, nil +} + +func (r *AmoRepository) UpdateField(ctx context.Context, field *model.Field) error { + return nil +} + +func (r *AmoRepository) InsertField(ctx context.Context, field *model.Field) error { + return nil +} + +// методы rules + +func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq) error { + //TODO:IMPLEMENT ME + + return nil + +} + +func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.RulesReq) error { + //TODO:IMPLEMENT ME + + return nil + +} + +func (r *AmoRepository) GettingQuizRules(ctx context.Context) (*model.Rule, error) { + //TODO:IMPLEMENT ME + + return &model.Rule{}, nil + +} + +// методы UTMs + +func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq) error { + return nil +} + +func (r *AmoRepository) SavingUserUtm(ctx context.Context, request *model.SaveUserListUTMReq, accountID string, quizID int) (*model.ListSavedIDUTMResp, error) { + return nil, nil +} + +func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.PaginationReq, accountID string, quizID int) (*model.GetListUserUTMResp, error) { + return nil, nil +} From 7e817cf088ee84536dabbb6d697fa54960866f1e Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 15:01:01 +0300 Subject: [PATCH 002/187] remove deps in amo repo --- repository/amo/amo.go | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index ce39133..0db08f6 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -156,14 +156,7 @@ func (r *AmoRepository) UpdateListTags(ctx context.Context) error { } -type CheckTagsDeps struct { - AccountID string // id quiz - ID int // id amo - EntityType model.EntityType - Tags []model.Tag -} - -func (r *AmoRepository) CheckTags(ctx context.Context, deps CheckTagsDeps) error { +func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID string) error { return nil } @@ -193,14 +186,7 @@ func (r *AmoRepository) UpdateListCustom(ctx context.Context) error { } -type CheckFieldsDeps struct { - AccountID string // id quiz - ID int // id amo - EntityType model.EntityType - Fields []model.Field -} - -func (r *AmoRepository) CheckFields(ctx context.Context, deps CheckFieldsDeps) error { +func (r *AmoRepository) CheckFields(ctx context.Context, Fields []model.Field, tokenID string) error { return nil } From 5bc95d6c6160882fae601eebcc1d3ec8c954b7dd Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 15:19:39 +0300 Subject: [PATCH 003/187] change type --- model/amoReq.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/amoReq.go b/model/amoReq.go index 859dfcc..7be5c36 100644 --- a/model/amoReq.go +++ b/model/amoReq.go @@ -2,7 +2,7 @@ package model type ListDeleteUTMIDsReq struct { /* - список айдишников utm которые удалить*/ - Utms []string `json:"utms"` + Utms []int `json:"utms"` } type PaginationReq struct { From 111207f0f3ba75c80abba7f668daff4ed119b3b8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 17:48:38 +0300 Subject: [PATCH 004/187] add new tables for amo integration --- dal/schema/000010_init.down.sql | 7 +++ dal/schema/000010_init.up.sql | 75 +++++++++++++++++++++++++++++++++ model/amo.go | 8 ---- 3 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 dal/schema/000010_init.down.sql create mode 100644 dal/schema/000010_init.up.sql diff --git a/dal/schema/000010_init.down.sql b/dal/schema/000010_init.down.sql new file mode 100644 index 0000000..99bfbd1 --- /dev/null +++ b/dal/schema/000010_init.down.sql @@ -0,0 +1,7 @@ +DROP TABLE IF EXISTS tags; +DROP TABLE IF EXISTS fields; +DROP TYPE IF EXISTS EntityType; +DROP TABLE IF EXISTS steps; +DROP TABLE IF EXISTS pipelines; +DROP TABLE IF EXISTS tokens; +DROP TABLE IF EXISTS users; diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql new file mode 100644 index 0000000..3a101c0 --- /dev/null +++ b/dal/schema/000010_init.up.sql @@ -0,0 +1,75 @@ +CREATE TABLE users ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AccountID VARCHAR(30) NOT NULL , -- id квизе из токена + AmoID INT NOT NULL , -- id в амо + Name VARCHAR(50) NOT NULL DEFAULT '', -- имя в амо + Email VARCHAR(50) NOT NULL DEFAULT '', -- почта в амо + Role VARCHAR(50) NOT NULL DEFAULT '', -- роль в амо + Group JSONB, -- вложенная структура так как в амо группы хранятся массивом структур + Deleted BOOLEAN, + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + Subdomain VARCHAR(50) NOT NULL DEFAULT '', + AmoUserID INT NOT NULL , -- id пользователя который подключал интеграцию + Country VARCHAR(50) NOT NULL DEFAULT '', -- страна в амо + FOREIGN KEY (AccountID) REFERENCES tokens (AccountID), -- связь с таблицей tokens + FOREIGN KEY (AmoID) REFERENCES pipelines (AccountID) -- связь с таблицей pipelines +); + +CREATE TABLE tokens ( + AccountID VARCHAR(30) PRIMARY KEY, -- связь с users AccountID + RefreshToken VARCHAR(512) NOT NULL , + AccessToken VARCHAR(512) NOT NULL , + AuthCode VARCHAR(512) NOT NULL , -- код авторизации который получаем при вебхук + Expiration TIMESTAMP NOT NULL, -- время истечения токенов + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE pipelines ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AmoID INT NOT NULL , --id воронки в амо + AccountID INT NOT NULL , --id аккаунта в амо связь с таблицей users AmoID + Name VARCHAR(50) NOT NULL DEFAULT '', --название воронки в амо + IsArchive BOOLEAN, --флаг архивной воронки в амо + Deleted BOOLEAN, + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (AccountID) REFERENCES users (AmoID) -- связь с таблицей users +); + +CREATE TABLE steps ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AmoID INT NOT NULL, --id шага воронки в амо + PipelineID INT NOT NULL, --id воронки AmoID pipelines + AccountID INT NOT NULL, --id аккаунта в амо связь с таблицей users AmoID + Name VARCHAR(50) NOT NULL DEFAULT '', --название воронки в амо + Color VARCHAR(50) NOT NULL DEFAULT '', --цвет шага в амо* + Deleted BOOLEAN, + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (AccountID) REFERENCES users (AmoID), + FOREIGN KEY (PipelineID) REFERENCES pipelines (ID) +); + +CREATE TYPE EntityType AS ENUM ('leads', 'contacts', 'companies', 'customers'); + +CREATE TABLE fields ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AmoID INT NOT NULL, -- айдишник кастомного поля в амо + Code VARCHAR(255) NOT NULL DEFAULT '', -- кодовое слово в амо + AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID + Name VARCHAR(50) NOT NULL DEFAULT '', -- название воронки в амо + Entity EntityType NOT NULL, -- тип сущности в амо, для которой это кастомное поле + Type VARCHAR(50) NOT NULL DEFAULT '', -- тип поля + Deleted BOOLEAN, + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (AccountID) REFERENCES users (AmoID) +); + +CREATE TABLE tags ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AmoID INT NOT NULL, -- айдишник тега в амо + AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID + Entity EntityType NOT NULL, -- сущность, к которой принадлежит этот тег + Name VARCHAR(50) NOT NULL DEFAULT '', -- название тега в амо + Color VARCHAR(50) NOT NULL DEFAULT '', -- цвет тега в амо + Deleted BOOLEAN, + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); diff --git a/model/amo.go b/model/amo.go index 74e268a..53fa9f9 100644 --- a/model/amo.go +++ b/model/amo.go @@ -57,8 +57,6 @@ type Pipeline struct { Deleted bool `json:"Deleted"` /* - таймштамп создания воронки в нашей системе*/ Createdat int64 `json:"CreatedAt"` - // время обновления - UpdateAt int64 `json:"UpdateAt"` } type Step struct { @@ -78,8 +76,6 @@ type Step struct { Deleted bool `json:"Deleted"` /* - таймштамп создания воронки в нашей системе*/ Createdat int64 `json:"CreatedAt"` - // время обновления - UpdateAt int64 `json:"UpdateAt"` } type Tag struct { @@ -99,8 +95,6 @@ type Tag struct { Deleted bool `json:"Deleted"` /* - таймштамп создания тега в нашей системе*/ Createdat int64 `json:"CreatedAt"` - // время обновления - UpdateAt int64 `json:"UpdateAt"` } type Field struct { @@ -122,8 +116,6 @@ type Field struct { Deleted bool `json:"Deleted"` /* - таймштамп создания воронки в нашей системе*/ Createdat int64 `json:"CreatedAt"` - // время обновления - UpdateAt int64 `json:"UpdateAt"` } type EntityType string From 35f553ab1d5f0670ba93d5e00dfac04f81c2a5f9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 18:17:07 +0300 Subject: [PATCH 005/187] add jsonb column to questions table --- dal/schema/000010_init.down.sql | 15 +++++++++++++-- dal/schema/000010_init.up.sql | 27 +++++++++++++++++++-------- model/amo.go | 2 +- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/dal/schema/000010_init.down.sql b/dal/schema/000010_init.down.sql index 99bfbd1..77d693b 100644 --- a/dal/schema/000010_init.down.sql +++ b/dal/schema/000010_init.down.sql @@ -1,7 +1,18 @@ +ALTER TABLE question + DROP COLUMN IF EXISTS utm, + DROP COLUMN IF EXISTS rules; + DROP TABLE IF EXISTS tags; DROP TABLE IF EXISTS fields; -DROP TYPE IF EXISTS EntityType; + +DO $$ + BEGIN + IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'entitytype') THEN + DROP TYPE EntityType; + END IF; + END $$; + DROP TABLE IF EXISTS steps; DROP TABLE IF EXISTS pipelines; DROP TABLE IF EXISTS tokens; -DROP TABLE IF EXISTS users; +DROP TABLE IF EXISTS users; \ No newline at end of file diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 3a101c0..afa070f 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -1,4 +1,4 @@ -CREATE TABLE users ( +CREATE TABLE IF NOT EXISTS users ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AccountID VARCHAR(30) NOT NULL , -- id квизе из токена AmoID INT NOT NULL , -- id в амо @@ -15,7 +15,7 @@ CREATE TABLE users ( FOREIGN KEY (AmoID) REFERENCES pipelines (AccountID) -- связь с таблицей pipelines ); -CREATE TABLE tokens ( +CREATE TABLE IF NOT EXISTS tokens ( AccountID VARCHAR(30) PRIMARY KEY, -- связь с users AccountID RefreshToken VARCHAR(512) NOT NULL , AccessToken VARCHAR(512) NOT NULL , @@ -24,7 +24,7 @@ CREATE TABLE tokens ( CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -CREATE TABLE pipelines ( +CREATE TABLE IF NOT EXISTS pipelines ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL , --id воронки в амо AccountID INT NOT NULL , --id аккаунта в амо связь с таблицей users AmoID @@ -35,7 +35,7 @@ CREATE TABLE pipelines ( FOREIGN KEY (AccountID) REFERENCES users (AmoID) -- связь с таблицей users ); -CREATE TABLE steps ( +CREATE TABLE IF NOT EXISTS steps ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL, --id шага воронки в амо PipelineID INT NOT NULL, --id воронки AmoID pipelines @@ -48,9 +48,14 @@ CREATE TABLE steps ( FOREIGN KEY (PipelineID) REFERENCES pipelines (ID) ); -CREATE TYPE EntityType AS ENUM ('leads', 'contacts', 'companies', 'customers'); +DO $$ + BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'entitytype') THEN + CREATE TYPE EntityType AS ENUM ('leads', 'contacts', 'companies', 'customers'); + END IF; +END $$; -CREATE TABLE fields ( +CREATE TABLE IF NOT EXISTS fields ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL, -- айдишник кастомного поля в амо Code VARCHAR(255) NOT NULL DEFAULT '', -- кодовое слово в амо @@ -63,7 +68,7 @@ CREATE TABLE fields ( FOREIGN KEY (AccountID) REFERENCES users (AmoID) ); -CREATE TABLE tags ( +CREATE TABLE IF NOT EXISTS tags ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL, -- айдишник тега в амо AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID @@ -71,5 +76,11 @@ CREATE TABLE tags ( Name VARCHAR(50) NOT NULL DEFAULT '', -- название тега в амо Color VARCHAR(50) NOT NULL DEFAULT '', -- цвет тега в амо Deleted BOOLEAN, - CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (AccountID) REFERENCES users (AmoID) ); + +ALTER TABLE question +ADD COLUMN utm jsonb NOT NULL DEFAULT '{}'; +ALTER TABLE question +ADD COLUMN rules jsonb NOT NULL DEFAULT '{}'; \ No newline at end of file diff --git a/model/amo.go b/model/amo.go index 53fa9f9..9d315ab 100644 --- a/model/amo.go +++ b/model/amo.go @@ -147,7 +147,7 @@ type Rule struct { /* - флаг мягкого удаления*/ Deleted bool `json:"Deleted"` /* - таймштамп создания воронки в нашей системе*/ - Createdat int `json:"CreatedAt"` + Createdat int64 `json:"CreatedAt"` } type Fieldsrule struct { From 1184036dbe81940882dc87c4fa88ca64b6ccfb91 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 18:20:58 +0300 Subject: [PATCH 006/187] update sqlc yaml --- sqlc.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sqlc.yaml b/sqlc.yaml index 625e6c4..f25b4b1 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -18,6 +18,12 @@ packages: - "./dal/schema/000006_init.down.sql" - "./dal/schema/000007_init.up.sql" - "./dal/schema/000007_init.down.sql" + - "./dal/schema/000008_init.up.sql" + - "./dal/schema/000008_init.down.sql" + - "./dal/schema/000009_init.up.sql" + - "./dal/schema/000009_init.down.sql" + - "./dal/schema/000010_init.up.sql" + - "./dal/schema/000010_init.down.sql" engine: "postgresql" emit_json_tags: true emit_db_tags: true From 9b6327b695096ee30ec3fa8357f5d0d64ad83d3c Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 18:37:04 +0300 Subject: [PATCH 007/187] update migrate files --- dal/schema/000010_init.up.sql | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index afa070f..ce86ee4 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -1,20 +1,3 @@ -CREATE TABLE IF NOT EXISTS users ( - ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, - AccountID VARCHAR(30) NOT NULL , -- id квизе из токена - AmoID INT NOT NULL , -- id в амо - Name VARCHAR(50) NOT NULL DEFAULT '', -- имя в амо - Email VARCHAR(50) NOT NULL DEFAULT '', -- почта в амо - Role VARCHAR(50) NOT NULL DEFAULT '', -- роль в амо - Group JSONB, -- вложенная структура так как в амо группы хранятся массивом структур - Deleted BOOLEAN, - CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - Subdomain VARCHAR(50) NOT NULL DEFAULT '', - AmoUserID INT NOT NULL , -- id пользователя который подключал интеграцию - Country VARCHAR(50) NOT NULL DEFAULT '', -- страна в амо - FOREIGN KEY (AccountID) REFERENCES tokens (AccountID), -- связь с таблицей tokens - FOREIGN KEY (AmoID) REFERENCES pipelines (AccountID) -- связь с таблицей pipelines -); - CREATE TABLE IF NOT EXISTS tokens ( AccountID VARCHAR(30) PRIMARY KEY, -- связь с users AccountID RefreshToken VARCHAR(512) NOT NULL , @@ -24,6 +7,23 @@ CREATE TABLE IF NOT EXISTS tokens ( CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); +CREATE TABLE IF NOT EXISTS users ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AccountID VARCHAR(30) NOT NULL , -- id квизе из токена + AmoID INT NOT NULL , -- id в амо + Name VARCHAR(50) NOT NULL DEFAULT '', -- имя в амо + Email VARCHAR(50) NOT NULL DEFAULT '', -- почта в амо + Role VARCHAR(50) NOT NULL DEFAULT '', -- роль в амо + "Group" JSONB, -- вложенная структура так как в амо группы хранятся массивом структур + Deleted BOOLEAN, + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + Subdomain VARCHAR(50) NOT NULL DEFAULT '', + AmoUserID INT NOT NULL , -- id пользователя который подключал интеграцию + Country VARCHAR(50) NOT NULL DEFAULT '', -- страна в амо + FOREIGN KEY (AccountID) REFERENCES tokens (AccountID), -- связь с таблицей tokens + FOREIGN KEY (AmoID) REFERENCES pipelines (AccountID) -- связь с таблицей pipelines +); + CREATE TABLE IF NOT EXISTS pipelines ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL , --id воронки в амо From ff64fa15e47bf072e4da9aa1a0c94e3c8066be68 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 19:28:46 +0300 Subject: [PATCH 008/187] removed foreign keys from new tables only implicit relationship --- dal/schema/000010_init.up.sql | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index ce86ee4..9cf27bd 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -1,5 +1,5 @@ CREATE TABLE IF NOT EXISTS tokens ( - AccountID VARCHAR(30) PRIMARY KEY, -- связь с users AccountID + AccountID VARCHAR(30) PRIMARY KEY, -- связь с users AccountID неявная посредством join RefreshToken VARCHAR(512) NOT NULL , AccessToken VARCHAR(512) NOT NULL , AuthCode VARCHAR(512) NOT NULL , -- код авторизации который получаем при вебхук @@ -19,33 +19,28 @@ CREATE TABLE IF NOT EXISTS users ( CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, Subdomain VARCHAR(50) NOT NULL DEFAULT '', AmoUserID INT NOT NULL , -- id пользователя который подключал интеграцию - Country VARCHAR(50) NOT NULL DEFAULT '', -- страна в амо - FOREIGN KEY (AccountID) REFERENCES tokens (AccountID), -- связь с таблицей tokens - FOREIGN KEY (AmoID) REFERENCES pipelines (AccountID) -- связь с таблицей pipelines + Country VARCHAR(50) NOT NULL DEFAULT '' -- страна в амо ); CREATE TABLE IF NOT EXISTS pipelines ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL , --id воронки в амо - AccountID INT NOT NULL , --id аккаунта в амо связь с таблицей users AmoID + AccountID INT NOT NULL , --id аккаунта в амо связь с таблицей users AmoID неявная посредством join Name VARCHAR(50) NOT NULL DEFAULT '', --название воронки в амо IsArchive BOOLEAN, --флаг архивной воронки в амо Deleted BOOLEAN, - CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (AccountID) REFERENCES users (AmoID) -- связь с таблицей users + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS steps ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL, --id шага воронки в амо - PipelineID INT NOT NULL, --id воронки AmoID pipelines - AccountID INT NOT NULL, --id аккаунта в амо связь с таблицей users AmoID + PipelineID INT NOT NULL, --id воронки AmoID pipelines неявная посредством join + AccountID INT NOT NULL, --id аккаунта в амо связь с таблицей users AmoID неявная посредством join Name VARCHAR(50) NOT NULL DEFAULT '', --название воронки в амо Color VARCHAR(50) NOT NULL DEFAULT '', --цвет шага в амо* Deleted BOOLEAN, - CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (AccountID) REFERENCES users (AmoID), - FOREIGN KEY (PipelineID) REFERENCES pipelines (ID) + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); DO $$ @@ -59,25 +54,23 @@ CREATE TABLE IF NOT EXISTS fields ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL, -- айдишник кастомного поля в амо Code VARCHAR(255) NOT NULL DEFAULT '', -- кодовое слово в амо - AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID + AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID неявная посредством join Name VARCHAR(50) NOT NULL DEFAULT '', -- название воронки в амо Entity EntityType NOT NULL, -- тип сущности в амо, для которой это кастомное поле Type VARCHAR(50) NOT NULL DEFAULT '', -- тип поля Deleted BOOLEAN, - CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (AccountID) REFERENCES users (AmoID) + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS tags ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL, -- айдишник тега в амо - AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID + AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID неявная посредством join Entity EntityType NOT NULL, -- сущность, к которой принадлежит этот тег Name VARCHAR(50) NOT NULL DEFAULT '', -- название тега в амо Color VARCHAR(50) NOT NULL DEFAULT '', -- цвет тега в амо Deleted BOOLEAN, - CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (AccountID) REFERENCES users (AmoID) + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ALTER TABLE question From 0c528d5afb836595b944dd686c8a4d7a7cbd233e Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 20:14:12 +0300 Subject: [PATCH 009/187] add some amo sql queries for account and webhook --- dal/db_query/queries.sql | 35 +++++++++++++++++++++++++++++++++++ repository/amo/amo.go | 6 +----- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 80d384a..30b8cb3 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -648,3 +648,38 @@ SELECT (SELECT registration_count FROM Registrations) AS registrations, (SELECT quiz_count FROM Quizes) AS quizes, (SELECT result_count FROM Results) AS results; + + +-- amo methods: + +-- name: CreateAmoAccount :exec +INSERT INTO users (AccountID, AmoID, Name, Email, Role, "Group", Deleted, CreatedAt, Subdomain, AmoUserID, Country) +VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11); + +-- name: CreateWebHook :exec +INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, CreatedAt) +VALUES ($1, $2, $3, $4, $5, $6); + +-- name: WebhookUpdate :exec +UPDATE tokens SET AccessToken = $1, RefreshToken = $2, Expiration = $3, CreatedAt = $4 WHERE AccountID = $5; + +-- name: GetAllTokens :many +SELECT * FROM tokens; + +-- name: CheckExpired :many +SELECT * FROM tokens WHERE Expiration <= EXTRACT(EPOCH FROM NOW()) + (10 * 60); + +-- name: WebhookDelete :exec +DELETE FROM tokens WHERE AccountID = $1; + +-- name: SoftDeleteAccount :exec +UPDATE users SET Deleted = TRUE WHERE AccountID = $1; + +-- name: GetCurrentAccount :exec +SELECT * FROM users WHERE AccountID = $1; + +-- name: CheckUsers :exec +UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5; + +-- name: GetUsersWithPagination :many +SELECT * FROM users ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; \ No newline at end of file diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 0db08f6..581b21c 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -45,11 +45,7 @@ func (r *AmoRepository) GetCurrentAccount(ctx context.Context, accountID string) return nil, nil } -func (r *AmoRepository) CreateAccount(ctx context.Context, accountID string) error { - return nil -} - -func (r *AmoRepository) UpdateAccount(ctx context.Context, accountID string, userInfo model.User) error { +func (r *AmoRepository) CreateAccount(ctx context.Context, accountID string, userInfo model.User) error { return nil } From 670d4d88a630e8c40575314ecdd714fbf76fbd89 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 20:18:52 +0300 Subject: [PATCH 010/187] gen with sqlc --- dal/sqlcgen/models.go | 99 +++++++++++-- dal/sqlcgen/queries.sql.go | 281 ++++++++++++++++++++++++++++++++++++- 2 files changed, 362 insertions(+), 18 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 75ae972..30fc25d 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -6,8 +6,11 @@ package sqlcgen import ( "database/sql" + "encoding/json" + "time" "github.com/google/uuid" + "github.com/sqlc-dev/pqtype" ) type Account struct { @@ -38,6 +41,28 @@ type Answer struct { Start bool `db:"start" json:"start"` } +type Field struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Code string `db:"code" json:"code"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Entity interface{} `db:"entity" json:"entity"` + Type string `db:"type" json:"type"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +type Pipeline struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Isarchive sql.NullBool `db:"isarchive" json:"isarchive"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + type Privilege struct { ID int32 `db:"id" json:"id"` Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` @@ -48,19 +73,21 @@ type Privilege struct { } type Question struct { - ID int64 `db:"id" json:"id"` - QuizID int64 `db:"quiz_id" json:"quiz_id"` - Title string `db:"title" json:"title"` - Description sql.NullString `db:"description" json:"description"` - Questiontype interface{} `db:"questiontype" json:"questiontype"` - Required sql.NullBool `db:"required" json:"required"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` - Page sql.NullInt16 `db:"page" json:"page"` - Content sql.NullString `db:"content" json:"content"` - Version sql.NullInt16 `db:"version" json:"version"` - ParentIds []int32 `db:"parent_ids" json:"parent_ids"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"` + ID int64 `db:"id" json:"id"` + QuizID int64 `db:"quiz_id" json:"quiz_id"` + Title string `db:"title" json:"title"` + Description sql.NullString `db:"description" json:"description"` + Questiontype interface{} `db:"questiontype" json:"questiontype"` + Required sql.NullBool `db:"required" json:"required"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Page sql.NullInt16 `db:"page" json:"page"` + Content sql.NullString `db:"content" json:"content"` + Version sql.NullInt16 `db:"version" json:"version"` + ParentIds []int32 `db:"parent_ids" json:"parent_ids"` + CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"` + Utm json.RawMessage `db:"utm" json:"utm"` + Rules json.RawMessage `db:"rules" json:"rules"` } type Quiz struct { @@ -94,3 +121,49 @@ type Quiz struct { AverageTimePassing sql.NullInt32 `db:"average_time_passing" json:"average_time_passing"` SessionsCount sql.NullInt32 `db:"sessions_count" json:"sessions_count"` } + +type Step struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +type Tag struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Accountid int32 `db:"accountid" json:"accountid"` + Entity interface{} `db:"entity" json:"entity"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +type Token struct { + Accountid string `db:"accountid" json:"accountid"` + Refreshtoken string `db:"refreshtoken" json:"refreshtoken"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` + Authcode string `db:"authcode" json:"authcode"` + Expiration time.Time `db:"expiration" json:"expiration"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +type User struct { + ID int64 `db:"id" json:"id"` + Accountid string `db:"accountid" json:"accountid"` + Amoid int32 `db:"amoid" json:"amoid"` + Name string `db:"name" json:"name"` + Email string `db:"email" json:"email"` + Role string `db:"role" json:"role"` + Group pqtype.NullRawMessage `db:"Group" json:"Group"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + Subdomain string `db:"subdomain" json:"subdomain"` + Amouserid int32 `db:"amouserid" json:"amouserid"` + Country string `db:"country" json:"country"` +} diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index e1a5951..bcf020e 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -12,6 +12,7 @@ import ( "github.com/google/uuid" "github.com/lib/pq" + "github.com/sqlc-dev/pqtype" ) const accountPagination = `-- name: AccountPagination :many @@ -131,6 +132,40 @@ func (q *Queries) CheckAndAddDefault(ctx context.Context, arg CheckAndAddDefault return err } +const checkExpired = `-- name: CheckExpired :many +SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokens WHERE Expiration <= EXTRACT(EPOCH FROM NOW()) + (10 * 60) +` + +func (q *Queries) CheckExpired(ctx context.Context) ([]Token, error) { + rows, err := q.db.QueryContext(ctx, checkExpired) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Token + for rows.Next() { + var i Token + if err := rows.Scan( + &i.Accountid, + &i.Refreshtoken, + &i.Accesstoken, + &i.Authcode, + &i.Expiration, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const checkResultOwner = `-- name: CheckResultOwner :one SELECT q.accountid FROM answer a JOIN quiz q ON a.quiz_id = q.id WHERE a.id = $1 AND a.deleted = FALSE AND a.start = false ` @@ -177,6 +212,29 @@ func (q *Queries) CheckResultsOwner(ctx context.Context, arg CheckResultsOwnerPa return items, nil } +const checkUsers = `-- name: CheckUsers :exec +UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5 +` + +type CheckUsersParams struct { + Name string `db:"name" json:"name"` + Group pqtype.NullRawMessage `db:"Group" json:"Group"` + Email string `db:"email" json:"email"` + Role string `db:"role" json:"role"` + Amoid int32 `db:"amoid" json:"amoid"` +} + +func (q *Queries) CheckUsers(ctx context.Context, arg CheckUsersParams) error { + _, err := q.db.ExecContext(ctx, checkUsers, + arg.Name, + arg.Group, + arg.Email, + arg.Role, + arg.Amoid, + ) + return err +} + const copyQuestion = `-- name: CopyQuestion :one INSERT INTO question( quiz_id, title, description, questiontype, required, @@ -312,6 +370,70 @@ func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) er return err } +const createAmoAccount = `-- name: CreateAmoAccount :exec + +INSERT INTO users (AccountID, AmoID, Name, Email, Role, "Group", Deleted, CreatedAt, Subdomain, AmoUserID, Country) +VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) +` + +type CreateAmoAccountParams struct { + Accountid string `db:"accountid" json:"accountid"` + Amoid int32 `db:"amoid" json:"amoid"` + Name string `db:"name" json:"name"` + Email string `db:"email" json:"email"` + Role string `db:"role" json:"role"` + Group pqtype.NullRawMessage `db:"Group" json:"Group"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + Subdomain string `db:"subdomain" json:"subdomain"` + Amouserid int32 `db:"amouserid" json:"amouserid"` + Country string `db:"country" json:"country"` +} + +// amo methods: +func (q *Queries) CreateAmoAccount(ctx context.Context, arg CreateAmoAccountParams) error { + _, err := q.db.ExecContext(ctx, createAmoAccount, + arg.Accountid, + arg.Amoid, + arg.Name, + arg.Email, + arg.Role, + arg.Group, + arg.Deleted, + arg.Createdat, + arg.Subdomain, + arg.Amouserid, + arg.Country, + ) + return err +} + +const createWebHook = `-- name: CreateWebHook :exec +INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, CreatedAt) +VALUES ($1, $2, $3, $4, $5, $6) +` + +type CreateWebHookParams struct { + Accountid string `db:"accountid" json:"accountid"` + Refreshtoken string `db:"refreshtoken" json:"refreshtoken"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` + Authcode string `db:"authcode" json:"authcode"` + Expiration time.Time `db:"expiration" json:"expiration"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +func (q *Queries) CreateWebHook(ctx context.Context, arg CreateWebHookParams) error { + _, err := q.db.ExecContext(ctx, createWebHook, + arg.Accountid, + arg.Refreshtoken, + arg.Accesstoken, + arg.Authcode, + arg.Expiration, + arg.Createdat, + ) + return err +} + const deleteAccountById = `-- name: DeleteAccountById :exec DELETE FROM account WHERE id = $1 ` @@ -340,7 +462,7 @@ func (q *Queries) DeletePrivilegeByID(ctx context.Context, id int32) error { } const deleteQuestion = `-- name: DeleteQuestion :one -UPDATE question SET deleted=true WHERE id=$1 RETURNING question.id, question.quiz_id, question.title, question.description, question.questiontype, question.required, question.deleted, question.page, question.content, question.version, question.parent_ids, question.created_at, question.updated_at +UPDATE question SET deleted=true WHERE id=$1 RETURNING question.id, question.quiz_id, question.title, question.description, question.questiontype, question.required, question.deleted, question.page, question.content, question.version, question.parent_ids, question.created_at, question.updated_at, question.utm, question.rules ` func (q *Queries) DeleteQuestion(ctx context.Context, id int64) (Question, error) { @@ -360,6 +482,8 @@ func (q *Queries) DeleteQuestion(ctx context.Context, id int64) (Question, error pq.Array(&i.ParentIds), &i.CreatedAt, &i.UpdatedAt, + &i.Utm, + &i.Rules, ) return i, err } @@ -857,6 +981,49 @@ func (q *Queries) GetAllAnswersByQuizID(ctx context.Context, session sql.NullStr return items, nil } +const getAllTokens = `-- name: GetAllTokens :many +SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokens +` + +func (q *Queries) GetAllTokens(ctx context.Context) ([]Token, error) { + rows, err := q.db.QueryContext(ctx, getAllTokens) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Token + for rows.Next() { + var i Token + if err := rows.Scan( + &i.Accountid, + &i.Refreshtoken, + &i.Accesstoken, + &i.Authcode, + &i.Expiration, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getCurrentAccount = `-- name: GetCurrentAccount :exec +SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country FROM users WHERE AccountID = $1 +` + +func (q *Queries) GetCurrentAccount(ctx context.Context, accountid string) error { + _, err := q.db.ExecContext(ctx, getCurrentAccount, accountid) + return err +} + const getExpiredPrivilege = `-- name: GetExpiredPrivilege :many SELECT id, privilegeID, privilege_name, amount, created_at FROM privileges @@ -1053,7 +1220,7 @@ func (q *Queries) GetQidOwner(ctx context.Context, qid uuid.NullUUID) (string, e } const getQuestionHistory = `-- name: GetQuestionHistory :many -SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at FROM question WHERE question.id = $1 OR question.id = ANY( +SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at, utm, rules FROM question WHERE question.id = $1 OR question.id = ANY( SELECT unnest(parent_ids) FROM question WHERE id = $1 ) ORDER BY question.id DESC LIMIT $2 OFFSET $3 ` @@ -1087,6 +1254,8 @@ func (q *Queries) GetQuestionHistory(ctx context.Context, arg GetQuestionHistory pq.Array(&i.ParentIds), &i.CreatedAt, &i.UpdatedAt, + &i.Utm, + &i.Rules, ); err != nil { return nil, err } @@ -1122,15 +1291,31 @@ const getQuestions = `-- name: GetQuestions :many SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at FROM question WHERE quiz_id = $1 AND deleted = FALSE ORDER BY page ASC ` -func (q *Queries) GetQuestions(ctx context.Context, quizID int64) ([]Question, error) { +type GetQuestionsRow struct { + ID int64 `db:"id" json:"id"` + QuizID int64 `db:"quiz_id" json:"quiz_id"` + Title string `db:"title" json:"title"` + Description sql.NullString `db:"description" json:"description"` + Questiontype interface{} `db:"questiontype" json:"questiontype"` + Required sql.NullBool `db:"required" json:"required"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Page sql.NullInt16 `db:"page" json:"page"` + Content sql.NullString `db:"content" json:"content"` + Version sql.NullInt16 `db:"version" json:"version"` + ParentIds []int32 `db:"parent_ids" json:"parent_ids"` + CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"` +} + +func (q *Queries) GetQuestions(ctx context.Context, quizID int64) ([]GetQuestionsRow, error) { rows, err := q.db.QueryContext(ctx, getQuestions, quizID) if err != nil { return nil, err } defer rows.Close() - var items []Question + var items []GetQuestionsRow for rows.Next() { - var i Question + var i GetQuestionsRow if err := rows.Scan( &i.ID, &i.QuizID, @@ -1400,6 +1585,51 @@ func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAn return items, nil } +const getUsersWithPagination = `-- name: GetUsersWithPagination :many +SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country FROM users ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +` + +type GetUsersWithPaginationParams struct { + Column1 interface{} `db:"column_1" json:"column_1"` + Limit int32 `db:"limit" json:"limit"` +} + +func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPaginationParams) ([]User, error) { + rows, err := q.db.QueryContext(ctx, getUsersWithPagination, arg.Column1, arg.Limit) + if err != nil { + return nil, err + } + defer rows.Close() + var items []User + for rows.Next() { + var i User + if err := rows.Scan( + &i.ID, + &i.Accountid, + &i.Amoid, + &i.Name, + &i.Email, + &i.Role, + &i.Group, + &i.Deleted, + &i.Createdat, + &i.Subdomain, + &i.Amouserid, + &i.Country, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const insertAnswers = `-- name: InsertAnswers :exec INSERT INTO answer( content, @@ -1851,6 +2081,15 @@ func (q *Queries) QuizCopyQid(ctx context.Context, arg QuizCopyQidParams) (QuizC return i, err } +const softDeleteAccount = `-- name: SoftDeleteAccount :exec +UPDATE users SET Deleted = TRUE WHERE AccountID = $1 +` + +func (q *Queries) SoftDeleteAccount(ctx context.Context, accountid string) error { + _, err := q.db.ExecContext(ctx, softDeleteAccount, accountid) + return err +} + const softDeleteResultByID = `-- name: SoftDeleteResultByID :exec UPDATE answer SET deleted = TRUE WHERE id = $1 AND deleted = FALSE ` @@ -1895,6 +2134,38 @@ func (q *Queries) UpdatePrivilegeAmount(ctx context.Context, arg UpdatePrivilege return err } +const webhookDelete = `-- name: WebhookDelete :exec +DELETE FROM tokens WHERE AccountID = $1 +` + +func (q *Queries) WebhookDelete(ctx context.Context, accountid string) error { + _, err := q.db.ExecContext(ctx, webhookDelete, accountid) + return err +} + +const webhookUpdate = `-- name: WebhookUpdate :exec +UPDATE tokens SET AccessToken = $1, RefreshToken = $2, Expiration = $3, CreatedAt = $4 WHERE AccountID = $5 +` + +type WebhookUpdateParams struct { + Accesstoken string `db:"accesstoken" json:"accesstoken"` + Refreshtoken string `db:"refreshtoken" json:"refreshtoken"` + Expiration time.Time `db:"expiration" json:"expiration"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + Accountid string `db:"accountid" json:"accountid"` +} + +func (q *Queries) WebhookUpdate(ctx context.Context, arg WebhookUpdateParams) error { + _, err := q.db.ExecContext(ctx, webhookUpdate, + arg.Accesstoken, + arg.Refreshtoken, + arg.Expiration, + arg.Createdat, + arg.Accountid, + ) + return err +} + const workerStatProcess = `-- name: WorkerStatProcess :exec WITH answer_aggregates AS ( SELECT From 286f65ee38627f1bd443d0846f2daed3db0e6db5 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 20:42:13 +0300 Subject: [PATCH 011/187] some update pagination query --- dal/db_query/queries.sql | 2 +- go.mod | 10 ++++++---- go.sum | 9 +++++++++ model/amo.go | 6 +++--- repository/amo/amo.go | 42 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 30b8cb3..d7d6a57 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -682,4 +682,4 @@ SELECT * FROM users WHERE AccountID = $1; UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5; -- name: GetUsersWithPagination :many -SELECT * FROM users ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; \ No newline at end of file +SELECT *, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; diff --git a/go.mod b/go.mod index 2f74b5c..9e74c54 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/google/uuid v1.6.0 github.com/lib/pq v1.10.9 github.com/rs/xid v1.5.0 - google.golang.org/protobuf v1.32.0 + google.golang.org/protobuf v1.33.0 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240202120244-c4ef330cfe5d penahub.gitlab.yandexcloud.net/backend/quiz/core.git v0.0.0-20240219174804-d78fd38511af ) @@ -18,7 +18,7 @@ require ( require ( github.com/andybalholm/brotli v1.0.5 // indirect github.com/dustin/go-humanize v1.0.1 // indirect - github.com/google/go-cmp v0.5.9 // indirect + github.com/google/go-cmp v0.6.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/json-iterator/go v1.1.12 // indirect @@ -33,11 +33,13 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/rivo/uniseg v0.2.0 // indirect + github.com/sqlc-dev/pqtype v0.3.0 // indirect + github.com/sqlc-dev/sqlc v1.26.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.51.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect - go.uber.org/atomic v1.7.0 // indirect - golang.org/x/crypto v0.19.0 // indirect + go.uber.org/atomic v1.11.0 // indirect + golang.org/x/crypto v0.20.0 // indirect golang.org/x/net v0.21.0 // indirect golang.org/x/sys v0.17.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/go.sum b/go.sum index 12c0db5..51dfdee 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,7 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -86,6 +87,10 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/sqlc-dev/pqtype v0.3.0 h1:b09TewZ3cSnO5+M1Kqq05y0+OjqIptxELaSayg7bmqk= +github.com/sqlc-dev/pqtype v0.3.0/go.mod h1:oyUjp5981ctiL9UYvj1bVvCKi8OXkCa0u645hce7CAs= +github.com/sqlc-dev/sqlc v1.26.0 h1:bW6TA1vVdi2lfqsEddN5tSznRMYcWez7hf+AOqSiEp8= +github.com/sqlc-dev/sqlc v1.26.0/go.mod h1:k2F3RWilLCup3D0XufrzZENCyXjtplALmHDmOt4v5bs= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= @@ -98,8 +103,11 @@ github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVS github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg= +golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ= golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU= golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= @@ -122,6 +130,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/model/amo.go b/model/amo.go index 9d315ab..478f6db 100644 --- a/model/amo.go +++ b/model/amo.go @@ -2,11 +2,11 @@ package model type User struct { /* - айдишник в нашей системе Primary Key*/ - ID int `json:"ID"` + ID int64 `json:"ID"` /* - id пользователя из токена в нашей системе*/ Accountid string `json:"AccountID"` /* - айдишник пользователя в амо*/ - AmoID int `json:"AmocrmID"` + AmoID int32 `json:"AmocrmID"` /* - имя аккаунта в амо*/ Name string `json:"Name"` /* - почта пользователя из амо*/ @@ -22,7 +22,7 @@ type User struct { /* - поддомен организации в амо*/ Subdomain string `json:"Subdomain"` /* - айдишник пользвателя, который подключал интеграцию*/ - Amouserid int `json:"AmoUserID"` + Amouserid int32 `json:"AmoUserID"` /* - страна указанная в настройках амо*/ Country string `json:"Country"` } diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 581b21c..6282ff6 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -3,6 +3,7 @@ package amo import ( "context" "database/sql" + "encoding/json" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" ) @@ -33,8 +34,45 @@ func (r *AmoRepository) UpdateListUsers(ctx context.Context) error { } -func (r *AmoRepository) GettingUserFromCash(ctx context.Context, req *model.PaginationReq) (*model.UserListResp, error) { - return nil, nil +func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListResp, error) { + rows, err := r.queries.GetUsersWithPagination(ctx, sqlcgen.GetUsersWithPaginationParams{ + Column1: req.Page, + Limit: int32(req.Size), + }) + + 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, + Role: row.Role, + 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 + + users = append(users, user) + } + + resp := model.UserListResp{} + + return users, nil } func (r *AmoRepository) SoftDeleteAccount(ctx context.Context, accountID string) error { From 24b7942061221eb1c5426aacf116ee0cb23779c0 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 20:43:28 +0300 Subject: [PATCH 012/187] gen with sqlc --- dal/sqlcgen/queries.sql.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index bcf020e..e4be502 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1586,7 +1586,7 @@ func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAn } const getUsersWithPagination = `-- name: GetUsersWithPagination :many -SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country FROM users ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 ` type GetUsersWithPaginationParams struct { @@ -1594,15 +1594,31 @@ type GetUsersWithPaginationParams struct { Limit int32 `db:"limit" json:"limit"` } -func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPaginationParams) ([]User, error) { +type GetUsersWithPaginationRow struct { + ID int64 `db:"id" json:"id"` + Accountid string `db:"accountid" json:"accountid"` + Amoid int32 `db:"amoid" json:"amoid"` + Name string `db:"name" json:"name"` + Email string `db:"email" json:"email"` + Role string `db:"role" json:"role"` + Group pqtype.NullRawMessage `db:"Group" json:"Group"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + Subdomain string `db:"subdomain" json:"subdomain"` + Amouserid int32 `db:"amouserid" json:"amouserid"` + Country string `db:"country" json:"country"` + TotalCount int64 `db:"total_count" json:"total_count"` +} + +func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPaginationParams) ([]GetUsersWithPaginationRow, error) { rows, err := q.db.QueryContext(ctx, getUsersWithPagination, arg.Column1, arg.Limit) if err != nil { return nil, err } defer rows.Close() - var items []User + var items []GetUsersWithPaginationRow for rows.Next() { - var i User + var i GetUsersWithPaginationRow if err := rows.Scan( &i.ID, &i.Accountid, @@ -1616,6 +1632,7 @@ func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPa &i.Subdomain, &i.Amouserid, &i.Country, + &i.TotalCount, ); err != nil { return nil, err } From b36797b22dae3287b73e729efd39529eb759b31c Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 21:09:09 +0300 Subject: [PATCH 013/187] add some methods repo in amo repo --- dal/db_query/queries.sql | 2 +- model/amoResp.go | 2 +- repository/amo/amo.go | 127 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 122 insertions(+), 9 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index d7d6a57..979c351 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -675,7 +675,7 @@ DELETE FROM tokens WHERE AccountID = $1; -- name: SoftDeleteAccount :exec UPDATE users SET Deleted = TRUE WHERE AccountID = $1; --- name: GetCurrentAccount :exec +-- name: GetCurrentAccount :one SELECT * FROM users WHERE AccountID = $1; -- name: CheckUsers :exec diff --git a/model/amoResp.go b/model/amoResp.go index bcf81b2..fe9253e 100644 --- a/model/amoResp.go +++ b/model/amoResp.go @@ -52,7 +52,7 @@ type UserListPipelinesResp struct { type UserListResp struct { /* - общее количество юзеров, которые у нас закешированы для этого пользователя*/ - Count int `json:"count"` + Count int64 `json:"count"` /* - список юзеров, которые были закешированы нашим сервисом*/ Items []User `json:"items"` } diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 6282ff6..c80f8d7 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -4,8 +4,10 @@ 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" + "time" ) type AmoRepository struct { @@ -43,7 +45,7 @@ func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *mode if err != nil { return nil, err } - + var count int64 var users []model.User for _, row := range rows { user := model.User{ @@ -58,6 +60,7 @@ func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *mode Amouserid: row.Amouserid, Country: row.Country, } + count = row.TotalCount var group []model.UserGroups if !row.Group.Valid { err := json.Unmarshal(row.Group.RawMessage, &group) @@ -70,24 +73,70 @@ func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *mode users = append(users, user) } - resp := model.UserListResp{} + resp := model.UserListResp{ + Count: count, + Items: users, + } - return users, nil + return &resp, nil } func (r *AmoRepository) SoftDeleteAccount(ctx context.Context, accountID string) error { + err := r.queries.SoftDeleteAccount(ctx, accountID) + if err != nil { + return err + } return nil } func (r *AmoRepository) GetCurrentAccount(ctx context.Context, accountID string) (*model.User, error) { + row, err := r.queries.GetCurrentAccount(ctx, accountID) return nil, 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{ + Accountid: accountID, + Amoid: userInfo.AmoID, + Name: userInfo.Name, + Email: userInfo.Email, + Role: userInfo.Role, + Group: pqtype.NullRawMessage{RawMessage: group, Valid: len(group) > 0}, + Createdat: sql.NullTime{Time: time.Now(), Valid: true}, + Subdomain: userInfo.Subdomain, + Amouserid: userInfo.Amouserid, + Country: userInfo.Country, + }) + + if err != nil { + return err + } + return nil } -func (r *AmoRepository) CheckUsers(ctx context.Context, amouserid int, user model.User) error { +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, + Name: user.Name, + Email: user.Email, + Role: user.Role, + Group: pqtype.NullRawMessage{RawMessage: group, Valid: len(group) > 0}, + }) + + if err != nil { + return err + } return nil } @@ -95,24 +144,88 @@ func (r *AmoRepository) CheckUsers(ctx context.Context, amouserid int, user mode // методы webhook func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) error { + err := r.queries.CreateWebHook(ctx, sqlcgen.CreateWebHookParams{ + Accountid: tokens.AccountID, + Refreshtoken: tokens.RefreshToken, + Accesstoken: tokens.AccessToken, + Authcode: tokens.AuthCode, + Expiration: time.Unix(tokens.Expiration, 0), + Createdat: sql.NullTime{Time: time.Unix(tokens.CreatedAt, 0), Valid: true}, + }) + + if err != nil { + return err + } + return nil } func (r *AmoRepository) WebhookUpdate(ctx context.Context, tokens model.Token) error { + err := r.queries.WebhookUpdate(ctx, sqlcgen.WebhookUpdateParams{ + Accountid: tokens.AccountID, + Accesstoken: tokens.AccessToken, + Refreshtoken: tokens.RefreshToken, + Expiration: time.Unix(tokens.Expiration, 0), + Createdat: sql.NullTime{Time: time.Unix(tokens.CreatedAt, 0), Valid: true}, + }) + + if err != nil { + return err + } + return nil } // воркер запускается каждые 5 минут, поэтомму ищем токены котторые исекают менее чем через 10 минут отдаем их на обноление func (r *AmoRepository) CheckExpired(ctx context.Context) ([]model.Token, error) { - return nil, nil + 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 } func (r *AmoRepository) GetAllTokens(ctx context.Context) ([]model.Token, error) { - return nil, nil + 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 } func (r *AmoRepository) WebhookDelete(ctx context.Context) error { - //TODO:IMPLEMENT ME return nil From 2841043cded76752accdf13e1f9e5a115d8a201c Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 21:10:42 +0300 Subject: [PATCH 014/187] gen with sqlc --- dal/sqlcgen/queries.sql.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index e4be502..1b860c2 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1015,13 +1015,28 @@ func (q *Queries) GetAllTokens(ctx context.Context) ([]Token, error) { return items, nil } -const getCurrentAccount = `-- name: GetCurrentAccount :exec +const getCurrentAccount = `-- name: GetCurrentAccount :one SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country FROM users WHERE AccountID = $1 ` -func (q *Queries) GetCurrentAccount(ctx context.Context, accountid string) error { - _, err := q.db.ExecContext(ctx, getCurrentAccount, accountid) - return err +func (q *Queries) GetCurrentAccount(ctx context.Context, accountid string) (User, error) { + row := q.db.QueryRowContext(ctx, getCurrentAccount, accountid) + var i User + err := row.Scan( + &i.ID, + &i.Accountid, + &i.Amoid, + &i.Name, + &i.Email, + &i.Role, + &i.Group, + &i.Deleted, + &i.Createdat, + &i.Subdomain, + &i.Amouserid, + &i.Country, + ) + return i, err } const getExpiredPrivilege = `-- name: GetExpiredPrivilege :many From 3db5dc130046954ad886aeb1c8ab187245b4bd92 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 21:15:20 +0300 Subject: [PATCH 015/187] add method for getting current account --- repository/amo/amo.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index c80f8d7..2986b90 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -91,7 +91,33 @@ func (r *AmoRepository) SoftDeleteAccount(ctx context.Context, accountID string) func (r *AmoRepository) GetCurrentAccount(ctx context.Context, accountID string) (*model.User, error) { row, err := r.queries.GetCurrentAccount(ctx, accountID) - return nil, nil + 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, + 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 { From 44291564e21f4a658e40c7d41df0f07241d18a60 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 17 Apr 2024 21:19:58 +0300 Subject: [PATCH 016/187] change some type --- model/amoResp.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/model/amoResp.go b/model/amoResp.go index fe9253e..9f3c797 100644 --- a/model/amoResp.go +++ b/model/amoResp.go @@ -7,7 +7,7 @@ type ConnectAccountResp struct { type GetCurrentAccountResp struct { /* - айдишник в нашей системе Primary Key*/ - ID int `json:"ID"` + ID int64 `json:"ID"` /* - имя аккаунта в амо*/ Name string `json:"Name"` /* - поддомен организации в амо*/ @@ -15,9 +15,9 @@ type GetCurrentAccountResp struct { /* - id пользователя из токена в нашей системе*/ Accountid string `json:"AccountID"` /* - айдишник пользвателя, который подключал интеграцию*/ - Amouserid int `json:"AmoUserID"` + Amouserid int32 `json:"AmoUserID"` /* - связь с аккаунтом в амо*/ - Amocrmid int `json:"AmocrmID"` + Amocrmid int32 `json:"AmocrmID"` /* - страна указанная в настройках амо*/ Country string `json:"Country"` /* - таймштамп создания аккаунта*/ From ae01145ce6ab758106353cd6e09fa32c00e356a2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 12:16:25 +0300 Subject: [PATCH 017/187] add query check tags --- dal/db_query/queries.sql | 46 ++++++++++++++++++++++++++++++++++- dal/schema/000010_init.up.sql | 4 +-- repository/amo/amo.go | 4 +-- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 979c351..49c1eb7 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -667,7 +667,7 @@ UPDATE tokens SET AccessToken = $1, RefreshToken = $2, Expiration = $3, CreatedA SELECT * FROM tokens; -- name: CheckExpired :many -SELECT * FROM tokens WHERE Expiration <= EXTRACT(EPOCH FROM NOW()) + (10 * 60); +SELECT * FROM tokens WHERE Expiration <= TO_TIMESTAMP(EXTRACT(EPOCH FROM NOW()) + (10 * 60)); -- name: WebhookDelete :exec DELETE FROM tokens WHERE AccountID = $1; @@ -683,3 +683,47 @@ UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5 -- name: GetUsersWithPagination :many SELECT *, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; + +-- name: GetTagsWithPagination :many +SELECT *, COUNT(*) OVER() as total_count FROM tags WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; + +-- name: GetStepsWithPagination :many +SELECT *, COUNT(*) OVER() as total_count FROM steps WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; + +-- name: GetPipelinesWithPagination :many +SELECT *, COUNT(*) OVER() as total_count FROM pipelines WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; + +-- name: GetFieldsWithPagination :many +SELECT *, COUNT(*) OVER() as total_count FROM fields WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; + +-- name: CheckTags :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE AccountID = $1 -- параметр AccountID из токена для получения id аккаунта в амо +), updated_tags AS ( + UPDATE tags + SET name = new_tags.name, color = new_tags.color, createdAt = CURRENT_TIMESTAMP + FROM ( + SELECT t.ID, t.Name, t.Color, t.AmoID + FROM UNNEST($3::entitytype[], $4::varchar(50)[], $5::varchar(50)[]) AS t(ID INT, Name VARCHAR(50), Color VARCHAR(50), AmoID INT) + ) AS new_tags + JOIN user_data ON tags.AccountID = user_data.AmoID + WHERE tags.amoID = $2 + AND tags.amoID = new_tags.amoID + AND tags.accountID = user_data.AmoID -- Используем полученный AmoID из таблицы users + AND tags.entity = $3 + AND tags.deleted = FALSE + RETURNING tags.id, tags.amoID +), inserted_tags AS ( + INSERT INTO tags (amoID, accountID, entity, name, color, createdAt) + SELECT $2, user_data.AmoID, $3, t.Name, t.Color, CURRENT_TIMESTAMP + FROM UNNEST($3::entitytype[], $4::varchar(50)[], $5::varchar(50)[]) AS t(Name VARCHAR(50), Color VARCHAR(50)) + JOIN user_data ON $1 = user_data.AmoID + WHERE (t.Name, t.Color) NOT IN (SELECT name, color FROM updated_tags) + RETURNING id, amoID +) +SELECT * FROM updated_tags +UNION ALL +SELECT * FROM inserted_tags; + diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 9cf27bd..07945b8 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -73,7 +73,7 @@ CREATE TABLE IF NOT EXISTS tags ( CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -ALTER TABLE question +ALTER TABLE quiz ADD COLUMN utm jsonb NOT NULL DEFAULT '{}'; -ALTER TABLE question +ALTER TABLE quiz ADD COLUMN rules jsonb NOT NULL DEFAULT '{}'; \ No newline at end of file diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 2986b90..4564d5d 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -175,7 +175,7 @@ func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) e Refreshtoken: tokens.RefreshToken, Accesstoken: tokens.AccessToken, Authcode: tokens.AuthCode, - Expiration: time.Unix(tokens.Expiration, 0), + Expiration: time.Unix(tokens.Expiration, 0).In(time.UTC), Createdat: sql.NullTime{Time: time.Unix(tokens.CreatedAt, 0), Valid: true}, }) @@ -191,7 +191,7 @@ func (r *AmoRepository) WebhookUpdate(ctx context.Context, tokens model.Token) e Accountid: tokens.AccountID, Accesstoken: tokens.AccessToken, Refreshtoken: tokens.RefreshToken, - Expiration: time.Unix(tokens.Expiration, 0), + Expiration: time.Unix(tokens.Expiration, 0).In(time.UTC), Createdat: sql.NullTime{Time: time.Unix(tokens.CreatedAt, 0), Valid: true}, }) From 42ba11f5a0d3281fca88d5dbbc9199e1a8d878d4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 13:10:59 +0300 Subject: [PATCH 018/187] fix sql --- dal/db_query/queries.sql | 41 +++++++++++++-------------------- dal/schema/000010_init.down.sql | 5 ++++ dal/schema/000010_init.up.sql | 19 +++++++++------ 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 49c1eb7..1204a00 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -700,30 +700,21 @@ SELECT *, COUNT(*) OVER() as total_count FROM fields WHERE Deleted = false ORDE WITH user_data AS ( SELECT AmoID FROM users - WHERE AccountID = $1 -- параметр AccountID из токена для получения id аккаунта в амо -), updated_tags AS ( - UPDATE tags - SET name = new_tags.name, color = new_tags.color, createdAt = CURRENT_TIMESTAMP - FROM ( - SELECT t.ID, t.Name, t.Color, t.AmoID - FROM UNNEST($3::entitytype[], $4::varchar(50)[], $5::varchar(50)[]) AS t(ID INT, Name VARCHAR(50), Color VARCHAR(50), AmoID INT) - ) AS new_tags - JOIN user_data ON tags.AccountID = user_data.AmoID - WHERE tags.amoID = $2 - AND tags.amoID = new_tags.amoID - AND tags.accountID = user_data.AmoID -- Используем полученный AmoID из таблицы users - AND tags.entity = $3 - AND tags.deleted = FALSE - RETURNING tags.id, tags.amoID -), inserted_tags AS ( - INSERT INTO tags (amoID, accountID, entity, name, color, createdAt) - SELECT $2, user_data.AmoID, $3, t.Name, t.Color, CURRENT_TIMESTAMP - FROM UNNEST($3::entitytype[], $4::varchar(50)[], $5::varchar(50)[]) AS t(Name VARCHAR(50), Color VARCHAR(50)) - JOIN user_data ON $1 = user_data.AmoID - WHERE (t.Name, t.Color) NOT IN (SELECT name, color FROM updated_tags) - RETURNING id, amoID + WHERE AccountID = $1 ) -SELECT * FROM updated_tags -UNION ALL -SELECT * FROM inserted_tags; +INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) +SELECT (new_tags->>'Amoid')::INT, + user_data.AmoID, + CAST(new_tags->>'Entity' AS entitytype), + new_tags->>'Name', + new_tags->>'Color', + CURRENT_TIMESTAMP +FROM json_array_elements($2::json[]) AS new_tags + JOIN user_data ON true +ON CONFLICT (amoID, accountID, entity) DO UPDATE + SET name = EXCLUDED.name, + color = EXCLUDED.color, + createdAt = CURRENT_TIMESTAMP +RETURNING *; + diff --git a/dal/schema/000010_init.down.sql b/dal/schema/000010_init.down.sql index 77d693b..e1e3bd4 100644 --- a/dal/schema/000010_init.down.sql +++ b/dal/schema/000010_init.down.sql @@ -1,3 +1,8 @@ +ALTER TABLE pipelines DROP CONSTRAINT IF EXISTS unique_pipeline; +ALTER TABLE steps DROP CONSTRAINT IF EXISTS unique_step; +ALTER TABLE fields DROP CONSTRAINT IF EXISTS unique_field; +ALTER TABLE tags DROP CONSTRAINT IF EXISTS unique_tag; + ALTER TABLE question DROP COLUMN IF EXISTS utm, DROP COLUMN IF EXISTS rules; diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 07945b8..067b384 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS users ( Email VARCHAR(50) NOT NULL DEFAULT '', -- почта в амо Role VARCHAR(50) NOT NULL DEFAULT '', -- роль в амо "Group" JSONB, -- вложенная структура так как в амо группы хранятся массивом структур - Deleted BOOLEAN, + Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, Subdomain VARCHAR(50) NOT NULL DEFAULT '', AmoUserID INT NOT NULL , -- id пользователя который подключал интеграцию @@ -27,8 +27,8 @@ CREATE TABLE IF NOT EXISTS pipelines ( AmoID INT NOT NULL , --id воронки в амо AccountID INT NOT NULL , --id аккаунта в амо связь с таблицей users AmoID неявная посредством join Name VARCHAR(50) NOT NULL DEFAULT '', --название воронки в амо - IsArchive BOOLEAN, --флаг архивной воронки в амо - Deleted BOOLEAN, + IsArchive BOOLEAN NOT NULL DEFAULT FALSE, --флаг архивной воронки в амо + Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); @@ -39,7 +39,7 @@ CREATE TABLE IF NOT EXISTS steps ( AccountID INT NOT NULL, --id аккаунта в амо связь с таблицей users AmoID неявная посредством join Name VARCHAR(50) NOT NULL DEFAULT '', --название воронки в амо Color VARCHAR(50) NOT NULL DEFAULT '', --цвет шага в амо* - Deleted BOOLEAN, + Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); @@ -58,7 +58,7 @@ CREATE TABLE IF NOT EXISTS fields ( Name VARCHAR(50) NOT NULL DEFAULT '', -- название воронки в амо Entity EntityType NOT NULL, -- тип сущности в амо, для которой это кастомное поле Type VARCHAR(50) NOT NULL DEFAULT '', -- тип поля - Deleted BOOLEAN, + Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); @@ -69,11 +69,16 @@ CREATE TABLE IF NOT EXISTS tags ( Entity EntityType NOT NULL, -- сущность, к которой принадлежит этот тег Name VARCHAR(50) NOT NULL DEFAULT '', -- название тега в амо Color VARCHAR(50) NOT NULL DEFAULT '', -- цвет тега в амо - Deleted BOOLEAN, + Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ALTER TABLE quiz ADD COLUMN utm jsonb NOT NULL DEFAULT '{}'; ALTER TABLE quiz -ADD COLUMN rules jsonb NOT NULL DEFAULT '{}'; \ No newline at end of file +ADD COLUMN rules jsonb NOT NULL DEFAULT '{}'; + +ALTER TABLE pipelines ADD CONSTRAINT unique_pipeline UNIQUE (amoID, accountID); +ALTER TABLE steps ADD CONSTRAINT unique_step UNIQUE (amoID, accountID, PipelineID); +ALTER TABLE fields ADD CONSTRAINT unique_field UNIQUE (amoID, accountID, entity); +ALTER TABLE tags ADD CONSTRAINT unique_tag UNIQUE (amoID, accountID, entity); \ No newline at end of file From 8fea5d95c3aa346847956a7da62a9534d12e89e9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 13:16:03 +0300 Subject: [PATCH 019/187] reworked schema and new sqlc generate --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/models.go | 100 +++++------ dal/sqlcgen/queries.sql.go | 330 +++++++++++++++++++++++++++++++++---- 3 files changed, 348 insertions(+), 84 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 1204a00..eab06f4 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -700,7 +700,7 @@ SELECT *, COUNT(*) OVER() as total_count FROM fields WHERE Deleted = false ORDE WITH user_data AS ( SELECT AmoID FROM users - WHERE AccountID = $1 + WHERE users.AccountID = $1 ) INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) SELECT (new_tags->>'Amoid')::INT, diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 30fc25d..f1d891e 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -49,7 +49,7 @@ type Field struct { Name string `db:"name" json:"name"` Entity interface{} `db:"entity" json:"entity"` Type string `db:"type" json:"type"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` + Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` } @@ -58,8 +58,8 @@ type Pipeline struct { Amoid int32 `db:"amoid" json:"amoid"` Accountid int32 `db:"accountid" json:"accountid"` Name string `db:"name" json:"name"` - Isarchive sql.NullBool `db:"isarchive" json:"isarchive"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` + Isarchive bool `db:"isarchive" json:"isarchive"` + Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` } @@ -73,53 +73,53 @@ type Privilege struct { } type Question struct { - ID int64 `db:"id" json:"id"` - QuizID int64 `db:"quiz_id" json:"quiz_id"` - Title string `db:"title" json:"title"` - Description sql.NullString `db:"description" json:"description"` - Questiontype interface{} `db:"questiontype" json:"questiontype"` - Required sql.NullBool `db:"required" json:"required"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` - Page sql.NullInt16 `db:"page" json:"page"` - Content sql.NullString `db:"content" json:"content"` - Version sql.NullInt16 `db:"version" json:"version"` - ParentIds []int32 `db:"parent_ids" json:"parent_ids"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"` - Utm json.RawMessage `db:"utm" json:"utm"` - Rules json.RawMessage `db:"rules" json:"rules"` + ID int64 `db:"id" json:"id"` + QuizID int64 `db:"quiz_id" json:"quiz_id"` + Title string `db:"title" json:"title"` + Description sql.NullString `db:"description" json:"description"` + Questiontype interface{} `db:"questiontype" json:"questiontype"` + Required sql.NullBool `db:"required" json:"required"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Page sql.NullInt16 `db:"page" json:"page"` + Content sql.NullString `db:"content" json:"content"` + Version sql.NullInt16 `db:"version" json:"version"` + ParentIds []int32 `db:"parent_ids" json:"parent_ids"` + CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"` } type Quiz struct { - ID int64 `db:"id" json:"id"` - Qid uuid.NullUUID `db:"qid" json:"qid"` - Accountid string `db:"accountid" json:"accountid"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` - Archived sql.NullBool `db:"archived" json:"archived"` - Fingerprinting sql.NullBool `db:"fingerprinting" json:"fingerprinting"` - Repeatable sql.NullBool `db:"repeatable" json:"repeatable"` - NotePrevented sql.NullBool `db:"note_prevented" json:"note_prevented"` - MailNotifications sql.NullBool `db:"mail_notifications" json:"mail_notifications"` - UniqueAnswers sql.NullBool `db:"unique_answers" json:"unique_answers"` - Super sql.NullBool `db:"super" json:"super"` - GroupID sql.NullInt64 `db:"group_id" json:"group_id"` - Name sql.NullString `db:"name" json:"name"` - Description sql.NullString `db:"description" json:"description"` - Config sql.NullString `db:"config" json:"config"` - Status interface{} `db:"status" json:"status"` - LimitAnswers sql.NullInt32 `db:"limit_answers" json:"limit_answers"` - DueTo sql.NullInt32 `db:"due_to" json:"due_to"` - TimeOfPassing sql.NullInt32 `db:"time_of_passing" json:"time_of_passing"` - Pausable sql.NullBool `db:"pausable" json:"pausable"` - Version sql.NullInt16 `db:"version" json:"version"` - VersionComment sql.NullString `db:"version_comment" json:"version_comment"` - ParentIds []int32 `db:"parent_ids" json:"parent_ids"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"` - QuestionsCount sql.NullInt32 `db:"questions_count" json:"questions_count"` - AnswersCount sql.NullInt32 `db:"answers_count" json:"answers_count"` - AverageTimePassing sql.NullInt32 `db:"average_time_passing" json:"average_time_passing"` - SessionsCount sql.NullInt32 `db:"sessions_count" json:"sessions_count"` + ID int64 `db:"id" json:"id"` + Qid uuid.NullUUID `db:"qid" json:"qid"` + Accountid string `db:"accountid" json:"accountid"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Archived sql.NullBool `db:"archived" json:"archived"` + Fingerprinting sql.NullBool `db:"fingerprinting" json:"fingerprinting"` + Repeatable sql.NullBool `db:"repeatable" json:"repeatable"` + NotePrevented sql.NullBool `db:"note_prevented" json:"note_prevented"` + MailNotifications sql.NullBool `db:"mail_notifications" json:"mail_notifications"` + UniqueAnswers sql.NullBool `db:"unique_answers" json:"unique_answers"` + Super sql.NullBool `db:"super" json:"super"` + GroupID sql.NullInt64 `db:"group_id" json:"group_id"` + Name sql.NullString `db:"name" json:"name"` + Description sql.NullString `db:"description" json:"description"` + Config sql.NullString `db:"config" json:"config"` + Status interface{} `db:"status" json:"status"` + LimitAnswers sql.NullInt32 `db:"limit_answers" json:"limit_answers"` + DueTo sql.NullInt32 `db:"due_to" json:"due_to"` + TimeOfPassing sql.NullInt32 `db:"time_of_passing" json:"time_of_passing"` + Pausable sql.NullBool `db:"pausable" json:"pausable"` + Version sql.NullInt16 `db:"version" json:"version"` + VersionComment sql.NullString `db:"version_comment" json:"version_comment"` + ParentIds []int32 `db:"parent_ids" json:"parent_ids"` + CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"` + QuestionsCount sql.NullInt32 `db:"questions_count" json:"questions_count"` + AnswersCount sql.NullInt32 `db:"answers_count" json:"answers_count"` + AverageTimePassing sql.NullInt32 `db:"average_time_passing" json:"average_time_passing"` + SessionsCount sql.NullInt32 `db:"sessions_count" json:"sessions_count"` + Utm json.RawMessage `db:"utm" json:"utm"` + Rules json.RawMessage `db:"rules" json:"rules"` } type Step struct { @@ -129,7 +129,7 @@ type Step struct { Accountid int32 `db:"accountid" json:"accountid"` Name string `db:"name" json:"name"` Color string `db:"color" json:"color"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` + Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` } @@ -140,7 +140,7 @@ type Tag struct { Entity interface{} `db:"entity" json:"entity"` Name string `db:"name" json:"name"` Color string `db:"color" json:"color"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` + Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` } @@ -161,7 +161,7 @@ type User struct { Email string `db:"email" json:"email"` Role string `db:"role" json:"role"` Group pqtype.NullRawMessage `db:"Group" json:"Group"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` + Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` Subdomain string `db:"subdomain" json:"subdomain"` Amouserid int32 `db:"amouserid" json:"amouserid"` diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 1b860c2..44ce61f 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -8,6 +8,7 @@ package sqlcgen import ( "context" "database/sql" + "encoding/json" "time" "github.com/google/uuid" @@ -133,7 +134,7 @@ func (q *Queries) CheckAndAddDefault(ctx context.Context, arg CheckAndAddDefault } const checkExpired = `-- name: CheckExpired :many -SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokens WHERE Expiration <= EXTRACT(EPOCH FROM NOW()) + (10 * 60) +SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokens WHERE Expiration <= TO_TIMESTAMP(EXTRACT(EPOCH FROM NOW()) + (10 * 60)) ` func (q *Queries) CheckExpired(ctx context.Context) ([]Token, error) { @@ -212,6 +213,65 @@ func (q *Queries) CheckResultsOwner(ctx context.Context, arg CheckResultsOwnerPa return items, nil } +const checkTags = `-- name: CheckTags :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +) +INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) +SELECT (new_tags->>'Amoid')::INT, + user_data.AmoID, + CAST(new_tags->>'Entity' AS entitytype), + new_tags->>'Name', + new_tags->>'Color', + CURRENT_TIMESTAMP +FROM json_array_elements($2::json[]) AS new_tags + JOIN user_data ON true +ON CONFLICT (amoID, accountID, entity) DO UPDATE + SET name = EXCLUDED.name, + color = EXCLUDED.color, + createdAt = CURRENT_TIMESTAMP +RETURNING id, amoid, accountid, entity, name, color, deleted, createdat +` + +type CheckTagsParams struct { + Accountid string `db:"accountid" json:"accountid"` + Column2 []json.RawMessage `db:"column_2" json:"column_2"` +} + +func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]Tag, error) { + rows, err := q.db.QueryContext(ctx, checkTags, arg.Accountid, pq.Array(arg.Column2)) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Tag + for rows.Next() { + var i Tag + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Accountid, + &i.Entity, + &i.Name, + &i.Color, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const checkUsers = `-- name: CheckUsers :exec UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5 ` @@ -383,7 +443,7 @@ type CreateAmoAccountParams struct { Email string `db:"email" json:"email"` Role string `db:"role" json:"role"` Group pqtype.NullRawMessage `db:"Group" json:"Group"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` + Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` Subdomain string `db:"subdomain" json:"subdomain"` Amouserid int32 `db:"amouserid" json:"amouserid"` @@ -462,7 +522,7 @@ func (q *Queries) DeletePrivilegeByID(ctx context.Context, id int32) error { } const deleteQuestion = `-- name: DeleteQuestion :one -UPDATE question SET deleted=true WHERE id=$1 RETURNING question.id, question.quiz_id, question.title, question.description, question.questiontype, question.required, question.deleted, question.page, question.content, question.version, question.parent_ids, question.created_at, question.updated_at, question.utm, question.rules +UPDATE question SET deleted=true WHERE id=$1 RETURNING question.id, question.quiz_id, question.title, question.description, question.questiontype, question.required, question.deleted, question.page, question.content, question.version, question.parent_ids, question.created_at, question.updated_at ` func (q *Queries) DeleteQuestion(ctx context.Context, id int64) (Question, error) { @@ -482,14 +542,12 @@ func (q *Queries) DeleteQuestion(ctx context.Context, id int64) (Question, error pq.Array(&i.ParentIds), &i.CreatedAt, &i.UpdatedAt, - &i.Utm, - &i.Rules, ) return i, err } const deleteQuizByID = `-- name: DeleteQuizByID :one -UPDATE quiz SET deleted=true WHERE quiz.id=$1 AND accountid=$2 RETURNING quiz.id, quiz.qid, quiz.accountid, quiz.deleted, quiz.archived, quiz.fingerprinting, quiz.repeatable, quiz.note_prevented, quiz.mail_notifications, quiz.unique_answers, quiz.super, quiz.group_id, quiz.name, quiz.description, quiz.config, quiz.status, quiz.limit_answers, quiz.due_to, quiz.time_of_passing, quiz.pausable, quiz.version, quiz.version_comment, quiz.parent_ids, quiz.created_at, quiz.updated_at, quiz.questions_count, quiz.answers_count, quiz.average_time_passing, quiz.sessions_count +UPDATE quiz SET deleted=true WHERE quiz.id=$1 AND accountid=$2 RETURNING quiz.id, quiz.qid, quiz.accountid, quiz.deleted, quiz.archived, quiz.fingerprinting, quiz.repeatable, quiz.note_prevented, quiz.mail_notifications, quiz.unique_answers, quiz.super, quiz.group_id, quiz.name, quiz.description, quiz.config, quiz.status, quiz.limit_answers, quiz.due_to, quiz.time_of_passing, quiz.pausable, quiz.version, quiz.version_comment, quiz.parent_ids, quiz.created_at, quiz.updated_at, quiz.questions_count, quiz.answers_count, quiz.average_time_passing, quiz.sessions_count, quiz.utm, quiz.rules ` type DeleteQuizByIDParams struct { @@ -530,6 +588,8 @@ func (q *Queries) DeleteQuizByID(ctx context.Context, arg DeleteQuizByIDParams) &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, + &i.Utm, + &i.Rules, ) return i, err } @@ -1083,6 +1143,114 @@ func (q *Queries) GetExpiredPrivilege(ctx context.Context, privilegeid sql.NullS return items, nil } +const getFieldsWithPagination = `-- name: GetFieldsWithPagination :many +SELECT id, amoid, code, accountid, name, entity, type, deleted, createdat, COUNT(*) OVER() as total_count FROM fields WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +` + +type GetFieldsWithPaginationParams struct { + Column1 interface{} `db:"column_1" json:"column_1"` + Limit int32 `db:"limit" json:"limit"` +} + +type GetFieldsWithPaginationRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Code string `db:"code" json:"code"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Entity interface{} `db:"entity" json:"entity"` + Type string `db:"type" json:"type"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + TotalCount int64 `db:"total_count" json:"total_count"` +} + +func (q *Queries) GetFieldsWithPagination(ctx context.Context, arg GetFieldsWithPaginationParams) ([]GetFieldsWithPaginationRow, error) { + rows, err := q.db.QueryContext(ctx, getFieldsWithPagination, arg.Column1, arg.Limit) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetFieldsWithPaginationRow + for rows.Next() { + var i GetFieldsWithPaginationRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Code, + &i.Accountid, + &i.Name, + &i.Entity, + &i.Type, + &i.Deleted, + &i.Createdat, + &i.TotalCount, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getPipelinesWithPagination = `-- name: GetPipelinesWithPagination :many +SELECT id, amoid, accountid, name, isarchive, deleted, createdat, COUNT(*) OVER() as total_count FROM pipelines WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +` + +type GetPipelinesWithPaginationParams struct { + Column1 interface{} `db:"column_1" json:"column_1"` + Limit int32 `db:"limit" json:"limit"` +} + +type GetPipelinesWithPaginationRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Isarchive bool `db:"isarchive" json:"isarchive"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + TotalCount int64 `db:"total_count" json:"total_count"` +} + +func (q *Queries) GetPipelinesWithPagination(ctx context.Context, arg GetPipelinesWithPaginationParams) ([]GetPipelinesWithPaginationRow, error) { + rows, err := q.db.QueryContext(ctx, getPipelinesWithPagination, arg.Column1, arg.Limit) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetPipelinesWithPaginationRow + for rows.Next() { + var i GetPipelinesWithPaginationRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Accountid, + &i.Name, + &i.Isarchive, + &i.Deleted, + &i.Createdat, + &i.TotalCount, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getPrivilegesByAccountID = `-- name: GetPrivilegesByAccountID :many SELECT id,privilegeID,privilege_name,amount, created_at FROM privileges WHERE account_id = $1 ` @@ -1235,7 +1403,7 @@ func (q *Queries) GetQidOwner(ctx context.Context, qid uuid.NullUUID) (string, e } const getQuestionHistory = `-- name: GetQuestionHistory :many -SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at, utm, rules FROM question WHERE question.id = $1 OR question.id = ANY( +SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at FROM question WHERE question.id = $1 OR question.id = ANY( SELECT unnest(parent_ids) FROM question WHERE id = $1 ) ORDER BY question.id DESC LIMIT $2 OFFSET $3 ` @@ -1269,8 +1437,6 @@ func (q *Queries) GetQuestionHistory(ctx context.Context, arg GetQuestionHistory pq.Array(&i.ParentIds), &i.CreatedAt, &i.UpdatedAt, - &i.Utm, - &i.Rules, ); err != nil { return nil, err } @@ -1306,31 +1472,15 @@ const getQuestions = `-- name: GetQuestions :many SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at FROM question WHERE quiz_id = $1 AND deleted = FALSE ORDER BY page ASC ` -type GetQuestionsRow struct { - ID int64 `db:"id" json:"id"` - QuizID int64 `db:"quiz_id" json:"quiz_id"` - Title string `db:"title" json:"title"` - Description sql.NullString `db:"description" json:"description"` - Questiontype interface{} `db:"questiontype" json:"questiontype"` - Required sql.NullBool `db:"required" json:"required"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` - Page sql.NullInt16 `db:"page" json:"page"` - Content sql.NullString `db:"content" json:"content"` - Version sql.NullInt16 `db:"version" json:"version"` - ParentIds []int32 `db:"parent_ids" json:"parent_ids"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"` -} - -func (q *Queries) GetQuestions(ctx context.Context, quizID int64) ([]GetQuestionsRow, error) { +func (q *Queries) GetQuestions(ctx context.Context, quizID int64) ([]Question, error) { rows, err := q.db.QueryContext(ctx, getQuestions, quizID) if err != nil { return nil, err } defer rows.Close() - var items []GetQuestionsRow + var items []Question for rows.Next() { - var i GetQuestionsRow + var i Question if err := rows.Scan( &i.ID, &i.QuizID, @@ -1360,7 +1510,7 @@ func (q *Queries) GetQuestions(ctx context.Context, quizID int64) ([]GetQuestion } const getQuizById = `-- name: GetQuizById :one -SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE id=$1 AND accountId=$2 +SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, utm, rules FROM quiz WHERE id=$1 AND accountId=$2 ` type GetQuizByIdParams struct { @@ -1401,12 +1551,14 @@ func (q *Queries) GetQuizById(ctx context.Context, arg GetQuizByIdParams) (Quiz, &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, + &i.Utm, + &i.Rules, ) return i, err } const getQuizByQid = `-- name: GetQuizByQid :one -SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz +SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, utm, rules FROM quiz WHERE deleted = false AND archived = false AND @@ -1447,6 +1599,8 @@ func (q *Queries) GetQuizByQid(ctx context.Context, qid uuid.NullUUID) (Quiz, er &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, + &i.Utm, + &i.Rules, ) return i, err } @@ -1468,7 +1622,7 @@ func (q *Queries) GetQuizConfig(ctx context.Context, id int64) (GetQuizConfigRow } const getQuizHistory = `-- name: GetQuizHistory :many -SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE quiz.id = $1 AND quiz.accountId = $4 OR quiz.id = ANY( +SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, utm, rules FROM quiz WHERE quiz.id = $1 AND quiz.accountId = $4 OR quiz.id = ANY( SELECT unnest(parent_ids) FROM quiz WHERE id = $1 ) ORDER BY quiz.id DESC LIMIT $2 OFFSET $3 ` @@ -1524,6 +1678,8 @@ func (q *Queries) GetQuizHistory(ctx context.Context, arg GetQuizHistoryParams) &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, + &i.Utm, + &i.Rules, ); err != nil { return nil, err } @@ -1600,6 +1756,114 @@ func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAn return items, nil } +const getStepsWithPagination = `-- name: GetStepsWithPagination :many +SELECT id, amoid, pipelineid, accountid, name, color, deleted, createdat, COUNT(*) OVER() as total_count FROM steps WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +` + +type GetStepsWithPaginationParams struct { + Column1 interface{} `db:"column_1" json:"column_1"` + Limit int32 `db:"limit" json:"limit"` +} + +type GetStepsWithPaginationRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + TotalCount int64 `db:"total_count" json:"total_count"` +} + +func (q *Queries) GetStepsWithPagination(ctx context.Context, arg GetStepsWithPaginationParams) ([]GetStepsWithPaginationRow, error) { + rows, err := q.db.QueryContext(ctx, getStepsWithPagination, arg.Column1, arg.Limit) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetStepsWithPaginationRow + for rows.Next() { + var i GetStepsWithPaginationRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Pipelineid, + &i.Accountid, + &i.Name, + &i.Color, + &i.Deleted, + &i.Createdat, + &i.TotalCount, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getTagsWithPagination = `-- name: GetTagsWithPagination :many +SELECT id, amoid, accountid, entity, name, color, deleted, createdat, COUNT(*) OVER() as total_count FROM tags WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +` + +type GetTagsWithPaginationParams struct { + Column1 interface{} `db:"column_1" json:"column_1"` + Limit int32 `db:"limit" json:"limit"` +} + +type GetTagsWithPaginationRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Accountid int32 `db:"accountid" json:"accountid"` + Entity interface{} `db:"entity" json:"entity"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + TotalCount int64 `db:"total_count" json:"total_count"` +} + +func (q *Queries) GetTagsWithPagination(ctx context.Context, arg GetTagsWithPaginationParams) ([]GetTagsWithPaginationRow, error) { + rows, err := q.db.QueryContext(ctx, getTagsWithPagination, arg.Column1, arg.Limit) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetTagsWithPaginationRow + for rows.Next() { + var i GetTagsWithPaginationRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Accountid, + &i.Entity, + &i.Name, + &i.Color, + &i.Deleted, + &i.Createdat, + &i.TotalCount, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getUsersWithPagination = `-- name: GetUsersWithPagination :many SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 ` @@ -1617,7 +1881,7 @@ type GetUsersWithPaginationRow struct { Email string `db:"email" json:"email"` Role string `db:"role" json:"role"` Group pqtype.NullRawMessage `db:"Group" json:"Group"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` + Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` Subdomain string `db:"subdomain" json:"subdomain"` Amouserid int32 `db:"amouserid" json:"amouserid"` @@ -2250,7 +2514,7 @@ SET average_time_passing = COALESCE(sta.average_session_time, 0), sessions_count = COALESCE(sta.sess,0) FROM - (SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE deleted = FALSE AND archived = FALSE) q_sub + (SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, utm, rules FROM quiz WHERE deleted = FALSE AND archived = FALSE) q_sub LEFT JOIN answer_aggregates aa ON q_sub.id = aa.quiz_id LEFT JOIN question_aggregates qa ON q_sub.id = qa.quiz_id LEFT JOIN session_times_aggregates sta ON q_sub.id = sta.quiz_id From 105a18f646f086aa9d42ad0a7abf7b107ec3c744 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 14:00:02 +0300 Subject: [PATCH 020/187] init new amo repo methods --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 2 +- model/amo.go | 26 +++--- model/amoReq.go | 2 +- model/amoResp.go | 6 +- repository/amo/amo.go | 170 +++++++++++++++++++++++++++++++++++-- 6 files changed, 180 insertions(+), 28 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index eab06f4..80ed910 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -703,7 +703,7 @@ WITH user_data AS ( WHERE users.AccountID = $1 ) INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) -SELECT (new_tags->>'Amoid')::INT, +SELECT (new_tags->>'AmoID')::INT, user_data.AmoID, CAST(new_tags->>'Entity' AS entitytype), new_tags->>'Name', diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 44ce61f..bbc1c37 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -220,7 +220,7 @@ WITH user_data AS ( WHERE users.AccountID = $1 ) INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) -SELECT (new_tags->>'Amoid')::INT, +SELECT (new_tags->>'AmoID')::INT, user_data.AmoID, CAST(new_tags->>'Entity' AS entitytype), new_tags->>'Name', diff --git a/model/amo.go b/model/amo.go index 478f6db..5fde7e9 100644 --- a/model/amo.go +++ b/model/amo.go @@ -44,11 +44,11 @@ type Token struct { type Pipeline struct { // айдишник в нашей системе Primary Key - ID int `json:"ID"` + ID int64 `json:"ID"` /* - айдишник воронки в амо*/ - Amoid int `json:"AmoID"` + Amoid int32 `json:"AmoID"` /* - связь с аккаунтом в интеграции амо id аккаунта в амо*/ - AccountID int `json:"AccountID"` + AccountID int32 `json:"AccountID"` /* - название воронки в амо*/ Name string `json:"Name"` /* - флаг архивной воронки в амо*/ @@ -61,13 +61,13 @@ type Pipeline struct { type Step struct { /* - айдишник в нашей системе Primary Key*/ - ID int `json:"ID"` + ID int64 `json:"ID"` /* - айдишник шага воронки в амо*/ - Amoid int `json:"AmoID"` + Amoid int32 `json:"AmoID"` /* - айдишник воронки в амо*/ - Pipelineid int `json:"PipelineID"` + Pipelineid int32 `json:"PipelineID"` /* - связь с аккаунтом в интеграции амо id в амо*/ - Accountid int `json:"AccountID"` + Accountid int32 `json:"AccountID"` /* - название воронки в амо*/ Name string `json:"Name"` /* - цвет шага в амо*/ @@ -80,11 +80,11 @@ type Step struct { type Tag struct { /* - айдишник в нашей системе Primary Key*/ - ID int `json:"ID"` + ID int64 `json:"ID"` /* - айдишник тега в амо*/ - Amoid int `json:"AmoID"` + Amoid int32 `json:"AmoID"` /* - связь с аккаунтом в интеграции амо id аккаунта в амо*/ - Accountid int `json:"AccountID"` + Accountid int32 `json:"AccountID"` /* - сущность, к которой принадлежит этот тег. Наверное, стоит сделать через enum в базе*/ Entity EntityType `json:"Entity"` /* - название тега в амо*/ @@ -99,13 +99,13 @@ type Tag struct { type Field struct { /* - айдишник в нашей системе Primary Key*/ - ID int `json:"ID"` + ID int64 `json:"ID"` /* - айдишник кастомного поля в амо*/ - Amoid int `json:"AmoID"` + Amoid int32 `json:"AmoID"` /* - кодовое слово в амо*/ Code string `json:"Code"` /* - связь с аккаунтом в интеграции амо id аккаунта в амо*/ - Accountid int `json:"AccountID"` + Accountid int32 `json:"AccountID"` /* - название воронки в амо*/ Name string `json:"Name"` /* - тип сущности в амо, для которой это кастомное поле*/ diff --git a/model/amoReq.go b/model/amoReq.go index 7be5c36..6628a93 100644 --- a/model/amoReq.go +++ b/model/amoReq.go @@ -9,7 +9,7 @@ type PaginationReq struct { /* - указание страницы пагинации. Если страница не указана, применять 0*/ Page int `json:"page"` /* - указание размера страницы пагинации. По умолчанию применять 25*/ - Size int `json:"size"` + Size int32 `json:"size"` } type RulesReq struct { diff --git a/model/amoResp.go b/model/amoResp.go index 9f3c797..5b260f1 100644 --- a/model/amoResp.go +++ b/model/amoResp.go @@ -38,14 +38,14 @@ type ListSavedIDUTMResp struct { type UserListFieldsResp struct { /* - общее количество кастомных полей, которые у нас закешированы для этого пользователя*/ - Count int `json:"count"` + Count int64 `json:"count"` /* - список кастомных полей, которые были закешированы нашим сервисом*/ Items []Field `json:"items"` } type UserListPipelinesResp struct { /* - общее количество воронок, которые у нас закешированы для этого пользователя*/ - Count int `json:"count"` + Count int64 `json:"count"` /* - список воронок, которые были закешированы нашим сервисом*/ Items []Pipeline `json:"items"` } @@ -61,7 +61,7 @@ type UserListStepsResp struct { /* - список шагов воронок, которые были закешированы нашим сервисом*/ Items []Step `json:"items"` /* - общее количество шагов воронок, которые у нас закешированы для этого пользователя*/ - Count int `json:"count"` + Count int64 `json:"count"` } type UserListTagsResp struct { diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 4564d5d..1c2a832 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "encoding/json" + "fmt" "github.com/sqlc-dev/pqtype" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" @@ -39,7 +40,7 @@ func (r *AmoRepository) UpdateListUsers(ctx context.Context) error { func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListResp, error) { rows, err := r.queries.GetUsersWithPagination(ctx, sqlcgen.GetUsersWithPaginationParams{ Column1: req.Page, - Limit: int32(req.Size), + Limit: req.Size, }) if err != nil { @@ -266,8 +267,36 @@ func (r *AmoRepository) UpdateListPipelines(ctx context.Context) error { } -func (r *AmoRepository) GettingPipelinesFromCash(ctx context.Context, req *model.PaginationReq) (*model.UserListPipelinesResp, error) { - return nil, nil +func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListPipelinesResp, error) { + rows, err := r.queries.GetPipelinesWithPagination(ctx, sqlcgen.GetPipelinesWithPaginationParams{ + Column1: req.Page, + Limit: req.Size, + }) + 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 } func (r *AmoRepository) CheckPipelines(ctx context.Context, accountID string, pipelines []model.Pipeline) error { @@ -288,8 +317,38 @@ func (r *AmoRepository) InsertPipeline(ctx context.Context, pipeline *model.Pipe // методы steps -func (r *AmoRepository) GettingStepsFromCash(ctx context.Context, req *model.PaginationReq) (*model.UserListStepsResp, error) { - return nil, nil +func (r *AmoRepository) GetStepsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListStepsResp, error) { + rows, err := r.queries.GetStepsWithPagination(ctx, sqlcgen.GetStepsWithPaginationParams{ + Column1: req.Page, + Limit: req.Size, + }) + 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 } func (r *AmoRepository) UpdateListSteps(ctx context.Context) error { @@ -318,8 +377,45 @@ func (r *AmoRepository) InsertStep(ctx context.Context, step *model.Step) error // методы tags -func (r *AmoRepository) GettingTagsFromCash(ctx context.Context, req *model.PaginationReq) (*model.UserListTagsResp, error) { - return nil, nil +func (r *AmoRepository) GetTagsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListTagsResp, error) { + rows, err := r.queries.GetTagsWithPagination(ctx, sqlcgen.GetTagsWithPaginationParams{ + Column1: req.Page, + Limit: req.Size, + }) + if err != nil { + return nil, err + } + + var count int64 + var tags []model.Tag + for _, row := range rows { + count = row.TotalCount + + var entity model.EntityType + if v, ok := row.Entity.(string); ok { + entity = model.EntityType(v) + } else { + fmt.Println("unexpected type for EntityType:", row.Entity) + } + + 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 } func (r *AmoRepository) UpdateListTags(ctx context.Context) error { @@ -330,7 +426,23 @@ func (r *AmoRepository) UpdateListTags(ctx context.Context) error { } func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID string) error { + var column2 []json.RawMessage + for _, tag := range tags { + jsonTag, err := json.Marshal(tag) + if err != nil { + return err + } + column2 = append(column2, jsonTag) + } + _, err := r.queries.CheckTags(ctx, sqlcgen.CheckTagsParams{ + Accountid: tokenID, + Column2: column2, + }) + + if err != nil { + return err + } return nil } @@ -348,8 +460,48 @@ func (r *AmoRepository) InsertTag(ctx context.Context, tag *model.Tag) error { // методы fields -func (r *AmoRepository) GettingFieldsFromCash(ctx context.Context, req *model.PaginationReq) (*model.UserListFieldsResp, error) { - return nil, nil +func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListFieldsResp, error) { + rows, err := r.queries.GetFieldsWithPagination(ctx, sqlcgen.GetFieldsWithPaginationParams{ + Column1: req.Page, + Limit: req.Size, + }) + if err != nil { + return nil, err + } + + var count int64 + var fields []model.Field + + for _, row := range rows { + count = row.TotalCount + + var entity model.EntityType + if v, ok := row.Entity.(string); ok { + entity = model.EntityType(v) + } else { + fmt.Println("unexpected type for EntityType:", row.Entity) + } + + 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 } func (r *AmoRepository) UpdateListCustom(ctx context.Context) error { From 5f2e01f9a025e333cf7d5bff4d837c70c2840f7a Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 15:50:11 +0300 Subject: [PATCH 021/187] add another check queryes --- dal/db_query/queries.sql | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 80ed910..0502f11 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -717,4 +717,66 @@ ON CONFLICT (amoID, accountID, entity) DO UPDATE createdAt = CURRENT_TIMESTAMP RETURNING *; +-- name: CheckPipelines :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +) +INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) +SELECT (new_pipelines->>'AmoID')::INT, + user_data.AmoID, + new_pipelines->>'Name', + new_pipelines->>'IsArchive', + CURRENT_TIMESTAMP +FROM json_array_elements($2::json[]) AS new_pipelines + JOIN user_data ON true +ON CONFLICT (amoID, accountID) DO UPDATE + SET name = EXCLUDED.name, + isArchive = EXCLUDED.isArchive, + createdAt = CURRENT_TIMESTAMP +RETURNING *; +-- name: CheckSteps :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +) +INSERT INTO steps (amoID,pipelineID, accountID, name, color, createdAt) +SELECT (new_steps->>'AmoID')::INT, + (new_steps->>'PipelineID')::INT, + user_data.AmoID, + new_steps->>'Name', + new_steps->>'Color', + CURRENT_TIMESTAMP +FROM json_array_elements($2::json[]) AS new_steps + JOIN user_data ON true +ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE + SET name = EXCLUDED.name, + color = EXCLUDED.color, + createdAt = CURRENT_TIMESTAMP +RETURNING *; + +-- name: CheckFields :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +) +INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) +SELECT (new_fields->>'AmoID')::INT, + new_fields->>'Code', + user_data.AmoID, + new_fields->>'Name', + CAST(new_fields->>'Entity' AS entitytype), + new_fields->>'Type', + CURRENT_TIMESTAMP +FROM json_array_elements($2::json[]) AS new_fields + JOIN user_data ON true +ON CONFLICT (amoID, accountID, entity) DO UPDATE + SET name = EXCLUDED.name, + code = EXCLUDED.code, + type = EXCLUDED.type, + createdAt = CURRENT_TIMESTAMP +RETURNING *; \ No newline at end of file From 9c1fb0644e01d787fb63734c36f5a14f542566c9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 15:52:22 +0300 Subject: [PATCH 022/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 178 +++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index bbc1c37..d16cdcd 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -167,6 +167,125 @@ func (q *Queries) CheckExpired(ctx context.Context) ([]Token, error) { return items, nil } +const checkFields = `-- name: CheckFields :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +) +INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) +SELECT (new_fields->>'AmoID')::INT, + new_fields->>'Code', + user_data.AmoID, + new_fields->>'Name', + CAST(new_fields->>'Entity' AS entitytype), + new_fields->>'Type', + CURRENT_TIMESTAMP +FROM json_array_elements($2::json[]) AS new_fields + JOIN user_data ON true +ON CONFLICT (amoID, accountID, entity) DO UPDATE + SET name = EXCLUDED.name, + code = EXCLUDED.code, + type = EXCLUDED.type, + createdAt = CURRENT_TIMESTAMP +RETURNING id, amoid, code, accountid, name, entity, type, deleted, createdat +` + +type CheckFieldsParams struct { + Accountid string `db:"accountid" json:"accountid"` + Column2 []json.RawMessage `db:"column_2" json:"column_2"` +} + +func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]Field, error) { + rows, err := q.db.QueryContext(ctx, checkFields, arg.Accountid, pq.Array(arg.Column2)) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Field + for rows.Next() { + var i Field + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Code, + &i.Accountid, + &i.Name, + &i.Entity, + &i.Type, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const checkPipelines = `-- name: CheckPipelines :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +) +INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) +SELECT (new_pipelines->>'AmoID')::INT, + user_data.AmoID, + new_pipelines->>'Name', + new_pipelines->>'IsArchive', + CURRENT_TIMESTAMP +FROM json_array_elements($2::json[]) AS new_pipelines + JOIN user_data ON true +ON CONFLICT (amoID, accountID) DO UPDATE + SET name = EXCLUDED.name, + isArchive = EXCLUDED.isArchive, + createdAt = CURRENT_TIMESTAMP +RETURNING id, amoid, accountid, name, isarchive, deleted, createdat +` + +type CheckPipelinesParams struct { + Accountid string `db:"accountid" json:"accountid"` + Column2 []json.RawMessage `db:"column_2" json:"column_2"` +} + +func (q *Queries) CheckPipelines(ctx context.Context, arg CheckPipelinesParams) ([]Pipeline, error) { + rows, err := q.db.QueryContext(ctx, checkPipelines, arg.Accountid, pq.Array(arg.Column2)) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Pipeline + for rows.Next() { + var i Pipeline + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Accountid, + &i.Name, + &i.Isarchive, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const checkResultOwner = `-- name: CheckResultOwner :one SELECT q.accountid FROM answer a JOIN quiz q ON a.quiz_id = q.id WHERE a.id = $1 AND a.deleted = FALSE AND a.start = false ` @@ -213,6 +332,65 @@ func (q *Queries) CheckResultsOwner(ctx context.Context, arg CheckResultsOwnerPa return items, nil } +const checkSteps = `-- name: CheckSteps :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +) +INSERT INTO steps (amoID,pipelineID, accountID, name, color, createdAt) +SELECT (new_steps->>'AmoID')::INT, + (new_steps->>'PipelineID')::INT, + user_data.AmoID, + new_steps->>'Name', + new_steps->>'Color', + CURRENT_TIMESTAMP +FROM json_array_elements($2::json[]) AS new_steps + JOIN user_data ON true +ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE + SET name = EXCLUDED.name, + color = EXCLUDED.color, + createdAt = CURRENT_TIMESTAMP +RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat +` + +type CheckStepsParams struct { + Accountid string `db:"accountid" json:"accountid"` + Column2 []json.RawMessage `db:"column_2" json:"column_2"` +} + +func (q *Queries) CheckSteps(ctx context.Context, arg CheckStepsParams) ([]Step, error) { + rows, err := q.db.QueryContext(ctx, checkSteps, arg.Accountid, pq.Array(arg.Column2)) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Step + for rows.Next() { + var i Step + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Pipelineid, + &i.Accountid, + &i.Name, + &i.Color, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const checkTags = `-- name: CheckTags :many WITH user_data AS ( SELECT AmoID From ad18b4a7884f00e5f0c24277882f45ae8057fc1f Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 16:15:20 +0300 Subject: [PATCH 023/187] update querys --- dal/db_query/queries.sql | 20 ++++---------------- repository/amo/amo.go | 23 +++++++++++++++++++---- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 0502f11..9d262ee 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -718,19 +718,13 @@ ON CONFLICT (amoID, accountID, entity) DO UPDATE RETURNING *; -- name: CheckPipelines :many -WITH user_data AS ( - SELECT AmoID - FROM users - WHERE users.AccountID = $1 -) INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) SELECT (new_pipelines->>'AmoID')::INT, - user_data.AmoID, + (new_pipelines->>'AccountID')::INT, new_pipelines->>'Name', new_pipelines->>'IsArchive', CURRENT_TIMESTAMP -FROM json_array_elements($2::json[]) AS new_pipelines - JOIN user_data ON true +FROM json_array_elements($1::json[]) AS new_pipelines ON CONFLICT (amoID, accountID) DO UPDATE SET name = EXCLUDED.name, isArchive = EXCLUDED.isArchive, @@ -738,20 +732,14 @@ ON CONFLICT (amoID, accountID) DO UPDATE RETURNING *; -- name: CheckSteps :many -WITH user_data AS ( - SELECT AmoID - FROM users - WHERE users.AccountID = $1 -) INSERT INTO steps (amoID,pipelineID, accountID, name, color, createdAt) SELECT (new_steps->>'AmoID')::INT, (new_steps->>'PipelineID')::INT, - user_data.AmoID, + (new_steps->>'AccountID')::INT, new_steps->>'Name', new_steps->>'Color', CURRENT_TIMESTAMP -FROM json_array_elements($2::json[]) AS new_steps - JOIN user_data ON true +FROM json_array_elements($1::json[]) AS new_steps ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE SET name = EXCLUDED.name, color = EXCLUDED.color, diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 1c2a832..2a95bf4 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -299,7 +299,7 @@ func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *mod return &resp, nil } -func (r *AmoRepository) CheckPipelines(ctx context.Context, accountID string, pipelines []model.Pipeline) error { +func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pipeline) error { return nil } @@ -358,9 +358,8 @@ func (r *AmoRepository) UpdateListSteps(ctx context.Context) error { } -func (r *AmoRepository) CheckSteps(ctx context.Context, accountID string, steps []model.Step) error { +func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) error { - return nil } func (r *AmoRepository) GetStepByID(ctx context.Context, accountID string, amoid int) (*model.Step, error) { @@ -511,8 +510,24 @@ func (r *AmoRepository) UpdateListCustom(ctx context.Context) error { } -func (r *AmoRepository) CheckFields(ctx context.Context, Fields []model.Field, tokenID string) error { +func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, tokenID string) error { + var column2 []json.RawMessage + for _, field := range fields { + jsonTag, err := json.Marshal(field) + if err != nil { + return err + } + column2 = append(column2, jsonTag) + } + _, err := r.queries.CheckFields(ctx, sqlcgen.CheckFieldsParams{ + Accountid: tokenID, + Column2: column2, + }) + + if err != nil { + return err + } return nil } From 56036e7c35076304bbd40bf0c278ff6b613b087b Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 16:16:19 +0300 Subject: [PATCH 024/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 38 ++++++++------------------------------ 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index d16cdcd..553ff0f 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -230,19 +230,13 @@ func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]Fie } const checkPipelines = `-- name: CheckPipelines :many -WITH user_data AS ( - SELECT AmoID - FROM users - WHERE users.AccountID = $1 -) INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) SELECT (new_pipelines->>'AmoID')::INT, - user_data.AmoID, + (new_pipelines->>'AccountID')::INT, new_pipelines->>'Name', new_pipelines->>'IsArchive', CURRENT_TIMESTAMP -FROM json_array_elements($2::json[]) AS new_pipelines - JOIN user_data ON true +FROM json_array_elements($1::json[]) AS new_pipelines ON CONFLICT (amoID, accountID) DO UPDATE SET name = EXCLUDED.name, isArchive = EXCLUDED.isArchive, @@ -250,13 +244,8 @@ ON CONFLICT (amoID, accountID) DO UPDATE RETURNING id, amoid, accountid, name, isarchive, deleted, createdat ` -type CheckPipelinesParams struct { - Accountid string `db:"accountid" json:"accountid"` - Column2 []json.RawMessage `db:"column_2" json:"column_2"` -} - -func (q *Queries) CheckPipelines(ctx context.Context, arg CheckPipelinesParams) ([]Pipeline, error) { - rows, err := q.db.QueryContext(ctx, checkPipelines, arg.Accountid, pq.Array(arg.Column2)) +func (q *Queries) CheckPipelines(ctx context.Context, dollar_1 []json.RawMessage) ([]Pipeline, error) { + rows, err := q.db.QueryContext(ctx, checkPipelines, pq.Array(dollar_1)) if err != nil { return nil, err } @@ -333,20 +322,14 @@ func (q *Queries) CheckResultsOwner(ctx context.Context, arg CheckResultsOwnerPa } const checkSteps = `-- name: CheckSteps :many -WITH user_data AS ( - SELECT AmoID - FROM users - WHERE users.AccountID = $1 -) INSERT INTO steps (amoID,pipelineID, accountID, name, color, createdAt) SELECT (new_steps->>'AmoID')::INT, (new_steps->>'PipelineID')::INT, - user_data.AmoID, + (new_steps->>'AccountID')::INT, new_steps->>'Name', new_steps->>'Color', CURRENT_TIMESTAMP -FROM json_array_elements($2::json[]) AS new_steps - JOIN user_data ON true +FROM json_array_elements($1::json[]) AS new_steps ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE SET name = EXCLUDED.name, color = EXCLUDED.color, @@ -354,13 +337,8 @@ ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat ` -type CheckStepsParams struct { - Accountid string `db:"accountid" json:"accountid"` - Column2 []json.RawMessage `db:"column_2" json:"column_2"` -} - -func (q *Queries) CheckSteps(ctx context.Context, arg CheckStepsParams) ([]Step, error) { - rows, err := q.db.QueryContext(ctx, checkSteps, arg.Accountid, pq.Array(arg.Column2)) +func (q *Queries) CheckSteps(ctx context.Context, dollar_1 []json.RawMessage) ([]Step, error) { + rows, err := q.db.QueryContext(ctx, checkSteps, pq.Array(dollar_1)) if err != nil { return nil, err } From acca2a858bf8b69a6dbfdea42eaafd9ecc2967c9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 16:20:05 +0300 Subject: [PATCH 025/187] add another check methods to amo repo --- repository/amo/amo.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 2a95bf4..cc55cd2 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -300,6 +300,19 @@ func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *mod } func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pipeline) error { + var dollar1 []json.RawMessage + for _, pipeline := range pipelines { + jsonTag, err := json.Marshal(pipeline) + if err != nil { + return err + } + dollar1 = append(dollar1, jsonTag) + } + _, err := r.queries.CheckPipelines(ctx, dollar1) + if err != nil { + return err + } + return nil } @@ -359,7 +372,20 @@ func (r *AmoRepository) UpdateListSteps(ctx context.Context) error { } func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) error { + var dollar1 []json.RawMessage + for _, step := range steps { + jsonTag, err := json.Marshal(step) + if err != nil { + return err + } + dollar1 = append(dollar1, jsonTag) + } + _, err := r.queries.CheckSteps(ctx, dollar1) + if err != nil { + return err + } + return nil } func (r *AmoRepository) GetStepByID(ctx context.Context, accountID string, amoid int) (*model.Step, error) { From 50f5b8cdf028ca8888337cc75ec32b5500842696 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 16:49:45 +0300 Subject: [PATCH 026/187] fix --- dal/db_query/queries.sql | 8 +++---- repository/amo/amo.go | 48 ---------------------------------------- 2 files changed, 4 insertions(+), 52 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 9d262ee..6894962 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -709,7 +709,7 @@ SELECT (new_tags->>'AmoID')::INT, new_tags->>'Name', new_tags->>'Color', CURRENT_TIMESTAMP -FROM json_array_elements($2::json[]) AS new_tags +FROM json_array_elements($2::json) AS new_tags JOIN user_data ON true ON CONFLICT (amoID, accountID, entity) DO UPDATE SET name = EXCLUDED.name, @@ -724,7 +724,7 @@ SELECT (new_pipelines->>'AmoID')::INT, new_pipelines->>'Name', new_pipelines->>'IsArchive', CURRENT_TIMESTAMP -FROM json_array_elements($1::json[]) AS new_pipelines +FROM json_array_elements($1::json) AS new_pipelines ON CONFLICT (amoID, accountID) DO UPDATE SET name = EXCLUDED.name, isArchive = EXCLUDED.isArchive, @@ -739,7 +739,7 @@ SELECT (new_steps->>'AmoID')::INT, new_steps->>'Name', new_steps->>'Color', CURRENT_TIMESTAMP -FROM json_array_elements($1::json[]) AS new_steps +FROM json_array_elements($1::json) AS new_steps ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE SET name = EXCLUDED.name, color = EXCLUDED.color, @@ -760,7 +760,7 @@ SELECT (new_fields->>'AmoID')::INT, CAST(new_fields->>'Entity' AS entitytype), new_fields->>'Type', CURRENT_TIMESTAMP -FROM json_array_elements($2::json[]) AS new_fields +FROM json_array_elements($2::json) AS new_fields JOIN user_data ON true ON CONFLICT (amoID, accountID, entity) DO UPDATE SET name = EXCLUDED.name, diff --git a/repository/amo/amo.go b/repository/amo/amo.go index cc55cd2..8bb936f 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -316,18 +316,6 @@ func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pi return nil } -func (r *AmoRepository) GetPipelineByID(ctx context.Context, accountID string, amoid int) (*model.Pipeline, error) { - return nil, nil -} - -func (r *AmoRepository) UpdatePipeline(ctx context.Context, pipeline *model.Pipeline) error { - return nil -} - -func (r *AmoRepository) InsertPipeline(ctx context.Context, pipeline *model.Pipeline) error { - return nil -} - // методы steps func (r *AmoRepository) GetStepsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListStepsResp, error) { @@ -388,18 +376,6 @@ func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) erro return nil } -func (r *AmoRepository) GetStepByID(ctx context.Context, accountID string, amoid int) (*model.Step, error) { - return nil, nil -} - -func (r *AmoRepository) UpdateStep(ctx context.Context, step *model.Step) error { - return nil -} - -func (r *AmoRepository) InsertStep(ctx context.Context, step *model.Step) error { - return nil -} - // методы tags func (r *AmoRepository) GetTagsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListTagsResp, error) { @@ -471,18 +447,6 @@ func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID return nil } -func (r *AmoRepository) GetTagByID(ctx context.Context, accountID string, amoid int, entity model.EntityType) (*model.Tag, error) { - return nil, nil -} - -func (r *AmoRepository) UpdateTag(ctx context.Context, tag *model.Tag) error { - return nil -} - -func (r *AmoRepository) InsertTag(ctx context.Context, tag *model.Tag) error { - return nil -} - // методы fields func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListFieldsResp, error) { @@ -557,18 +521,6 @@ func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, t return nil } -func (r *AmoRepository) GetFieldByID(ctx context.Context, accountID string, amoid int, entity model.EntityType) (*model.Field, error) { - return nil, nil -} - -func (r *AmoRepository) UpdateField(ctx context.Context, field *model.Field) error { - return nil -} - -func (r *AmoRepository) InsertField(ctx context.Context, field *model.Field) error { - return nil -} - // методы rules func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq) error { From 78dc91629cffc130bba56b6c3771ee647a676752 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 16:50:25 +0300 Subject: [PATCH 027/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 553ff0f..0efdcef 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -181,7 +181,7 @@ SELECT (new_fields->>'AmoID')::INT, CAST(new_fields->>'Entity' AS entitytype), new_fields->>'Type', CURRENT_TIMESTAMP -FROM json_array_elements($2::json[]) AS new_fields +FROM json_array_elements($2::json) AS new_fields JOIN user_data ON true ON CONFLICT (amoID, accountID, entity) DO UPDATE SET name = EXCLUDED.name, @@ -192,12 +192,12 @@ RETURNING id, amoid, code, accountid, name, entity, type, deleted, createdat ` type CheckFieldsParams struct { - Accountid string `db:"accountid" json:"accountid"` - Column2 []json.RawMessage `db:"column_2" json:"column_2"` + Accountid string `db:"accountid" json:"accountid"` + Column2 json.RawMessage `db:"column_2" json:"column_2"` } func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]Field, error) { - rows, err := q.db.QueryContext(ctx, checkFields, arg.Accountid, pq.Array(arg.Column2)) + rows, err := q.db.QueryContext(ctx, checkFields, arg.Accountid, arg.Column2) if err != nil { return nil, err } @@ -236,7 +236,7 @@ SELECT (new_pipelines->>'AmoID')::INT, new_pipelines->>'Name', new_pipelines->>'IsArchive', CURRENT_TIMESTAMP -FROM json_array_elements($1::json[]) AS new_pipelines +FROM json_array_elements($1::json) AS new_pipelines ON CONFLICT (amoID, accountID) DO UPDATE SET name = EXCLUDED.name, isArchive = EXCLUDED.isArchive, @@ -244,8 +244,8 @@ ON CONFLICT (amoID, accountID) DO UPDATE RETURNING id, amoid, accountid, name, isarchive, deleted, createdat ` -func (q *Queries) CheckPipelines(ctx context.Context, dollar_1 []json.RawMessage) ([]Pipeline, error) { - rows, err := q.db.QueryContext(ctx, checkPipelines, pq.Array(dollar_1)) +func (q *Queries) CheckPipelines(ctx context.Context, dollar_1 json.RawMessage) ([]Pipeline, error) { + rows, err := q.db.QueryContext(ctx, checkPipelines, dollar_1) if err != nil { return nil, err } @@ -329,7 +329,7 @@ SELECT (new_steps->>'AmoID')::INT, new_steps->>'Name', new_steps->>'Color', CURRENT_TIMESTAMP -FROM json_array_elements($1::json[]) AS new_steps +FROM json_array_elements($1::json) AS new_steps ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE SET name = EXCLUDED.name, color = EXCLUDED.color, @@ -337,8 +337,8 @@ ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat ` -func (q *Queries) CheckSteps(ctx context.Context, dollar_1 []json.RawMessage) ([]Step, error) { - rows, err := q.db.QueryContext(ctx, checkSteps, pq.Array(dollar_1)) +func (q *Queries) CheckSteps(ctx context.Context, dollar_1 json.RawMessage) ([]Step, error) { + rows, err := q.db.QueryContext(ctx, checkSteps, dollar_1) if err != nil { return nil, err } @@ -382,7 +382,7 @@ SELECT (new_tags->>'AmoID')::INT, new_tags->>'Name', new_tags->>'Color', CURRENT_TIMESTAMP -FROM json_array_elements($2::json[]) AS new_tags +FROM json_array_elements($2::json) AS new_tags JOIN user_data ON true ON CONFLICT (amoID, accountID, entity) DO UPDATE SET name = EXCLUDED.name, @@ -392,12 +392,12 @@ RETURNING id, amoid, accountid, entity, name, color, deleted, createdat ` type CheckTagsParams struct { - Accountid string `db:"accountid" json:"accountid"` - Column2 []json.RawMessage `db:"column_2" json:"column_2"` + Accountid string `db:"accountid" json:"accountid"` + Column2 json.RawMessage `db:"column_2" json:"column_2"` } func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]Tag, error) { - rows, err := q.db.QueryContext(ctx, checkTags, arg.Accountid, pq.Array(arg.Column2)) + rows, err := q.db.QueryContext(ctx, checkTags, arg.Accountid, arg.Column2) if err != nil { return nil, err } From 6cf866392df57271e50edac0a2749f7f90c636dc Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 16:54:33 +0300 Subject: [PATCH 028/187] fix --- repository/amo/amo.go | 49 +++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 8bb936f..b6e8dd5 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -300,15 +300,11 @@ func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *mod } func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pipeline) error { - var dollar1 []json.RawMessage - for _, pipeline := range pipelines { - jsonTag, err := json.Marshal(pipeline) - if err != nil { - return err - } - dollar1 = append(dollar1, jsonTag) + dollar1, err := json.Marshal(pipelines) + if err != nil { + return err } - _, err := r.queries.CheckPipelines(ctx, dollar1) + _, err = r.queries.CheckPipelines(ctx, dollar1) if err != nil { return err } @@ -360,15 +356,12 @@ func (r *AmoRepository) UpdateListSteps(ctx context.Context) error { } func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) error { - var dollar1 []json.RawMessage - for _, step := range steps { - jsonTag, err := json.Marshal(step) - if err != nil { - return err - } - dollar1 = append(dollar1, jsonTag) + dollar1, err := json.Marshal(steps) + if err != nil { + return err } - _, err := r.queries.CheckSteps(ctx, dollar1) + + _, err = r.queries.CheckSteps(ctx, dollar1) if err != nil { return err } @@ -427,16 +420,12 @@ func (r *AmoRepository) UpdateListTags(ctx context.Context) error { } func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID string) error { - var column2 []json.RawMessage - for _, tag := range tags { - jsonTag, err := json.Marshal(tag) - if err != nil { - return err - } - column2 = append(column2, jsonTag) + column2, err := json.Marshal(tags) + if err != nil { + return err } - _, err := r.queries.CheckTags(ctx, sqlcgen.CheckTagsParams{ + _, err = r.queries.CheckTags(ctx, sqlcgen.CheckTagsParams{ Accountid: tokenID, Column2: column2, }) @@ -501,16 +490,12 @@ func (r *AmoRepository) UpdateListCustom(ctx context.Context) error { } func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, tokenID string) error { - var column2 []json.RawMessage - for _, field := range fields { - jsonTag, err := json.Marshal(field) - if err != nil { - return err - } - column2 = append(column2, jsonTag) + column2, err := json.Marshal(fields) + if err != nil { + return err } - _, err := r.queries.CheckFields(ctx, sqlcgen.CheckFieldsParams{ + _, err = r.queries.CheckFields(ctx, sqlcgen.CheckFieldsParams{ Accountid: tokenID, Column2: column2, }) From 1a0cc487cbb357f7aa678fc80fa21196009b4ddc Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 17:02:39 +0300 Subject: [PATCH 029/187] fix --- dal/db_query/queries.sql | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 6894962..8264f39 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -706,8 +706,8 @@ INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) SELECT (new_tags->>'AmoID')::INT, user_data.AmoID, CAST(new_tags->>'Entity' AS entitytype), - new_tags->>'Name', - new_tags->>'Color', + new_tags->>'Name'::varchar(50), + new_tags->>'Color'::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($2::json) AS new_tags JOIN user_data ON true @@ -721,8 +721,8 @@ RETURNING *; INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) SELECT (new_pipelines->>'AmoID')::INT, (new_pipelines->>'AccountID')::INT, - new_pipelines->>'Name', - new_pipelines->>'IsArchive', + new_pipelines->>'Name'::varchar(50), + new_pipelines->>'IsArchive'::BOOLEAN, CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_pipelines ON CONFLICT (amoID, accountID) DO UPDATE @@ -736,8 +736,8 @@ INSERT INTO steps (amoID,pipelineID, accountID, name, color, createdAt) SELECT (new_steps->>'AmoID')::INT, (new_steps->>'PipelineID')::INT, (new_steps->>'AccountID')::INT, - new_steps->>'Name', - new_steps->>'Color', + new_steps->>'Name'::varchar(50), + new_steps->>'Color'::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_steps ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE @@ -754,11 +754,11 @@ WITH user_data AS ( ) INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) SELECT (new_fields->>'AmoID')::INT, - new_fields->>'Code', + new_fields->>'Code'::varchar(255), user_data.AmoID, - new_fields->>'Name', + new_fields->>'Name'::varchar(50), CAST(new_fields->>'Entity' AS entitytype), - new_fields->>'Type', + new_fields->>'Type'::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($2::json) AS new_fields JOIN user_data ON true From a38933b45d40c1ea77849adece3ef804b9b3002f Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 17:03:29 +0300 Subject: [PATCH 030/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 0efdcef..64615b4 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -175,11 +175,11 @@ WITH user_data AS ( ) INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) SELECT (new_fields->>'AmoID')::INT, - new_fields->>'Code', + new_fields->>'Code'::varchar(255), user_data.AmoID, - new_fields->>'Name', + new_fields->>'Name'::varchar(50), CAST(new_fields->>'Entity' AS entitytype), - new_fields->>'Type', + new_fields->>'Type'::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($2::json) AS new_fields JOIN user_data ON true @@ -233,8 +233,8 @@ const checkPipelines = `-- name: CheckPipelines :many INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) SELECT (new_pipelines->>'AmoID')::INT, (new_pipelines->>'AccountID')::INT, - new_pipelines->>'Name', - new_pipelines->>'IsArchive', + new_pipelines->>'Name'::varchar(50), + new_pipelines->>'IsArchive'::BOOLEAN, CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_pipelines ON CONFLICT (amoID, accountID) DO UPDATE @@ -326,8 +326,8 @@ INSERT INTO steps (amoID,pipelineID, accountID, name, color, createdAt) SELECT (new_steps->>'AmoID')::INT, (new_steps->>'PipelineID')::INT, (new_steps->>'AccountID')::INT, - new_steps->>'Name', - new_steps->>'Color', + new_steps->>'Name'::varchar(50), + new_steps->>'Color'::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_steps ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE @@ -379,8 +379,8 @@ INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) SELECT (new_tags->>'AmoID')::INT, user_data.AmoID, CAST(new_tags->>'Entity' AS entitytype), - new_tags->>'Name', - new_tags->>'Color', + new_tags->>'Name'::varchar(50), + new_tags->>'Color'::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($2::json) AS new_tags JOIN user_data ON true From dccb464f9ed912c06694ea37450e38ff3343d7e1 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 17:57:34 +0300 Subject: [PATCH 031/187] add restincs --- dal/db_query/queries.sql | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 8264f39..a494dac 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -706,8 +706,8 @@ INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) SELECT (new_tags->>'AmoID')::INT, user_data.AmoID, CAST(new_tags->>'Entity' AS entitytype), - new_tags->>'Name'::varchar(50), - new_tags->>'Color'::varchar(50), + COALESCE(new_tags->>'Name', '')::varchar(50), + COALESCE(new_tags->>'Color', '')::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($2::json) AS new_tags JOIN user_data ON true @@ -721,8 +721,8 @@ RETURNING *; INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) SELECT (new_pipelines->>'AmoID')::INT, (new_pipelines->>'AccountID')::INT, - new_pipelines->>'Name'::varchar(50), - new_pipelines->>'IsArchive'::BOOLEAN, + COALESCE(new_pipelines->>'Name', '')::varchar(50), + CASE WHEN (new_pipelines->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_pipelines ON CONFLICT (amoID, accountID) DO UPDATE @@ -736,8 +736,8 @@ INSERT INTO steps (amoID,pipelineID, accountID, name, color, createdAt) SELECT (new_steps->>'AmoID')::INT, (new_steps->>'PipelineID')::INT, (new_steps->>'AccountID')::INT, - new_steps->>'Name'::varchar(50), - new_steps->>'Color'::varchar(50), + COALESCE(new_steps->>'Name', '')::varchar(50), + COALESCE(new_steps->>'Color', '')::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_steps ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE @@ -754,11 +754,11 @@ WITH user_data AS ( ) INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) SELECT (new_fields->>'AmoID')::INT, - new_fields->>'Code'::varchar(255), + COALESCE(new_fields->>'Code', '')::varchar(255), user_data.AmoID, - new_fields->>'Name'::varchar(50), + COALESCE(new_fields->>'Name', '')::varchar(50), CAST(new_fields->>'Entity' AS entitytype), - new_fields->>'Type'::varchar(50), + COALESCE(new_fields->>'Type', '')::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($2::json) AS new_fields JOIN user_data ON true From 091f7e72f465b0823d3f9c7333320e41397f60b1 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 17:58:32 +0300 Subject: [PATCH 032/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 64615b4..ccb3ef2 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -175,11 +175,11 @@ WITH user_data AS ( ) INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) SELECT (new_fields->>'AmoID')::INT, - new_fields->>'Code'::varchar(255), + COALESCE(new_fields->>'Code', '')::varchar(255), user_data.AmoID, - new_fields->>'Name'::varchar(50), + COALESCE(new_fields->>'Name', '')::varchar(50), CAST(new_fields->>'Entity' AS entitytype), - new_fields->>'Type'::varchar(50), + COALESCE(new_fields->>'Type', '')::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($2::json) AS new_fields JOIN user_data ON true @@ -233,8 +233,8 @@ const checkPipelines = `-- name: CheckPipelines :many INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) SELECT (new_pipelines->>'AmoID')::INT, (new_pipelines->>'AccountID')::INT, - new_pipelines->>'Name'::varchar(50), - new_pipelines->>'IsArchive'::BOOLEAN, + COALESCE(new_pipelines->>'Name', '')::varchar(50), + CASE WHEN (new_pipelines->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_pipelines ON CONFLICT (amoID, accountID) DO UPDATE @@ -326,8 +326,8 @@ INSERT INTO steps (amoID,pipelineID, accountID, name, color, createdAt) SELECT (new_steps->>'AmoID')::INT, (new_steps->>'PipelineID')::INT, (new_steps->>'AccountID')::INT, - new_steps->>'Name'::varchar(50), - new_steps->>'Color'::varchar(50), + COALESCE(new_steps->>'Name', '')::varchar(50), + COALESCE(new_steps->>'Color', '')::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_steps ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE @@ -379,8 +379,8 @@ INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) SELECT (new_tags->>'AmoID')::INT, user_data.AmoID, CAST(new_tags->>'Entity' AS entitytype), - new_tags->>'Name'::varchar(50), - new_tags->>'Color'::varchar(50), + COALESCE(new_tags->>'Name', '')::varchar(50), + COALESCE(new_tags->>'Color', '')::varchar(50), CURRENT_TIMESTAMP FROM json_array_elements($2::json) AS new_tags JOIN user_data ON true From 95fd5f2412868794d88a157186ef71134f77780d Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 18:14:14 +0300 Subject: [PATCH 033/187] add restincs --- dal/db_query/queries.sql | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index a494dac..37972e8 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -712,9 +712,9 @@ SELECT (new_tags->>'AmoID')::INT, FROM json_array_elements($2::json) AS new_tags JOIN user_data ON true ON CONFLICT (amoID, accountID, entity) DO UPDATE - SET name = EXCLUDED.name, - color = EXCLUDED.color, - createdAt = CURRENT_TIMESTAMP + SET name = CASE WHEN tags.name <> EXCLUDED.name THEN EXCLUDED.name ELSE tags.name END, + color = CASE WHEN tags.color <> EXCLUDED.color THEN EXCLUDED.color ELSE tags.color END, + createdAt = CASE WHEN tags.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE tags.createdAt END RETURNING *; -- name: CheckPipelines :many @@ -726,9 +726,9 @@ SELECT (new_pipelines->>'AmoID')::INT, CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_pipelines ON CONFLICT (amoID, accountID) DO UPDATE - SET name = EXCLUDED.name, - isArchive = EXCLUDED.isArchive, - createdAt = CURRENT_TIMESTAMP + SET name = CASE WHEN pipelines.name <> EXCLUDED.name THEN EXCLUDED.name ELSE pipelines.name END, + isArchive = CASE WHEN pipelines.isArchive <> EXCLUDED.isArchive THEN EXCLUDED.isArchive ELSE pipelines.isArchive END, + createdAt = CASE WHEN pipelines.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE pipelines.createdAt END RETURNING *; -- name: CheckSteps :many @@ -741,9 +741,9 @@ SELECT (new_steps->>'AmoID')::INT, CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_steps ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE - SET name = EXCLUDED.name, - color = EXCLUDED.color, - createdAt = CURRENT_TIMESTAMP + SET name = CASE WHEN steps.name <> EXCLUDED.name THEN EXCLUDED.name ELSE steps.name END, + color = CASE WHEN steps.color <> EXCLUDED.color THEN EXCLUDED.color ELSE steps.color END, + createdAt = CASE WHEN steps.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE steps.createdAt END RETURNING *; -- name: CheckFields :many @@ -763,8 +763,8 @@ SELECT (new_fields->>'AmoID')::INT, FROM json_array_elements($2::json) AS new_fields JOIN user_data ON true ON CONFLICT (amoID, accountID, entity) DO UPDATE - SET name = EXCLUDED.name, - code = EXCLUDED.code, - type = EXCLUDED.type, - createdAt = CURRENT_TIMESTAMP + SET name = CASE WHEN fields.name <> EXCLUDED.name THEN EXCLUDED.name ELSE fields.name END, + code = CASE WHEN fields.code <> EXCLUDED.code THEN EXCLUDED.code ELSE fields.code END, + type = CASE WHEN fields.type <> EXCLUDED.type THEN EXCLUDED.type ELSE fields.type END, + createdAt = CASE WHEN fields.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE fields.createdAt END RETURNING *; \ No newline at end of file From 0bb104019522378bf0e556e13d5a62bcb52c386b Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 17:59:24 +0300 Subject: [PATCH 034/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index ccb3ef2..56756dd 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -184,10 +184,10 @@ SELECT (new_fields->>'AmoID')::INT, FROM json_array_elements($2::json) AS new_fields JOIN user_data ON true ON CONFLICT (amoID, accountID, entity) DO UPDATE - SET name = EXCLUDED.name, - code = EXCLUDED.code, - type = EXCLUDED.type, - createdAt = CURRENT_TIMESTAMP + SET name = CASE WHEN fields.name <> EXCLUDED.name THEN EXCLUDED.name ELSE fields.name END, + code = CASE WHEN fields.code <> EXCLUDED.code THEN EXCLUDED.code ELSE fields.code END, + type = CASE WHEN fields.type <> EXCLUDED.type THEN EXCLUDED.type ELSE fields.type END, + createdAt = CASE WHEN fields.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE fields.createdAt END RETURNING id, amoid, code, accountid, name, entity, type, deleted, createdat ` @@ -238,9 +238,9 @@ SELECT (new_pipelines->>'AmoID')::INT, CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_pipelines ON CONFLICT (amoID, accountID) DO UPDATE - SET name = EXCLUDED.name, - isArchive = EXCLUDED.isArchive, - createdAt = CURRENT_TIMESTAMP + SET name = CASE WHEN pipelines.name <> EXCLUDED.name THEN EXCLUDED.name ELSE pipelines.name END, + isArchive = CASE WHEN pipelines.isArchive <> EXCLUDED.isArchive THEN EXCLUDED.isArchive ELSE pipelines.isArchive END, + createdAt = CASE WHEN pipelines.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE pipelines.createdAt END RETURNING id, amoid, accountid, name, isarchive, deleted, createdat ` @@ -331,9 +331,9 @@ SELECT (new_steps->>'AmoID')::INT, CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS new_steps ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE - SET name = EXCLUDED.name, - color = EXCLUDED.color, - createdAt = CURRENT_TIMESTAMP + SET name = CASE WHEN steps.name <> EXCLUDED.name THEN EXCLUDED.name ELSE steps.name END, + color = CASE WHEN steps.color <> EXCLUDED.color THEN EXCLUDED.color ELSE steps.color END, + createdAt = CASE WHEN steps.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE steps.createdAt END RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat ` @@ -385,9 +385,9 @@ SELECT (new_tags->>'AmoID')::INT, FROM json_array_elements($2::json) AS new_tags JOIN user_data ON true ON CONFLICT (amoID, accountID, entity) DO UPDATE - SET name = EXCLUDED.name, - color = EXCLUDED.color, - createdAt = CURRENT_TIMESTAMP + SET name = CASE WHEN tags.name <> EXCLUDED.name THEN EXCLUDED.name ELSE tags.name END, + color = CASE WHEN tags.color <> EXCLUDED.color THEN EXCLUDED.color ELSE tags.color END, + createdAt = CASE WHEN tags.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE tags.createdAt END RETURNING id, amoid, accountid, entity, name, color, deleted, createdat ` From 460f903f3e2de77e58f539c8fcc67f59dcc39fe7 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 20:32:07 +0300 Subject: [PATCH 035/187] separate queries --- dal/db_query/queries.sql | 150 ++++++++++++++++++++------------ dal/schema/000010_init.down.sql | 8 +- dal/schema/000010_init.up.sql | 8 +- 3 files changed, 104 insertions(+), 62 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 37972e8..e017063 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -701,70 +701,112 @@ WITH user_data AS ( SELECT AmoID FROM users WHERE users.AccountID = $1 -) +), +insert AS ( INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) -SELECT (new_tags->>'AmoID')::INT, - user_data.AmoID, - CAST(new_tags->>'Entity' AS entitytype), - COALESCE(new_tags->>'Name', '')::varchar(50), - COALESCE(new_tags->>'Color', '')::varchar(50), - CURRENT_TIMESTAMP -FROM json_array_elements($2::json) AS new_tags - JOIN user_data ON true -ON CONFLICT (amoID, accountID, entity) DO UPDATE - SET name = CASE WHEN tags.name <> EXCLUDED.name THEN EXCLUDED.name ELSE tags.name END, - color = CASE WHEN tags.color <> EXCLUDED.color THEN EXCLUDED.color ELSE tags.color END, - createdAt = CASE WHEN tags.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE tags.createdAt END -RETURNING *; + SELECT (new_tags->>'AmoID')::INT, + user_data.AmoID, + CAST(new_tags->>'Entity' AS entitytype), + COALESCE(new_tags->>'Name', '')::varchar(50), + COALESCE(new_tags->>'Color', '')::varchar(50), + CURRENT_TIMESTAMP + FROM json_array_elements($2::json) AS new_tags + JOIN user_data ON true + ON CONFLICT (amoID, accountID, entity) DO NOTHING + RETURNING EXCLUDED.* +) +SELECT * FROM insert; + +-- name: UpdateTags :json_array +UPDATE tags AS t +SET name = (update_data ->> 'Name')::varchar(50), + color = (update_data ->> 'Color')::varchar(50), + createdAt = CURRENT_TIMESTAMP +FROM json_array_elements($1::json) AS update_data +WHERE t.amoID = (update_data ->> 'AmoID')::INT + AND t.accountID = (update_data ->> 'AccountID')::INT + AND t.Entity = (update_data ->> 'Entity')::entitytype; + -- name: CheckPipelines :many +WITH insert AS ( INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) -SELECT (new_pipelines->>'AmoID')::INT, - (new_pipelines->>'AccountID')::INT, - COALESCE(new_pipelines->>'Name', '')::varchar(50), - CASE WHEN (new_pipelines->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, - CURRENT_TIMESTAMP -FROM json_array_elements($1::json) AS new_pipelines -ON CONFLICT (amoID, accountID) DO UPDATE - SET name = CASE WHEN pipelines.name <> EXCLUDED.name THEN EXCLUDED.name ELSE pipelines.name END, - isArchive = CASE WHEN pipelines.isArchive <> EXCLUDED.isArchive THEN EXCLUDED.isArchive ELSE pipelines.isArchive END, - createdAt = CASE WHEN pipelines.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE pipelines.createdAt END -RETURNING *; + SELECT (new_pipelines->>'AmoID')::INT, + (new_pipelines->>'AccountID')::INT, + COALESCE(new_pipelines->>'Name', '')::varchar(50), + CASE WHEN (new_pipelines->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, + CURRENT_TIMESTAMP + FROM json_array_elements($1::json) AS new_pipelines + ON CONFLICT (amoID, accountID) DO NOTHING + RETURNING EXCLUDED.* +) +SELECT * FROM insert; + +-- name: UpdatePipelines :exec +UPDATE pipelines AS p +SET name = (update_data ->> 'Name')::varchar(50), + isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END, + createdAt = CURRENT_TIMESTAMP +FROM json_array_elements($1::json) AS update_data +WHERE p.amoID = (update_data ->> 'AmoID')::INT + AND p.accountID = (update_data ->> 'AccountID')::INT; + -- name: CheckSteps :many -INSERT INTO steps (amoID,pipelineID, accountID, name, color, createdAt) -SELECT (new_steps->>'AmoID')::INT, - (new_steps->>'PipelineID')::INT, - (new_steps->>'AccountID')::INT, - COALESCE(new_steps->>'Name', '')::varchar(50), - COALESCE(new_steps->>'Color', '')::varchar(50), - CURRENT_TIMESTAMP -FROM json_array_elements($1::json) AS new_steps -ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE - SET name = CASE WHEN steps.name <> EXCLUDED.name THEN EXCLUDED.name ELSE steps.name END, - color = CASE WHEN steps.color <> EXCLUDED.color THEN EXCLUDED.color ELSE steps.color END, - createdAt = CASE WHEN steps.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE steps.createdAt END -RETURNING *; +WITH insert AS ( +INSERT INTO steps (amoID, pipelineID, accountID, name, color, createdAt) + SELECT (new_steps->>'AmoID')::INT, + (new_steps->>'PipelineID')::INT, + (new_steps->>'AccountID')::INT, + COALESCE(new_steps->>'Name', '')::varchar(50), + COALESCE(new_steps->>'Color', '')::varchar(50), + CURRENT_TIMESTAMP + FROM json_array_elements($1::json) AS new_steps + ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING + RETURNING EXCLUDED.* +) +SELECT * FROM insert; + +-- name: UpdateSteps :exec +UPDATE steps AS s +SET name = (update_data ->> 'Name')::varchar(50), + color = (update_data ->> 'Color')::varchar(50), + createdAt = CURRENT_TIMESTAMP +FROM json_array_elements($1::json) AS update_data +WHERE s.amoID = (update_data ->> 'AmoID')::INT + AND s.accountID = (update_data ->> 'AccountID')::INT + AND s.pipelineID = (update_data ->> 'PipelineID')::INT; + -- name: CheckFields :many WITH user_data AS ( SELECT AmoID FROM users WHERE users.AccountID = $1 +), +insert AS ( + INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) + SELECT (new_fields->>'AmoID')::INT, + COALESCE(new_fields->>'Code', '')::varchar(255), + user_data.AmoID, + COALESCE(new_fields->>'Name', '')::varchar(50), + CAST(new_fields->>'Entity' AS entitytype), + COALESCE(new_fields->>'Type', '')::varchar(50), + CURRENT_TIMESTAMP + FROM json_array_elements($2::json) AS new_fields + JOIN user_data ON true + ON CONFLICT (amoID, accountID, entity) DO NOTHING + RETURNING EXCLUDED.* ) -INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) -SELECT (new_fields->>'AmoID')::INT, - COALESCE(new_fields->>'Code', '')::varchar(255), - user_data.AmoID, - COALESCE(new_fields->>'Name', '')::varchar(50), - CAST(new_fields->>'Entity' AS entitytype), - COALESCE(new_fields->>'Type', '')::varchar(50), - CURRENT_TIMESTAMP -FROM json_array_elements($2::json) AS new_fields - JOIN user_data ON true -ON CONFLICT (amoID, accountID, entity) DO UPDATE - SET name = CASE WHEN fields.name <> EXCLUDED.name THEN EXCLUDED.name ELSE fields.name END, - code = CASE WHEN fields.code <> EXCLUDED.code THEN EXCLUDED.code ELSE fields.code END, - type = CASE WHEN fields.type <> EXCLUDED.type THEN EXCLUDED.type ELSE fields.type END, - createdAt = CASE WHEN fields.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE fields.createdAt END -RETURNING *; \ No newline at end of file +SELECT * FROM insert; + +-- name: UpdateFields :exec +UPDATE fields AS f +SET name = (update_data ->> 'Name')::varchar(50), + code = (update_data ->> 'Code')::varchar(255), + type = (update_data ->> 'Type')::varchar(50), + createdAt = CURRENT_TIMESTAMP +FROM json_array_elements($1::json) AS update_data +WHERE f.amoID = (update_data ->> 'AmoID')::INT + AND f.accountID = (update_data ->> 'AccountID')::INT + AND f.Entity = (update_data ->> 'Entity')::entitytype; diff --git a/dal/schema/000010_init.down.sql b/dal/schema/000010_init.down.sql index e1e3bd4..75a69ea 100644 --- a/dal/schema/000010_init.down.sql +++ b/dal/schema/000010_init.down.sql @@ -1,7 +1,7 @@ -ALTER TABLE pipelines DROP CONSTRAINT IF EXISTS unique_pipeline; -ALTER TABLE steps DROP CONSTRAINT IF EXISTS unique_step; -ALTER TABLE fields DROP CONSTRAINT IF EXISTS unique_field; -ALTER TABLE tags DROP CONSTRAINT IF EXISTS unique_tag; +DROP INDEX IF EXISTS idx_unique_pipeline; +DROP INDEX IF EXISTS idx_unique_step; +DROP INDEX IF EXISTS idx_unique_field; +DROP INDEX IF EXISTS idx_unique_tag; ALTER TABLE question DROP COLUMN IF EXISTS utm, diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 067b384..44a7c2d 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -78,7 +78,7 @@ ADD COLUMN utm jsonb NOT NULL DEFAULT '{}'; ALTER TABLE quiz ADD COLUMN rules jsonb NOT NULL DEFAULT '{}'; -ALTER TABLE pipelines ADD CONSTRAINT unique_pipeline UNIQUE (amoID, accountID); -ALTER TABLE steps ADD CONSTRAINT unique_step UNIQUE (amoID, accountID, PipelineID); -ALTER TABLE fields ADD CONSTRAINT unique_field UNIQUE (amoID, accountID, entity); -ALTER TABLE tags ADD CONSTRAINT unique_tag UNIQUE (amoID, accountID, entity); \ No newline at end of file +CREATE UNIQUE INDEX idx_unique_pipeline ON pipelines (amoID, accountID); +CREATE UNIQUE INDEX idx_unique_step ON steps (amoID, accountID, PipelineID); +CREATE UNIQUE INDEX idx_unique_field ON fields (amoID, accountID, entity); +CREATE UNIQUE INDEX idx_unique_tag ON tags (amoID, accountID, entity); \ No newline at end of file From 07644bfbf03ab3ca0569b97be0517ba509d59e18 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 23:05:49 +0300 Subject: [PATCH 036/187] test --- dal/db_query/queries.sql | 155 ++++++++++----------- dal/sqlcgen/queries.sql.go | 270 +++++++++++++------------------------ 2 files changed, 176 insertions(+), 249 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index e017063..61b5171 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -696,28 +696,7 @@ SELECT *, COUNT(*) OVER() as total_count FROM pipelines WHERE Deleted = false O -- name: GetFieldsWithPagination :many SELECT *, COUNT(*) OVER() as total_count FROM fields WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; --- name: CheckTags :many -WITH user_data AS ( - SELECT AmoID - FROM users - WHERE users.AccountID = $1 -), -insert AS ( -INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) - SELECT (new_tags->>'AmoID')::INT, - user_data.AmoID, - CAST(new_tags->>'Entity' AS entitytype), - COALESCE(new_tags->>'Name', '')::varchar(50), - COALESCE(new_tags->>'Color', '')::varchar(50), - CURRENT_TIMESTAMP - FROM json_array_elements($2::json) AS new_tags - JOIN user_data ON true - ON CONFLICT (amoID, accountID, entity) DO NOTHING - RETURNING EXCLUDED.* -) -SELECT * FROM insert; - --- name: UpdateTags :json_array +-- name: UpdateTags :exec UPDATE tags AS t SET name = (update_data ->> 'Name')::varchar(50), color = (update_data ->> 'Color')::varchar(50), @@ -727,21 +706,6 @@ WHERE t.amoID = (update_data ->> 'AmoID')::INT AND t.accountID = (update_data ->> 'AccountID')::INT AND t.Entity = (update_data ->> 'Entity')::entitytype; - --- name: CheckPipelines :many -WITH insert AS ( -INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) - SELECT (new_pipelines->>'AmoID')::INT, - (new_pipelines->>'AccountID')::INT, - COALESCE(new_pipelines->>'Name', '')::varchar(50), - CASE WHEN (new_pipelines->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, - CURRENT_TIMESTAMP - FROM json_array_elements($1::json) AS new_pipelines - ON CONFLICT (amoID, accountID) DO NOTHING - RETURNING EXCLUDED.* -) -SELECT * FROM insert; - -- name: UpdatePipelines :exec UPDATE pipelines AS p SET name = (update_data ->> 'Name')::varchar(50), @@ -751,22 +715,6 @@ FROM json_array_elements($1::json) AS update_data WHERE p.amoID = (update_data ->> 'AmoID')::INT AND p.accountID = (update_data ->> 'AccountID')::INT; - --- name: CheckSteps :many -WITH insert AS ( -INSERT INTO steps (amoID, pipelineID, accountID, name, color, createdAt) - SELECT (new_steps->>'AmoID')::INT, - (new_steps->>'PipelineID')::INT, - (new_steps->>'AccountID')::INT, - COALESCE(new_steps->>'Name', '')::varchar(50), - COALESCE(new_steps->>'Color', '')::varchar(50), - CURRENT_TIMESTAMP - FROM json_array_elements($1::json) AS new_steps - ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING - RETURNING EXCLUDED.* -) -SELECT * FROM insert; - -- name: UpdateSteps :exec UPDATE steps AS s SET name = (update_data ->> 'Name')::varchar(50), @@ -777,29 +725,6 @@ WHERE s.amoID = (update_data ->> 'AmoID')::INT AND s.accountID = (update_data ->> 'AccountID')::INT AND s.pipelineID = (update_data ->> 'PipelineID')::INT; - --- name: CheckFields :many -WITH user_data AS ( - SELECT AmoID - FROM users - WHERE users.AccountID = $1 -), -insert AS ( - INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) - SELECT (new_fields->>'AmoID')::INT, - COALESCE(new_fields->>'Code', '')::varchar(255), - user_data.AmoID, - COALESCE(new_fields->>'Name', '')::varchar(50), - CAST(new_fields->>'Entity' AS entitytype), - COALESCE(new_fields->>'Type', '')::varchar(50), - CURRENT_TIMESTAMP - FROM json_array_elements($2::json) AS new_fields - JOIN user_data ON true - ON CONFLICT (amoID, accountID, entity) DO NOTHING - RETURNING EXCLUDED.* -) -SELECT * FROM insert; - -- name: UpdateFields :exec UPDATE fields AS f SET name = (update_data ->> 'Name')::varchar(50), @@ -810,3 +735,81 @@ FROM json_array_elements($1::json) AS update_data WHERE f.amoID = (update_data ->> 'AmoID')::INT AND f.accountID = (update_data ->> 'AccountID')::INT AND f.Entity = (update_data ->> 'Entity')::entitytype; + +-- name: CheckTags :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +), new_tags AS ( + SELECT (tag->>'AmoID')::INT AS amoID, + (tag->>'Entity')::entitytype AS Entity, + COALESCE(tag->>'Name', '')::VARCHAR(50) AS name, + COALESCE(tag->>'Color', '')::VARCHAR(50) AS color + FROM json_array_elements($2::json) AS tag +), inserted_tags AS ( + INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) + SELECT nt.amoID, + ud.AmoID, + nt.Entity, + nt.name, + nt.color, + CURRENT_TIMESTAMP + FROM new_tags nt + JOIN user_data ud ON true + ON CONFLICT (amoID, accountID, Entity) DO NOTHING + RETURNING * +) +SELECT * FROM inserted_tags; + + +-- -- name: CheckPipelines :many +-- WITH insertt AS ( +-- INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) +-- SELECT (new_pipelines->>'AmoID')::INT, +-- (new_pipelines->>'AccountID')::INT, +-- COALESCE(new_pipelines->>'Name', '')::varchar(50), +-- CASE WHEN (new_pipelines->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, +-- CURRENT_TIMESTAMP +-- FROM json_array_elements($1::json) AS new_pipelines +-- ON CONFLICT (amoID, accountID) DO NOTHING +-- RETURNING EXCLUDED.* +-- ) +-- SELECT * FROM insertt; +-- +-- -- name: CheckFields :many +-- WITH user_data AS ( +-- SELECT AmoID +-- FROM users +-- WHERE users.AccountID = $1 +-- ), +-- insertt AS ( +-- INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) +-- SELECT (new_fields->>'AmoID')::INT, +-- COALESCE(new_fields->>'Code', '')::varchar(255), +-- user_data.AmoID, +-- COALESCE(new_fields->>'Name', '')::varchar(50), +-- CAST(new_fields->>'Entity' AS entitytype), +-- COALESCE(new_fields->>'Type', '')::varchar(50), +-- CURRENT_TIMESTAMP +-- FROM json_array_elements($2::json) AS new_fields +-- JOIN user_data ON true +-- ON CONFLICT (amoID, accountID, entity) DO NOTHING +-- RETURNING EXCLUDED.* +-- ) +-- SELECT * FROM insertt; +-- +-- -- name: CheckSteps :many +-- WITH insertt AS ( +-- INSERT INTO steps (amoID, pipelineID, accountID, name, color, createdAt) +-- SELECT (new_steps->>'AmoID')::INT, +-- (new_steps->>'PipelineID')::INT, +-- (new_steps->>'AccountID')::INT, +-- COALESCE(new_steps->>'Name', '')::varchar(50), +-- COALESCE(new_steps->>'Color', '')::varchar(50), +-- CURRENT_TIMESTAMP +-- FROM json_array_elements($1::json) AS new_steps +-- ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING +-- RETURNING EXCLUDED.* +-- ) +-- SELECT * FROM insertt; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 56756dd..ca5f117 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -167,114 +167,6 @@ func (q *Queries) CheckExpired(ctx context.Context) ([]Token, error) { return items, nil } -const checkFields = `-- name: CheckFields :many -WITH user_data AS ( - SELECT AmoID - FROM users - WHERE users.AccountID = $1 -) -INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) -SELECT (new_fields->>'AmoID')::INT, - COALESCE(new_fields->>'Code', '')::varchar(255), - user_data.AmoID, - COALESCE(new_fields->>'Name', '')::varchar(50), - CAST(new_fields->>'Entity' AS entitytype), - COALESCE(new_fields->>'Type', '')::varchar(50), - CURRENT_TIMESTAMP -FROM json_array_elements($2::json) AS new_fields - JOIN user_data ON true -ON CONFLICT (amoID, accountID, entity) DO UPDATE - SET name = CASE WHEN fields.name <> EXCLUDED.name THEN EXCLUDED.name ELSE fields.name END, - code = CASE WHEN fields.code <> EXCLUDED.code THEN EXCLUDED.code ELSE fields.code END, - type = CASE WHEN fields.type <> EXCLUDED.type THEN EXCLUDED.type ELSE fields.type END, - createdAt = CASE WHEN fields.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE fields.createdAt END -RETURNING id, amoid, code, accountid, name, entity, type, deleted, createdat -` - -type CheckFieldsParams struct { - Accountid string `db:"accountid" json:"accountid"` - Column2 json.RawMessage `db:"column_2" json:"column_2"` -} - -func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]Field, error) { - rows, err := q.db.QueryContext(ctx, checkFields, arg.Accountid, arg.Column2) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Field - for rows.Next() { - var i Field - if err := rows.Scan( - &i.ID, - &i.Amoid, - &i.Code, - &i.Accountid, - &i.Name, - &i.Entity, - &i.Type, - &i.Deleted, - &i.Createdat, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const checkPipelines = `-- name: CheckPipelines :many -INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) -SELECT (new_pipelines->>'AmoID')::INT, - (new_pipelines->>'AccountID')::INT, - COALESCE(new_pipelines->>'Name', '')::varchar(50), - CASE WHEN (new_pipelines->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, - CURRENT_TIMESTAMP -FROM json_array_elements($1::json) AS new_pipelines -ON CONFLICT (amoID, accountID) DO UPDATE - SET name = CASE WHEN pipelines.name <> EXCLUDED.name THEN EXCLUDED.name ELSE pipelines.name END, - isArchive = CASE WHEN pipelines.isArchive <> EXCLUDED.isArchive THEN EXCLUDED.isArchive ELSE pipelines.isArchive END, - createdAt = CASE WHEN pipelines.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE pipelines.createdAt END -RETURNING id, amoid, accountid, name, isarchive, deleted, createdat -` - -func (q *Queries) CheckPipelines(ctx context.Context, dollar_1 json.RawMessage) ([]Pipeline, error) { - rows, err := q.db.QueryContext(ctx, checkPipelines, dollar_1) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Pipeline - for rows.Next() { - var i Pipeline - if err := rows.Scan( - &i.ID, - &i.Amoid, - &i.Accountid, - &i.Name, - &i.Isarchive, - &i.Deleted, - &i.Createdat, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - const checkResultOwner = `-- name: CheckResultOwner :one SELECT q.accountid FROM answer a JOIN quiz q ON a.quiz_id = q.id WHERE a.id = $1 AND a.deleted = FALSE AND a.start = false ` @@ -321,74 +213,31 @@ func (q *Queries) CheckResultsOwner(ctx context.Context, arg CheckResultsOwnerPa return items, nil } -const checkSteps = `-- name: CheckSteps :many -INSERT INTO steps (amoID,pipelineID, accountID, name, color, createdAt) -SELECT (new_steps->>'AmoID')::INT, - (new_steps->>'PipelineID')::INT, - (new_steps->>'AccountID')::INT, - COALESCE(new_steps->>'Name', '')::varchar(50), - COALESCE(new_steps->>'Color', '')::varchar(50), - CURRENT_TIMESTAMP -FROM json_array_elements($1::json) AS new_steps -ON CONFLICT (amoID, accountID, PipelineID) DO UPDATE - SET name = CASE WHEN steps.name <> EXCLUDED.name THEN EXCLUDED.name ELSE steps.name END, - color = CASE WHEN steps.color <> EXCLUDED.color THEN EXCLUDED.color ELSE steps.color END, - createdAt = CASE WHEN steps.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE steps.createdAt END -RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat -` - -func (q *Queries) CheckSteps(ctx context.Context, dollar_1 json.RawMessage) ([]Step, error) { - rows, err := q.db.QueryContext(ctx, checkSteps, dollar_1) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Step - for rows.Next() { - var i Step - if err := rows.Scan( - &i.ID, - &i.Amoid, - &i.Pipelineid, - &i.Accountid, - &i.Name, - &i.Color, - &i.Deleted, - &i.Createdat, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - const checkTags = `-- name: CheckTags :many WITH user_data AS ( SELECT AmoID FROM users WHERE users.AccountID = $1 +), new_tags AS ( + SELECT (tag->>'AmoID')::INT AS amoID, + (tag->>'Entity')::entitytype AS Entity, + COALESCE(tag->>'Name', '')::VARCHAR(50) AS name, + COALESCE(tag->>'Color', '')::VARCHAR(50) AS color + FROM json_array_elements($2::json) AS tag +), inserted_tags AS ( + INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) + SELECT nt.amoID, + ud.AmoID, + nt.Entity, + nt.name, + nt.color, + CURRENT_TIMESTAMP + FROM new_tags nt + JOIN user_data ud ON true + ON CONFLICT (amoID, accountID, Entity) DO NOTHING + RETURNING id, amoid, accountid, entity, name, color, deleted, createdat ) -INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt) -SELECT (new_tags->>'AmoID')::INT, - user_data.AmoID, - CAST(new_tags->>'Entity' AS entitytype), - COALESCE(new_tags->>'Name', '')::varchar(50), - COALESCE(new_tags->>'Color', '')::varchar(50), - CURRENT_TIMESTAMP -FROM json_array_elements($2::json) AS new_tags - JOIN user_data ON true -ON CONFLICT (amoID, accountID, entity) DO UPDATE - SET name = CASE WHEN tags.name <> EXCLUDED.name THEN EXCLUDED.name ELSE tags.name END, - color = CASE WHEN tags.color <> EXCLUDED.color THEN EXCLUDED.color ELSE tags.color END, - createdAt = CASE WHEN tags.createdAt <> CURRENT_TIMESTAMP THEN CURRENT_TIMESTAMP ELSE tags.createdAt END -RETURNING id, amoid, accountid, entity, name, color, deleted, createdat +SELECT id, amoid, accountid, entity, name, color, deleted, createdat FROM inserted_tags ` type CheckTagsParams struct { @@ -396,15 +245,26 @@ type CheckTagsParams struct { Column2 json.RawMessage `db:"column_2" json:"column_2"` } -func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]Tag, error) { +type CheckTagsRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Accountid int32 `db:"accountid" json:"accountid"` + Entity interface{} `db:"entity" json:"entity"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]CheckTagsRow, error) { rows, err := q.db.QueryContext(ctx, checkTags, arg.Accountid, arg.Column2) if err != nil { return nil, err } defer rows.Close() - var items []Tag + var items []CheckTagsRow for rows.Next() { - var i Tag + var i CheckTagsRow if err := rows.Scan( &i.ID, &i.Amoid, @@ -2551,6 +2411,38 @@ func (q *Queries) SoftDeleteResultByID(ctx context.Context, id int64) error { return err } +const updateFields = `-- name: UpdateFields :exec +UPDATE fields AS f +SET name = (update_data ->> 'Name')::varchar(50), + code = (update_data ->> 'Code')::varchar(255), + type = (update_data ->> 'Type')::varchar(50), + createdAt = CURRENT_TIMESTAMP +FROM json_array_elements($1::json) AS update_data +WHERE f.amoID = (update_data ->> 'AmoID')::INT + AND f.accountID = (update_data ->> 'AccountID')::INT + AND f.Entity = (update_data ->> 'Entity')::entitytype +` + +func (q *Queries) UpdateFields(ctx context.Context, dollar_1 json.RawMessage) error { + _, err := q.db.ExecContext(ctx, updateFields, dollar_1) + return err +} + +const updatePipelines = `-- name: UpdatePipelines :exec +UPDATE pipelines AS p +SET name = (update_data ->> 'Name')::varchar(50), + isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END, + createdAt = CURRENT_TIMESTAMP +FROM json_array_elements($1::json) AS update_data +WHERE p.amoID = (update_data ->> 'AmoID')::INT + AND p.accountID = (update_data ->> 'AccountID')::INT +` + +func (q *Queries) UpdatePipelines(ctx context.Context, dollar_1 json.RawMessage) error { + _, err := q.db.ExecContext(ctx, updatePipelines, dollar_1) + return err +} + const updatePrivilege = `-- name: UpdatePrivilege :exec UPDATE privileges SET amount = $1, created_at = $2 WHERE account_id = $3 AND privilegeID = $4 ` @@ -2586,6 +2478,38 @@ func (q *Queries) UpdatePrivilegeAmount(ctx context.Context, arg UpdatePrivilege return err } +const updateSteps = `-- name: UpdateSteps :exec +UPDATE steps AS s +SET name = (update_data ->> 'Name')::varchar(50), + color = (update_data ->> 'Color')::varchar(50), + createdAt = CURRENT_TIMESTAMP +FROM json_array_elements($1::json) AS update_data +WHERE s.amoID = (update_data ->> 'AmoID')::INT + AND s.accountID = (update_data ->> 'AccountID')::INT + AND s.pipelineID = (update_data ->> 'PipelineID')::INT +` + +func (q *Queries) UpdateSteps(ctx context.Context, dollar_1 json.RawMessage) error { + _, err := q.db.ExecContext(ctx, updateSteps, dollar_1) + return err +} + +const updateTags = `-- name: UpdateTags :exec +UPDATE tags AS t +SET name = (update_data ->> 'Name')::varchar(50), + color = (update_data ->> 'Color')::varchar(50), + createdAt = CURRENT_TIMESTAMP +FROM json_array_elements($1::json) AS update_data +WHERE t.amoID = (update_data ->> 'AmoID')::INT + AND t.accountID = (update_data ->> 'AccountID')::INT + AND t.Entity = (update_data ->> 'Entity')::entitytype +` + +func (q *Queries) UpdateTags(ctx context.Context, dollar_1 json.RawMessage) error { + _, err := q.db.ExecContext(ctx, updateTags, dollar_1) + return err +} + const webhookDelete = `-- name: WebhookDelete :exec DELETE FROM tokens WHERE AccountID = $1 ` From ce2d4fedb9184db9c3d69fd481cfce87830c93aa Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 23:14:38 +0300 Subject: [PATCH 037/187] test --- repository/amo/amo.go | 94 +++++++++++++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 31 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index b6e8dd5..c42b03b 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -300,14 +300,14 @@ func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *mod } func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pipeline) error { - dollar1, err := json.Marshal(pipelines) - if err != nil { - return err - } - _, err = r.queries.CheckPipelines(ctx, dollar1) - if err != nil { - return err - } + //dollar1, err := json.Marshal(pipelines) + //if err != nil { + // return err + //} + //_, err = r.queries.CheckPipelines(ctx, dollar1) + //if err != nil { + // return err + //} return nil } @@ -356,15 +356,15 @@ func (r *AmoRepository) UpdateListSteps(ctx context.Context) error { } func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) error { - dollar1, err := json.Marshal(steps) - if err != nil { - return err - } - - _, err = r.queries.CheckSteps(ctx, dollar1) - if err != nil { - return err - } + //dollar1, err := json.Marshal(steps) + //if err != nil { + // return err + //} + // + //_, err = r.queries.CheckSteps(ctx, dollar1) + //if err != nil { + // return err + //} return nil } @@ -425,11 +425,43 @@ func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID return err } - _, err = r.queries.CheckTags(ctx, sqlcgen.CheckTagsParams{ + rows, err := r.queries.CheckTags(ctx, sqlcgen.CheckTagsParams{ Accountid: tokenID, Column2: column2, }) + if rows != nil { + var toUpdate []model.Tag + for _, row := range rows { + var entity model.EntityType + if v, ok := row.Entity.(string); ok { + entity = model.EntityType(v) + } else { + fmt.Println("unexpected type for EntityType:", row.Entity) + } + to := model.Tag{ + ID: row.ID, + Amoid: row.Amoid, + Accountid: row.Accountid, + Entity: entity, + Name: row.Name, + Color: &row.Color, + Createdat: row.Createdat.Time.Unix(), + } + 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 + } + } + if err != nil { return err } @@ -490,19 +522,19 @@ func (r *AmoRepository) UpdateListCustom(ctx context.Context) error { } func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, tokenID string) error { - column2, err := json.Marshal(fields) - if err != nil { - return err - } - - _, err = r.queries.CheckFields(ctx, sqlcgen.CheckFieldsParams{ - Accountid: tokenID, - Column2: column2, - }) - - if err != nil { - return err - } + //column2, err := json.Marshal(fields) + //if err != nil { + // return err + //} + // + //_, err = r.queries.CheckFields(ctx, sqlcgen.CheckFieldsParams{ + // Accountid: tokenID, + // Column2: column2, + //}) + // + //if err != nil { + // return err + //} return nil } From 3960a1a5d5440e6e954ff773643b1d263a631873 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 18 Apr 2024 23:35:30 +0300 Subject: [PATCH 038/187] update --- repository/amo/amo.go | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index c42b03b..af42dee 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -4,7 +4,6 @@ import ( "context" "database/sql" "encoding/json" - "fmt" "github.com/sqlc-dev/pqtype" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" @@ -386,11 +385,8 @@ func (r *AmoRepository) GetTagsWithPagination(ctx context.Context, req *model.Pa count = row.TotalCount var entity model.EntityType - if v, ok := row.Entity.(string); ok { - entity = model.EntityType(v) - } else { - fmt.Println("unexpected type for EntityType:", row.Entity) - } + v := string(row.Entity.([]byte)) + entity = model.EntityType(v) tag := model.Tag{ ID: row.ID, @@ -434,11 +430,8 @@ func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID var toUpdate []model.Tag for _, row := range rows { var entity model.EntityType - if v, ok := row.Entity.(string); ok { - entity = model.EntityType(v) - } else { - fmt.Println("unexpected type for EntityType:", row.Entity) - } + v := string(row.Entity.([]byte)) + entity = model.EntityType(v) to := model.Tag{ ID: row.ID, Amoid: row.Amoid, @@ -486,11 +479,8 @@ func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model. count = row.TotalCount var entity model.EntityType - if v, ok := row.Entity.(string); ok { - entity = model.EntityType(v) - } else { - fmt.Println("unexpected type for EntityType:", row.Entity) - } + v := string(row.Entity.([]byte)) + entity = model.EntityType(v) field := model.Field{ ID: row.ID, From 92cf86e26b75d502b9a1e0aa7f3581d579161495 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 19 Apr 2024 10:59:51 +0300 Subject: [PATCH 039/187] rework query --- dal/db_query/queries.sql | 122 +++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 50 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 61b5171..6f9bf10 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -762,54 +762,76 @@ WITH user_data AS ( ) SELECT * FROM inserted_tags; +-- name: CheckPipelines :many +WITH new_pipelines AS ( + SELECT (pipeline->>'AmoID')::INT, + (pipeline->>'AccountID')::INT, + COALESCE(pipeline->>'Name', '')::varchar(50), + CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, + CURRENT_TIMESTAMP + FROM json_array_elements($1::json) AS pipeline +), inserted_pipelines AS( + INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) + SELECT np.amoID, + np.accountID, + np.name, + np.isArchive, + CURRENT_TIMESTAMP + FROM new_pipelines np + ON CONFLICT (amoID, accountID) DO NOTHING + RETURNING * +) +SELECT * FROM inserted_pipelines; --- -- name: CheckPipelines :many --- WITH insertt AS ( --- INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) --- SELECT (new_pipelines->>'AmoID')::INT, --- (new_pipelines->>'AccountID')::INT, --- COALESCE(new_pipelines->>'Name', '')::varchar(50), --- CASE WHEN (new_pipelines->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, --- CURRENT_TIMESTAMP --- FROM json_array_elements($1::json) AS new_pipelines --- ON CONFLICT (amoID, accountID) DO NOTHING --- RETURNING EXCLUDED.* --- ) --- SELECT * FROM insertt; --- --- -- name: CheckFields :many --- WITH user_data AS ( --- SELECT AmoID --- FROM users --- WHERE users.AccountID = $1 --- ), --- insertt AS ( --- INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) --- SELECT (new_fields->>'AmoID')::INT, --- COALESCE(new_fields->>'Code', '')::varchar(255), --- user_data.AmoID, --- COALESCE(new_fields->>'Name', '')::varchar(50), --- CAST(new_fields->>'Entity' AS entitytype), --- COALESCE(new_fields->>'Type', '')::varchar(50), --- CURRENT_TIMESTAMP --- FROM json_array_elements($2::json) AS new_fields --- JOIN user_data ON true --- ON CONFLICT (amoID, accountID, entity) DO NOTHING --- RETURNING EXCLUDED.* --- ) --- SELECT * FROM insertt; --- --- -- name: CheckSteps :many --- WITH insertt AS ( --- INSERT INTO steps (amoID, pipelineID, accountID, name, color, createdAt) --- SELECT (new_steps->>'AmoID')::INT, --- (new_steps->>'PipelineID')::INT, --- (new_steps->>'AccountID')::INT, --- COALESCE(new_steps->>'Name', '')::varchar(50), --- COALESCE(new_steps->>'Color', '')::varchar(50), --- CURRENT_TIMESTAMP --- FROM json_array_elements($1::json) AS new_steps --- ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING --- RETURNING EXCLUDED.* --- ) --- SELECT * FROM insertt; +-- name: CheckFields :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +), new_fields AS ( + SELECT (field->>'AmoID')::INT, + COALESCE(field->>'Code', '')::varchar(255), + COALESCE(field->>'Name', '')::varchar(50), + CAST(field->>'Entity' AS entitytype), + COALESCE(field->>'Type', '')::varchar(50), + CURRENT_TIMESTAMP + FROM json_array_elements($2::json) AS field +), inserted_fields AS( + INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) + SELECT nf.amoID, + nf.code, + ud.AmoID, + nf.name, + nf.Entity, + nf.type, + CURRENT_TIMESTAMP + FROM new_fields nf + JOIN user_data ud ON true + ON CONFLICT (amoID, accountID, entity) DO NOTHING + RETURNING * +) +SELECT * from inserted_fields; + + +-- name: CheckSteps :many +WITH new_steps AS ( + SELECT (step->>'AmoID')::INT, + (step->>'PipelineID')::INT, + (step->>'AccountID')::INT, + COALESCE(step->>'Name', '')::varchar(50), + COALESCE(step->>'Color', '')::varchar(50), + CURRENT_TIMESTAMP + FROM json_array_elements($1::json) AS step +), inserted_steps AS ( + INSERT INTO steps (amoID, pipelineID, accountID, name, color, createdAt) + SELECT ns.amoID, + ns.pipelineID, + ns.accountID, + ns.name, + ns.color, + CURRENT_TIMESTAMP + FROM new_steps ns + ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING + RETURNING * +) +SELECT * FROM inserted_steps; From 582afc3395070ca97d17da9c291815b7421a4f64 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 19 Apr 2024 11:02:20 +0300 Subject: [PATCH 040/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 210 +++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index ca5f117..e1be252 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -167,6 +167,149 @@ func (q *Queries) CheckExpired(ctx context.Context) ([]Token, error) { return items, nil } +const checkFields = `-- name: CheckFields :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +), new_fields AS ( + SELECT (field->>'AmoID')::INT, + COALESCE(field->>'Code', '')::varchar(255), + COALESCE(field->>'Name', '')::varchar(50), + CAST(field->>'Entity' AS entitytype), + COALESCE(field->>'Type', '')::varchar(50), + CURRENT_TIMESTAMP + FROM json_array_elements($2::json) AS field +), inserted_fields AS( + INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) + SELECT nf.amoID, + nf.code, + ud.AmoID, + nf.name, + nf.Entity, + nf.type, + CURRENT_TIMESTAMP + FROM new_fields nf + JOIN user_data ud ON true + ON CONFLICT (amoID, accountID, entity) DO NOTHING + RETURNING id, amoid, code, accountid, name, entity, type, deleted, createdat +) +SELECT id, amoid, code, accountid, name, entity, type, deleted, createdat from inserted_fields +` + +type CheckFieldsParams struct { + Accountid string `db:"accountid" json:"accountid"` + Column2 json.RawMessage `db:"column_2" json:"column_2"` +} + +type CheckFieldsRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Code string `db:"code" json:"code"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Entity interface{} `db:"entity" json:"entity"` + Type string `db:"type" json:"type"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]CheckFieldsRow, error) { + rows, err := q.db.QueryContext(ctx, checkFields, arg.Accountid, arg.Column2) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CheckFieldsRow + for rows.Next() { + var i CheckFieldsRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Code, + &i.Accountid, + &i.Name, + &i.Entity, + &i.Type, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const checkPipelines = `-- name: CheckPipelines :many +WITH new_pipelines AS ( + SELECT (pipeline->>'AmoID')::INT, + (pipeline->>'AccountID')::INT, + COALESCE(pipeline->>'Name', '')::varchar(50), + CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, + CURRENT_TIMESTAMP + FROM json_array_elements($1::json) AS pipeline +), inserted_pipelines AS( + INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) + SELECT np.amoID, + np.accountID, + np.name, + np.isArchive, + CURRENT_TIMESTAMP + FROM new_pipelines np + ON CONFLICT (amoID, accountID) DO NOTHING + RETURNING id, amoid, accountid, name, isarchive, deleted, createdat +) +SELECT id, amoid, accountid, name, isarchive, deleted, createdat FROM inserted_pipelines +` + +type CheckPipelinesRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Isarchive bool `db:"isarchive" json:"isarchive"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +func (q *Queries) CheckPipelines(ctx context.Context, dollar_1 json.RawMessage) ([]CheckPipelinesRow, error) { + rows, err := q.db.QueryContext(ctx, checkPipelines, dollar_1) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CheckPipelinesRow + for rows.Next() { + var i CheckPipelinesRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Accountid, + &i.Name, + &i.Isarchive, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const checkResultOwner = `-- name: CheckResultOwner :one SELECT q.accountid FROM answer a JOIN quiz q ON a.quiz_id = q.id WHERE a.id = $1 AND a.deleted = FALSE AND a.start = false ` @@ -213,6 +356,73 @@ func (q *Queries) CheckResultsOwner(ctx context.Context, arg CheckResultsOwnerPa return items, nil } +const checkSteps = `-- name: CheckSteps :many +WITH new_steps AS ( + SELECT (step->>'AmoID')::INT, + (step->>'PipelineID')::INT, + (step->>'AccountID')::INT, + COALESCE(step->>'Name', '')::varchar(50), + COALESCE(step->>'Color', '')::varchar(50), + CURRENT_TIMESTAMP + FROM json_array_elements($1::json) AS step +), inserted_steps AS ( + INSERT INTO steps (amoID, pipelineID, accountID, name, color, createdAt) + SELECT ns.amoID, + ns.pipelineID, + ns.accountID, + ns.name, + ns.color, + CURRENT_TIMESTAMP + FROM new_steps ns + ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING + RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat +) +SELECT id, amoid, pipelineid, accountid, name, color, deleted, createdat FROM inserted_steps +` + +type CheckStepsRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +func (q *Queries) CheckSteps(ctx context.Context, dollar_1 json.RawMessage) ([]CheckStepsRow, error) { + rows, err := q.db.QueryContext(ctx, checkSteps, dollar_1) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CheckStepsRow + for rows.Next() { + var i CheckStepsRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Pipelineid, + &i.Accountid, + &i.Name, + &i.Color, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const checkTags = `-- name: CheckTags :many WITH user_data AS ( SELECT AmoID From 630c321e514c710b9de15cf2b5fd05f7955af0b2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 19 Apr 2024 11:19:11 +0300 Subject: [PATCH 041/187] update repo methods --- repository/amo/amo.go | 138 +++++++++++++++++++++++++++++++++--------- 1 file changed, 108 insertions(+), 30 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index af42dee..236b3f8 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -299,14 +299,38 @@ func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *mod } func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pipeline) error { - //dollar1, err := json.Marshal(pipelines) - //if err != nil { - // return err - //} - //_, err = r.queries.CheckPipelines(ctx, dollar1) - //if err != nil { - // return err - //} + 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{ + ID: row.ID, + Amoid: row.Amoid, + AccountID: row.Accountid, + Name: row.Name, + Isarchive: row.Isarchive, + Createdat: row.Createdat.Time.Unix(), + } + 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 + } + } return nil } @@ -355,15 +379,41 @@ func (r *AmoRepository) UpdateListSteps(ctx context.Context) error { } func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) error { - //dollar1, err := json.Marshal(steps) - //if err != nil { - // return err - //} - // - //_, err = r.queries.CheckSteps(ctx, dollar1) - //if err != nil { - // return err - //} + 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{ + ID: row.ID, + Amoid: row.Amoid, + Pipelineid: row.Pipelineid, + Accountid: row.Accountid, + Name: row.Name, + Color: row.Color, + Createdat: row.Createdat.Time.Unix(), + } + 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 + } + } return nil } @@ -512,19 +562,47 @@ func (r *AmoRepository) UpdateListCustom(ctx context.Context) error { } func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, tokenID string) error { - //column2, err := json.Marshal(fields) - //if err != nil { - // return err - //} - // - //_, err = r.queries.CheckFields(ctx, sqlcgen.CheckFieldsParams{ - // Accountid: tokenID, - // Column2: column2, - //}) - // - //if err != nil { - // return err - //} + 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{ + ID: row.ID, + Amoid: row.Amoid, + Code: row.Code, + Accountid: row.Accountid, + Entity: entity, + Type: row.Type, + Createdat: row.Createdat.Time.Unix(), + } + 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 + } + } return nil } From f1be15ae1b321358812926530cebe0b29e64eb7d Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 19 Apr 2024 11:30:09 +0300 Subject: [PATCH 042/187] update queries --- dal/db_query/queries.sql | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 6f9bf10..faaf6dd 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -764,11 +764,11 @@ SELECT * FROM inserted_tags; -- name: CheckPipelines :many WITH new_pipelines AS ( - SELECT (pipeline->>'AmoID')::INT, - (pipeline->>'AccountID')::INT, - COALESCE(pipeline->>'Name', '')::varchar(50), - CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, - CURRENT_TIMESTAMP + SELECT (pipeline->>'AmoID')::INT AS amoID, + (pipeline->>'AccountID')::INT AS accountID, + COALESCE(pipeline->>'Name', '')::varchar(50) AS name, + CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END AS isArchive, + CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS pipeline ), inserted_pipelines AS( INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) @@ -776,7 +776,7 @@ WITH new_pipelines AS ( np.accountID, np.name, np.isArchive, - CURRENT_TIMESTAMP + np.createdAt FROM new_pipelines np ON CONFLICT (amoID, accountID) DO NOTHING RETURNING * @@ -789,12 +789,12 @@ WITH user_data AS ( FROM users WHERE users.AccountID = $1 ), new_fields AS ( - SELECT (field->>'AmoID')::INT, - COALESCE(field->>'Code', '')::varchar(255), - COALESCE(field->>'Name', '')::varchar(50), - CAST(field->>'Entity' AS entitytype), - COALESCE(field->>'Type', '')::varchar(50), - CURRENT_TIMESTAMP + SELECT (field->>'AmoID')::INT AS amoID, + COALESCE(field->>'Code', '')::varchar(255) AS code, + COALESCE(field->>'Name', '')::varchar(50) AS name, + CAST(field->>'Entity' AS entitytype) AS Entity, + COALESCE(field->>'Type', '')::varchar(50) AS type, + CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($2::json) AS field ), inserted_fields AS( INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) @@ -804,7 +804,7 @@ WITH user_data AS ( nf.name, nf.Entity, nf.type, - CURRENT_TIMESTAMP + nf.createdAt FROM new_fields nf JOIN user_data ud ON true ON CONFLICT (amoID, accountID, entity) DO NOTHING @@ -815,12 +815,12 @@ SELECT * from inserted_fields; -- name: CheckSteps :many WITH new_steps AS ( - SELECT (step->>'AmoID')::INT, - (step->>'PipelineID')::INT, - (step->>'AccountID')::INT, - COALESCE(step->>'Name', '')::varchar(50), - COALESCE(step->>'Color', '')::varchar(50), - CURRENT_TIMESTAMP + SELECT (step->>'AmoID')::INT AS amoID, + (step->>'PipelineID')::INT AS pipelineID, + (step->>'AccountID')::INT AS accountID, + COALESCE(step->>'Name', '')::varchar(50) AS name, + COALESCE(step->>'Color', '')::varchar(50) AS color, + CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS step ), inserted_steps AS ( INSERT INTO steps (amoID, pipelineID, accountID, name, color, createdAt) @@ -829,7 +829,7 @@ WITH new_steps AS ( ns.accountID, ns.name, ns.color, - CURRENT_TIMESTAMP + ns.createdAt FROM new_steps ns ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING RETURNING * From b6b37bac246c14a840d0ba313d4910e17b5a9f67 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 19 Apr 2024 11:31:54 +0300 Subject: [PATCH 043/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 40 +++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index e1be252..0a7e074 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -173,12 +173,12 @@ WITH user_data AS ( FROM users WHERE users.AccountID = $1 ), new_fields AS ( - SELECT (field->>'AmoID')::INT, - COALESCE(field->>'Code', '')::varchar(255), - COALESCE(field->>'Name', '')::varchar(50), - CAST(field->>'Entity' AS entitytype), - COALESCE(field->>'Type', '')::varchar(50), - CURRENT_TIMESTAMP + SELECT (field->>'AmoID')::INT AS amoID, + COALESCE(field->>'Code', '')::varchar(255) AS code, + COALESCE(field->>'Name', '')::varchar(50) AS name, + CAST(field->>'Entity' AS entitytype) AS Entity, + COALESCE(field->>'Type', '')::varchar(50) AS type, + CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($2::json) AS field ), inserted_fields AS( INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) @@ -188,7 +188,7 @@ WITH user_data AS ( nf.name, nf.Entity, nf.type, - CURRENT_TIMESTAMP + nf.createdAt FROM new_fields nf JOIN user_data ud ON true ON CONFLICT (amoID, accountID, entity) DO NOTHING @@ -249,11 +249,11 @@ func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]Che const checkPipelines = `-- name: CheckPipelines :many WITH new_pipelines AS ( - SELECT (pipeline->>'AmoID')::INT, - (pipeline->>'AccountID')::INT, - COALESCE(pipeline->>'Name', '')::varchar(50), - CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END, - CURRENT_TIMESTAMP + SELECT (pipeline->>'AmoID')::INT AS amoID, + (pipeline->>'AccountID')::INT AS accountID, + COALESCE(pipeline->>'Name', '')::varchar(50) AS name, + CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END AS isArchive, + CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS pipeline ), inserted_pipelines AS( INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) @@ -261,7 +261,7 @@ WITH new_pipelines AS ( np.accountID, np.name, np.isArchive, - CURRENT_TIMESTAMP + np.createdAt FROM new_pipelines np ON CONFLICT (amoID, accountID) DO NOTHING RETURNING id, amoid, accountid, name, isarchive, deleted, createdat @@ -358,12 +358,12 @@ func (q *Queries) CheckResultsOwner(ctx context.Context, arg CheckResultsOwnerPa const checkSteps = `-- name: CheckSteps :many WITH new_steps AS ( - SELECT (step->>'AmoID')::INT, - (step->>'PipelineID')::INT, - (step->>'AccountID')::INT, - COALESCE(step->>'Name', '')::varchar(50), - COALESCE(step->>'Color', '')::varchar(50), - CURRENT_TIMESTAMP + SELECT (step->>'AmoID')::INT AS amoID, + (step->>'PipelineID')::INT AS pipelineID, + (step->>'AccountID')::INT AS accountID, + COALESCE(step->>'Name', '')::varchar(50) AS name, + COALESCE(step->>'Color', '')::varchar(50) AS color, + CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS step ), inserted_steps AS ( INSERT INTO steps (amoID, pipelineID, accountID, name, color, createdAt) @@ -372,7 +372,7 @@ WITH new_steps AS ( ns.accountID, ns.name, ns.color, - CURRENT_TIMESTAMP + ns.createdAt FROM new_steps ns ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat From fa0a053add5c2eaaf9d4e878c8b31a653a02af36 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 19 Apr 2024 11:54:50 +0300 Subject: [PATCH 044/187] fixed timezone --- repository/amo/amo.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 236b3f8..a8d6dd7 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -175,7 +175,7 @@ func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) e Refreshtoken: tokens.RefreshToken, Accesstoken: tokens.AccessToken, Authcode: tokens.AuthCode, - Expiration: time.Unix(tokens.Expiration, 0).In(time.UTC), + Expiration: time.Unix(tokens.Expiration, 0).In(time.Unix(tokens.CreatedAt, 0).Location()), Createdat: sql.NullTime{Time: time.Unix(tokens.CreatedAt, 0), Valid: true}, }) @@ -191,7 +191,7 @@ func (r *AmoRepository) WebhookUpdate(ctx context.Context, tokens model.Token) e Accountid: tokens.AccountID, Accesstoken: tokens.AccessToken, Refreshtoken: tokens.RefreshToken, - Expiration: time.Unix(tokens.Expiration, 0).In(time.UTC), + Expiration: time.Unix(tokens.Expiration, 0).In(time.Unix(tokens.CreatedAt, 0).Location()), Createdat: sql.NullTime{Time: time.Unix(tokens.CreatedAt, 0), Valid: true}, }) From 64e45ebed8aeae576ee38afd2a610e37dd9ceaf1 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 19 Apr 2024 17:41:25 +0300 Subject: [PATCH 045/187] fix token value size --- dal/schema/000010_init.up.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 44a7c2d..bca7e83 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -1,8 +1,8 @@ CREATE TABLE IF NOT EXISTS tokens ( AccountID VARCHAR(30) PRIMARY KEY, -- связь с users AccountID неявная посредством join - RefreshToken VARCHAR(512) NOT NULL , - AccessToken VARCHAR(512) NOT NULL , - AuthCode VARCHAR(512) NOT NULL , -- код авторизации который получаем при вебхук + RefreshToken TEXT NOT NULL , + AccessToken TEXT NOT NULL , + AuthCode TEXT NOT NULL , -- код авторизации который получаем при вебхук Expiration TIMESTAMP NOT NULL, -- время истечения токенов CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); From 775d9d32cf34a709df71a6faa3ab91aa7d715827 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 20 Apr 2024 11:59:16 +0300 Subject: [PATCH 046/187] update schema --- dal/db_query/queries.sql | 10 +++++++++- dal/schema/000010_init.down.sql | 1 + dal/schema/000010_init.up.sql | 7 ++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index faaf6dd..f236c1b 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -678,7 +678,7 @@ UPDATE users SET Deleted = TRUE WHERE AccountID = $1; -- name: GetCurrentAccount :one SELECT * FROM users WHERE AccountID = $1; --- name: CheckUsers :exec +-- name: CheckMainUser :exec UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5; -- name: GetUsersWithPagination :many @@ -835,3 +835,11 @@ WITH new_steps AS ( RETURNING * ) SELECT * FROM inserted_steps; + +-- name: CheckUsers :exec +INSERT INTO users (AmoID, Name, Email, Role, "Group", AmoUserID) +VALUES ($1, $2, $3, $4, $5, $6) +ON CONFLICT (AmoID) DO NOTHING; + +-- name: UpdateUsers :exec +UPDATE users SET Name = $2, Email = $3, Role = $4, "Group" = $5, AmoUserID = $6 WHERE AmoID = $1; \ No newline at end of file diff --git a/dal/schema/000010_init.down.sql b/dal/schema/000010_init.down.sql index 75a69ea..43fe0a6 100644 --- a/dal/schema/000010_init.down.sql +++ b/dal/schema/000010_init.down.sql @@ -1,3 +1,4 @@ +DROP INDEX IF EXISTS idx_unique_users; DROP INDEX IF EXISTS idx_unique_pipeline; DROP INDEX IF EXISTS idx_unique_step; DROP INDEX IF EXISTS idx_unique_field; diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index bca7e83..167f8ce 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -9,12 +9,12 @@ CREATE TABLE IF NOT EXISTS tokens ( CREATE TABLE IF NOT EXISTS users ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, - AccountID VARCHAR(30) NOT NULL , -- id квизе из токена + AccountID VARCHAR(30) NOT NULL DEFAULT '', -- id квизе из токена AmoID INT NOT NULL , -- id в амо Name VARCHAR(50) NOT NULL DEFAULT '', -- имя в амо Email VARCHAR(50) NOT NULL DEFAULT '', -- почта в амо - Role VARCHAR(50) NOT NULL DEFAULT '', -- роль в амо - "Group" JSONB, -- вложенная структура так как в амо группы хранятся массивом структур + Role INT NOT NULL DEFAULT 0, -- роль в амо + "Group" INT NOT NULL DEFAULT 0, -- вложенная структура так как в амо группы хранятся массивом структур Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, Subdomain VARCHAR(50) NOT NULL DEFAULT '', @@ -78,6 +78,7 @@ ADD COLUMN utm jsonb NOT NULL DEFAULT '{}'; ALTER TABLE quiz ADD COLUMN rules jsonb NOT NULL DEFAULT '{}'; +CREATE UNIQUE INDEX idx_unique_users ON users (amoID); CREATE UNIQUE INDEX idx_unique_pipeline ON pipelines (amoID, accountID); CREATE UNIQUE INDEX idx_unique_step ON steps (amoID, accountID, PipelineID); CREATE UNIQUE INDEX idx_unique_field ON fields (amoID, accountID, entity); From 9a0b7b45cf88905c1e8756e156e6f383023d6860 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 20 Apr 2024 12:01:25 +0300 Subject: [PATCH 047/187] update sqlc gen --- dal/sqlcgen/models.go | 25 ++++---- dal/sqlcgen/queries.sql.go | 117 ++++++++++++++++++++++++++----------- 2 files changed, 96 insertions(+), 46 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index f1d891e..6af78b9 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -10,7 +10,6 @@ import ( "time" "github.com/google/uuid" - "github.com/sqlc-dev/pqtype" ) type Account struct { @@ -154,16 +153,16 @@ type Token struct { } type User struct { - ID int64 `db:"id" json:"id"` - Accountid string `db:"accountid" json:"accountid"` - Amoid int32 `db:"amoid" json:"amoid"` - Name string `db:"name" json:"name"` - Email string `db:"email" json:"email"` - Role string `db:"role" json:"role"` - Group pqtype.NullRawMessage `db:"Group" json:"Group"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` - Subdomain string `db:"subdomain" json:"subdomain"` - Amouserid int32 `db:"amouserid" json:"amouserid"` - Country string `db:"country" json:"country"` + ID int64 `db:"id" json:"id"` + Accountid string `db:"accountid" json:"accountid"` + Amoid int32 `db:"amoid" json:"amoid"` + Name string `db:"name" json:"name"` + Email string `db:"email" json:"email"` + Role int32 `db:"role" json:"role"` + Group int32 `db:"Group" json:"Group"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + Subdomain string `db:"subdomain" json:"subdomain"` + Amouserid int32 `db:"amouserid" json:"amouserid"` + Country string `db:"country" json:"country"` } diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 0a7e074..3b7f96a 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -13,7 +13,6 @@ import ( "github.com/google/uuid" "github.com/lib/pq" - "github.com/sqlc-dev/pqtype" ) const accountPagination = `-- name: AccountPagination :many @@ -247,6 +246,29 @@ func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]Che return items, nil } +const checkMainUser = `-- name: CheckMainUser :exec +UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5 +` + +type CheckMainUserParams struct { + Name string `db:"name" json:"name"` + Group int32 `db:"Group" json:"Group"` + Email string `db:"email" json:"email"` + Role int32 `db:"role" json:"role"` + Amoid int32 `db:"amoid" json:"amoid"` +} + +func (q *Queries) CheckMainUser(ctx context.Context, arg CheckMainUserParams) error { + _, err := q.db.ExecContext(ctx, checkMainUser, + arg.Name, + arg.Group, + arg.Email, + arg.Role, + arg.Amoid, + ) + return err +} + const checkPipelines = `-- name: CheckPipelines :many WITH new_pipelines AS ( SELECT (pipeline->>'AmoID')::INT AS amoID, @@ -499,24 +521,28 @@ func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]CheckTa } const checkUsers = `-- name: CheckUsers :exec -UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5 +INSERT INTO users (AmoID, Name, Email, Role, "Group", AmoUserID) +VALUES ($1, $2, $3, $4, $5, $6) +ON CONFLICT (AmoID) DO NOTHING ` type CheckUsersParams struct { - Name string `db:"name" json:"name"` - Group pqtype.NullRawMessage `db:"Group" json:"Group"` - Email string `db:"email" json:"email"` - Role string `db:"role" json:"role"` - Amoid int32 `db:"amoid" json:"amoid"` + Amoid int32 `db:"amoid" json:"amoid"` + Name string `db:"name" json:"name"` + Email string `db:"email" json:"email"` + Role int32 `db:"role" json:"role"` + Group int32 `db:"Group" json:"Group"` + Amouserid int32 `db:"amouserid" json:"amouserid"` } func (q *Queries) CheckUsers(ctx context.Context, arg CheckUsersParams) error { _, err := q.db.ExecContext(ctx, checkUsers, + arg.Amoid, arg.Name, - arg.Group, arg.Email, arg.Role, - arg.Amoid, + arg.Group, + arg.Amouserid, ) return err } @@ -663,17 +689,17 @@ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) ` type CreateAmoAccountParams struct { - Accountid string `db:"accountid" json:"accountid"` - Amoid int32 `db:"amoid" json:"amoid"` - Name string `db:"name" json:"name"` - Email string `db:"email" json:"email"` - Role string `db:"role" json:"role"` - Group pqtype.NullRawMessage `db:"Group" json:"Group"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` - Subdomain string `db:"subdomain" json:"subdomain"` - Amouserid int32 `db:"amouserid" json:"amouserid"` - Country string `db:"country" json:"country"` + Accountid string `db:"accountid" json:"accountid"` + Amoid int32 `db:"amoid" json:"amoid"` + Name string `db:"name" json:"name"` + Email string `db:"email" json:"email"` + Role int32 `db:"role" json:"role"` + Group int32 `db:"Group" json:"Group"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + Subdomain string `db:"subdomain" json:"subdomain"` + Amouserid int32 `db:"amouserid" json:"amouserid"` + Country string `db:"country" json:"country"` } // amo methods: @@ -2100,19 +2126,19 @@ type GetUsersWithPaginationParams struct { } type GetUsersWithPaginationRow struct { - ID int64 `db:"id" json:"id"` - Accountid string `db:"accountid" json:"accountid"` - Amoid int32 `db:"amoid" json:"amoid"` - Name string `db:"name" json:"name"` - Email string `db:"email" json:"email"` - Role string `db:"role" json:"role"` - Group pqtype.NullRawMessage `db:"Group" json:"Group"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` - Subdomain string `db:"subdomain" json:"subdomain"` - Amouserid int32 `db:"amouserid" json:"amouserid"` - Country string `db:"country" json:"country"` - TotalCount int64 `db:"total_count" json:"total_count"` + ID int64 `db:"id" json:"id"` + Accountid string `db:"accountid" json:"accountid"` + Amoid int32 `db:"amoid" json:"amoid"` + Name string `db:"name" json:"name"` + Email string `db:"email" json:"email"` + Role int32 `db:"role" json:"role"` + Group int32 `db:"Group" json:"Group"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + Subdomain string `db:"subdomain" json:"subdomain"` + Amouserid int32 `db:"amouserid" json:"amouserid"` + Country string `db:"country" json:"country"` + TotalCount int64 `db:"total_count" json:"total_count"` } func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPaginationParams) ([]GetUsersWithPaginationRow, error) { @@ -2720,6 +2746,31 @@ func (q *Queries) UpdateTags(ctx context.Context, dollar_1 json.RawMessage) erro return err } +const updateUsers = `-- name: UpdateUsers :exec +UPDATE users SET Name = $2, Email = $3, Role = $4, "Group" = $5, AmoUserID = $6 WHERE AmoID = $1 +` + +type UpdateUsersParams struct { + Amoid int32 `db:"amoid" json:"amoid"` + Name string `db:"name" json:"name"` + Email string `db:"email" json:"email"` + Role int32 `db:"role" json:"role"` + Group int32 `db:"Group" json:"Group"` + Amouserid int32 `db:"amouserid" json:"amouserid"` +} + +func (q *Queries) UpdateUsers(ctx context.Context, arg UpdateUsersParams) error { + _, err := q.db.ExecContext(ctx, updateUsers, + arg.Amoid, + arg.Name, + arg.Email, + arg.Role, + arg.Group, + arg.Amouserid, + ) + return err +} + const webhookDelete = `-- name: WebhookDelete :exec DELETE FROM tokens WHERE AccountID = $1 ` From 8818afa463bcb90db7c9eeb93e385557be1598a7 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 20 Apr 2024 12:13:57 +0300 Subject: [PATCH 048/187] update amo repo methods --- model/amo.go | 4 +-- repository/amo/amo.go | 71 ++++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/model/amo.go b/model/amo.go index 5fde7e9..858b1c7 100644 --- a/model/amo.go +++ b/model/amo.go @@ -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"` /* - таймштамп создания аккаунта*/ diff --git a/repository/amo/amo.go b/repository/amo/amo.go index a8d6dd7..012f8f1 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -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 From 34628bcc93eb9815dddb2219b8e1855d55c961be Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 20 Apr 2024 12:14:24 +0300 Subject: [PATCH 049/187] update amo repo methods --- repository/amo/amo.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 012f8f1..ca92c81 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -138,6 +138,8 @@ func (r *AmoRepository) CheckMainUser(ctx context.Context, user model.User) erro if err != nil { return err } + + return nil } func (r *AmoRepository) CheckAndUpdateUsers(ctx context.Context, user model.User) error { From 759ec37def86bb508b23c3ea11a7abd6bdccd133 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 20 Apr 2024 12:22:38 +0300 Subject: [PATCH 050/187] update sqlc gen --- dal/db_query/queries.sql | 19 +++++++++++++++++-- dal/sqlcgen/queries.sql.go | 35 ++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index f236c1b..99c7463 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -661,7 +661,22 @@ INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, VALUES ($1, $2, $3, $4, $5, $6); -- name: WebhookUpdate :exec -UPDATE tokens SET AccessToken = $1, RefreshToken = $2, Expiration = $3, CreatedAt = $4 WHERE AccountID = $5; +UPDATE tokens +SET + AccessToken = updated_data.AccessToken, + RefreshToken = updated_data.RefreshToken, + Expiration = updated_data.Expiration, + CreatedAt = updated_data.CreatedAt +FROM + jsonb_to_recordset($1::jsonb) AS updated_data( + AccountID VARCHAR(30), + AccessToken TEXT, + RefreshToken TEXT, + Expiration TIMESTAMP, + CreatedAt TIMESTAMP +) +WHERE + tokens.AccountID = updated_data.AccountID; -- name: GetAllTokens :many SELECT * FROM tokens; @@ -842,4 +857,4 @@ VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (AmoID) DO NOTHING; -- name: UpdateUsers :exec -UPDATE users SET Name = $2, Email = $3, Role = $4, "Group" = $5, AmoUserID = $6 WHERE AmoID = $1; \ No newline at end of file +UPDATE users SET Name = $2, Email = $3, Role = $4, "Group" = $5, AmoUserID = $6 WHERE AmoID = $1; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 3b7f96a..7e76f82 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2781,25 +2781,26 @@ func (q *Queries) WebhookDelete(ctx context.Context, accountid string) error { } const webhookUpdate = `-- name: WebhookUpdate :exec -UPDATE tokens SET AccessToken = $1, RefreshToken = $2, Expiration = $3, CreatedAt = $4 WHERE AccountID = $5 +UPDATE tokens +SET + AccessToken = updated_data.AccessToken, + RefreshToken = updated_data.RefreshToken, + Expiration = updated_data.Expiration, + CreatedAt = updated_data.CreatedAt +FROM + jsonb_to_recordset($1::jsonb) AS updated_data( + AccountID VARCHAR(30), + AccessToken TEXT, + RefreshToken TEXT, + Expiration TIMESTAMP, + CreatedAt TIMESTAMP +) +WHERE + tokens.AccountID = updated_data.AccountID ` -type WebhookUpdateParams struct { - Accesstoken string `db:"accesstoken" json:"accesstoken"` - Refreshtoken string `db:"refreshtoken" json:"refreshtoken"` - Expiration time.Time `db:"expiration" json:"expiration"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` - Accountid string `db:"accountid" json:"accountid"` -} - -func (q *Queries) WebhookUpdate(ctx context.Context, arg WebhookUpdateParams) error { - _, err := q.db.ExecContext(ctx, webhookUpdate, - arg.Accesstoken, - arg.Refreshtoken, - arg.Expiration, - arg.Createdat, - arg.Accountid, - ) +func (q *Queries) WebhookUpdate(ctx context.Context, dollar_1 json.RawMessage) error { + _, err := q.db.ExecContext(ctx, webhookUpdate, dollar_1) return err } From bd471a64c94b87cdd7a8d419f91067ceb95bd349 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 12:17:18 +0300 Subject: [PATCH 051/187] update amo repo --- repository/amo/amo.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index ca92c81..7a60053 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -189,15 +189,13 @@ func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) e return nil } -func (r *AmoRepository) WebhookUpdate(ctx context.Context, tokens model.Token) error { - err := r.queries.WebhookUpdate(ctx, sqlcgen.WebhookUpdateParams{ - Accountid: tokens.AccountID, - Accesstoken: tokens.AccessToken, - Refreshtoken: tokens.RefreshToken, - Expiration: time.Unix(tokens.Expiration, 0).In(time.Unix(tokens.CreatedAt, 0).Location()), - Createdat: sql.NullTime{Time: time.Unix(tokens.CreatedAt, 0), Valid: true}, - }) +func (r *AmoRepository) WebhookUpdate(ctx context.Context, tokens []model.Token) error { + dollar1, err := json.Marshal(tokens) + if err != nil { + return err + } + err = r.queries.WebhookUpdate(ctx, dollar1) if err != nil { return err } From 17590bde8e9d9b55b74236483ccb72056e6cfd0b Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 13:16:20 +0300 Subject: [PATCH 052/187] update query --- dal/db_query/queries.sql | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 99c7463..0ada38f 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -661,22 +661,13 @@ INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, VALUES ($1, $2, $3, $4, $5, $6); -- name: WebhookUpdate :exec -UPDATE tokens -SET - AccessToken = updated_data.AccessToken, - RefreshToken = updated_data.RefreshToken, - Expiration = updated_data.Expiration, - CreatedAt = updated_data.CreatedAt -FROM - jsonb_to_recordset($1::jsonb) AS updated_data( - AccountID VARCHAR(30), - AccessToken TEXT, - RefreshToken TEXT, - Expiration TIMESTAMP, - CreatedAt TIMESTAMP -) -WHERE - tokens.AccountID = updated_data.AccountID; +UPDATE tokens AS t +SET AccessToken = (update_data ->> 'access_token')::varchar(50), + RefreshToken = (update_data ->> 'refresh_token')::varchar(50), + Expiration = to_timestamp((update_data ->> 'expiration')::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours', + CreatedAt = to_timestamp((update_data ->> 'created_at')::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours' +FROM json_array_elements($1::json) AS update_data +WHERE t.accountID = (update_data ->> 'account_id')::VARCHAR(30); -- name: GetAllTokens :many SELECT * FROM tokens; From b7afcb01b168fa68196bd0ce4b97c661d29337d3 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 13:07:44 +0300 Subject: [PATCH 053/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 7e76f82..a0f70ed 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2781,22 +2781,13 @@ func (q *Queries) WebhookDelete(ctx context.Context, accountid string) error { } const webhookUpdate = `-- name: WebhookUpdate :exec -UPDATE tokens -SET - AccessToken = updated_data.AccessToken, - RefreshToken = updated_data.RefreshToken, - Expiration = updated_data.Expiration, - CreatedAt = updated_data.CreatedAt -FROM - jsonb_to_recordset($1::jsonb) AS updated_data( - AccountID VARCHAR(30), - AccessToken TEXT, - RefreshToken TEXT, - Expiration TIMESTAMP, - CreatedAt TIMESTAMP -) -WHERE - tokens.AccountID = updated_data.AccountID +UPDATE tokens AS t +SET AccessToken = (update_data ->> 'access_token')::varchar(50), + RefreshToken = (update_data ->> 'refresh_token')::varchar(50), + Expiration = to_timestamp((update_data ->> 'expiration')::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours', + CreatedAt = to_timestamp((update_data ->> 'created_at')::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours' +FROM json_array_elements($1::json) AS update_data +WHERE t.accountID = (update_data ->> 'account_id')::VARCHAR(30) ` func (q *Queries) WebhookUpdate(ctx context.Context, dollar_1 json.RawMessage) error { From f056b6ffcc4a848ae1efc16f9f51f4bcf46606cd Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 16:52:25 +0300 Subject: [PATCH 054/187] update sqlc gen --- dal/db_query/queries.sql | 3 +++ dal/sqlcgen/queries.sql.go | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 0ada38f..e1a406e 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -849,3 +849,6 @@ ON CONFLICT (AmoID) DO NOTHING; -- name: UpdateUsers :exec UPDATE users SET Name = $2, Email = $3, Role = $4, "Group" = $5, AmoUserID = $6 WHERE AmoID = $1; + +-- name: GetTokenById :exec +SELECT * FROM tokens WHERE accountID = $1; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index a0f70ed..f2b7e75 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2116,6 +2116,15 @@ func (q *Queries) GetTagsWithPagination(ctx context.Context, arg GetTagsWithPagi return items, nil } +const getTokenById = `-- name: GetTokenById :exec +SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokens WHERE accountID = $1 +` + +func (q *Queries) GetTokenById(ctx context.Context, accountid string) error { + _, err := q.db.ExecContext(ctx, getTokenById, accountid) + return err +} + const getUsersWithPagination = `-- name: GetUsersWithPagination :many SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 ` From 96cbd85a2de2613915830c29bb3dec7486cc8102 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 16:57:43 +0300 Subject: [PATCH 055/187] update sqlc gen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 17 +++++++++++++---- repository/amo/amo.go | 11 +++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index e1a406e..d5d4d78 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -850,5 +850,5 @@ ON CONFLICT (AmoID) DO NOTHING; -- name: UpdateUsers :exec UPDATE users SET Name = $2, Email = $3, Role = $4, "Group" = $5, AmoUserID = $6 WHERE AmoID = $1; --- name: GetTokenById :exec +-- name: GetTokenById :one SELECT * FROM tokens WHERE accountID = $1; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index f2b7e75..3735a8a 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2116,13 +2116,22 @@ func (q *Queries) GetTagsWithPagination(ctx context.Context, arg GetTagsWithPagi return items, nil } -const getTokenById = `-- name: GetTokenById :exec +const getTokenById = `-- name: GetTokenById :one SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokens WHERE accountID = $1 ` -func (q *Queries) GetTokenById(ctx context.Context, accountid string) error { - _, err := q.db.ExecContext(ctx, getTokenById, accountid) - return err +func (q *Queries) GetTokenById(ctx context.Context, accountid string) (Token, error) { + row := q.db.QueryRowContext(ctx, getTokenById, accountid) + var i Token + err := row.Scan( + &i.Accountid, + &i.Refreshtoken, + &i.Accesstoken, + &i.Authcode, + &i.Expiration, + &i.Createdat, + ) + return i, err } const getUsersWithPagination = `-- name: GetUsersWithPagination :many diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 7a60053..8f24ea3 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -170,6 +170,17 @@ func (r *AmoRepository) CheckAndUpdateUsers(ctx context.Context, user model.User return err } +func (r *AmoRepository) GetTokenByID(ctx context.Context, accountID string) (*model.Token, error) { + row, err := r.queries.GetTokenById(ctx, accountID) + if err != nil { + return nil, err + } + + return &model.Token{ + AccountID: row., + } +} + // методы webhook func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) error { From 116a42d49000a05fb9e4050bae88e7f0095ee443 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 16:59:53 +0300 Subject: [PATCH 056/187] add method in amo repo GetTokenByID --- repository/amo/amo.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 8f24ea3..e921e90 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -171,14 +171,16 @@ func (r *AmoRepository) CheckAndUpdateUsers(ctx context.Context, user model.User } func (r *AmoRepository) GetTokenByID(ctx context.Context, accountID string) (*model.Token, error) { - row, err := r.queries.GetTokenById(ctx, accountID) - if err != nil { - return nil, err - } + row, err := r.queries.GetTokenById(ctx, accountID) + if err != nil { + return nil, err + } - return &model.Token{ - AccountID: row., - } + return &model.Token{ + AccountID: row.Accountid, + RefreshToken: row.Refreshtoken, + AccessToken: row.Accesstoken, + }, nil } // методы webhook From 96a89cd0b56a2a5c01d5e3066f013b474988f01b Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 17:01:10 +0300 Subject: [PATCH 057/187] delete some methods --- repository/amo/amo.go | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index e921e90..32a8287 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -273,13 +273,6 @@ func (r *AmoRepository) WebhookDelete(ctx context.Context) error { // методы pipelines -func (r *AmoRepository) UpdateListPipelines(ctx context.Context) error { - //TODO:IMPLEMENT ME - - return nil - -} - func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListPipelinesResp, error) { rows, err := r.queries.GetPipelinesWithPagination(ctx, sqlcgen.GetPipelinesWithPaginationParams{ Column1: req.Page, @@ -385,13 +378,6 @@ func (r *AmoRepository) GetStepsWithPagination(ctx context.Context, req *model.P return &resp, nil } -func (r *AmoRepository) UpdateListSteps(ctx context.Context) error { - //TODO:IMPLEMENT ME - - return nil - -} - func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) error { dollar1, err := json.Marshal(steps) if err != nil { @@ -472,13 +458,6 @@ func (r *AmoRepository) GetTagsWithPagination(ctx context.Context, req *model.Pa return &resp, nil } -func (r *AmoRepository) UpdateListTags(ctx context.Context) error { - //TODO:IMPLEMENT ME - - return nil - -} - func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID string) error { column2, err := json.Marshal(tags) if err != nil { @@ -568,13 +547,6 @@ func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model. return &resp, nil } -func (r *AmoRepository) UpdateListCustom(ctx context.Context) error { - //TODO:IMPLEMENT ME - - return nil - -} - func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, tokenID string) error { column2, err := json.Marshal(fields) if err != nil { From 134018576f4e57c5d7a2dc0a22bc4ad413e09d1e Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 18:15:10 +0300 Subject: [PATCH 058/187] update sql queryes --- dal/db_query/queries.sql | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index d5d4d78..5e47fa0 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -688,19 +688,31 @@ SELECT * FROM users WHERE AccountID = $1; UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5; -- name: GetUsersWithPagination :many -SELECT *, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; +SELECT *, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false AND AccountID = $3 ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; -- name: GetTagsWithPagination :many -SELECT *, COUNT(*) OVER() as total_count FROM tags WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; +SELECT t.*, COUNT(*) OVER() as total_count +FROM tags t JOIN (SELECT AmoID FROM users WHERE AccountID = $1) u ON t.AccountID = u.AmoID +WHERE t.Deleted = false +ORDER BY t.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: GetStepsWithPagination :many -SELECT *, COUNT(*) OVER() as total_count FROM steps WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; +SELECT s.*, COUNT(*) OVER() as total_count +FROM steps s JOIN (SELECT AmoID FROM users WHERE AccountID = $1) u ON s.AccountID = u.AmoID +WHERE s.Deleted = false +ORDER BY s.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: GetPipelinesWithPagination :many -SELECT *, COUNT(*) OVER() as total_count FROM pipelines WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; +SELECT p.*, COUNT(*) OVER() as total_count +FROM pipelines p JOIN (SELECT AmoID FROM users WHERE AccountID = $1) u ON p.AccountID = u.AmoID +WHERE p.Deleted = false +ORDER BY p.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: GetFieldsWithPagination :many -SELECT *, COUNT(*) OVER() as total_count FROM fields WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; +SELECT f.*, COUNT(*) OVER() as total_count +FROM fields f JOIN (SELECT AmoID FROM users WHERE AccountID = $1) u ON f.AccountID = u.AmoID +WHERE f.Deleted = false +ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: UpdateTags :exec UPDATE tags AS t @@ -818,7 +830,6 @@ WITH user_data AS ( ) SELECT * from inserted_fields; - -- name: CheckSteps :many WITH new_steps AS ( SELECT (step->>'AmoID')::INT AS amoID, From 30192e61cba0c9ae45eac1c73d32d29886cabc65 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 18:18:53 +0300 Subject: [PATCH 059/187] update sqlc gen --- dal/db_query/queries.sql | 8 +++--- dal/sqlcgen/queries.sql.go | 57 +++++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 5e47fa0..a8b03ef 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -692,25 +692,25 @@ SELECT *, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false AND Ac -- name: GetTagsWithPagination :many SELECT t.*, COUNT(*) OVER() as total_count -FROM tags t JOIN (SELECT AmoID FROM users WHERE AccountID = $1) u ON t.AccountID = u.AmoID +FROM tags t JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON t.AccountID = u.AmoID WHERE t.Deleted = false ORDER BY t.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: GetStepsWithPagination :many SELECT s.*, COUNT(*) OVER() as total_count -FROM steps s JOIN (SELECT AmoID FROM users WHERE AccountID = $1) u ON s.AccountID = u.AmoID +FROM steps s JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON s.AccountID = u.AmoID WHERE s.Deleted = false ORDER BY s.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: GetPipelinesWithPagination :many SELECT p.*, COUNT(*) OVER() as total_count -FROM pipelines p JOIN (SELECT AmoID FROM users WHERE AccountID = $1) u ON p.AccountID = u.AmoID +FROM pipelines p JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON p.AccountID = u.AmoID WHERE p.Deleted = false ORDER BY p.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: GetFieldsWithPagination :many SELECT f.*, COUNT(*) OVER() as total_count -FROM fields f JOIN (SELECT AmoID FROM users WHERE AccountID = $1) u ON f.AccountID = u.AmoID +FROM fields f JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON f.AccountID = u.AmoID WHERE f.Deleted = false ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 3735a8a..0470b03 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1396,12 +1396,16 @@ func (q *Queries) GetExpiredPrivilege(ctx context.Context, privilegeid sql.NullS } const getFieldsWithPagination = `-- name: GetFieldsWithPagination :many -SELECT id, amoid, code, accountid, name, entity, type, deleted, createdat, COUNT(*) OVER() as total_count FROM fields WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +SELECT f.id, f.amoid, f.code, f.accountid, f.name, f.entity, f.type, f.deleted, f.createdat, COUNT(*) OVER() as total_count +FROM fields f JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON f.AccountID = u.AmoID +WHERE f.Deleted = false +ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3 ` type GetFieldsWithPaginationParams struct { - Column1 interface{} `db:"column_1" json:"column_1"` - Limit int32 `db:"limit" json:"limit"` + Accountid string `db:"accountid" json:"accountid"` + Column2 interface{} `db:"column_2" json:"column_2"` + Limit int32 `db:"limit" json:"limit"` } type GetFieldsWithPaginationRow struct { @@ -1418,7 +1422,7 @@ type GetFieldsWithPaginationRow struct { } func (q *Queries) GetFieldsWithPagination(ctx context.Context, arg GetFieldsWithPaginationParams) ([]GetFieldsWithPaginationRow, error) { - rows, err := q.db.QueryContext(ctx, getFieldsWithPagination, arg.Column1, arg.Limit) + rows, err := q.db.QueryContext(ctx, getFieldsWithPagination, arg.Accountid, arg.Column2, arg.Limit) if err != nil { return nil, err } @@ -1452,12 +1456,16 @@ func (q *Queries) GetFieldsWithPagination(ctx context.Context, arg GetFieldsWith } const getPipelinesWithPagination = `-- name: GetPipelinesWithPagination :many -SELECT id, amoid, accountid, name, isarchive, deleted, createdat, COUNT(*) OVER() as total_count FROM pipelines WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +SELECT p.id, p.amoid, p.accountid, p.name, p.isarchive, p.deleted, p.createdat, COUNT(*) OVER() as total_count +FROM pipelines p JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON p.AccountID = u.AmoID +WHERE p.Deleted = false +ORDER BY p.ID OFFSET ($2 - 1) * $3 LIMIT $3 ` type GetPipelinesWithPaginationParams struct { - Column1 interface{} `db:"column_1" json:"column_1"` - Limit int32 `db:"limit" json:"limit"` + Accountid string `db:"accountid" json:"accountid"` + Column2 interface{} `db:"column_2" json:"column_2"` + Limit int32 `db:"limit" json:"limit"` } type GetPipelinesWithPaginationRow struct { @@ -1472,7 +1480,7 @@ type GetPipelinesWithPaginationRow struct { } func (q *Queries) GetPipelinesWithPagination(ctx context.Context, arg GetPipelinesWithPaginationParams) ([]GetPipelinesWithPaginationRow, error) { - rows, err := q.db.QueryContext(ctx, getPipelinesWithPagination, arg.Column1, arg.Limit) + rows, err := q.db.QueryContext(ctx, getPipelinesWithPagination, arg.Accountid, arg.Column2, arg.Limit) if err != nil { return nil, err } @@ -2009,12 +2017,16 @@ func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAn } const getStepsWithPagination = `-- name: GetStepsWithPagination :many -SELECT id, amoid, pipelineid, accountid, name, color, deleted, createdat, COUNT(*) OVER() as total_count FROM steps WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +SELECT s.id, s.amoid, s.pipelineid, s.accountid, s.name, s.color, s.deleted, s.createdat, COUNT(*) OVER() as total_count +FROM steps s JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON s.AccountID = u.AmoID +WHERE s.Deleted = false +ORDER BY s.ID OFFSET ($2 - 1) * $3 LIMIT $3 ` type GetStepsWithPaginationParams struct { - Column1 interface{} `db:"column_1" json:"column_1"` - Limit int32 `db:"limit" json:"limit"` + Accountid string `db:"accountid" json:"accountid"` + Column2 interface{} `db:"column_2" json:"column_2"` + Limit int32 `db:"limit" json:"limit"` } type GetStepsWithPaginationRow struct { @@ -2030,7 +2042,7 @@ type GetStepsWithPaginationRow struct { } func (q *Queries) GetStepsWithPagination(ctx context.Context, arg GetStepsWithPaginationParams) ([]GetStepsWithPaginationRow, error) { - rows, err := q.db.QueryContext(ctx, getStepsWithPagination, arg.Column1, arg.Limit) + rows, err := q.db.QueryContext(ctx, getStepsWithPagination, arg.Accountid, arg.Column2, arg.Limit) if err != nil { return nil, err } @@ -2063,12 +2075,16 @@ func (q *Queries) GetStepsWithPagination(ctx context.Context, arg GetStepsWithPa } const getTagsWithPagination = `-- name: GetTagsWithPagination :many -SELECT id, amoid, accountid, entity, name, color, deleted, createdat, COUNT(*) OVER() as total_count FROM tags WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +SELECT t.id, t.amoid, t.accountid, t.entity, t.name, t.color, t.deleted, t.createdat, COUNT(*) OVER() as total_count +FROM tags t JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON t.AccountID = u.AmoID +WHERE t.Deleted = false +ORDER BY t.ID OFFSET ($2 - 1) * $3 LIMIT $3 ` type GetTagsWithPaginationParams struct { - Column1 interface{} `db:"column_1" json:"column_1"` - Limit int32 `db:"limit" json:"limit"` + Accountid string `db:"accountid" json:"accountid"` + Column2 interface{} `db:"column_2" json:"column_2"` + Limit int32 `db:"limit" json:"limit"` } type GetTagsWithPaginationRow struct { @@ -2084,7 +2100,7 @@ type GetTagsWithPaginationRow struct { } func (q *Queries) GetTagsWithPagination(ctx context.Context, arg GetTagsWithPaginationParams) ([]GetTagsWithPaginationRow, error) { - rows, err := q.db.QueryContext(ctx, getTagsWithPagination, arg.Column1, arg.Limit) + rows, err := q.db.QueryContext(ctx, getTagsWithPagination, arg.Accountid, arg.Column2, arg.Limit) if err != nil { return nil, err } @@ -2135,12 +2151,13 @@ func (q *Queries) GetTokenById(ctx context.Context, accountid string) (Token, er } const getUsersWithPagination = `-- name: GetUsersWithPagination :many -SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false AND AccountID = $3 ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 ` type GetUsersWithPaginationParams struct { - Column1 interface{} `db:"column_1" json:"column_1"` - Limit int32 `db:"limit" json:"limit"` + Column1 interface{} `db:"column_1" json:"column_1"` + Limit int32 `db:"limit" json:"limit"` + Accountid string `db:"accountid" json:"accountid"` } type GetUsersWithPaginationRow struct { @@ -2160,7 +2177,7 @@ type GetUsersWithPaginationRow struct { } func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPaginationParams) ([]GetUsersWithPaginationRow, error) { - rows, err := q.db.QueryContext(ctx, getUsersWithPagination, arg.Column1, arg.Limit) + rows, err := q.db.QueryContext(ctx, getUsersWithPagination, arg.Column1, arg.Limit, arg.Accountid) if err != nil { return nil, err } From bba21decd33ad3bebcb97905188b33a96142c634 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 18:21:36 +0300 Subject: [PATCH 060/187] update amo repo methods --- repository/amo/amo.go | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 32a8287..c961445 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -29,17 +29,11 @@ func NewAmoRepository(deps Deps) *AmoRepository { // методы пользователя -func (r *AmoRepository) UpdateListUsers(ctx context.Context) error { - //TODO:IMPLEMENT ME - - return nil - -} - -func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListResp, error) { +func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListResp, error) { rows, err := r.queries.GetUsersWithPagination(ctx, sqlcgen.GetUsersWithPaginationParams{ - Column1: req.Page, - Limit: req.Size, + Column1: req.Page, + Limit: req.Size, + Accountid: accountID, }) if err != nil { @@ -273,10 +267,11 @@ func (r *AmoRepository) WebhookDelete(ctx context.Context) error { // методы pipelines -func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListPipelinesResp, error) { +func (r *AmoRepository) GetPipelinesWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListPipelinesResp, error) { rows, err := r.queries.GetPipelinesWithPagination(ctx, sqlcgen.GetPipelinesWithPaginationParams{ - Column1: req.Page, - Limit: req.Size, + Accountid: accountID, + Column2: req.Page, + Limit: req.Size, }) if err != nil { return nil, err @@ -344,10 +339,11 @@ func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pi // методы steps -func (r *AmoRepository) GetStepsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListStepsResp, error) { +func (r *AmoRepository) GetStepsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListStepsResp, error) { rows, err := r.queries.GetStepsWithPagination(ctx, sqlcgen.GetStepsWithPaginationParams{ - Column1: req.Page, - Limit: req.Size, + Accountid: accountID, + Column2: req.Page, + Limit: req.Size, }) if err != nil { return nil, err @@ -420,10 +416,11 @@ func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) erro // методы tags -func (r *AmoRepository) GetTagsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListTagsResp, error) { +func (r *AmoRepository) GetTagsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListTagsResp, error) { rows, err := r.queries.GetTagsWithPagination(ctx, sqlcgen.GetTagsWithPaginationParams{ - Column1: req.Page, - Limit: req.Size, + Accountid: accountID, + Column2: req.Page, + Limit: req.Size, }) if err != nil { return nil, err @@ -506,10 +503,11 @@ func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID // методы fields -func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model.PaginationReq) (*model.UserListFieldsResp, error) { +func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListFieldsResp, error) { rows, err := r.queries.GetFieldsWithPagination(ctx, sqlcgen.GetFieldsWithPaginationParams{ - Column1: req.Page, - Limit: req.Size, + Accountid: accountID, + Column2: req.Page, + Limit: req.Size, }) if err != nil { return nil, err From 836cb9fce7f10414f75ad2265fc956f2d061e699 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 21 Apr 2024 18:22:38 +0300 Subject: [PATCH 061/187] update amo repo methods --- repository/amo/amo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index c961445..e96aaf8 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -615,7 +615,7 @@ func (r *AmoRepository) GettingQuizRules(ctx context.Context) (*model.Rule, erro // методы UTMs -func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq) error { +func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq, accountId string) error { return nil } From c17fb92c5051afa1b75f7493acd8526797a68d5e Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 11:33:25 +0300 Subject: [PATCH 062/187] update sqlc gen --- dal/db_query/queries.sql | 10 +++++++++- dal/sqlcgen/queries.sql.go | 28 ++++++++++++++++------------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index a8b03ef..ef71af5 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -688,7 +688,15 @@ SELECT * FROM users WHERE AccountID = $1; UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5; -- name: GetUsersWithPagination :many -SELECT *, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false AND AccountID = $3 ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2; +WITH user_data AS ( + SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false +) +SELECT u.ID, u.Name, u.Email, u.Role, u."Group", u.CreatedAt, u.Subdomain, u.Country, u.AmoUserID, u.AmoID, + COUNT(*) OVER() as total_count +FROM users u +JOIN user_data a ON u.AmoUserID = a.AmoID +WHERE u.Deleted = false +ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: GetTagsWithPagination :many SELECT t.*, COUNT(*) OVER() as total_count diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 0470b03..8ec336b 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2151,33 +2151,39 @@ func (q *Queries) GetTokenById(ctx context.Context, accountid string) (Token, er } const getUsersWithPagination = `-- name: GetUsersWithPagination :many -SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false AND AccountID = $3 ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2 +WITH user_data AS ( + SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false +) +SELECT u.ID, u.Name, u.Email, u.Role, u."Group", u.CreatedAt, u.Subdomain, u.Country, u.AmoUserID, u.AmoID, + COUNT(*) OVER() as total_count +FROM users u +JOIN user_data a ON u.AmoUserID = a.AmoID +WHERE u.Deleted = false +ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3 ` type GetUsersWithPaginationParams struct { - Column1 interface{} `db:"column_1" json:"column_1"` - Limit int32 `db:"limit" json:"limit"` Accountid string `db:"accountid" json:"accountid"` + Column2 interface{} `db:"column_2" json:"column_2"` + Limit int32 `db:"limit" json:"limit"` } type GetUsersWithPaginationRow struct { ID int64 `db:"id" json:"id"` - Accountid string `db:"accountid" json:"accountid"` - Amoid int32 `db:"amoid" json:"amoid"` Name string `db:"name" json:"name"` Email string `db:"email" json:"email"` Role int32 `db:"role" json:"role"` Group int32 `db:"Group" json:"Group"` - Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` Subdomain string `db:"subdomain" json:"subdomain"` - Amouserid int32 `db:"amouserid" json:"amouserid"` Country string `db:"country" json:"country"` + Amouserid int32 `db:"amouserid" json:"amouserid"` + Amoid int32 `db:"amoid" json:"amoid"` TotalCount int64 `db:"total_count" json:"total_count"` } func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPaginationParams) ([]GetUsersWithPaginationRow, error) { - rows, err := q.db.QueryContext(ctx, getUsersWithPagination, arg.Column1, arg.Limit, arg.Accountid) + rows, err := q.db.QueryContext(ctx, getUsersWithPagination, arg.Accountid, arg.Column2, arg.Limit) if err != nil { return nil, err } @@ -2187,17 +2193,15 @@ func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPa var i GetUsersWithPaginationRow if err := rows.Scan( &i.ID, - &i.Accountid, - &i.Amoid, &i.Name, &i.Email, &i.Role, &i.Group, - &i.Deleted, &i.Createdat, &i.Subdomain, - &i.Amouserid, &i.Country, + &i.Amouserid, + &i.Amoid, &i.TotalCount, ); err != nil { return nil, err From b6e53fdff75080ba2380580d466ea3c1a2172400 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 11:37:07 +0300 Subject: [PATCH 063/187] update sqlc gen --- dal/db_query/queries.sql | 3 +-- dal/sqlcgen/queries.sql.go | 15 +++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index ef71af5..215c2e6 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -691,8 +691,7 @@ UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5 WITH user_data AS ( SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false ) -SELECT u.ID, u.Name, u.Email, u.Role, u."Group", u.CreatedAt, u.Subdomain, u.Country, u.AmoUserID, u.AmoID, - COUNT(*) OVER() as total_count +SELECT u.*, COUNT(*) OVER() as total_count FROM users u JOIN user_data a ON u.AmoUserID = a.AmoID WHERE u.Deleted = false diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 8ec336b..7a0369b 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2154,8 +2154,7 @@ const getUsersWithPagination = `-- name: GetUsersWithPagination :many WITH user_data AS ( SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false ) -SELECT u.ID, u.Name, u.Email, u.Role, u."Group", u.CreatedAt, u.Subdomain, u.Country, u.AmoUserID, u.AmoID, - COUNT(*) OVER() as total_count +SELECT u.id, u.accountid, u.amoid, u.name, u.email, u.role, u."Group", u.deleted, u.createdat, u.subdomain, u.amouserid, u.country, COUNT(*) OVER() as total_count FROM users u JOIN user_data a ON u.AmoUserID = a.AmoID WHERE u.Deleted = false @@ -2170,15 +2169,17 @@ type GetUsersWithPaginationParams struct { type GetUsersWithPaginationRow struct { ID int64 `db:"id" json:"id"` + Accountid string `db:"accountid" json:"accountid"` + Amoid int32 `db:"amoid" json:"amoid"` Name string `db:"name" json:"name"` Email string `db:"email" json:"email"` Role int32 `db:"role" json:"role"` Group int32 `db:"Group" json:"Group"` + Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` Subdomain string `db:"subdomain" json:"subdomain"` - Country string `db:"country" json:"country"` Amouserid int32 `db:"amouserid" json:"amouserid"` - Amoid int32 `db:"amoid" json:"amoid"` + Country string `db:"country" json:"country"` TotalCount int64 `db:"total_count" json:"total_count"` } @@ -2193,15 +2194,17 @@ func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPa var i GetUsersWithPaginationRow if err := rows.Scan( &i.ID, + &i.Accountid, + &i.Amoid, &i.Name, &i.Email, &i.Role, &i.Group, + &i.Deleted, &i.Createdat, &i.Subdomain, - &i.Country, &i.Amouserid, - &i.Amoid, + &i.Country, &i.TotalCount, ); err != nil { return nil, err From 3fc5433afe9b9ce3c6a365873ff18d33fa00fca9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 11:43:59 +0300 Subject: [PATCH 064/187] update amo repo method --- repository/amo/amo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index e96aaf8..7a7b3a9 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -31,7 +31,7 @@ func NewAmoRepository(deps Deps) *AmoRepository { func (r *AmoRepository) GettingUserWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListResp, error) { rows, err := r.queries.GetUsersWithPagination(ctx, sqlcgen.GetUsersWithPaginationParams{ - Column1: req.Page, + Column2: req.Page, Limit: req.Size, Accountid: accountID, }) From e01edb871e9b51358ec130b918efcd1bce178450 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 13:39:34 +0300 Subject: [PATCH 065/187] update migrate schema --- dal/schema/000010_init.down.sql | 6 ++---- dal/schema/000010_init.up.sql | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/dal/schema/000010_init.down.sql b/dal/schema/000010_init.down.sql index 43fe0a6..4b044b7 100644 --- a/dal/schema/000010_init.down.sql +++ b/dal/schema/000010_init.down.sql @@ -4,10 +4,8 @@ DROP INDEX IF EXISTS idx_unique_step; DROP INDEX IF EXISTS idx_unique_field; DROP INDEX IF EXISTS idx_unique_tag; -ALTER TABLE question - DROP COLUMN IF EXISTS utm, - DROP COLUMN IF EXISTS rules; - +DROP TABLE IF EXISTS rules; +DROP TABLE IF EXISTS utms; DROP TABLE IF EXISTS tags; DROP TABLE IF EXISTS fields; diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 167f8ce..3f6f4e7 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -73,8 +73,29 @@ CREATE TABLE IF NOT EXISTS tags ( CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -ALTER TABLE quiz -ADD COLUMN utm jsonb NOT NULL DEFAULT '{}'; +CREATE TABLE IF NOT EXISTS utms ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AmoFieldID INT NOT NULL, -- id field в амо + QuizID INT NOT NULL, -- id опроса + AccountID INT NOT NULL, -- id аккаунта в амо AMOID + Name VARCHAR(50) NOT NULL DEFAULT '', -- название utm + Deleted BOOLEAN NOT NULL DEFAULT FALSE, + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS rules ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AccountID INT NOT NULL, -- id аккаунта в амо AMOID + QuizID INT NOT NULL, -- id опроса + PerformerID INT NOT NULL, -- айдишник ответственного за сделку + PipelineID INT NOT NULL, --id воронки AmoID pipelines неявная посредством join + StepID INT NOT NULL , -- id этапа steps AmoID join + UTMS INTEGER[], -- список UTM для этого опроса id utm + FieldsRule JSONB NOT NULL DEFAULT '{}', -- вложенная структура + Deleted BOOLEAN NOT NULL DEFAULT FALSE, + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + ALTER TABLE quiz ADD COLUMN rules jsonb NOT NULL DEFAULT '{}'; From 1f6a5c6ee684cc6443147113ba3dc268d5f7881b Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 13:43:09 +0300 Subject: [PATCH 066/187] update sqlc gen --- dal/sqlcgen/models.go | 24 +++++++++++++++++++++++- dal/sqlcgen/queries.sql.go | 14 +++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 6af78b9..9b817a1 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -117,10 +117,22 @@ type Quiz struct { AnswersCount sql.NullInt32 `db:"answers_count" json:"answers_count"` AverageTimePassing sql.NullInt32 `db:"average_time_passing" json:"average_time_passing"` SessionsCount sql.NullInt32 `db:"sessions_count" json:"sessions_count"` - Utm json.RawMessage `db:"utm" json:"utm"` Rules json.RawMessage `db:"rules" json:"rules"` } +type Rule struct { + ID int64 `db:"id" json:"id"` + Accountid int32 `db:"accountid" json:"accountid"` + Quizid int32 `db:"quizid" json:"quizid"` + Performerid int32 `db:"performerid" json:"performerid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + Stepid int32 `db:"stepid" json:"stepid"` + Utms []int32 `db:"utms" json:"utms"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + type Step struct { ID int64 `db:"id" json:"id"` Amoid int32 `db:"amoid" json:"amoid"` @@ -166,3 +178,13 @@ type User struct { Amouserid int32 `db:"amouserid" json:"amouserid"` Country string `db:"country" json:"country"` } + +type Utm struct { + ID int64 `db:"id" json:"id"` + Amofieldid int32 `db:"amofieldid" json:"amofieldid"` + Quizid int32 `db:"quizid" json:"quizid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 7a0369b..b61b288 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -799,7 +799,7 @@ func (q *Queries) DeleteQuestion(ctx context.Context, id int64) (Question, error } const deleteQuizByID = `-- name: DeleteQuizByID :one -UPDATE quiz SET deleted=true WHERE quiz.id=$1 AND accountid=$2 RETURNING quiz.id, quiz.qid, quiz.accountid, quiz.deleted, quiz.archived, quiz.fingerprinting, quiz.repeatable, quiz.note_prevented, quiz.mail_notifications, quiz.unique_answers, quiz.super, quiz.group_id, quiz.name, quiz.description, quiz.config, quiz.status, quiz.limit_answers, quiz.due_to, quiz.time_of_passing, quiz.pausable, quiz.version, quiz.version_comment, quiz.parent_ids, quiz.created_at, quiz.updated_at, quiz.questions_count, quiz.answers_count, quiz.average_time_passing, quiz.sessions_count, quiz.utm, quiz.rules +UPDATE quiz SET deleted=true WHERE quiz.id=$1 AND accountid=$2 RETURNING quiz.id, quiz.qid, quiz.accountid, quiz.deleted, quiz.archived, quiz.fingerprinting, quiz.repeatable, quiz.note_prevented, quiz.mail_notifications, quiz.unique_answers, quiz.super, quiz.group_id, quiz.name, quiz.description, quiz.config, quiz.status, quiz.limit_answers, quiz.due_to, quiz.time_of_passing, quiz.pausable, quiz.version, quiz.version_comment, quiz.parent_ids, quiz.created_at, quiz.updated_at, quiz.questions_count, quiz.answers_count, quiz.average_time_passing, quiz.sessions_count, quiz.rules ` type DeleteQuizByIDParams struct { @@ -840,7 +840,6 @@ func (q *Queries) DeleteQuizByID(ctx context.Context, arg DeleteQuizByIDParams) &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, - &i.Utm, &i.Rules, ) return i, err @@ -1770,7 +1769,7 @@ func (q *Queries) GetQuestions(ctx context.Context, quizID int64) ([]Question, e } const getQuizById = `-- name: GetQuizById :one -SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, utm, rules FROM quiz WHERE id=$1 AND accountId=$2 +SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, rules FROM quiz WHERE id=$1 AND accountId=$2 ` type GetQuizByIdParams struct { @@ -1811,14 +1810,13 @@ func (q *Queries) GetQuizById(ctx context.Context, arg GetQuizByIdParams) (Quiz, &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, - &i.Utm, &i.Rules, ) return i, err } const getQuizByQid = `-- name: GetQuizByQid :one -SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, utm, rules FROM quiz +SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, rules FROM quiz WHERE deleted = false AND archived = false AND @@ -1859,7 +1857,6 @@ func (q *Queries) GetQuizByQid(ctx context.Context, qid uuid.NullUUID) (Quiz, er &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, - &i.Utm, &i.Rules, ) return i, err @@ -1882,7 +1879,7 @@ func (q *Queries) GetQuizConfig(ctx context.Context, id int64) (GetQuizConfigRow } const getQuizHistory = `-- name: GetQuizHistory :many -SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, utm, rules FROM quiz WHERE quiz.id = $1 AND quiz.accountId = $4 OR quiz.id = ANY( +SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, rules FROM quiz WHERE quiz.id = $1 AND quiz.accountId = $4 OR quiz.id = ANY( SELECT unnest(parent_ids) FROM quiz WHERE id = $1 ) ORDER BY quiz.id DESC LIMIT $2 OFFSET $3 ` @@ -1938,7 +1935,6 @@ func (q *Queries) GetQuizHistory(ctx context.Context, arg GetQuizHistoryParams) &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, - &i.Utm, &i.Rules, ); err != nil { return nil, err @@ -2889,7 +2885,7 @@ SET average_time_passing = COALESCE(sta.average_session_time, 0), sessions_count = COALESCE(sta.sess,0) FROM - (SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, utm, rules FROM quiz WHERE deleted = FALSE AND archived = FALSE) q_sub + (SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, rules FROM quiz WHERE deleted = FALSE AND archived = FALSE) q_sub LEFT JOIN answer_aggregates aa ON q_sub.id = aa.quiz_id LEFT JOIN question_aggregates qa ON q_sub.id = qa.quiz_id LEFT JOIN session_times_aggregates sta ON q_sub.id = sta.quiz_id From 4a6ed865a7d2398ae8fa39aebb090b342a16a1f2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 13:44:18 +0300 Subject: [PATCH 067/187] update sqlc gen --- dal/schema/000010_init.up.sql | 5 +-- dal/sqlcgen/models.go | 59 +++++++++++++++++------------------ dal/sqlcgen/queries.sql.go | 14 +++------ 3 files changed, 35 insertions(+), 43 deletions(-) diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 3f6f4e7..fff445f 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -96,11 +96,8 @@ CREATE TABLE IF NOT EXISTS rules ( CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -ALTER TABLE quiz -ADD COLUMN rules jsonb NOT NULL DEFAULT '{}'; - CREATE UNIQUE INDEX idx_unique_users ON users (amoID); CREATE UNIQUE INDEX idx_unique_pipeline ON pipelines (amoID, accountID); CREATE UNIQUE INDEX idx_unique_step ON steps (amoID, accountID, PipelineID); CREATE UNIQUE INDEX idx_unique_field ON fields (amoID, accountID, entity); -CREATE UNIQUE INDEX idx_unique_tag ON tags (amoID, accountID, entity); \ No newline at end of file +CREATE UNIQUE INDEX idx_unique_tag ON tags (amoID, accountID, entity); diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 9b817a1..06fd657 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -88,36 +88,35 @@ type Question struct { } type Quiz struct { - ID int64 `db:"id" json:"id"` - Qid uuid.NullUUID `db:"qid" json:"qid"` - Accountid string `db:"accountid" json:"accountid"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` - Archived sql.NullBool `db:"archived" json:"archived"` - Fingerprinting sql.NullBool `db:"fingerprinting" json:"fingerprinting"` - Repeatable sql.NullBool `db:"repeatable" json:"repeatable"` - NotePrevented sql.NullBool `db:"note_prevented" json:"note_prevented"` - MailNotifications sql.NullBool `db:"mail_notifications" json:"mail_notifications"` - UniqueAnswers sql.NullBool `db:"unique_answers" json:"unique_answers"` - Super sql.NullBool `db:"super" json:"super"` - GroupID sql.NullInt64 `db:"group_id" json:"group_id"` - Name sql.NullString `db:"name" json:"name"` - Description sql.NullString `db:"description" json:"description"` - Config sql.NullString `db:"config" json:"config"` - Status interface{} `db:"status" json:"status"` - LimitAnswers sql.NullInt32 `db:"limit_answers" json:"limit_answers"` - DueTo sql.NullInt32 `db:"due_to" json:"due_to"` - TimeOfPassing sql.NullInt32 `db:"time_of_passing" json:"time_of_passing"` - Pausable sql.NullBool `db:"pausable" json:"pausable"` - Version sql.NullInt16 `db:"version" json:"version"` - VersionComment sql.NullString `db:"version_comment" json:"version_comment"` - ParentIds []int32 `db:"parent_ids" json:"parent_ids"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"` - QuestionsCount sql.NullInt32 `db:"questions_count" json:"questions_count"` - AnswersCount sql.NullInt32 `db:"answers_count" json:"answers_count"` - AverageTimePassing sql.NullInt32 `db:"average_time_passing" json:"average_time_passing"` - SessionsCount sql.NullInt32 `db:"sessions_count" json:"sessions_count"` - Rules json.RawMessage `db:"rules" json:"rules"` + ID int64 `db:"id" json:"id"` + Qid uuid.NullUUID `db:"qid" json:"qid"` + Accountid string `db:"accountid" json:"accountid"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Archived sql.NullBool `db:"archived" json:"archived"` + Fingerprinting sql.NullBool `db:"fingerprinting" json:"fingerprinting"` + Repeatable sql.NullBool `db:"repeatable" json:"repeatable"` + NotePrevented sql.NullBool `db:"note_prevented" json:"note_prevented"` + MailNotifications sql.NullBool `db:"mail_notifications" json:"mail_notifications"` + UniqueAnswers sql.NullBool `db:"unique_answers" json:"unique_answers"` + Super sql.NullBool `db:"super" json:"super"` + GroupID sql.NullInt64 `db:"group_id" json:"group_id"` + Name sql.NullString `db:"name" json:"name"` + Description sql.NullString `db:"description" json:"description"` + Config sql.NullString `db:"config" json:"config"` + Status interface{} `db:"status" json:"status"` + LimitAnswers sql.NullInt32 `db:"limit_answers" json:"limit_answers"` + DueTo sql.NullInt32 `db:"due_to" json:"due_to"` + TimeOfPassing sql.NullInt32 `db:"time_of_passing" json:"time_of_passing"` + Pausable sql.NullBool `db:"pausable" json:"pausable"` + Version sql.NullInt16 `db:"version" json:"version"` + VersionComment sql.NullString `db:"version_comment" json:"version_comment"` + ParentIds []int32 `db:"parent_ids" json:"parent_ids"` + CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"` + QuestionsCount sql.NullInt32 `db:"questions_count" json:"questions_count"` + AnswersCount sql.NullInt32 `db:"answers_count" json:"answers_count"` + AverageTimePassing sql.NullInt32 `db:"average_time_passing" json:"average_time_passing"` + SessionsCount sql.NullInt32 `db:"sessions_count" json:"sessions_count"` } type Rule struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index b61b288..8f6a18b 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -799,7 +799,7 @@ func (q *Queries) DeleteQuestion(ctx context.Context, id int64) (Question, error } const deleteQuizByID = `-- name: DeleteQuizByID :one -UPDATE quiz SET deleted=true WHERE quiz.id=$1 AND accountid=$2 RETURNING quiz.id, quiz.qid, quiz.accountid, quiz.deleted, quiz.archived, quiz.fingerprinting, quiz.repeatable, quiz.note_prevented, quiz.mail_notifications, quiz.unique_answers, quiz.super, quiz.group_id, quiz.name, quiz.description, quiz.config, quiz.status, quiz.limit_answers, quiz.due_to, quiz.time_of_passing, quiz.pausable, quiz.version, quiz.version_comment, quiz.parent_ids, quiz.created_at, quiz.updated_at, quiz.questions_count, quiz.answers_count, quiz.average_time_passing, quiz.sessions_count, quiz.rules +UPDATE quiz SET deleted=true WHERE quiz.id=$1 AND accountid=$2 RETURNING quiz.id, quiz.qid, quiz.accountid, quiz.deleted, quiz.archived, quiz.fingerprinting, quiz.repeatable, quiz.note_prevented, quiz.mail_notifications, quiz.unique_answers, quiz.super, quiz.group_id, quiz.name, quiz.description, quiz.config, quiz.status, quiz.limit_answers, quiz.due_to, quiz.time_of_passing, quiz.pausable, quiz.version, quiz.version_comment, quiz.parent_ids, quiz.created_at, quiz.updated_at, quiz.questions_count, quiz.answers_count, quiz.average_time_passing, quiz.sessions_count ` type DeleteQuizByIDParams struct { @@ -840,7 +840,6 @@ func (q *Queries) DeleteQuizByID(ctx context.Context, arg DeleteQuizByIDParams) &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, - &i.Rules, ) return i, err } @@ -1769,7 +1768,7 @@ func (q *Queries) GetQuestions(ctx context.Context, quizID int64) ([]Question, e } const getQuizById = `-- name: GetQuizById :one -SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, rules FROM quiz WHERE id=$1 AND accountId=$2 +SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE id=$1 AND accountId=$2 ` type GetQuizByIdParams struct { @@ -1810,13 +1809,12 @@ func (q *Queries) GetQuizById(ctx context.Context, arg GetQuizByIdParams) (Quiz, &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, - &i.Rules, ) return i, err } const getQuizByQid = `-- name: GetQuizByQid :one -SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, rules FROM quiz +SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE deleted = false AND archived = false AND @@ -1857,7 +1855,6 @@ func (q *Queries) GetQuizByQid(ctx context.Context, qid uuid.NullUUID) (Quiz, er &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, - &i.Rules, ) return i, err } @@ -1879,7 +1876,7 @@ func (q *Queries) GetQuizConfig(ctx context.Context, id int64) (GetQuizConfigRow } const getQuizHistory = `-- name: GetQuizHistory :many -SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, rules FROM quiz WHERE quiz.id = $1 AND quiz.accountId = $4 OR quiz.id = ANY( +SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE quiz.id = $1 AND quiz.accountId = $4 OR quiz.id = ANY( SELECT unnest(parent_ids) FROM quiz WHERE id = $1 ) ORDER BY quiz.id DESC LIMIT $2 OFFSET $3 ` @@ -1935,7 +1932,6 @@ func (q *Queries) GetQuizHistory(ctx context.Context, arg GetQuizHistoryParams) &i.AnswersCount, &i.AverageTimePassing, &i.SessionsCount, - &i.Rules, ); err != nil { return nil, err } @@ -2885,7 +2881,7 @@ SET average_time_passing = COALESCE(sta.average_session_time, 0), sessions_count = COALESCE(sta.sess,0) FROM - (SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count, rules FROM quiz WHERE deleted = FALSE AND archived = FALSE) q_sub + (SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE deleted = FALSE AND archived = FALSE) q_sub LEFT JOIN answer_aggregates aa ON q_sub.id = aa.quiz_id LEFT JOIN question_aggregates qa ON q_sub.id = qa.quiz_id LEFT JOIN session_times_aggregates sta ON q_sub.id = sta.quiz_id From 8a0237ee6ee399f5a05383d0cf771c9fdce449fc Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 17:13:18 +0300 Subject: [PATCH 068/187] update queries --- dal/db_query/queries.sql | 67 +++++++++++++++++++++++++++++++++++++--- model/amo.go | 2 +- repository/amo/amo.go | 4 +-- 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 215c2e6..e4aaca3 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -785,7 +785,14 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, Entity) DO NOTHING RETURNING * ) -SELECT * FROM inserted_tags; +SELECT nt.* +FROM new_tags nt +WHERE NOT EXISTS ( + SELECT * + FROM inserted_tags ins + JOIN user_data ud ON true + WHERE ins.amoID = nt.amoID AND ins.accountID = ud.amoid AND ins.Entity = nt.Entity +); -- name: CheckPipelines :many WITH new_pipelines AS ( @@ -806,7 +813,13 @@ WITH new_pipelines AS ( ON CONFLICT (amoID, accountID) DO NOTHING RETURNING * ) -SELECT * FROM inserted_pipelines; +SELECT np.* +FROM new_pipelines np +WHERE NOT EXISTS ( + SELECT * + FROM inserted_pipelines ins + WHERE ins.amoID = np.amoID AND ins.accountID = np.accountID +); -- name: CheckFields :many WITH user_data AS ( @@ -835,7 +848,14 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, entity) DO NOTHING RETURNING * ) -SELECT * from inserted_fields; +SELECT nf.* +FROM new_fields nf +WHERE NOT EXISTS ( + SELECT * + FROM inserted_fields ins + JOIN user_data ud ON true + WHERE ins.amoID = nf.amoID AND ins.accountID = ud.amoid AND ins.Entity = nf.Entity +); -- name: CheckSteps :many WITH new_steps AS ( @@ -858,7 +878,13 @@ WITH new_steps AS ( ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING RETURNING * ) -SELECT * FROM inserted_steps; +SELECT ns.* +FROM new_steps ns +WHERE NOT EXISTS ( + SELECT * + FROM inserted_steps ins + WHERE ins.amoID = ns.amoID AND ins.accountID = ns.accountID AND ins.pipelineID = ns.pipelineID +); -- name: CheckUsers :exec INSERT INTO users (AmoID, Name, Email, Role, "Group", AmoUserID) @@ -870,3 +896,36 @@ UPDATE users SET Name = $2, Email = $3, Role = $4, "Group" = $5, AmoUserID = $6 -- name: GetTokenById :one SELECT * FROM tokens WHERE accountID = $1; + +-- name: DeletingUTM :exec +UPDATE utms SET Deleted = true WHERE ID = ANY($1::int[]); + +-- name: GetUTMsWithPagination :many +SELECT ut.*, COUNT(*) OVER() as total_count +FROM utms ut JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON ut.AccountID = u.AmoID +WHERE ut.Deleted = false +ORDER BY ut.ID OFFSET ($2 - 1) * $3 LIMIT $3; + +-- name: SaveUTMs :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +), new_UTMs AS ( + SELECT (utm->>'AmoFieldID')::INT AS amoFieldID, + COALESCE(utm->>'QuizID', '')::INT AS quizID, + COALESCE(utm->>'Name', '')::varchar(50) AS name, + CURRENT_TIMESTAMP AS createdAt + FROM json_array_elements($2::json) AS utm +), inserted_utms AS( + INSERT INTO utms (AmoFieldID, QuizID, AccountID, Name, createdAt) + SELECT nu.amoFieldID, + nu.quizID, + ud.AmoID, + nu.name, + nu.createdAt + FROM new_UTMs nu + JOIN user_data ud ON true + RETURNING * +) +SELECT * from inserted_utms; \ No newline at end of file diff --git a/model/amo.go b/model/amo.go index 858b1c7..caaad3e 100644 --- a/model/amo.go +++ b/model/amo.go @@ -164,7 +164,7 @@ type FieldRule struct { type UTM struct { /* - айдишник в нашей системе Primary Key*/ - ID int `json:"ID" bson:"ID"` + ID int `json:"ID"` /* - айдишник кастомного поля в амо*/ Amofieldid int `json:"AmoFieldID"` /* - айдишник квиза*/ diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 7a7b3a9..8c53c8c 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -615,11 +615,11 @@ func (r *AmoRepository) GettingQuizRules(ctx context.Context) (*model.Rule, erro // методы UTMs -func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq, accountId string) error { +func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq) error { return nil } -func (r *AmoRepository) SavingUserUtm(ctx context.Context, request *model.SaveUserListUTMReq, accountID string, quizID int) (*model.ListSavedIDUTMResp, error) { +func (r *AmoRepository) SavingUserUtm(ctx context.Context, request *model.SaveUserListUTMReq, accountID string) (*model.ListSavedIDUTMResp, error) { return nil, nil } From 87cdea8e08b4541a7fc4f4cc7b569a8497bb3a65 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 17:25:42 +0300 Subject: [PATCH 069/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 234 +++++++++++++++++++++++++++++-------- 1 file changed, 187 insertions(+), 47 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 8f6a18b..8724dd3 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -193,7 +193,14 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, entity) DO NOTHING RETURNING id, amoid, code, accountid, name, entity, type, deleted, createdat ) -SELECT id, amoid, code, accountid, name, entity, type, deleted, createdat from inserted_fields +SELECT nf.amoid, nf.code, nf.name, nf.entity, nf.type, nf.createdat +FROM new_fields nf +WHERE NOT EXISTS ( + SELECT id, ins.amoid, code, accountid, name, entity, type, deleted, createdat, ud.amoid + FROM inserted_fields ins + JOIN user_data ud ON true + WHERE ins.amoID = nf.amoID AND ins.accountID = ud.amoid AND ins.Entity = nf.Entity +) ` type CheckFieldsParams struct { @@ -202,15 +209,12 @@ type CheckFieldsParams struct { } type CheckFieldsRow struct { - ID int64 `db:"id" json:"id"` - Amoid int32 `db:"amoid" json:"amoid"` - Code string `db:"code" json:"code"` - Accountid int32 `db:"accountid" json:"accountid"` - Name string `db:"name" json:"name"` - Entity interface{} `db:"entity" json:"entity"` - Type string `db:"type" json:"type"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` + Amoid int32 `db:"amoid" json:"amoid"` + Code string `db:"code" json:"code"` + Name string `db:"name" json:"name"` + Entity interface{} `db:"entity" json:"entity"` + Type string `db:"type" json:"type"` + Createdat interface{} `db:"createdat" json:"createdat"` } func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]CheckFieldsRow, error) { @@ -223,14 +227,11 @@ func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]Che for rows.Next() { var i CheckFieldsRow if err := rows.Scan( - &i.ID, &i.Amoid, &i.Code, - &i.Accountid, &i.Name, &i.Entity, &i.Type, - &i.Deleted, &i.Createdat, ); err != nil { return nil, err @@ -288,17 +289,21 @@ WITH new_pipelines AS ( ON CONFLICT (amoID, accountID) DO NOTHING RETURNING id, amoid, accountid, name, isarchive, deleted, createdat ) -SELECT id, amoid, accountid, name, isarchive, deleted, createdat FROM inserted_pipelines +SELECT np.amoid, np.accountid, np.name, np.isarchive, np.createdat +FROM new_pipelines np +WHERE NOT EXISTS ( + SELECT id, amoid, accountid, name, isarchive, deleted, createdat + FROM inserted_pipelines ins + WHERE ins.amoID = np.amoID AND ins.accountID = np.accountID +) ` type CheckPipelinesRow struct { - ID int64 `db:"id" json:"id"` - Amoid int32 `db:"amoid" json:"amoid"` - Accountid int32 `db:"accountid" json:"accountid"` - Name string `db:"name" json:"name"` - Isarchive bool `db:"isarchive" json:"isarchive"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` + Amoid int32 `db:"amoid" json:"amoid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Isarchive bool `db:"isarchive" json:"isarchive"` + Createdat interface{} `db:"createdat" json:"createdat"` } func (q *Queries) CheckPipelines(ctx context.Context, dollar_1 json.RawMessage) ([]CheckPipelinesRow, error) { @@ -311,12 +316,10 @@ func (q *Queries) CheckPipelines(ctx context.Context, dollar_1 json.RawMessage) for rows.Next() { var i CheckPipelinesRow if err := rows.Scan( - &i.ID, &i.Amoid, &i.Accountid, &i.Name, &i.Isarchive, - &i.Deleted, &i.Createdat, ); err != nil { return nil, err @@ -399,18 +402,22 @@ WITH new_steps AS ( ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat ) -SELECT id, amoid, pipelineid, accountid, name, color, deleted, createdat FROM inserted_steps +SELECT ns.amoid, ns.pipelineid, ns.accountid, ns.name, ns.color, ns.createdat +FROM new_steps ns +WHERE NOT EXISTS ( + SELECT id, amoid, pipelineid, accountid, name, color, deleted, createdat + FROM inserted_steps ins + WHERE ins.amoID = ns.amoID AND ins.accountID = ns.accountID AND ins.pipelineID = ns.pipelineID +) ` type CheckStepsRow struct { - ID int64 `db:"id" json:"id"` - Amoid int32 `db:"amoid" json:"amoid"` - Pipelineid int32 `db:"pipelineid" json:"pipelineid"` - Accountid int32 `db:"accountid" json:"accountid"` - Name string `db:"name" json:"name"` - Color string `db:"color" json:"color"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` + Amoid int32 `db:"amoid" json:"amoid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` + Createdat interface{} `db:"createdat" json:"createdat"` } func (q *Queries) CheckSteps(ctx context.Context, dollar_1 json.RawMessage) ([]CheckStepsRow, error) { @@ -423,13 +430,11 @@ func (q *Queries) CheckSteps(ctx context.Context, dollar_1 json.RawMessage) ([]C for rows.Next() { var i CheckStepsRow if err := rows.Scan( - &i.ID, &i.Amoid, &i.Pipelineid, &i.Accountid, &i.Name, &i.Color, - &i.Deleted, &i.Createdat, ); err != nil { return nil, err @@ -469,7 +474,14 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, Entity) DO NOTHING RETURNING id, amoid, accountid, entity, name, color, deleted, createdat ) -SELECT id, amoid, accountid, entity, name, color, deleted, createdat FROM inserted_tags +SELECT nt.amoid, nt.entity, nt.name, nt.color +FROM new_tags nt +WHERE NOT EXISTS ( + SELECT id, ins.amoid, accountid, entity, name, color, deleted, createdat, ud.amoid + FROM inserted_tags ins + JOIN user_data ud ON true + WHERE ins.amoID = nt.amoID AND ins.accountID = ud.amoid AND ins.Entity = nt.Entity +) ` type CheckTagsParams struct { @@ -478,14 +490,10 @@ type CheckTagsParams struct { } type CheckTagsRow struct { - ID int64 `db:"id" json:"id"` - Amoid int32 `db:"amoid" json:"amoid"` - Accountid int32 `db:"accountid" json:"accountid"` - Entity interface{} `db:"entity" json:"entity"` - Name string `db:"name" json:"name"` - Color string `db:"color" json:"color"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` + Amoid int32 `db:"amoid" json:"amoid"` + Entity interface{} `db:"entity" json:"entity"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` } func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]CheckTagsRow, error) { @@ -498,14 +506,10 @@ func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]CheckTa for rows.Next() { var i CheckTagsRow if err := rows.Scan( - &i.ID, &i.Amoid, - &i.Accountid, &i.Entity, &i.Name, &i.Color, - &i.Deleted, - &i.Createdat, ); err != nil { return nil, err } @@ -844,6 +848,15 @@ func (q *Queries) DeleteQuizByID(ctx context.Context, arg DeleteQuizByIDParams) return i, err } +const deletingUTM = `-- name: DeletingUTM :exec +UPDATE utms SET Deleted = true WHERE ID = ANY($1::int[]) +` + +func (q *Queries) DeletingUTM(ctx context.Context, dollar_1 []int32) error { + _, err := q.db.ExecContext(ctx, deletingUTM, pq.Array(dollar_1)) + return err +} + const deviceStatistics = `-- name: DeviceStatistics :many WITH DeviceStats AS ( SELECT @@ -2142,6 +2155,62 @@ func (q *Queries) GetTokenById(ctx context.Context, accountid string) (Token, er return i, err } +const getUTMsWithPagination = `-- name: GetUTMsWithPagination :many +SELECT ut.id, ut.amofieldid, ut.quizid, ut.accountid, ut.name, ut.deleted, ut.createdat, COUNT(*) OVER() as total_count +FROM utms ut JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON ut.AccountID = u.AmoID +WHERE ut.Deleted = false +ORDER BY ut.ID OFFSET ($2 - 1) * $3 LIMIT $3 +` + +type GetUTMsWithPaginationParams struct { + Accountid string `db:"accountid" json:"accountid"` + Column2 interface{} `db:"column_2" json:"column_2"` + Limit int32 `db:"limit" json:"limit"` +} + +type GetUTMsWithPaginationRow struct { + ID int64 `db:"id" json:"id"` + Amofieldid int32 `db:"amofieldid" json:"amofieldid"` + Quizid int32 `db:"quizid" json:"quizid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + TotalCount int64 `db:"total_count" json:"total_count"` +} + +func (q *Queries) GetUTMsWithPagination(ctx context.Context, arg GetUTMsWithPaginationParams) ([]GetUTMsWithPaginationRow, error) { + rows, err := q.db.QueryContext(ctx, getUTMsWithPagination, arg.Accountid, arg.Column2, arg.Limit) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetUTMsWithPaginationRow + for rows.Next() { + var i GetUTMsWithPaginationRow + if err := rows.Scan( + &i.ID, + &i.Amofieldid, + &i.Quizid, + &i.Accountid, + &i.Name, + &i.Deleted, + &i.Createdat, + &i.TotalCount, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getUsersWithPagination = `-- name: GetUsersWithPagination :many WITH user_data AS ( SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false @@ -2663,6 +2732,77 @@ func (q *Queries) QuizCopyQid(ctx context.Context, arg QuizCopyQidParams) (QuizC return i, err } +const saveUTMs = `-- name: SaveUTMs :many +WITH user_data AS ( + SELECT AmoID + FROM users + WHERE users.AccountID = $1 +), new_UTMs AS ( + SELECT (utm->>'AmoFieldID')::INT AS amoFieldID, + COALESCE(utm->>'QuizID', '')::INT AS quizID, + COALESCE(utm->>'Name', '')::varchar(50) AS name, + CURRENT_TIMESTAMP AS createdAt + FROM json_array_elements($2::json) AS utm +), inserted_utms AS( + INSERT INTO utms (AmoFieldID, QuizID, AccountID, Name, createdAt) + SELECT nu.amoFieldID, + nu.quizID, + ud.AmoID, + nu.name, + nu.createdAt + FROM new_UTMs nu + JOIN user_data ud ON true + RETURNING id, amofieldid, quizid, accountid, name, deleted, createdat +) +SELECT id, amofieldid, quizid, accountid, name, deleted, createdat from inserted_utms +` + +type SaveUTMsParams struct { + Accountid string `db:"accountid" json:"accountid"` + Column2 json.RawMessage `db:"column_2" json:"column_2"` +} + +type SaveUTMsRow struct { + ID int64 `db:"id" json:"id"` + Amofieldid int32 `db:"amofieldid" json:"amofieldid"` + Quizid int32 `db:"quizid" json:"quizid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +func (q *Queries) SaveUTMs(ctx context.Context, arg SaveUTMsParams) ([]SaveUTMsRow, error) { + rows, err := q.db.QueryContext(ctx, saveUTMs, arg.Accountid, arg.Column2) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SaveUTMsRow + for rows.Next() { + var i SaveUTMsRow + if err := rows.Scan( + &i.ID, + &i.Amofieldid, + &i.Quizid, + &i.Accountid, + &i.Name, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const softDeleteAccount = `-- name: SoftDeleteAccount :exec UPDATE users SET Deleted = TRUE WHERE AccountID = $1 ` From 33c04c20f31d91faa03892429eaf1cb9c6e9777e Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 17:43:11 +0300 Subject: [PATCH 070/187] update sqlc gen --- dal/db_query/queries.sql | 12 +++++++----- dal/sqlcgen/queries.sql.go | 26 ++++++++++++++++---------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index e4aaca3..01ba829 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -763,7 +763,7 @@ WHERE f.amoID = (update_data ->> 'AmoID')::INT -- name: CheckTags :many WITH user_data AS ( - SELECT AmoID + SELECT AmoID, AccountID FROM users WHERE users.AccountID = $1 ), new_tags AS ( @@ -785,8 +785,9 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, Entity) DO NOTHING RETURNING * ) -SELECT nt.* +SELECT nt.*,ud.AccountID FROM new_tags nt + JOIN user_data ud ON true WHERE NOT EXISTS ( SELECT * FROM inserted_tags ins @@ -823,7 +824,7 @@ WHERE NOT EXISTS ( -- name: CheckFields :many WITH user_data AS ( - SELECT AmoID + SELECT AmoID, AccountID FROM users WHERE users.AccountID = $1 ), new_fields AS ( @@ -848,8 +849,9 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, entity) DO NOTHING RETURNING * ) -SELECT nf.* +SELECT nf.*,ud.AccountID FROM new_fields nf + JOIN user_data ud ON true WHERE NOT EXISTS ( SELECT * FROM inserted_fields ins @@ -928,4 +930,4 @@ WITH user_data AS ( JOIN user_data ud ON true RETURNING * ) -SELECT * from inserted_utms; \ No newline at end of file +SELECT * from inserted_utms; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 8724dd3..3bd8c55 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -168,7 +168,7 @@ func (q *Queries) CheckExpired(ctx context.Context) ([]Token, error) { const checkFields = `-- name: CheckFields :many WITH user_data AS ( - SELECT AmoID + SELECT AmoID, AccountID FROM users WHERE users.AccountID = $1 ), new_fields AS ( @@ -193,10 +193,11 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, entity) DO NOTHING RETURNING id, amoid, code, accountid, name, entity, type, deleted, createdat ) -SELECT nf.amoid, nf.code, nf.name, nf.entity, nf.type, nf.createdat +SELECT nf.amoid, nf.code, nf.name, nf.entity, nf.type, nf.createdat,ud.AccountID FROM new_fields nf + JOIN user_data ud ON true WHERE NOT EXISTS ( - SELECT id, ins.amoid, code, accountid, name, entity, type, deleted, createdat, ud.amoid + SELECT id, ins.amoid, code, ins.accountid, name, entity, type, deleted, createdat, ud.amoid, ud.accountid FROM inserted_fields ins JOIN user_data ud ON true WHERE ins.amoID = nf.amoID AND ins.accountID = ud.amoid AND ins.Entity = nf.Entity @@ -215,6 +216,7 @@ type CheckFieldsRow struct { Entity interface{} `db:"entity" json:"entity"` Type string `db:"type" json:"type"` Createdat interface{} `db:"createdat" json:"createdat"` + Accountid string `db:"accountid" json:"accountid"` } func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]CheckFieldsRow, error) { @@ -233,6 +235,7 @@ func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]Che &i.Entity, &i.Type, &i.Createdat, + &i.Accountid, ); err != nil { return nil, err } @@ -452,7 +455,7 @@ func (q *Queries) CheckSteps(ctx context.Context, dollar_1 json.RawMessage) ([]C const checkTags = `-- name: CheckTags :many WITH user_data AS ( - SELECT AmoID + SELECT AmoID, AccountID FROM users WHERE users.AccountID = $1 ), new_tags AS ( @@ -474,10 +477,11 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, Entity) DO NOTHING RETURNING id, amoid, accountid, entity, name, color, deleted, createdat ) -SELECT nt.amoid, nt.entity, nt.name, nt.color +SELECT nt.amoid, nt.entity, nt.name, nt.color,ud.AccountID FROM new_tags nt + JOIN user_data ud ON true WHERE NOT EXISTS ( - SELECT id, ins.amoid, accountid, entity, name, color, deleted, createdat, ud.amoid + SELECT id, ins.amoid, ins.accountid, entity, name, color, deleted, createdat, ud.amoid, ud.accountid FROM inserted_tags ins JOIN user_data ud ON true WHERE ins.amoID = nt.amoID AND ins.accountID = ud.amoid AND ins.Entity = nt.Entity @@ -490,10 +494,11 @@ type CheckTagsParams struct { } type CheckTagsRow struct { - Amoid int32 `db:"amoid" json:"amoid"` - Entity interface{} `db:"entity" json:"entity"` - Name string `db:"name" json:"name"` - Color string `db:"color" json:"color"` + Amoid int32 `db:"amoid" json:"amoid"` + Entity interface{} `db:"entity" json:"entity"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` + Accountid string `db:"accountid" json:"accountid"` } func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]CheckTagsRow, error) { @@ -510,6 +515,7 @@ func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]CheckTa &i.Entity, &i.Name, &i.Color, + &i.Accountid, ); err != nil { return nil, err } From 4ca35ec382103763a301bee9e531cfcf925f4939 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 17:45:39 +0300 Subject: [PATCH 071/187] update sqlc gen --- dal/db_query/queries.sql | 8 ++++---- dal/sqlcgen/queries.sql.go | 28 ++++++++++++++-------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 01ba829..c609b4f 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -763,7 +763,7 @@ WHERE f.amoID = (update_data ->> 'AmoID')::INT -- name: CheckTags :many WITH user_data AS ( - SELECT AmoID, AccountID + SELECT AmoID FROM users WHERE users.AccountID = $1 ), new_tags AS ( @@ -785,7 +785,7 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, Entity) DO NOTHING RETURNING * ) -SELECT nt.*,ud.AccountID +SELECT nt.*,ud.AmoID FROM new_tags nt JOIN user_data ud ON true WHERE NOT EXISTS ( @@ -824,7 +824,7 @@ WHERE NOT EXISTS ( -- name: CheckFields :many WITH user_data AS ( - SELECT AmoID, AccountID + SELECT AmoID FROM users WHERE users.AccountID = $1 ), new_fields AS ( @@ -849,7 +849,7 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, entity) DO NOTHING RETURNING * ) -SELECT nf.*,ud.AccountID +SELECT nf.*,ud.AmoID FROM new_fields nf JOIN user_data ud ON true WHERE NOT EXISTS ( diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 3bd8c55..8f39d4f 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -168,7 +168,7 @@ func (q *Queries) CheckExpired(ctx context.Context) ([]Token, error) { const checkFields = `-- name: CheckFields :many WITH user_data AS ( - SELECT AmoID, AccountID + SELECT AmoID FROM users WHERE users.AccountID = $1 ), new_fields AS ( @@ -193,11 +193,11 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, entity) DO NOTHING RETURNING id, amoid, code, accountid, name, entity, type, deleted, createdat ) -SELECT nf.amoid, nf.code, nf.name, nf.entity, nf.type, nf.createdat,ud.AccountID +SELECT nf.amoid, nf.code, nf.name, nf.entity, nf.type, nf.createdat,ud.AmoID FROM new_fields nf JOIN user_data ud ON true WHERE NOT EXISTS ( - SELECT id, ins.amoid, code, ins.accountid, name, entity, type, deleted, createdat, ud.amoid, ud.accountid + SELECT id, ins.amoid, code, accountid, name, entity, type, deleted, createdat, ud.amoid FROM inserted_fields ins JOIN user_data ud ON true WHERE ins.amoID = nf.amoID AND ins.accountID = ud.amoid AND ins.Entity = nf.Entity @@ -216,7 +216,7 @@ type CheckFieldsRow struct { Entity interface{} `db:"entity" json:"entity"` Type string `db:"type" json:"type"` Createdat interface{} `db:"createdat" json:"createdat"` - Accountid string `db:"accountid" json:"accountid"` + Amoid_2 int32 `db:"amoid_2" json:"amoid_2"` } func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]CheckFieldsRow, error) { @@ -235,7 +235,7 @@ func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]Che &i.Entity, &i.Type, &i.Createdat, - &i.Accountid, + &i.Amoid_2, ); err != nil { return nil, err } @@ -455,7 +455,7 @@ func (q *Queries) CheckSteps(ctx context.Context, dollar_1 json.RawMessage) ([]C const checkTags = `-- name: CheckTags :many WITH user_data AS ( - SELECT AmoID, AccountID + SELECT AmoID FROM users WHERE users.AccountID = $1 ), new_tags AS ( @@ -477,11 +477,11 @@ WITH user_data AS ( ON CONFLICT (amoID, accountID, Entity) DO NOTHING RETURNING id, amoid, accountid, entity, name, color, deleted, createdat ) -SELECT nt.amoid, nt.entity, nt.name, nt.color,ud.AccountID +SELECT nt.amoid, nt.entity, nt.name, nt.color,ud.AmoID FROM new_tags nt JOIN user_data ud ON true WHERE NOT EXISTS ( - SELECT id, ins.amoid, ins.accountid, entity, name, color, deleted, createdat, ud.amoid, ud.accountid + SELECT id, ins.amoid, accountid, entity, name, color, deleted, createdat, ud.amoid FROM inserted_tags ins JOIN user_data ud ON true WHERE ins.amoID = nt.amoID AND ins.accountID = ud.amoid AND ins.Entity = nt.Entity @@ -494,11 +494,11 @@ type CheckTagsParams struct { } type CheckTagsRow struct { - Amoid int32 `db:"amoid" json:"amoid"` - Entity interface{} `db:"entity" json:"entity"` - Name string `db:"name" json:"name"` - Color string `db:"color" json:"color"` - Accountid string `db:"accountid" json:"accountid"` + Amoid int32 `db:"amoid" json:"amoid"` + Entity interface{} `db:"entity" json:"entity"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` + Amoid_2 int32 `db:"amoid_2" json:"amoid_2"` } func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]CheckTagsRow, error) { @@ -515,7 +515,7 @@ func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]CheckTa &i.Entity, &i.Name, &i.Color, - &i.Accountid, + &i.Amoid_2, ); err != nil { return nil, err } From bb9fb36a606aabda71757f7254e6aaa8d0947e01 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 17:48:44 +0300 Subject: [PATCH 072/187] update amo repo methods --- repository/amo/amo.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 8c53c8c..05ad493 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -314,12 +314,10 @@ func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pi var toUpdate []model.Pipeline for _, row := range rows { to := model.Pipeline{ - ID: row.ID, Amoid: row.Amoid, AccountID: row.Accountid, Name: row.Name, Isarchive: row.Isarchive, - Createdat: row.Createdat.Time.Unix(), } toUpdate = append(toUpdate, to) } @@ -389,13 +387,11 @@ func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) erro var toUpdate []model.Step for _, row := range rows { to := 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(), } toUpdate = append(toUpdate, to) } @@ -473,13 +469,11 @@ func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID v := string(row.Entity.([]byte)) entity = model.EntityType(v) to := model.Tag{ - ID: row.ID, Amoid: row.Amoid, - Accountid: row.Accountid, + Accountid: row.Amoid_2, Entity: entity, Name: row.Name, Color: &row.Color, - Createdat: row.Createdat.Time.Unix(), } toUpdate = append(toUpdate, to) } @@ -567,13 +561,11 @@ func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, t v := string(row.Entity.([]byte)) entity = model.EntityType(v) to := model.Field{ - ID: row.ID, Amoid: row.Amoid, Code: row.Code, - Accountid: row.Accountid, + Accountid: row.Amoid_2, Entity: entity, Type: row.Type, - Createdat: row.Createdat.Time.Unix(), } toUpdate = append(toUpdate, to) } From 60c71ffbc884769a4c3d2f3942beb4907c9e2f20 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 17:50:20 +0300 Subject: [PATCH 073/187] update amo repo methods --- repository/amo/amo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 05ad493..88c8190 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -611,7 +611,7 @@ func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.List return nil } -func (r *AmoRepository) SavingUserUtm(ctx context.Context, request *model.SaveUserListUTMReq, accountID string) (*model.ListSavedIDUTMResp, error) { +func (r *AmoRepository) SavingUserUtm(ctx context.Context, utms []model.UTM, accountID string) (*model.ListSavedIDUTMResp, error) { return nil, nil } From cc0512de5e15ee75469159f3b2d7a203238ce5b4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 18:13:59 +0300 Subject: [PATCH 074/187] update amo repo --- dal/db_query/queries.sql | 2 +- model/amo.go | 8 +++--- model/amoReq.go | 2 +- model/amoResp.go | 4 +-- repository/amo/amo.go | 56 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 62 insertions(+), 10 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index c609b4f..315a89b 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -905,7 +905,7 @@ UPDATE utms SET Deleted = true WHERE ID = ANY($1::int[]); -- name: GetUTMsWithPagination :many SELECT ut.*, COUNT(*) OVER() as total_count FROM utms ut JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON ut.AccountID = u.AmoID -WHERE ut.Deleted = false +WHERE ut.Deleted = false and ut.QuizID = $4 ORDER BY ut.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: SaveUTMs :many diff --git a/model/amo.go b/model/amo.go index caaad3e..12c7a75 100644 --- a/model/amo.go +++ b/model/amo.go @@ -164,13 +164,13 @@ type FieldRule struct { type UTM struct { /* - айдишник в нашей системе Primary Key*/ - ID int `json:"ID"` + ID int64 `json:"ID"` /* - айдишник кастомного поля в амо*/ - Amofieldid int `json:"AmoFieldID"` + Amofieldid int32 `json:"AmoFieldID"` /* - айдишник квиза*/ - Quizid int `json:"QuizID"` + Quizid int32 `json:"QuizID"` /* - связь с аккаунтом в интеграции амо id амо*/ - Accountid int `json:"AccountID"` + Accountid int32 `json:"AccountID"` /* - название тега в амо*/ Name string `json:"Name"` /* - флаг мягкого удаления*/ diff --git a/model/amoReq.go b/model/amoReq.go index 6628a93..915be29 100644 --- a/model/amoReq.go +++ b/model/amoReq.go @@ -2,7 +2,7 @@ package model type ListDeleteUTMIDsReq struct { /* - список айдишников utm которые удалить*/ - Utms []int `json:"utms"` + Utms []int32 `json:"utms"` } type PaginationReq struct { diff --git a/model/amoResp.go b/model/amoResp.go index 5b260f1..b23663e 100644 --- a/model/amoResp.go +++ b/model/amoResp.go @@ -26,14 +26,14 @@ type GetCurrentAccountResp struct { type GetListUserUTMResp struct { /* - общее количество юзеров, которые у нас закешированы для этого пользователя*/ - Count int `json:"count"` + Count int64 `json:"count"` /* - список юзеров, которые были закешированы нашим сервисом*/ Items []UTM `json:"items"` } type ListSavedIDUTMResp struct { /* - список айдишников сохранённых меток*/ - Ids []string `json:"IDs"` + Ids []int64 `json:"IDs"` } type UserListFieldsResp struct { diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 88c8190..f4f79a1 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -608,13 +608,65 @@ func (r *AmoRepository) GettingQuizRules(ctx context.Context) (*model.Rule, erro // методы UTMs func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq) error { + err := r.queries.DeletingUTM(ctx, request.Utms) + if err != nil { + return err + } return nil } func (r *AmoRepository) SavingUserUtm(ctx context.Context, utms []model.UTM, accountID string) (*model.ListSavedIDUTMResp, error) { - return nil, nil + 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 } func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.PaginationReq, accountID string, quizID int) (*model.GetListUserUTMResp, error) { - return nil, nil + rows, err := r.queries.GetUTMsWithPagination(ctx, sqlcgen.GetUTMsWithPaginationParams{ + Accountid: accountID, + Column2: request.Page, + Limit: request.Size, + }) + + 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 } From fe838742034337ccb5adc54b49b588fe6c67f966 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 18:15:03 +0300 Subject: [PATCH 075/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 8f39d4f..fbbfbba 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2164,7 +2164,7 @@ func (q *Queries) GetTokenById(ctx context.Context, accountid string) (Token, er const getUTMsWithPagination = `-- name: GetUTMsWithPagination :many SELECT ut.id, ut.amofieldid, ut.quizid, ut.accountid, ut.name, ut.deleted, ut.createdat, COUNT(*) OVER() as total_count FROM utms ut JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON ut.AccountID = u.AmoID -WHERE ut.Deleted = false +WHERE ut.Deleted = false and ut.QuizID = $4 ORDER BY ut.ID OFFSET ($2 - 1) * $3 LIMIT $3 ` @@ -2172,6 +2172,7 @@ type GetUTMsWithPaginationParams struct { Accountid string `db:"accountid" json:"accountid"` Column2 interface{} `db:"column_2" json:"column_2"` Limit int32 `db:"limit" json:"limit"` + Quizid int32 `db:"quizid" json:"quizid"` } type GetUTMsWithPaginationRow struct { @@ -2186,7 +2187,12 @@ type GetUTMsWithPaginationRow struct { } func (q *Queries) GetUTMsWithPagination(ctx context.Context, arg GetUTMsWithPaginationParams) ([]GetUTMsWithPaginationRow, error) { - rows, err := q.db.QueryContext(ctx, getUTMsWithPagination, arg.Accountid, arg.Column2, arg.Limit) + rows, err := q.db.QueryContext(ctx, getUTMsWithPagination, + arg.Accountid, + arg.Column2, + arg.Limit, + arg.Quizid, + ) if err != nil { return nil, err } From 18b9520cd3db71b4aa07b94b9d3faece43e78738 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 18:16:12 +0300 Subject: [PATCH 076/187] update amo repo --- repository/amo/amo.go | 1 + 1 file changed, 1 insertion(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index f4f79a1..c97d86e 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -641,6 +641,7 @@ func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.Pagin Accountid: accountID, Column2: request.Page, Limit: request.Size, + Quizid: int32(quizID), }) if err != nil { From e653229047e6401fb6d8acc5be3782b90a2c3a72 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 22 Apr 2024 18:23:54 +0300 Subject: [PATCH 077/187] add todo --- repository/amo/amo.go | 1 + 1 file changed, 1 insertion(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index c97d86e..e721a54 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -615,6 +615,7 @@ func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.List return nil } +// todo нужно ли тут ограничивать индексом что то func (r *AmoRepository) SavingUserUtm(ctx context.Context, utms []model.UTM, accountID string) (*model.ListSavedIDUTMResp, error) { column2, err := json.Marshal(utms) if err != nil { From ebf5c9f4de7ab9acf245babd51d0f36ad73c80ac Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Apr 2024 14:42:13 +0300 Subject: [PATCH 078/187] add rule querys --- dal/db_query/queries.sql | 13 +++++++++++++ model/amoReq.go | 7 +++++-- repository/amo/amo.go | 5 ++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 315a89b..4de2631 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -931,3 +931,16 @@ WITH user_data AS ( RETURNING * ) SELECT * from inserted_utms; + +-- name: GetQuizRule +SELECT * FROM rules WHERE QuizID = $1 AND Deleted = false; + +-- name: SetQuizSettings +INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, UTMS, FieldsRule) +SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, + $4 AS StepID,$5 AS UTMS,$6 AS FieldsRule FROM users u WHERE u.AccountID = $7; + +-- name: ChangeQuizSettings +UPDATE rules +SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 +WHERE AccountID = (SELECT AmoID FROM users WHERE AccountID = $6) AND QuizID = $7; diff --git a/model/amoReq.go b/model/amoReq.go index 915be29..4699560 100644 --- a/model/amoReq.go +++ b/model/amoReq.go @@ -13,8 +13,11 @@ type PaginationReq struct { } type RulesReq struct { - /* - ID квиза*/ - ID string `json:"ID"` + PerformerID int // айдишник ответственного за сделку + PipelineID int // айдишник воронки + StepID int // айдишник этапа + Utms []int // список UTM для этого опроса + Fieldsrule Fieldsrule // правила заполнения полей сущностей в амо } type SaveUserListUTMReq struct { diff --git a/repository/amo/amo.go b/repository/amo/amo.go index e721a54..168af0c 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -584,15 +584,14 @@ func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, t // методы rules -func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq) error { +func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error { //TODO:IMPLEMENT ME return nil } -func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.RulesReq) error { - //TODO:IMPLEMENT ME +func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error { return nil From b6b8f67cb654c36dd31b8eab420d6ac90588aa5c Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Apr 2024 14:48:15 +0300 Subject: [PATCH 079/187] update sqlc gen --- dal/db_query/queries.sql | 12 +++-- dal/sqlcgen/queries.sql.go | 108 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 5 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 4de2631..2789185 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -932,15 +932,17 @@ WITH user_data AS ( ) SELECT * from inserted_utms; --- name: GetQuizRule +-- name: GetQuizRule :one SELECT * FROM rules WHERE QuizID = $1 AND Deleted = false; --- name: SetQuizSettings +-- name: SetQuizSettings :one INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, UTMS, FieldsRule) SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, - $4 AS StepID,$5 AS UTMS,$6 AS FieldsRule FROM users u WHERE u.AccountID = $7; + $4 AS StepID,$5 AS UTMS,$6 AS FieldsRule FROM users u WHERE u.AccountID = $7 +RETURNING *; --- name: ChangeQuizSettings +-- name: ChangeQuizSettings :one UPDATE rules SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 -WHERE AccountID = (SELECT AmoID FROM users WHERE AccountID = $6) AND QuizID = $7; +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 +RETURNING *; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index fbbfbba..b7a0970 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -114,6 +114,49 @@ func (q *Queries) ArchiveQuiz(ctx context.Context, arg ArchiveQuizParams) error return err } +const changeQuizSettings = `-- name: ChangeQuizSettings :one +UPDATE rules +SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 +RETURNING id, accountid, quizid, performerid, pipelineid, stepid, utms, fieldsrule, deleted, createdat +` + +type ChangeQuizSettingsParams struct { + Performerid int32 `db:"performerid" json:"performerid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + Stepid int32 `db:"stepid" json:"stepid"` + Utms []int32 `db:"utms" json:"utms"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Accountid string `db:"accountid" json:"accountid"` + Quizid int32 `db:"quizid" json:"quizid"` +} + +func (q *Queries) ChangeQuizSettings(ctx context.Context, arg ChangeQuizSettingsParams) (Rule, error) { + row := q.db.QueryRowContext(ctx, changeQuizSettings, + arg.Performerid, + arg.Pipelineid, + arg.Stepid, + pq.Array(arg.Utms), + arg.Fieldsrule, + arg.Accountid, + arg.Quizid, + ) + var i Rule + err := row.Scan( + &i.ID, + &i.Accountid, + &i.Quizid, + &i.Performerid, + &i.Pipelineid, + &i.Stepid, + pq.Array(&i.Utms), + &i.Fieldsrule, + &i.Deleted, + &i.Createdat, + ) + return i, err +} + const checkAndAddDefault = `-- name: CheckAndAddDefault :exec UPDATE privileges SET amount = $1, created_at = NOW() @@ -1965,6 +2008,28 @@ func (q *Queries) GetQuizHistory(ctx context.Context, arg GetQuizHistoryParams) return items, nil } +const getQuizRule = `-- name: GetQuizRule :one +SELECT id, accountid, quizid, performerid, pipelineid, stepid, utms, fieldsrule, deleted, createdat FROM rules WHERE QuizID = $1 AND Deleted = false +` + +func (q *Queries) GetQuizRule(ctx context.Context, quizid int32) (Rule, error) { + row := q.db.QueryRowContext(ctx, getQuizRule, quizid) + var i Rule + err := row.Scan( + &i.ID, + &i.Accountid, + &i.Quizid, + &i.Performerid, + &i.Pipelineid, + &i.Stepid, + pq.Array(&i.Utms), + &i.Fieldsrule, + &i.Deleted, + &i.Createdat, + ) + return i, err +} + const getResultAnswers = `-- name: GetResultAnswers :many SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip FROM answer WHERE session = ( SELECT session FROM answer WHERE answer.id = $1) ORDER BY question_id, created_at DESC @@ -2815,6 +2880,49 @@ func (q *Queries) SaveUTMs(ctx context.Context, arg SaveUTMsParams) ([]SaveUTMsR return items, nil } +const setQuizSettings = `-- name: SetQuizSettings :one +INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, UTMS, FieldsRule) +SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, + $4 AS StepID,$5 AS UTMS,$6 AS FieldsRule FROM users u WHERE u.AccountID = $7 +RETURNING id, accountid, quizid, performerid, pipelineid, stepid, utms, fieldsrule, deleted, createdat +` + +type SetQuizSettingsParams struct { + Quizid int32 `db:"quizid" json:"quizid"` + Performerid int32 `db:"performerid" json:"performerid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + Stepid int32 `db:"stepid" json:"stepid"` + Utms []int32 `db:"utms" json:"utms"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Accountid string `db:"accountid" json:"accountid"` +} + +func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams) (Rule, error) { + row := q.db.QueryRowContext(ctx, setQuizSettings, + arg.Quizid, + arg.Performerid, + arg.Pipelineid, + arg.Stepid, + pq.Array(arg.Utms), + arg.Fieldsrule, + arg.Accountid, + ) + var i Rule + err := row.Scan( + &i.ID, + &i.Accountid, + &i.Quizid, + &i.Performerid, + &i.Pipelineid, + &i.Stepid, + pq.Array(&i.Utms), + &i.Fieldsrule, + &i.Deleted, + &i.Createdat, + ) + return i, err +} + const softDeleteAccount = `-- name: SoftDeleteAccount :exec UPDATE users SET Deleted = TRUE WHERE AccountID = $1 ` From 9cbe8b6b275b1b3d31a305228710314e38a01f79 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Apr 2024 15:04:24 +0300 Subject: [PATCH 080/187] init rule methods in amo repo --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 2 +- model/amo.go | 14 +++---- model/amoReq.go | 8 ++-- repository/amo/amo.go | 86 ++++++++++++++++++++++++++++++++++---- 5 files changed, 91 insertions(+), 21 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 2789185..1129581 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -944,5 +944,5 @@ RETURNING *; -- name: ChangeQuizSettings :one UPDATE rules SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 -WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false RETURNING *; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index b7a0970..7a03b14 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -117,7 +117,7 @@ func (q *Queries) ArchiveQuiz(ctx context.Context, arg ArchiveQuizParams) error const changeQuizSettings = `-- name: ChangeQuizSettings :one UPDATE rules SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 -WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false RETURNING id, accountid, quizid, performerid, pipelineid, stepid, utms, fieldsrule, deleted, createdat ` diff --git a/model/amo.go b/model/amo.go index 12c7a75..54aba09 100644 --- a/model/amo.go +++ b/model/amo.go @@ -129,19 +129,19 @@ const ( type Rule struct { /* - айдишник в нашей системе*/ - ID int `json:"ID"` + ID int64 `json:"ID"` /* - связь с аккаунтом в интеграции амо id в амо*/ - Accountid int `json:"AccountID"` + Accountid int32 `json:"AccountID"` /* - айдишник опроса*/ - Quizid int `json:"QuizID"` + Quizid int32 `json:"QuizID"` /* - айдишник ответственного за сделку*/ - Performerid int `json:"PerformerID"` + Performerid int32 `json:"PerformerID"` /* - айдишник воронки*/ - Pipelineid int `json:"PipelineID"` + Pipelineid int32 `json:"PipelineID"` /* - айдишник этапа*/ - Stepid int `json:"StepID"` + Stepid int32 `json:"StepID"` /* - список UTM для этого опроса*/ - Utms []int `json:"UTMs"` + Utms []int32 `json:"UTMs"` /* - правила заполнения полей сущностей в амо*/ Fieldsrule Fieldsrule `json:"FieldsRule"` /* - флаг мягкого удаления*/ diff --git a/model/amoReq.go b/model/amoReq.go index 4699560..81f7209 100644 --- a/model/amoReq.go +++ b/model/amoReq.go @@ -13,10 +13,10 @@ type PaginationReq struct { } type RulesReq struct { - PerformerID int // айдишник ответственного за сделку - PipelineID int // айдишник воронки - StepID int // айдишник этапа - Utms []int // список UTM для этого опроса + PerformerID int32 // айдишник ответственного за сделку + PipelineID int32 // айдишник воронки + StepID int32 // айдишник этапа + Utms []int32 // список UTM для этого опроса Fieldsrule Fieldsrule // правила заполнения полей сущностей в амо } diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 168af0c..74b33d3 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -584,24 +584,94 @@ func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, t // методы rules -func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error { - //TODO:IMPLEMENT ME +func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) (*model.Rule, error) { + jsonFieldRule, err := json.Marshal(request.Fieldsrule) + if err != nil { + return nil, err + } + row, err := r.queries.ChangeQuizSettings(ctx, sqlcgen.ChangeQuizSettingsParams{ + Performerid: request.PerformerID, + Pipelineid: request.PipelineID, + Stepid: request.StepID, + Utms: request.Utms, + Fieldsrule: jsonFieldRule, + Accountid: accountID, + Quizid: int32(quizID), + }) - return nil + var fieldsRule model.Fieldsrule + err = json.Unmarshal(row.Fieldsrule, &fieldsRule) + if err != nil { + return nil, err + } + return &model.Rule{ + ID: row.ID, + Accountid: row.Accountid, + Quizid: row.Quizid, + Performerid: row.Performerid, + Pipelineid: row.Pipelineid, + Stepid: row.Stepid, + Utms: row.Utms, + Fieldsrule: fieldsRule, + }, nil } -func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error { +func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) (*model.Rule, error) { + jsonFieldRule, err := json.Marshal(request.Fieldsrule) + if err != nil { + return nil, err + } + row, err := r.queries.SetQuizSettings(ctx, sqlcgen.SetQuizSettingsParams{ + Performerid: request.PerformerID, + Pipelineid: request.PipelineID, + Stepid: request.StepID, + Utms: request.Utms, + Fieldsrule: jsonFieldRule, + Accountid: accountID, + Quizid: int32(quizID), + }) - return nil + var fieldsRule model.Fieldsrule + err = json.Unmarshal(row.Fieldsrule, &fieldsRule) + if err != nil { + return nil, err + } + return &model.Rule{ + ID: row.ID, + Accountid: row.Accountid, + Quizid: row.Quizid, + Performerid: row.Performerid, + Pipelineid: row.Pipelineid, + Stepid: row.Stepid, + Utms: row.Utms, + Fieldsrule: fieldsRule, + }, nil } -func (r *AmoRepository) GettingQuizRules(ctx context.Context) (*model.Rule, error) { - //TODO:IMPLEMENT ME +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 + } - return &model.Rule{}, nil + var fieldsRule model.Fieldsrule + err = json.Unmarshal(row.Fieldsrule, &fieldsRule) + if err != nil { + return nil, err + } + return &model.Rule{ + ID: row.ID, + Accountid: row.Accountid, + Quizid: row.Quizid, + Performerid: row.Performerid, + Pipelineid: row.Pipelineid, + Stepid: row.Stepid, + Utms: row.Utms, + Fieldsrule: fieldsRule, + }, nil } // методы UTMs From 6d04e29714d06e3bc09bea74065770e831a7b7ba Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Apr 2024 17:02:36 +0300 Subject: [PATCH 081/187] add todo --- middleware/middleware.go | 1 + 1 file changed, 1 insertion(+) diff --git a/middleware/middleware.go b/middleware/middleware.go index bd4290d..0c596d7 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -36,6 +36,7 @@ func AnswererChain() fiber.Handler { func JWTAuth() fiber.Handler { return func(c *fiber.Ctx) error { + //todo также сделать для хуков на добавление удаление в амо if c.Path() == "/quiz/logo" { return c.Next() } From 5a487f69942e954991831e9e5a681a3cb358d8b2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Apr 2024 18:02:15 +0300 Subject: [PATCH 082/187] add webhook delete query --- dal/db_query/queries.sql | 9 ++++++++- repository/amo/amo.go | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 1129581..f3af91c 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -676,7 +676,14 @@ SELECT * FROM tokens; SELECT * FROM tokens WHERE Expiration <= TO_TIMESTAMP(EXTRACT(EPOCH FROM NOW()) + (10 * 60)); -- name: WebhookDelete :exec -DELETE FROM tokens WHERE AccountID = $1; +WITH userd AS ( + UPDATE users SET Deleted = true WHERE AmoUserID = $1 RETURNING AccountID +), +tokend AS ( + DELETE FROM tokens WHERE AccountID IN (SELECT AccountID FROM userd) RETURNING * +) +SELECT * FROM tokend; + -- name: SoftDeleteAccount :exec UPDATE users SET Deleted = TRUE WHERE AccountID = $1; diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 74b33d3..fd7524a 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -259,7 +259,7 @@ func (r *AmoRepository) GetAllTokens(ctx context.Context) ([]model.Token, error) return tokens, nil } -func (r *AmoRepository) WebhookDelete(ctx context.Context) error { +func (r *AmoRepository) WebhookDelete(ctx context.Context, amoID int) error { return nil From 268f16f54d1b4ace6c2db456b3214b1f97514e96 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Apr 2024 18:04:31 +0300 Subject: [PATCH 083/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 7a03b14..86899b3 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3066,11 +3066,17 @@ func (q *Queries) UpdateUsers(ctx context.Context, arg UpdateUsersParams) error } const webhookDelete = `-- name: WebhookDelete :exec -DELETE FROM tokens WHERE AccountID = $1 +WITH userd AS ( + UPDATE users SET Deleted = true WHERE AmoUserID = $1 RETURNING AccountID +), +tokend AS ( + DELETE FROM tokens WHERE AccountID IN (SELECT AccountID FROM userd) RETURNING accountid, refreshtoken, accesstoken, authcode, expiration, createdat +) +SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokend ` -func (q *Queries) WebhookDelete(ctx context.Context, accountid string) error { - _, err := q.db.ExecContext(ctx, webhookDelete, accountid) +func (q *Queries) WebhookDelete(ctx context.Context, amouserid int32) error { + _, err := q.db.ExecContext(ctx, webhookDelete, amouserid) return err } From 8f7c74046bec96b1e47c171461a77561b46301d4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 23 Apr 2024 18:06:39 +0300 Subject: [PATCH 084/187] add webhook repo method --- repository/amo/amo.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index fd7524a..a78d4a5 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -260,9 +260,11 @@ func (r *AmoRepository) GetAllTokens(ctx context.Context) ([]model.Token, error) } func (r *AmoRepository) WebhookDelete(ctx context.Context, amoID int) error { - + err := r.queries.WebhookDelete(ctx, int32(amoID)) + if err != nil { + return err + } return nil - } // методы pipelines From 6d80f5c5e87da34e18b12cc19b37e39f69dde234 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 28 Apr 2024 16:03:05 +0300 Subject: [PATCH 085/187] init field type enum --- dal/db_query/queries.sql | 4 ++-- dal/schema/000010_init.down.sql | 11 +++++++++-- dal/schema/000010_init.up.sql | 10 ++++++++-- model/amo.go | 31 ++++++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index f3af91c..ea8a617 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -761,7 +761,7 @@ WHERE s.amoID = (update_data ->> 'AmoID')::INT UPDATE fields AS f SET name = (update_data ->> 'Name')::varchar(50), code = (update_data ->> 'Code')::varchar(255), - type = (update_data ->> 'Type')::varchar(50), + type = (update_data ->> 'Type')::fieldtype, createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data WHERE f.amoID = (update_data ->> 'AmoID')::INT @@ -839,7 +839,7 @@ WITH user_data AS ( COALESCE(field->>'Code', '')::varchar(255) AS code, COALESCE(field->>'Name', '')::varchar(50) AS name, CAST(field->>'Entity' AS entitytype) AS Entity, - COALESCE(field->>'Type', '')::varchar(50) AS type, + COALESCE(field->>'Type', '')::fieldtype AS type, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($2::json) AS field ), inserted_fields AS( diff --git a/dal/schema/000010_init.down.sql b/dal/schema/000010_init.down.sql index 4b044b7..8316b7a 100644 --- a/dal/schema/000010_init.down.sql +++ b/dal/schema/000010_init.down.sql @@ -13,8 +13,15 @@ DO $$ BEGIN IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'entitytype') THEN DROP TYPE EntityType; - END IF; - END $$; + END IF; +END $$; + +DO $$ + BEGIN + IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'fieldtype') THEN + DROP TYPE FieldType; + END IF; +END $$; DROP TABLE IF EXISTS steps; DROP TABLE IF EXISTS pipelines; diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index fff445f..c106296 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -42,7 +42,6 @@ CREATE TABLE IF NOT EXISTS steps ( Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); - DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'entitytype') THEN @@ -50,6 +49,13 @@ DO $$ END IF; END $$; +DO $$ + BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'fieldtype') THEN + CREATE TYPE FieldType AS ENUM ('text', 'numeric', 'checkbox', 'select', 'multiselect', 'date', 'url', 'textarea', 'radiobutton', 'streetaddress', 'smart_address', 'birthday', 'legal_entity', 'date_time', 'price', 'category', 'items', 'tracking_data', 'linked_entity', 'chained_list', 'monetary', 'file', 'payer', 'supplier'); + END IF; +END $$; + CREATE TABLE IF NOT EXISTS fields ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL, -- айдишник кастомного поля в амо @@ -57,7 +63,7 @@ CREATE TABLE IF NOT EXISTS fields ( AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID неявная посредством join Name VARCHAR(50) NOT NULL DEFAULT '', -- название воронки в амо Entity EntityType NOT NULL, -- тип сущности в амо, для которой это кастомное поле - Type VARCHAR(50) NOT NULL DEFAULT '', -- тип поля + Type FieldType NOT NULL DEFAULT '', -- тип поля Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); diff --git a/model/amo.go b/model/amo.go index 54aba09..5208464 100644 --- a/model/amo.go +++ b/model/amo.go @@ -111,7 +111,7 @@ type Field struct { /* - тип сущности в амо, для которой это кастомное поле*/ Entity EntityType `json:"Entity"` /* - тип поля https://www.amocrm.ru/developers/content/crm_platform/custom-fields#%D0%94%D0%BE%D1%81%D1%82%D1%83%D0%BF%D0%BD%D1%8B%D0%B5-%D1%82%D0%B8%D0%BF%D1%8B-%D0%BF%D0%BE%D0%BB%D0%B5%D0%B9*/ - Type string `json:"Type"` + Type FieldType `json:"Type"` /* - флаг мягкого удаления*/ Deleted bool `json:"Deleted"` /* - таймштамп создания воронки в нашей системе*/ @@ -178,3 +178,32 @@ type UTM struct { /* - таймштамп создания тега в нашей системе*/ Createdat int64 `json:"CreatedAt"` } + +type FieldType string + +const ( + TypeAmoText FieldType = "text" //Текст + TypeAmoNumeric FieldType = "numeric" //Число + TypeAmoCheckbox FieldType = "checkbox" //Флаг + TypeAmoSelect FieldType = "select" //Список + TypeAmoMultiselect FieldType = "multiselect" //Мультисписок + TypeAmoDate FieldType = "date" //Дата + TypeAmoUrl FieldType = "url" // Ссылка + TypeAmoTextarea FieldType = "textarea" // Текстовая область + TypeAmoRadiobutton FieldType = "radiobutton" // Переключатель + TypeAmoStreetAddress FieldType = "streetaddress" // Короткий адрес + TypeAmoSmartAddress FieldType = "smart_address" // Адрес + TypeAmoBirthday FieldType = "birthday" // День рождения + TypeAmoLegalEntity FieldType = "legal_entity" // Юр. лицо + TypeAmoDateTime FieldType = "date_time" // Дата и время + TypeAmoPrice FieldType = "price" //Цена + TypeAmoCategory FieldType = "category" // Категория + TypeAmoItems FieldType = "items" // Предметы + TypeAmoTrackingData FieldType = "tracking_data" // Отслеживаемые данные + TypeAmoLinkedEntity FieldType = "linked_entity" // Связь с другим элементом + TypeAmoChainedList FieldType = "chained_list" // Каталоги и списки (платная опция Супер-поля) + TypeAmoMonetary FieldType = "monetary" // Денежное (платная опция Супер-поля) + TypeAmoFile FieldType = "file" // Файл + TypeAmoPayer FieldType = "payer" // Плательщик (только в списке Счета-покупки) + TypeAmoSupplier FieldType = "supplier" // Поставщик (только в списке Счета-покупки) +) From e4cdfcace223367aa20294e739addc823d98e8c6 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 28 Apr 2024 16:06:06 +0300 Subject: [PATCH 086/187] update sqlcgen with new enub type --- dal/sqlcgen/models.go | 2 +- dal/sqlcgen/queries.sql.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 06fd657..c8d9052 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -47,7 +47,7 @@ type Field struct { Accountid int32 `db:"accountid" json:"accountid"` Name string `db:"name" json:"name"` Entity interface{} `db:"entity" json:"entity"` - Type string `db:"type" json:"type"` + Type interface{} `db:"type" json:"type"` Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` } diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 86899b3..fe4d2b9 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -219,7 +219,7 @@ WITH user_data AS ( COALESCE(field->>'Code', '')::varchar(255) AS code, COALESCE(field->>'Name', '')::varchar(50) AS name, CAST(field->>'Entity' AS entitytype) AS Entity, - COALESCE(field->>'Type', '')::varchar(50) AS type, + COALESCE(field->>'Type', '')::fieldtype AS type, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($2::json) AS field ), inserted_fields AS( @@ -257,7 +257,7 @@ type CheckFieldsRow struct { Code string `db:"code" json:"code"` Name string `db:"name" json:"name"` Entity interface{} `db:"entity" json:"entity"` - Type string `db:"type" json:"type"` + Type interface{} `db:"type" json:"type"` Createdat interface{} `db:"createdat" json:"createdat"` Amoid_2 int32 `db:"amoid_2" json:"amoid_2"` } @@ -1475,7 +1475,7 @@ type GetFieldsWithPaginationRow struct { Accountid int32 `db:"accountid" json:"accountid"` Name string `db:"name" json:"name"` Entity interface{} `db:"entity" json:"entity"` - Type string `db:"type" json:"type"` + Type interface{} `db:"type" json:"type"` Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` TotalCount int64 `db:"total_count" json:"total_count"` @@ -2945,7 +2945,7 @@ const updateFields = `-- name: UpdateFields :exec UPDATE fields AS f SET name = (update_data ->> 'Name')::varchar(50), code = (update_data ->> 'Code')::varchar(255), - type = (update_data ->> 'Type')::varchar(50), + type = (update_data ->> 'Type')::fieldtype, createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data WHERE f.amoID = (update_data ->> 'AmoID')::INT From 07bf63c8dfe5e7a41a3bcd6ac22517ec7c7d49b0 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 28 Apr 2024 16:09:35 +0300 Subject: [PATCH 087/187] update amo repo --- repository/amo/amo.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index a78d4a5..59b7f6c 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -519,6 +519,10 @@ func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model. 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, @@ -526,7 +530,7 @@ func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model. Accountid: row.Accountid, Name: row.Name, Entity: entity, - Type: row.Type, + Type: fieldType, Createdat: row.Createdat.Time.Unix(), } @@ -562,12 +566,17 @@ func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, t 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) + to := model.Field{ Amoid: row.Amoid, Code: row.Code, Accountid: row.Amoid_2, Entity: entity, - Type: row.Type, + Type: fieldType, } toUpdate = append(toUpdate, to) } From 8768bbe3e68232c32f67727d57f2cc4fca1bcb5b Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 28 Apr 2024 17:18:05 +0300 Subject: [PATCH 088/187] get utms by ids list --- dal/db_query/queries.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index ea8a617..ac28521 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -953,3 +953,6 @@ UPDATE rules SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false RETURNING *; + +-- name: GetUserUTMByListIDs +SELECT * FROM utms WHERE ID = ANY($1::int[]) AND Deleted = false; \ No newline at end of file From a976fa2bc6a29567f2a7a9cf83fd4a98f8eb9791 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 28 Apr 2024 17:19:44 +0300 Subject: [PATCH 089/187] update sqlc gen --- dal/db_query/queries.sql | 4 ++-- dal/sqlcgen/queries.sql.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index ac28521..5627a2d 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -954,5 +954,5 @@ SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false RETURNING *; --- name: GetUserUTMByListIDs -SELECT * FROM utms WHERE ID = ANY($1::int[]) AND Deleted = false; \ No newline at end of file +-- name: GetUserUTMByListIDs :many +SELECT * FROM utms WHERE ID = ANY($1::int[]) AND Deleted = false; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index fe4d2b9..c88ef9d 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2288,6 +2288,41 @@ func (q *Queries) GetUTMsWithPagination(ctx context.Context, arg GetUTMsWithPagi return items, nil } +const getUserUTMByListIDs = `-- name: GetUserUTMByListIDs :many +SELECT id, amofieldid, quizid, accountid, name, deleted, createdat FROM utms WHERE ID = ANY($1::int[]) AND Deleted = false +` + +func (q *Queries) GetUserUTMByListIDs(ctx context.Context, dollar_1 []int32) ([]Utm, error) { + rows, err := q.db.QueryContext(ctx, getUserUTMByListIDs, pq.Array(dollar_1)) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Utm + for rows.Next() { + var i Utm + if err := rows.Scan( + &i.ID, + &i.Amofieldid, + &i.Quizid, + &i.Accountid, + &i.Name, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getUsersWithPagination = `-- name: GetUsersWithPagination :many WITH user_data AS ( SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false From 10012b3fc8ddf70818cca04af38db7867f051ebb Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 28 Apr 2024 17:26:04 +0300 Subject: [PATCH 090/187] init GetUserUTMByListIDs amo repo method --- repository/amo/amo.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 59b7f6c..90e2977 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -752,3 +752,27 @@ func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.Pagin Items: utmS, }, nil } + +func (r *AmoRepository) GetUserUTMByListIDs(ctx context.Context, ids []int32) ([]model.UTM, error) { + rows, err := r.queries.GetUserUTMByListIDs(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, + Deleted: row.Deleted, + Createdat: row.Createdat.Time.Unix(), + } + + utmS = append(utmS, utm) + } + + return utmS, nil +} From 017c49a59e258e4b3edeb4a6487d11ac16773e82 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 28 Apr 2024 17:48:06 +0300 Subject: [PATCH 091/187] add to GetUserUTMByListIDs retunrning entity field --- dal/db_query/queries.sql | 6 +++++- model/amo.go | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 5627a2d..d26184c 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -955,4 +955,8 @@ WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizI RETURNING *; -- name: GetUserUTMByListIDs :many -SELECT * FROM utms WHERE ID = ANY($1::int[]) AND Deleted = false; +SELECT utms.ID, utms.AmoFieldID, f.Entity AS AmoFieldType, utms.QuizID, utms.AccountID, utms.Name +FROM utms + JOIN fields f ON utms.AmoFieldID = f.AmoID +WHERE utms.ID = ANY($1::int[]) AND utms.Deleted = false; + diff --git a/model/amo.go b/model/amo.go index 5208464..17dec30 100644 --- a/model/amo.go +++ b/model/amo.go @@ -207,3 +207,12 @@ const ( TypeAmoPayer FieldType = "payer" // Плательщик (только в списке Счета-покупки) TypeAmoSupplier FieldType = "supplier" // Поставщик (только в списке Счета-покупки) ) + +type CheckUserUTM struct { + ID int64 `json:"ID"` + Amofieldid int32 `json:"AmoFieldID"` + AmoFieldType EntityType `json:"Entity"` + Quizid int32 `json:"QuizID"` + Accountid int32 `json:"AccountID"` + Name string `json:"Name"` +} From 0f28fe5439fd0d0f3a44379773d59cac77bafe0c Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 28 Apr 2024 17:49:17 +0300 Subject: [PATCH 092/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index c88ef9d..4d6d773 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2289,26 +2289,37 @@ func (q *Queries) GetUTMsWithPagination(ctx context.Context, arg GetUTMsWithPagi } const getUserUTMByListIDs = `-- name: GetUserUTMByListIDs :many -SELECT id, amofieldid, quizid, accountid, name, deleted, createdat FROM utms WHERE ID = ANY($1::int[]) AND Deleted = false +SELECT utms.ID, utms.AmoFieldID, f.Entity AS AmoFieldType, utms.QuizID, utms.AccountID, utms.Name +FROM utms + JOIN fields f ON utms.AmoFieldID = f.AmoID +WHERE utms.ID = ANY($1::int[]) AND utms.Deleted = false ` -func (q *Queries) GetUserUTMByListIDs(ctx context.Context, dollar_1 []int32) ([]Utm, error) { +type GetUserUTMByListIDsRow struct { + ID int64 `db:"id" json:"id"` + Amofieldid int32 `db:"amofieldid" json:"amofieldid"` + Amofieldtype interface{} `db:"amofieldtype" json:"amofieldtype"` + Quizid int32 `db:"quizid" json:"quizid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` +} + +func (q *Queries) GetUserUTMByListIDs(ctx context.Context, dollar_1 []int32) ([]GetUserUTMByListIDsRow, error) { rows, err := q.db.QueryContext(ctx, getUserUTMByListIDs, pq.Array(dollar_1)) if err != nil { return nil, err } defer rows.Close() - var items []Utm + var items []GetUserUTMByListIDsRow for rows.Next() { - var i Utm + var i GetUserUTMByListIDsRow if err := rows.Scan( &i.ID, &i.Amofieldid, + &i.Amofieldtype, &i.Quizid, &i.Accountid, &i.Name, - &i.Deleted, - &i.Createdat, ); err != nil { return nil, err } From e5a9eb4fe300440fb093da0cab06db11499fd3da Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 28 Apr 2024 17:55:00 +0300 Subject: [PATCH 093/187] update amo repo --- repository/amo/amo.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 90e2977..20b825f 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -753,22 +753,24 @@ func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.Pagin }, nil } -func (r *AmoRepository) GetUserUTMByListIDs(ctx context.Context, ids []int32) ([]model.UTM, error) { +func (r *AmoRepository) GetUserUTMByListIDs(ctx context.Context, ids []int32) ([]model.CheckUserUTM, error) { rows, err := r.queries.GetUserUTMByListIDs(ctx, ids) if err != nil { return nil, err } - var utmS []model.UTM + var utmS []model.CheckUserUTM for _, row := range rows { - 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(), + var entity model.EntityType + v := string(row.Amofieldtype.([]byte)) + entity = model.EntityType(v) + utm := model.CheckUserUTM{ + ID: row.ID, + Amofieldid: row.Amofieldid, + Quizid: row.Quizid, + Accountid: row.Accountid, + Name: row.Name, + AmoFieldType: entity, } utmS = append(utmS, utm) From 66093eefe34027ba71d1de89a663224a7c43c938 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 28 Apr 2024 23:41:22 +0300 Subject: [PATCH 094/187] amofieldID in utm may be null default 0 --- dal/schema/000010_init.up.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index c106296..b8922ce 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -81,7 +81,7 @@ CREATE TABLE IF NOT EXISTS tags ( CREATE TABLE IF NOT EXISTS utms ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, - AmoFieldID INT NOT NULL, -- id field в амо + AmoFieldID INT NOT NULL DEFAULT 0, -- id field в амо QuizID INT NOT NULL, -- id опроса AccountID INT NOT NULL, -- id аккаунта в амо AMOID Name VARCHAR(50) NOT NULL DEFAULT '', -- название utm From 1f5d80d625ebf09689852dd3c57fd33e1edb94eb Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 10:39:57 +0300 Subject: [PATCH 095/187] add check utms query --- dal/db_query/queries.sql | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index d26184c..c45dccd 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -954,9 +954,12 @@ SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false RETURNING *; --- name: GetUserUTMByListIDs :many -SELECT utms.ID, utms.AmoFieldID, f.Entity AS AmoFieldType, utms.QuizID, utms.AccountID, utms.Name -FROM utms - JOIN fields f ON utms.AmoFieldID = f.AmoID -WHERE utms.ID = ANY($1::int[]) AND utms.Deleted = false; +-- name: CheckUtms +SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name +FROM + utms u + LEFT JOIN + fields f ON u.Name = f.Name +WHERE + u.ID = ANY($1::int[]) AND u.Deleted = FALSE; From 785465b7ad205ef431e340e9facac391b05e0993 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 10:42:48 +0300 Subject: [PATCH 096/187] update sqlc gen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 93 +++++++++++++++++++------------------- 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index c45dccd..d6b2c99 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -954,7 +954,7 @@ SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false RETURNING *; --- name: CheckUtms +-- name: CheckUtms :many SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name FROM utms u diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 4d6d773..59e4ab0 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -600,6 +600,53 @@ func (q *Queries) CheckUsers(ctx context.Context, arg CheckUsersParams) error { return err } +const checkUtms = `-- name: CheckUtms :many +SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name +FROM + utms u + LEFT JOIN + fields f ON u.Name = f.Name +WHERE + u.ID = ANY($1::int[]) AND u.Deleted = FALSE +` + +type CheckUtmsRow struct { + ID int64 `db:"id" json:"id"` + Amofieldid int32 `db:"amofieldid" json:"amofieldid"` + Quizid int32 `db:"quizid" json:"quizid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` +} + +func (q *Queries) CheckUtms(ctx context.Context, dollar_1 []int32) ([]CheckUtmsRow, error) { + rows, err := q.db.QueryContext(ctx, checkUtms, pq.Array(dollar_1)) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CheckUtmsRow + for rows.Next() { + var i CheckUtmsRow + if err := rows.Scan( + &i.ID, + &i.Amofieldid, + &i.Quizid, + &i.Accountid, + &i.Name, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const copyQuestion = `-- name: CopyQuestion :one INSERT INTO question( quiz_id, title, description, questiontype, required, @@ -2288,52 +2335,6 @@ func (q *Queries) GetUTMsWithPagination(ctx context.Context, arg GetUTMsWithPagi return items, nil } -const getUserUTMByListIDs = `-- name: GetUserUTMByListIDs :many -SELECT utms.ID, utms.AmoFieldID, f.Entity AS AmoFieldType, utms.QuizID, utms.AccountID, utms.Name -FROM utms - JOIN fields f ON utms.AmoFieldID = f.AmoID -WHERE utms.ID = ANY($1::int[]) AND utms.Deleted = false -` - -type GetUserUTMByListIDsRow struct { - ID int64 `db:"id" json:"id"` - Amofieldid int32 `db:"amofieldid" json:"amofieldid"` - Amofieldtype interface{} `db:"amofieldtype" json:"amofieldtype"` - Quizid int32 `db:"quizid" json:"quizid"` - Accountid int32 `db:"accountid" json:"accountid"` - Name string `db:"name" json:"name"` -} - -func (q *Queries) GetUserUTMByListIDs(ctx context.Context, dollar_1 []int32) ([]GetUserUTMByListIDsRow, error) { - rows, err := q.db.QueryContext(ctx, getUserUTMByListIDs, pq.Array(dollar_1)) - if err != nil { - return nil, err - } - defer rows.Close() - var items []GetUserUTMByListIDsRow - for rows.Next() { - var i GetUserUTMByListIDsRow - if err := rows.Scan( - &i.ID, - &i.Amofieldid, - &i.Amofieldtype, - &i.Quizid, - &i.Accountid, - &i.Name, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - const getUsersWithPagination = `-- name: GetUsersWithPagination :many WITH user_data AS ( SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false From d06dd24c30561d5e41687ae1f326f19af4a3092f Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 10:47:20 +0300 Subject: [PATCH 097/187] update amo repo --- model/amo.go | 9 --------- repository/amo/amo.go | 22 +++++++++------------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/model/amo.go b/model/amo.go index 17dec30..5208464 100644 --- a/model/amo.go +++ b/model/amo.go @@ -207,12 +207,3 @@ const ( TypeAmoPayer FieldType = "payer" // Плательщик (только в списке Счета-покупки) TypeAmoSupplier FieldType = "supplier" // Поставщик (только в списке Счета-покупки) ) - -type CheckUserUTM struct { - ID int64 `json:"ID"` - Amofieldid int32 `json:"AmoFieldID"` - AmoFieldType EntityType `json:"Entity"` - Quizid int32 `json:"QuizID"` - Accountid int32 `json:"AccountID"` - Name string `json:"Name"` -} diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 20b825f..c5f0d93 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -753,24 +753,20 @@ func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.Pagin }, nil } -func (r *AmoRepository) GetUserUTMByListIDs(ctx context.Context, ids []int32) ([]model.CheckUserUTM, error) { - rows, err := r.queries.GetUserUTMByListIDs(ctx, ids) +func (r *AmoRepository) CheckUserUtms(ctx context.Context, ids []int32) ([]model.UTM, error) { + rows, err := r.queries.CheckUtms(ctx, ids) if err != nil { return nil, err } - var utmS []model.CheckUserUTM + var utmS []model.UTM for _, row := range rows { - var entity model.EntityType - v := string(row.Amofieldtype.([]byte)) - entity = model.EntityType(v) - utm := model.CheckUserUTM{ - ID: row.ID, - Amofieldid: row.Amofieldid, - Quizid: row.Quizid, - Accountid: row.Accountid, - Name: row.Name, - AmoFieldType: entity, + utm := model.UTM{ + ID: row.ID, + Amofieldid: row.Amofieldid, + Quizid: row.Quizid, + Accountid: row.Accountid, + Name: row.Name, } utmS = append(utmS, utm) From a6113819641d85b3c6e00c657991148bdc444ad9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 11:37:50 +0300 Subject: [PATCH 098/187] update sqlc gen --- dal/db_query/queries.sql | 4 ++-- dal/sqlcgen/queries.sql.go | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index d6b2c99..e615ef2 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -955,11 +955,11 @@ WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizI RETURNING *; -- name: CheckUtms :many -SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name +SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name,CASE WHEN f.Name IS NOT NULL THEN true ELSE false END AS Status FROM utms u LEFT JOIN fields f ON u.Name = f.Name WHERE - u.ID = ANY($1::int[]) AND u.Deleted = FALSE; + u.ID IN ($1)::int[] AND u.Deleted = FALSE; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 59e4ab0..dae14e0 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -601,13 +601,13 @@ func (q *Queries) CheckUsers(ctx context.Context, arg CheckUsersParams) error { } const checkUtms = `-- name: CheckUtms :many -SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name +SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name,CASE WHEN f.Name IS NOT NULL THEN true ELSE false END AS Status FROM utms u LEFT JOIN fields f ON u.Name = f.Name WHERE - u.ID = ANY($1::int[]) AND u.Deleted = FALSE + u.ID IN ($1)::int[] AND u.Deleted = FALSE ` type CheckUtmsRow struct { @@ -616,10 +616,11 @@ type CheckUtmsRow struct { Quizid int32 `db:"quizid" json:"quizid"` Accountid int32 `db:"accountid" json:"accountid"` Name string `db:"name" json:"name"` + Status bool `db:"status" json:"status"` } -func (q *Queries) CheckUtms(ctx context.Context, dollar_1 []int32) ([]CheckUtmsRow, error) { - rows, err := q.db.QueryContext(ctx, checkUtms, pq.Array(dollar_1)) +func (q *Queries) CheckUtms(ctx context.Context, id int64) ([]CheckUtmsRow, error) { + rows, err := q.db.QueryContext(ctx, checkUtms, id) if err != nil { return nil, err } @@ -633,6 +634,7 @@ func (q *Queries) CheckUtms(ctx context.Context, dollar_1 []int32) ([]CheckUtmsR &i.Quizid, &i.Accountid, &i.Name, + &i.Status, ); err != nil { return nil, err } From 019e94b8ffa224806967d0c7fb158442a751ee6d Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 11:41:16 +0300 Subject: [PATCH 099/187] update sqlc gen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index e615ef2..985753d 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -961,5 +961,5 @@ FROM LEFT JOIN fields f ON u.Name = f.Name WHERE - u.ID IN ($1)::int[] AND u.Deleted = FALSE; + u.ID = ANY($1::int[]) AND u.Deleted = FALSE; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index dae14e0..8f423ab 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -607,7 +607,7 @@ FROM LEFT JOIN fields f ON u.Name = f.Name WHERE - u.ID IN ($1)::int[] AND u.Deleted = FALSE + u.ID = ANY($1::int[]) AND u.Deleted = FALSE ` type CheckUtmsRow struct { @@ -619,8 +619,8 @@ type CheckUtmsRow struct { Status bool `db:"status" json:"status"` } -func (q *Queries) CheckUtms(ctx context.Context, id int64) ([]CheckUtmsRow, error) { - rows, err := q.db.QueryContext(ctx, checkUtms, id) +func (q *Queries) CheckUtms(ctx context.Context, dollar_1 []int32) ([]CheckUtmsRow, error) { + rows, err := q.db.QueryContext(ctx, checkUtms, pq.Array(dollar_1)) if err != nil { return nil, err } From 13aacb936b85ea855bd5faeac6ac9df3343ed8e1 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 11:45:00 +0300 Subject: [PATCH 100/187] update amo repo --- model/amo.go | 9 +++++++++ repository/amo/amo.go | 7 ++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/model/amo.go b/model/amo.go index 5208464..7527250 100644 --- a/model/amo.go +++ b/model/amo.go @@ -207,3 +207,12 @@ const ( TypeAmoPayer FieldType = "payer" // Плательщик (только в списке Счета-покупки) TypeAmoSupplier FieldType = "supplier" // Поставщик (только в списке Счета-покупки) ) + +type CheckUserUtm struct { + ID int64 `json:"ID"` + Amofieldid int32 `json:"AmoFieldID"` + Quizid int32 `json:"QuizID"` + Accountid int32 `json:"AccountID"` + Name string `json:"Name"` + Status bool +} diff --git a/repository/amo/amo.go b/repository/amo/amo.go index c5f0d93..f1a3a62 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -753,20 +753,21 @@ func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.Pagin }, nil } -func (r *AmoRepository) CheckUserUtms(ctx context.Context, ids []int32) ([]model.UTM, error) { +func (r *AmoRepository) CheckUserUtms(ctx context.Context, ids []int32) ([]model.CheckUserUtm, error) { rows, err := r.queries.CheckUtms(ctx, ids) if err != nil { return nil, err } - var utmS []model.UTM + var utmS []model.CheckUserUtm for _, row := range rows { - utm := model.UTM{ + utm := model.CheckUserUtm{ ID: row.ID, Amofieldid: row.Amofieldid, Quizid: row.Quizid, Accountid: row.Accountid, Name: row.Name, + Status: row.Status, } utmS = append(utmS, utm) From 6021decc203c99d682272e28cb8dd5f952c99e86 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 12:32:12 +0300 Subject: [PATCH 101/187] update schema --- dal/schema/000010_init.up.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index b8922ce..90084a9 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -63,7 +63,7 @@ CREATE TABLE IF NOT EXISTS fields ( AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID неявная посредством join Name VARCHAR(50) NOT NULL DEFAULT '', -- название воронки в амо Entity EntityType NOT NULL, -- тип сущности в амо, для которой это кастомное поле - Type FieldType NOT NULL DEFAULT '', -- тип поля + Type FieldType NOT NULL, -- тип поля Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); From 673f4ebb60d647a3c08c9476391e84119b149878 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 12:36:30 +0300 Subject: [PATCH 102/187] update schema --- dal/schema/000010_init.up.sql | 2 +- model/amo.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 90084a9..9efd74b 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -52,7 +52,7 @@ END $$; DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'fieldtype') THEN - CREATE TYPE FieldType AS ENUM ('text', 'numeric', 'checkbox', 'select', 'multiselect', 'date', 'url', 'textarea', 'radiobutton', 'streetaddress', 'smart_address', 'birthday', 'legal_entity', 'date_time', 'price', 'category', 'items', 'tracking_data', 'linked_entity', 'chained_list', 'monetary', 'file', 'payer', 'supplier'); + CREATE TYPE FieldType AS ENUM ('text', 'numeric', 'checkbox', 'select', 'multiselect', 'date', 'url', 'textarea', 'radiobutton', 'streetaddress', 'smart_address', 'birthday', 'legal_entity', 'date_time', 'price', 'category', 'items', 'tracking_data', 'linked_entity', 'chained_list', 'monetary', 'file', 'payer', 'supplier', 'multitext'); END IF; END $$; diff --git a/model/amo.go b/model/amo.go index 7527250..e1c9564 100644 --- a/model/amo.go +++ b/model/amo.go @@ -206,6 +206,7 @@ const ( TypeAmoFile FieldType = "file" // Файл TypeAmoPayer FieldType = "payer" // Плательщик (только в списке Счета-покупки) TypeAmoSupplier FieldType = "supplier" // Поставщик (только в списке Счета-покупки) + TypeAmoMultiText FieldType = "multitext" // что то чего нет в списке полей но она есть в амо)) ) type CheckUserUtm struct { From 2e8be2295394aa9461265c8de5b2d04d5b4a08ed Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 13:24:36 +0300 Subject: [PATCH 103/187] add new query for update utm --- dal/db_query/queries.sql | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 985753d..8a6e443 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -963,3 +963,10 @@ FROM WHERE u.ID = ANY($1::int[]) AND u.Deleted = FALSE; +-- name: UpdateUtms :exec +UPDATE utms AS u +SET name = (update_data ->> 'Name')::varchar(50), + AmoFieldID = (update_data ->> 'AmoFieldID')::INT +FROM json_array_elements($1::json) AS update_data +WHERE u.ID = (update_data ->> 'ID')::INT; + From 6581709257ba4431d2eda35a3e03e0b3e520e1ff Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 13:25:59 +0300 Subject: [PATCH 104/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 8f423ab..58ce483 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3114,6 +3114,19 @@ func (q *Queries) UpdateUsers(ctx context.Context, arg UpdateUsersParams) error return err } +const updateUtms = `-- name: UpdateUtms :exec +UPDATE utms AS u +SET name = (update_data ->> 'Name')::varchar(50), + AmoFieldID = (update_data ->> 'AmoFieldID')::INT +FROM json_array_elements($1::json) AS update_data +WHERE u.ID = (update_data ->> 'ID')::INT +` + +func (q *Queries) UpdateUtms(ctx context.Context, dollar_1 json.RawMessage) error { + _, err := q.db.ExecContext(ctx, updateUtms, dollar_1) + return err +} + const webhookDelete = `-- name: WebhookDelete :exec WITH userd AS ( UPDATE users SET Deleted = true WHERE AmoUserID = $1 RETURNING AccountID From d5f9e390b44d572b94a544a7857bbc643818ac89 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 13:30:33 +0300 Subject: [PATCH 105/187] add new amo repo method --- repository/amo/amo.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index f1a3a62..1eeefb0 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -775,3 +775,17 @@ func (r *AmoRepository) CheckUserUtms(ctx context.Context, ids []int32) ([]model 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 +} From 1be4abee5de7ab4f06d0e64f86aef5cd9f574252 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 13:52:46 +0300 Subject: [PATCH 106/187] add new query --- dal/db_query/queries.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 8a6e443..8b079b5 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -970,3 +970,6 @@ SET name = (update_data ->> 'Name')::varchar(50), FROM json_array_elements($1::json) AS update_data WHERE u.ID = (update_data ->> 'ID')::INT; +-- name UpdateUtmsFields :exec +UPDATE utms AS u SET AmoFieldID = f.AmoFieldID FROM fields AS f +WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE; \ No newline at end of file From 8d81ca1f3398e25d9891a48bc7d72a67c51cfaba Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 13:55:02 +0300 Subject: [PATCH 107/187] update sqlc gen --- dal/db_query/queries.sql | 4 ++-- dal/sqlcgen/queries.sql.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 8b079b5..9f671a1 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -970,6 +970,6 @@ SET name = (update_data ->> 'Name')::varchar(50), FROM json_array_elements($1::json) AS update_data WHERE u.ID = (update_data ->> 'ID')::INT; --- name UpdateUtmsFields :exec +-- name: UpdateUtmsFields :exec UPDATE utms AS u SET AmoFieldID = f.AmoFieldID FROM fields AS f -WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE; \ No newline at end of file +WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 58ce483..eb02844 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3127,6 +3127,16 @@ func (q *Queries) UpdateUtms(ctx context.Context, dollar_1 json.RawMessage) erro return err } +const updateUtmsFields = `-- name: UpdateUtmsFields :exec +UPDATE utms AS u SET AmoFieldID = f.AmoFieldID FROM fields AS f +WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE +` + +func (q *Queries) UpdateUtmsFields(ctx context.Context, dollar_1 []int32) error { + _, err := q.db.ExecContext(ctx, updateUtmsFields, pq.Array(dollar_1)) + return err +} + const webhookDelete = `-- name: WebhookDelete :exec WITH userd AS ( UPDATE users SET Deleted = true WHERE AmoUserID = $1 RETURNING AccountID From 94595c15b58235e95d669f0bec18001578ffb934 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 13:57:59 +0300 Subject: [PATCH 108/187] add new method to amo repo --- repository/amo/amo.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 1eeefb0..2863a7f 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -789,3 +789,12 @@ func (r *AmoRepository) UpdateUTMs(ctx context.Context, utms []model.UTM) error 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 +} From 642347a2474ff208ed1066838353fd7eef61dccb Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 15:19:26 +0300 Subject: [PATCH 109/187] fix query --- dal/sqlcgen/queries.sql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index eb02844..84db297 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3128,7 +3128,7 @@ func (q *Queries) UpdateUtms(ctx context.Context, dollar_1 json.RawMessage) erro } const updateUtmsFields = `-- name: UpdateUtmsFields :exec -UPDATE utms AS u SET AmoFieldID = f.AmoFieldID FROM fields AS f +UPDATE utms AS u SET AmoFieldID = f.AmoID FROM fields AS f WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE ` From dea1519ce2370be6eb895542bad40e02133107a2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 15:21:02 +0300 Subject: [PATCH 110/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 84db297..eb02844 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3128,7 +3128,7 @@ func (q *Queries) UpdateUtms(ctx context.Context, dollar_1 json.RawMessage) erro } const updateUtmsFields = `-- name: UpdateUtmsFields :exec -UPDATE utms AS u SET AmoFieldID = f.AmoID FROM fields AS f +UPDATE utms AS u SET AmoFieldID = f.AmoFieldID FROM fields AS f WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE ` From c5336475b2b9c85d8e7650b8ca21c2572f8506c1 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 29 Apr 2024 17:03:48 +0300 Subject: [PATCH 111/187] rework change tag type for all entity --- model/amo.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/model/amo.go b/model/amo.go index e1c9564..53b0f55 100644 --- a/model/amo.go +++ b/model/amo.go @@ -121,10 +121,10 @@ type Field struct { type EntityType string const ( - LeadsTags EntityType = "leads" - ContactsTags EntityType = "contacts" - CompaniesTags EntityType = "companies" - CustomersTags EntityType = "customers" + LeadsType EntityType = "leads" + ContactsType EntityType = "contacts" + CompaniesType EntityType = "companies" + CustomersType EntityType = "customers" ) type Rule struct { From 93bae2af57d7b77d406309d37c767fcd8e32ac6a Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 30 Apr 2024 00:24:45 +0300 Subject: [PATCH 112/187] chage CheckUtms --- dal/db_query/queries.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 9f671a1..dd12bce 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -955,7 +955,7 @@ WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizI RETURNING *; -- name: CheckUtms :many -SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name,CASE WHEN f.Name IS NOT NULL THEN true ELSE false END AS Status +SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name,f.Name FROM utms u LEFT JOIN From 283cc6926468a1ef3697c41681e84a2a720f8c61 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 30 Apr 2024 00:26:23 +0300 Subject: [PATCH 113/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index eb02844..5621b01 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -601,7 +601,7 @@ func (q *Queries) CheckUsers(ctx context.Context, arg CheckUsersParams) error { } const checkUtms = `-- name: CheckUtms :many -SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name,CASE WHEN f.Name IS NOT NULL THEN true ELSE false END AS Status +SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name,f.Name FROM utms u LEFT JOIN @@ -611,12 +611,12 @@ WHERE ` type CheckUtmsRow struct { - ID int64 `db:"id" json:"id"` - Amofieldid int32 `db:"amofieldid" json:"amofieldid"` - Quizid int32 `db:"quizid" json:"quizid"` - Accountid int32 `db:"accountid" json:"accountid"` - Name string `db:"name" json:"name"` - Status bool `db:"status" json:"status"` + ID int64 `db:"id" json:"id"` + Amofieldid int32 `db:"amofieldid" json:"amofieldid"` + Quizid int32 `db:"quizid" json:"quizid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Name_2 sql.NullString `db:"name_2" json:"name_2"` } func (q *Queries) CheckUtms(ctx context.Context, dollar_1 []int32) ([]CheckUtmsRow, error) { @@ -634,7 +634,7 @@ func (q *Queries) CheckUtms(ctx context.Context, dollar_1 []int32) ([]CheckUtmsR &i.Quizid, &i.Accountid, &i.Name, - &i.Status, + &i.Name_2, ); err != nil { return nil, err } From 0a8ce824fe960ad93b56cdf42dac27ebf96dc3b9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 30 Apr 2024 00:28:44 +0300 Subject: [PATCH 114/187] update amo repo method --- model/amo.go | 12 ++++++------ repository/amo/amo.go | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/model/amo.go b/model/amo.go index 53b0f55..f5732ad 100644 --- a/model/amo.go +++ b/model/amo.go @@ -210,10 +210,10 @@ const ( ) type CheckUserUtm struct { - ID int64 `json:"ID"` - Amofieldid int32 `json:"AmoFieldID"` - Quizid int32 `json:"QuizID"` - Accountid int32 `json:"AccountID"` - Name string `json:"Name"` - Status bool + ID int64 `json:"ID"` + Amofieldid int32 `json:"AmoFieldID"` + Quizid int32 `json:"QuizID"` + Accountid int32 `json:"AccountID"` + Name string `json:"Name"` + AmoFieldName string } diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 2863a7f..64ba6c6 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -762,12 +762,12 @@ func (r *AmoRepository) CheckUserUtms(ctx context.Context, ids []int32) ([]model var utmS []model.CheckUserUtm for _, row := range rows { utm := model.CheckUserUtm{ - ID: row.ID, - Amofieldid: row.Amofieldid, - Quizid: row.Quizid, - Accountid: row.Accountid, - Name: row.Name, - Status: row.Status, + ID: row.ID, + Amofieldid: row.Amofieldid, + Quizid: row.Quizid, + Accountid: row.Accountid, + Name: row.Name, + AmoFieldName: row.Name_2.String, } utmS = append(utmS, utm) From 9803bbdff8039c4a35a586f31650b5b8610ecb00 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 30 Apr 2024 00:42:31 +0300 Subject: [PATCH 115/187] update queries --- dal/db_query/queries.sql | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index dd12bce..45f5a7f 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -954,14 +954,16 @@ SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false RETURNING *; --- name: CheckUtms :many -SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name,f.Name -FROM - utms u - LEFT JOIN - fields f ON u.Name = f.Name +-- name: GetUtmsByID :many +SELECT ID,AmoFieldID,QuizID,AccountID,Name +FROM utms WHERE - u.ID = ANY($1::int[]) AND u.Deleted = FALSE; + ID = ANY($1::int[]) AND Deleted = FALSE; + +-- name: GetUserFieldsByID +SELECT ID,AmoID,Code,AccountID,Name,Entity,Type +FROM fields +WHERE AccountID = $1; -- name: UpdateUtms :exec UPDATE utms AS u From ff9d34fa841cd274f1c28970b317f0b9c3debfca Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 30 Apr 2024 00:45:58 +0300 Subject: [PATCH 116/187] update sqlc gen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 140 ++++++++++++++++++++++++------------- 2 files changed, 92 insertions(+), 50 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 45f5a7f..fab3858 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -960,7 +960,7 @@ FROM utms WHERE ID = ANY($1::int[]) AND Deleted = FALSE; --- name: GetUserFieldsByID +-- name: GetUserFieldsByID :many SELECT ID,AmoID,Code,AccountID,Name,Entity,Type FROM fields WHERE AccountID = $1; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 5621b01..db43aab 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -600,55 +600,6 @@ func (q *Queries) CheckUsers(ctx context.Context, arg CheckUsersParams) error { return err } -const checkUtms = `-- name: CheckUtms :many -SELECT u.ID,u.AmoFieldID,u.QuizID,u.AccountID,u.Name,f.Name -FROM - utms u - LEFT JOIN - fields f ON u.Name = f.Name -WHERE - u.ID = ANY($1::int[]) AND u.Deleted = FALSE -` - -type CheckUtmsRow struct { - ID int64 `db:"id" json:"id"` - Amofieldid int32 `db:"amofieldid" json:"amofieldid"` - Quizid int32 `db:"quizid" json:"quizid"` - Accountid int32 `db:"accountid" json:"accountid"` - Name string `db:"name" json:"name"` - Name_2 sql.NullString `db:"name_2" json:"name_2"` -} - -func (q *Queries) CheckUtms(ctx context.Context, dollar_1 []int32) ([]CheckUtmsRow, error) { - rows, err := q.db.QueryContext(ctx, checkUtms, pq.Array(dollar_1)) - if err != nil { - return nil, err - } - defer rows.Close() - var items []CheckUtmsRow - for rows.Next() { - var i CheckUtmsRow - if err := rows.Scan( - &i.ID, - &i.Amofieldid, - &i.Quizid, - &i.Accountid, - &i.Name, - &i.Name_2, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - const copyQuestion = `-- name: CopyQuestion :one INSERT INTO question( quiz_id, title, description, questiontype, required, @@ -2337,6 +2288,53 @@ func (q *Queries) GetUTMsWithPagination(ctx context.Context, arg GetUTMsWithPagi return items, nil } +const getUserFieldsByID = `-- name: GetUserFieldsByID :many +SELECT ID,AmoID,Code,AccountID,Name,Entity,Type +FROM fields +WHERE AccountID = $1 +` + +type GetUserFieldsByIDRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Code string `db:"code" json:"code"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Entity interface{} `db:"entity" json:"entity"` + Type interface{} `db:"type" json:"type"` +} + +func (q *Queries) GetUserFieldsByID(ctx context.Context, accountid int32) ([]GetUserFieldsByIDRow, error) { + rows, err := q.db.QueryContext(ctx, getUserFieldsByID, accountid) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetUserFieldsByIDRow + for rows.Next() { + var i GetUserFieldsByIDRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Code, + &i.Accountid, + &i.Name, + &i.Entity, + &i.Type, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getUsersWithPagination = `-- name: GetUsersWithPagination :many WITH user_data AS ( SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false @@ -2407,6 +2405,50 @@ func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPa return items, nil } +const getUtmsByID = `-- name: GetUtmsByID :many +SELECT ID,AmoFieldID,QuizID,AccountID,Name +FROM utms +WHERE + ID = ANY($1::int[]) AND Deleted = FALSE +` + +type GetUtmsByIDRow struct { + ID int64 `db:"id" json:"id"` + Amofieldid int32 `db:"amofieldid" json:"amofieldid"` + Quizid int32 `db:"quizid" json:"quizid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` +} + +func (q *Queries) GetUtmsByID(ctx context.Context, dollar_1 []int32) ([]GetUtmsByIDRow, error) { + rows, err := q.db.QueryContext(ctx, getUtmsByID, pq.Array(dollar_1)) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetUtmsByIDRow + for rows.Next() { + var i GetUtmsByIDRow + if err := rows.Scan( + &i.ID, + &i.Amofieldid, + &i.Quizid, + &i.Accountid, + &i.Name, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const insertAnswers = `-- name: InsertAnswers :exec INSERT INTO answer( content, From ccbdad7e6421d6d221dea36096bf900eabbf9a6a Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 30 Apr 2024 00:52:48 +0300 Subject: [PATCH 117/187] update amo repo methods --- model/amo.go | 9 -------- repository/amo/amo.go | 51 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/model/amo.go b/model/amo.go index f5732ad..38c557f 100644 --- a/model/amo.go +++ b/model/amo.go @@ -208,12 +208,3 @@ const ( TypeAmoSupplier FieldType = "supplier" // Поставщик (только в списке Счета-покупки) TypeAmoMultiText FieldType = "multitext" // что то чего нет в списке полей но она есть в амо)) ) - -type CheckUserUtm struct { - ID int64 `json:"ID"` - Amofieldid int32 `json:"AmoFieldID"` - Quizid int32 `json:"QuizID"` - Accountid int32 `json:"AccountID"` - Name string `json:"Name"` - AmoFieldName string -} diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 64ba6c6..5cd6cc3 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -593,6 +593,38 @@ func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, t return nil } +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 +} + // методы rules func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) (*model.Rule, error) { @@ -753,21 +785,20 @@ func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.Pagin }, nil } -func (r *AmoRepository) CheckUserUtms(ctx context.Context, ids []int32) ([]model.CheckUserUtm, error) { - rows, err := r.queries.CheckUtms(ctx, ids) +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.CheckUserUtm + var utmS []model.UTM for _, row := range rows { - utm := model.CheckUserUtm{ - ID: row.ID, - Amofieldid: row.Amofieldid, - Quizid: row.Quizid, - Accountid: row.Accountid, - Name: row.Name, - AmoFieldName: row.Name_2.String, + utm := model.UTM{ + ID: row.ID, + Amofieldid: row.Amofieldid, + Quizid: row.Quizid, + Accountid: row.Accountid, + Name: row.Name, } utmS = append(utmS, utm) From 6dedaa14a96bd86eff85f78bb63b97a2d0db542a Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 30 Apr 2024 01:24:22 +0300 Subject: [PATCH 118/187] missinf field name --- repository/amo/amo.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 5cd6cc3..84e7a40 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -577,9 +577,11 @@ func (r *AmoRepository) CheckFields(ctx context.Context, fields []model.Field, t Accountid: row.Amoid_2, Entity: entity, Type: fieldType, + Name: row.Name, } toUpdate = append(toUpdate, to) } + dollar1, err := json.Marshal(toUpdate) if err != nil { return err From 77355a3a328ab11a8dff09d6fd4e9feb0d66436e Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 30 Apr 2024 01:29:07 +0300 Subject: [PATCH 119/187] update sqlc gen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index fab3858..24f99d9 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -973,5 +973,5 @@ FROM json_array_elements($1::json) AS update_data WHERE u.ID = (update_data ->> 'ID')::INT; -- name: UpdateUtmsFields :exec -UPDATE utms AS u SET AmoFieldID = f.AmoFieldID FROM fields AS f +UPDATE utms AS u SET AmoFieldID = f.AmoID FROM fields AS f WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index db43aab..fd6b4b9 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3170,7 +3170,7 @@ func (q *Queries) UpdateUtms(ctx context.Context, dollar_1 json.RawMessage) erro } const updateUtmsFields = `-- name: UpdateUtmsFields :exec -UPDATE utms AS u SET AmoFieldID = f.AmoFieldID FROM fields AS f +UPDATE utms AS u SET AmoFieldID = f.AmoID FROM fields AS f WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE ` From f3052eef0ba46946522c42a12317c6161abe3815 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 10:33:15 +0300 Subject: [PATCH 120/187] add new sql query --- dal/db_query/queries.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 24f99d9..80e7f92 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -975,3 +975,6 @@ WHERE u.ID = (update_data ->> 'ID')::INT; -- name: UpdateUtmsFields :exec UPDATE utms AS u SET AmoFieldID = f.AmoID FROM fields AS f WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE; + +-- name: GetQuestionListByIDs :many +SELECT * FROM question WHERE id = ANY($1::int[]) AND deleted = FALSE \ No newline at end of file From 17d5622ed177e094c03eb4b117ebd640912c0986 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 10:35:17 +0300 Subject: [PATCH 121/187] update sqlc gen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 80e7f92..b790274 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -977,4 +977,4 @@ UPDATE utms AS u SET AmoFieldID = f.AmoID FROM fields AS f WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE; -- name: GetQuestionListByIDs :many -SELECT * FROM question WHERE id = ANY($1::int[]) AND deleted = FALSE \ No newline at end of file +SELECT * FROM question WHERE id = ANY($1::int[]) AND deleted = FALSE; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index fd6b4b9..c4c52d0 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1771,6 +1771,47 @@ func (q *Queries) GetQuestionHistory(ctx context.Context, arg GetQuestionHistory return items, nil } +const getQuestionListByIDs = `-- name: GetQuestionListByIDs :many +SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at FROM question WHERE id = ANY($1::int[]) AND deleted = FALSE +` + +func (q *Queries) GetQuestionListByIDs(ctx context.Context, dollar_1 []int32) ([]Question, error) { + rows, err := q.db.QueryContext(ctx, getQuestionListByIDs, pq.Array(dollar_1)) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Question + for rows.Next() { + var i Question + if err := rows.Scan( + &i.ID, + &i.QuizID, + &i.Title, + &i.Description, + &i.Questiontype, + &i.Required, + &i.Deleted, + &i.Page, + &i.Content, + &i.Version, + pq.Array(&i.ParentIds), + &i.CreatedAt, + &i.UpdatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getQuestionTitle = `-- name: GetQuestionTitle :one SELECT title, questiontype,page FROM question WHERE id = $1 ` From 5192490c05eaf1ebfee1821ff1e8d025b86b0145 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 10:42:31 +0300 Subject: [PATCH 122/187] add new que repo method, and add in amodal struct que repo --- dal/dal.go | 19 ++++++++++++------ repository/question/question.go | 34 +++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/dal/dal.go b/dal/dal.go index 3acc8a5..aa33fab 100644 --- a/dal/dal.go +++ b/dal/dal.go @@ -133,9 +133,10 @@ func (d *DAL) Init() error { } type AmoDal struct { - conn *sql.DB - queries *sqlcgen.Queries - AmoRepo *amo.AmoRepository + conn *sql.DB + queries *sqlcgen.Queries + AmoRepo *amo.AmoRepository + QuestionRepo *question.QuestionRepository } func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { @@ -158,10 +159,16 @@ func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { Pool: pool, }) + questionRepo := question.NewQuestionRepository(question.Deps{ + Queries: queries, + Pool: pool, + }) + return &AmoDal{ - conn: pool, - queries: queries, - AmoRepo: amoRepo, + conn: pool, + queries: queries, + AmoRepo: amoRepo, + QuestionRepo: questionRepo, }, nil } diff --git a/repository/question/question.go b/repository/question/question.go index 866a35b..88e33e0 100644 --- a/repository/question/question.go +++ b/repository/question/question.go @@ -245,12 +245,12 @@ func (r *QuestionRepository) UpdateQuestion(ctx context.Context, record model.Qu params = append(params, record.Required, record.Version, record.Id) var placeholders []any - for i:=1;i<=len(params);i++ { + for i := 1; i <= len(params); i++ { placeholders = append(placeholders, i) } query = fmt.Sprintf(query, placeholders...) - + _, err := r.pool.ExecContext(ctx, query, params...) return err } @@ -455,3 +455,33 @@ func (r *QuestionRepository) ForSortingResults(ctx context.Context, allAnswers [ return sortedAllAnswers, nil } + +func (r *QuestionRepository) GetQuestionListByIDs(ctx context.Context, ids []int32) ([]model.Question, error) { + rows, err := r.queries.GetQuestionListByIDs(ctx, ids) + if err != nil { + return nil, err + } + + var questions []model.Question + for _, row := range rows { + question := model.Question{ + Id: uint64(row.ID), + QuizId: uint64(row.QuizID), + Title: row.Title, + Description: row.Description.String, + Type: string(row.Questiontype.([]byte)), + Required: row.Required.Bool, + Deleted: row.Deleted.Bool, + Page: int(row.Page.Int16), + Content: row.Content.String, + Version: int(row.Version.Int16), + ParentIds: row.ParentIds, + CreatedAt: row.CreatedAt.Time, + UpdatedAt: row.UpdatedAt.Time, + } + + questions = append(questions, question) + } + + return questions, nil +} From c0fdc51a292be182a9997c180020fd831661cd5c Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 11:38:26 +0300 Subject: [PATCH 123/187] type mapping que types and amo fields types --- model/amo.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/model/amo.go b/model/amo.go index 38c557f..8a22d19 100644 --- a/model/amo.go +++ b/model/amo.go @@ -208,3 +208,18 @@ const ( TypeAmoSupplier FieldType = "supplier" // Поставщик (только в списке Счета-покупки) TypeAmoMultiText FieldType = "multitext" // что то чего нет в списке полей но она есть в амо)) ) + +var TypeMapping = map[string]FieldType{ + "variant": TypeAmoChainedList, + "images": TypeAmoFile, + "varimg": TypeAmoFile, + "file": TypeAmoFile, + "text": TypeAmoText, + "emoji": TypeAmoText, + "select": TypeAmoSelect, + "date": TypeAmoDate, + "number": TypeAmoNumeric, + "page": TypeAmoText, + "rating": TypeAmoText, + "result": TypeAmoText, +} From 23ddff02505eb509deeb809dea0e5531c2fe5474 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 12:33:36 +0300 Subject: [PATCH 124/187] update schema and add new query --- dal/db_query/queries.sql | 4 ++++ dal/schema/000010_init.down.sql | 1 + dal/schema/000010_init.up.sql | 11 ++++++----- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index b790274..0a4a753 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -978,3 +978,7 @@ WHERE u.Name = f.Name AND u.ID = ANY($1::int[]) AND u.Deleted = FALSE; -- name: GetQuestionListByIDs :many SELECT * FROM question WHERE id = ANY($1::int[]) AND deleted = FALSE; + +-- name: UpdateFieldRules :exec +UPDATE rules SET FieldsRule = $1 +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $2) AND QuizID = $3 AND Deleted = false; diff --git a/dal/schema/000010_init.down.sql b/dal/schema/000010_init.down.sql index 8316b7a..dfe3f54 100644 --- a/dal/schema/000010_init.down.sql +++ b/dal/schema/000010_init.down.sql @@ -3,6 +3,7 @@ DROP INDEX IF EXISTS idx_unique_pipeline; DROP INDEX IF EXISTS idx_unique_step; DROP INDEX IF EXISTS idx_unique_field; DROP INDEX IF EXISTS idx_unique_tag; +DROP INDEX IF EXISTS idx_unique_rules; DROP TABLE IF EXISTS rules; DROP TABLE IF EXISTS utms; diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 9efd74b..1799ab5 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -102,8 +102,9 @@ CREATE TABLE IF NOT EXISTS rules ( CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -CREATE UNIQUE INDEX idx_unique_users ON users (amoID); -CREATE UNIQUE INDEX idx_unique_pipeline ON pipelines (amoID, accountID); -CREATE UNIQUE INDEX idx_unique_step ON steps (amoID, accountID, PipelineID); -CREATE UNIQUE INDEX idx_unique_field ON fields (amoID, accountID, entity); -CREATE UNIQUE INDEX idx_unique_tag ON tags (amoID, accountID, entity); +CREATE UNIQUE INDEX idx_unique_users ON users (amoID) WHERE Deleted = false; +CREATE UNIQUE INDEX idx_unique_pipeline ON pipelines (amoID, accountID) WHERE Deleted = false; +CREATE UNIQUE INDEX idx_unique_step ON steps (amoID, accountID, PipelineID) WHERE Deleted = false; +CREATE UNIQUE INDEX idx_unique_field ON fields (amoID, accountID, entity) WHERE Deleted = false; +CREATE UNIQUE INDEX idx_unique_tag ON tags (amoID, accountID, entity) WHERE Deleted = false; +CREATE UNIQUE INDEX idx_unique_rules ON rules (accountID, QuizID) WHERE Deleted = false; From 71d30df53fde211939347f762008a394e6c18820 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 12:35:03 +0300 Subject: [PATCH 125/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index c4c52d0..96749b7 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3073,6 +3073,22 @@ func (q *Queries) SoftDeleteResultByID(ctx context.Context, id int64) error { return err } +const updateFieldRules = `-- name: UpdateFieldRules :exec +UPDATE rules SET FieldsRule = $1 +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $2) AND QuizID = $3 AND Deleted = false +` + +type UpdateFieldRulesParams struct { + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Accountid string `db:"accountid" json:"accountid"` + Quizid int32 `db:"quizid" json:"quizid"` +} + +func (q *Queries) UpdateFieldRules(ctx context.Context, arg UpdateFieldRulesParams) error { + _, err := q.db.ExecContext(ctx, updateFieldRules, arg.Fieldsrule, arg.Accountid, arg.Quizid) + return err +} + const updateFields = `-- name: UpdateFields :exec UPDATE fields AS f SET name = (update_data ->> 'Name')::varchar(50), From e7ca25ad580f64ad1c0650c3a9e84b01e34b895e Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 12:40:25 +0300 Subject: [PATCH 126/187] add new amo repo method --- repository/amo/amo.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 84e7a40..203b53b 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -719,6 +719,24 @@ func (r *AmoRepository) GettingQuizRules(ctx context.Context, quizID int) (*mode }, nil } +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 +} + // методы UTMs func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq) error { From ab927207fd810ad3c1e21fe776ddac0ee0d1a39b Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 12:53:29 +0300 Subject: [PATCH 127/187] update schema --- dal/schema/000010_init.up.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 1799ab5..85b8042 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -102,7 +102,7 @@ CREATE TABLE IF NOT EXISTS rules ( CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -CREATE UNIQUE INDEX idx_unique_users ON users (amoID) WHERE Deleted = false; +CREATE UNIQUE INDEX idx_unique_users ON users (amoID); CREATE UNIQUE INDEX idx_unique_pipeline ON pipelines (amoID, accountID) WHERE Deleted = false; CREATE UNIQUE INDEX idx_unique_step ON steps (amoID, accountID, PipelineID) WHERE Deleted = false; CREATE UNIQUE INDEX idx_unique_field ON fields (amoID, accountID, entity) WHERE Deleted = false; From f784e0847dc99fd50af9d2a2d8ce79ce8b332864 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 12:58:06 +0300 Subject: [PATCH 128/187] update schema --- dal/schema/000010_init.up.sql | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 85b8042..490a072 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -103,8 +103,8 @@ CREATE TABLE IF NOT EXISTS rules ( ); CREATE UNIQUE INDEX idx_unique_users ON users (amoID); -CREATE UNIQUE INDEX idx_unique_pipeline ON pipelines (amoID, accountID) WHERE Deleted = false; -CREATE UNIQUE INDEX idx_unique_step ON steps (amoID, accountID, PipelineID) WHERE Deleted = false; -CREATE UNIQUE INDEX idx_unique_field ON fields (amoID, accountID, entity) WHERE Deleted = false; -CREATE UNIQUE INDEX idx_unique_tag ON tags (amoID, accountID, entity) WHERE Deleted = false; -CREATE UNIQUE INDEX idx_unique_rules ON rules (accountID, QuizID) WHERE Deleted = false; +CREATE UNIQUE INDEX idx_unique_pipeline ON pipelines (amoID, accountID); +CREATE UNIQUE INDEX idx_unique_step ON steps (amoID, accountID, PipelineID); +CREATE UNIQUE INDEX idx_unique_field ON fields (amoID, accountID, entity); +CREATE UNIQUE INDEX idx_unique_tag ON tags (amoID, accountID, entity); +CREATE UNIQUE INDEX idx_unique_rules ON rules (accountID, QuizID); From b7c2876b4427ddf952152f9827404c3f2f5ab495 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 14:05:33 +0300 Subject: [PATCH 129/187] update queries --- dal/db_query/queries.sql | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 0a4a753..8a12354 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -945,14 +945,12 @@ SELECT * FROM rules WHERE QuizID = $1 AND Deleted = false; -- name: SetQuizSettings :one INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, UTMS, FieldsRule) SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, - $4 AS StepID,$5 AS UTMS,$6 AS FieldsRule FROM users u WHERE u.AccountID = $7 -RETURNING *; + $4 AS StepID,$5 AS UTMS,$6 AS FieldsRule FROM users u WHERE u.AccountID = $7; --- name: ChangeQuizSettings :one +-- name: ChangeQuizSettings :exec UPDATE rules SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 -WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false -RETURNING *; +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false; -- name: GetUtmsByID :many SELECT ID,AmoFieldID,QuizID,AccountID,Name From 4cdd0c27842366beafa8e8230167b653607e6201 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 14:06:49 +0300 Subject: [PATCH 130/187] update sqlc gen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 44 +++++++------------------------------- 2 files changed, 9 insertions(+), 37 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 8a12354..c5a61e8 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -942,7 +942,7 @@ SELECT * from inserted_utms; -- name: GetQuizRule :one SELECT * FROM rules WHERE QuizID = $1 AND Deleted = false; --- name: SetQuizSettings :one +-- name: SetQuizSettings :exec INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, UTMS, FieldsRule) SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, $4 AS StepID,$5 AS UTMS,$6 AS FieldsRule FROM users u WHERE u.AccountID = $7; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 96749b7..5f68d3b 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -114,11 +114,10 @@ func (q *Queries) ArchiveQuiz(ctx context.Context, arg ArchiveQuizParams) error return err } -const changeQuizSettings = `-- name: ChangeQuizSettings :one +const changeQuizSettings = `-- name: ChangeQuizSettings :exec UPDATE rules SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false -RETURNING id, accountid, quizid, performerid, pipelineid, stepid, utms, fieldsrule, deleted, createdat ` type ChangeQuizSettingsParams struct { @@ -131,8 +130,8 @@ type ChangeQuizSettingsParams struct { Quizid int32 `db:"quizid" json:"quizid"` } -func (q *Queries) ChangeQuizSettings(ctx context.Context, arg ChangeQuizSettingsParams) (Rule, error) { - row := q.db.QueryRowContext(ctx, changeQuizSettings, +func (q *Queries) ChangeQuizSettings(ctx context.Context, arg ChangeQuizSettingsParams) error { + _, err := q.db.ExecContext(ctx, changeQuizSettings, arg.Performerid, arg.Pipelineid, arg.Stepid, @@ -141,20 +140,7 @@ func (q *Queries) ChangeQuizSettings(ctx context.Context, arg ChangeQuizSettings arg.Accountid, arg.Quizid, ) - var i Rule - err := row.Scan( - &i.ID, - &i.Accountid, - &i.Quizid, - &i.Performerid, - &i.Pipelineid, - &i.Stepid, - pq.Array(&i.Utms), - &i.Fieldsrule, - &i.Deleted, - &i.Createdat, - ) - return i, err + return err } const checkAndAddDefault = `-- name: CheckAndAddDefault :exec @@ -3012,11 +2998,10 @@ func (q *Queries) SaveUTMs(ctx context.Context, arg SaveUTMsParams) ([]SaveUTMsR return items, nil } -const setQuizSettings = `-- name: SetQuizSettings :one +const setQuizSettings = `-- name: SetQuizSettings :exec INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, UTMS, FieldsRule) SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, $4 AS StepID,$5 AS UTMS,$6 AS FieldsRule FROM users u WHERE u.AccountID = $7 -RETURNING id, accountid, quizid, performerid, pipelineid, stepid, utms, fieldsrule, deleted, createdat ` type SetQuizSettingsParams struct { @@ -3029,8 +3014,8 @@ type SetQuizSettingsParams struct { Accountid string `db:"accountid" json:"accountid"` } -func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams) (Rule, error) { - row := q.db.QueryRowContext(ctx, setQuizSettings, +func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams) error { + _, err := q.db.ExecContext(ctx, setQuizSettings, arg.Quizid, arg.Performerid, arg.Pipelineid, @@ -3039,20 +3024,7 @@ func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams arg.Fieldsrule, arg.Accountid, ) - var i Rule - err := row.Scan( - &i.ID, - &i.Accountid, - &i.Quizid, - &i.Performerid, - &i.Pipelineid, - &i.Stepid, - pq.Array(&i.Utms), - &i.Fieldsrule, - &i.Deleted, - &i.Createdat, - ) - return i, err + return err } const softDeleteAccount = `-- name: SoftDeleteAccount :exec From 53df9a4f3ee74fc60762c900518da086a53ccd7e Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 14:11:38 +0300 Subject: [PATCH 131/187] update amo repo methods --- repository/amo/amo.go | 42 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 203b53b..204361e 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -629,12 +629,12 @@ func (r *AmoRepository) GetUserFieldsByID(ctx context.Context, accountID int32) // методы rules -func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) (*model.Rule, error) { +func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error { jsonFieldRule, err := json.Marshal(request.Fieldsrule) if err != nil { - return nil, err + return err } - row, err := r.queries.ChangeQuizSettings(ctx, sqlcgen.ChangeQuizSettingsParams{ + err = r.queries.ChangeQuizSettings(ctx, sqlcgen.ChangeQuizSettingsParams{ Performerid: request.PerformerID, Pipelineid: request.PipelineID, Stepid: request.StepID, @@ -644,30 +644,19 @@ func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.R Quizid: int32(quizID), }) - var fieldsRule model.Fieldsrule - err = json.Unmarshal(row.Fieldsrule, &fieldsRule) if err != nil { - return nil, err + return err } - return &model.Rule{ - ID: row.ID, - Accountid: row.Accountid, - Quizid: row.Quizid, - Performerid: row.Performerid, - Pipelineid: row.Pipelineid, - Stepid: row.Stepid, - Utms: row.Utms, - Fieldsrule: fieldsRule, - }, nil + return nil } -func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) (*model.Rule, error) { +func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error { jsonFieldRule, err := json.Marshal(request.Fieldsrule) if err != nil { - return nil, err + return err } - row, err := r.queries.SetQuizSettings(ctx, sqlcgen.SetQuizSettingsParams{ + err = r.queries.SetQuizSettings(ctx, sqlcgen.SetQuizSettingsParams{ Performerid: request.PerformerID, Pipelineid: request.PipelineID, Stepid: request.StepID, @@ -677,22 +666,11 @@ func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.Rule Quizid: int32(quizID), }) - var fieldsRule model.Fieldsrule - err = json.Unmarshal(row.Fieldsrule, &fieldsRule) if err != nil { - return nil, err + return err } - return &model.Rule{ - ID: row.ID, - Accountid: row.Accountid, - Quizid: row.Quizid, - Performerid: row.Performerid, - Pipelineid: row.Pipelineid, - Stepid: row.Stepid, - Utms: row.Utms, - Fieldsrule: fieldsRule, - }, nil + return nil } func (r *AmoRepository) GettingQuizRules(ctx context.Context, quizID int) (*model.Rule, error) { From a865b8f3f4d82a6d70826d6d08507f59a942ff98 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 15:32:13 +0300 Subject: [PATCH 132/187] update schema --- dal/db_query/queries.sql | 20 ++++++++++---------- dal/schema/000010_init.up.sql | 12 ++++++------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index c5a61e8..f09ed79 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -730,7 +730,7 @@ ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: UpdateTags :exec UPDATE tags AS t -SET name = (update_data ->> 'Name')::varchar(50), +SET name = (update_data ->> 'Name')::varchar(1024), color = (update_data ->> 'Color')::varchar(50), createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -740,7 +740,7 @@ WHERE t.amoID = (update_data ->> 'AmoID')::INT -- name: UpdatePipelines :exec UPDATE pipelines AS p -SET name = (update_data ->> 'Name')::varchar(50), +SET name = (update_data ->> 'Name')::varchar(1024), isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END, createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -749,7 +749,7 @@ WHERE p.amoID = (update_data ->> 'AmoID')::INT -- name: UpdateSteps :exec UPDATE steps AS s -SET name = (update_data ->> 'Name')::varchar(50), +SET name = (update_data ->> 'Name')::varchar(1024), color = (update_data ->> 'Color')::varchar(50), createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -759,7 +759,7 @@ WHERE s.amoID = (update_data ->> 'AmoID')::INT -- name: UpdateFields :exec UPDATE fields AS f -SET name = (update_data ->> 'Name')::varchar(50), +SET name = (update_data ->> 'Name')::varchar(1024), code = (update_data ->> 'Code')::varchar(255), type = (update_data ->> 'Type')::fieldtype, createdAt = CURRENT_TIMESTAMP @@ -776,7 +776,7 @@ WITH user_data AS ( ), new_tags AS ( SELECT (tag->>'AmoID')::INT AS amoID, (tag->>'Entity')::entitytype AS Entity, - COALESCE(tag->>'Name', '')::VARCHAR(50) AS name, + COALESCE(tag->>'Name', '')::VARCHAR(1024) AS name, COALESCE(tag->>'Color', '')::VARCHAR(50) AS color FROM json_array_elements($2::json) AS tag ), inserted_tags AS ( @@ -806,7 +806,7 @@ WHERE NOT EXISTS ( WITH new_pipelines AS ( SELECT (pipeline->>'AmoID')::INT AS amoID, (pipeline->>'AccountID')::INT AS accountID, - COALESCE(pipeline->>'Name', '')::varchar(50) AS name, + COALESCE(pipeline->>'Name', '')::varchar(1024) AS name, CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END AS isArchive, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS pipeline @@ -837,7 +837,7 @@ WITH user_data AS ( ), new_fields AS ( SELECT (field->>'AmoID')::INT AS amoID, COALESCE(field->>'Code', '')::varchar(255) AS code, - COALESCE(field->>'Name', '')::varchar(50) AS name, + COALESCE(field->>'Name', '')::varchar(1024) AS name, CAST(field->>'Entity' AS entitytype) AS Entity, COALESCE(field->>'Type', '')::fieldtype AS type, CURRENT_TIMESTAMP AS createdAt @@ -871,7 +871,7 @@ WITH new_steps AS ( SELECT (step->>'AmoID')::INT AS amoID, (step->>'PipelineID')::INT AS pipelineID, (step->>'AccountID')::INT AS accountID, - COALESCE(step->>'Name', '')::varchar(50) AS name, + COALESCE(step->>'Name', '')::varchar(1024) AS name, COALESCE(step->>'Color', '')::varchar(50) AS color, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS step @@ -923,7 +923,7 @@ WITH user_data AS ( ), new_UTMs AS ( SELECT (utm->>'AmoFieldID')::INT AS amoFieldID, COALESCE(utm->>'QuizID', '')::INT AS quizID, - COALESCE(utm->>'Name', '')::varchar(50) AS name, + COALESCE(utm->>'Name', '')::varchar(1024) AS name, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($2::json) AS utm ), inserted_utms AS( @@ -965,7 +965,7 @@ WHERE AccountID = $1; -- name: UpdateUtms :exec UPDATE utms AS u -SET name = (update_data ->> 'Name')::varchar(50), +SET name = (update_data ->> 'Name')::varchar(1024), AmoFieldID = (update_data ->> 'AmoFieldID')::INT FROM json_array_elements($1::json) AS update_data WHERE u.ID = (update_data ->> 'ID')::INT; diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 490a072..b86c131 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS users ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AccountID VARCHAR(30) NOT NULL DEFAULT '', -- id квизе из токена AmoID INT NOT NULL , -- id в амо - Name VARCHAR(50) NOT NULL DEFAULT '', -- имя в амо + Name VARCHAR(1024) NOT NULL DEFAULT '', -- имя в амо Email VARCHAR(50) NOT NULL DEFAULT '', -- почта в амо Role INT NOT NULL DEFAULT 0, -- роль в амо "Group" INT NOT NULL DEFAULT 0, -- вложенная структура так как в амо группы хранятся массивом структур @@ -26,7 +26,7 @@ CREATE TABLE IF NOT EXISTS pipelines ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL , --id воронки в амо AccountID INT NOT NULL , --id аккаунта в амо связь с таблицей users AmoID неявная посредством join - Name VARCHAR(50) NOT NULL DEFAULT '', --название воронки в амо + Name VARCHAR(1024) NOT NULL DEFAULT '', --название воронки в амо IsArchive BOOLEAN NOT NULL DEFAULT FALSE, --флаг архивной воронки в амо Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP @@ -37,7 +37,7 @@ CREATE TABLE IF NOT EXISTS steps ( AmoID INT NOT NULL, --id шага воронки в амо PipelineID INT NOT NULL, --id воронки AmoID pipelines неявная посредством join AccountID INT NOT NULL, --id аккаунта в амо связь с таблицей users AmoID неявная посредством join - Name VARCHAR(50) NOT NULL DEFAULT '', --название воронки в амо + Name VARCHAR(1024) NOT NULL DEFAULT '', --название воронки в амо Color VARCHAR(50) NOT NULL DEFAULT '', --цвет шага в амо* Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP @@ -61,7 +61,7 @@ CREATE TABLE IF NOT EXISTS fields ( AmoID INT NOT NULL, -- айдишник кастомного поля в амо Code VARCHAR(255) NOT NULL DEFAULT '', -- кодовое слово в амо AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID неявная посредством join - Name VARCHAR(50) NOT NULL DEFAULT '', -- название воронки в амо + Name VARCHAR(1024) NOT NULL DEFAULT '', -- название воронки в амо Entity EntityType NOT NULL, -- тип сущности в амо, для которой это кастомное поле Type FieldType NOT NULL, -- тип поля Deleted BOOLEAN NOT NULL DEFAULT FALSE, @@ -73,7 +73,7 @@ CREATE TABLE IF NOT EXISTS tags ( AmoID INT NOT NULL, -- айдишник тега в амо AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID неявная посредством join Entity EntityType NOT NULL, -- сущность, к которой принадлежит этот тег - Name VARCHAR(50) NOT NULL DEFAULT '', -- название тега в амо + Name VARCHAR(1024) NOT NULL DEFAULT '', -- название тега в амо Color VARCHAR(50) NOT NULL DEFAULT '', -- цвет тега в амо Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP @@ -84,7 +84,7 @@ CREATE TABLE IF NOT EXISTS utms ( AmoFieldID INT NOT NULL DEFAULT 0, -- id field в амо QuizID INT NOT NULL, -- id опроса AccountID INT NOT NULL, -- id аккаунта в амо AMOID - Name VARCHAR(50) NOT NULL DEFAULT '', -- название utm + Name VARCHAR(1024) NOT NULL DEFAULT '', -- название utm Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); From 3f70f243f7d47afdcb6e37e722702558b8a67d8a Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 2 May 2024 15:34:03 +0300 Subject: [PATCH 133/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 5f68d3b..7379195 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -203,7 +203,7 @@ WITH user_data AS ( ), new_fields AS ( SELECT (field->>'AmoID')::INT AS amoID, COALESCE(field->>'Code', '')::varchar(255) AS code, - COALESCE(field->>'Name', '')::varchar(50) AS name, + COALESCE(field->>'Name', '')::varchar(1024) AS name, CAST(field->>'Entity' AS entitytype) AS Entity, COALESCE(field->>'Type', '')::fieldtype AS type, CURRENT_TIMESTAMP AS createdAt @@ -306,7 +306,7 @@ const checkPipelines = `-- name: CheckPipelines :many WITH new_pipelines AS ( SELECT (pipeline->>'AmoID')::INT AS amoID, (pipeline->>'AccountID')::INT AS accountID, - COALESCE(pipeline->>'Name', '')::varchar(50) AS name, + COALESCE(pipeline->>'Name', '')::varchar(1024) AS name, CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END AS isArchive, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS pipeline @@ -418,7 +418,7 @@ WITH new_steps AS ( SELECT (step->>'AmoID')::INT AS amoID, (step->>'PipelineID')::INT AS pipelineID, (step->>'AccountID')::INT AS accountID, - COALESCE(step->>'Name', '')::varchar(50) AS name, + COALESCE(step->>'Name', '')::varchar(1024) AS name, COALESCE(step->>'Color', '')::varchar(50) AS color, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS step @@ -490,7 +490,7 @@ WITH user_data AS ( ), new_tags AS ( SELECT (tag->>'AmoID')::INT AS amoID, (tag->>'Entity')::entitytype AS Entity, - COALESCE(tag->>'Name', '')::VARCHAR(50) AS name, + COALESCE(tag->>'Name', '')::VARCHAR(1024) AS name, COALESCE(tag->>'Color', '')::VARCHAR(50) AS color FROM json_array_elements($2::json) AS tag ), inserted_tags AS ( @@ -2935,7 +2935,7 @@ WITH user_data AS ( ), new_UTMs AS ( SELECT (utm->>'AmoFieldID')::INT AS amoFieldID, COALESCE(utm->>'QuizID', '')::INT AS quizID, - COALESCE(utm->>'Name', '')::varchar(50) AS name, + COALESCE(utm->>'Name', '')::varchar(1024) AS name, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($2::json) AS utm ), inserted_utms AS( @@ -3063,7 +3063,7 @@ func (q *Queries) UpdateFieldRules(ctx context.Context, arg UpdateFieldRulesPara const updateFields = `-- name: UpdateFields :exec UPDATE fields AS f -SET name = (update_data ->> 'Name')::varchar(50), +SET name = (update_data ->> 'Name')::varchar(1024), code = (update_data ->> 'Code')::varchar(255), type = (update_data ->> 'Type')::fieldtype, createdAt = CURRENT_TIMESTAMP @@ -3080,7 +3080,7 @@ func (q *Queries) UpdateFields(ctx context.Context, dollar_1 json.RawMessage) er const updatePipelines = `-- name: UpdatePipelines :exec UPDATE pipelines AS p -SET name = (update_data ->> 'Name')::varchar(50), +SET name = (update_data ->> 'Name')::varchar(1024), isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END, createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -3130,7 +3130,7 @@ func (q *Queries) UpdatePrivilegeAmount(ctx context.Context, arg UpdatePrivilege const updateSteps = `-- name: UpdateSteps :exec UPDATE steps AS s -SET name = (update_data ->> 'Name')::varchar(50), +SET name = (update_data ->> 'Name')::varchar(1024), color = (update_data ->> 'Color')::varchar(50), createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -3146,7 +3146,7 @@ func (q *Queries) UpdateSteps(ctx context.Context, dollar_1 json.RawMessage) err const updateTags = `-- name: UpdateTags :exec UPDATE tags AS t -SET name = (update_data ->> 'Name')::varchar(50), +SET name = (update_data ->> 'Name')::varchar(1024), color = (update_data ->> 'Color')::varchar(50), createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -3187,7 +3187,7 @@ func (q *Queries) UpdateUsers(ctx context.Context, arg UpdateUsersParams) error const updateUtms = `-- name: UpdateUtms :exec UPDATE utms AS u -SET name = (update_data ->> 'Name')::varchar(50), +SET name = (update_data ->> 'Name')::varchar(1024), AmoFieldID = (update_data ->> 'AmoFieldID')::INT FROM json_array_elements($1::json) AS update_data WHERE u.ID = (update_data ->> 'ID')::INT From c5932c38597b20498a97ff25963b511d085519ed Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 3 May 2024 10:07:16 +0300 Subject: [PATCH 134/187] update name column size = title question size --- dal/db_query/queries.sql | 21 ++++++++++----------- dal/schema/000010_init.up.sql | 12 ++++++------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index f09ed79..ac1e937 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -684,7 +684,6 @@ tokend AS ( ) SELECT * FROM tokend; - -- name: SoftDeleteAccount :exec UPDATE users SET Deleted = TRUE WHERE AccountID = $1; @@ -730,7 +729,7 @@ ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: UpdateTags :exec UPDATE tags AS t -SET name = (update_data ->> 'Name')::varchar(1024), +SET name = (update_data ->> 'Name')::varchar(512), color = (update_data ->> 'Color')::varchar(50), createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -740,7 +739,7 @@ WHERE t.amoID = (update_data ->> 'AmoID')::INT -- name: UpdatePipelines :exec UPDATE pipelines AS p -SET name = (update_data ->> 'Name')::varchar(1024), +SET name = (update_data ->> 'Name')::varchar(512), isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END, createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -749,7 +748,7 @@ WHERE p.amoID = (update_data ->> 'AmoID')::INT -- name: UpdateSteps :exec UPDATE steps AS s -SET name = (update_data ->> 'Name')::varchar(1024), +SET name = (update_data ->> 'Name')::varchar(512), color = (update_data ->> 'Color')::varchar(50), createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -759,7 +758,7 @@ WHERE s.amoID = (update_data ->> 'AmoID')::INT -- name: UpdateFields :exec UPDATE fields AS f -SET name = (update_data ->> 'Name')::varchar(1024), +SET name = (update_data ->> 'Name')::varchar(512), code = (update_data ->> 'Code')::varchar(255), type = (update_data ->> 'Type')::fieldtype, createdAt = CURRENT_TIMESTAMP @@ -776,7 +775,7 @@ WITH user_data AS ( ), new_tags AS ( SELECT (tag->>'AmoID')::INT AS amoID, (tag->>'Entity')::entitytype AS Entity, - COALESCE(tag->>'Name', '')::VARCHAR(1024) AS name, + COALESCE(tag->>'Name', '')::VARCHAR(512) AS name, COALESCE(tag->>'Color', '')::VARCHAR(50) AS color FROM json_array_elements($2::json) AS tag ), inserted_tags AS ( @@ -806,7 +805,7 @@ WHERE NOT EXISTS ( WITH new_pipelines AS ( SELECT (pipeline->>'AmoID')::INT AS amoID, (pipeline->>'AccountID')::INT AS accountID, - COALESCE(pipeline->>'Name', '')::varchar(1024) AS name, + COALESCE(pipeline->>'Name', '')::varchar(512) AS name, CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END AS isArchive, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS pipeline @@ -837,7 +836,7 @@ WITH user_data AS ( ), new_fields AS ( SELECT (field->>'AmoID')::INT AS amoID, COALESCE(field->>'Code', '')::varchar(255) AS code, - COALESCE(field->>'Name', '')::varchar(1024) AS name, + COALESCE(field->>'Name', '')::varchar(512) AS name, CAST(field->>'Entity' AS entitytype) AS Entity, COALESCE(field->>'Type', '')::fieldtype AS type, CURRENT_TIMESTAMP AS createdAt @@ -871,7 +870,7 @@ WITH new_steps AS ( SELECT (step->>'AmoID')::INT AS amoID, (step->>'PipelineID')::INT AS pipelineID, (step->>'AccountID')::INT AS accountID, - COALESCE(step->>'Name', '')::varchar(1024) AS name, + COALESCE(step->>'Name', '')::varchar(512) AS name, COALESCE(step->>'Color', '')::varchar(50) AS color, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS step @@ -923,7 +922,7 @@ WITH user_data AS ( ), new_UTMs AS ( SELECT (utm->>'AmoFieldID')::INT AS amoFieldID, COALESCE(utm->>'QuizID', '')::INT AS quizID, - COALESCE(utm->>'Name', '')::varchar(1024) AS name, + COALESCE(utm->>'Name', '')::varchar(512) AS name, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($2::json) AS utm ), inserted_utms AS( @@ -965,7 +964,7 @@ WHERE AccountID = $1; -- name: UpdateUtms :exec UPDATE utms AS u -SET name = (update_data ->> 'Name')::varchar(1024), +SET name = (update_data ->> 'Name')::varchar(512), AmoFieldID = (update_data ->> 'AmoFieldID')::INT FROM json_array_elements($1::json) AS update_data WHERE u.ID = (update_data ->> 'ID')::INT; diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index b86c131..3a4db3c 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS users ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AccountID VARCHAR(30) NOT NULL DEFAULT '', -- id квизе из токена AmoID INT NOT NULL , -- id в амо - Name VARCHAR(1024) NOT NULL DEFAULT '', -- имя в амо + Name VARCHAR(512) NOT NULL DEFAULT '', -- имя в амо Email VARCHAR(50) NOT NULL DEFAULT '', -- почта в амо Role INT NOT NULL DEFAULT 0, -- роль в амо "Group" INT NOT NULL DEFAULT 0, -- вложенная структура так как в амо группы хранятся массивом структур @@ -26,7 +26,7 @@ CREATE TABLE IF NOT EXISTS pipelines ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AmoID INT NOT NULL , --id воронки в амо AccountID INT NOT NULL , --id аккаунта в амо связь с таблицей users AmoID неявная посредством join - Name VARCHAR(1024) NOT NULL DEFAULT '', --название воронки в амо + Name VARCHAR(512) NOT NULL DEFAULT '', --название воронки в амо IsArchive BOOLEAN NOT NULL DEFAULT FALSE, --флаг архивной воронки в амо Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP @@ -37,7 +37,7 @@ CREATE TABLE IF NOT EXISTS steps ( AmoID INT NOT NULL, --id шага воронки в амо PipelineID INT NOT NULL, --id воронки AmoID pipelines неявная посредством join AccountID INT NOT NULL, --id аккаунта в амо связь с таблицей users AmoID неявная посредством join - Name VARCHAR(1024) NOT NULL DEFAULT '', --название воронки в амо + Name VARCHAR(512) NOT NULL DEFAULT '', --название воронки в амо Color VARCHAR(50) NOT NULL DEFAULT '', --цвет шага в амо* Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP @@ -61,7 +61,7 @@ CREATE TABLE IF NOT EXISTS fields ( AmoID INT NOT NULL, -- айдишник кастомного поля в амо Code VARCHAR(255) NOT NULL DEFAULT '', -- кодовое слово в амо AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID неявная посредством join - Name VARCHAR(1024) NOT NULL DEFAULT '', -- название воронки в амо + Name VARCHAR(512) NOT NULL DEFAULT '', -- название воронки в амо Entity EntityType NOT NULL, -- тип сущности в амо, для которой это кастомное поле Type FieldType NOT NULL, -- тип поля Deleted BOOLEAN NOT NULL DEFAULT FALSE, @@ -73,7 +73,7 @@ CREATE TABLE IF NOT EXISTS tags ( AmoID INT NOT NULL, -- айдишник тега в амо AccountID INT NOT NULL, -- id аккаунта в амо связь с таблицей users AmoID неявная посредством join Entity EntityType NOT NULL, -- сущность, к которой принадлежит этот тег - Name VARCHAR(1024) NOT NULL DEFAULT '', -- название тега в амо + Name VARCHAR(512) NOT NULL DEFAULT '', -- название тега в амо Color VARCHAR(50) NOT NULL DEFAULT '', -- цвет тега в амо Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP @@ -84,7 +84,7 @@ CREATE TABLE IF NOT EXISTS utms ( AmoFieldID INT NOT NULL DEFAULT 0, -- id field в амо QuizID INT NOT NULL, -- id опроса AccountID INT NOT NULL, -- id аккаунта в амо AMOID - Name VARCHAR(1024) NOT NULL DEFAULT '', -- название utm + Name VARCHAR(512) NOT NULL DEFAULT '', -- название utm Deleted BOOLEAN NOT NULL DEFAULT FALSE, CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); From 2ce1a36bbbd5f8142f1f872cfbdc3d262fd48905 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 3 May 2024 10:09:06 +0300 Subject: [PATCH 135/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 7379195..f98b31d 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -203,7 +203,7 @@ WITH user_data AS ( ), new_fields AS ( SELECT (field->>'AmoID')::INT AS amoID, COALESCE(field->>'Code', '')::varchar(255) AS code, - COALESCE(field->>'Name', '')::varchar(1024) AS name, + COALESCE(field->>'Name', '')::varchar(512) AS name, CAST(field->>'Entity' AS entitytype) AS Entity, COALESCE(field->>'Type', '')::fieldtype AS type, CURRENT_TIMESTAMP AS createdAt @@ -306,7 +306,7 @@ const checkPipelines = `-- name: CheckPipelines :many WITH new_pipelines AS ( SELECT (pipeline->>'AmoID')::INT AS amoID, (pipeline->>'AccountID')::INT AS accountID, - COALESCE(pipeline->>'Name', '')::varchar(1024) AS name, + COALESCE(pipeline->>'Name', '')::varchar(512) AS name, CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END AS isArchive, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS pipeline @@ -418,7 +418,7 @@ WITH new_steps AS ( SELECT (step->>'AmoID')::INT AS amoID, (step->>'PipelineID')::INT AS pipelineID, (step->>'AccountID')::INT AS accountID, - COALESCE(step->>'Name', '')::varchar(1024) AS name, + COALESCE(step->>'Name', '')::varchar(512) AS name, COALESCE(step->>'Color', '')::varchar(50) AS color, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($1::json) AS step @@ -490,7 +490,7 @@ WITH user_data AS ( ), new_tags AS ( SELECT (tag->>'AmoID')::INT AS amoID, (tag->>'Entity')::entitytype AS Entity, - COALESCE(tag->>'Name', '')::VARCHAR(1024) AS name, + COALESCE(tag->>'Name', '')::VARCHAR(512) AS name, COALESCE(tag->>'Color', '')::VARCHAR(50) AS color FROM json_array_elements($2::json) AS tag ), inserted_tags AS ( @@ -2935,7 +2935,7 @@ WITH user_data AS ( ), new_UTMs AS ( SELECT (utm->>'AmoFieldID')::INT AS amoFieldID, COALESCE(utm->>'QuizID', '')::INT AS quizID, - COALESCE(utm->>'Name', '')::varchar(1024) AS name, + COALESCE(utm->>'Name', '')::varchar(512) AS name, CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($2::json) AS utm ), inserted_utms AS( @@ -3063,7 +3063,7 @@ func (q *Queries) UpdateFieldRules(ctx context.Context, arg UpdateFieldRulesPara const updateFields = `-- name: UpdateFields :exec UPDATE fields AS f -SET name = (update_data ->> 'Name')::varchar(1024), +SET name = (update_data ->> 'Name')::varchar(512), code = (update_data ->> 'Code')::varchar(255), type = (update_data ->> 'Type')::fieldtype, createdAt = CURRENT_TIMESTAMP @@ -3080,7 +3080,7 @@ func (q *Queries) UpdateFields(ctx context.Context, dollar_1 json.RawMessage) er const updatePipelines = `-- name: UpdatePipelines :exec UPDATE pipelines AS p -SET name = (update_data ->> 'Name')::varchar(1024), +SET name = (update_data ->> 'Name')::varchar(512), isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END, createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -3130,7 +3130,7 @@ func (q *Queries) UpdatePrivilegeAmount(ctx context.Context, arg UpdatePrivilege const updateSteps = `-- name: UpdateSteps :exec UPDATE steps AS s -SET name = (update_data ->> 'Name')::varchar(1024), +SET name = (update_data ->> 'Name')::varchar(512), color = (update_data ->> 'Color')::varchar(50), createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -3146,7 +3146,7 @@ func (q *Queries) UpdateSteps(ctx context.Context, dollar_1 json.RawMessage) err const updateTags = `-- name: UpdateTags :exec UPDATE tags AS t -SET name = (update_data ->> 'Name')::varchar(1024), +SET name = (update_data ->> 'Name')::varchar(512), color = (update_data ->> 'Color')::varchar(50), createdAt = CURRENT_TIMESTAMP FROM json_array_elements($1::json) AS update_data @@ -3187,7 +3187,7 @@ func (q *Queries) UpdateUsers(ctx context.Context, arg UpdateUsersParams) error const updateUtms = `-- name: UpdateUtms :exec UPDATE utms AS u -SET name = (update_data ->> 'Name')::varchar(1024), +SET name = (update_data ->> 'Name')::varchar(512), AmoFieldID = (update_data ->> 'AmoFieldID')::INT FROM json_array_elements($1::json) AS update_data WHERE u.ID = (update_data ->> 'ID')::INT From f6c3470bd87297dad3986885e38ad5c53b41d187 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 3 May 2024 11:20:31 +0300 Subject: [PATCH 136/187] update sqlc gen --- dal/db_query/queries.sql | 13 ++++++++----- dal/sqlcgen/queries.sql.go | 13 ++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index ac1e937..7c543d9 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -678,14 +678,17 @@ SELECT * FROM tokens WHERE Expiration <= TO_TIMESTAMP(EXTRACT(EPOCH FROM NOW()) -- name: WebhookDelete :exec WITH userd AS ( UPDATE users SET Deleted = true WHERE AmoUserID = $1 RETURNING AccountID -), -tokend AS ( - DELETE FROM tokens WHERE AccountID IN (SELECT AccountID FROM userd) RETURNING * ) -SELECT * FROM tokend; +DELETE FROM tokens WHERE AccountID IN (SELECT AccountID FROM userd); -- name: SoftDeleteAccount :exec -UPDATE users SET Deleted = TRUE WHERE AccountID = $1; +WITH userd AS ( + SELECT AmoUserID FROM users WHERE users.AccountID = $1 +), + tokend AS ( + UPDATE users SET Deleted = true WHERE AmoUserID IN (SELECT AmoUserID FROM userd) RETURNING users.AccountID + ) +DELETE FROM tokens WHERE tokens.AccountID IN (SELECT AccountID FROM tokend); -- name: GetCurrentAccount :one SELECT * FROM users WHERE AccountID = $1; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index f98b31d..1477735 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3028,7 +3028,13 @@ func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams } const softDeleteAccount = `-- name: SoftDeleteAccount :exec -UPDATE users SET Deleted = TRUE WHERE AccountID = $1 +WITH userd AS ( + SELECT AmoUserID FROM users WHERE users.AccountID = $1 +), + tokend AS ( + UPDATE users SET Deleted = true WHERE AmoUserID IN (SELECT AmoUserID FROM userd) RETURNING users.AccountID + ) +DELETE FROM tokens WHERE tokens.AccountID IN (SELECT AccountID FROM tokend) ` func (q *Queries) SoftDeleteAccount(ctx context.Context, accountid string) error { @@ -3211,11 +3217,8 @@ func (q *Queries) UpdateUtmsFields(ctx context.Context, dollar_1 []int32) error const webhookDelete = `-- name: WebhookDelete :exec WITH userd AS ( UPDATE users SET Deleted = true WHERE AmoUserID = $1 RETURNING AccountID -), -tokend AS ( - DELETE FROM tokens WHERE AccountID IN (SELECT AccountID FROM userd) RETURNING accountid, refreshtoken, accesstoken, authcode, expiration, createdat ) -SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokend +DELETE FROM tokens WHERE AccountID IN (SELECT AccountID FROM userd) ` func (q *Queries) WebhookDelete(ctx context.Context, amouserid int32) error { From 450d3330d61a37a53040e393fc3aff089d05505a Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 3 May 2024 17:58:28 +0300 Subject: [PATCH 137/187] update queries --- dal/db_query/queries.sql | 61 +++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 7c543d9..fc2c118 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -733,8 +733,7 @@ ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: UpdateTags :exec UPDATE tags AS t SET name = (update_data ->> 'Name')::varchar(512), - color = (update_data ->> 'Color')::varchar(50), - createdAt = CURRENT_TIMESTAMP + color = (update_data ->> 'Color')::varchar(50) FROM json_array_elements($1::json) AS update_data WHERE t.amoID = (update_data ->> 'AmoID')::INT AND t.accountID = (update_data ->> 'AccountID')::INT @@ -743,8 +742,7 @@ WHERE t.amoID = (update_data ->> 'AmoID')::INT -- name: UpdatePipelines :exec UPDATE pipelines AS p SET name = (update_data ->> 'Name')::varchar(512), - isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END, - createdAt = CURRENT_TIMESTAMP + isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END FROM json_array_elements($1::json) AS update_data WHERE p.amoID = (update_data ->> 'AmoID')::INT AND p.accountID = (update_data ->> 'AccountID')::INT; @@ -752,8 +750,7 @@ WHERE p.amoID = (update_data ->> 'AmoID')::INT -- name: UpdateSteps :exec UPDATE steps AS s SET name = (update_data ->> 'Name')::varchar(512), - color = (update_data ->> 'Color')::varchar(50), - createdAt = CURRENT_TIMESTAMP + color = (update_data ->> 'Color')::varchar(50) FROM json_array_elements($1::json) AS update_data WHERE s.amoID = (update_data ->> 'AmoID')::INT AND s.accountID = (update_data ->> 'AccountID')::INT @@ -763,8 +760,7 @@ WHERE s.amoID = (update_data ->> 'AmoID')::INT UPDATE fields AS f SET name = (update_data ->> 'Name')::varchar(512), code = (update_data ->> 'Code')::varchar(255), - type = (update_data ->> 'Type')::fieldtype, - createdAt = CURRENT_TIMESTAMP + type = (update_data ->> 'Type')::fieldtype FROM json_array_elements($1::json) AS update_data WHERE f.amoID = (update_data ->> 'AmoID')::INT AND f.accountID = (update_data ->> 'AccountID')::INT @@ -897,14 +893,6 @@ WHERE NOT EXISTS ( WHERE ins.amoID = ns.amoID AND ins.accountID = ns.accountID AND ins.pipelineID = ns.pipelineID ); --- name: CheckUsers :exec -INSERT INTO users (AmoID, Name, Email, Role, "Group", AmoUserID) -VALUES ($1, $2, $3, $4, $5, $6) -ON CONFLICT (AmoID) DO NOTHING; - --- name: UpdateUsers :exec -UPDATE users SET Name = $2, Email = $3, Role = $4, "Group" = $5, AmoUserID = $6 WHERE AmoID = $1; - -- name: GetTokenById :one SELECT * FROM tokens WHERE accountID = $1; @@ -982,3 +970,44 @@ SELECT * FROM question WHERE id = ANY($1::int[]) AND deleted = FALSE; -- name: UpdateFieldRules :exec UPDATE rules SET FieldsRule = $1 WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $2) AND QuizID = $3 AND Deleted = false; + +-- name: UpdateUsers :exec +UPDATE users AS u +SET Name = (update_data ->> 'Name')::varchar(512), + Email = (update_data ->> 'Email')::varchar(50), + Role = (update_data ->> 'Role')::INT, + "Group" = (update_data ->> 'Group')::INT, + AmoUserID= (update_data ->> 'AmoUserID')::INT +FROM json_array_elements($1::json) AS update_data +WHERE u.AmoID = (update_data ->> 'AmocrmID')::INT; + +-- name: CheckUsers :many +WITH new_users AS ( + SELECT (u->>'AmocrmID')::INT AS AmoID, + (u->>'Name')::VARCHAR(512) AS Name, + (u->>'Group')::INT AS "Group", + (u->>'Role')::INT AS Role, + (u->>'Email')::VARCHAR(50) AS Email, + (u->>'AmoUserID')::INT AS AmoUserID, + CURRENT_TIMESTAMP AS createdAt + FROM json_array_elements($1::json) AS u +), inserted_users AS ( + INSERT INTO users (AmoID, Name, "Group", Role, Email, AmoUserID,createdAt) + SELECT nu.AmoID, + nu.Name, + nu."Group", + nu.Role, + nu.Email, + nu.AmoUserID, + nu.createdAt + FROM new_users nu + ON CONFLICT (amoID) DO NOTHING + RETURNING * +) +SELECT nu.* +FROM new_users nu +WHERE NOT EXISTS ( + SELECT * + FROM inserted_users ins + WHERE ins.amoID = nu.amoID +); \ No newline at end of file From fe2fac1eac5cf54c7bf2aab39727ebf51c9ea28e Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 3 May 2024 18:00:30 +0300 Subject: [PATCH 138/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 129 +++++++++++++++++++++++-------------- 1 file changed, 81 insertions(+), 48 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 1477735..6068530 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -559,31 +559,77 @@ func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]CheckTa return items, nil } -const checkUsers = `-- name: CheckUsers :exec -INSERT INTO users (AmoID, Name, Email, Role, "Group", AmoUserID) -VALUES ($1, $2, $3, $4, $5, $6) -ON CONFLICT (AmoID) DO NOTHING +const checkUsers = `-- name: CheckUsers :many +WITH new_users AS ( + SELECT (u->>'AmocrmID')::INT AS AmoID, + (u->>'Name')::VARCHAR(512) AS Name, + (u->>'Group')::INT AS "Group", + (u->>'Role')::INT AS Role, + (u->>'Email')::VARCHAR(50) AS Email, + (u->>'AmoUserID')::INT AS AmoUserID, + CURRENT_TIMESTAMP AS createdAt + FROM json_array_elements($1::json) AS u +), inserted_users AS ( + INSERT INTO users (AmoID, Name, "Group", Role, Email, AmoUserID,createdAt) + SELECT nu.AmoID, + nu.Name, + nu."Group", + nu.Role, + nu.Email, + nu.AmoUserID, + nu.createdAt + FROM new_users nu + ON CONFLICT (amoID) DO NOTHING + RETURNING id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country +) +SELECT nu.amoid, nu.name, nu."Group", nu.role, nu.email, nu.amouserid, nu.createdat +FROM new_users nu +WHERE NOT EXISTS ( + SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country + FROM inserted_users ins + WHERE ins.amoID = nu.amoID +) ` -type CheckUsersParams struct { - Amoid int32 `db:"amoid" json:"amoid"` - Name string `db:"name" json:"name"` - Email string `db:"email" json:"email"` - Role int32 `db:"role" json:"role"` - Group int32 `db:"Group" json:"Group"` - Amouserid int32 `db:"amouserid" json:"amouserid"` +type CheckUsersRow struct { + Amoid int32 `db:"amoid" json:"amoid"` + Name string `db:"name" json:"name"` + Group int32 `db:"Group" json:"Group"` + Role int32 `db:"role" json:"role"` + Email string `db:"email" json:"email"` + Amouserid int32 `db:"amouserid" json:"amouserid"` + Createdat interface{} `db:"createdat" json:"createdat"` } -func (q *Queries) CheckUsers(ctx context.Context, arg CheckUsersParams) error { - _, err := q.db.ExecContext(ctx, checkUsers, - arg.Amoid, - arg.Name, - arg.Email, - arg.Role, - arg.Group, - arg.Amouserid, - ) - return err +func (q *Queries) CheckUsers(ctx context.Context, dollar_1 json.RawMessage) ([]CheckUsersRow, error) { + rows, err := q.db.QueryContext(ctx, checkUsers, dollar_1) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CheckUsersRow + for rows.Next() { + var i CheckUsersRow + if err := rows.Scan( + &i.Amoid, + &i.Name, + &i.Group, + &i.Role, + &i.Email, + &i.Amouserid, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil } const copyQuestion = `-- name: CopyQuestion :one @@ -3071,8 +3117,7 @@ const updateFields = `-- name: UpdateFields :exec UPDATE fields AS f SET name = (update_data ->> 'Name')::varchar(512), code = (update_data ->> 'Code')::varchar(255), - type = (update_data ->> 'Type')::fieldtype, - createdAt = CURRENT_TIMESTAMP + type = (update_data ->> 'Type')::fieldtype FROM json_array_elements($1::json) AS update_data WHERE f.amoID = (update_data ->> 'AmoID')::INT AND f.accountID = (update_data ->> 'AccountID')::INT @@ -3087,8 +3132,7 @@ func (q *Queries) UpdateFields(ctx context.Context, dollar_1 json.RawMessage) er const updatePipelines = `-- name: UpdatePipelines :exec UPDATE pipelines AS p SET name = (update_data ->> 'Name')::varchar(512), - isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END, - createdAt = CURRENT_TIMESTAMP + isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END FROM json_array_elements($1::json) AS update_data WHERE p.amoID = (update_data ->> 'AmoID')::INT AND p.accountID = (update_data ->> 'AccountID')::INT @@ -3137,8 +3181,7 @@ func (q *Queries) UpdatePrivilegeAmount(ctx context.Context, arg UpdatePrivilege const updateSteps = `-- name: UpdateSteps :exec UPDATE steps AS s SET name = (update_data ->> 'Name')::varchar(512), - color = (update_data ->> 'Color')::varchar(50), - createdAt = CURRENT_TIMESTAMP + color = (update_data ->> 'Color')::varchar(50) FROM json_array_elements($1::json) AS update_data WHERE s.amoID = (update_data ->> 'AmoID')::INT AND s.accountID = (update_data ->> 'AccountID')::INT @@ -3153,8 +3196,7 @@ func (q *Queries) UpdateSteps(ctx context.Context, dollar_1 json.RawMessage) err const updateTags = `-- name: UpdateTags :exec UPDATE tags AS t SET name = (update_data ->> 'Name')::varchar(512), - color = (update_data ->> 'Color')::varchar(50), - createdAt = CURRENT_TIMESTAMP + color = (update_data ->> 'Color')::varchar(50) FROM json_array_elements($1::json) AS update_data WHERE t.amoID = (update_data ->> 'AmoID')::INT AND t.accountID = (update_data ->> 'AccountID')::INT @@ -3167,27 +3209,18 @@ func (q *Queries) UpdateTags(ctx context.Context, dollar_1 json.RawMessage) erro } const updateUsers = `-- name: UpdateUsers :exec -UPDATE users SET Name = $2, Email = $3, Role = $4, "Group" = $5, AmoUserID = $6 WHERE AmoID = $1 +UPDATE users AS u +SET Name = (update_data ->> 'Name')::varchar(512), + Email = (update_data ->> 'Email')::varchar(50), + Role = (update_data ->> 'Role')::INT, + "Group" = (update_data ->> 'Group')::INT, + AmoUserID= (update_data ->> 'AmoUserID')::INT +FROM json_array_elements($1::json) AS update_data +WHERE u.AmoID = (update_data ->> 'AmocrmID')::INT ` -type UpdateUsersParams struct { - Amoid int32 `db:"amoid" json:"amoid"` - Name string `db:"name" json:"name"` - Email string `db:"email" json:"email"` - Role int32 `db:"role" json:"role"` - Group int32 `db:"Group" json:"Group"` - Amouserid int32 `db:"amouserid" json:"amouserid"` -} - -func (q *Queries) UpdateUsers(ctx context.Context, arg UpdateUsersParams) error { - _, err := q.db.ExecContext(ctx, updateUsers, - arg.Amoid, - arg.Name, - arg.Email, - arg.Role, - arg.Group, - arg.Amouserid, - ) +func (q *Queries) UpdateUsers(ctx context.Context, dollar_1 json.RawMessage) error { + _, err := q.db.ExecContext(ctx, updateUsers, dollar_1) return err } From e610703cbfc76480cc538898a0ed76f5ab28d627 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 3 May 2024 18:11:57 +0300 Subject: [PATCH 139/187] update amo repo users check method --- repository/amo/amo.go | 52 +++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 204361e..8ae7f7c 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -6,7 +6,6 @@ import ( "encoding/json" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" - "strings" "time" ) @@ -136,32 +135,41 @@ func (r *AmoRepository) CheckMainUser(ctx context.Context, user model.User) erro 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, - }) +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 + } - // чекаем на конфликт - 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 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) if err != nil { return err } - return nil } - return err + + return nil } func (r *AmoRepository) GetTokenByID(ctx context.Context, accountID string) (*model.Token, error) { From 7c100e16becb19512f1057a8d036f68f2712bae6 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 11:46:00 +0300 Subject: [PATCH 140/187] commented some types in TypeMapping --- model/amo.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model/amo.go b/model/amo.go index 8a22d19..141cacb 100644 --- a/model/amo.go +++ b/model/amo.go @@ -210,13 +210,13 @@ const ( ) var TypeMapping = map[string]FieldType{ - "variant": TypeAmoChainedList, + "variant": TypeAmoText, //TypeAmoChainedList "images": TypeAmoFile, "varimg": TypeAmoFile, "file": TypeAmoFile, "text": TypeAmoText, "emoji": TypeAmoText, - "select": TypeAmoSelect, + "select": TypeAmoText, // TypeAmoSelect "date": TypeAmoDate, "number": TypeAmoNumeric, "page": TypeAmoText, From 23fa6ca17d94e0510f5b471fe3294397b15dca36 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 12:54:23 +0300 Subject: [PATCH 141/187] add new table for reserching amo fields post statuses --- dal/schema/000010_init.down.sql | 1 + dal/schema/000010_init.up.sql | 10 ++++++++++ model/amo.go | 4 ++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/dal/schema/000010_init.down.sql b/dal/schema/000010_init.down.sql index dfe3f54..02f12e4 100644 --- a/dal/schema/000010_init.down.sql +++ b/dal/schema/000010_init.down.sql @@ -5,6 +5,7 @@ DROP INDEX IF EXISTS idx_unique_field; DROP INDEX IF EXISTS idx_unique_tag; DROP INDEX IF EXISTS idx_unique_rules; +DROP TABLE IF EXISTS amoCRMStatuses; DROP TABLE IF EXISTS rules; DROP TABLE IF EXISTS utms; DROP TABLE IF EXISTS tags; diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index 3a4db3c..efcb6ea 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -108,3 +108,13 @@ CREATE UNIQUE INDEX idx_unique_step ON steps (amoID, accountID, PipelineID); CREATE UNIQUE INDEX idx_unique_field ON fields (amoID, accountID, entity); CREATE UNIQUE INDEX idx_unique_tag ON tags (amoID, accountID, entity); CREATE UNIQUE INDEX idx_unique_rules ON rules (accountID, QuizID); + +CREATE TABLE IF NOT EXISTS amoCRMStatuses ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AccountID INT NOT NULL, -- id аккаунта в амо + QuestionID BIGINT NOT NULL REFERENCES question(id), + AnswerID BIGINT NOT NULL REFERENCES answer(id), + FieldID INT NOT NULL, -- айдишник кастомного поля в амо + Status TEXT NOT NULL DEFAULT '', -- запись о ошибке, либо успехе + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/model/amo.go b/model/amo.go index 141cacb..8a22d19 100644 --- a/model/amo.go +++ b/model/amo.go @@ -210,13 +210,13 @@ const ( ) var TypeMapping = map[string]FieldType{ - "variant": TypeAmoText, //TypeAmoChainedList + "variant": TypeAmoChainedList, "images": TypeAmoFile, "varimg": TypeAmoFile, "file": TypeAmoFile, "text": TypeAmoText, "emoji": TypeAmoText, - "select": TypeAmoText, // TypeAmoSelect + "select": TypeAmoSelect, "date": TypeAmoDate, "number": TypeAmoNumeric, "page": TypeAmoText, From 0343a1093a6d9267f0beda386b83f9335b0279ef Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 13:31:59 +0300 Subject: [PATCH 142/187] add new query --- dal/db_query/queries.sql | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index fc2c118..729cc5a 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1010,4 +1010,16 @@ WHERE NOT EXISTS ( SELECT * FROM inserted_users ins WHERE ins.amoID = nu.amoID -); \ No newline at end of file +); + +-- name: GettingAmoUsersTrueResults :many +SELECT a.*,t.accesstoken,r.* +FROM answer a + INNER JOIN quiz q ON a.quiz_id = q.id + LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID + INNER JOIN rules r ON q.id = r.QuizID + INNER JOIN tokens t ON q.accountid = t.AccountID + INNER JOIN users u ON q.accountid = u.accountid AND u.amoid = r.accountid +WHERE a.result = true + AND s.id IS NULL + AND r.deleted = false; \ No newline at end of file From c6d6e8c09c552d78198e1e834e1c098c227f5737 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 13:33:50 +0300 Subject: [PATCH 143/187] update sqlc gen --- dal/sqlcgen/models.go | 10 ++++ dal/sqlcgen/queries.sql.go | 96 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index c8d9052..6172c73 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -20,6 +20,16 @@ type Account struct { Deleted sql.NullBool `db:"deleted" json:"deleted"` } +type Amocrmstatus struct { + ID int64 `db:"id" json:"id"` + Accountid int32 `db:"accountid" json:"accountid"` + Questionid int64 `db:"questionid" json:"questionid"` + Answerid int64 `db:"answerid" json:"answerid"` + Fieldid int32 `db:"fieldid" json:"fieldid"` + Status string `db:"status" json:"status"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + type Answer struct { ID int64 `db:"id" json:"id"` Content sql.NullString `db:"content" json:"content"` diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 6068530..defbf86 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2522,6 +2522,102 @@ func (q *Queries) GetUtmsByID(ctx context.Context, dollar_1 []int32) ([]GetUtmsB return items, nil } +const gettingAmoUsersTrueResults = `-- name: GettingAmoUsersTrueResults :many +SELECT a.id, a.content, a.quiz_id, a.question_id, a.fingerprint, a.session, a.created_at, a.result, a.new, a.deleted, a.email, a.device_type, a.device, a.os, a.browser, a.ip, a.start,t.accesstoken,r.id, r.accountid, r.quizid, r.performerid, r.pipelineid, r.stepid, r.utms, r.fieldsrule, r.deleted, r.createdat +FROM answer a + INNER JOIN quiz q ON a.quiz_id = q.id + LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID + INNER JOIN rules r ON q.id = r.QuizID + INNER JOIN tokens t ON q.accountid = t.AccountID + INNER JOIN users u ON q.accountid = u.accountid AND u.amoid = r.accountid +WHERE a.result = true + AND s.id IS NULL + AND r.deleted = false +` + +type GettingAmoUsersTrueResultsRow struct { + ID int64 `db:"id" json:"id"` + Content sql.NullString `db:"content" json:"content"` + QuizID int64 `db:"quiz_id" json:"quiz_id"` + QuestionID int64 `db:"question_id" json:"question_id"` + Fingerprint sql.NullString `db:"fingerprint" json:"fingerprint"` + Session sql.NullString `db:"session" json:"session"` + CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + Result sql.NullBool `db:"result" json:"result"` + New sql.NullBool `db:"new" json:"new"` + Deleted sql.NullBool `db:"deleted" json:"deleted"` + Email string `db:"email" json:"email"` + DeviceType string `db:"device_type" json:"device_type"` + Device string `db:"device" json:"device"` + Os string `db:"os" json:"os"` + Browser string `db:"browser" json:"browser"` + Ip string `db:"ip" json:"ip"` + Start bool `db:"start" json:"start"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` + ID_2 int64 `db:"id_2" json:"id_2"` + Accountid int32 `db:"accountid" json:"accountid"` + Quizid int32 `db:"quizid" json:"quizid"` + Performerid int32 `db:"performerid" json:"performerid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + Stepid int32 `db:"stepid" json:"stepid"` + Utms []int32 `db:"utms" json:"utms"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Deleted_2 bool `db:"deleted_2" json:"deleted_2"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + +func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoUsersTrueResultsRow, error) { + rows, err := q.db.QueryContext(ctx, gettingAmoUsersTrueResults) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GettingAmoUsersTrueResultsRow + for rows.Next() { + var i GettingAmoUsersTrueResultsRow + if err := rows.Scan( + &i.ID, + &i.Content, + &i.QuizID, + &i.QuestionID, + &i.Fingerprint, + &i.Session, + &i.CreatedAt, + &i.Result, + &i.New, + &i.Deleted, + &i.Email, + &i.DeviceType, + &i.Device, + &i.Os, + &i.Browser, + &i.Ip, + &i.Start, + &i.Accesstoken, + &i.ID_2, + &i.Accountid, + &i.Quizid, + &i.Performerid, + &i.Pipelineid, + &i.Stepid, + pq.Array(&i.Utms), + &i.Fieldsrule, + &i.Deleted_2, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const insertAnswers = `-- name: InsertAnswers :exec INSERT INTO answer( content, From c1155b2bb289ef4cd2fabc9cc178fda1301b1137 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 13:37:43 +0300 Subject: [PATCH 144/187] update sqlc gen --- dal/db_query/queries.sql | 3 ++- dal/sqlcgen/queries.sql.go | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 729cc5a..2b754b1 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1022,4 +1022,5 @@ FROM answer a INNER JOIN users u ON q.accountid = u.accountid AND u.amoid = r.accountid WHERE a.result = true AND s.id IS NULL - AND r.deleted = false; \ No newline at end of file + AND r.deleted = false + AND q.deleted = false; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index defbf86..2b56f37 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2533,6 +2533,7 @@ FROM answer a WHERE a.result = true AND s.id IS NULL AND r.deleted = false + AND q.deleted = false ` type GettingAmoUsersTrueResultsRow struct { From 7b3e64c1e7e96669c0eb5831e18a075c8a8a39b5 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 13:49:49 +0300 Subject: [PATCH 145/187] update query --- dal/db_query/queries.sql | 3 ++- repository/amo/amo.go | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 2b754b1..d577b56 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1013,7 +1013,7 @@ WHERE NOT EXISTS ( ); -- name: GettingAmoUsersTrueResults :many -SELECT a.*,t.accesstoken,r.* +SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,t.accesstoken,r.accountid,r.utms,r.fieldsrule,r.performerid,r.stepid,r.pipelineid FROM answer a INNER JOIN quiz q ON a.quiz_id = q.id LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID @@ -1022,5 +1022,6 @@ FROM answer a INNER JOIN users u ON q.accountid = u.accountid AND u.amoid = r.accountid WHERE a.result = true AND s.id IS NULL + AND a.deleted = false AND r.deleted = false AND q.deleted = false; diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 8ae7f7c..bcf6ad7 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -835,3 +835,10 @@ func (r *AmoRepository) UpdateUtmsFields(ctx context.Context, ids []int32) error return nil } + +func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) error { + rows, err := r.queries.GettingAmoUsersTrueResults(ctx) + if err != nil { + return err + } +} From 68df239d7d1c226b4ed161cc7bd39467c8f0011a Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 13:39:08 +0300 Subject: [PATCH 146/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 61 ++++++++++---------------------------- 1 file changed, 16 insertions(+), 45 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 2b56f37..72e5344 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2523,7 +2523,7 @@ func (q *Queries) GetUtmsByID(ctx context.Context, dollar_1 []int32) ([]GetUtmsB } const gettingAmoUsersTrueResults = `-- name: GettingAmoUsersTrueResults :many -SELECT a.id, a.content, a.quiz_id, a.question_id, a.fingerprint, a.session, a.created_at, a.result, a.new, a.deleted, a.email, a.device_type, a.device, a.os, a.browser, a.ip, a.start,t.accesstoken,r.id, r.accountid, r.quizid, r.performerid, r.pipelineid, r.stepid, r.utms, r.fieldsrule, r.deleted, r.createdat +SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,t.accesstoken,r.accountid,r.utms,r.fieldsrule,r.performerid,r.stepid,r.pipelineid FROM answer a INNER JOIN quiz q ON a.quiz_id = q.id LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID @@ -2532,39 +2532,25 @@ FROM answer a INNER JOIN users u ON q.accountid = u.accountid AND u.amoid = r.accountid WHERE a.result = true AND s.id IS NULL + AND a.deleted = false AND r.deleted = false AND q.deleted = false ` type GettingAmoUsersTrueResultsRow struct { - ID int64 `db:"id" json:"id"` - Content sql.NullString `db:"content" json:"content"` QuizID int64 `db:"quiz_id" json:"quiz_id"` - QuestionID int64 `db:"question_id" json:"question_id"` - Fingerprint sql.NullString `db:"fingerprint" json:"fingerprint"` - Session sql.NullString `db:"session" json:"session"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + ID int64 `db:"id" json:"id"` Result sql.NullBool `db:"result" json:"result"` - New sql.NullBool `db:"new" json:"new"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` - Email string `db:"email" json:"email"` - DeviceType string `db:"device_type" json:"device_type"` - Device string `db:"device" json:"device"` - Os string `db:"os" json:"os"` - Browser string `db:"browser" json:"browser"` - Ip string `db:"ip" json:"ip"` - Start bool `db:"start" json:"start"` + QuestionID int64 `db:"question_id" json:"question_id"` + Content sql.NullString `db:"content" json:"content"` + Session sql.NullString `db:"session" json:"session"` Accesstoken string `db:"accesstoken" json:"accesstoken"` - ID_2 int64 `db:"id_2" json:"id_2"` Accountid int32 `db:"accountid" json:"accountid"` - Quizid int32 `db:"quizid" json:"quizid"` - Performerid int32 `db:"performerid" json:"performerid"` - Pipelineid int32 `db:"pipelineid" json:"pipelineid"` - Stepid int32 `db:"stepid" json:"stepid"` Utms []int32 `db:"utms" json:"utms"` Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` - Deleted_2 bool `db:"deleted_2" json:"deleted_2"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` + Performerid int32 `db:"performerid" json:"performerid"` + Stepid int32 `db:"stepid" json:"stepid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` } func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoUsersTrueResultsRow, error) { @@ -2577,34 +2563,19 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU for rows.Next() { var i GettingAmoUsersTrueResultsRow if err := rows.Scan( - &i.ID, - &i.Content, &i.QuizID, - &i.QuestionID, - &i.Fingerprint, - &i.Session, - &i.CreatedAt, + &i.ID, &i.Result, - &i.New, - &i.Deleted, - &i.Email, - &i.DeviceType, - &i.Device, - &i.Os, - &i.Browser, - &i.Ip, - &i.Start, + &i.QuestionID, + &i.Content, + &i.Session, &i.Accesstoken, - &i.ID_2, &i.Accountid, - &i.Quizid, - &i.Performerid, - &i.Pipelineid, - &i.Stepid, pq.Array(&i.Utms), &i.Fieldsrule, - &i.Deleted_2, - &i.Createdat, + &i.Performerid, + &i.Stepid, + &i.Pipelineid, ); err != nil { return nil, err } From 38cdac154bfbcbd2bd435e90b401a93e2bff2105 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 14:03:52 +0300 Subject: [PATCH 147/187] init new amo repo method --- model/amo.go | 16 ++++++++++++++++ repository/amo/amo.go | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/model/amo.go b/model/amo.go index 8a22d19..f0c1b47 100644 --- a/model/amo.go +++ b/model/amo.go @@ -223,3 +223,19 @@ var TypeMapping = map[string]FieldType{ "rating": TypeAmoText, "result": TypeAmoText, } + +type AmoUsersTrueResults struct { + QuizID int64 + AnswerID int64 + Result bool + QuestionID int64 + Content string + Session string + AccessToken string + AmoAccountID int32 + UTMs []int32 + FieldsRule Fieldsrule + PerformerID int32 + StepID int32 + PipelineID int32 +} diff --git a/repository/amo/amo.go b/repository/amo/amo.go index bcf6ad7..707eb9b 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -836,9 +836,38 @@ func (r *AmoRepository) UpdateUtmsFields(ctx context.Context, ids []int32) error return nil } -func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) error { +func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model.AmoUsersTrueResults, error) { rows, err := r.queries.GettingAmoUsersTrueResults(ctx) if err != nil { - return err + 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 + } + result := model.AmoUsersTrueResults{ + QuizID: row.QuizID, + AnswerID: row.QuizID, + Result: row.Result.Bool, + QuestionID: row.QuestionID, + Content: row.Content.String, + Session: row.Session.String, + AccessToken: row.Accesstoken, + AmoAccountID: row.Accountid, + UTMs: row.Utms, + FieldsRule: fieldsRule, + PerformerID: row.Performerid, + StepID: row.Stepid, + PipelineID: row.Pipelineid, + } + + results = append(results, result) + } + + return results, nil } From f1d3073dc9f14be7c6b62e9215496be7f581c35d Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 15:35:52 +0300 Subject: [PATCH 148/187] update amorepo init --- dal/dal.go | 7 +++++++ repository/answer/answer.go | 1 + 2 files changed, 8 insertions(+) diff --git a/dal/dal.go b/dal/dal.go index aa33fab..49c4b2c 100644 --- a/dal/dal.go +++ b/dal/dal.go @@ -137,6 +137,7 @@ type AmoDal struct { queries *sqlcgen.Queries AmoRepo *amo.AmoRepository QuestionRepo *question.QuestionRepository + AnswerRepo *answer.AnswerRepository } func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { @@ -164,11 +165,17 @@ func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { Pool: pool, }) + answerRepo := answer.NewAnswerRepository(answer.Deps{ + Queries: queries, + Pool: pool, + }) + return &AmoDal{ conn: pool, queries: queries, AmoRepo: amoRepo, QuestionRepo: questionRepo, + AnswerRepo: answerRepo, }, nil } diff --git a/repository/answer/answer.go b/repository/answer/answer.go index e579112..7e9c90c 100644 --- a/repository/answer/answer.go +++ b/repository/answer/answer.go @@ -83,6 +83,7 @@ func (r *AnswerRepository) GetAllAnswersByQuizID(ctx context.Context, session st } for _, row := range rows { + //todo тут забыл добавить проверку на то что minio !=nil /*if row.Questiontype == model.TypeFile { fmt.Println("GALL", row.Qid, row.QuestionID, row.Content) fileURL, err := r.answerMinio.GetAnswerURL(ctx, row.Qid.UUID.String(), row.QuestionID, row.Content.String) From 10b5336acfe6aed9f4255006abc0d6d5f7d2ad5b Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 15:59:43 +0300 Subject: [PATCH 149/187] update schema --- dal/schema/000010_init.up.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index efcb6ea..d9bdce1 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -112,9 +112,9 @@ CREATE UNIQUE INDEX idx_unique_rules ON rules (accountID, QuizID); CREATE TABLE IF NOT EXISTS amoCRMStatuses ( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AccountID INT NOT NULL, -- id аккаунта в амо - QuestionID BIGINT NOT NULL REFERENCES question(id), - AnswerID BIGINT NOT NULL REFERENCES answer(id), - FieldID INT NOT NULL, -- айдишник кастомного поля в амо + DealID INT NOT NULL, -- id сделки в которую добавлялось + AnswerID BIGINT NOT NULL REFERENCES answer(id), -- id true result который вызвал действие + FieldID INTEGER[] NOT NULL, -- айдишники кастомных полей в амо в которые добавлялись контенты ответов Status TEXT NOT NULL DEFAULT '', -- запись о ошибке, либо успехе CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); \ No newline at end of file From ba2d1c25c74979352551206561971b52a4d0c0d5 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 5 May 2024 10:42:43 +0300 Subject: [PATCH 150/187] update query add performer name --- dal/db_query/queries.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index d577b56..f61bc1d 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1013,7 +1013,7 @@ WHERE NOT EXISTS ( ); -- name: GettingAmoUsersTrueResults :many -SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,t.accesstoken,r.accountid,r.utms,r.fieldsrule,r.performerid,r.stepid,r.pipelineid +SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,t.accesstoken,r.accountid,r.utms,r.fieldsrule,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name FROM answer a INNER JOIN quiz q ON a.quiz_id = q.id LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID From 4a3c0925fa8d950ef4f5a599439c4d80a6a424e2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 4 May 2024 13:40:24 +0300 Subject: [PATCH 151/187] update sqlc gen --- dal/sqlcgen/models.go | 14 +++++++------- dal/sqlcgen/queries.sql.go | 30 ++++++++++++++++-------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 6172c73..5cae87e 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -21,13 +21,13 @@ type Account struct { } type Amocrmstatus struct { - ID int64 `db:"id" json:"id"` - Accountid int32 `db:"accountid" json:"accountid"` - Questionid int64 `db:"questionid" json:"questionid"` - Answerid int64 `db:"answerid" json:"answerid"` - Fieldid int32 `db:"fieldid" json:"fieldid"` - Status string `db:"status" json:"status"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` + ID int64 `db:"id" json:"id"` + Accountid int32 `db:"accountid" json:"accountid"` + Dealid int32 `db:"dealid" json:"dealid"` + Answerid int64 `db:"answerid" json:"answerid"` + Fieldid []int32 `db:"fieldid" json:"fieldid"` + Status string `db:"status" json:"status"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` } type Answer struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 72e5344..0eb474b 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2523,7 +2523,7 @@ func (q *Queries) GetUtmsByID(ctx context.Context, dollar_1 []int32) ([]GetUtmsB } const gettingAmoUsersTrueResults = `-- name: GettingAmoUsersTrueResults :many -SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,t.accesstoken,r.accountid,r.utms,r.fieldsrule,r.performerid,r.stepid,r.pipelineid +SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,t.accesstoken,r.accountid,r.utms,r.fieldsrule,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name FROM answer a INNER JOIN quiz q ON a.quiz_id = q.id LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID @@ -2538,19 +2538,20 @@ WHERE a.result = true ` type GettingAmoUsersTrueResultsRow struct { - QuizID int64 `db:"quiz_id" json:"quiz_id"` - ID int64 `db:"id" json:"id"` - Result sql.NullBool `db:"result" json:"result"` - QuestionID int64 `db:"question_id" json:"question_id"` - Content sql.NullString `db:"content" json:"content"` - Session sql.NullString `db:"session" json:"session"` - Accesstoken string `db:"accesstoken" json:"accesstoken"` - Accountid int32 `db:"accountid" json:"accountid"` - Utms []int32 `db:"utms" json:"utms"` - Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` - Performerid int32 `db:"performerid" json:"performerid"` - Stepid int32 `db:"stepid" json:"stepid"` - Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + QuizID int64 `db:"quiz_id" json:"quiz_id"` + ID int64 `db:"id" json:"id"` + Result sql.NullBool `db:"result" json:"result"` + QuestionID int64 `db:"question_id" json:"question_id"` + Content sql.NullString `db:"content" json:"content"` + Session sql.NullString `db:"session" json:"session"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` + Accountid int32 `db:"accountid" json:"accountid"` + Utms []int32 `db:"utms" json:"utms"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Performerid int32 `db:"performerid" json:"performerid"` + Stepid int32 `db:"stepid" json:"stepid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + PerformerName string `db:"performer_name" json:"performer_name"` } func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoUsersTrueResultsRow, error) { @@ -2576,6 +2577,7 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU &i.Performerid, &i.Stepid, &i.Pipelineid, + &i.PerformerName, ); err != nil { return nil, err } From 07895eccdd077d7bd7f39a8c3e7e2f37222a3560 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 5 May 2024 10:47:42 +0300 Subject: [PATCH 152/187] update amo repo mrthod --- model/amo.go | 39 ++++++++++++++++++++------------------- repository/amo/amo.go | 27 ++++++++++++++------------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/model/amo.go b/model/amo.go index f0c1b47..a0bdc0d 100644 --- a/model/amo.go +++ b/model/amo.go @@ -210,32 +210,33 @@ const ( ) var TypeMapping = map[string]FieldType{ - "variant": TypeAmoChainedList, - "images": TypeAmoFile, - "varimg": TypeAmoFile, + "variant": TypeAmoText, //TypeAmoChainedList, + "images": TypeAmoText, //TypeAmoFile, + "varimg": TypeAmoText, //TypeAmoFile, "file": TypeAmoFile, "text": TypeAmoText, "emoji": TypeAmoText, - "select": TypeAmoSelect, - "date": TypeAmoDate, - "number": TypeAmoNumeric, + "select": TypeAmoText, //TypeAmoSelect, + "date": TypeAmoText, //TypeAmoDate, + "number": TypeAmoText, //TypeAmoNumeric, "page": TypeAmoText, "rating": TypeAmoText, "result": TypeAmoText, } type AmoUsersTrueResults struct { - QuizID int64 - AnswerID int64 - Result bool - QuestionID int64 - Content string - Session string - AccessToken string - AmoAccountID int32 - UTMs []int32 - FieldsRule Fieldsrule - PerformerID int32 - StepID int32 - PipelineID int32 + QuizID int64 + AnswerID int64 + Result bool + QuestionID int64 + Content string + Session string + AccessToken string + AmoAccountID int32 + UTMs []int32 + FieldsRule Fieldsrule + PerformerID int32 + StepID int32 + PipelineID int32 + PerformerName string } diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 707eb9b..90ad3f8 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -851,19 +851,20 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model return nil, err } result := model.AmoUsersTrueResults{ - QuizID: row.QuizID, - AnswerID: row.QuizID, - Result: row.Result.Bool, - QuestionID: row.QuestionID, - Content: row.Content.String, - Session: row.Session.String, - AccessToken: row.Accesstoken, - AmoAccountID: row.Accountid, - UTMs: row.Utms, - FieldsRule: fieldsRule, - PerformerID: row.Performerid, - StepID: row.Stepid, - PipelineID: row.Pipelineid, + QuizID: row.QuizID, + AnswerID: row.QuizID, + Result: row.Result.Bool, + QuestionID: row.QuestionID, + Content: row.Content.String, + Session: row.Session.String, + AccessToken: row.Accesstoken, + AmoAccountID: row.Accountid, + UTMs: row.Utms, + FieldsRule: fieldsRule, + PerformerID: row.Performerid, + StepID: row.Stepid, + PipelineID: row.Pipelineid, + PerformerName: row.PerformerName, } results = append(results, result) From 7aabcb97f64de68d71799ce2eec5373b0132b841 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 11:54:05 +0300 Subject: [PATCH 153/187] missing error --- repository/amo/amo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 90ad3f8..50a9839 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -852,7 +852,7 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model } result := model.AmoUsersTrueResults{ QuizID: row.QuizID, - AnswerID: row.QuizID, + AnswerID: row.ID, Result: row.Result.Bool, QuestionID: row.QuestionID, Content: row.Content.String, From 52015d8136bd176d01e9456302cb671afcd8ef5d Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 12:55:45 +0300 Subject: [PATCH 154/187] update schema and add setting deal query --- dal/db_query/queries.sql | 7 +++++++ dal/schema/000010_init.up.sql | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index f61bc1d..9ae344a 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1025,3 +1025,10 @@ WHERE a.result = true AND a.deleted = false AND r.deleted = false AND q.deleted = false; + +-- name: SettingDealStatus :exec +INSERT INTO amoCRMStatuses (AccountID, DealID, AnswerID, Status) +SELECT u.AmoID, $1, $2, $3 +FROM tokens AS t + JOIN users AS u ON t.AccountID = u.AccountID +WHERE t.AccessToken = $4; \ No newline at end of file diff --git a/dal/schema/000010_init.up.sql b/dal/schema/000010_init.up.sql index d9bdce1..37ee29e 100644 --- a/dal/schema/000010_init.up.sql +++ b/dal/schema/000010_init.up.sql @@ -114,7 +114,6 @@ CREATE TABLE IF NOT EXISTS amoCRMStatuses ( AccountID INT NOT NULL, -- id аккаунта в амо DealID INT NOT NULL, -- id сделки в которую добавлялось AnswerID BIGINT NOT NULL REFERENCES answer(id), -- id true result который вызвал действие - FieldID INTEGER[] NOT NULL, -- айдишники кастомных полей в амо в которые добавлялись контенты ответов Status TEXT NOT NULL DEFAULT '', -- запись о ошибке, либо успехе CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); \ No newline at end of file From f1911fda2c5419c37232b31375cb8b01ad30ddc4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 12:57:14 +0300 Subject: [PATCH 155/187] update sqlc gen --- dal/sqlcgen/models.go | 1 - dal/sqlcgen/queries.sql.go | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 5cae87e..9070372 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -25,7 +25,6 @@ type Amocrmstatus struct { Accountid int32 `db:"accountid" json:"accountid"` Dealid int32 `db:"dealid" json:"dealid"` Answerid int64 `db:"answerid" json:"answerid"` - Fieldid []int32 `db:"fieldid" json:"fieldid"` Status string `db:"status" json:"status"` Createdat sql.NullTime `db:"createdat" json:"createdat"` } diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 0eb474b..fe80c13 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3143,6 +3143,31 @@ func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams return err } +const settingDealStatus = `-- name: SettingDealStatus :exec +INSERT INTO amoCRMStatuses (AccountID, DealID, AnswerID, Status) +SELECT u.AmoID, $1, $2, $3 +FROM tokens AS t + JOIN users AS u ON t.AccountID = u.AccountID +WHERE t.AccessToken = $4 +` + +type SettingDealStatusParams struct { + Dealid int32 `db:"dealid" json:"dealid"` + Answerid int64 `db:"answerid" json:"answerid"` + Status string `db:"status" json:"status"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` +} + +func (q *Queries) SettingDealStatus(ctx context.Context, arg SettingDealStatusParams) error { + _, err := q.db.ExecContext(ctx, settingDealStatus, + arg.Dealid, + arg.Answerid, + arg.Status, + arg.Accesstoken, + ) + return err +} + const softDeleteAccount = `-- name: SoftDeleteAccount :exec WITH userd AS ( SELECT AmoUserID FROM users WHERE users.AccountID = $1 From 7877cdcdc2d7357d447cabb3602ccb9901a014a9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 13:01:56 +0300 Subject: [PATCH 156/187] add new amo repo method --- repository/amo/amo.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 50a9839..c7d7e19 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -872,3 +872,18 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model return results, nil } + +func (r *AmoRepository) SaveDealStatus(ctx context.Context, dealID int32, answerID int64, status string, accessToken string) error { + err := r.queries.SettingDealStatus(ctx, sqlcgen.SettingDealStatusParams{ + Dealid: dealID, + Answerid: answerID, + Status: status, + Accesstoken: accessToken, + }) + + if err != nil { + return err + } + + return nil +} From eb032d19f851a9361fbf827ea86611704b93179d Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 13:34:01 +0300 Subject: [PATCH 157/187] add deps struct for setting created deal statuses --- repository/amo/amo.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index c7d7e19..d833d04 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -873,12 +873,19 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model return results, nil } -func (r *AmoRepository) SaveDealStatus(ctx context.Context, dealID int32, answerID int64, status string, accessToken string) error { +type SaveDealDeps struct { + DealID int32 + AnswerID int64 + Status string + AccessToken string +} + +func (r *AmoRepository) SaveDealStatus(ctx context.Context, deps SaveDealDeps) error { err := r.queries.SettingDealStatus(ctx, sqlcgen.SettingDealStatusParams{ - Dealid: dealID, - Answerid: answerID, - Status: status, - Accesstoken: accessToken, + Dealid: deps.DealID, + Answerid: deps.AnswerID, + Status: deps.Status, + Accesstoken: deps.AccessToken, }) if err != nil { From e12c66a560c1cd0f907e9a4abe68829aa519852b Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 13:58:59 +0300 Subject: [PATCH 158/187] update query --- dal/db_query/queries.sql | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 9ae344a..e65a653 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -661,13 +661,8 @@ INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, VALUES ($1, $2, $3, $4, $5, $6); -- name: WebhookUpdate :exec -UPDATE tokens AS t -SET AccessToken = (update_data ->> 'access_token')::varchar(50), - RefreshToken = (update_data ->> 'refresh_token')::varchar(50), - Expiration = to_timestamp((update_data ->> 'expiration')::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours', - CreatedAt = to_timestamp((update_data ->> 'created_at')::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours' -FROM json_array_elements($1::json) AS update_data -WHERE t.accountID = (update_data ->> 'account_id')::VARCHAR(30); +UPDATE tokens SET AccessToken = $1,RefreshToken = $2,Expiration = to_timestamp($3) AT TIME ZONE 'UTC' + INTERVAL '3 hours',CreatedAt = to_timestamp($4) AT TIME ZONE 'UTC' + INTERVAL '3 hours' +WHERE accountID = $5; -- name: GetAllTokens :many SELECT * FROM tokens; From f57e3431164b5c3ecf6001fc48f76ec54f71e396 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 14:00:28 +0300 Subject: [PATCH 159/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index fe80c13..0005352 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3355,17 +3355,26 @@ func (q *Queries) WebhookDelete(ctx context.Context, amouserid int32) error { } const webhookUpdate = `-- name: WebhookUpdate :exec -UPDATE tokens AS t -SET AccessToken = (update_data ->> 'access_token')::varchar(50), - RefreshToken = (update_data ->> 'refresh_token')::varchar(50), - Expiration = to_timestamp((update_data ->> 'expiration')::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours', - CreatedAt = to_timestamp((update_data ->> 'created_at')::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours' -FROM json_array_elements($1::json) AS update_data -WHERE t.accountID = (update_data ->> 'account_id')::VARCHAR(30) +UPDATE tokens SET AccessToken = $1,RefreshToken = $2,Expiration = to_timestamp($3) AT TIME ZONE 'UTC' + INTERVAL '3 hours',CreatedAt = to_timestamp($4) AT TIME ZONE 'UTC' + INTERVAL '3 hours' +WHERE accountID = $5 ` -func (q *Queries) WebhookUpdate(ctx context.Context, dollar_1 json.RawMessage) error { - _, err := q.db.ExecContext(ctx, webhookUpdate, dollar_1) +type WebhookUpdateParams struct { + Accesstoken string `db:"accesstoken" json:"accesstoken"` + Refreshtoken string `db:"refreshtoken" json:"refreshtoken"` + ToTimestamp float64 `db:"to_timestamp" json:"to_timestamp"` + ToTimestamp_2 float64 `db:"to_timestamp_2" json:"to_timestamp_2"` + Accountid string `db:"accountid" json:"accountid"` +} + +func (q *Queries) WebhookUpdate(ctx context.Context, arg WebhookUpdateParams) error { + _, err := q.db.ExecContext(ctx, webhookUpdate, + arg.Accesstoken, + arg.Refreshtoken, + arg.ToTimestamp, + arg.ToTimestamp_2, + arg.Accountid, + ) return err } From dacc4c4eb5f6ea226b4a1402ee558d21f3be9aaa Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 14:03:41 +0300 Subject: [PATCH 160/187] update amo repo --- repository/amo/amo.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index d833d04..fc78656 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -204,13 +204,14 @@ func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) e return nil } -func (r *AmoRepository) WebhookUpdate(ctx context.Context, tokens []model.Token) error { - dollar1, err := json.Marshal(tokens) - if err != nil { - return err - } - - err = r.queries.WebhookUpdate(ctx, dollar1) +func (r *AmoRepository) WebhookUpdate(ctx context.Context, token model.Token) error { + err := r.queries.WebhookUpdate(ctx, sqlcgen.WebhookUpdateParams{ + Accesstoken: token.AccessToken, + Refreshtoken: token.RefreshToken, + Accountid: token.AccountID, + ToTimestamp: token.Expiration, + ToTimestamp_2: token.CreatedAt, + }) if err != nil { return err } From eeb6853b6a6fbd1b401fc3a7a8592a6b6c5c2ad7 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 14:06:55 +0300 Subject: [PATCH 161/187] update sqlc gen --- dal/db_query/queries.sql | 4 ++-- dal/sqlcgen/queries.sql.go | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index e65a653..e44f997 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -661,7 +661,7 @@ INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, VALUES ($1, $2, $3, $4, $5, $6); -- name: WebhookUpdate :exec -UPDATE tokens SET AccessToken = $1,RefreshToken = $2,Expiration = to_timestamp($3) AT TIME ZONE 'UTC' + INTERVAL '3 hours',CreatedAt = to_timestamp($4) AT TIME ZONE 'UTC' + INTERVAL '3 hours' +UPDATE tokens SET AccessToken = $1,RefreshToken = $2,Expiration = to_timestamp(($3)::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours',CreatedAt = to_timestamp(($4)::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours' WHERE accountID = $5; -- name: GetAllTokens :many @@ -1026,4 +1026,4 @@ INSERT INTO amoCRMStatuses (AccountID, DealID, AnswerID, Status) SELECT u.AmoID, $1, $2, $3 FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID -WHERE t.AccessToken = $4; \ No newline at end of file +WHERE t.AccessToken = $4; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 0005352..23187ef 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3355,24 +3355,24 @@ func (q *Queries) WebhookDelete(ctx context.Context, amouserid int32) error { } const webhookUpdate = `-- name: WebhookUpdate :exec -UPDATE tokens SET AccessToken = $1,RefreshToken = $2,Expiration = to_timestamp($3) AT TIME ZONE 'UTC' + INTERVAL '3 hours',CreatedAt = to_timestamp($4) AT TIME ZONE 'UTC' + INTERVAL '3 hours' +UPDATE tokens SET AccessToken = $1,RefreshToken = $2,Expiration = to_timestamp(($3)::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours',CreatedAt = to_timestamp(($4)::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours' WHERE accountID = $5 ` type WebhookUpdateParams struct { - Accesstoken string `db:"accesstoken" json:"accesstoken"` - Refreshtoken string `db:"refreshtoken" json:"refreshtoken"` - ToTimestamp float64 `db:"to_timestamp" json:"to_timestamp"` - ToTimestamp_2 float64 `db:"to_timestamp_2" json:"to_timestamp_2"` - Accountid string `db:"accountid" json:"accountid"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` + Refreshtoken string `db:"refreshtoken" json:"refreshtoken"` + Column3 int64 `db:"column_3" json:"column_3"` + Column4 int64 `db:"column_4" json:"column_4"` + Accountid string `db:"accountid" json:"accountid"` } func (q *Queries) WebhookUpdate(ctx context.Context, arg WebhookUpdateParams) error { _, err := q.db.ExecContext(ctx, webhookUpdate, arg.Accesstoken, arg.Refreshtoken, - arg.ToTimestamp, - arg.ToTimestamp_2, + arg.Column3, + arg.Column4, arg.Accountid, ) return err From 3afa7086d8e74794eecedbcba37941d60ca3f718 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 14:08:33 +0300 Subject: [PATCH 162/187] update amo repo --- repository/amo/amo.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index fc78656..d835a19 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -206,11 +206,11 @@ func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) e func (r *AmoRepository) WebhookUpdate(ctx context.Context, token model.Token) error { err := r.queries.WebhookUpdate(ctx, sqlcgen.WebhookUpdateParams{ - Accesstoken: token.AccessToken, - Refreshtoken: token.RefreshToken, - Accountid: token.AccountID, - ToTimestamp: token.Expiration, - ToTimestamp_2: token.CreatedAt, + Accesstoken: token.AccessToken, + Refreshtoken: token.RefreshToken, + Accountid: token.AccountID, + Column3: token.Expiration, + Column4: token.CreatedAt, }) if err != nil { return err From 3f402879a64aa5e38f9d22ae4557062998e615a4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 20:33:18 +0300 Subject: [PATCH 163/187] add new query for update amoCRM statuses --- dal/db_query/queries.sql | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index e44f997..5adfef7 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1021,9 +1021,13 @@ WHERE a.result = true AND r.deleted = false AND q.deleted = false; --- name: SettingDealStatus :exec +-- name: SettingDealAmoStatus :exec INSERT INTO amoCRMStatuses (AccountID, DealID, AnswerID, Status) SELECT u.AmoID, $1, $2, $3 FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $4; + +-- name: UpdatingDealAmoStatus :exec +UPDATE amoCRMStatuses SET Status = $1 +WHERE AnswerID = $2 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3); \ No newline at end of file From ff72e7b0410f8b8944848c5af831079aefacfed8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 20:36:24 +0300 Subject: [PATCH 164/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 23187ef..bf41a46 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3143,7 +3143,7 @@ func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams return err } -const settingDealStatus = `-- name: SettingDealStatus :exec +const settingDealAmoStatus = `-- name: SettingDealAmoStatus :exec INSERT INTO amoCRMStatuses (AccountID, DealID, AnswerID, Status) SELECT u.AmoID, $1, $2, $3 FROM tokens AS t @@ -3151,15 +3151,15 @@ FROM tokens AS t WHERE t.AccessToken = $4 ` -type SettingDealStatusParams struct { +type SettingDealAmoStatusParams struct { Dealid int32 `db:"dealid" json:"dealid"` Answerid int64 `db:"answerid" json:"answerid"` Status string `db:"status" json:"status"` Accesstoken string `db:"accesstoken" json:"accesstoken"` } -func (q *Queries) SettingDealStatus(ctx context.Context, arg SettingDealStatusParams) error { - _, err := q.db.ExecContext(ctx, settingDealStatus, +func (q *Queries) SettingDealAmoStatus(ctx context.Context, arg SettingDealAmoStatusParams) error { + _, err := q.db.ExecContext(ctx, settingDealAmoStatus, arg.Dealid, arg.Answerid, arg.Status, @@ -3342,6 +3342,22 @@ func (q *Queries) UpdateUtmsFields(ctx context.Context, dollar_1 []int32) error return err } +const updatingDealAmoStatus = `-- name: UpdatingDealAmoStatus :exec +UPDATE amoCRMStatuses SET Status = $1 +WHERE AnswerID = $2 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3) +` + +type UpdatingDealAmoStatusParams struct { + Status string `db:"status" json:"status"` + Answerid int64 `db:"answerid" json:"answerid"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` +} + +func (q *Queries) UpdatingDealAmoStatus(ctx context.Context, arg UpdatingDealAmoStatusParams) error { + _, err := q.db.ExecContext(ctx, updatingDealAmoStatus, arg.Status, arg.Answerid, arg.Accesstoken) + return err +} + const webhookDelete = `-- name: WebhookDelete :exec WITH userd AS ( UPDATE users SET Deleted = true WHERE AmoUserID = $1 RETURNING AccountID From 3d390409ec60ac2b7406fe1b4a6926bb7c0844a8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 20:43:50 +0300 Subject: [PATCH 165/187] add amo repo method --- dal/db_query/queries.sql | 2 +- repository/amo/amo.go | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 5adfef7..81d26ca 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1030,4 +1030,4 @@ WHERE t.AccessToken = $4; -- name: UpdatingDealAmoStatus :exec UPDATE amoCRMStatuses SET Status = $1 -WHERE AnswerID = $2 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3); \ No newline at end of file +WHERE AnswerID = $2 AND DealID = $4 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3); \ No newline at end of file diff --git a/repository/amo/amo.go b/repository/amo/amo.go index d835a19..debf7d2 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -874,15 +874,15 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model return results, nil } -type SaveDealDeps struct { +type SaveDealAmoDeps struct { DealID int32 AnswerID int64 Status string AccessToken string } -func (r *AmoRepository) SaveDealStatus(ctx context.Context, deps SaveDealDeps) error { - err := r.queries.SettingDealStatus(ctx, sqlcgen.SettingDealStatusParams{ +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, @@ -895,3 +895,17 @@ func (r *AmoRepository) SaveDealStatus(ctx context.Context, deps SaveDealDeps) e return nil } + +func (r *AmoRepository) UpdatingDealAmoStatus(ctx context.Context, deps SaveDealAmoDeps) error { + err := r.queries.UpdatingDealAmoStatus(ctx, sqlcgen.UpdatingDealAmoStatusParams{ + Status: deps.Status, + Answerid: deps.AnswerID, + Accesstoken: deps.AccessToken, + }) + + if err != nil { + return err + } + + return nil +} From 8fbefe5fe02c0931155af66722b056da897240df Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 20:45:19 +0300 Subject: [PATCH 166/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index bf41a46..e9735d9 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3344,17 +3344,23 @@ func (q *Queries) UpdateUtmsFields(ctx context.Context, dollar_1 []int32) error const updatingDealAmoStatus = `-- name: UpdatingDealAmoStatus :exec UPDATE amoCRMStatuses SET Status = $1 -WHERE AnswerID = $2 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3) +WHERE AnswerID = $2 AND DealID = $4 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3) ` type UpdatingDealAmoStatusParams struct { Status string `db:"status" json:"status"` Answerid int64 `db:"answerid" json:"answerid"` Accesstoken string `db:"accesstoken" json:"accesstoken"` + Dealid int32 `db:"dealid" json:"dealid"` } func (q *Queries) UpdatingDealAmoStatus(ctx context.Context, arg UpdatingDealAmoStatusParams) error { - _, err := q.db.ExecContext(ctx, updatingDealAmoStatus, arg.Status, arg.Answerid, arg.Accesstoken) + _, err := q.db.ExecContext(ctx, updatingDealAmoStatus, + arg.Status, + arg.Answerid, + arg.Accesstoken, + arg.Dealid, + ) return err } From 58e8f1dabdec2f5400c6b6d01b1f7cfda1fe2163 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 20:47:36 +0300 Subject: [PATCH 167/187] update amo repo method --- repository/amo/amo.go | 1 + 1 file changed, 1 insertion(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index debf7d2..c8f8ef9 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -901,6 +901,7 @@ func (r *AmoRepository) UpdatingDealAmoStatus(ctx context.Context, deps SaveDeal Status: deps.Status, Answerid: deps.AnswerID, Accesstoken: deps.AccessToken, + Dealid: deps.DealID, }) if err != nil { From 1d429fceee71417b602b23564aa085b7577a0611 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 21:38:35 +0300 Subject: [PATCH 168/187] remove answer id in updating --- dal/db_query/queries.sql | 2 +- repository/amo/amo.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 81d26ca..b7c259d 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1030,4 +1030,4 @@ WHERE t.AccessToken = $4; -- name: UpdatingDealAmoStatus :exec UPDATE amoCRMStatuses SET Status = $1 -WHERE AnswerID = $2 AND DealID = $4 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3); \ No newline at end of file +WHERE DealID = $2 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3); \ No newline at end of file diff --git a/repository/amo/amo.go b/repository/amo/amo.go index c8f8ef9..052616e 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -899,7 +899,6 @@ func (r *AmoRepository) SaveDealAmoStatus(ctx context.Context, deps SaveDealAmoD func (r *AmoRepository) UpdatingDealAmoStatus(ctx context.Context, deps SaveDealAmoDeps) error { err := r.queries.UpdatingDealAmoStatus(ctx, sqlcgen.UpdatingDealAmoStatusParams{ Status: deps.Status, - Answerid: deps.AnswerID, Accesstoken: deps.AccessToken, Dealid: deps.DealID, }) From db80ad28a0505fac085d38d0390754d367431256 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 21:40:34 +0300 Subject: [PATCH 169/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index e9735d9..d5ca3d4 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3344,23 +3344,17 @@ func (q *Queries) UpdateUtmsFields(ctx context.Context, dollar_1 []int32) error const updatingDealAmoStatus = `-- name: UpdatingDealAmoStatus :exec UPDATE amoCRMStatuses SET Status = $1 -WHERE AnswerID = $2 AND DealID = $4 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3) +WHERE DealID = $2 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3) ` type UpdatingDealAmoStatusParams struct { Status string `db:"status" json:"status"` - Answerid int64 `db:"answerid" json:"answerid"` - Accesstoken string `db:"accesstoken" json:"accesstoken"` Dealid int32 `db:"dealid" json:"dealid"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` } func (q *Queries) UpdatingDealAmoStatus(ctx context.Context, arg UpdatingDealAmoStatusParams) error { - _, err := q.db.ExecContext(ctx, updatingDealAmoStatus, - arg.Status, - arg.Answerid, - arg.Accesstoken, - arg.Dealid, - ) + _, err := q.db.ExecContext(ctx, updatingDealAmoStatus, arg.Status, arg.Dealid, arg.Accesstoken) return err } From edeece4bfbf3cfe809dbcb1e589abed447a1947d Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 6 May 2024 23:07:10 +0300 Subject: [PATCH 170/187] update sqlc gen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index b7c259d..634da7a 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1030,4 +1030,4 @@ WHERE t.AccessToken = $4; -- name: UpdatingDealAmoStatus :exec UPDATE amoCRMStatuses SET Status = $1 -WHERE DealID = $2 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3); \ No newline at end of file +WHERE DealID = $2 AND AccountID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3); diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index d5ca3d4..f47dfe9 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3344,7 +3344,7 @@ func (q *Queries) UpdateUtmsFields(ctx context.Context, dollar_1 []int32) error const updatingDealAmoStatus = `-- name: UpdatingDealAmoStatus :exec UPDATE amoCRMStatuses SET Status = $1 -WHERE DealID = $2 AND AmoID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3) +WHERE DealID = $2 AND AccountID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3) ` type UpdatingDealAmoStatusParams struct { From e34ea9c23046fd2ae3a0de0569a66fe5078ca795 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 16:37:24 +0300 Subject: [PATCH 171/187] update fieldRule --- model/amo.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/model/amo.go b/model/amo.go index a0bdc0d..ddcb94e 100644 --- a/model/amo.go +++ b/model/amo.go @@ -151,10 +151,10 @@ type Rule struct { } type Fieldsrule struct { - Lead []FieldRule `json:"Lead"` - Contact []FieldRule `json:"Contact"` - Company []FieldRule `json:"Company"` - Customer []FieldRule `json:"Customer"` + Lead []FieldRule `json:"Lead"` + Contact []ContactRules `json:"Contact"` + Company []FieldRule `json:"Company"` + Customer []FieldRule `json:"Customer"` } type FieldRule struct { @@ -162,6 +162,11 @@ type FieldRule struct { Questionid map[int]int `json:"QuestionID"` // ключ id вопроса значение id астомного поля } +type ContactRules struct { + // ключ имя, значение id кастомного поля + ContactRuleMap map[string]int +} + type UTM struct { /* - айдишник в нашей системе Primary Key*/ ID int64 `json:"ID"` From 8f231d803329106b7204a1a47b12c8b944a99c98 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 16:40:29 +0300 Subject: [PATCH 172/187] update amo dal initialize --- dal/dal.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dal/dal.go b/dal/dal.go index 49c4b2c..6a500c2 100644 --- a/dal/dal.go +++ b/dal/dal.go @@ -138,6 +138,7 @@ type AmoDal struct { AmoRepo *amo.AmoRepository QuestionRepo *question.QuestionRepository AnswerRepo *answer.AnswerRepository + QuizRepo *quiz.QuizRepository } func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { @@ -170,12 +171,18 @@ func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { Pool: pool, }) + quizRepo := quiz.NewQuizRepository(quiz.Deps{ + Queries: queries, + Pool: pool, + }) + return &AmoDal{ conn: pool, queries: queries, AmoRepo: amoRepo, QuestionRepo: questionRepo, AnswerRepo: answerRepo, + QuizRepo: quizRepo, }, nil } From 7b58bc46facb3d015fa88c2b64b20a64e55eb630 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 17:08:33 +0300 Subject: [PATCH 173/187] add contact quiz config struct --- model/amo.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/model/amo.go b/model/amo.go index ddcb94e..44bc09f 100644 --- a/model/amo.go +++ b/model/amo.go @@ -167,6 +167,26 @@ type ContactRules struct { ContactRuleMap map[string]int } +type QuizContact struct { + FormContact struct { + Fields struct { + Name ContactField `json:"name"` + Email ContactField `json:"email"` + Phone ContactField `json:"phone"` + Text ContactField `json:"text"` + Address ContactField `json:"address"` + } `json:"fields"` + } `json:"formContact"` +} + +type ContactField struct { + Text string `json:"text"` + InnerText string `json:"innerText"` + Key string `json:"key"` + Required bool `json:"required"` + Used bool `json:"used"` +} + type UTM struct { /* - айдишник в нашей системе Primary Key*/ ID int64 `json:"ID"` From 9a2f7a097e145ea64c44a110aafc443cf78ea0c8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 17:29:19 +0300 Subject: [PATCH 174/187] add types contact quiz config, for detection --- model/amo.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/model/amo.go b/model/amo.go index 44bc09f..11f881b 100644 --- a/model/amo.go +++ b/model/amo.go @@ -187,6 +187,16 @@ type ContactField struct { Used bool `json:"used"` } +type ContactQuizConfig string + +const ( + TypeContactName ContactQuizConfig = "name" + TypeContactEmail ContactQuizConfig = "email" + TypeContactPhone ContactQuizConfig = "phone" + TypeContactText ContactQuizConfig = "text" + TypeContactAddress ContactQuizConfig = "address" +) + type UTM struct { /* - айдишник в нашей системе Primary Key*/ ID int64 `json:"ID"` From b2e6000c1f404ca61b23db184810954f61e2926a Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 18:29:04 +0300 Subject: [PATCH 175/187] update --- model/amo.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/model/amo.go b/model/amo.go index 11f881b..ec87d68 100644 --- a/model/amo.go +++ b/model/amo.go @@ -151,10 +151,10 @@ type Rule struct { } type Fieldsrule struct { - Lead []FieldRule `json:"Lead"` - Contact []ContactRules `json:"Contact"` - Company []FieldRule `json:"Company"` - Customer []FieldRule `json:"Customer"` + Lead []FieldRule `json:"Lead"` + Contact ContactRules `json:"Contact"` + Company []FieldRule `json:"Company"` + Customer []FieldRule `json:"Customer"` } type FieldRule struct { From 358bd8cd34703d5fcda24eea10143296a661b4de Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 20:17:53 +0300 Subject: [PATCH 176/187] add queries foe batching delete amo entity --- dal/db_query/queries.sql | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 634da7a..9f1148c 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1031,3 +1031,18 @@ WHERE t.AccessToken = $4; -- name: UpdatingDealAmoStatus :exec UPDATE amoCRMStatuses SET Status = $1 WHERE DealID = $2 AND AccountID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3); + +-- name: DeleteFields :exec +UPDATE fields SET Deleted = true WHERE ID = ANY($1); + +-- name: DeleteTags :exec +UPDATE tags SET Deleted = true WHERE ID = ANY($1); + +-- name: DeleteSteps :exec +UPDATE steps SET Deleted = true WHERE ID = ANY($1); + +-- name: DeletePipelines :exec +UPDATE pipelines SET Deleted = true WHERE ID = ANY($1); + +-- name: DeleteUsers :exec +UPDATE users SET Deleted = true WHERE ID = ANY($1); From 506efb40fc521a41c3fdeae32e25a48da49f0e18 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 20:20:07 +0300 Subject: [PATCH 177/187] update sqlc gen --- dal/sqlcgen/queries.sql.go | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index f47dfe9..33d6a2f 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -840,6 +840,24 @@ func (q *Queries) DeleteAccountById(ctx context.Context, id uuid.UUID) error { return err } +const deleteFields = `-- name: DeleteFields :exec +UPDATE fields SET Deleted = true WHERE ID = ANY($1) +` + +func (q *Queries) DeleteFields(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deleteFields, id) + return err +} + +const deletePipelines = `-- name: DeletePipelines :exec +UPDATE pipelines SET Deleted = true WHERE ID = ANY($1) +` + +func (q *Queries) DeletePipelines(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deletePipelines, id) + return err +} + const deletePrivilegeByAccID = `-- name: DeletePrivilegeByAccID :exec DELETE FROM privileges WHERE account_id = $1 ` @@ -929,6 +947,33 @@ func (q *Queries) DeleteQuizByID(ctx context.Context, arg DeleteQuizByIDParams) return i, err } +const deleteSteps = `-- name: DeleteSteps :exec +UPDATE steps SET Deleted = true WHERE ID = ANY($1) +` + +func (q *Queries) DeleteSteps(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deleteSteps, id) + return err +} + +const deleteTags = `-- name: DeleteTags :exec +UPDATE tags SET Deleted = true WHERE ID = ANY($1) +` + +func (q *Queries) DeleteTags(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deleteTags, id) + return err +} + +const deleteUsers = `-- name: DeleteUsers :exec +UPDATE users SET Deleted = true WHERE ID = ANY($1) +` + +func (q *Queries) DeleteUsers(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deleteUsers, id) + return err +} + const deletingUTM = `-- name: DeletingUTM :exec UPDATE utms SET Deleted = true WHERE ID = ANY($1::int[]) ` From 7cb5ac8273156647ba18b90cc56181de65d2badd Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 20:24:35 +0300 Subject: [PATCH 178/187] update sqlc gen --- dal/db_query/queries.sql | 10 +++++----- dal/sqlcgen/queries.sql.go | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 9f1148c..b173976 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1033,16 +1033,16 @@ UPDATE amoCRMStatuses SET Status = $1 WHERE DealID = $2 AND AccountID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3); -- name: DeleteFields :exec -UPDATE fields SET Deleted = true WHERE ID = ANY($1); +UPDATE fields SET Deleted = true WHERE ID = ANY(array[$1]); -- name: DeleteTags :exec -UPDATE tags SET Deleted = true WHERE ID = ANY($1); +UPDATE tags SET Deleted = true WHERE ID = ANY(array[$1]); -- name: DeleteSteps :exec -UPDATE steps SET Deleted = true WHERE ID = ANY($1); +UPDATE steps SET Deleted = true WHERE ID = ANY(array[$1]); -- name: DeletePipelines :exec -UPDATE pipelines SET Deleted = true WHERE ID = ANY($1); +UPDATE pipelines SET Deleted = true WHERE ID = ANY(array[$1]); -- name: DeleteUsers :exec -UPDATE users SET Deleted = true WHERE ID = ANY($1); +UPDATE users SET Deleted = true WHERE ID = ANY(array[$1]); diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 33d6a2f..18746eb 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -841,7 +841,7 @@ func (q *Queries) DeleteAccountById(ctx context.Context, id uuid.UUID) error { } const deleteFields = `-- name: DeleteFields :exec -UPDATE fields SET Deleted = true WHERE ID = ANY($1) +UPDATE fields SET Deleted = true WHERE ID = ANY(array[$1]) ` func (q *Queries) DeleteFields(ctx context.Context, id int64) error { @@ -850,7 +850,7 @@ func (q *Queries) DeleteFields(ctx context.Context, id int64) error { } const deletePipelines = `-- name: DeletePipelines :exec -UPDATE pipelines SET Deleted = true WHERE ID = ANY($1) +UPDATE pipelines SET Deleted = true WHERE ID = ANY(array[$1]) ` func (q *Queries) DeletePipelines(ctx context.Context, id int64) error { @@ -948,7 +948,7 @@ func (q *Queries) DeleteQuizByID(ctx context.Context, arg DeleteQuizByIDParams) } const deleteSteps = `-- name: DeleteSteps :exec -UPDATE steps SET Deleted = true WHERE ID = ANY($1) +UPDATE steps SET Deleted = true WHERE ID = ANY(array[$1]) ` func (q *Queries) DeleteSteps(ctx context.Context, id int64) error { @@ -957,7 +957,7 @@ func (q *Queries) DeleteSteps(ctx context.Context, id int64) error { } const deleteTags = `-- name: DeleteTags :exec -UPDATE tags SET Deleted = true WHERE ID = ANY($1) +UPDATE tags SET Deleted = true WHERE ID = ANY(array[$1]) ` func (q *Queries) DeleteTags(ctx context.Context, id int64) error { @@ -966,7 +966,7 @@ func (q *Queries) DeleteTags(ctx context.Context, id int64) error { } const deleteUsers = `-- name: DeleteUsers :exec -UPDATE users SET Deleted = true WHERE ID = ANY($1) +UPDATE users SET Deleted = true WHERE ID = ANY(array[$1]) ` func (q *Queries) DeleteUsers(ctx context.Context, id int64) error { From 61cc3891acf750a67c59d31bfb0e2cfdee337105 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 20:26:13 +0300 Subject: [PATCH 179/187] update sqlc gen --- dal/db_query/queries.sql | 10 +++++----- dal/sqlcgen/queries.sql.go | 30 +++++++++++++++--------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index b173976..10346d5 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1033,16 +1033,16 @@ UPDATE amoCRMStatuses SET Status = $1 WHERE DealID = $2 AND AccountID = (SELECT u.AmoID FROM tokens AS t JOIN users AS u ON t.AccountID = u.AccountID WHERE t.AccessToken = $3); -- name: DeleteFields :exec -UPDATE fields SET Deleted = true WHERE ID = ANY(array[$1]); +UPDATE fields SET Deleted = true WHERE ID = ANY($1::bigint[]); -- name: DeleteTags :exec -UPDATE tags SET Deleted = true WHERE ID = ANY(array[$1]); +UPDATE tags SET Deleted = true WHERE ID = ANY($1::bigint[]); -- name: DeleteSteps :exec -UPDATE steps SET Deleted = true WHERE ID = ANY(array[$1]); +UPDATE steps SET Deleted = true WHERE ID = ANY($1::bigint[]); -- name: DeletePipelines :exec -UPDATE pipelines SET Deleted = true WHERE ID = ANY(array[$1]); +UPDATE pipelines SET Deleted = true WHERE ID = ANY($1::bigint[]); -- name: DeleteUsers :exec -UPDATE users SET Deleted = true WHERE ID = ANY(array[$1]); +UPDATE users SET Deleted = true WHERE ID = ANY($1::bigint[]); diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 18746eb..40a349f 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -841,20 +841,20 @@ func (q *Queries) DeleteAccountById(ctx context.Context, id uuid.UUID) error { } const deleteFields = `-- name: DeleteFields :exec -UPDATE fields SET Deleted = true WHERE ID = ANY(array[$1]) +UPDATE fields SET Deleted = true WHERE ID = ANY($1::bigint[]) ` -func (q *Queries) DeleteFields(ctx context.Context, id int64) error { - _, err := q.db.ExecContext(ctx, deleteFields, id) +func (q *Queries) DeleteFields(ctx context.Context, dollar_1 []int64) error { + _, err := q.db.ExecContext(ctx, deleteFields, pq.Array(dollar_1)) return err } const deletePipelines = `-- name: DeletePipelines :exec -UPDATE pipelines SET Deleted = true WHERE ID = ANY(array[$1]) +UPDATE pipelines SET Deleted = true WHERE ID = ANY($1::bigint[]) ` -func (q *Queries) DeletePipelines(ctx context.Context, id int64) error { - _, err := q.db.ExecContext(ctx, deletePipelines, id) +func (q *Queries) DeletePipelines(ctx context.Context, dollar_1 []int64) error { + _, err := q.db.ExecContext(ctx, deletePipelines, pq.Array(dollar_1)) return err } @@ -948,29 +948,29 @@ func (q *Queries) DeleteQuizByID(ctx context.Context, arg DeleteQuizByIDParams) } const deleteSteps = `-- name: DeleteSteps :exec -UPDATE steps SET Deleted = true WHERE ID = ANY(array[$1]) +UPDATE steps SET Deleted = true WHERE ID = ANY($1::bigint[]) ` -func (q *Queries) DeleteSteps(ctx context.Context, id int64) error { - _, err := q.db.ExecContext(ctx, deleteSteps, id) +func (q *Queries) DeleteSteps(ctx context.Context, dollar_1 []int64) error { + _, err := q.db.ExecContext(ctx, deleteSteps, pq.Array(dollar_1)) return err } const deleteTags = `-- name: DeleteTags :exec -UPDATE tags SET Deleted = true WHERE ID = ANY(array[$1]) +UPDATE tags SET Deleted = true WHERE ID = ANY($1::bigint[]) ` -func (q *Queries) DeleteTags(ctx context.Context, id int64) error { - _, err := q.db.ExecContext(ctx, deleteTags, id) +func (q *Queries) DeleteTags(ctx context.Context, dollar_1 []int64) error { + _, err := q.db.ExecContext(ctx, deleteTags, pq.Array(dollar_1)) return err } const deleteUsers = `-- name: DeleteUsers :exec -UPDATE users SET Deleted = true WHERE ID = ANY(array[$1]) +UPDATE users SET Deleted = true WHERE ID = ANY($1::bigint[]) ` -func (q *Queries) DeleteUsers(ctx context.Context, id int64) error { - _, err := q.db.ExecContext(ctx, deleteUsers, id) +func (q *Queries) DeleteUsers(ctx context.Context, dollar_1 []int64) error { + _, err := q.db.ExecContext(ctx, deleteUsers, pq.Array(dollar_1)) return err } From 2914bf587e12ea2712b37d49e8fd525ce44e5fb4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 20:29:43 +0300 Subject: [PATCH 180/187] add new delete methods to amo repo --- repository/amo/amo.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 052616e..84fad6c 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -185,6 +185,14 @@ func (r *AmoRepository) GetTokenByID(ctx context.Context, accountID string) (*mo }, nil } +func (r *AmoRepository) DeleteUsers(ctx context.Context, ids []int64) error { + err := r.queries.DeleteUsers(ctx, ids) + if err != nil { + return err + } + return nil +} + // методы webhook func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) error { @@ -346,6 +354,14 @@ func (r *AmoRepository) CheckPipelines(ctx context.Context, pipelines []model.Pi return nil } +func (r *AmoRepository) DeletePipelines(ctx context.Context, ids []int64) error { + err := r.queries.DeletePipelines(ctx, ids) + if err != nil { + return err + } + return nil +} + // методы steps func (r *AmoRepository) GetStepsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListStepsResp, error) { @@ -421,6 +437,14 @@ func (r *AmoRepository) CheckSteps(ctx context.Context, steps []model.Step) erro return nil } +func (r *AmoRepository) DeleteSteps(ctx context.Context, ids []int64) error { + err := r.queries.DeleteSteps(ctx, ids) + if err != nil { + return err + } + return nil +} + // методы tags func (r *AmoRepository) GetTagsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListTagsResp, error) { @@ -506,6 +530,14 @@ func (r *AmoRepository) CheckTags(ctx context.Context, tags []model.Tag, tokenID return nil } +func (r *AmoRepository) DeleteTags(ctx context.Context, ids []int64) error { + err := r.queries.DeleteTags(ctx, ids) + if err != nil { + return err + } + return nil +} + // методы fields func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListFieldsResp, error) { @@ -636,6 +668,14 @@ func (r *AmoRepository) GetUserFieldsByID(ctx context.Context, accountID int32) return fields, err } +func (r *AmoRepository) DeleteFields(ctx context.Context, ids []int64) error { + err := r.queries.DeleteTags(ctx, ids) + if err != nil { + return err + } + return nil +} + // методы rules func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error { From 17e2f6acba38492ec428e15daffeb48f6755f92a Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 20:30:31 +0300 Subject: [PATCH 181/187] add new delete methods to amo repo --- repository/amo/amo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 84fad6c..1dc6a0d 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -669,7 +669,7 @@ func (r *AmoRepository) GetUserFieldsByID(ctx context.Context, accountID int32) } func (r *AmoRepository) DeleteFields(ctx context.Context, ids []int64) error { - err := r.queries.DeleteTags(ctx, ids) + err := r.queries.DeleteFields(ctx, ids) if err != nil { return err } From 9f3b71ecd56ea43596e4c069f487e459044842c8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 20:44:20 +0300 Subject: [PATCH 182/187] add new queries --- dal/db_query/queries.sql | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 10346d5..5dc2551 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -946,7 +946,7 @@ WHERE -- name: GetUserFieldsByID :many SELECT ID,AmoID,Code,AccountID,Name,Entity,Type FROM fields -WHERE AccountID = $1; +WHERE AccountID = $1 AND Deleted = false; -- name: UpdateUtms :exec UPDATE utms AS u @@ -1046,3 +1046,23 @@ UPDATE pipelines SET Deleted = true WHERE ID = ANY($1::bigint[]); -- name: DeleteUsers :exec UPDATE users SET Deleted = true WHERE ID = ANY($1::bigint[]); + +-- name: GetUserTagsByID :many +SELECT ID,AmoID,AccountID,Name,Entity,Type,Color +FROM tags +WHERE AccountID = $1 AND Deleted = false; + +-- name: GetUserStepsByID :many +SELECT ID,AmoID,PipelineID,AccountID,Name,Color +FROM steps +WHERE AccountID = $1 AND Deleted = false; + +-- name: GetUserPipelinesByID :many +SELECT ID,AmoID,AccountID,Name,IsArchive +FROM pipelines +WHERE AccountID = $1 AND Deleted = false; + +-- name: GetUserUsersByID :many +SELECT ID,AccountID,AmoID,Name,Email,Role,"Group",Subdomain,AmoUserID,Country +FROM users +WHERE AmoUserID = $1 AND Deleted = false; \ No newline at end of file From 767c884ae0de00d61724bd0e412963020d4bb381 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 20:47:49 +0300 Subject: [PATCH 183/187] update sqlc gen --- dal/db_query/queries.sql | 4 +- dal/sqlcgen/queries.sql.go | 188 ++++++++++++++++++++++++++++++++++++- 2 files changed, 189 insertions(+), 3 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 5dc2551..c65c60c 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1048,7 +1048,7 @@ UPDATE pipelines SET Deleted = true WHERE ID = ANY($1::bigint[]); UPDATE users SET Deleted = true WHERE ID = ANY($1::bigint[]); -- name: GetUserTagsByID :many -SELECT ID,AmoID,AccountID,Name,Entity,Type,Color +SELECT ID,AmoID,AccountID,Name,Entity,Color FROM tags WHERE AccountID = $1 AND Deleted = false; @@ -1065,4 +1065,4 @@ WHERE AccountID = $1 AND Deleted = false; -- name: GetUserUsersByID :many SELECT ID,AccountID,AmoID,Name,Email,Role,"Group",Subdomain,AmoUserID,Country FROM users -WHERE AmoUserID = $1 AND Deleted = false; \ No newline at end of file +WHERE AmoUserID = $1 AND Deleted = false; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 40a349f..fd12b82 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2409,7 +2409,7 @@ func (q *Queries) GetUTMsWithPagination(ctx context.Context, arg GetUTMsWithPagi const getUserFieldsByID = `-- name: GetUserFieldsByID :many SELECT ID,AmoID,Code,AccountID,Name,Entity,Type FROM fields -WHERE AccountID = $1 +WHERE AccountID = $1 AND Deleted = false ` type GetUserFieldsByIDRow struct { @@ -2453,6 +2453,192 @@ func (q *Queries) GetUserFieldsByID(ctx context.Context, accountid int32) ([]Get return items, nil } +const getUserPipelinesByID = `-- name: GetUserPipelinesByID :many +SELECT ID,AmoID,AccountID,Name,IsArchive +FROM pipelines +WHERE AccountID = $1 AND Deleted = false +` + +type GetUserPipelinesByIDRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Isarchive bool `db:"isarchive" json:"isarchive"` +} + +func (q *Queries) GetUserPipelinesByID(ctx context.Context, accountid int32) ([]GetUserPipelinesByIDRow, error) { + rows, err := q.db.QueryContext(ctx, getUserPipelinesByID, accountid) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetUserPipelinesByIDRow + for rows.Next() { + var i GetUserPipelinesByIDRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Accountid, + &i.Name, + &i.Isarchive, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getUserStepsByID = `-- name: GetUserStepsByID :many +SELECT ID,AmoID,PipelineID,AccountID,Name,Color +FROM steps +WHERE AccountID = $1 AND Deleted = false +` + +type GetUserStepsByIDRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Pipelineid int32 `db:"pipelineid" json:"pipelineid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Color string `db:"color" json:"color"` +} + +func (q *Queries) GetUserStepsByID(ctx context.Context, accountid int32) ([]GetUserStepsByIDRow, error) { + rows, err := q.db.QueryContext(ctx, getUserStepsByID, accountid) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetUserStepsByIDRow + for rows.Next() { + var i GetUserStepsByIDRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Pipelineid, + &i.Accountid, + &i.Name, + &i.Color, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getUserTagsByID = `-- name: GetUserTagsByID :many +SELECT ID,AmoID,AccountID,Name,Entity,Color +FROM tags +WHERE AccountID = $1 AND Deleted = false +` + +type GetUserTagsByIDRow struct { + ID int64 `db:"id" json:"id"` + Amoid int32 `db:"amoid" json:"amoid"` + Accountid int32 `db:"accountid" json:"accountid"` + Name string `db:"name" json:"name"` + Entity interface{} `db:"entity" json:"entity"` + Color string `db:"color" json:"color"` +} + +func (q *Queries) GetUserTagsByID(ctx context.Context, accountid int32) ([]GetUserTagsByIDRow, error) { + rows, err := q.db.QueryContext(ctx, getUserTagsByID, accountid) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetUserTagsByIDRow + for rows.Next() { + var i GetUserTagsByIDRow + if err := rows.Scan( + &i.ID, + &i.Amoid, + &i.Accountid, + &i.Name, + &i.Entity, + &i.Color, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getUserUsersByID = `-- name: GetUserUsersByID :many +SELECT ID,AccountID,AmoID,Name,Email,Role,"Group",Subdomain,AmoUserID,Country +FROM users +WHERE AmoUserID = $1 AND Deleted = false +` + +type GetUserUsersByIDRow struct { + ID int64 `db:"id" json:"id"` + Accountid string `db:"accountid" json:"accountid"` + Amoid int32 `db:"amoid" json:"amoid"` + Name string `db:"name" json:"name"` + Email string `db:"email" json:"email"` + Role int32 `db:"role" json:"role"` + Group int32 `db:"Group" json:"Group"` + Subdomain string `db:"subdomain" json:"subdomain"` + Amouserid int32 `db:"amouserid" json:"amouserid"` + Country string `db:"country" json:"country"` +} + +func (q *Queries) GetUserUsersByID(ctx context.Context, amouserid int32) ([]GetUserUsersByIDRow, error) { + rows, err := q.db.QueryContext(ctx, getUserUsersByID, amouserid) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetUserUsersByIDRow + for rows.Next() { + var i GetUserUsersByIDRow + if err := rows.Scan( + &i.ID, + &i.Accountid, + &i.Amoid, + &i.Name, + &i.Email, + &i.Role, + &i.Group, + &i.Subdomain, + &i.Amouserid, + &i.Country, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getUsersWithPagination = `-- name: GetUsersWithPagination :many WITH user_data AS ( SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false From 10399fe4c21f9cedaa9e605c970b65189f1c1a7a Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 7 May 2024 20:57:56 +0300 Subject: [PATCH 184/187] add queries for getting amo user entitys --- repository/amo/amo.go | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 1dc6a0d..63d05fa 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -193,6 +193,33 @@ func (r *AmoRepository) DeleteUsers(ctx context.Context, ids []int64) error { 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 +} + // методы webhook func (r *AmoRepository) WebhookCreate(ctx context.Context, tokens model.Token) error { @@ -362,6 +389,28 @@ func (r *AmoRepository) DeletePipelines(ctx context.Context, ids []int64) error 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 +} + // методы steps func (r *AmoRepository) GetStepsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListStepsResp, error) { @@ -445,6 +494,29 @@ func (r *AmoRepository) DeleteSteps(ctx context.Context, ids []int64) error { 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 +} + // методы tags func (r *AmoRepository) GetTagsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListTagsResp, error) { @@ -538,6 +610,32 @@ func (r *AmoRepository) DeleteTags(ctx context.Context, ids []int64) error { 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 +} + // методы fields func (r *AmoRepository) GetFieldsWithPagination(ctx context.Context, req *model.PaginationReq, accountID string) (*model.UserListFieldsResp, error) { From 0c30ce306d4fa450d11e76d8922e29234c544f8d Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 10 May 2024 11:58:09 +0300 Subject: [PATCH 185/187] add new query foe fetch field by amoid --- dal/db_query/queries.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index c65c60c..6325103 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1066,3 +1066,6 @@ WHERE AccountID = $1 AND Deleted = false; SELECT ID,AccountID,AmoID,Name,Email,Role,"Group",Subdomain,AmoUserID,Country FROM users WHERE AmoUserID = $1 AND Deleted = false; + +--name: GetFieldByAmoID :one +SELECT * FROM fields WHERE AmoID = $1 AND Deleted = false; From 6ff56437fd289204ba6d3670434b599841bfc706 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 10 May 2024 12:02:17 +0300 Subject: [PATCH 186/187] update sqlc gen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 6325103..58f8e68 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1067,5 +1067,5 @@ SELECT ID,AccountID,AmoID,Name,Email,Role,"Group",Subdomain,AmoUserID,Country FROM users WHERE AmoUserID = $1 AND Deleted = false; ---name: GetFieldByAmoID :one +-- name: GetFieldByAmoID :one SELECT * FROM fields WHERE AmoID = $1 AND Deleted = false; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index fd12b82..b781462 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1532,6 +1532,27 @@ func (q *Queries) GetExpiredPrivilege(ctx context.Context, privilegeid sql.NullS return items, nil } +const getFieldByAmoID = `-- name: GetFieldByAmoID :one +SELECT id, amoid, code, accountid, name, entity, type, deleted, createdat FROM fields WHERE AmoID = $1 AND Deleted = false +` + +func (q *Queries) GetFieldByAmoID(ctx context.Context, amoid int32) (Field, error) { + row := q.db.QueryRowContext(ctx, getFieldByAmoID, amoid) + var i Field + err := row.Scan( + &i.ID, + &i.Amoid, + &i.Code, + &i.Accountid, + &i.Name, + &i.Entity, + &i.Type, + &i.Deleted, + &i.Createdat, + ) + return i, err +} + const getFieldsWithPagination = `-- name: GetFieldsWithPagination :many SELECT f.id, f.amoid, f.code, f.accountid, f.name, f.entity, f.type, f.deleted, f.createdat, COUNT(*) OVER() as total_count FROM fields f JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON f.AccountID = u.AmoID From 72cb7d7da6e9b0523105b3d92660c469190ffdda Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 10 May 2024 12:09:20 +0300 Subject: [PATCH 187/187] add new amo repo for getting field by ID --- repository/amo/amo.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 63d05fa..f8a262a 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -774,6 +774,31 @@ func (r *AmoRepository) DeleteFields(ctx context.Context, ids []int64) error { 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 +} + // методы rules func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error {