Merge branch 'fixPagination' into 'main'
Fix pagination See merge request backend/quiz/common!25
This commit is contained in:
commit
4b0f100655
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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.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
|
||||
@ -1004,18 +1004,19 @@ 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)
|
||||
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[]);
|
||||
|
4
dal/schema/000013_init.down.sql
Normal file
4
dal/schema/000013_init.down.sql
Normal file
@ -0,0 +1,4 @@
|
||||
ALTER TABLE rules
|
||||
DROP COLUMN IF EXISTS TagsToAdd;
|
||||
ALTER TABLE users
|
||||
DROP COLUMN IF EXISTS DriveURL;
|
4
dal/schema/000013_init.up.sql
Normal file
4
dal/schema/000013_init.up.sql
Normal file
@ -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"`
|
||||
Deleted bool `db:"deleted" json:"deleted"`
|
||||
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
||||
Tagstoadd json.RawMessage `db:"tagstoadd" json:"tagstoadd"`
|
||||
}
|
||||
|
||||
type Step struct {
|
||||
@ -185,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"`
|
||||
}
|
||||
|
@ -116,8 +116,8 @@ 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
|
||||
`
|
||||
|
||||
@ -126,6 +126,7 @@ type ChangeQuizSettingsParams struct {
|
||||
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"`
|
||||
}
|
||||
@ -136,6 +137,7 @@ func (q *Queries) ChangeQuizSettings(ctx context.Context, arg ChangeQuizSettings
|
||||
arg.Pipelineid,
|
||||
arg.Stepid,
|
||||
arg.Fieldsrule,
|
||||
arg.Tagstoadd,
|
||||
arg.Accountid,
|
||||
arg.Quizid,
|
||||
)
|
||||
@ -581,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
|
||||
)
|
||||
@ -778,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 {
|
||||
@ -794,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:
|
||||
@ -810,6 +813,7 @@ func (q *Queries) CreateAmoAccount(ctx context.Context, arg CreateAmoAccountPara
|
||||
arg.Subdomain,
|
||||
arg.Amouserid,
|
||||
arg.Country,
|
||||
arg.Driveurl,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@ -1490,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) {
|
||||
@ -1509,6 +1513,7 @@ func (q *Queries) GetCurrentAccount(ctx context.Context, accountid string) (User
|
||||
&i.Subdomain,
|
||||
&i.Amouserid,
|
||||
&i.Country,
|
||||
&i.Driveurl,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@ -1630,7 +1635,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 +1751,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
|
||||
`
|
||||
@ -2279,7 +2284,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 +2300,7 @@ func (q *Queries) GetQuizRule(ctx context.Context, quizid int32) (Rule, error) {
|
||||
&i.Fieldsrule,
|
||||
&i.Deleted,
|
||||
&i.Createdat,
|
||||
&i.Tagstoadd,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@ -2363,7 +2369,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 +2433,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
|
||||
`
|
||||
@ -2738,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
|
||||
@ -2764,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"`
|
||||
}
|
||||
|
||||
@ -2789,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
|
||||
@ -2810,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.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
|
||||
INNER JOIN quiz q ON a.quiz_id = q.id
|
||||
LEFT JOIN amoCRMStatuses s ON a.id = s.AnswerID
|
||||
@ -2822,6 +2830,7 @@ WHERE a.result = true
|
||||
AND a.deleted = false
|
||||
AND r.deleted = false
|
||||
AND q.deleted = false
|
||||
AND u.deleted = false
|
||||
`
|
||||
|
||||
type GettingAmoUsersTrueResultsRow struct {
|
||||
@ -2835,10 +2844,14 @@ type GettingAmoUsersTrueResultsRow struct {
|
||||
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"`
|
||||
Accountid_2 string `db:"accountid_2" json:"accountid_2"`
|
||||
Driveurl string `db:"driveurl" json:"driveurl"`
|
||||
}
|
||||
|
||||
func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoUsersTrueResultsRow, error) {
|
||||
@ -2861,10 +2874,14 @@ func (q *Queries) GettingAmoUsersTrueResults(ctx context.Context) ([]GettingAmoU
|
||||
&i.Accesstoken,
|
||||
&i.Accountid,
|
||||
&i.Fieldsrule,
|
||||
&i.Tagstoadd,
|
||||
&i.Performerid,
|
||||
&i.Stepid,
|
||||
&i.Pipelineid,
|
||||
&i.PerformerName,
|
||||
&i.Subdomain,
|
||||
&i.Accountid_2,
|
||||
&i.Driveurl,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -3356,9 +3373,9 @@ 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
|
||||
`
|
||||
|
||||
@ -3368,6 +3385,7 @@ type SetQuizSettingsParams struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
@ -3378,6 +3396,7 @@ func (q *Queries) SetQuizSettings(ctx context.Context, arg SetQuizSettingsParams
|
||||
arg.Pipelineid,
|
||||
arg.Stepid,
|
||||
arg.Fieldsrule,
|
||||
arg.Tagstoadd,
|
||||
arg.Accountid,
|
||||
)
|
||||
var id int64
|
||||
@ -3390,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 {
|
||||
@ -3464,7 +3483,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 {
|
||||
@ -3591,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 {
|
||||
|
15
model/amo.go
15
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 {
|
||||
@ -144,12 +146,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"`
|
||||
@ -270,8 +281,12 @@ type AmoUsersTrueResults struct {
|
||||
AmoAccountID int32
|
||||
UTMs UTMSavingMap
|
||||
FieldsRule Fieldsrule
|
||||
TagsToAdd TagsToAdd
|
||||
PerformerID int32
|
||||
StepID int32
|
||||
PipelineID int32
|
||||
PerformerName string
|
||||
SubDomain string
|
||||
QuizAccountID string
|
||||
DriveURL string
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ type RulesReq struct {
|
||||
StepID int32 // айдишник этапа
|
||||
//Utms []int32 // список UTM для этого опроса
|
||||
Fieldsrule Fieldsrule // правила заполнения полей сущностей в амо
|
||||
TagsToAdd TagsToAdd
|
||||
}
|
||||
|
||||
//type SaveUserListUTMReq struct {
|
||||
|
@ -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 {
|
||||
|
@ -2,4 +2,4 @@ package pj_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,
|
||||
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 {
|
||||
@ -807,12 +809,17 @@ 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,
|
||||
Stepid: request.StepID,
|
||||
//Utms: request.Utms,
|
||||
Fieldsrule: jsonFieldRule,
|
||||
Tagstoadd: jsonTagsToAdd,
|
||||
Accountid: accountID,
|
||||
Quizid: int32(quizID),
|
||||
})
|
||||
@ -829,12 +836,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),
|
||||
})
|
||||
@ -858,6 +870,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,
|
||||
@ -867,6 +885,7 @@ func (r *AmoRepository) GettingQuizRules(ctx context.Context, quizID int) (*mode
|
||||
Stepid: row.Stepid,
|
||||
//Utms: row.Utms,
|
||||
Fieldsrule: fieldsRule,
|
||||
TagsToAdd: tagsToAdd,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -1024,6 +1043,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,
|
||||
@ -1035,10 +1060,14 @@ 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,
|
||||
PerformerName: row.PerformerName,
|
||||
SubDomain: row.Subdomain,
|
||||
QuizAccountID: row.Accountid_2,
|
||||
DriveURL: row.Driveurl,
|
||||
}
|
||||
|
||||
results = append(results, result)
|
||||
|
@ -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
|
||||
|
43
utils/verifyPrivilege.go
Normal file
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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user