From ea2ce5a483786d25e5267414c165a6106a8a0ef5 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 5 Jun 2024 15:27:34 +0300 Subject: [PATCH 01/21] update query now return domain --- dal/db_query/queries.sql | 2 +- pj_errors/pg_error.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 32117ec..4dec4b3 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -993,7 +993,7 @@ SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, 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 +,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,u.subdomain 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/pj_errors/pg_error.go b/pj_errors/pg_error.go index 5ee2ba5..a91a3e3 100644 --- a/pj_errors/pg_error.go +++ b/pj_errors/pg_error.go @@ -2,4 +2,4 @@ package pj_errors import "errors" -var ErrNotFound = errors.New("not found") +var ErrNotFound = errors.New("not found") \ No newline at end of file From bb62ea3da02920793f09504a86a3886e7df800dd Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 4 Jun 2024 19:14:47 +0300 Subject: [PATCH 02/21] update sqlc gen --- dal/sqlcgen/queries.sql.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index bbe4a8b..bf473b0 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2810,7 +2810,7 @@ SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, 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 +,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,u.subdomain FROM answer a INNER JOIN quiz q ON a.quiz_id = q.id LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID @@ -2839,6 +2839,7 @@ type GettingAmoUsersTrueResultsRow struct { Stepid int32 `db:"stepid" json:"stepid"` Pipelineid int32 `db:"pipelineid" json:"pipelineid"` PerformerName string `db:"performer_name" json:"performer_name"` + Subdomain string `db:"subdomain" json:"subdomain"` } func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoUsersTrueResultsRow, error) { @@ -2865,6 +2866,7 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU &i.Stepid, &i.Pipelineid, &i.PerformerName, + &i.Subdomain, ); err != nil { return nil, err } From 79b803bc969462017a8e85a4a64965b83a33ca3e Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 5 Jun 2024 19:27:15 +0300 Subject: [PATCH 03/21] fix now in pagination query user dont be deleted --- dal/db_query/queries.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 32117ec..a1e531a 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -736,25 +736,25 @@ ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3; -- name: GetTagsWithPagination :many SELECT t.*, COUNT(*) OVER() as total_count -FROM tags t JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1) u ON t.AccountID = u.AmoID +FROM tags t JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false) 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 users.AccountID = $1) u ON s.AccountID = u.AmoID +FROM steps s JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false) u ON s.AccountID = u.AmoID WHERE s.Deleted = false AND PipelineID = $4 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 users.AccountID = $1) u ON p.AccountID = u.AmoID +FROM pipelines p JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false) 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 users.AccountID = $1) u ON f.AccountID = u.AmoID +FROM fields f JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false) u ON f.AccountID = u.AmoID WHERE f.Deleted = false ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3; From c06e955c6ad83c994969db32147dbef89465e164 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 4 Jun 2024 19:16:00 +0300 Subject: [PATCH 04/21] update sqlc gen --- dal/sqlcgen/queries.sql.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index bbe4a8b..915ed02 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1630,7 +1630,7 @@ func (q *Queries) GetFieldByAmoID(ctx context.Context, amoid int32) (Field, erro 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 fields f JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false) u ON f.AccountID = u.AmoID WHERE f.Deleted = false ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3 ` @@ -1746,7 +1746,7 @@ func (q *Queries) GetListStartQuiz(ctx context.Context, accountid string) ([]int const getPipelinesWithPagination = `-- name: GetPipelinesWithPagination :many 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 +FROM pipelines p JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false) u ON p.AccountID = u.AmoID WHERE p.Deleted = false ORDER BY p.ID OFFSET ($2 - 1) * $3 LIMIT $3 ` @@ -2363,7 +2363,7 @@ func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAn const getStepsWithPagination = `-- name: GetStepsWithPagination :many 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 +FROM steps s JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false) u ON s.AccountID = u.AmoID WHERE s.Deleted = false AND PipelineID = $4 ORDER BY s.ID OFFSET ($2 - 1) * $3 LIMIT $3 ` @@ -2427,7 +2427,7 @@ func (q *Queries) GetStepsWithPagination(ctx context.Context, arg GetStepsWithPa const getTagsWithPagination = `-- name: GetTagsWithPagination :many 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 +FROM tags t JOIN (SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false) u ON t.AccountID = u.AmoID WHERE t.Deleted = false ORDER BY t.ID OFFSET ($2 - 1) * $3 LIMIT $3 ` From 518b0708272467ffaf22c78589271bd445ffe10f Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 4 Jun 2024 19:19:22 +0300 Subject: [PATCH 05/21] fix update rules where user not deleted --- 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 a1e531a..3b51301 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -944,7 +944,7 @@ 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; +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $2 AND users.Deleted = false) AND QuizID = $3 AND Deleted = false; -- name: UpdateUsers :exec UPDATE users AS u diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 915ed02..9164665 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3464,7 +3464,7 @@ func (q *Queries) TemplateCopy(ctx context.Context, arg TemplateCopyParams) (int 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 +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $2 AND users.Deleted = false) AND QuizID = $3 AND Deleted = false ` type UpdateFieldRulesParams struct { From 7fac4248132823379e51230a2fdaf3a7b4486bd0 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 5 Jun 2024 15:33:24 +0300 Subject: [PATCH 06/21] update amo repo method, now return domain --- model/amo.go | 1 + repository/amo/amo.go | 1 + 2 files changed, 2 insertions(+) diff --git a/model/amo.go b/model/amo.go index 5d99fdd..34c4bb2 100644 --- a/model/amo.go +++ b/model/amo.go @@ -274,4 +274,5 @@ type AmoUsersTrueResults struct { StepID int32 PipelineID int32 PerformerName string + SubDomain string } diff --git a/repository/amo/amo.go b/repository/amo/amo.go index e87cbb1..6e229e7 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -1039,6 +1039,7 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model StepID: row.Stepid, PipelineID: row.Pipelineid, PerformerName: row.PerformerName, + SubDomain: row.Subdomain, } results = append(results, result) From 7b3d9e21ed2bb27c60421346fc5fd1b55106ddc3 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 5 Jun 2024 20:42:03 +0300 Subject: [PATCH 07/21] fix --- 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 3b51301..bbf255c 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1004,7 +1004,8 @@ WHERE a.result = true AND s.id IS NULL AND a.deleted = false AND r.deleted = false - AND q.deleted = false; + AND q.deleted = false + AND u.deleted = false; -- name: SettingDealAmoStatus :exec INSERT INTO amoCRMStatuses (AccountID, DealID, AnswerID, Status) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 9164665..996b525 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2822,6 +2822,7 @@ WHERE a.result = true AND a.deleted = false AND r.deleted = false AND q.deleted = false + AND u.deleted = false ` type GettingAmoUsersTrueResultsRow struct { From 1a7fcd8df0541862c0a2c72aef27d468155447b2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 15:59:56 +0300 Subject: [PATCH 08/21] add new type for add tags to rule --- dal/schema/000013_init.down.sql | 2 ++ dal/schema/000013_init.up.sql | 2 ++ model/amo.go | 9 +++++++++ sqlc.yaml | 2 ++ 4 files changed, 15 insertions(+) create mode 100644 dal/schema/000013_init.down.sql create mode 100644 dal/schema/000013_init.up.sql diff --git a/dal/schema/000013_init.down.sql b/dal/schema/000013_init.down.sql new file mode 100644 index 0000000..6e8b352 --- /dev/null +++ b/dal/schema/000013_init.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE rules +DROP COLUMN IF EXISTS TagsToAdd; \ No newline at end of file diff --git a/dal/schema/000013_init.up.sql b/dal/schema/000013_init.up.sql new file mode 100644 index 0000000..c748bd9 --- /dev/null +++ b/dal/schema/000013_init.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE rules +ADD COLUMN TagsToAdd JSONB DEFAULT '{}'; \ No newline at end of file diff --git a/model/amo.go b/model/amo.go index 34c4bb2..75e74ec 100644 --- a/model/amo.go +++ b/model/amo.go @@ -144,12 +144,21 @@ type Rule struct { //Utms []int32 `json:"UTMs"` /* - правила заполнения полей сущностей в амо*/ Fieldsrule Fieldsrule `json:"FieldsRule"` + // теги добавляемые к сделке + TagsToAdd TagsToAdd `json:"TagsToAdd"` /* - флаг мягкого удаления*/ Deleted bool `json:"Deleted"` /* - таймштамп создания воронки в нашей системе*/ Createdat int64 `json:"CreatedAt"` } +type TagsToAdd struct { + Lead []int64 `json:"Lead"` + Contact []int64 `json:"Contact"` + Company []int64 `json:"Company"` + Customer []int64 `json:"Customer"` +} + type Fieldsrule struct { Lead []FieldRule `json:"Lead"` Contact ContactRules `json:"Contact"` diff --git a/sqlc.yaml b/sqlc.yaml index 665bef1..9658197 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -28,6 +28,8 @@ packages: - "./dal/schema/000011_init.down.sql" - "./dal/schema/000012_init.up.sql" - "./dal/schema/000012_init.down.sql" + - "./dal/schema/000013_init.up.sql" + - "./dal/schema/000013_init.down.sql" engine: "postgresql" emit_json_tags: true emit_db_tags: true From a5a4d9bfc694febd879d61180e729c0fc33c3434 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 16:07:25 +0300 Subject: [PATCH 09/21] update rule queries --- dal/db_query/queries.sql | 10 +++++----- model/amoReq.go | 1 + repository/amo/amo.go | 4 ++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 11d8b03..f2eaf35 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -928,15 +928,15 @@ SELECT * FROM tokens WHERE accountID = $1; SELECT * FROM rules WHERE QuizID = $1 AND Deleted = false; -- name: SetQuizSettings :one -INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, FieldsRule) +INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, FieldsRule,TagsToAdd) SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, - $4 AS StepID,$5 AS FieldsRule FROM users u WHERE u.AccountID = $6 AND u.Deleted = false + $4 AS StepID,$5 AS FieldsRule,$6 AS TagsToAdd FROM users u WHERE u.AccountID = $7 AND u.Deleted = false RETURNING id; -- name: ChangeQuizSettings :one UPDATE rules -SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4 -WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $5 AND users.Deleted = false) AND QuizID = $6 AND Deleted = false +SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4, TagsToAdd=$5 +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6 AND users.Deleted = false) AND QuizID = $7 AND Deleted = false RETURNING id; -- name: GetQuestionListByIDs :many @@ -993,7 +993,7 @@ SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, 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,u.subdomain +,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name,u.subdomain 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/model/amoReq.go b/model/amoReq.go index 7338707..10a724c 100644 --- a/model/amoReq.go +++ b/model/amoReq.go @@ -18,6 +18,7 @@ type RulesReq struct { StepID int32 // айдишник этапа //Utms []int32 // список UTM для этого опроса Fieldsrule Fieldsrule // правила заполнения полей сущностей в амо + TagsToAdd TagsToAdd } //type SaveUserListUTMReq struct { diff --git a/repository/amo/amo.go b/repository/amo/amo.go index 6e229e7..b78bc05 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -807,6 +807,10 @@ func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.R if err != nil { return err } + jsonTagsToAdd, err := json.Marshal(request.TagsToAdd) + if err != nil { + return err + } _, err = r.queries.ChangeQuizSettings(ctx, sqlcgen.ChangeQuizSettingsParams{ Performerid: request.PerformerID, Pipelineid: request.PipelineID, From 41ba9e34e32c0c62274c931ae7dc1f40f5e3e82f Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 5 Jun 2024 22:05:32 +0300 Subject: [PATCH 10/21] update sqlc gen --- dal/sqlcgen/models.go | 20 ++++++----- dal/sqlcgen/queries.sql.go | 74 +++++++++++++++++++++----------------- 2 files changed, 52 insertions(+), 42 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 4844126..45ff6ec 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -10,6 +10,7 @@ import ( "time" "github.com/google/uuid" + "github.com/sqlc-dev/pqtype" ) type Account struct { @@ -130,15 +131,16 @@ type Quiz struct { } 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"` - Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` + 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"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + Tagstoadd pqtype.NullRawMessage `db:"tagstoadd" json:"tagstoadd"` } type Step struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index c743bbd..b99063f 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -13,6 +13,7 @@ import ( "github.com/google/uuid" "github.com/lib/pq" + "github.com/sqlc-dev/pqtype" ) const accountPagination = `-- name: AccountPagination :many @@ -116,18 +117,19 @@ func (q *Queries) ArchiveQuiz(ctx context.Context, arg ArchiveQuizParams) error const changeQuizSettings = `-- name: ChangeQuizSettings :one UPDATE rules -SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4 -WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $5 AND users.Deleted = false) AND QuizID = $6 AND Deleted = false +SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4, TagsToAdd=$5 +WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6 AND users.Deleted = false) AND QuizID = $7 AND Deleted = false RETURNING id ` type ChangeQuizSettingsParams struct { - Performerid int32 `db:"performerid" json:"performerid"` - Pipelineid int32 `db:"pipelineid" json:"pipelineid"` - Stepid int32 `db:"stepid" json:"stepid"` - Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` - Accountid string `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"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Tagstoadd pqtype.NullRawMessage `db:"tagstoadd" json:"tagstoadd"` + Accountid string `db:"accountid" json:"accountid"` + Quizid int32 `db:"quizid" json:"quizid"` } func (q *Queries) ChangeQuizSettings(ctx context.Context, arg ChangeQuizSettingsParams) (int64, error) { @@ -136,6 +138,7 @@ func (q *Queries) ChangeQuizSettings(ctx context.Context, arg ChangeQuizSettings arg.Pipelineid, arg.Stepid, arg.Fieldsrule, + arg.Tagstoadd, arg.Accountid, arg.Quizid, ) @@ -2279,7 +2282,7 @@ func (q *Queries) GetQuizHistory(ctx context.Context, arg GetQuizHistoryParams) } const getQuizRule = `-- name: GetQuizRule :one -SELECT id, accountid, quizid, performerid, pipelineid, stepid, fieldsrule, deleted, createdat FROM rules WHERE QuizID = $1 AND Deleted = false +SELECT id, accountid, quizid, performerid, pipelineid, stepid, fieldsrule, deleted, createdat, tagstoadd FROM rules WHERE QuizID = $1 AND Deleted = false ` func (q *Queries) GetQuizRule(ctx context.Context, quizid int32) (Rule, error) { @@ -2295,6 +2298,7 @@ func (q *Queries) GetQuizRule(ctx context.Context, quizid int32) (Rule, error) { &i.Fieldsrule, &i.Deleted, &i.Createdat, + &i.Tagstoadd, ) return i, err } @@ -2810,7 +2814,7 @@ SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, 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,u.subdomain +,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name,u.subdomain FROM answer a INNER JOIN quiz q ON a.quiz_id = q.id LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID @@ -2826,21 +2830,22 @@ 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"` - Utm json.RawMessage `db:"utm" json:"utm"` - Accesstoken string `db:"accesstoken" json:"accesstoken"` - Accountid int32 `db:"accountid" json:"accountid"` - 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"` - Subdomain string `db:"subdomain" json:"subdomain"` + 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"` + Utm json.RawMessage `db:"utm" json:"utm"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` + Accountid int32 `db:"accountid" json:"accountid"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Tagstoadd pqtype.NullRawMessage `db:"tagstoadd" json:"tagstoadd"` + 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"` + Subdomain string `db:"subdomain" json:"subdomain"` } func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoUsersTrueResultsRow, error) { @@ -2863,6 +2868,7 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU &i.Accesstoken, &i.Accountid, &i.Fieldsrule, + &i.Tagstoadd, &i.Performerid, &i.Stepid, &i.Pipelineid, @@ -3359,19 +3365,20 @@ func (q *Queries) QuizCopyQid(ctx context.Context, arg QuizCopyQidParams) (QuizC } const setQuizSettings = `-- name: SetQuizSettings :one -INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, FieldsRule) +INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, FieldsRule,TagsToAdd) SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, - $4 AS StepID,$5 AS FieldsRule FROM users u WHERE u.AccountID = $6 AND u.Deleted = false + $4 AS StepID,$5 AS FieldsRule,$6 AS TagsToAdd FROM users u WHERE u.AccountID = $7 AND u.Deleted = false RETURNING id ` 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"` - Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` - Accountid string `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"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Tagstoadd pqtype.NullRawMessage `db:"tagstoadd" json:"tagstoadd"` + Accountid string `db:"accountid" json:"accountid"` } func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams) (int64, error) { @@ -3381,6 +3388,7 @@ func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams arg.Pipelineid, arg.Stepid, arg.Fieldsrule, + arg.Tagstoadd, arg.Accountid, ) var id int64 From cb1e8abc7933dc5725930faa242d9dcaede27364 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 5 Jun 2024 22:07:15 +0300 Subject: [PATCH 11/21] update sqlc gen --- dal/schema/000013_init.up.sql | 2 +- dal/sqlcgen/models.go | 21 ++++++------ dal/sqlcgen/queries.sql.go | 61 +++++++++++++++++------------------ 3 files changed, 41 insertions(+), 43 deletions(-) diff --git a/dal/schema/000013_init.up.sql b/dal/schema/000013_init.up.sql index c748bd9..72aeba6 100644 --- a/dal/schema/000013_init.up.sql +++ b/dal/schema/000013_init.up.sql @@ -1,2 +1,2 @@ ALTER TABLE rules -ADD COLUMN TagsToAdd JSONB DEFAULT '{}'; \ No newline at end of file +ADD COLUMN TagsToAdd JSONB NOT NULL DEFAULT '{}'; diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 45ff6ec..659e943 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 { @@ -131,16 +130,16 @@ type Quiz struct { } 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"` - Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` - Tagstoadd pqtype.NullRawMessage `db:"tagstoadd" json:"tagstoadd"` + 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"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` + Tagstoadd json.RawMessage `db:"tagstoadd" json:"tagstoadd"` } type Step struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index b99063f..6404508 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 @@ -123,13 +122,13 @@ RETURNING id ` type ChangeQuizSettingsParams struct { - Performerid int32 `db:"performerid" json:"performerid"` - Pipelineid int32 `db:"pipelineid" json:"pipelineid"` - Stepid int32 `db:"stepid" json:"stepid"` - Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` - Tagstoadd pqtype.NullRawMessage `db:"tagstoadd" json:"tagstoadd"` - Accountid string `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"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Tagstoadd json.RawMessage `db:"tagstoadd" json:"tagstoadd"` + Accountid string `db:"accountid" json:"accountid"` + Quizid int32 `db:"quizid" json:"quizid"` } func (q *Queries) ChangeQuizSettings(ctx context.Context, arg ChangeQuizSettingsParams) (int64, error) { @@ -2830,22 +2829,22 @@ 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"` - Utm json.RawMessage `db:"utm" json:"utm"` - Accesstoken string `db:"accesstoken" json:"accesstoken"` - Accountid int32 `db:"accountid" json:"accountid"` - Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` - Tagstoadd pqtype.NullRawMessage `db:"tagstoadd" json:"tagstoadd"` - 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"` - Subdomain string `db:"subdomain" json:"subdomain"` + 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"` + Utm json.RawMessage `db:"utm" json:"utm"` + Accesstoken string `db:"accesstoken" json:"accesstoken"` + Accountid int32 `db:"accountid" json:"accountid"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Tagstoadd json.RawMessage `db:"tagstoadd" json:"tagstoadd"` + 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"` + Subdomain string `db:"subdomain" json:"subdomain"` } func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoUsersTrueResultsRow, error) { @@ -3372,13 +3371,13 @@ SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, ` 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"` - Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` - Tagstoadd pqtype.NullRawMessage `db:"tagstoadd" json:"tagstoadd"` - Accountid string `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"` + Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` + Tagstoadd json.RawMessage `db:"tagstoadd" json:"tagstoadd"` + Accountid string `db:"accountid" json:"accountid"` } func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams) (int64, error) { From 6395b9fa3a2ba060986e0858bdcd27abff2d536d Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 16:20:31 +0300 Subject: [PATCH 12/21] add new type tagsToAdd to rules repo methods --- model/amo.go | 1 + repository/amo/amo.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/model/amo.go b/model/amo.go index 75e74ec..4a90abd 100644 --- a/model/amo.go +++ b/model/amo.go @@ -279,6 +279,7 @@ type AmoUsersTrueResults struct { AmoAccountID int32 UTMs UTMSavingMap FieldsRule Fieldsrule + TagsToAdd TagsToAdd PerformerID int32 StepID int32 PipelineID int32 diff --git a/repository/amo/amo.go b/repository/amo/amo.go index b78bc05..f63b78e 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -817,6 +817,7 @@ func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.R Stepid: request.StepID, //Utms: request.Utms, Fieldsrule: jsonFieldRule, + Tagstoadd: jsonTagsToAdd, Accountid: accountID, Quizid: int32(quizID), }) @@ -833,12 +834,17 @@ func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.Rule if err != nil { return err } + jsonTagsToAdd, err := json.Marshal(request.TagsToAdd) + if err != nil { + return err + } _, err = r.queries.SetQuizSettings(ctx, sqlcgen.SetQuizSettingsParams{ Performerid: request.PerformerID, Pipelineid: request.PipelineID, Stepid: request.StepID, //Utms: request.Utms, Fieldsrule: jsonFieldRule, + Tagstoadd: jsonTagsToAdd, Accountid: accountID, Quizid: int32(quizID), }) @@ -862,6 +868,12 @@ func (r *AmoRepository) GettingQuizRules(ctx context.Context, quizID int) (*mode return nil, err } + var tagsToAdd model.TagsToAdd + err = json.Unmarshal(row.Tagstoadd, &tagsToAdd) + if err != nil { + return nil, err + } + return &model.Rule{ ID: row.ID, Accountid: row.Accountid, @@ -871,6 +883,7 @@ func (r *AmoRepository) GettingQuizRules(ctx context.Context, quizID int) (*mode Stepid: row.Stepid, //Utms: row.Utms, Fieldsrule: fieldsRule, + TagsToAdd: tagsToAdd, }, nil } @@ -1028,6 +1041,12 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model return nil, err } + var tagsToAdd model.TagsToAdd + err = json.Unmarshal(row.Tagstoadd, &tagsToAdd) + if err != nil { + return nil, err + } + result := model.AmoUsersTrueResults{ QuizID: row.QuizID, AnswerID: row.ID, @@ -1039,6 +1058,7 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model AmoAccountID: row.Accountid, UTMs: utm, FieldsRule: fieldsRule, + TagsToAdd: tagsToAdd, PerformerID: row.Performerid, StepID: row.Stepid, PipelineID: row.Pipelineid, From e62c51eba65fc138bb09851cfab410b91439ad80 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 17:12:05 +0300 Subject: [PATCH 13/21] add to amoDal account repo, and add value to query --- dal/dal.go | 7 +++++++ dal/db_query/queries.sql | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/dal/dal.go b/dal/dal.go index 9dc4d46..4d0c38d 100644 --- a/dal/dal.go +++ b/dal/dal.go @@ -116,6 +116,7 @@ type AmoDal struct { QuestionRepo *question.QuestionRepository AnswerRepo *answer.AnswerRepository QuizRepo *quiz.QuizRepository + AccountRepo *account.AccountRepository } func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { @@ -153,6 +154,11 @@ func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { Pool: pool, }) + accountRepo := account.NewAccountRepository(account.Deps{ + Queries: queries, + Pool: pool, + }) + return &AmoDal{ conn: pool, queries: queries, @@ -160,6 +166,7 @@ func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { QuestionRepo: questionRepo, AnswerRepo: answerRepo, QuizRepo: quizRepo, + AccountRepo: accountRepo, }, nil } diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index f2eaf35..ec93940 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -993,7 +993,7 @@ SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, FROM answer a2 WHERE a2.start = true AND a2.session = a.session LIMIT 1) AS utm -,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name,u.subdomain +,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name,u.subdomain,u.accountid FROM answer a INNER JOIN quiz q ON a.quiz_id = q.id LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID From 588d878d7c7a6cea70384ab19a0a63bdec158ec1 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 17:13:10 +0300 Subject: [PATCH 14/21] update sqlc gen --- dal/sqlcgen/queries.sql.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 6404508..b932684 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -2813,7 +2813,7 @@ SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, FROM answer a2 WHERE a2.start = true AND a2.session = a.session LIMIT 1) AS utm -,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name,u.subdomain +,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name,u.subdomain,u.accountid FROM answer a INNER JOIN quiz q ON a.quiz_id = q.id LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID @@ -2845,6 +2845,7 @@ type GettingAmoUsersTrueResultsRow struct { Pipelineid int32 `db:"pipelineid" json:"pipelineid"` PerformerName string `db:"performer_name" json:"performer_name"` Subdomain string `db:"subdomain" json:"subdomain"` + Accountid_2 string `db:"accountid_2" json:"accountid_2"` } func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoUsersTrueResultsRow, error) { @@ -2873,6 +2874,7 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU &i.Pipelineid, &i.PerformerName, &i.Subdomain, + &i.Accountid_2, ); err != nil { return nil, err } From 501a3689fd9b8ce64bfa65c925bf15b12ce8245b Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 17:15:20 +0300 Subject: [PATCH 15/21] add to return quiz account id in GettingAmoUsersTrueResults --- model/amo.go | 1 + repository/amo/amo.go | 1 + 2 files changed, 2 insertions(+) diff --git a/model/amo.go b/model/amo.go index 4a90abd..31014d3 100644 --- a/model/amo.go +++ b/model/amo.go @@ -285,4 +285,5 @@ type AmoUsersTrueResults struct { PipelineID int32 PerformerName string SubDomain string + QuizAccountID string } diff --git a/repository/amo/amo.go b/repository/amo/amo.go index f63b78e..eb9a22c 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -1064,6 +1064,7 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model PipelineID: row.Pipelineid, PerformerName: row.PerformerName, SubDomain: row.Subdomain, + QuizAccountID: row.Accountid_2, } results = append(results, result) From 74ecde46ec07e14b059e4a667192e261c31f7c31 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 17:27:18 +0300 Subject: [PATCH 16/21] add verify user privileges utils --- utils/verifyPrivilege.go | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 utils/verifyPrivilege.go diff --git a/utils/verifyPrivilege.go b/utils/verifyPrivilege.go new file mode 100644 index 0000000..32b3968 --- /dev/null +++ b/utils/verifyPrivilege.go @@ -0,0 +1,43 @@ +package utils + +import ( + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" + "time" +) + +func VerifyUserPrivileges(currentPrivileges []model.ShortPrivilege) bool { + if HasQuizCntPrivilege(currentPrivileges) { + return true + } + + if HasUnlimitedPrivilege(currentPrivileges) { + return true + } + + return false +} + +func HasQuizCntPrivilege(privileges []model.ShortPrivilege) bool { + for _, privilege := range privileges { + if privilege.PrivilegeID == "quizCnt" && privilege.Amount > 0 { + return true + } + } + return false +} + +func HasUnlimitedPrivilege(privileges []model.ShortPrivilege) bool { + for _, privilege := range privileges { + if privilege.PrivilegeID == "quizUnlimTime" { + return IsPrivilegeExpired(privilege) + } + } + return false +} + +func IsPrivilegeExpired(privilege model.ShortPrivilege) bool { + expirationTime := privilege.CreatedAt.Add(time.Duration(privilege.Amount) * 24 * time.Hour) + + currentTime := time.Now() + return currentTime.Before(expirationTime) +} From 41e21f5f847f85371a1a554c4e5afad4071be60a Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 17:58:11 +0300 Subject: [PATCH 17/21] add drive url to amo entity models and queries --- dal/db_query/queries.sql | 6 +++--- dal/schema/000013_init.down.sql | 4 +++- dal/schema/000013_init.up.sql | 2 ++ model/amo.go | 3 +++ model/amoResp.go | 3 ++- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index ec93940..4315596 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -686,8 +686,8 @@ RETURNING quiz_id; -- 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); +INSERT INTO users (AccountID, AmoID, Name, Email, Role, "Group", Deleted, CreatedAt, Subdomain, AmoUserID, Country,DriveURL) +VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12); -- name: CreateWebHook :exec INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, CreatedAt) @@ -993,7 +993,7 @@ SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, FROM answer a2 WHERE a2.start = true AND a2.session = a.session LIMIT 1) AS utm -,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name,u.subdomain,u.accountid +,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name,u.subdomain,u.accountid,u.driveurl 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/000013_init.down.sql b/dal/schema/000013_init.down.sql index 6e8b352..26cb247 100644 --- a/dal/schema/000013_init.down.sql +++ b/dal/schema/000013_init.down.sql @@ -1,2 +1,4 @@ ALTER TABLE rules -DROP COLUMN IF EXISTS TagsToAdd; \ No newline at end of file +DROP COLUMN IF EXISTS TagsToAdd; +ALTER TABLE users +DROP COLUMN IF EXISTS DriveURL; \ No newline at end of file diff --git a/dal/schema/000013_init.up.sql b/dal/schema/000013_init.up.sql index 72aeba6..44d4155 100644 --- a/dal/schema/000013_init.up.sql +++ b/dal/schema/000013_init.up.sql @@ -1,2 +1,4 @@ ALTER TABLE rules ADD COLUMN TagsToAdd JSONB NOT NULL DEFAULT '{}'; +ALTER TABLE users +ADD COLUMN DriveURL text NOT NULL DEFAULT ''; diff --git a/model/amo.go b/model/amo.go index 31014d3..b5bf0ea 100644 --- a/model/amo.go +++ b/model/amo.go @@ -25,6 +25,8 @@ type User struct { Amouserid int32 `json:"AmoUserID"` /* - страна указанная в настройках амо*/ Country string `json:"Country"` + // урл объектного хранилища пользователя в амо + DriveURL string `json:"DriveURL"` } type UserGroups struct { @@ -286,4 +288,5 @@ type AmoUsersTrueResults struct { PerformerName string SubDomain string QuizAccountID string + DriveURL string } diff --git a/model/amoResp.go b/model/amoResp.go index 6ea7cc6..fd49975 100644 --- a/model/amoResp.go +++ b/model/amoResp.go @@ -21,7 +21,8 @@ type GetCurrentAccountResp struct { /* - страна указанная в настройках амо*/ Country string `json:"Country"` /* - таймштамп создания аккаунта*/ - Createdat int64 `json:"CreatedAt"` + Createdat int64 `json:"CreatedAt"` + DriveURL string `json:"DriveURL"` } //type GetListUserUTMResp struct { From 4f688b5bc85d73ab8f6fdd9da9026fa942019acc Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 17:59:19 +0300 Subject: [PATCH 18/21] update sqlc gen --- dal/sqlcgen/models.go | 1 + dal/sqlcgen/queries.sql.go | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 659e943..d6e25c0 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -186,4 +186,5 @@ type User struct { Subdomain string `db:"subdomain" json:"subdomain"` Amouserid int32 `db:"amouserid" json:"amouserid"` Country string `db:"country" json:"country"` + Driveurl string `db:"driveurl" json:"driveurl"` } diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index b932684..971ab6d 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -583,12 +583,12 @@ WITH new_users AS ( nu.createdAt FROM new_users nu ON CONFLICT (amoID) DO NOTHING - RETURNING id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country + RETURNING id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country, driveurl ) 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 + SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country, driveurl FROM inserted_users ins WHERE ins.amoID = nu.amoID ) @@ -780,8 +780,8 @@ func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (A 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) +INSERT INTO users (AccountID, AmoID, Name, Email, Role, "Group", Deleted, CreatedAt, Subdomain, AmoUserID, Country,DriveURL) +VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) ` type CreateAmoAccountParams struct { @@ -796,6 +796,7 @@ type CreateAmoAccountParams struct { Subdomain string `db:"subdomain" json:"subdomain"` Amouserid int32 `db:"amouserid" json:"amouserid"` Country string `db:"country" json:"country"` + Driveurl string `db:"driveurl" json:"driveurl"` } // amo methods: @@ -812,6 +813,7 @@ func (q *Queries) CreateAmoAccount(ctx context.Context, arg CreateAmoAccountPara arg.Subdomain, arg.Amouserid, arg.Country, + arg.Driveurl, ) return err } @@ -1492,7 +1494,7 @@ func (q *Queries) GetAllTokens(ctx context.Context) ([]Token, error) { } const getCurrentAccount = `-- name: GetCurrentAccount :one -SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country FROM users WHERE AccountID = $1 AND Deleted = false +SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country, driveurl FROM users WHERE AccountID = $1 AND Deleted = false ` func (q *Queries) GetCurrentAccount(ctx context.Context, accountid string) (User, error) { @@ -1511,6 +1513,7 @@ func (q *Queries) GetCurrentAccount(ctx context.Context, accountid string) (User &i.Subdomain, &i.Amouserid, &i.Country, + &i.Driveurl, ) return i, err } @@ -2741,7 +2744,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.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 +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, u.driveurl, COUNT(*) OVER() as total_count FROM users u JOIN user_data a ON u.AmoUserID = a.AmoID WHERE u.Deleted = false @@ -2767,6 +2770,7 @@ type GetUsersWithPaginationRow struct { Subdomain string `db:"subdomain" json:"subdomain"` Amouserid int32 `db:"amouserid" json:"amouserid"` Country string `db:"country" json:"country"` + Driveurl string `db:"driveurl" json:"driveurl"` TotalCount int64 `db:"total_count" json:"total_count"` } @@ -2792,6 +2796,7 @@ func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPa &i.Subdomain, &i.Amouserid, &i.Country, + &i.Driveurl, &i.TotalCount, ); err != nil { return nil, err @@ -2813,7 +2818,7 @@ SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, FROM answer a2 WHERE a2.start = true AND a2.session = a.session LIMIT 1) AS utm -,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name,u.subdomain,u.accountid +,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM users u WHERE u.amoid = r.performerid) AS performer_name,u.subdomain,u.accountid,u.driveurl FROM answer a INNER JOIN quiz q ON a.quiz_id = q.id LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID @@ -2846,6 +2851,7 @@ type GettingAmoUsersTrueResultsRow struct { PerformerName string `db:"performer_name" json:"performer_name"` Subdomain string `db:"subdomain" json:"subdomain"` Accountid_2 string `db:"accountid_2" json:"accountid_2"` + Driveurl string `db:"driveurl" json:"driveurl"` } func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoUsersTrueResultsRow, error) { @@ -2875,6 +2881,7 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU &i.PerformerName, &i.Subdomain, &i.Accountid_2, + &i.Driveurl, ); err != nil { return nil, err } From 7e16f4547ed52dac9f44f7d6a8493e16062d7782 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 18:03:18 +0300 Subject: [PATCH 19/21] add drive url to some methods where it need --- repository/amo/amo.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/repository/amo/amo.go b/repository/amo/amo.go index eb9a22c..76c0cb6 100644 --- a/repository/amo/amo.go +++ b/repository/amo/amo.go @@ -93,6 +93,7 @@ func (r *AmoRepository) GetCurrentAccount(ctx context.Context, accountID string) Subdomain: row.Subdomain, Amouserid: row.Amouserid, Country: row.Country, + DriveURL: row.Driveurl, } return &user, nil @@ -110,6 +111,7 @@ func (r *AmoRepository) CreateAccount(ctx context.Context, accountID string, use Subdomain: userInfo.Subdomain, Amouserid: userInfo.Amouserid, Country: userInfo.Country, + Driveurl: userInfo.DriveURL, }) if err != nil { @@ -1065,6 +1067,7 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model PerformerName: row.PerformerName, SubDomain: row.Subdomain, QuizAccountID: row.Accountid_2, + DriveURL: row.Driveurl, } results = append(results, result) From d88027bd0e26c894170e0f5a2a76cae314fc3d68 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 20:56:09 +0300 Subject: [PATCH 20/21] fix exspression --- dal/db_query/queries.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 4315596..47181e4 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1012,11 +1012,11 @@ 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; +WHERE t.AccessToken = $4 AND u.Deleted = false; -- 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); +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 AND u.Deleted = false); -- name: DeleteFields :exec UPDATE fields SET Deleted = true WHERE ID = ANY($1::bigint[]); From b161daea28418a92def602da0f96dfeaacf58bb8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 8 Jun 2024 20:58:33 +0300 Subject: [PATCH 21/21] update sqlc gen --- dal/sqlcgen/queries.sql.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 971ab6d..1d2ff0b 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3409,7 +3409,7 @@ 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 +WHERE t.AccessToken = $4 AND u.Deleted = false ` type SettingDealAmoStatusParams struct { @@ -3610,7 +3610,7 @@ func (q *Queries) UpdateUsers(ctx context.Context, dollar_1 json.RawMessage) 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) +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 AND u.Deleted = false) ` type UpdatingDealAmoStatusParams struct {