From c005af4b60b6398fd5553f944409321cce1ec496 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 20:19:56 +0300 Subject: [PATCH 01/14] add utm in answer struct and init new mig files for add column utm to db --- dal/schema/000011_init.down.sql | 2 ++ dal/schema/000011_init.up.sql | 2 ++ model/model.go | 1 + sqlc.yaml | 2 ++ 4 files changed, 7 insertions(+) create mode 100644 dal/schema/000011_init.down.sql create mode 100644 dal/schema/000011_init.up.sql diff --git a/dal/schema/000011_init.down.sql b/dal/schema/000011_init.down.sql new file mode 100644 index 0000000..c70ca6c --- /dev/null +++ b/dal/schema/000011_init.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE answer +DROP COLUMN utm; \ No newline at end of file diff --git a/dal/schema/000011_init.up.sql b/dal/schema/000011_init.up.sql new file mode 100644 index 0000000..03da6ff --- /dev/null +++ b/dal/schema/000011_init.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE answer +ADD COLUMN utm text NOT NULL DEFAULT ''; \ No newline at end of file diff --git a/model/model.go b/model/model.go index e5fcaea..82fc385 100644 --- a/model/model.go +++ b/model/model.go @@ -130,6 +130,7 @@ type Answer struct { IP string OS string Start bool + Utm string // строка с utm метками amocrm } type ResultContent struct { diff --git a/sqlc.yaml b/sqlc.yaml index f25b4b1..a2ed050 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -24,6 +24,8 @@ packages: - "./dal/schema/000009_init.down.sql" - "./dal/schema/000010_init.up.sql" - "./dal/schema/000010_init.down.sql" + - "./dal/schema/000011_init.up.sql" + - "./dal/schema/000011_init.down.sql" engine: "postgresql" emit_json_tags: true emit_db_tags: true From 4b40faa60f057b640050323c89932492556c5cb1 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 20:22:23 +0300 Subject: [PATCH 02/14] update sqlcgen --- dal/db_query/queries.sql | 5 +++-- dal/sqlcgen/models.go | 1 + dal/sqlcgen/queries.sql.go | 7 +++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index ae33bc6..3f8d445 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -305,8 +305,9 @@ INSERT INTO answer( os, browser, ip, - start -) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13); + start, + utm +) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14); -- 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 = ( diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 9070372..db6f553 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -47,6 +47,7 @@ type Answer struct { Browser string `db:"browser" json:"browser"` Ip string `db:"ip" json:"ip"` Start bool `db:"start" json:"start"` + Utm string `db:"utm" json:"utm"` } type Field struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 50df311..7ffcd76 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2920,8 +2920,9 @@ INSERT INTO answer( os, browser, ip, - start -) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13) + start, + utm +) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14) ` type InsertAnswersParams struct { @@ -2938,6 +2939,7 @@ type InsertAnswersParams struct { Browser string `db:"browser" json:"browser"` Ip string `db:"ip" json:"ip"` Start bool `db:"start" json:"start"` + Utm string `db:"utm" json:"utm"` } func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) error { @@ -2955,6 +2957,7 @@ func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) er arg.Browser, arg.Ip, arg.Start, + arg.Utm, ) return err } From e041a2a49a14ccac345fb69f220a6da3355a7ea5 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 20:51:59 +0300 Subject: [PATCH 03/14] change text utm to jsonb, and add type map for utm --- dal/schema/000011_init.up.sql | 2 +- model/model.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dal/schema/000011_init.up.sql b/dal/schema/000011_init.up.sql index 03da6ff..9ae09cf 100644 --- a/dal/schema/000011_init.up.sql +++ b/dal/schema/000011_init.up.sql @@ -1,2 +1,2 @@ ALTER TABLE answer -ADD COLUMN utm text NOT NULL DEFAULT ''; \ No newline at end of file +ADD COLUMN utm jsonb NOT NULL DEFAULT '{}'; \ No newline at end of file diff --git a/model/model.go b/model/model.go index 82fc385..7503018 100644 --- a/model/model.go +++ b/model/model.go @@ -130,7 +130,7 @@ type Answer struct { IP string OS string Start bool - Utm string // строка с utm метками amocrm + Utm UTMSavingMap } type ResultContent struct { @@ -293,3 +293,5 @@ type AnswerExport struct { New bool `json:"new"` CreatedAt time.Time `json:"created_at"` } + +type UTMSavingMap map[string]string From da0213dd9329a24bcd787eb9a199921c11b11e71 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 20:53:00 +0300 Subject: [PATCH 04/14] update sqlcgen --- dal/sqlcgen/models.go | 36 ++++++++++++++++++------------------ dal/sqlcgen/queries.sql.go | 28 ++++++++++++++-------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index db6f553..9901e83 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -30,24 +30,24 @@ type Amocrmstatus struct { } type Answer 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"` - Utm string `db:"utm" json:"utm"` + 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"` + Utm json.RawMessage `db:"utm" json:"utm"` } type Field struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 7ffcd76..cb4592f 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2926,20 +2926,20 @@ INSERT INTO answer( ` type InsertAnswersParams struct { - 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"` - Result sql.NullBool `db:"result" json:"result"` - 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"` - Utm string `db:"utm" json:"utm"` + 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"` + Result sql.NullBool `db:"result" json:"result"` + 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"` + Utm json.RawMessage `db:"utm" json:"utm"` } func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) error { From 55bdce51fff37a42f5a8e7366ae3cb47f5f74c13 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 20:58:51 +0300 Subject: [PATCH 05/14] update answer create method, add utm to it --- repository/answer/answer.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/repository/answer/answer.go b/repository/answer/answer.go index 7e9c90c..005bc4f 100644 --- a/repository/answer/answer.go +++ b/repository/answer/answer.go @@ -3,6 +3,7 @@ package answer 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" ) @@ -40,6 +41,11 @@ func (r *AnswerRepository) CreateAnswers(ctx context.Context, answers []model.An } for _, ans := range answers { + utmJSON, err := json.Marshal(ans.Utm) + if err != nil { + return nil, []error{err} + } + params := sqlcgen.InsertAnswersParams{ Content: sql.NullString{String: ans.Content, Valid: true}, QuizID: int64(quizID), @@ -54,9 +60,10 @@ func (r *AnswerRepository) CreateAnswers(ctx context.Context, answers []model.An Browser: ans.Browser, Os: ans.OS, Start: ans.Start, + Utm: utmJSON, } - err := r.queries.InsertAnswers(ctx, params) + err = r.queries.InsertAnswers(ctx, params) if err != nil { errs = append(errs, err) } else { From d9be9bbf00d53164519469559d1e89c22766ca26 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 21:11:58 +0300 Subject: [PATCH 06/14] update --- repository/answer/answer.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/repository/answer/answer.go b/repository/answer/answer.go index 005bc4f..8b8b3df 100644 --- a/repository/answer/answer.go +++ b/repository/answer/answer.go @@ -41,9 +41,12 @@ func (r *AnswerRepository) CreateAnswers(ctx context.Context, answers []model.An } for _, ans := range answers { - utmJSON, err := json.Marshal(ans.Utm) - if err != nil { - return nil, []error{err} + var utmJSON []byte + if ans.Utm != nil { + utmJSON, err = json.Marshal(ans.Utm) + if err != nil { + return nil, []error{err} + } } params := sqlcgen.InsertAnswersParams{ From 9f317e2fa67fd62accab1876a2a6377924d665e7 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 21:43:21 +0300 Subject: [PATCH 07/14] migrate del table utms, and column utms from rules and commented all queries with utms --- dal/db_query/queries.sql | 94 ++++++++++++++++----------------- dal/schema/000011_init.down.sql | 15 +++++- dal/schema/000011_init.up.sql | 5 +- 3 files changed, 65 insertions(+), 49 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 3f8d445..2f846d8 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -915,72 +915,72 @@ WHERE NOT EXISTS ( SELECT * FROM tokens WHERE accountID = $1; -- name: DeletingUTM :exec -UPDATE utms SET Deleted = true WHERE ID = ANY($1::int[]); +-- 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 and ut.QuizID = $4 -ORDER BY ut.ID OFFSET ($2 - 1) * $3 LIMIT $3; +-- 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 and ut.QuizID = $4 +-- 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(512) 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; +-- 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(512) 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; -- name: GetQuizRule :one SELECT * FROM rules WHERE QuizID = $1 AND Deleted = false; -- name: SetQuizSettings :exec -INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, UTMS, FieldsRule) +INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, 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 FieldsRule FROM users u WHERE u.AccountID = $6; -- name: ChangeQuizSettings :exec UPDATE rules -SET PerformerID = $1,PipelineID = $2,StepID = $3,UTMS = $4,FieldsRule = $5 +SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4 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 utms -WHERE - ID = ANY($1::int[]) AND Deleted = FALSE; - --- name: GetUserFieldsByID :many -SELECT ID,AmoID,Code,AccountID,Name,Entity,Type -FROM fields -WHERE AccountID = $1 AND Deleted = false; +-- SELECT ID,AmoFieldID,QuizID,AccountID,Name +-- FROM utms +-- WHERE +-- ID = ANY($1::int[]) AND Deleted = FALSE; +-- +-- -- name: GetUserFieldsByID :many +-- SELECT ID,AmoID,Code,AccountID,Name,Entity,Type +-- FROM fields +-- WHERE AccountID = $1 AND Deleted = false; -- name: UpdateUtms :exec -UPDATE utms AS u -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; +-- UPDATE utms AS u +-- 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; -- 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; +-- 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; @@ -1031,7 +1031,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 u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name +SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,t.accesstoken,r.accountid,a.utm,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 diff --git a/dal/schema/000011_init.down.sql b/dal/schema/000011_init.down.sql index c70ca6c..bcaa9ff 100644 --- a/dal/schema/000011_init.down.sql +++ b/dal/schema/000011_init.down.sql @@ -1,2 +1,15 @@ ALTER TABLE answer -DROP COLUMN utm; \ No newline at end of file +DROP COLUMN utm; + +CREATE TABLE IF NOT EXISTS utms ( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AmoFieldID INT NOT NULL DEFAULT 0, -- id field в амо + QuizID INT NOT NULL, -- id опроса + AccountID INT NOT NULL, -- id аккаунта в амо AMOID + Name VARCHAR(512) NOT NULL DEFAULT '', -- название utm + Deleted BOOLEAN NOT NULL DEFAULT FALSE, + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +ALTER TABLE rules + ADD COLUMN UTMS INTEGER[]; \ No newline at end of file diff --git a/dal/schema/000011_init.up.sql b/dal/schema/000011_init.up.sql index 9ae09cf..f268036 100644 --- a/dal/schema/000011_init.up.sql +++ b/dal/schema/000011_init.up.sql @@ -1,2 +1,5 @@ ALTER TABLE answer -ADD COLUMN utm jsonb NOT NULL DEFAULT '{}'; \ No newline at end of file +ADD COLUMN utm jsonb NOT NULL DEFAULT '{}'; +DROP TABLE IF EXISTS utms; +ALTER TABLE rules +DROP COLUMN IF EXISTS UTMS; \ No newline at end of file From b8f7fa08cb548cce41832e4927120a20e0586527 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 21:46:36 +0300 Subject: [PATCH 08/14] update sqlcgen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/models.go | 11 -- dal/sqlcgen/queries.sql.go | 383 ++++++++----------------------------- 3 files changed, 85 insertions(+), 311 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 2f846d8..6ffa8fe 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -958,7 +958,7 @@ SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, -- name: ChangeQuizSettings :exec UPDATE rules SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4 -WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6) AND QuizID = $7 AND Deleted = false; +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $5) AND QuizID = $6 AND Deleted = false; -- name: GetUtmsByID :many -- SELECT ID,AmoFieldID,QuizID,AccountID,Name diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 9901e83..4844126 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -136,7 +136,6 @@ type Rule 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"` Deleted bool `db:"deleted" json:"deleted"` Createdat sql.NullTime `db:"createdat" json:"createdat"` @@ -187,13 +186,3 @@ 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 cb4592f..b79a7a3 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -116,15 +116,14 @@ func (q *Queries) ArchiveQuiz(ctx context.Context, arg ArchiveQuizParams) error 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 +SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4 +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $5) AND QuizID = $6 AND Deleted = false ` 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"` @@ -135,7 +134,6 @@ func (q *Queries) ChangeQuizSettings(ctx context.Context, arg ChangeQuizSettings arg.Performerid, arg.Pipelineid, arg.Stepid, - pq.Array(arg.Utms), arg.Fieldsrule, arg.Accountid, arg.Quizid, @@ -975,11 +973,47 @@ func (q *Queries) DeleteUsers(ctx context.Context, dollar_1 []int64) error { } const deletingUTM = `-- name: DeletingUTM :exec -UPDATE utms SET Deleted = true WHERE ID = ANY($1::int[]) + + + +SELECT id, accountid, quizid, performerid, pipelineid, stepid, fieldsrule, deleted, createdat FROM rules WHERE QuizID = $1 AND Deleted = false ` -func (q *Queries) DeletingUTM(ctx context.Context, dollar_1 []int32) error { - _, err := q.db.ExecContext(ctx, deletingUTM, pq.Array(dollar_1)) +// UPDATE utms SET Deleted = true WHERE ID = ANY($1::int[]); +// 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 and ut.QuizID = $4 +// ORDER BY ut.ID OFFSET ($2 - 1) * $3 LIMIT $3; +// 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(512) 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; +func (q *Queries) DeletingUTM(ctx context.Context, quizid int32) error { + _, err := q.db.ExecContext(ctx, deletingUTM, quizid) return err } @@ -1925,47 +1959,6 @@ 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 ` @@ -2203,28 +2196,6 @@ 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 @@ -2427,115 +2398,6 @@ 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 and ut.QuizID = $4 -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"` - Quizid int32 `db:"quizid" json:"quizid"` -} - -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, - arg.Quizid, - ) - 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 getUserFieldsByID = `-- name: GetUserFieldsByID :many -SELECT ID,AmoID,Code,AccountID,Name,Entity,Type -FROM fields -WHERE AccountID = $1 AND Deleted = false -` - -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 getUserPipelinesByID = `-- name: GetUserPipelinesByID :many SELECT ID,AmoID,AccountID,Name,IsArchive FROM pipelines @@ -2793,35 +2655,54 @@ func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPa } const getUtmsByID = `-- name: GetUtmsByID :many -SELECT ID,AmoFieldID,QuizID,AccountID,Name -FROM utms -WHERE - ID = ANY($1::int[]) AND Deleted = FALSE + + + +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 ` -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) { +// SELECT ID,AmoFieldID,QuizID,AccountID,Name +// FROM utms +// WHERE +// +// ID = ANY($1::int[]) AND Deleted = FALSE; +// +// -- name: GetUserFieldsByID :many +// SELECT ID,AmoID,Code,AccountID,Name,Entity,Type +// FROM fields +// WHERE AccountID = $1 AND Deleted = false; +// UPDATE utms AS u +// 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; +// 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; +func (q *Queries) GetUtmsByID(ctx context.Context, dollar_1 []int32) ([]Question, error) { rows, err := q.db.QueryContext(ctx, getUtmsByID, pq.Array(dollar_1)) if err != nil { return nil, err } defer rows.Close() - var items []GetUtmsByIDRow + var items []Question for rows.Next() { - var i GetUtmsByIDRow + var i Question if err := rows.Scan( &i.ID, - &i.Amofieldid, - &i.Quizid, - &i.Accountid, - &i.Name, + &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 } @@ -2837,7 +2718,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 u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name +SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,t.accesstoken,r.accountid,a.utm,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 @@ -2860,7 +2741,7 @@ type GettingAmoUsersTrueResultsRow struct { 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"` + Utm json.RawMessage `db:"utm" json:"utm"` Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` Performerid int32 `db:"performerid" json:"performerid"` Stepid int32 `db:"stepid" json:"stepid"` @@ -2886,7 +2767,7 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU &i.Session, &i.Accesstoken, &i.Accountid, - pq.Array(&i.Utms), + &i.Utm, &i.Fieldsrule, &i.Performerid, &i.Stepid, @@ -3360,81 +3241,10 @@ 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(512) 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 setQuizSettings = `-- name: SetQuizSettings :exec -INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, UTMS, FieldsRule) +INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, 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 FieldsRule FROM users u WHERE u.AccountID = $6 ` type SetQuizSettingsParams struct { @@ -3442,7 +3252,6 @@ type SetQuizSettingsParams 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"` } @@ -3453,7 +3262,6 @@ func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams arg.Performerid, arg.Pipelineid, arg.Stepid, - pq.Array(arg.Utms), arg.Fieldsrule, arg.Accountid, ) @@ -3661,29 +3469,6 @@ func (q *Queries) UpdateUsers(ctx context.Context, dollar_1 json.RawMessage) err return err } -const updateUtms = `-- name: UpdateUtms :exec -UPDATE utms AS u -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 -` - -func (q *Queries) UpdateUtms(ctx context.Context, dollar_1 json.RawMessage) error { - _, err := q.db.ExecContext(ctx, updateUtms, dollar_1) - return err -} - -const updateUtmsFields = `-- 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 -` - -func (q *Queries) UpdateUtmsFields(ctx context.Context, dollar_1 []int32) error { - _, err := q.db.ExecContext(ctx, updateUtmsFields, pq.Array(dollar_1)) - return err -} - const updatingDealAmoStatus = `-- 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) From 83b2c9e22a486c1209458f064b04641719caf66b Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 21:50:51 +0300 Subject: [PATCH 09/14] update sqlcgen --- dal/db_query/queries.sql | 55 ------------ dal/db_query/utm.sql | 54 ++++++++++++ dal/sqlcgen/queries.sql.go | 170 ++++++++++++++----------------------- 3 files changed, 116 insertions(+), 163 deletions(-) create mode 100644 dal/db_query/utm.sql diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 6ffa8fe..9434a48 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -914,39 +914,6 @@ WHERE NOT EXISTS ( -- 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 and ut.QuizID = $4 --- 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(512) 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; - -- name: GetQuizRule :one SELECT * FROM rules WHERE QuizID = $1 AND Deleted = false; @@ -960,28 +927,6 @@ UPDATE rules SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4 WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $5) AND QuizID = $6 AND Deleted = false; --- name: GetUtmsByID :many --- SELECT ID,AmoFieldID,QuizID,AccountID,Name --- FROM utms --- WHERE --- ID = ANY($1::int[]) AND Deleted = FALSE; --- --- -- name: GetUserFieldsByID :many --- SELECT ID,AmoID,Code,AccountID,Name,Entity,Type --- FROM fields --- WHERE AccountID = $1 AND Deleted = false; - --- name: UpdateUtms :exec --- UPDATE utms AS u --- 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; - --- 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; diff --git a/dal/db_query/utm.sql b/dal/db_query/utm.sql new file mode 100644 index 0000000..ccdd252 --- /dev/null +++ b/dal/db_query/utm.sql @@ -0,0 +1,54 @@ +-- name: GetUtmsByID :many +-- SELECT ID,AmoFieldID,QuizID,AccountID,Name +-- FROM utms +-- WHERE +-- ID = ANY($1::int[]) AND Deleted = FALSE; +-- +-- -- name: GetUserFieldsByID :many +-- SELECT ID,AmoID,Code,AccountID,Name,Entity,Type +-- FROM fields +-- WHERE AccountID = $1 AND Deleted = false; + +-- name: UpdateUtms :exec +-- UPDATE utms AS u +-- 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; + +-- 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: 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 and ut.QuizID = $4 +-- 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(512) 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; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index b79a7a3..a5f9817 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -972,51 +972,6 @@ func (q *Queries) DeleteUsers(ctx context.Context, dollar_1 []int64) error { return err } -const deletingUTM = `-- name: DeletingUTM :exec - - - -SELECT id, accountid, quizid, performerid, pipelineid, stepid, fieldsrule, deleted, createdat FROM rules WHERE QuizID = $1 AND Deleted = false -` - -// UPDATE utms SET Deleted = true WHERE ID = ANY($1::int[]); -// 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 and ut.QuizID = $4 -// ORDER BY ut.ID OFFSET ($2 - 1) * $3 LIMIT $3; -// 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(512) 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; -func (q *Queries) DeletingUTM(ctx context.Context, quizid int32) error { - _, err := q.db.ExecContext(ctx, deletingUTM, quizid) - return err -} - const deviceStatistics = `-- name: DeviceStatistics :many WITH DeviceStats AS ( SELECT @@ -1959,6 +1914,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 ` @@ -2196,6 +2192,27 @@ func (q *Queries) GetQuizHistory(ctx context.Context, arg GetQuizHistoryParams) return items, nil } +const getQuizRule = `-- name: GetQuizRule :one +SELECT id, accountid, quizid, performerid, pipelineid, stepid, 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, + &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 @@ -2654,69 +2671,6 @@ func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPa return items, nil } -const getUtmsByID = `-- name: GetUtmsByID :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 -` - -// SELECT ID,AmoFieldID,QuizID,AccountID,Name -// FROM utms -// WHERE -// -// ID = ANY($1::int[]) AND Deleted = FALSE; -// -// -- name: GetUserFieldsByID :many -// SELECT ID,AmoID,Code,AccountID,Name,Entity,Type -// FROM fields -// WHERE AccountID = $1 AND Deleted = false; -// UPDATE utms AS u -// 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; -// 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; -func (q *Queries) GetUtmsByID(ctx context.Context, dollar_1 []int32) ([]Question, error) { - rows, err := q.db.QueryContext(ctx, getUtmsByID, 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 gettingAmoUsersTrueResults = `-- name: GettingAmoUsersTrueResults :many SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,t.accesstoken,r.accountid,a.utm,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 From 168a6d8fece5d8c43d446db8827a1e1bc783470a Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 21:54:53 +0300 Subject: [PATCH 10/14] commented utms methods --- dal/db_query/queries.sql | 5 + dal/db_query/utm.sql | 4 - repository/amo/amo.go | 211 ++++++++++++++++++++------------------- 3 files changed, 112 insertions(+), 108 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 9434a48..1c4eb93 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1037,3 +1037,8 @@ WHERE AmoUserID = $1 AND Deleted = false; -- name: GetFieldByAmoID :one SELECT * FROM fields WHERE AmoID = $1 AND Deleted = false; + +-- name: GetUserFieldsByID :many +SELECT ID,AmoID,Code,AccountID,Name,Entity,Type +FROM fields +WHERE AccountID = $1 AND Deleted = false; diff --git a/dal/db_query/utm.sql b/dal/db_query/utm.sql index ccdd252..7885e41 100644 --- a/dal/db_query/utm.sql +++ b/dal/db_query/utm.sql @@ -4,10 +4,6 @@ -- WHERE -- ID = ANY($1::int[]) AND Deleted = FALSE; -- --- -- name: GetUserFieldsByID :many --- SELECT ID,AmoID,Code,AccountID,Name,Entity,Type --- FROM fields --- WHERE AccountID = $1 AND Deleted = false; -- name: UpdateUtms :exec -- UPDATE utms AS u diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 06a4c51..ac923a8 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -811,10 +811,10 @@ func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.R Performerid: request.PerformerID, Pipelineid: request.PipelineID, Stepid: request.StepID, - Utms: request.Utms, - Fieldsrule: jsonFieldRule, - Accountid: accountID, - Quizid: int32(quizID), + //Utms: request.Utms, + Fieldsrule: jsonFieldRule, + Accountid: accountID, + Quizid: int32(quizID), }) if err != nil { @@ -833,10 +833,10 @@ func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.Rule Performerid: request.PerformerID, Pipelineid: request.PipelineID, Stepid: request.StepID, - Utms: request.Utms, - Fieldsrule: jsonFieldRule, - Accountid: accountID, - Quizid: int32(quizID), + //Utms: request.Utms, + Fieldsrule: jsonFieldRule, + Accountid: accountID, + Quizid: int32(quizID), }) if err != nil { @@ -865,8 +865,8 @@ func (r *AmoRepository) GettingQuizRules(ctx context.Context, quizID int) (*mode Performerid: row.Performerid, Pipelineid: row.Pipelineid, Stepid: row.Stepid, - Utms: row.Utms, - Fieldsrule: fieldsRule, + //Utms: row.Utms, + Fieldsrule: fieldsRule, }, nil } @@ -891,112 +891,115 @@ func (r *AmoRepository) UpdateFieldRules(ctx context.Context, fieldRules model.F // методы UTMs func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.ListDeleteUTMIDsReq) error { - err := r.queries.DeletingUTM(ctx, request.Utms) - if err != nil { - return err - } + //err := r.queries.DeletingUTM(ctx, request.Utms) + //if err != nil { + // return err + //} 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 { - return nil, err - } - rows, err := r.queries.SaveUTMs(ctx, sqlcgen.SaveUTMsParams{ - Accountid: accountID, - Column2: column2, - }) + //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 - var ids []int64 - - for _, row := range rows { - ids = append(ids, row.ID) - } - - return &model.ListSavedIDUTMResp{ - Ids: ids, - }, nil + return nil, nil } func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.PaginationReq, accountID string, quizID int) (*model.GetListUserUTMResp, error) { - rows, err := r.queries.GetUTMsWithPagination(ctx, sqlcgen.GetUTMsWithPaginationParams{ - Accountid: accountID, - Column2: request.Page, - Limit: request.Size, - Quizid: int32(quizID), - }) - - if err != nil { - return nil, err - } - - var count int64 - var utmS []model.UTM - - for _, row := range rows { - count = row.TotalCount - utm := model.UTM{ - ID: row.ID, - Amofieldid: row.Amofieldid, - Quizid: row.Quizid, - Accountid: row.Accountid, - Name: row.Name, - Deleted: row.Deleted, - Createdat: row.Createdat.Time.Unix(), - } - - utmS = append(utmS, utm) - } - - return &model.GetListUserUTMResp{ - Count: count, - Items: utmS, - }, nil + //rows, err := r.queries.GetUTMsWithPagination(ctx, sqlcgen.GetUTMsWithPaginationParams{ + // Accountid: accountID, + // Column2: request.Page, + // Limit: request.Size, + // Quizid: int32(quizID), + //}) + // + //if err != nil { + // return nil, err + //} + // + //var count int64 + //var utmS []model.UTM + // + //for _, row := range rows { + // count = row.TotalCount + // utm := model.UTM{ + // ID: row.ID, + // Amofieldid: row.Amofieldid, + // Quizid: row.Quizid, + // Accountid: row.Accountid, + // Name: row.Name, + // Deleted: row.Deleted, + // Createdat: row.Createdat.Time.Unix(), + // } + // + // utmS = append(utmS, utm) + //} + // + //return &model.GetListUserUTMResp{ + // Count: count, + // Items: utmS, + //}, nil + return nil, nil } func (r *AmoRepository) GetUtmsByID(ctx context.Context, ids []int32) ([]model.UTM, error) { - rows, err := r.queries.GetUtmsByID(ctx, ids) - if err != nil { - return nil, err - } + //rows, err := r.queries.GetUtmsByID(ctx, ids) + //if err != nil { + // return nil, err + //} + // + //var utmS []model.UTM + //for _, row := range rows { + // utm := model.UTM{ + // ID: row.ID, + // Amofieldid: row.Amofieldid, + // Quizid: row.Quizid, + // Accountid: row.Accountid, + // Name: row.Name, + // } + // + // utmS = append(utmS, utm) + //} - var utmS []model.UTM - for _, row := range rows { - utm := model.UTM{ - ID: row.ID, - Amofieldid: row.Amofieldid, - Quizid: row.Quizid, - Accountid: row.Accountid, - Name: row.Name, - } - - utmS = append(utmS, utm) - } - - return utmS, nil + return nil, 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 - } + //dollar1, err := json.Marshal(utms) + //if err != nil { + // return err + //} + //err = r.queries.UpdateUtms(ctx, dollar1) + // + //if err != nil { + // return err + //} return nil } func (r *AmoRepository) UpdateUtmsFields(ctx context.Context, ids []int32) error { - err := r.queries.UpdateUtmsFields(ctx, ids) - if err != nil { - return err - } + //err := r.queries.UpdateUtmsFields(ctx, ids) + //if err != nil { + // return err + //} return nil } @@ -1016,15 +1019,15 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model return nil, err } result := model.AmoUsersTrueResults{ - QuizID: row.QuizID, - AnswerID: row.ID, - Result: row.Result.Bool, - QuestionID: row.QuestionID, - Content: row.Content.String, - Session: row.Session.String, - AccessToken: row.Accesstoken, - AmoAccountID: row.Accountid, - UTMs: row.Utms, + QuizID: row.QuizID, + AnswerID: row.ID, + Result: row.Result.Bool, + QuestionID: row.QuestionID, + Content: row.Content.String, + Session: row.Session.String, + AccessToken: row.Accesstoken, + AmoAccountID: row.Accountid, + //UTMs: row.Utms, FieldsRule: fieldsRule, PerformerID: row.Performerid, StepID: row.Stepid, From b898091905751997fd353da749a3cd2ede9017b9 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 21:55:38 +0300 Subject: [PATCH 11/14] update sqlcgen --- dal/sqlcgen/queries.sql.go | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index a5f9817..6cb5f78 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2415,6 +2415,53 @@ func (q *Queries) GetTokenById(ctx context.Context, accountid string) (Token, er return i, err } +const getUserFieldsByID = `-- name: GetUserFieldsByID :many +SELECT ID,AmoID,Code,AccountID,Name,Entity,Type +FROM fields +WHERE AccountID = $1 AND Deleted = false +` + +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 getUserPipelinesByID = `-- name: GetUserPipelinesByID :many SELECT ID,AmoID,AccountID,Name,IsArchive FROM pipelines From 6bd772ee236cb01ec52e48f7ce8dafc87a2d20dd Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 17 May 2024 22:03:20 +0300 Subject: [PATCH 12/14] commented utms methods --- model/amo.go | 36 +++---- model/amoReq.go | 18 ++-- model/amoResp.go | 12 +-- repository/amo/amo.go | 232 ++++++++++++++++++++++-------------------- 4 files changed, 152 insertions(+), 146 deletions(-) diff --git a/model/amo.go b/model/amo.go index ec87d68..5d99fdd 100644 --- a/model/amo.go +++ b/model/amo.go @@ -141,7 +141,7 @@ type Rule struct { /* - айдишник этапа*/ Stepid int32 `json:"StepID"` /* - список UTM для этого опроса*/ - Utms []int32 `json:"UTMs"` + //Utms []int32 `json:"UTMs"` /* - правила заполнения полей сущностей в амо*/ Fieldsrule Fieldsrule `json:"FieldsRule"` /* - флаг мягкого удаления*/ @@ -197,22 +197,22 @@ const ( TypeContactAddress ContactQuizConfig = "address" ) -type UTM struct { - /* - айдишник в нашей системе Primary Key*/ - ID int64 `json:"ID"` - /* - айдишник кастомного поля в амо*/ - Amofieldid int32 `json:"AmoFieldID"` - /* - айдишник квиза*/ - Quizid int32 `json:"QuizID"` - /* - связь с аккаунтом в интеграции амо id амо*/ - Accountid int32 `json:"AccountID"` - /* - название тега в амо*/ - Name string `json:"Name"` - /* - флаг мягкого удаления*/ - Deleted bool `json:"Deleted"` - /* - таймштамп создания тега в нашей системе*/ - Createdat int64 `json:"CreatedAt"` -} +//type UTM struct { +// /* - айдишник в нашей системе Primary Key*/ +// ID int64 `json:"ID"` +// /* - айдишник кастомного поля в амо*/ +// Amofieldid int32 `json:"AmoFieldID"` +// /* - айдишник квиза*/ +// Quizid int32 `json:"QuizID"` +// /* - связь с аккаунтом в интеграции амо id амо*/ +// Accountid int32 `json:"AccountID"` +// /* - название тега в амо*/ +// Name string `json:"Name"` +// /* - флаг мягкого удаления*/ +// Deleted bool `json:"Deleted"` +// /* - таймштамп создания тега в нашей системе*/ +// Createdat int64 `json:"CreatedAt"` +//} type FieldType string @@ -268,7 +268,7 @@ type AmoUsersTrueResults struct { Session string AccessToken string AmoAccountID int32 - UTMs []int32 + UTMs UTMSavingMap FieldsRule Fieldsrule PerformerID int32 StepID int32 diff --git a/model/amoReq.go b/model/amoReq.go index 81f7209..7338707 100644 --- a/model/amoReq.go +++ b/model/amoReq.go @@ -13,14 +13,14 @@ type PaginationReq struct { } type RulesReq struct { - PerformerID int32 // айдишник ответственного за сделку - PipelineID int32 // айдишник воронки - StepID int32 // айдишник этапа - Utms []int32 // список UTM для этого опроса - Fieldsrule Fieldsrule // правила заполнения полей сущностей в амо + PerformerID int32 // айдишник ответственного за сделку + PipelineID int32 // айдишник воронки + StepID int32 // айдишник этапа + //Utms []int32 // список UTM для этого опроса + Fieldsrule Fieldsrule // правила заполнения полей сущностей в амо } -type SaveUserListUTMReq struct { - /* - список utm для сохранения. сохранять только те, которых в этом аккаунте ещё нет*/ - Utms []UTM `json:"utms"` -} +//type SaveUserListUTMReq struct { +// /* - список utm для сохранения. сохранять только те, которых в этом аккаунте ещё нет*/ +// Utms []UTM `json:"utms"` +//} diff --git a/model/amoResp.go b/model/amoResp.go index b23663e..6ea7cc6 100644 --- a/model/amoResp.go +++ b/model/amoResp.go @@ -24,12 +24,12 @@ type GetCurrentAccountResp struct { Createdat int64 `json:"CreatedAt"` } -type GetListUserUTMResp struct { - /* - общее количество юзеров, которые у нас закешированы для этого пользователя*/ - Count int64 `json:"count"` - /* - список юзеров, которые были закешированы нашим сервисом*/ - Items []UTM `json:"items"` -} +//type GetListUserUTMResp struct { +// /* - общее количество юзеров, которые у нас закешированы для этого пользователя*/ +// Count int64 `json:"count"` +// /* - список юзеров, которые были закешированы нашим сервисом*/ +// Items []UTM `json:"items"` +//} type ListSavedIDUTMResp struct { /* - список айдишников сохранённых меток*/ diff --git a/repository/amo/amo.go b/repository/amo/amo.go index ac923a8..39fee52 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -899,110 +899,109 @@ func (r *AmoRepository) DeletingUserUtm(ctx context.Context, request *model.List } // todo нужно ли тут ограничивать индексом что то -func (r *AmoRepository) SavingUserUtm(ctx context.Context, utms []model.UTM, accountID string) (*model.ListSavedIDUTMResp, error) { - //column2, err := json.Marshal(utms) - //if err != nil { - // return nil, err - //} - //rows, err := r.queries.SaveUTMs(ctx, sqlcgen.SaveUTMsParams{ - // Accountid: accountID, - // Column2: column2, - //}) - // - //var ids []int64 - // - //for _, row := range rows { - // ids = append(ids, row.ID) - //} - // - //return &model.ListSavedIDUTMResp{ - // Ids: ids, - //}, nil - - return nil, nil -} - -func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.PaginationReq, accountID string, quizID int) (*model.GetListUserUTMResp, error) { - //rows, err := r.queries.GetUTMsWithPagination(ctx, sqlcgen.GetUTMsWithPaginationParams{ - // Accountid: accountID, - // Column2: request.Page, - // Limit: request.Size, - // Quizid: int32(quizID), - //}) - // - //if err != nil { - // return nil, err - //} - // - //var count int64 - //var utmS []model.UTM - // - //for _, row := range rows { - // count = row.TotalCount - // utm := model.UTM{ - // ID: row.ID, - // Amofieldid: row.Amofieldid, - // Quizid: row.Quizid, - // Accountid: row.Accountid, - // Name: row.Name, - // Deleted: row.Deleted, - // Createdat: row.Createdat.Time.Unix(), - // } - // - // utmS = append(utmS, utm) - //} - // - //return &model.GetListUserUTMResp{ - // Count: count, - // Items: utmS, - //}, nil - return nil, nil -} - -func (r *AmoRepository) GetUtmsByID(ctx context.Context, ids []int32) ([]model.UTM, error) { - //rows, err := r.queries.GetUtmsByID(ctx, ids) - //if err != nil { - // return nil, err - //} - // - //var utmS []model.UTM - //for _, row := range rows { - // utm := model.UTM{ - // ID: row.ID, - // Amofieldid: row.Amofieldid, - // Quizid: row.Quizid, - // Accountid: row.Accountid, - // Name: row.Name, - // } - // - // utmS = append(utmS, utm) - //} - - return nil, nil -} - -func (r *AmoRepository) UpdateUTMs(ctx context.Context, utms []model.UTM) error { - //dollar1, err := json.Marshal(utms) - //if err != nil { - // return err - //} - //err = r.queries.UpdateUtms(ctx, dollar1) - // - //if err != nil { - // return err - //} - - return nil -} - -func (r *AmoRepository) UpdateUtmsFields(ctx context.Context, ids []int32) error { - //err := r.queries.UpdateUtmsFields(ctx, ids) - //if err != nil { - // return err - //} - - return nil -} +//func (r *AmoRepository) SavingUserUtm(ctx context.Context, utms []model.UTM, accountID string) (*model.ListSavedIDUTMResp, error) { +// column2, err := json.Marshal(utms) +// if err != nil { +// return nil, err +// } +// rows, err := r.queries.SaveUTMs(ctx, sqlcgen.SaveUTMsParams{ +// Accountid: accountID, +// Column2: column2, +// }) +// +// var ids []int64 +// +// for _, row := range rows { +// ids = append(ids, row.ID) +// } +// +// return &model.ListSavedIDUTMResp{ +// Ids: ids, +// }, nil +// +// return nil, nil +//} +// +//func (r *AmoRepository) GettingUserUtm(ctx context.Context, request *model.PaginationReq, accountID string, quizID int) (*model.GetListUserUTMResp, error) { +// rows, err := r.queries.GetUTMsWithPagination(ctx, sqlcgen.GetUTMsWithPaginationParams{ +// Accountid: accountID, +// Column2: request.Page, +// Limit: request.Size, +// Quizid: int32(quizID), +// }) +// +// if err != nil { +// return nil, err +// } +// +// var count int64 +// var utmS []model.UTM +// +// for _, row := range rows { +// count = row.TotalCount +// utm := model.UTM{ +// ID: row.ID, +// Amofieldid: row.Amofieldid, +// Quizid: row.Quizid, +// Accountid: row.Accountid, +// Name: row.Name, +// Deleted: row.Deleted, +// Createdat: row.Createdat.Time.Unix(), +// } +// +// utmS = append(utmS, utm) +// } +// +// return &model.GetListUserUTMResp{ +// Count: count, +// Items: utmS, +// }, nil +//} +// +//func (r *AmoRepository) GetUtmsByID(ctx context.Context, ids []int32) ([]model.UTM, error) { +// rows, err := r.queries.GetUtmsByID(ctx, ids) +// if err != nil { +// return nil, err +// } +// +// var utmS []model.UTM +// for _, row := range rows { +// utm := model.UTM{ +// ID: row.ID, +// Amofieldid: row.Amofieldid, +// Quizid: row.Quizid, +// Accountid: row.Accountid, +// Name: row.Name, +// } +// +// utmS = append(utmS, utm) +// } +// +// return utmS, nil +//} +// +//func (r *AmoRepository) UpdateUTMs(ctx context.Context, utms []model.UTM) error { +// dollar1, err := json.Marshal(utms) +// if err != nil { +// return err +// } +// err = r.queries.UpdateUtms(ctx, dollar1) +// +// if err != nil { +// return err +// } +// +// return nil +//} +// +//func (r *AmoRepository) UpdateUtmsFields(ctx context.Context, ids []int32) error { +// err := r.queries.UpdateUtmsFields(ctx, ids) +// if err != nil { +// return err +// } +// +// return nil +//} func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model.AmoUsersTrueResults, error) { rows, err := r.queries.GettingAmoUsersTrueResults(ctx) @@ -1018,16 +1017,23 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model if err != nil { return nil, err } + + var utm model.UTMSavingMap + err = json.Unmarshal(row.Utm, &utm) + if err != nil { + return nil, err + } + result := model.AmoUsersTrueResults{ - QuizID: row.QuizID, - AnswerID: row.ID, - Result: row.Result.Bool, - QuestionID: row.QuestionID, - Content: row.Content.String, - Session: row.Session.String, - AccessToken: row.Accesstoken, - AmoAccountID: row.Accountid, - //UTMs: row.Utms, + QuizID: row.QuizID, + AnswerID: row.ID, + Result: row.Result.Bool, + QuestionID: row.QuestionID, + Content: row.Content.String, + Session: row.Session.String, + AccessToken: row.Accesstoken, + AmoAccountID: row.Accountid, + UTMs: utm, FieldsRule: fieldsRule, PerformerID: row.Performerid, StepID: row.Stepid, From bfce6075df5fe4ab7f211b9735e983320428d81d Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 19 May 2024 15:20:21 +0300 Subject: [PATCH 13/14] update query, now utms getting from answer start where sesion = session resut answer --- dal/db_query/queries.sql | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 1c4eb93..955383c 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -976,7 +976,12 @@ 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,a.utm,r.fieldsrule,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name +SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, + (SELECT a2.utm + FROM answer a2 + WHERE a2.start = true AND a2.session = a.session + LIMIT 1) AS utm +,t.accesstoken,r.accountid,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 c74e1f39548024577cc85172b273dfd7c0b1a5e8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 19 May 2024 15:22:35 +0300 Subject: [PATCH 14/14] update sqlcgen --- dal/sqlcgen/queries.sql.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 6cb5f78..9e75f43 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2719,7 +2719,12 @@ func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPa } const gettingAmoUsersTrueResults = `-- name: GettingAmoUsersTrueResults :many -SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,t.accesstoken,r.accountid,a.utm,r.fieldsrule,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name +SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, + (SELECT a2.utm + FROM answer a2 + WHERE a2.start = true AND a2.session = a.session + LIMIT 1) AS utm +,t.accesstoken,r.accountid,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 @@ -2740,9 +2745,9 @@ type GettingAmoUsersTrueResultsRow struct { QuestionID int64 `db:"question_id" json:"question_id"` Content sql.NullString `db:"content" json:"content"` Session sql.NullString `db:"session" json:"session"` + Utm json.RawMessage `db:"utm" json:"utm"` Accesstoken string `db:"accesstoken" json:"accesstoken"` Accountid int32 `db:"accountid" json:"accountid"` - Utm json.RawMessage `db:"utm" json:"utm"` Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` Performerid int32 `db:"performerid" json:"performerid"` Stepid int32 `db:"stepid" json:"stepid"` @@ -2766,9 +2771,9 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU &i.QuestionID, &i.Content, &i.Session, + &i.Utm, &i.Accesstoken, &i.Accountid, - &i.Utm, &i.Fieldsrule, &i.Performerid, &i.Stepid,