Merge branch 'fixPagination' into 'main'

Fix pagination

See merge request backend/quiz/common!25
This commit is contained in:
Mikhail 2024-06-08 21:05:53 +00:00
commit 4b0f100655
13 changed files with 164 additions and 36 deletions

@ -116,6 +116,7 @@ type AmoDal struct {
QuestionRepo *question.QuestionRepository QuestionRepo *question.QuestionRepository
AnswerRepo *answer.AnswerRepository AnswerRepo *answer.AnswerRepository
QuizRepo *quiz.QuizRepository QuizRepo *quiz.QuizRepository
AccountRepo *account.AccountRepository
} }
func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) { func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) {
@ -153,6 +154,11 @@ func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) {
Pool: pool, Pool: pool,
}) })
accountRepo := account.NewAccountRepository(account.Deps{
Queries: queries,
Pool: pool,
})
return &AmoDal{ return &AmoDal{
conn: pool, conn: pool,
queries: queries, queries: queries,
@ -160,6 +166,7 @@ func NewAmoDal(ctx context.Context, cred string) (*AmoDal, error) {
QuestionRepo: questionRepo, QuestionRepo: questionRepo,
AnswerRepo: answerRepo, AnswerRepo: answerRepo,
QuizRepo: quizRepo, QuizRepo: quizRepo,
AccountRepo: accountRepo,
}, nil }, nil
} }

@ -686,8 +686,8 @@ RETURNING quiz_id;
-- amo methods: -- amo methods:
-- name: CreateAmoAccount :exec -- name: CreateAmoAccount :exec
INSERT INTO users (AccountID, AmoID, Name, Email, Role, "Group", Deleted, CreatedAt, Subdomain, AmoUserID, Country) 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); VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12);
-- name: CreateWebHook :exec -- name: CreateWebHook :exec
INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, CreatedAt) INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, CreatedAt)
@ -736,25 +736,25 @@ ORDER BY u.ID OFFSET ($2 - 1) * $3 LIMIT $3;
-- name: GetTagsWithPagination :many -- name: GetTagsWithPagination :many
SELECT t.*, COUNT(*) OVER() as total_count 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 WHERE t.Deleted = false
ORDER BY t.ID OFFSET ($2 - 1) * $3 LIMIT $3; ORDER BY t.ID OFFSET ($2 - 1) * $3 LIMIT $3;
-- name: GetStepsWithPagination :many -- name: GetStepsWithPagination :many
SELECT s.*, COUNT(*) OVER() as total_count 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 WHERE s.Deleted = false AND PipelineID = $4
ORDER BY s.ID OFFSET ($2 - 1) * $3 LIMIT $3; ORDER BY s.ID OFFSET ($2 - 1) * $3 LIMIT $3;
-- name: GetPipelinesWithPagination :many -- name: GetPipelinesWithPagination :many
SELECT p.*, COUNT(*) OVER() as total_count 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 WHERE p.Deleted = false
ORDER BY p.ID OFFSET ($2 - 1) * $3 LIMIT $3; ORDER BY p.ID OFFSET ($2 - 1) * $3 LIMIT $3;
-- name: GetFieldsWithPagination :many -- name: GetFieldsWithPagination :many
SELECT f.*, COUNT(*) OVER() as total_count 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 WHERE f.Deleted = false
ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3; ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3;
@ -928,15 +928,15 @@ SELECT * FROM tokens WHERE accountID = $1;
SELECT * FROM rules WHERE QuizID = $1 AND Deleted = false; SELECT * FROM rules WHERE QuizID = $1 AND Deleted = false;
-- name: SetQuizSettings :one -- 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, 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; RETURNING id;
-- name: ChangeQuizSettings :one -- name: ChangeQuizSettings :one
UPDATE rules UPDATE rules
SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4 SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4, TagsToAdd=$5
WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $5 AND users.Deleted = false) AND QuizID = $6 AND Deleted = false WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6 AND users.Deleted = false) AND QuizID = $7 AND Deleted = false
RETURNING id; RETURNING id;
-- name: GetQuestionListByIDs :many -- name: GetQuestionListByIDs :many
@ -944,7 +944,7 @@ SELECT * FROM question WHERE id = ANY($1::int[]) AND deleted = FALSE;
-- name: UpdateFieldRules :exec -- name: UpdateFieldRules :exec
UPDATE rules SET FieldsRule = $1 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 -- name: UpdateUsers :exec
UPDATE users AS u UPDATE users AS u
@ -993,7 +993,7 @@ SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,
FROM answer a2 FROM answer a2
WHERE a2.start = true AND a2.session = a.session WHERE a2.start = true AND a2.session = a.session
LIMIT 1) AS utm 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.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 FROM answer a
INNER JOIN quiz q ON a.quiz_id = q.id INNER JOIN quiz q ON a.quiz_id = q.id
LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID
@ -1004,18 +1004,19 @@ WHERE a.result = true
AND s.id IS NULL AND s.id IS NULL
AND a.deleted = false AND a.deleted = false
AND r.deleted = false AND r.deleted = false
AND q.deleted = false; AND q.deleted = false
AND u.deleted = false;
-- name: SettingDealAmoStatus :exec -- name: SettingDealAmoStatus :exec
INSERT INTO amoCRMStatuses (AccountID, DealID, AnswerID, Status) INSERT INTO amoCRMStatuses (AccountID, DealID, AnswerID, Status)
SELECT u.AmoID, $1, $2, $3 SELECT u.AmoID, $1, $2, $3
FROM tokens AS t FROM tokens AS t
JOIN users AS u ON t.AccountID = u.AccountID JOIN users AS u ON t.AccountID = u.AccountID
WHERE t.AccessToken = $4; WHERE t.AccessToken = $4 AND u.Deleted = false;
-- name: UpdatingDealAmoStatus :exec -- name: UpdatingDealAmoStatus :exec
UPDATE amoCRMStatuses SET Status = $1 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 -- name: DeleteFields :exec
UPDATE fields SET Deleted = true WHERE ID = ANY($1::bigint[]); UPDATE fields SET Deleted = true WHERE ID = ANY($1::bigint[]);

@ -0,0 +1,4 @@
ALTER TABLE rules
DROP COLUMN IF EXISTS TagsToAdd;
ALTER TABLE users
DROP COLUMN IF EXISTS DriveURL;

@ -0,0 +1,4 @@
ALTER TABLE rules
ADD COLUMN TagsToAdd JSONB NOT NULL DEFAULT '{}';
ALTER TABLE users
ADD COLUMN DriveURL text NOT NULL DEFAULT '';

@ -139,6 +139,7 @@ type Rule struct {
Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"`
Deleted bool `db:"deleted" json:"deleted"` Deleted bool `db:"deleted" json:"deleted"`
Createdat sql.NullTime `db:"createdat" json:"createdat"` Createdat sql.NullTime `db:"createdat" json:"createdat"`
Tagstoadd json.RawMessage `db:"tagstoadd" json:"tagstoadd"`
} }
type Step struct { type Step struct {
@ -185,4 +186,5 @@ type User struct {
Subdomain string `db:"subdomain" json:"subdomain"` Subdomain string `db:"subdomain" json:"subdomain"`
Amouserid int32 `db:"amouserid" json:"amouserid"` Amouserid int32 `db:"amouserid" json:"amouserid"`
Country string `db:"country" json:"country"` Country string `db:"country" json:"country"`
Driveurl string `db:"driveurl" json:"driveurl"`
} }

@ -116,8 +116,8 @@ func (q *Queries) ArchiveQuiz(ctx context.Context, arg ArchiveQuizParams) error
const changeQuizSettings = `-- name: ChangeQuizSettings :one const changeQuizSettings = `-- name: ChangeQuizSettings :one
UPDATE rules UPDATE rules
SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4 SET PerformerID = $1,PipelineID = $2,StepID = $3,FieldsRule = $4, TagsToAdd=$5
WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $5 AND users.Deleted = false) AND QuizID = $6 AND Deleted = false WHERE AccountID = (SELECT AmoID FROM users WHERE users.AccountID = $6 AND users.Deleted = false) AND QuizID = $7 AND Deleted = false
RETURNING id RETURNING id
` `
@ -126,6 +126,7 @@ type ChangeQuizSettingsParams struct {
Pipelineid int32 `db:"pipelineid" json:"pipelineid"` Pipelineid int32 `db:"pipelineid" json:"pipelineid"`
Stepid int32 `db:"stepid" json:"stepid"` Stepid int32 `db:"stepid" json:"stepid"`
Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"`
Tagstoadd json.RawMessage `db:"tagstoadd" json:"tagstoadd"`
Accountid string `db:"accountid" json:"accountid"` Accountid string `db:"accountid" json:"accountid"`
Quizid int32 `db:"quizid" json:"quizid"` Quizid int32 `db:"quizid" json:"quizid"`
} }
@ -136,6 +137,7 @@ func (q *Queries) ChangeQuizSettings(ctx context.Context, arg ChangeQuizSettings
arg.Pipelineid, arg.Pipelineid,
arg.Stepid, arg.Stepid,
arg.Fieldsrule, arg.Fieldsrule,
arg.Tagstoadd,
arg.Accountid, arg.Accountid,
arg.Quizid, arg.Quizid,
) )
@ -581,12 +583,12 @@ WITH new_users AS (
nu.createdAt nu.createdAt
FROM new_users nu FROM new_users nu
ON CONFLICT (amoID) DO NOTHING 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 SELECT nu.amoid, nu.name, nu."Group", nu.role, nu.email, nu.amouserid, nu.createdat
FROM new_users nu FROM new_users nu
WHERE NOT EXISTS ( 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 FROM inserted_users ins
WHERE ins.amoID = nu.amoID WHERE ins.amoID = nu.amoID
) )
@ -778,8 +780,8 @@ func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (A
const createAmoAccount = `-- name: CreateAmoAccount :exec const createAmoAccount = `-- name: CreateAmoAccount :exec
INSERT INTO users (AccountID, AmoID, Name, Email, Role, "Group", Deleted, CreatedAt, Subdomain, AmoUserID, Country) 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) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
` `
type CreateAmoAccountParams struct { type CreateAmoAccountParams struct {
@ -794,6 +796,7 @@ type CreateAmoAccountParams struct {
Subdomain string `db:"subdomain" json:"subdomain"` Subdomain string `db:"subdomain" json:"subdomain"`
Amouserid int32 `db:"amouserid" json:"amouserid"` Amouserid int32 `db:"amouserid" json:"amouserid"`
Country string `db:"country" json:"country"` Country string `db:"country" json:"country"`
Driveurl string `db:"driveurl" json:"driveurl"`
} }
// amo methods: // amo methods:
@ -810,6 +813,7 @@ func (q *Queries) CreateAmoAccount(ctx context.Context, arg CreateAmoAccountPara
arg.Subdomain, arg.Subdomain,
arg.Amouserid, arg.Amouserid,
arg.Country, arg.Country,
arg.Driveurl,
) )
return err return err
} }
@ -1490,7 +1494,7 @@ func (q *Queries) GetAllTokens(ctx context.Context) ([]Token, error) {
} }
const getCurrentAccount = `-- name: GetCurrentAccount :one 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) { func (q *Queries) GetCurrentAccount(ctx context.Context, accountid string) (User, error) {
@ -1509,6 +1513,7 @@ func (q *Queries) GetCurrentAccount(ctx context.Context, accountid string) (User
&i.Subdomain, &i.Subdomain,
&i.Amouserid, &i.Amouserid,
&i.Country, &i.Country,
&i.Driveurl,
) )
return i, err return i, err
} }
@ -1630,7 +1635,7 @@ func (q *Queries) GetFieldByAmoID(ctx context.Context, amoid int32) (Field, erro
const getFieldsWithPagination = `-- name: GetFieldsWithPagination :many 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 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 WHERE f.Deleted = false
ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3 ORDER BY f.ID OFFSET ($2 - 1) * $3 LIMIT $3
` `
@ -1746,7 +1751,7 @@ func (q *Queries) GetListStartQuiz(ctx context.Context, accountid string) ([]int
const getPipelinesWithPagination = `-- name: GetPipelinesWithPagination :many 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 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 WHERE p.Deleted = false
ORDER BY p.ID OFFSET ($2 - 1) * $3 LIMIT $3 ORDER BY p.ID OFFSET ($2 - 1) * $3 LIMIT $3
` `
@ -2279,7 +2284,7 @@ func (q *Queries) GetQuizHistory(ctx context.Context, arg GetQuizHistoryParams)
} }
const getQuizRule = `-- name: GetQuizRule :one 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) { func (q *Queries) GetQuizRule(ctx context.Context, quizid int32) (Rule, error) {
@ -2295,6 +2300,7 @@ func (q *Queries) GetQuizRule(ctx context.Context, quizid int32) (Rule, error) {
&i.Fieldsrule, &i.Fieldsrule,
&i.Deleted, &i.Deleted,
&i.Createdat, &i.Createdat,
&i.Tagstoadd,
) )
return i, err return i, err
} }
@ -2363,7 +2369,7 @@ func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAn
const getStepsWithPagination = `-- name: GetStepsWithPagination :many 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 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 WHERE s.Deleted = false AND PipelineID = $4
ORDER BY s.ID OFFSET ($2 - 1) * $3 LIMIT $3 ORDER BY s.ID OFFSET ($2 - 1) * $3 LIMIT $3
` `
@ -2427,7 +2433,7 @@ func (q *Queries) GetStepsWithPagination(ctx context.Context, arg GetStepsWithPa
const getTagsWithPagination = `-- name: GetTagsWithPagination :many 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 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 WHERE t.Deleted = false
ORDER BY t.ID OFFSET ($2 - 1) * $3 LIMIT $3 ORDER BY t.ID OFFSET ($2 - 1) * $3 LIMIT $3
` `
@ -2738,7 +2744,7 @@ const getUsersWithPagination = `-- name: GetUsersWithPagination :many
WITH user_data AS ( WITH user_data AS (
SELECT AmoID FROM users WHERE users.AccountID = $1 AND Deleted = false 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 FROM users u
JOIN user_data a ON u.AmoUserID = a.AmoID JOIN user_data a ON u.AmoUserID = a.AmoID
WHERE u.Deleted = false WHERE u.Deleted = false
@ -2764,6 +2770,7 @@ type GetUsersWithPaginationRow struct {
Subdomain string `db:"subdomain" json:"subdomain"` Subdomain string `db:"subdomain" json:"subdomain"`
Amouserid int32 `db:"amouserid" json:"amouserid"` Amouserid int32 `db:"amouserid" json:"amouserid"`
Country string `db:"country" json:"country"` Country string `db:"country" json:"country"`
Driveurl string `db:"driveurl" json:"driveurl"`
TotalCount int64 `db:"total_count" json:"total_count"` TotalCount int64 `db:"total_count" json:"total_count"`
} }
@ -2789,6 +2796,7 @@ func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPa
&i.Subdomain, &i.Subdomain,
&i.Amouserid, &i.Amouserid,
&i.Country, &i.Country,
&i.Driveurl,
&i.TotalCount, &i.TotalCount,
); err != nil { ); err != nil {
return nil, err return nil, err
@ -2810,7 +2818,7 @@ SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session,
FROM answer a2 FROM answer a2
WHERE a2.start = true AND a2.session = a.session WHERE a2.start = true AND a2.session = a.session
LIMIT 1) AS utm 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.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 FROM answer a
INNER JOIN quiz q ON a.quiz_id = q.id INNER JOIN quiz q ON a.quiz_id = q.id
LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID
@ -2822,6 +2830,7 @@ WHERE a.result = true
AND a.deleted = false AND a.deleted = false
AND r.deleted = false AND r.deleted = false
AND q.deleted = false AND q.deleted = false
AND u.deleted = false
` `
type GettingAmoUsersTrueResultsRow struct { type GettingAmoUsersTrueResultsRow struct {
@ -2835,10 +2844,14 @@ type GettingAmoUsersTrueResultsRow struct {
Accesstoken string `db:"accesstoken" json:"accesstoken"` Accesstoken string `db:"accesstoken" json:"accesstoken"`
Accountid int32 `db:"accountid" json:"accountid"` Accountid int32 `db:"accountid" json:"accountid"`
Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"`
Tagstoadd json.RawMessage `db:"tagstoadd" json:"tagstoadd"`
Performerid int32 `db:"performerid" json:"performerid"` Performerid int32 `db:"performerid" json:"performerid"`
Stepid int32 `db:"stepid" json:"stepid"` Stepid int32 `db:"stepid" json:"stepid"`
Pipelineid int32 `db:"pipelineid" json:"pipelineid"` Pipelineid int32 `db:"pipelineid" json:"pipelineid"`
PerformerName string `db:"performer_name" json:"performer_name"` 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) { func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoUsersTrueResultsRow, error) {
@ -2861,10 +2874,14 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU
&i.Accesstoken, &i.Accesstoken,
&i.Accountid, &i.Accountid,
&i.Fieldsrule, &i.Fieldsrule,
&i.Tagstoadd,
&i.Performerid, &i.Performerid,
&i.Stepid, &i.Stepid,
&i.Pipelineid, &i.Pipelineid,
&i.PerformerName, &i.PerformerName,
&i.Subdomain,
&i.Accountid_2,
&i.Driveurl,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
@ -3356,9 +3373,9 @@ func (q *Queries) QuizCopyQid(ctx context.Context, arg QuizCopyQidParams) (QuizC
} }
const setQuizSettings = `-- name: SetQuizSettings :one 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, 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 RETURNING id
` `
@ -3368,6 +3385,7 @@ type SetQuizSettingsParams struct {
Pipelineid int32 `db:"pipelineid" json:"pipelineid"` Pipelineid int32 `db:"pipelineid" json:"pipelineid"`
Stepid int32 `db:"stepid" json:"stepid"` Stepid int32 `db:"stepid" json:"stepid"`
Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"` Fieldsrule json.RawMessage `db:"fieldsrule" json:"fieldsrule"`
Tagstoadd json.RawMessage `db:"tagstoadd" json:"tagstoadd"`
Accountid string `db:"accountid" json:"accountid"` Accountid string `db:"accountid" json:"accountid"`
} }
@ -3378,6 +3396,7 @@ func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams
arg.Pipelineid, arg.Pipelineid,
arg.Stepid, arg.Stepid,
arg.Fieldsrule, arg.Fieldsrule,
arg.Tagstoadd,
arg.Accountid, arg.Accountid,
) )
var id int64 var id int64
@ -3390,7 +3409,7 @@ INSERT INTO amoCRMStatuses (AccountID, DealID, AnswerID, Status)
SELECT u.AmoID, $1, $2, $3 SELECT u.AmoID, $1, $2, $3
FROM tokens AS t FROM tokens AS t
JOIN users AS u ON t.AccountID = u.AccountID JOIN users AS u ON t.AccountID = u.AccountID
WHERE t.AccessToken = $4 WHERE t.AccessToken = $4 AND u.Deleted = false
` `
type SettingDealAmoStatusParams struct { type SettingDealAmoStatusParams struct {
@ -3464,7 +3483,7 @@ func (q *Queries) TemplateCopy(ctx context.Context, arg TemplateCopyParams) (int
const updateFieldRules = `-- name: UpdateFieldRules :exec const updateFieldRules = `-- name: UpdateFieldRules :exec
UPDATE rules SET FieldsRule = $1 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 { type UpdateFieldRulesParams struct {
@ -3591,7 +3610,7 @@ func (q *Queries) UpdateUsers(ctx context.Context, dollar_1 json.RawMessage) err
const updatingDealAmoStatus = `-- name: UpdatingDealAmoStatus :exec const updatingDealAmoStatus = `-- name: UpdatingDealAmoStatus :exec
UPDATE amoCRMStatuses SET Status = $1 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 { type UpdatingDealAmoStatusParams struct {

@ -25,6 +25,8 @@ type User struct {
Amouserid int32 `json:"AmoUserID"` Amouserid int32 `json:"AmoUserID"`
/* - страна указанная в настройках амо*/ /* - страна указанная в настройках амо*/
Country string `json:"Country"` Country string `json:"Country"`
// урл объектного хранилища пользователя в амо
DriveURL string `json:"DriveURL"`
} }
type UserGroups struct { type UserGroups struct {
@ -144,12 +146,21 @@ type Rule struct {
//Utms []int32 `json:"UTMs"` //Utms []int32 `json:"UTMs"`
/* - правила заполнения полей сущностей в амо*/ /* - правила заполнения полей сущностей в амо*/
Fieldsrule Fieldsrule `json:"FieldsRule"` Fieldsrule Fieldsrule `json:"FieldsRule"`
// теги добавляемые к сделке
TagsToAdd TagsToAdd `json:"TagsToAdd"`
/* - флаг мягкого удаления*/ /* - флаг мягкого удаления*/
Deleted bool `json:"Deleted"` Deleted bool `json:"Deleted"`
/* - таймштамп создания воронки в нашей системе*/ /* - таймштамп создания воронки в нашей системе*/
Createdat int64 `json:"CreatedAt"` 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 { type Fieldsrule struct {
Lead []FieldRule `json:"Lead"` Lead []FieldRule `json:"Lead"`
Contact ContactRules `json:"Contact"` Contact ContactRules `json:"Contact"`
@ -270,8 +281,12 @@ type AmoUsersTrueResults struct {
AmoAccountID int32 AmoAccountID int32
UTMs UTMSavingMap UTMs UTMSavingMap
FieldsRule Fieldsrule FieldsRule Fieldsrule
TagsToAdd TagsToAdd
PerformerID int32 PerformerID int32
StepID int32 StepID int32
PipelineID int32 PipelineID int32
PerformerName string PerformerName string
SubDomain string
QuizAccountID string
DriveURL string
} }

@ -18,6 +18,7 @@ type RulesReq struct {
StepID int32 // айдишник этапа StepID int32 // айдишник этапа
//Utms []int32 // список UTM для этого опроса //Utms []int32 // список UTM для этого опроса
Fieldsrule Fieldsrule // правила заполнения полей сущностей в амо Fieldsrule Fieldsrule // правила заполнения полей сущностей в амо
TagsToAdd TagsToAdd
} }
//type SaveUserListUTMReq struct { //type SaveUserListUTMReq struct {

@ -21,7 +21,8 @@ type GetCurrentAccountResp struct {
/* - страна указанная в настройках амо*/ /* - страна указанная в настройках амо*/
Country string `json:"Country"` Country string `json:"Country"`
/* - таймштамп создания аккаунта*/ /* - таймштамп создания аккаунта*/
Createdat int64 `json:"CreatedAt"` Createdat int64 `json:"CreatedAt"`
DriveURL string `json:"DriveURL"`
} }
//type GetListUserUTMResp struct { //type GetListUserUTMResp struct {

@ -2,4 +2,4 @@ package pj_errors
import "errors" import "errors"
var ErrNotFound = errors.New("not found") var ErrNotFound = errors.New("not found")

@ -93,6 +93,7 @@ func (r *AmoRepository) GetCurrentAccount(ctx context.Context, accountID string)
Subdomain: row.Subdomain, Subdomain: row.Subdomain,
Amouserid: row.Amouserid, Amouserid: row.Amouserid,
Country: row.Country, Country: row.Country,
DriveURL: row.Driveurl,
} }
return &user, nil return &user, nil
@ -110,6 +111,7 @@ func (r *AmoRepository) CreateAccount(ctx context.Context, accountID string, use
Subdomain: userInfo.Subdomain, Subdomain: userInfo.Subdomain,
Amouserid: userInfo.Amouserid, Amouserid: userInfo.Amouserid,
Country: userInfo.Country, Country: userInfo.Country,
Driveurl: userInfo.DriveURL,
}) })
if err != nil { if err != nil {
@ -807,12 +809,17 @@ func (r *AmoRepository) ChangeQuizSettings(ctx context.Context, request *model.R
if err != nil { if err != nil {
return err return err
} }
jsonTagsToAdd, err := json.Marshal(request.TagsToAdd)
if err != nil {
return err
}
_, err = r.queries.ChangeQuizSettings(ctx, sqlcgen.ChangeQuizSettingsParams{ _, err = r.queries.ChangeQuizSettings(ctx, sqlcgen.ChangeQuizSettingsParams{
Performerid: request.PerformerID, Performerid: request.PerformerID,
Pipelineid: request.PipelineID, Pipelineid: request.PipelineID,
Stepid: request.StepID, Stepid: request.StepID,
//Utms: request.Utms, //Utms: request.Utms,
Fieldsrule: jsonFieldRule, Fieldsrule: jsonFieldRule,
Tagstoadd: jsonTagsToAdd,
Accountid: accountID, Accountid: accountID,
Quizid: int32(quizID), Quizid: int32(quizID),
}) })
@ -829,12 +836,17 @@ func (r *AmoRepository) SetQuizSettings(ctx context.Context, request *model.Rule
if err != nil { if err != nil {
return err return err
} }
jsonTagsToAdd, err := json.Marshal(request.TagsToAdd)
if err != nil {
return err
}
_, err = r.queries.SetQuizSettings(ctx, sqlcgen.SetQuizSettingsParams{ _, err = r.queries.SetQuizSettings(ctx, sqlcgen.SetQuizSettingsParams{
Performerid: request.PerformerID, Performerid: request.PerformerID,
Pipelineid: request.PipelineID, Pipelineid: request.PipelineID,
Stepid: request.StepID, Stepid: request.StepID,
//Utms: request.Utms, //Utms: request.Utms,
Fieldsrule: jsonFieldRule, Fieldsrule: jsonFieldRule,
Tagstoadd: jsonTagsToAdd,
Accountid: accountID, Accountid: accountID,
Quizid: int32(quizID), Quizid: int32(quizID),
}) })
@ -858,6 +870,12 @@ func (r *AmoRepository) GettingQuizRules(ctx context.Context, quizID int) (*mode
return nil, err return nil, err
} }
var tagsToAdd model.TagsToAdd
err = json.Unmarshal(row.Tagstoadd, &tagsToAdd)
if err != nil {
return nil, err
}
return &model.Rule{ return &model.Rule{
ID: row.ID, ID: row.ID,
Accountid: row.Accountid, Accountid: row.Accountid,
@ -867,6 +885,7 @@ func (r *AmoRepository) GettingQuizRules(ctx context.Context, quizID int) (*mode
Stepid: row.Stepid, Stepid: row.Stepid,
//Utms: row.Utms, //Utms: row.Utms,
Fieldsrule: fieldsRule, Fieldsrule: fieldsRule,
TagsToAdd: tagsToAdd,
}, nil }, nil
} }
@ -1024,6 +1043,12 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model
return nil, err return nil, err
} }
var tagsToAdd model.TagsToAdd
err = json.Unmarshal(row.Tagstoadd, &tagsToAdd)
if err != nil {
return nil, err
}
result := model.AmoUsersTrueResults{ result := model.AmoUsersTrueResults{
QuizID: row.QuizID, QuizID: row.QuizID,
AnswerID: row.ID, AnswerID: row.ID,
@ -1035,10 +1060,14 @@ func (r *AmoRepository) GettingAmoUsersTrueResults(ctx context.Context) ([]model
AmoAccountID: row.Accountid, AmoAccountID: row.Accountid,
UTMs: utm, UTMs: utm,
FieldsRule: fieldsRule, FieldsRule: fieldsRule,
TagsToAdd: tagsToAdd,
PerformerID: row.Performerid, PerformerID: row.Performerid,
StepID: row.Stepid, StepID: row.Stepid,
PipelineID: row.Pipelineid, PipelineID: row.Pipelineid,
PerformerName: row.PerformerName, PerformerName: row.PerformerName,
SubDomain: row.Subdomain,
QuizAccountID: row.Accountid_2,
DriveURL: row.Driveurl,
} }
results = append(results, result) results = append(results, result)

@ -28,6 +28,8 @@ packages:
- "./dal/schema/000011_init.down.sql" - "./dal/schema/000011_init.down.sql"
- "./dal/schema/000012_init.up.sql" - "./dal/schema/000012_init.up.sql"
- "./dal/schema/000012_init.down.sql" - "./dal/schema/000012_init.down.sql"
- "./dal/schema/000013_init.up.sql"
- "./dal/schema/000013_init.down.sql"
engine: "postgresql" engine: "postgresql"
emit_json_tags: true emit_json_tags: true
emit_db_tags: true emit_db_tags: true

43
utils/verifyPrivilege.go Normal file

@ -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)
}