From 0b725ece2228686b429535d1034151d05260d0e6 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 18:56:25 +0300 Subject: [PATCH 01/45] add migrate schema for lead target --- dal/schema/000014_init.down.sql | 2 ++ dal/schema/000014_init.up.sql | 11 +++++++++++ model/model.go | 10 ++++++++++ sqlc.yaml | 2 ++ 4 files changed, 25 insertions(+) create mode 100644 dal/schema/000014_init.down.sql create mode 100644 dal/schema/000014_init.up.sql diff --git a/dal/schema/000014_init.down.sql b/dal/schema/000014_init.down.sql new file mode 100644 index 0000000..79f5d03 --- /dev/null +++ b/dal/schema/000014_init.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE account ADD column email varchar(50) NOT NULL default ''; +DROP TABLE IF EXISTS leadtarget; \ No newline at end of file diff --git a/dal/schema/000014_init.up.sql b/dal/schema/000014_init.up.sql new file mode 100644 index 0000000..19275fb --- /dev/null +++ b/dal/schema/000014_init.up.sql @@ -0,0 +1,11 @@ +AlTER TABLE account DROP column email; + +CREATE TABLE IF NOT EXISTS leadtarget( + ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, + AccountID varchar(30) NOT NULL, + Type varchar(50) NOT NULL, + QuizID integer NOT NULL DEFAULT 0, + Target text NOT NULL, + Deleted boolean NOT NULL DEFAULT false, + CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP +) \ No newline at end of file diff --git a/model/model.go b/model/model.go index 3882503..2026e8a 100644 --- a/model/model.go +++ b/model/model.go @@ -308,3 +308,13 @@ type AnswerExport struct { } type UTMSavingMap map[string]string + +type LeadTarget struct { + ID int64 `json:"id"` + AccountID string `json:"accountID"` + Type string `json:"type"` + QuizID int32 `json:"quizID"` + Target string `json:"target"` + Deleted bool `json:"deleted"` + CreatedAt time.Time `json:"createdAt"` +} diff --git a/sqlc.yaml b/sqlc.yaml index 9658197..d367be4 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -30,6 +30,8 @@ packages: - "./dal/schema/000012_init.down.sql" - "./dal/schema/000013_init.up.sql" - "./dal/schema/000013_init.down.sql" + - "./dal/schema/000014_init.up.sql" + - "./dal/schema/000014_init.down.sql" engine: "postgresql" emit_json_tags: true emit_db_tags: true From 3a299020fdb323f7046ff88c1cc37312624edc0e Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 19:04:45 +0300 Subject: [PATCH 02/45] update sqlc gen --- dal/db_query/queries.sql | 4 +--- dal/sqlcgen/models.go | 11 ++++++++++- dal/sqlcgen/queries.sql.go | 24 ++++-------------------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 47181e4..0bf5b03 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -134,7 +134,6 @@ SELECT p.amount, p.created_at, a.id, - a.email, qz.config FROM privileges AS p @@ -144,7 +143,7 @@ WHERE qz.id = $1; -- name: CreateAccount :one -INSERT INTO account (id, user_id, email, created_at, deleted) VALUES ($1, $2, $3, $4, $5) RETURNING *;; +INSERT INTO account (id, user_id, created_at, deleted) VALUES ($1, $2, $3, $4) RETURNING *; -- name: DeletePrivilegeByAccID :exec DELETE FROM privileges WHERE account_id = $1; @@ -248,7 +247,6 @@ UPDATE privileges SET amount = $1 WHERE id = $2; SELECT a.id, a.user_id, - a.email, a.created_at, COALESCE(p.ID,0), coalesce(p.privilegeid,''), diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index d6e25c0..e83ced9 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -15,7 +15,6 @@ import ( type Account struct { ID uuid.UUID `db:"id" json:"id"` UserID sql.NullString `db:"user_id" json:"user_id"` - Email sql.NullString `db:"email" json:"email"` CreatedAt sql.NullTime `db:"created_at" json:"created_at"` Deleted sql.NullBool `db:"deleted" json:"deleted"` } @@ -62,6 +61,16 @@ type Field struct { Createdat sql.NullTime `db:"createdat" json:"createdat"` } +type Leadtarget struct { + ID int64 `db:"id" json:"id"` + Accountid string `db:"accountid" json:"accountid"` + Type string `db:"type" json:"type"` + Quizid int32 `db:"quizid" json:"quizid"` + Target string `db:"target" json:"target"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat sql.NullTime `db:"createdat" json:"createdat"` +} + type Pipeline struct { ID int64 `db:"id" json:"id"` Amoid int32 `db:"amoid" json:"amoid"` diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 1d2ff0b..480fe84 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -25,22 +25,15 @@ type AccountPaginationParams struct { Offset int32 `db:"offset" json:"offset"` } -type AccountPaginationRow struct { - ID uuid.UUID `db:"id" json:"id"` - UserID sql.NullString `db:"user_id" json:"user_id"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` -} - -func (q *Queries) AccountPagination(ctx context.Context, arg AccountPaginationParams) ([]AccountPaginationRow, error) { +func (q *Queries) AccountPagination(ctx context.Context, arg AccountPaginationParams) ([]Account, error) { rows, err := q.db.QueryContext(ctx, accountPagination, arg.Limit, arg.Offset) if err != nil { return nil, err } defer rows.Close() - var items []AccountPaginationRow + var items []Account for rows.Next() { - var i AccountPaginationRow + var i Account if err := rows.Scan( &i.ID, &i.UserID, @@ -748,13 +741,12 @@ func (q *Queries) CopyQuizQuestions(ctx context.Context, arg CopyQuizQuestionsPa } const createAccount = `-- name: CreateAccount :one -INSERT INTO account (id, user_id, email, created_at, deleted) VALUES ($1, $2, $3, $4, $5) RETURNING id, user_id, email, created_at, deleted +INSERT INTO account (id, user_id, created_at, deleted) VALUES ($1, $2, $3, $4) RETURNING id, user_id, created_at, deleted ` type CreateAccountParams struct { ID uuid.UUID `db:"id" json:"id"` UserID sql.NullString `db:"user_id" json:"user_id"` - Email sql.NullString `db:"email" json:"email"` CreatedAt sql.NullTime `db:"created_at" json:"created_at"` Deleted sql.NullBool `db:"deleted" json:"deleted"` } @@ -763,7 +755,6 @@ func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (A row := q.db.QueryRowContext(ctx, createAccount, arg.ID, arg.UserID, - arg.Email, arg.CreatedAt, arg.Deleted, ) @@ -771,7 +762,6 @@ func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (A err := row.Scan( &i.ID, &i.UserID, - &i.Email, &i.CreatedAt, &i.Deleted, ) @@ -1309,7 +1299,6 @@ const getAccAndPrivilegeByEmail = `-- name: GetAccAndPrivilegeByEmail :one SELECT a.id, a.user_id, - a.email, a.created_at, COALESCE(p.ID,0), coalesce(p.privilegeid,''), @@ -1325,7 +1314,6 @@ WHERE type GetAccAndPrivilegeByEmailRow struct { ID uuid.UUID `db:"id" json:"id"` UserID sql.NullString `db:"user_id" json:"user_id"` - Email sql.NullString `db:"email" json:"email"` CreatedAt sql.NullTime `db:"created_at" json:"created_at"` ID_2 int32 `db:"id_2" json:"id_2"` Privilegeid string `db:"privilegeid" json:"privilegeid"` @@ -1339,7 +1327,6 @@ func (q *Queries) GetAccAndPrivilegeByEmail(ctx context.Context, userID sql.Null err := row.Scan( &i.ID, &i.UserID, - &i.Email, &i.CreatedAt, &i.ID_2, &i.Privilegeid, @@ -1894,7 +1881,6 @@ SELECT p.amount, p.created_at, a.id, - a.email, qz.config FROM privileges AS p @@ -1910,7 +1896,6 @@ type GetPrivilegesQuizAccountRow struct { Amount sql.NullInt32 `db:"amount" json:"amount"` CreatedAt sql.NullTime `db:"created_at" json:"created_at"` ID uuid.UUID `db:"id" json:"id"` - Email sql.NullString `db:"email" json:"email"` Config sql.NullString `db:"config" json:"config"` } @@ -1929,7 +1914,6 @@ func (q *Queries) GetPrivilegesQuizAccount(ctx context.Context, id int64) ([]Get &i.Amount, &i.CreatedAt, &i.ID, - &i.Email, &i.Config, ); err != nil { return nil, err From 9e84512bd6a09acdd92c4843e73479cb2d027868 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 19:06:11 +0300 Subject: [PATCH 03/45] update sqlc gen --- dal/schema/000014_init.up.sql | 4 ++-- dal/sqlcgen/models.go | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/dal/schema/000014_init.up.sql b/dal/schema/000014_init.up.sql index 19275fb..9315d3d 100644 --- a/dal/schema/000014_init.up.sql +++ b/dal/schema/000014_init.up.sql @@ -7,5 +7,5 @@ CREATE TABLE IF NOT EXISTS leadtarget( QuizID integer NOT NULL DEFAULT 0, Target text NOT NULL, Deleted boolean NOT NULL DEFAULT false, - CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP -) \ No newline at end of file + CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index e83ced9..9b75e78 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -62,13 +62,13 @@ type Field struct { } type Leadtarget struct { - ID int64 `db:"id" json:"id"` - Accountid string `db:"accountid" json:"accountid"` - Type string `db:"type" json:"type"` - Quizid int32 `db:"quizid" json:"quizid"` - Target string `db:"target" json:"target"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat sql.NullTime `db:"createdat" json:"createdat"` + ID int64 `db:"id" json:"id"` + Accountid string `db:"accountid" json:"accountid"` + Type string `db:"type" json:"type"` + Quizid int32 `db:"quizid" json:"quizid"` + Target string `db:"target" json:"target"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat time.Time `db:"createdat" json:"createdat"` } type Pipeline struct { From 75e1867c7b36747bc8612403f76d0fd017c72cd2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 19:12:53 +0300 Subject: [PATCH 04/45] update account repo methods --- model/model.go | 1 - repository/account/account.go | 3 --- 2 files changed, 4 deletions(-) diff --git a/model/model.go b/model/model.go index 2026e8a..ce39d85 100644 --- a/model/model.go +++ b/model/model.go @@ -282,7 +282,6 @@ type ExpiredPrivileges struct { type Account struct { ID string `json:"id"` UserID string `json:"user_id"` - Email string `json:"email"` CreatedAt time.Time `json:"created_at"` Deleted bool `json:"deleted"` Privileges map[string]ShortPrivilege `json:"privileges"` diff --git a/repository/account/account.go b/repository/account/account.go index 1cb97b6..9283c4f 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -99,7 +99,6 @@ func (r *AccountRepository) CreateAccount(ctx context.Context, data *model.Accou row, err := r.queries.CreateAccount(ctx, sqlcgen.CreateAccountParams{ ID: uuid.MustParse(data.ID), UserID: sql.NullString{String: data.UserID, Valid: data.UserID != ""}, - Email: sql.NullString{String: data.Email, Valid: data.Email != ""}, CreatedAt: sql.NullTime{Time: data.CreatedAt, Valid: !data.CreatedAt.IsZero()}, Deleted: sql.NullBool{Bool: data.Deleted, Valid: true}, }) @@ -110,7 +109,6 @@ func (r *AccountRepository) CreateAccount(ctx context.Context, data *model.Accou createdAccount := model.Account{ ID: row.ID.String(), UserID: row.UserID.String, - Email: row.Email.String, } for _, privilege := range data.Privileges { @@ -318,7 +316,6 @@ func (r *AccountRepository) GetAccAndPrivilegeByEmail(ctx context.Context, email account.ID = row.ID.String() account.UserID = row.UserID.String - account.Email = row.Email.String account.CreatedAt = row.CreatedAt.Time if row.ID_2 != 0 { From fe7dad850060b6b4c9789806cf9e4465dbcb4588 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 19:56:29 +0300 Subject: [PATCH 05/45] add some queries for lead target --- dal/db_query/queries.sql | 9 +++++++++ repository/account/account.go | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 0bf5b03..48b5081 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1063,3 +1063,12 @@ WHERE AccountID = $1 AND Deleted = false; UPDATE privileges p SET amount = amount - 1 FROM account a WHERE p.account_id = a.id AND a.user_id = $1 AND p.privilegeID = $2 AND p.amount > 0 RETURNING p.id, p.privilegeID, p.account_id, p.privilege_name, p.amount, p.created_at;; + +-- name: CreateLeadTarget :one +INSERT INTO leadtarget (accountID,type,quizID,target) VALUES ($1,$2,$3,$4) RETURNING *; + +-- name: DeleteLeadTarget :exec +UPDATE leadtarget SET deleted = true WHERE id = $1; + +-- name: UpdateLeadTarget :one +UPDATE leadtarget SET target = $1 WHERE id = $2 AND deleted=false; \ No newline at end of file diff --git a/repository/account/account.go b/repository/account/account.go index 9283c4f..f24d0e5 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -362,3 +362,23 @@ func (r *AccountRepository) ManualDone(ctx context.Context, userID string) error return nil } + +func (r *AccountRepository) PostLeadTarget(ctx context.Context, req model.LeadTarget) (model.LeadTarget, error) { + + return model.LeadTarget{}, nil +} + +func (r *AccountRepository) DeleteLeadTarget(ctx context.Context, id int64) error { + + return nil +} + +func (r *AccountRepository) GetLeadTarget(ctx context.Context, id int64) (model.LeadTarget, error) { + + return model.LeadTarget{}, nil +} + +func (r *AccountRepository) UpdateLeadTarget(ctx context.Context, req model.LeadTarget) (model.LeadTarget, error) { + + return model.LeadTarget{}, nil +} From b2078aff4d91aaa405a9cb941b6ea8f7a2c9a272 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 19:58:11 +0300 Subject: [PATCH 06/45] update sqlc gen --- dal/db_query/queries.sql | 2 +- dal/sqlcgen/queries.sql.go | 64 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 48b5081..f737248 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1071,4 +1071,4 @@ INSERT INTO leadtarget (accountID,type,quizID,target) VALUES ($1,$2,$3,$4) RETUR UPDATE leadtarget SET deleted = true WHERE id = $1; -- name: UpdateLeadTarget :one -UPDATE leadtarget SET target = $1 WHERE id = $2 AND deleted=false; \ No newline at end of file +UPDATE leadtarget SET target = $1 WHERE id = $2 AND deleted=false RETURNING *; diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 480fe84..3430464 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -808,6 +808,37 @@ func (q *Queries) CreateAmoAccount(ctx context.Context, arg CreateAmoAccountPara return err } +const createLeadTarget = `-- name: CreateLeadTarget :one +INSERT INTO leadtarget (accountID,type,quizID,target) VALUES ($1,$2,$3,$4) RETURNING id, accountid, type, quizid, target, deleted, createdat +` + +type CreateLeadTargetParams struct { + Accountid string `db:"accountid" json:"accountid"` + Type string `db:"type" json:"type"` + Quizid int32 `db:"quizid" json:"quizid"` + Target string `db:"target" json:"target"` +} + +func (q *Queries) CreateLeadTarget(ctx context.Context, arg CreateLeadTargetParams) (Leadtarget, error) { + row := q.db.QueryRowContext(ctx, createLeadTarget, + arg.Accountid, + arg.Type, + arg.Quizid, + arg.Target, + ) + var i Leadtarget + err := row.Scan( + &i.ID, + &i.Accountid, + &i.Type, + &i.Quizid, + &i.Target, + &i.Deleted, + &i.Createdat, + ) + return i, err +} + const createWebHook = `-- name: CreateWebHook :exec INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, CreatedAt) VALUES ($1, $2, $3, $4, $5, $6) @@ -877,6 +908,15 @@ func (q *Queries) DeleteFields(ctx context.Context, dollar_1 []int64) error { return err } +const deleteLeadTarget = `-- name: DeleteLeadTarget :exec +UPDATE leadtarget SET deleted = true WHERE id = $1 +` + +func (q *Queries) DeleteLeadTarget(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deleteLeadTarget, id) + return err +} + const deletePipelines = `-- name: DeletePipelines :exec UPDATE pipelines SET Deleted = true WHERE ID = ANY($1::bigint[]) ` @@ -3497,6 +3537,30 @@ func (q *Queries) UpdateFields(ctx context.Context, dollar_1 json.RawMessage) er return err } +const updateLeadTarget = `-- name: UpdateLeadTarget :one +UPDATE leadtarget SET target = $1 WHERE id = $2 AND deleted=false RETURNING id, accountid, type, quizid, target, deleted, createdat +` + +type UpdateLeadTargetParams struct { + Target string `db:"target" json:"target"` + ID int64 `db:"id" json:"id"` +} + +func (q *Queries) UpdateLeadTarget(ctx context.Context, arg UpdateLeadTargetParams) (Leadtarget, error) { + row := q.db.QueryRowContext(ctx, updateLeadTarget, arg.Target, arg.ID) + var i Leadtarget + err := row.Scan( + &i.ID, + &i.Accountid, + &i.Type, + &i.Quizid, + &i.Target, + &i.Deleted, + &i.Createdat, + ) + return i, err +} + const updatePipelines = `-- name: UpdatePipelines :exec UPDATE pipelines AS p SET name = (update_data ->> 'Name')::varchar(512), From bfffb1bc3ccea42bb95fa7cde31bb30973f63722 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 20:05:34 +0300 Subject: [PATCH 07/45] add repo methods for lead target --- repository/account/account.go | 43 +++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/repository/account/account.go b/repository/account/account.go index f24d0e5..1c33031 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -364,12 +364,31 @@ func (r *AccountRepository) ManualDone(ctx context.Context, userID string) error } func (r *AccountRepository) PostLeadTarget(ctx context.Context, req model.LeadTarget) (model.LeadTarget, error) { - - return model.LeadTarget{}, nil + row, err := r.queries.CreateLeadTarget(ctx, sqlcgen.CreateLeadTargetParams{ + Accountid: req.AccountID, + Type: req.Type, + Quizid: req.QuizID, + Target: req.Target, + }) + if err != nil { + return model.LeadTarget{}, err + } + return model.LeadTarget{ + ID: row.ID, + AccountID: row.Accountid, + Type: row.Type, + QuizID: row.Quizid, + Target: row.Target, + Deleted: row.Deleted, + CreatedAt: row.Createdat, + }, nil } func (r *AccountRepository) DeleteLeadTarget(ctx context.Context, id int64) error { - + err := r.queries.DeleteLeadTarget(ctx, id) + if err != nil { + return err + } return nil } @@ -379,6 +398,20 @@ func (r *AccountRepository) GetLeadTarget(ctx context.Context, id int64) (model. } func (r *AccountRepository) UpdateLeadTarget(ctx context.Context, req model.LeadTarget) (model.LeadTarget, error) { - - return model.LeadTarget{}, nil + row, err := r.queries.UpdateLeadTarget(ctx, sqlcgen.UpdateLeadTargetParams{ + Target: req.Target, + ID: req.ID, + }) + if err != nil { + return model.LeadTarget{}, nil + } + return model.LeadTarget{ + ID: row.ID, + AccountID: row.Accountid, + Type: row.Type, + QuizID: row.Quizid, + Target: row.Target, + Deleted: row.Deleted, + CreatedAt: row.Createdat, + }, nil } From 3d0ee4560f62fea9dd751b8ab5e3f1e289138c42 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 10 Jun 2024 20:08:52 +0300 Subject: [PATCH 08/45] add repo methods for lead target --- repository/account/account.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repository/account/account.go b/repository/account/account.go index 1c33031..524b9ba 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -392,9 +392,9 @@ func (r *AccountRepository) DeleteLeadTarget(ctx context.Context, id int64) erro return nil } -func (r *AccountRepository) GetLeadTarget(ctx context.Context, id int64) (model.LeadTarget, error) { +func (r *AccountRepository) GetLeadTarget(ctx context.Context, accountID string) ([]model.LeadTarget, error) { - return model.LeadTarget{}, nil + return []model.LeadTarget{}, nil } func (r *AccountRepository) UpdateLeadTarget(ctx context.Context, req model.LeadTarget) (model.LeadTarget, error) { From df0974000967baff4513a63545a61f82e309e708 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 11 Jun 2024 16:49:15 +0300 Subject: [PATCH 09/45] add getting lead target query --- dal/db_query/queries.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index f737248..2413636 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1072,3 +1072,6 @@ UPDATE leadtarget SET deleted = true WHERE id = $1; -- name: UpdateLeadTarget :one UPDATE leadtarget SET target = $1 WHERE id = $2 AND deleted=false RETURNING *; + +-- name: GetLeadTarget :many +SELECT * FROM leadtarget WHERE accountID = $1 AND quizID = $2 AND deleted=false; From 4c6b21e7f029c4090cca81431d2ba439d78a7231 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 11 Jun 2024 16:51:08 +0300 Subject: [PATCH 10/45] update sqlc gen --- dal/sqlcgen/queries.sql.go | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 3430464..189b9de 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1720,6 +1720,46 @@ func (q *Queries) GetFieldsWithPagination(ctx context.Context, arg GetFieldsWith return items, nil } +const getLeadTarget = `-- name: GetLeadTarget :many +SELECT id, accountid, type, quizid, target, deleted, createdat FROM leadtarget WHERE accountID = $1 AND quizID = $2 AND deleted=false +` + +type GetLeadTargetParams struct { + Accountid string `db:"accountid" json:"accountid"` + Quizid int32 `db:"quizid" json:"quizid"` +} + +func (q *Queries) GetLeadTarget(ctx context.Context, arg GetLeadTargetParams) ([]Leadtarget, error) { + rows, err := q.db.QueryContext(ctx, getLeadTarget, arg.Accountid, arg.Quizid) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Leadtarget + for rows.Next() { + var i Leadtarget + if err := rows.Scan( + &i.ID, + &i.Accountid, + &i.Type, + &i.Quizid, + &i.Target, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getListCreatedQuizzes = `-- name: GetListCreatedQuizzes :many SELECT id FROM quiz From 30262442b2d415b52dafe361061caf4fdd59794f Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 11 Jun 2024 16:56:31 +0300 Subject: [PATCH 11/45] add repo method for get lead target --- repository/account/account.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/repository/account/account.go b/repository/account/account.go index 524b9ba..8c9eac3 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -392,9 +392,28 @@ func (r *AccountRepository) DeleteLeadTarget(ctx context.Context, id int64) erro return nil } -func (r *AccountRepository) GetLeadTarget(ctx context.Context, accountID string) ([]model.LeadTarget, error) { +func (r *AccountRepository) GetLeadTarget(ctx context.Context, accountID string, quizID int32) ([]model.LeadTarget, error) { + rows, err := r.queries.GetLeadTarget(ctx, sqlcgen.GetLeadTargetParams{ + Accountid: accountID, + Quizid: quizID, + }) + if err != nil { + return nil, err + } + var leadTargets []model.LeadTarget + for _, row := range rows { + leadTargets = append(leadTargets, model.LeadTarget{ + ID: row.ID, + AccountID: row.Accountid, + Type: row.Type, + QuizID: row.Quizid, + Target: row.Target, + Deleted: row.Deleted, + CreatedAt: row.Createdat, + }) + } - return []model.LeadTarget{}, nil + return leadTargets, nil } func (r *AccountRepository) UpdateLeadTarget(ctx context.Context, req model.LeadTarget) (model.LeadTarget, error) { From 0013810bb8821e182c9701c096d78703bb4ac917 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 11 Jun 2024 19:08:22 +0300 Subject: [PATCH 12/45] added error handling --- repository/account/account.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/repository/account/account.go b/repository/account/account.go index 8c9eac3..2bedd34 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -398,6 +398,9 @@ func (r *AccountRepository) GetLeadTarget(ctx context.Context, accountID string, Quizid: quizID, }) if err != nil { + if err == sql.ErrNoRows { + return nil, pj_errors.ErrNotFound + } return nil, err } var leadTargets []model.LeadTarget @@ -422,7 +425,10 @@ func (r *AccountRepository) UpdateLeadTarget(ctx context.Context, req model.Lead ID: req.ID, }) if err != nil { - return model.LeadTarget{}, nil + if err == sql.ErrNoRows { + return model.LeadTarget{}, pj_errors.ErrNotFound + } + return model.LeadTarget{}, err } return model.LeadTarget{ ID: row.ID, From 9a8f348b2cc4bb1b21aeb0d249e87830c6c50706 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 11 Jun 2024 19:36:23 +0300 Subject: [PATCH 13/45] remove from smtpClient username and password --- clients/smtp.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/clients/smtp.go b/clients/smtp.go index 2b2f802..8c59537 100644 --- a/clients/smtp.go +++ b/clients/smtp.go @@ -13,8 +13,6 @@ type Deps struct { SmtpHost string SmtpPort string SmtpSender string - Username string - Password string ApiKey string FiberClient *fiber.Client } @@ -24,8 +22,6 @@ type SmtpClient struct { smtpHost string smtpPort string smtpSender string - username string - password string apiKey string fiberClient *fiber.Client } @@ -39,8 +35,6 @@ func NewSmtpClient(deps Deps) *SmtpClient { smtpHost: deps.SmtpHost, smtpPort: deps.SmtpPort, smtpSender: deps.SmtpSender, - username: deps.Username, - password: deps.Password, apiKey: deps.ApiKey, fiberClient: deps.FiberClient, } From b6ef236e9cff90773a74b6f5b439783a7a4ff04a Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 12 Jun 2024 11:19:29 +0300 Subject: [PATCH 14/45] update migrate files, add enum --- dal/schema/000014_init.down.sql | 8 ++++++++ dal/schema/000014_init.up.sql | 11 +++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/dal/schema/000014_init.down.sql b/dal/schema/000014_init.down.sql index 79f5d03..1fe8abb 100644 --- a/dal/schema/000014_init.down.sql +++ b/dal/schema/000014_init.down.sql @@ -1,2 +1,10 @@ ALTER TABLE account ADD column email varchar(50) NOT NULL default ''; + +DO $$ + BEGIN + IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'leadtargettype') THEN + DROP TYPE LeadTargetType; + END IF; + END $$; + DROP TABLE IF EXISTS leadtarget; \ No newline at end of file diff --git a/dal/schema/000014_init.up.sql b/dal/schema/000014_init.up.sql index 9315d3d..f775929 100644 --- a/dal/schema/000014_init.up.sql +++ b/dal/schema/000014_init.up.sql @@ -1,11 +1,18 @@ AlTER TABLE account DROP column email; +DO $$ + BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'leadtargettype') THEN + CREATE TYPE LeadTargetType AS ENUM ('mail', 'telegram', 'whatsapp'); + END IF; + END $$; + CREATE TABLE IF NOT EXISTS leadtarget( ID BIGSERIAL UNIQUE NOT NULL PRIMARY KEY, AccountID varchar(30) NOT NULL, - Type varchar(50) NOT NULL, + Type LeadTargetType NOT NULL, QuizID integer NOT NULL DEFAULT 0, Target text NOT NULL, Deleted boolean NOT NULL DEFAULT false, CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP -) +); From 5d83fbbd5ec93d542eb97657075d5c0d8637d950 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 12 Jun 2024 11:22:34 +0300 Subject: [PATCH 15/45] add leadTargetType to model --- model/model.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/model/model.go b/model/model.go index ce39d85..4611976 100644 --- a/model/model.go +++ b/model/model.go @@ -309,11 +309,19 @@ type AnswerExport struct { type UTMSavingMap map[string]string type LeadTarget struct { - ID int64 `json:"id"` - AccountID string `json:"accountID"` - Type string `json:"type"` - QuizID int32 `json:"quizID"` - Target string `json:"target"` - Deleted bool `json:"deleted"` - CreatedAt time.Time `json:"createdAt"` + ID int64 `json:"id"` + AccountID string `json:"accountID"` + Type LeadTargetType `json:"type"` + QuizID int32 `json:"quizID"` + Target string `json:"target"` + Deleted bool `json:"deleted"` + CreatedAt time.Time `json:"createdAt"` } + +type LeadTargetType string + +const ( + LeadTargetEmail LeadTargetType = "mail" + LeadTargetTg LeadTargetType = "telegram" + LeadTargetTgWhatsapp LeadTargetType = "whatsapp" +) From da7f30385194f26a15a3aad634f483e3a278ca47 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 12 Jun 2024 11:24:37 +0300 Subject: [PATCH 16/45] update sqlc gen --- dal/sqlcgen/models.go | 14 +++++++------- dal/sqlcgen/queries.sql.go | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 9b75e78..2bf4670 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -62,13 +62,13 @@ type Field struct { } type Leadtarget struct { - ID int64 `db:"id" json:"id"` - Accountid string `db:"accountid" json:"accountid"` - Type string `db:"type" json:"type"` - Quizid int32 `db:"quizid" json:"quizid"` - Target string `db:"target" json:"target"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat time.Time `db:"createdat" json:"createdat"` + ID int64 `db:"id" json:"id"` + Accountid string `db:"accountid" json:"accountid"` + Type interface{} `db:"type" json:"type"` + Quizid int32 `db:"quizid" json:"quizid"` + Target string `db:"target" json:"target"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat time.Time `db:"createdat" json:"createdat"` } type Pipeline struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 189b9de..c2f2ebc 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -813,10 +813,10 @@ INSERT INTO leadtarget (accountID,type,quizID,target) VALUES ($1,$2,$3,$4) RETUR ` type CreateLeadTargetParams struct { - Accountid string `db:"accountid" json:"accountid"` - Type string `db:"type" json:"type"` - Quizid int32 `db:"quizid" json:"quizid"` - Target string `db:"target" json:"target"` + Accountid string `db:"accountid" json:"accountid"` + Type interface{} `db:"type" json:"type"` + Quizid int32 `db:"quizid" json:"quizid"` + Target string `db:"target" json:"target"` } func (q *Queries) CreateLeadTarget(ctx context.Context, arg CreateLeadTargetParams) (Leadtarget, error) { From 567119396b9952d0f9995bbbaef5edaf91fb66fc Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 12 Jun 2024 11:31:34 +0300 Subject: [PATCH 17/45] update lead target repo methods --- repository/account/account.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/repository/account/account.go b/repository/account/account.go index 2bedd34..ebfd7a5 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -373,10 +373,15 @@ func (r *AccountRepository) PostLeadTarget(ctx context.Context, req model.LeadTa if err != nil { return model.LeadTarget{}, err } + + var targetType model.LeadTargetType + v := string(row.Type.([]byte)) + targetType = model.LeadTargetType(v) + return model.LeadTarget{ ID: row.ID, AccountID: row.Accountid, - Type: row.Type, + Type: targetType, QuizID: row.Quizid, Target: row.Target, Deleted: row.Deleted, @@ -405,10 +410,14 @@ func (r *AccountRepository) GetLeadTarget(ctx context.Context, accountID string, } var leadTargets []model.LeadTarget for _, row := range rows { + var targetType model.LeadTargetType + v := string(row.Type.([]byte)) + targetType = model.LeadTargetType(v) + leadTargets = append(leadTargets, model.LeadTarget{ ID: row.ID, AccountID: row.Accountid, - Type: row.Type, + Type: targetType, QuizID: row.Quizid, Target: row.Target, Deleted: row.Deleted, @@ -430,10 +439,15 @@ func (r *AccountRepository) UpdateLeadTarget(ctx context.Context, req model.Lead } return model.LeadTarget{}, err } + + var targetType model.LeadTargetType + v := string(row.Type.([]byte)) + targetType = model.LeadTargetType(v) + return model.LeadTarget{ ID: row.ID, AccountID: row.Accountid, - Type: row.Type, + Type: targetType, QuizID: row.Quizid, Target: row.Target, Deleted: row.Deleted, From 11882ffe22cfd63c4ebd600ff6250409b1db453a Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 12 Jun 2024 11:35:24 +0300 Subject: [PATCH 18/45] add map for validation LeadTargetType --- model/model.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/model/model.go b/model/model.go index 4611976..c013f0c 100644 --- a/model/model.go +++ b/model/model.go @@ -325,3 +325,9 @@ const ( LeadTargetTg LeadTargetType = "telegram" LeadTargetTgWhatsapp LeadTargetType = "whatsapp" ) + +var ValidLeadTargetTypes = map[string]bool{ + "mail": true, + "telegram": true, + "whatsapp": true, +} From 65d859bf3945d7c74746042c72e6ed1866e4d4d7 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 24 Jun 2024 13:41:49 +0300 Subject: [PATCH 19/45] prepare for merge --- dal/schema/{000014_init.down.sql => 000017_init.down.sql} | 0 dal/schema/{000014_init.up.sql => 000017_init.up.sql} | 0 sqlc.yaml | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename dal/schema/{000014_init.down.sql => 000017_init.down.sql} (100%) rename dal/schema/{000014_init.up.sql => 000017_init.up.sql} (100%) diff --git a/dal/schema/000014_init.down.sql b/dal/schema/000017_init.down.sql similarity index 100% rename from dal/schema/000014_init.down.sql rename to dal/schema/000017_init.down.sql diff --git a/dal/schema/000014_init.up.sql b/dal/schema/000017_init.up.sql similarity index 100% rename from dal/schema/000014_init.up.sql rename to dal/schema/000017_init.up.sql diff --git a/sqlc.yaml b/sqlc.yaml index d367be4..8aac75c 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -30,8 +30,8 @@ packages: - "./dal/schema/000012_init.down.sql" - "./dal/schema/000013_init.up.sql" - "./dal/schema/000013_init.down.sql" - - "./dal/schema/000014_init.up.sql" - - "./dal/schema/000014_init.down.sql" + - "./dal/schema/000017_init.up.sql" + - "./dal/schema/000017_init.down.sql" engine: "postgresql" emit_json_tags: true emit_db_tags: true From 6982631f2a4b591a95ceb4921105ba7fe136b7ea Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 24 Jun 2024 13:51:35 +0300 Subject: [PATCH 20/45] update sqlc gen --- dal/db_query/queries.sql | 4 +- dal/sqlcgen/queries.sql.go | 254 ++++++++++++++++++------------------- 2 files changed, 128 insertions(+), 130 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index e5868b8..0bc82b7 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -134,7 +134,6 @@ SELECT p.amount, p.created_at, a.id, - a.email, qz.config FROM privileges AS p @@ -144,7 +143,7 @@ WHERE qz.id = $1; -- name: CreateAccount :one -INSERT INTO account (id, user_id, email, created_at, deleted) VALUES ($1, $2, $3, $4, $5) RETURNING *;; +INSERT INTO account (id, user_id, created_at, deleted) VALUES ($1, $2, $3, $4) RETURNING *; -- name: DeletePrivilegeByAccID :exec DELETE FROM privileges WHERE account_id = $1; @@ -248,7 +247,6 @@ UPDATE privileges SET amount = $1 WHERE id = $2; SELECT a.id, a.user_id, - a.email, a.created_at, COALESCE(p.ID,0), coalesce(p.privilegeid,''), diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 736febf..73d8199 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -206,29 +206,29 @@ WITH user_data AS ( SELECT AmoID FROM accountsAmo WHERE accountsAmo.AccountID = $1 AND accountsAmo.Deleted = false ), new_fields AS ( SELECT (field->>'AmoID')::INT AS amoID, - COALESCE(field->>'Code', '')::varchar(255) AS code, - COALESCE(field->>'Name', '')::varchar(512) AS name, - CAST(field->>'Entity' AS entitytype) AS Entity, - COALESCE(field->>'Type', '')::fieldtype AS type, - CURRENT_TIMESTAMP AS createdAt + COALESCE(field->>'Code', '')::varchar(255) AS code, + COALESCE(field->>'Name', '')::varchar(512) AS name, + CAST(field->>'Entity' AS entitytype) AS Entity, + COALESCE(field->>'Type', '')::fieldtype AS type, + CURRENT_TIMESTAMP AS createdAt FROM json_array_elements($2::json) AS field ), inserted_fields AS( INSERT INTO fields (amoID, code, accountID, name, Entity, type, createdAt) - SELECT nf.amoID, - nf.code, - ud.AmoID, - nf.name, - nf.Entity, - nf.type, - nf.createdAt - FROM new_fields nf - JOIN user_data ud ON true - ON CONFLICT (amoID, accountID, entity) DO NOTHING - RETURNING id, amoid, code, accountid, name, entity, type, deleted, createdat + SELECT nf.amoID, + nf.code, + ud.AmoID, + nf.name, + nf.Entity, + nf.type, + nf.createdAt + FROM new_fields nf + JOIN user_data ud ON true + ON CONFLICT (amoID, accountID, entity) DO NOTHING + RETURNING id, amoid, code, accountid, name, entity, type, deleted, createdat ) SELECT nf.amoid, nf.code, nf.name, nf.entity, nf.type, nf.createdat,ud.AmoID FROM new_fields nf - JOIN user_data ud ON true + JOIN user_data ud ON true WHERE NOT EXISTS ( SELECT id, ins.amoid, code, accountid, name, entity, type, deleted, createdat, ud.amoid FROM inserted_fields ins @@ -293,14 +293,14 @@ WITH new_pipelines AS ( FROM json_array_elements($1::json) AS pipeline ), inserted_pipelines AS( INSERT INTO pipelines (amoID, accountID, name, isArchive, createdAt) - SELECT np.amoID, - np.accountID, - np.name, - np.isArchive, - np.createdAt - FROM new_pipelines np - ON CONFLICT (amoID, accountID) DO NOTHING - RETURNING id, amoid, accountid, name, isarchive, deleted, createdat + SELECT np.amoID, + np.accountID, + np.name, + np.isArchive, + np.createdAt + FROM new_pipelines np + ON CONFLICT (amoID, accountID) DO NOTHING + RETURNING id, amoid, accountid, name, isarchive, deleted, createdat ) SELECT np.amoid, np.accountid, np.name, np.isarchive, np.createdat FROM new_pipelines np @@ -405,15 +405,15 @@ WITH new_steps AS ( FROM json_array_elements($1::json) AS step ), inserted_steps AS ( INSERT INTO steps (amoID, pipelineID, accountID, name, color, createdAt) - SELECT ns.amoID, - ns.pipelineID, - ns.accountID, - ns.name, - ns.color, - ns.createdAt - FROM new_steps ns - ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING - RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat + SELECT ns.amoID, + ns.pipelineID, + ns.accountID, + ns.name, + ns.color, + ns.createdAt + FROM new_steps ns + ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING + RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat ) SELECT ns.amoid, ns.pipelineid, ns.accountid, ns.name, ns.color, ns.createdat FROM new_steps ns @@ -487,7 +487,7 @@ WITH user_data AS ( ) SELECT nt.amoid, nt.entity, nt.name, nt.color,ud.AmoID FROM new_tags nt - JOIN user_data ud ON true + JOIN user_data ud ON true WHERE NOT EXISTS ( SELECT id, ins.amoid, accountid, entity, name, color, deleted, createdat, ud.amoid FROM inserted_tags ins @@ -546,7 +546,7 @@ INSERT INTO question( SELECT $1, title, description, questiontype, required, page, content, version, parent_ids FROM question WHERE question.id=$2 - RETURNING question.id, quiz_id, created_at, updated_at +RETURNING question.id, quiz_id, created_at, updated_at ` type CopyQuestionParams struct { @@ -578,12 +578,12 @@ INSERT INTO question ( quiz_id, title, description, questiontype, required, page, content, version, parent_ids, created_at, updated_at ) -SELECT +SELECT $2, title, description, questiontype, required, page, content, version, parent_ids, created_at, updated_at -FROM +FROM question -WHERE +WHERE question.quiz_id = $1 AND deleted = false ` @@ -605,7 +605,7 @@ INSERT INTO quiz( SELECT accountid, archived,fingerprinting,repeatable,note_prevented,mail_notifications,unique_answers,name,description,config, status,limit_answers,due_to,time_of_passing,pausable,version,version_comment,parent_ids,questions_count, super, group_id FROM quiz WHERE quiz.id=$1 AND quiz.accountId=$2 - RETURNING id, qid,created_at, updated_at +RETURNING id, qid,created_at, updated_at ` type CopyQuizParams struct { @@ -1061,7 +1061,7 @@ INSERT INTO question( SELECT quiz_id, title, description, questiontype, required, page, content, version, parent_ids FROM question WHERE question.id=$1 - RETURNING question.id, quiz_id, created_at, updated_at +RETURNING question.id, quiz_id, created_at, updated_at ` type DuplicateQuestionRow struct { @@ -1088,13 +1088,13 @@ WITH TimeBucket AS ( SELECT date_trunc('hour', timestamp_bucket)::TIMESTAMP AS time_interval_start, COALESCE(LEAD( - date_trunc('hour', timestamp_bucket)::TIMESTAMP - ) OVER (ORDER BY timestamp_bucket), NOW()) AS time_interval_end + date_trunc('hour', timestamp_bucket)::TIMESTAMP + ) OVER (ORDER BY timestamp_bucket), NOW()) AS time_interval_end FROM - generate_series(TO_TIMESTAMP($1), TO_TIMESTAMP($2), CASE - WHEN EXTRACT(epoch FROM TO_TIMESTAMP($2)) - EXTRACT(epoch FROM TO_TIMESTAMP($1)) > 172800 THEN '1 day'::interval - ELSE '1 hour'::interval - END) AS timestamp_bucket + generate_series(TO_TIMESTAMP($1), TO_TIMESTAMP($2), CASE + WHEN EXTRACT(epoch FROM TO_TIMESTAMP($2)) - EXTRACT(epoch FROM TO_TIMESTAMP($1)) > 172800 THEN '1 day'::interval + ELSE '1 hour'::interval + END) AS timestamp_bucket ), OpenStats AS ( SELECT @@ -1175,7 +1175,7 @@ SELECT CASE WHEN COALESCE(os.open_count, 0) > 0 THEN COALESCE(rs.true_result_count, 0)::float / COALESCE(os.open_count, 0)::float ELSE 0 - END::float AS conversion, + END::float AS conversion, COALESCE(at.avg_time, 0) AS avg_time FROM TimeBucket tb @@ -2837,11 +2837,11 @@ func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPa const gettingAmoUsersTrueResults = `-- name: GettingAmoUsersTrueResults :many SELECT a.quiz_id,a.id,a.result,a.question_id,a.content,a.session, - COALESCE((SELECT a2.utm - FROM answer a2 - WHERE a2.start = true AND a2.session = a.session - LIMIT 1), '{}'::jsonb) AS utm -,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM usersAmo u WHERE u.AmoUserID = r.performerid AND u.deleted = false) AS performer_name,u.subdomain,u.accountid,u.driveurl + COALESCE((SELECT a2.utm + FROM answer a2 + WHERE a2.start = true AND a2.session = a.session + LIMIT 1), '{}'::jsonb) AS utm + ,t.accesstoken,r.accountid,r.fieldsrule,r.tagstoadd,r.performerid,r.stepid,r.pipelineid,(SELECT u.name FROM usersAmo u WHERE u.AmoUserID = r.performerid AND u.deleted = false) 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 @@ -3050,7 +3050,7 @@ INSERT INTO question ( updated_at ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) - RETURNING id, created_at, updated_at +RETURNING id, created_at, updated_at ` type InsertQuestionParams struct { @@ -3110,7 +3110,7 @@ INSERT INTO quiz (accountid, qid ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19) - RETURNING id, created_at, updated_at, qid +RETURNING id, created_at, updated_at, qid ` type InsertQuizParams struct { @@ -3182,7 +3182,7 @@ INSERT INTO question( SELECT quiz_id, title, description, questiontype, required, page, content, version, parent_ids, true as deleted FROM question WHERE question.id=$1 - RETURNING question.id, quiz_id, parent_ids +RETURNING question.id, quiz_id, parent_ids ` type MoveToHistoryRow struct { @@ -3206,7 +3206,7 @@ INSERT INTO quiz(deleted, SELECT true as deleted, accountid, archived,fingerprinting,repeatable,note_prevented,mail_notifications,unique_answers,name,description,config, status,limit_answers,due_to,time_of_passing,pausable,version,version_comment,parent_ids,questions_count,answers_count,average_time_passing, super, group_id FROM quiz WHERE quiz.id=$1 AND quiz.accountid=$2 - RETURNING quiz.id, qid, parent_ids +RETURNING quiz.id, qid, parent_ids ` type MoveToHistoryQuizParams struct { @@ -3230,10 +3230,10 @@ func (q *Queries) MoveToHistoryQuiz(ctx context.Context, arg MoveToHistoryQuizPa const questionsStatistics = `-- name: QuestionsStatistics :many WITH Funnel AS ( SELECT - COUNT(DISTINCT a.session) FILTER (WHERE a.start = FALSE) AS count_start_false, - COUNT(DISTINCT a.session) FILTER (WHERE a.start = TRUE) AS count_start_true, - COUNT(DISTINCT CASE WHEN a.result = FALSE AND qid_true_result IS NOT NULL THEN a.session END) AS count_f_result_with_t_question, - COUNT(DISTINCT a.session) FILTER (WHERE a.result = TRUE) AS count_t_result + COUNT(DISTINCT a.session) FILTER (WHERE a.start = FALSE) AS count_start_false, + COUNT(DISTINCT a.session) FILTER (WHERE a.start = TRUE) AS count_start_true, + COUNT(DISTINCT CASE WHEN a.result = FALSE AND qid_true_result IS NOT NULL THEN a.session END) AS count_f_result_with_t_question, + COUNT(DISTINCT a.session) FILTER (WHERE a.result = TRUE) AS count_t_result FROM answer a LEFT JOIN ( @@ -3248,23 +3248,23 @@ WITH Funnel AS ( AND a.created_at <= TO_TIMESTAMP($3) ), Results AS ( - SELECT - COALESCE(q.title, '') AS question_title, - COUNT(*) AS total_answers, - CAST(COUNT(*) * 100.0 / NULLIF(SUM(COUNT(*)) FILTER (WHERE a.result = TRUE) OVER (PARTITION BY a.quiz_id), 0) AS FLOAT8) AS percentage - FROM - question q - JOIN answer a ON q.id = a.question_id - WHERE - a.quiz_id = $1 - AND a.created_at >= TO_TIMESTAMP($2) - AND a.created_at <= TO_TIMESTAMP($3) - AND a.result = TRUE - GROUP BY - q.title, a.quiz_id, a.result - HAVING - COUNT(*) >= 1 - ), + SELECT + COALESCE(q.title, '') AS question_title, + COUNT(*) AS total_answers, + CAST(COUNT(*) * 100.0 / NULLIF(SUM(COUNT(*)) FILTER (WHERE a.result = TRUE) OVER (PARTITION BY a.quiz_id), 0) AS FLOAT8) AS percentage + FROM + question q + JOIN answer a ON q.id = a.question_id + WHERE + a.quiz_id = $1 + AND a.created_at >= TO_TIMESTAMP($2) + AND a.created_at <= TO_TIMESTAMP($3) + AND a.result = TRUE + GROUP BY + q.title, a.quiz_id, a.result + HAVING + COUNT(*) >= 1 + ), LastContent AS ( SELECT a.question_id, @@ -3280,35 +3280,35 @@ WITH Funnel AS ( answer WHERE quiz_id = $1 - AND start != true + AND start != true AND created_at >= TO_TIMESTAMP($2) AND created_at <= TO_TIMESTAMP($3) GROUP BY - question_id, session + question_id, session ) AS last_created_at_one_session ON a.session = last_created_at_one_session.session AND a.question_id = last_created_at_one_session.question_id AND a.created_at = last_created_at_one_session.last_created_at ), Questions AS ( - SELECT - q.title AS question_title, - q.page AS question_page, - lc.last_answer_content AS answer_content, - CAST( - COUNT(CASE WHEN a.result = FALSE THEN 1 END) * 100.0 / NULLIF(SUM(COUNT(CASE WHEN a.result = FALSE THEN 1 END)) OVER (PARTITION BY q.id), 0) AS FLOAT8 - ) AS percentage - FROM - question q - JOIN LastContent lc ON q.id = lc.question_id - JOIN answer a ON q.id = a.question_id - WHERE - a.quiz_id = $1 - AND a.start != true - AND a.created_at >= TO_TIMESTAMP($2) - AND a.created_at <= TO_TIMESTAMP($3) - GROUP BY - q.id, q.title, lc.last_answer_content - HAVING - COUNT(*) >= 1 -) + SELECT + q.title AS question_title, + q.page AS question_page, + lc.last_answer_content AS answer_content, + CAST( + COUNT(CASE WHEN a.result = FALSE THEN 1 END) * 100.0 / NULLIF(SUM(COUNT(CASE WHEN a.result = FALSE THEN 1 END)) OVER (PARTITION BY q.id), 0) AS FLOAT8 + ) AS percentage + FROM + question q + JOIN LastContent lc ON q.id = lc.question_id + JOIN answer a ON q.id = a.question_id + WHERE + a.quiz_id = $1 + AND a.start != true + AND a.created_at >= TO_TIMESTAMP($2) + AND a.created_at <= TO_TIMESTAMP($3) + GROUP BY + q.id, q.title, lc.last_answer_content + HAVING + COUNT(*) >= 1 + ) SELECT Funnel.count_start_false, Funnel.count_start_true, @@ -3322,8 +3322,8 @@ SELECT COALESCE(Questions.percentage, 0) AS questions_percentage FROM Funnel - LEFT JOIN Results ON true - LEFT JOIN Questions ON Questions.percentage >= 1 + LEFT JOIN Results ON true + LEFT JOIN Questions ON Questions.percentage >= 1 ` type QuestionsStatisticsParams struct { @@ -3380,18 +3380,18 @@ func (q *Queries) QuestionsStatistics(ctx context.Context, arg QuestionsStatisti } const quizCopyQid = `-- name: QuizCopyQid :one - INSERT INTO quiz ( - accountid, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, name, description, config, - status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, questions_count, answers_count, average_time_passing, super, group_id - ) - SELECT - $2, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, name, description, config, - status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, questions_count, answers_count, average_time_passing, super, group_id - FROM - quiz as q - WHERE - q.qid = $1 - RETURNING (select id from quiz where qid = $1),id, qid +INSERT INTO quiz ( + accountid, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, name, description, config, + status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, questions_count, answers_count, average_time_passing, super, group_id +) +SELECT + $2, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, name, description, config, + status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, questions_count, answers_count, average_time_passing, super, group_id +FROM + quiz as q +WHERE + q.qid = $1 +RETURNING (select id from quiz where qid = $1),id, qid ` type QuizCopyQidParams struct { @@ -3416,7 +3416,7 @@ const setQuizSettings = `-- name: SetQuizSettings :one 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,$6 AS TagsToAdd FROM accountsamo u WHERE u.AccountID = $7 AND u.Deleted = false - RETURNING id +RETURNING id ` type SetQuizSettingsParams struct { @@ -3473,10 +3473,10 @@ const softDeleteAccount = `-- name: SoftDeleteAccount :exec WITH amoCompany AS ( SELECT AmoID FROM accountsAmo WHERE accountsAmo.AccountID = $1 ),usersDel AS ( - UPDATE usersAmo SET Deleted = true WHERE AmoID = (SELECT AmoID FROM amoCompany) - ), - companyDel AS ( UPDATE accountsAmo SET Deleted = true WHERE AmoID = (SELECT AmoID FROM amoCompany) - ) + UPDATE usersAmo SET Deleted = true WHERE AmoID = (SELECT AmoID FROM amoCompany) +), + companyDel AS ( UPDATE accountsAmo SET Deleted = true WHERE AmoID = (SELECT AmoID FROM amoCompany) + ) DELETE FROM tokens WHERE tokens.AccountID = $1 ` @@ -3741,9 +3741,9 @@ const webhookDelete = `-- name: WebhookDelete :exec WITH companyDel AS ( UPDATE accountsAmo SET Deleted = true WHERE accountsAmo.AmoID = $1 RETURNING AccountID ), -userDel AS ( -UPDATE usersAmo SET Deleted = true WHERE AmoID = $1 -) + userDel AS ( + UPDATE usersAmo SET Deleted = true WHERE AmoID = $1 + ) DELETE FROM tokens WHERE AccountID IN (SELECT AccountID FROM companyDel) ` @@ -3827,11 +3827,11 @@ SET answers_count = COALESCE(aa.unique_true_answers_count, 0), average_time_passing = COALESCE(sta.average_session_time, 0), sessions_count = COALESCE(sta.sess,0) - FROM +FROM (SELECT id, qid, accountid, deleted, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, super, group_id, name, description, config, status, limit_answers, due_to, time_of_passing, pausable, version, version_comment, parent_ids, created_at, updated_at, questions_count, answers_count, average_time_passing, sessions_count FROM quiz WHERE deleted = FALSE AND archived = FALSE) q_sub -LEFT JOIN answer_aggregates aa ON q_sub.id = aa.quiz_id - LEFT JOIN question_aggregates qa ON q_sub.id = qa.quiz_id - LEFT JOIN session_times_aggregates sta ON q_sub.id = sta.quiz_id + LEFT JOIN answer_aggregates aa ON q_sub.id = aa.quiz_id + LEFT JOIN question_aggregates qa ON q_sub.id = qa.quiz_id + LEFT JOIN session_times_aggregates sta ON q_sub.id = sta.quiz_id WHERE q.id = q_sub.id ` From 0bf45822a652ff3493f6690a0cb5a1ebfa082516 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 24 Jun 2024 16:26:38 +0300 Subject: [PATCH 21/45] update get lead target method --- repository/account/account.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repository/account/account.go b/repository/account/account.go index ebfd7a5..9557f2d 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -404,9 +404,9 @@ func (r *AccountRepository) GetLeadTarget(ctx context.Context, accountID string, }) if err != nil { if err == sql.ErrNoRows { - return nil, pj_errors.ErrNotFound + return []model.LeadTarget{}, pj_errors.ErrNotFound } - return nil, err + return []model.LeadTarget{}, err } var leadTargets []model.LeadTarget for _, row := range rows { From 99008ff19374d3865a4cf55bc0f663c5758867a4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 27 Jun 2024 22:15:13 +0300 Subject: [PATCH 22/45] add new mig files and some queries --- dal/db_query/queries.sql | 7 +++++++ dal/schema/000018_init.down.sql | 8 ++++++++ dal/schema/000018_init.up.sql | 16 ++++++++++++++++ sqlc.yaml | 2 ++ 4 files changed, 33 insertions(+) create mode 100644 dal/schema/000018_init.down.sql create mode 100644 dal/schema/000018_init.up.sql diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 0bc82b7..e56617d 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1067,3 +1067,10 @@ UPDATE leadtarget SET target = $1 WHERE id = $2 AND deleted=false RETURNING *; -- name: GetLeadTarget :many SELECT * FROM leadtarget WHERE accountID = $1 AND quizID = $2 AND deleted=false; + +-- name: CreateTgAccount :one +INSERT INTO tgAccounts (ApiID, ApiHash, PhoneNumber, Status) +VALUES ($1, $2, $3, $4) RETURNING id; + +-- name: GetAllTgAccounts :many +SELECT * FROM tgAccounts WHERE Deleted = false; diff --git a/dal/schema/000018_init.down.sql b/dal/schema/000018_init.down.sql new file mode 100644 index 0000000..32bdc01 --- /dev/null +++ b/dal/schema/000018_init.down.sql @@ -0,0 +1,8 @@ +DROP TABLE IF EXISTS tgAccounts; + +DO $$ + BEGIN + IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'tgAccountStatus') THEN + DROP TYPE TgAccountStatus; + END IF; + END $$; \ No newline at end of file diff --git a/dal/schema/000018_init.up.sql b/dal/schema/000018_init.up.sql new file mode 100644 index 0000000..b845759 --- /dev/null +++ b/dal/schema/000018_init.up.sql @@ -0,0 +1,16 @@ +DO $$ + BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'tgAccountStatus') THEN + CREATE TYPE TgAccountStatus AS ENUM ('active', 'inactive', 'ban'); + END IF; + END $$; + +CREATE TABLE IF NOT EXISTS tgAccounts ( + id bigserial primary key , + ApiID integer not null, + ApiHash text not null , + PhoneNumber text not null , + Status TgAccountStatus not null, + Deleted bool not null default false, + CreatedAt timestamp not null default current_timestamp +) \ No newline at end of file diff --git a/sqlc.yaml b/sqlc.yaml index 031ec0e..c92df77 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -36,6 +36,8 @@ packages: - "./dal/schema/000016_init.down.sql" - "./dal/schema/000017_init.up.sql" - "./dal/schema/000017_init.down.sql" + - "./dal/schema/000018_init.up.sql" + - "./dal/schema/000018_init.down.sql" engine: "postgresql" emit_json_tags: true emit_db_tags: true From 0f877904d4c25e371c93754100aecae2cc633004 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 27 Jun 2024 22:17:33 +0300 Subject: [PATCH 23/45] update sqlc gen --- dal/sqlcgen/models.go | 10 +++++++ dal/sqlcgen/queries.sql.go | 59 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 448fa8a..1356f4c 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -192,6 +192,16 @@ type Tag struct { Createdat sql.NullTime `db:"createdat" json:"createdat"` } +type Tgaccount struct { + ID int64 `db:"id" json:"id"` + Apiid int32 `db:"apiid" json:"apiid"` + Apihash string `db:"apihash" json:"apihash"` + Phonenumber string `db:"phonenumber" json:"phonenumber"` + Status interface{} `db:"status" json:"status"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat time.Time `db:"createdat" json:"createdat"` +} + type Token struct { Accountid string `db:"accountid" json:"accountid"` Refreshtoken string `db:"refreshtoken" json:"refreshtoken"` diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 73d8199..c578621 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -737,6 +737,30 @@ func (q *Queries) CreateLeadTarget(ctx context.Context, arg CreateLeadTargetPara return i, err } +const createTgAccount = `-- name: CreateTgAccount :one +INSERT INTO tgAccounts (ApiID, ApiHash, PhoneNumber, Status) +VALUES ($1, $2, $3, $4) RETURNING id +` + +type CreateTgAccountParams struct { + Apiid int32 `db:"apiid" json:"apiid"` + Apihash string `db:"apihash" json:"apihash"` + Phonenumber string `db:"phonenumber" json:"phonenumber"` + Status interface{} `db:"status" json:"status"` +} + +func (q *Queries) CreateTgAccount(ctx context.Context, arg CreateTgAccountParams) (int64, error) { + row := q.db.QueryRowContext(ctx, createTgAccount, + arg.Apiid, + arg.Apihash, + arg.Phonenumber, + arg.Status, + ) + var id int64 + err := row.Scan(&id) + return id, err +} + const createWebHook = `-- name: CreateWebHook :exec INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, CreatedAt) VALUES ($1, $2, $3, $4, $5, $6) @@ -1421,6 +1445,41 @@ func (q *Queries) GetAllCompanyUsers(ctx context.Context, amoid int32) ([]Usersa return items, nil } +const getAllTgAccounts = `-- name: GetAllTgAccounts :many +SELECT id, apiid, apihash, phonenumber, status, deleted, createdat FROM tgAccounts WHERE Deleted = false +` + +func (q *Queries) GetAllTgAccounts(ctx context.Context) ([]Tgaccount, error) { + rows, err := q.db.QueryContext(ctx, getAllTgAccounts) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Tgaccount + for rows.Next() { + var i Tgaccount + if err := rows.Scan( + &i.ID, + &i.Apiid, + &i.Apihash, + &i.Phonenumber, + &i.Status, + &i.Deleted, + &i.Createdat, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getAllTokens = `-- name: GetAllTokens :many SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokens ` From dc9d35442939cde52587500a9889661007c36e17 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 27 Jun 2024 22:30:57 +0300 Subject: [PATCH 24/45] add tg repo and some methods --- dal/dal.go | 8 ++++++ model/tg.go | 21 ++++++++++++++++ repository/tg/tg.go | 59 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 model/tg.go create mode 100644 repository/tg/tg.go diff --git a/dal/dal.go b/dal/dal.go index 106e537..1b0de35 100644 --- a/dal/dal.go +++ b/dal/dal.go @@ -17,6 +17,7 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/quiz" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/result" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/statistics" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/tg" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/repository/workers" "time" ) @@ -34,6 +35,7 @@ type DAL struct { WorkerRepo *workers.WorkerRepository StatisticsRepo *statistics.StatisticsRepository WorkerAnsRepo *answer.WorkerAnswerRepository + TgRepo *tg.TgRepo } func New(ctx context.Context, cred string, minioClient *minio.Client) (*DAL, error) { @@ -100,6 +102,11 @@ func New(ctx context.Context, cred string, minioClient *minio.Client) (*DAL, err Pool: pool, }) + tgRepo := tg.NewTgRepo(tg.Deps{ + Queries: queries, + Pool: pool, + }) + return &DAL{ conn: pool, queries: queries, @@ -111,6 +118,7 @@ func New(ctx context.Context, cred string, minioClient *minio.Client) (*DAL, err WorkerRepo: workerRepo, StatisticsRepo: statisticsRepo, WorkerAnsRepo: workerAnsRepo, + TgRepo: tgRepo, }, nil } diff --git a/model/tg.go b/model/tg.go new file mode 100644 index 0000000..cc6be7e --- /dev/null +++ b/model/tg.go @@ -0,0 +1,21 @@ +package model + +import "time" + +type TgAccount struct { + ID int64 + ApiID int32 + ApiHash string + PhoneNumber string + Status TgAccountStatus + Deleted bool + CreatedAt time.Time +} + +type TgAccountStatus string + +const ( + ActiveTg TgAccountStatus = "active" + InactiveTg TgAccountStatus = "inactive" + BanTg TgAccountStatus = "ban" +) diff --git a/repository/tg/tg.go b/repository/tg/tg.go new file mode 100644 index 0000000..66e95ce --- /dev/null +++ b/repository/tg/tg.go @@ -0,0 +1,59 @@ +package tg + +import ( + "context" + "database/sql" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" +) + +type TgRepo struct { + queries *sqlcgen.Queries + pool *sql.DB +} + +type Deps struct { + Queries *sqlcgen.Queries + Pool *sql.DB +} + +func NewTgRepo(deps Deps) *TgRepo { + return &TgRepo{ + queries: deps.Queries, + pool: deps.Pool, + } +} + +func (r *TgRepo) CreateTgAccount(ctx context.Context, data model.TgAccount) (int64, error) { + id, err := r.queries.CreateTgAccount(ctx, sqlcgen.CreateTgAccountParams{ + Apiid: data.ApiID, + Apihash: data.ApiHash, + Phonenumber: data.PhoneNumber, + Status: data.Status, + }) + if err != nil { + return 0, err + } + return id, err +} + +func (r *TgRepo) GetAllTgAccounts(ctx context.Context) ([]model.TgAccount, error) { + rows, err := r.queries.GetAllTgAccounts(ctx) + if err != nil { + return nil, err + } + var result []model.TgAccount + for _, row := range rows { + result = append(result, model.TgAccount{ + ID: row.ID, + ApiID: row.Apiid, + ApiHash: row.Apihash, + PhoneNumber: row.Phonenumber, + Status: row.Status.(model.TgAccountStatus), + Deleted: row.Deleted, + CreatedAt: row.Createdat, + }) + } + + return result, nil +} From f77e86125c6feb4f2138d63a8eda49072ab6e24a Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 27 Jun 2024 22:40:42 +0300 Subject: [PATCH 25/45] add tg repo and some methods --- dal/db_query/queries.sql | 4 ++-- dal/schema/000018_init.up.sql | 1 + model/tg.go | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index e56617d..4e6793a 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1069,8 +1069,8 @@ UPDATE leadtarget SET target = $1 WHERE id = $2 AND deleted=false RETURNING *; SELECT * FROM leadtarget WHERE accountID = $1 AND quizID = $2 AND deleted=false; -- name: CreateTgAccount :one -INSERT INTO tgAccounts (ApiID, ApiHash, PhoneNumber, Status) -VALUES ($1, $2, $3, $4) RETURNING id; +INSERT INTO tgAccounts (ApiID, ApiHash, PhoneNumber,Password, Status) +VALUES ($1, $2, $3, $4, $5) RETURNING id; -- name: GetAllTgAccounts :many SELECT * FROM tgAccounts WHERE Deleted = false; diff --git a/dal/schema/000018_init.up.sql b/dal/schema/000018_init.up.sql index b845759..4a801eb 100644 --- a/dal/schema/000018_init.up.sql +++ b/dal/schema/000018_init.up.sql @@ -10,6 +10,7 @@ CREATE TABLE IF NOT EXISTS tgAccounts ( ApiID integer not null, ApiHash text not null , PhoneNumber text not null , + Password text not null , Status TgAccountStatus not null, Deleted bool not null default false, CreatedAt timestamp not null default current_timestamp diff --git a/model/tg.go b/model/tg.go index cc6be7e..543a777 100644 --- a/model/tg.go +++ b/model/tg.go @@ -7,6 +7,7 @@ type TgAccount struct { ApiID int32 ApiHash string PhoneNumber string + Password string Status TgAccountStatus Deleted bool CreatedAt time.Time From 1fd7fa2d2d18028d9072c4339a97af76a768b0a2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 27 Jun 2024 22:43:00 +0300 Subject: [PATCH 26/45] update sqlc gen --- dal/sqlcgen/models.go | 1 + dal/sqlcgen/queries.sql.go | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 1356f4c..0a45556 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -197,6 +197,7 @@ type Tgaccount struct { Apiid int32 `db:"apiid" json:"apiid"` Apihash string `db:"apihash" json:"apihash"` Phonenumber string `db:"phonenumber" json:"phonenumber"` + Password string `db:"password" json:"password"` Status interface{} `db:"status" json:"status"` Deleted bool `db:"deleted" json:"deleted"` Createdat time.Time `db:"createdat" json:"createdat"` diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index c578621..f71d910 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -738,14 +738,15 @@ func (q *Queries) CreateLeadTarget(ctx context.Context, arg CreateLeadTargetPara } const createTgAccount = `-- name: CreateTgAccount :one -INSERT INTO tgAccounts (ApiID, ApiHash, PhoneNumber, Status) -VALUES ($1, $2, $3, $4) RETURNING id +INSERT INTO tgAccounts (ApiID, ApiHash, PhoneNumber,Password, Status) +VALUES ($1, $2, $3, $4, $5) RETURNING id ` type CreateTgAccountParams struct { Apiid int32 `db:"apiid" json:"apiid"` Apihash string `db:"apihash" json:"apihash"` Phonenumber string `db:"phonenumber" json:"phonenumber"` + Password string `db:"password" json:"password"` Status interface{} `db:"status" json:"status"` } @@ -754,6 +755,7 @@ func (q *Queries) CreateTgAccount(ctx context.Context, arg CreateTgAccountParams arg.Apiid, arg.Apihash, arg.Phonenumber, + arg.Password, arg.Status, ) var id int64 @@ -1446,7 +1448,7 @@ func (q *Queries) GetAllCompanyUsers(ctx context.Context, amoid int32) ([]Usersa } const getAllTgAccounts = `-- name: GetAllTgAccounts :many -SELECT id, apiid, apihash, phonenumber, status, deleted, createdat FROM tgAccounts WHERE Deleted = false +SELECT id, apiid, apihash, phonenumber, password, status, deleted, createdat FROM tgAccounts WHERE Deleted = false ` func (q *Queries) GetAllTgAccounts(ctx context.Context) ([]Tgaccount, error) { @@ -1463,6 +1465,7 @@ func (q *Queries) GetAllTgAccounts(ctx context.Context) ([]Tgaccount, error) { &i.Apiid, &i.Apihash, &i.Phonenumber, + &i.Password, &i.Status, &i.Deleted, &i.Createdat, From f997b952c31f44f521fceb6c6fa2088f9c7b22b7 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 27 Jun 2024 22:44:40 +0300 Subject: [PATCH 27/45] add tg repo and some methods --- repository/tg/tg.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/repository/tg/tg.go b/repository/tg/tg.go index 66e95ce..51ae267 100644 --- a/repository/tg/tg.go +++ b/repository/tg/tg.go @@ -30,6 +30,7 @@ func (r *TgRepo) CreateTgAccount(ctx context.Context, data model.TgAccount) (int Apihash: data.ApiHash, Phonenumber: data.PhoneNumber, Status: data.Status, + Password: data.Password, }) if err != nil { return 0, err @@ -52,6 +53,7 @@ func (r *TgRepo) GetAllTgAccounts(ctx context.Context) ([]model.TgAccount, error Status: row.Status.(model.TgAccountStatus), Deleted: row.Deleted, CreatedAt: row.Createdat, + Password: row.Password, }) } From 5c863f56bd989edbec2088ea18cb7a46bd83525f Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 28 Jun 2024 08:56:14 +0300 Subject: [PATCH 28/45] added error not found --- repository/tg/tg.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/repository/tg/tg.go b/repository/tg/tg.go index 51ae267..c9b4044 100644 --- a/repository/tg/tg.go +++ b/repository/tg/tg.go @@ -5,6 +5,7 @@ import ( "database/sql" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors" ) type TgRepo struct { @@ -41,6 +42,9 @@ func (r *TgRepo) CreateTgAccount(ctx context.Context, data model.TgAccount) (int func (r *TgRepo) GetAllTgAccounts(ctx context.Context) ([]model.TgAccount, error) { rows, err := r.queries.GetAllTgAccounts(ctx) if err != nil { + if err == sql.ErrNoRows { + return nil, pj_errors.ErrNotFound + } return nil, err } var result []model.TgAccount From bf14e745201b3bceec9cd2521ed3a5594fb1f323 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 12:12:50 +0300 Subject: [PATCH 29/45] fix []byte implimentation --- repository/tg/tg.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/repository/tg/tg.go b/repository/tg/tg.go index c9b4044..534ebeb 100644 --- a/repository/tg/tg.go +++ b/repository/tg/tg.go @@ -5,7 +5,6 @@ import ( "database/sql" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" - "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors" ) type TgRepo struct { @@ -42,19 +41,19 @@ func (r *TgRepo) CreateTgAccount(ctx context.Context, data model.TgAccount) (int func (r *TgRepo) GetAllTgAccounts(ctx context.Context) ([]model.TgAccount, error) { rows, err := r.queries.GetAllTgAccounts(ctx) if err != nil { - if err == sql.ErrNoRows { - return nil, pj_errors.ErrNotFound - } return nil, err } var result []model.TgAccount for _, row := range rows { + var status model.TgAccountStatus + s := string(row.Status.([]byte)) + status = model.TgAccountStatus(s) result = append(result, model.TgAccount{ ID: row.ID, ApiID: row.Apiid, ApiHash: row.Apihash, PhoneNumber: row.Phonenumber, - Status: row.Status.(model.TgAccountStatus), + Status: status, Deleted: row.Deleted, CreatedAt: row.Createdat, Password: row.Password, From 908aac5ae955cca2000ea1c662c6dfc876c759bb Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 15:30:09 +0300 Subject: [PATCH 30/45] add update queries --- dal/db_query/queries.sql | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index 4e6793a..eda2105 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1074,3 +1074,9 @@ VALUES ($1, $2, $3, $4, $5) RETURNING id; -- name: GetAllTgAccounts :many SELECT * FROM tgAccounts WHERE Deleted = false; + +-- name: UpdateStatusTg :exec +UPDATE tgAccounts SET Status = $1 WHERE id = $2; + +-- name: SoftDeleteTgAccount :exec +UPDATE tgAccounts SET Deleted = true WHERE id = $1; \ No newline at end of file From b752297075946d9c603f674b2037bb1107511acd Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 15:31:32 +0300 Subject: [PATCH 31/45] update sqlc gen --- dal/sqlcgen/queries.sql.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index f71d910..74da9de 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3556,6 +3556,15 @@ func (q *Queries) SoftDeleteResultByID(ctx context.Context, id int64) error { return err } +const softDeleteTgAccount = `-- name: SoftDeleteTgAccount :exec +UPDATE tgAccounts SET Deleted = true WHERE id = $1 +` + +func (q *Queries) SoftDeleteTgAccount(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, softDeleteTgAccount, id) + return err +} + const templateCopy = `-- name: TemplateCopy :one WITH copied_quiz AS ( INSERT INTO quiz (accountid, name,fingerprinting,repeatable,note_prevented,mail_notifications,unique_answers,super,group_id, description, config, status,limit_answers,due_to,time_of_passing,pausable,version,version_comment, parent_ids) @@ -3753,6 +3762,20 @@ func (q *Queries) UpdatePrivilegeAmount(ctx context.Context, arg UpdatePrivilege return err } +const updateStatusTg = `-- name: UpdateStatusTg :exec +UPDATE tgAccounts SET Status = $1 WHERE id = $2 +` + +type UpdateStatusTgParams struct { + Status interface{} `db:"status" json:"status"` + ID int64 `db:"id" json:"id"` +} + +func (q *Queries) UpdateStatusTg(ctx context.Context, arg UpdateStatusTgParams) error { + _, err := q.db.ExecContext(ctx, updateStatusTg, arg.Status, arg.ID) + return err +} + const updateSteps = `-- name: UpdateSteps :exec UPDATE steps AS s SET name = (update_data ->> 'Name')::varchar(512), From 6dbbc07f9c248758b7213cfbf11341cf2acfe304 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 15:35:46 +0300 Subject: [PATCH 32/45] add repo methods for new queries --- repository/tg/tg.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/repository/tg/tg.go b/repository/tg/tg.go index 534ebeb..ffeafa1 100644 --- a/repository/tg/tg.go +++ b/repository/tg/tg.go @@ -62,3 +62,22 @@ func (r *TgRepo) GetAllTgAccounts(ctx context.Context) ([]model.TgAccount, error return result, nil } + +func (r *TgRepo) UpdateStatusTg(ctx context.Context, id int64, status model.TgAccountStatus) error { + err := r.queries.UpdateStatusTg(ctx, sqlcgen.UpdateStatusTgParams{ + Status: status, + ID: id, + }) + if err != nil { + return err + } + return nil +} + +func (r *TgRepo) SoftDeleteTgAccount(ctx context.Context, id int64) error { + err := r.queries.SoftDeleteTgAccount(ctx, id) + if err != nil { + return err + } + return nil +} From 60c3dd519fb2e4675c5a9205942e9159c715898d Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 16:08:20 +0300 Subject: [PATCH 33/45] add index for dont accept duplicate --- dal/schema/000018_init.down.sql | 4 +++- dal/schema/000018_init.up.sql | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/dal/schema/000018_init.down.sql b/dal/schema/000018_init.down.sql index 32bdc01..749dbb0 100644 --- a/dal/schema/000018_init.down.sql +++ b/dal/schema/000018_init.down.sql @@ -5,4 +5,6 @@ DO $$ IF EXISTS (SELECT 1 FROM pg_type WHERE typname = 'tgAccountStatus') THEN DROP TYPE TgAccountStatus; END IF; - END $$; \ No newline at end of file + END $$; + +DROP INDEX IF EXISTS idx_apiid_apihash; \ No newline at end of file diff --git a/dal/schema/000018_init.up.sql b/dal/schema/000018_init.up.sql index 4a801eb..e165366 100644 --- a/dal/schema/000018_init.up.sql +++ b/dal/schema/000018_init.up.sql @@ -14,4 +14,6 @@ CREATE TABLE IF NOT EXISTS tgAccounts ( Status TgAccountStatus not null, Deleted bool not null default false, CreatedAt timestamp not null default current_timestamp -) \ No newline at end of file +); + +CREATE UNIQUE INDEX idx_apiid_apihash ON tgAccounts (ApiID, ApiHash) WHERE Deleted = false; From 42ace400b7307bb8514af7c2d6580254dc69a5a2 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 16:12:53 +0300 Subject: [PATCH 34/45] error --- repository/tg/tg.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/repository/tg/tg.go b/repository/tg/tg.go index ffeafa1..7aa10d7 100644 --- a/repository/tg/tg.go +++ b/repository/tg/tg.go @@ -5,6 +5,7 @@ import ( "database/sql" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" + "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors" ) type TgRepo struct { @@ -41,6 +42,9 @@ func (r *TgRepo) CreateTgAccount(ctx context.Context, data model.TgAccount) (int func (r *TgRepo) GetAllTgAccounts(ctx context.Context) ([]model.TgAccount, error) { rows, err := r.queries.GetAllTgAccounts(ctx) if err != nil { + if err == sql.ErrNoRows { + return nil, pj_errors.ErrNotFound + } return nil, err } var result []model.TgAccount From 6c22684890f5ef1ee9adf3b84d513c453cf7b67c Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 2 Jul 2024 18:15:20 +0300 Subject: [PATCH 35/45] update query --- dal/db_query/queries.sql | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index eda2105..6595e94 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1079,4 +1079,7 @@ SELECT * FROM tgAccounts WHERE Deleted = false; UPDATE tgAccounts SET Status = $1 WHERE id = $2; -- name: SoftDeleteTgAccount :exec -UPDATE tgAccounts SET Deleted = true WHERE id = $1; \ No newline at end of file +UPDATE tgAccounts SET Deleted = true WHERE id = $1; + +-- name: SearchIDByAppIDanAppHash :one +SELECT * FROM tgAccounts WHERE ApiID = $1 and ApiHash=$2 and Deleted = false; \ No newline at end of file From c3faa2a21c88eac78e052e0ff97f5611462455a7 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 1 Jul 2024 15:32:19 +0300 Subject: [PATCH 36/45] update sqlc gen --- dal/sqlcgen/queries.sql.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 74da9de..47dc7cd 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -3474,6 +3474,31 @@ func (q *Queries) QuizCopyQid(ctx context.Context, arg QuizCopyQidParams) (QuizC return i, err } +const searchIDByAppIDanAppHash = `-- name: SearchIDByAppIDanAppHash :one +SELECT id, apiid, apihash, phonenumber, password, status, deleted, createdat FROM tgAccounts WHERE ApiID = $1 and ApiHash=$2 and Deleted = false +` + +type SearchIDByAppIDanAppHashParams struct { + Apiid int32 `db:"apiid" json:"apiid"` + Apihash string `db:"apihash" json:"apihash"` +} + +func (q *Queries) SearchIDByAppIDanAppHash(ctx context.Context, arg SearchIDByAppIDanAppHashParams) (Tgaccount, error) { + row := q.db.QueryRowContext(ctx, searchIDByAppIDanAppHash, arg.Apiid, arg.Apihash) + var i Tgaccount + err := row.Scan( + &i.ID, + &i.Apiid, + &i.Apihash, + &i.Phonenumber, + &i.Password, + &i.Status, + &i.Deleted, + &i.Createdat, + ) + return i, err +} + const setQuizSettings = `-- name: SetQuizSettings :one INSERT INTO rules (AccountID, QuizID, PerformerID, PipelineID, StepID, FieldsRule,TagsToAdd) SELECT u.AmoID AS AccountID,$1 AS QuizID,$2 AS PerformerID,$3 AS PipelineID, From c2a0ba2ac3074312c8deaa1debcdefb4977d7dc0 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 2 Jul 2024 18:20:13 +0300 Subject: [PATCH 37/45] update query --- repository/tg/tg.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/repository/tg/tg.go b/repository/tg/tg.go index 7aa10d7..4675079 100644 --- a/repository/tg/tg.go +++ b/repository/tg/tg.go @@ -85,3 +85,27 @@ func (r *TgRepo) SoftDeleteTgAccount(ctx context.Context, id int64) error { } return nil } + +func (r *TgRepo) SearchIDByAppIDanAppHash(ctx context.Context, appID int32, appHash string) (*model.TgAccount, error) { + row, err := r.queries.SearchIDByAppIDanAppHash(ctx, sqlcgen.SearchIDByAppIDanAppHashParams{ + Apiid: appID, + Apihash: appHash, + }) + if err != nil { + return nil, err + } + var status model.TgAccountStatus + s := string(row.Status.([]byte)) + status = model.TgAccountStatus(s) + + return &model.TgAccount{ + ID: row.ID, + ApiID: row.Apiid, + ApiHash: row.Apihash, + PhoneNumber: row.Phonenumber, + Status: status, + Deleted: row.Deleted, + CreatedAt: row.Createdat, + Password: row.Password, + }, nil +} From b7ab43df146b8a98bbcf3163a8a5ac92cf1e4c17 Mon Sep 17 00:00:00 2001 From: pasha1coil Date: Thu, 11 Jul 2024 11:37:39 +0300 Subject: [PATCH 38/45] modify lead target mig files and added struct for redis tg task --- dal/schema/000017_init.up.sql | 1 + model/model.go | 15 ++++++++------- model/tg.go | 6 ++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/dal/schema/000017_init.up.sql b/dal/schema/000017_init.up.sql index f775929..8da2b34 100644 --- a/dal/schema/000017_init.up.sql +++ b/dal/schema/000017_init.up.sql @@ -13,6 +13,7 @@ CREATE TABLE IF NOT EXISTS leadtarget( Type LeadTargetType NOT NULL, QuizID integer NOT NULL DEFAULT 0, Target text NOT NULL, + InviteLink text NOT NULL DEFAULT '', Deleted boolean NOT NULL DEFAULT false, CreatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); diff --git a/model/model.go b/model/model.go index c013f0c..5477142 100644 --- a/model/model.go +++ b/model/model.go @@ -309,13 +309,14 @@ type AnswerExport struct { type UTMSavingMap map[string]string type LeadTarget struct { - ID int64 `json:"id"` - AccountID string `json:"accountID"` - Type LeadTargetType `json:"type"` - QuizID int32 `json:"quizID"` - Target string `json:"target"` - Deleted bool `json:"deleted"` - CreatedAt time.Time `json:"createdAt"` + ID int64 `json:"id"` + AccountID string `json:"accountID"` + Type LeadTargetType `json:"type"` + QuizID int32 `json:"quizID"` + Target string `json:"target"` + InviteLink string `json:"inviteLink"` // например указывается для типа телеграмма для созданного канала + Deleted bool `json:"deleted"` + CreatedAt time.Time `json:"createdAt"` } type LeadTargetType string diff --git a/model/tg.go b/model/tg.go index 543a777..0db8bbe 100644 --- a/model/tg.go +++ b/model/tg.go @@ -20,3 +20,9 @@ const ( InactiveTg TgAccountStatus = "inactive" BanTg TgAccountStatus = "ban" ) + +type TgRedisTask struct { + Name string + QuizID int32 + AccountID string +} From e6acec0da16bdb9205860da22e29580d776f0d6c Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 16:14:04 +0300 Subject: [PATCH 39/45] update lead target queries, added entity invite link --- 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 6595e94..5addaf5 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1057,13 +1057,13 @@ INSERT INTO amoContact (AccountID, AmoID, Field) VALUES ($1, $2, $3) RETURNING A UPDATE amoContact SET Field = $1,AmoID=$3 WHERE ID = $2; -- name: CreateLeadTarget :one -INSERT INTO leadtarget (accountID,type,quizID,target) VALUES ($1,$2,$3,$4) RETURNING *; +INSERT INTO leadtarget (accountID,type,quizID,target,InviteLink) VALUES ($1,$2,$3,$4,$5) RETURNING *; -- name: DeleteLeadTarget :exec UPDATE leadtarget SET deleted = true WHERE id = $1; -- name: UpdateLeadTarget :one -UPDATE leadtarget SET target = $1 WHERE id = $2 AND deleted=false RETURNING *; +UPDATE leadtarget SET target = $1,InviteLink = $2 WHERE id = $3 AND deleted=false RETURNING *; -- name: GetLeadTarget :many SELECT * FROM leadtarget WHERE accountID = $1 AND quizID = $2 AND deleted=false; From 8bc80d1457012dc4aa1de0e4fd3c351a58f88760 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 16:26:52 +0300 Subject: [PATCH 40/45] update sqlc gen --- dal/sqlcgen/models.go | 15 ++++++++------- dal/sqlcgen/queries.sql.go | 26 ++++++++++++++++---------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 0a45556..1788137 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -81,13 +81,14 @@ type Field struct { } type Leadtarget struct { - ID int64 `db:"id" json:"id"` - Accountid string `db:"accountid" json:"accountid"` - Type interface{} `db:"type" json:"type"` - Quizid int32 `db:"quizid" json:"quizid"` - Target string `db:"target" json:"target"` - Deleted bool `db:"deleted" json:"deleted"` - Createdat time.Time `db:"createdat" json:"createdat"` + ID int64 `db:"id" json:"id"` + Accountid string `db:"accountid" json:"accountid"` + Type interface{} `db:"type" json:"type"` + Quizid int32 `db:"quizid" json:"quizid"` + Target string `db:"target" json:"target"` + Invitelink string `db:"invitelink" json:"invitelink"` + Deleted bool `db:"deleted" json:"deleted"` + Createdat time.Time `db:"createdat" json:"createdat"` } type Pipeline struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 47dc7cd..54a157b 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -707,14 +707,15 @@ func (q *Queries) CreateAmoAccount(ctx context.Context, arg CreateAmoAccountPara } const createLeadTarget = `-- name: CreateLeadTarget :one -INSERT INTO leadtarget (accountID,type,quizID,target) VALUES ($1,$2,$3,$4) RETURNING id, accountid, type, quizid, target, deleted, createdat +INSERT INTO leadtarget (accountID,type,quizID,target,InviteLink) VALUES ($1,$2,$3,$4,$5) RETURNING id, accountid, type, quizid, target, invitelink, deleted, createdat ` type CreateLeadTargetParams struct { - Accountid string `db:"accountid" json:"accountid"` - Type interface{} `db:"type" json:"type"` - Quizid int32 `db:"quizid" json:"quizid"` - Target string `db:"target" json:"target"` + Accountid string `db:"accountid" json:"accountid"` + Type interface{} `db:"type" json:"type"` + Quizid int32 `db:"quizid" json:"quizid"` + Target string `db:"target" json:"target"` + Invitelink string `db:"invitelink" json:"invitelink"` } func (q *Queries) CreateLeadTarget(ctx context.Context, arg CreateLeadTargetParams) (Leadtarget, error) { @@ -723,6 +724,7 @@ func (q *Queries) CreateLeadTarget(ctx context.Context, arg CreateLeadTargetPara arg.Type, arg.Quizid, arg.Target, + arg.Invitelink, ) var i Leadtarget err := row.Scan( @@ -731,6 +733,7 @@ func (q *Queries) CreateLeadTarget(ctx context.Context, arg CreateLeadTargetPara &i.Type, &i.Quizid, &i.Target, + &i.Invitelink, &i.Deleted, &i.Createdat, ) @@ -1757,7 +1760,7 @@ func (q *Queries) GetFieldsWithPagination(ctx context.Context, arg GetFieldsWith } const getLeadTarget = `-- name: GetLeadTarget :many -SELECT id, accountid, type, quizid, target, deleted, createdat FROM leadtarget WHERE accountID = $1 AND quizID = $2 AND deleted=false +SELECT id, accountid, type, quizid, target, invitelink, deleted, createdat FROM leadtarget WHERE accountID = $1 AND quizID = $2 AND deleted=false ` type GetLeadTargetParams struct { @@ -1780,6 +1783,7 @@ func (q *Queries) GetLeadTarget(ctx context.Context, arg GetLeadTargetParams) ([ &i.Type, &i.Quizid, &i.Target, + &i.Invitelink, &i.Deleted, &i.Createdat, ); err != nil { @@ -3715,16 +3719,17 @@ func (q *Queries) UpdateFields(ctx context.Context, dollar_1 json.RawMessage) er } const updateLeadTarget = `-- name: UpdateLeadTarget :one -UPDATE leadtarget SET target = $1 WHERE id = $2 AND deleted=false RETURNING id, accountid, type, quizid, target, deleted, createdat +UPDATE leadtarget SET target = $1,InviteLink = $2 WHERE id = $3 AND deleted=false RETURNING id, accountid, type, quizid, target, invitelink, deleted, createdat ` type UpdateLeadTargetParams struct { - Target string `db:"target" json:"target"` - ID int64 `db:"id" json:"id"` + Target string `db:"target" json:"target"` + Invitelink string `db:"invitelink" json:"invitelink"` + ID int64 `db:"id" json:"id"` } func (q *Queries) UpdateLeadTarget(ctx context.Context, arg UpdateLeadTargetParams) (Leadtarget, error) { - row := q.db.QueryRowContext(ctx, updateLeadTarget, arg.Target, arg.ID) + row := q.db.QueryRowContext(ctx, updateLeadTarget, arg.Target, arg.Invitelink, arg.ID) var i Leadtarget err := row.Scan( &i.ID, @@ -3732,6 +3737,7 @@ func (q *Queries) UpdateLeadTarget(ctx context.Context, arg UpdateLeadTargetPara &i.Type, &i.Quizid, &i.Target, + &i.Invitelink, &i.Deleted, &i.Createdat, ) From 0b8534fae5b22a82d9411f1790270390f7973620 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 11 Jul 2024 16:32:42 +0300 Subject: [PATCH 41/45] added invite link to lead target repo methods --- repository/account/account.go | 59 +++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/repository/account/account.go b/repository/account/account.go index 9557f2d..163ea10 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -365,10 +365,11 @@ func (r *AccountRepository) ManualDone(ctx context.Context, userID string) error func (r *AccountRepository) PostLeadTarget(ctx context.Context, req model.LeadTarget) (model.LeadTarget, error) { row, err := r.queries.CreateLeadTarget(ctx, sqlcgen.CreateLeadTargetParams{ - Accountid: req.AccountID, - Type: req.Type, - Quizid: req.QuizID, - Target: req.Target, + Accountid: req.AccountID, + Type: req.Type, + Quizid: req.QuizID, + Target: req.Target, + Invitelink: req.InviteLink, }) if err != nil { return model.LeadTarget{}, err @@ -379,13 +380,14 @@ func (r *AccountRepository) PostLeadTarget(ctx context.Context, req model.LeadTa targetType = model.LeadTargetType(v) return model.LeadTarget{ - ID: row.ID, - AccountID: row.Accountid, - Type: targetType, - QuizID: row.Quizid, - Target: row.Target, - Deleted: row.Deleted, - CreatedAt: row.Createdat, + ID: row.ID, + AccountID: row.Accountid, + Type: targetType, + QuizID: row.Quizid, + Target: row.Target, + Deleted: row.Deleted, + CreatedAt: row.Createdat, + InviteLink: row.Invitelink, }, nil } @@ -415,13 +417,14 @@ func (r *AccountRepository) GetLeadTarget(ctx context.Context, accountID string, targetType = model.LeadTargetType(v) leadTargets = append(leadTargets, model.LeadTarget{ - ID: row.ID, - AccountID: row.Accountid, - Type: targetType, - QuizID: row.Quizid, - Target: row.Target, - Deleted: row.Deleted, - CreatedAt: row.Createdat, + ID: row.ID, + AccountID: row.Accountid, + Type: targetType, + QuizID: row.Quizid, + Target: row.Target, + Deleted: row.Deleted, + CreatedAt: row.Createdat, + InviteLink: row.Invitelink, }) } @@ -430,8 +433,9 @@ func (r *AccountRepository) GetLeadTarget(ctx context.Context, accountID string, func (r *AccountRepository) UpdateLeadTarget(ctx context.Context, req model.LeadTarget) (model.LeadTarget, error) { row, err := r.queries.UpdateLeadTarget(ctx, sqlcgen.UpdateLeadTargetParams{ - Target: req.Target, - ID: req.ID, + Invitelink: req.InviteLink, + Target: req.Target, + ID: req.ID, }) if err != nil { if err == sql.ErrNoRows { @@ -445,12 +449,13 @@ func (r *AccountRepository) UpdateLeadTarget(ctx context.Context, req model.Lead targetType = model.LeadTargetType(v) return model.LeadTarget{ - ID: row.ID, - AccountID: row.Accountid, - Type: targetType, - QuizID: row.Quizid, - Target: row.Target, - Deleted: row.Deleted, - CreatedAt: row.Createdat, + ID: row.ID, + AccountID: row.Accountid, + Type: targetType, + QuizID: row.Quizid, + Target: row.Target, + Deleted: row.Deleted, + CreatedAt: row.Createdat, + InviteLink: row.Invitelink, }, nil } From d93b846bfc870707a0b00fede983e329cf8573be Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 14 Jul 2024 10:35:20 +0300 Subject: [PATCH 42/45] added not null for tables accounnt and privilege --- dal/schema/000019_init.down.sql | 11 +++++++++++ dal/schema/000019_init.up.sql | 11 +++++++++++ sqlc.yaml | 2 ++ 3 files changed, 24 insertions(+) create mode 100644 dal/schema/000019_init.down.sql create mode 100644 dal/schema/000019_init.up.sql diff --git a/dal/schema/000019_init.down.sql b/dal/schema/000019_init.down.sql new file mode 100644 index 0000000..e76faa2 --- /dev/null +++ b/dal/schema/000019_init.down.sql @@ -0,0 +1,11 @@ +ALTER TABLE account +ALTER COLUMN user_id DROP NOT NULL, +ALTER COLUMN created_at DROP NOT NULL, +ALTER COLUMN deleted DROP NOT NULL; + +ALTER TABLE privileges +ALTER COLUMN privilegeID DROP NOT NULL, +ALTER COLUMN account_id DROP NOT NULL, +ALTER COLUMN privilege_name DROP NOT NULL, +ALTER COLUMN amount DROP NOT NULL, +ALTER COLUMN created_at DROP NOT NULL; \ No newline at end of file diff --git a/dal/schema/000019_init.up.sql b/dal/schema/000019_init.up.sql new file mode 100644 index 0000000..ac84b67 --- /dev/null +++ b/dal/schema/000019_init.up.sql @@ -0,0 +1,11 @@ +ALTER TABLE account +ALTER COLUMN user_id SET NOT NULL, +ALTER COLUMN created_at SET NOT NULL, +ALTER COLUMN deleted SET NOT NULL; + +ALTER TABLE privileges +ALTER COLUMN privilegeID SET NOT NULL, +ALTER COLUMN account_id SET NOT NULL, +ALTER COLUMN privilege_name SET NOT NULL, +ALTER COLUMN amount SET NOT NULL, +ALTER COLUMN created_at SET NOT NULL; \ No newline at end of file diff --git a/sqlc.yaml b/sqlc.yaml index c92df77..a23277c 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -38,6 +38,8 @@ packages: - "./dal/schema/000017_init.down.sql" - "./dal/schema/000018_init.up.sql" - "./dal/schema/000018_init.down.sql" + - "./dal/schema/000019_init.up.sql" + - "./dal/schema/000019_init.down.sql" engine: "postgresql" emit_json_tags: true emit_db_tags: true From 8fb3a4a1d39977c635a553e0370506f8676b746f Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 14 Jul 2024 10:39:02 +0300 Subject: [PATCH 43/45] update sqlc gen --- dal/sqlcgen/models.go | 20 +++--- dal/sqlcgen/queries.sql.go | 138 ++++++++++++++++++------------------- 2 files changed, 79 insertions(+), 79 deletions(-) diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index 1788137..c9fbf05 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -13,10 +13,10 @@ import ( ) type Account struct { - ID uuid.UUID `db:"id" json:"id"` - UserID sql.NullString `db:"user_id" json:"user_id"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` + ID uuid.UUID `db:"id" json:"id"` + UserID string `db:"user_id" json:"user_id"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + Deleted bool `db:"deleted" json:"deleted"` } type Accountsamo struct { @@ -102,12 +102,12 @@ type Pipeline struct { } type Privilege struct { - ID int32 `db:"id" json:"id"` - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` - AccountID uuid.NullUUID `db:"account_id" json:"account_id"` - PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"` - Amount sql.NullInt32 `db:"amount" json:"amount"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + ID int32 `db:"id" json:"id"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + AccountID uuid.UUID `db:"account_id" json:"account_id"` + PrivilegeName string `db:"privilege_name" json:"privilege_name"` + Amount int32 `db:"amount" json:"amount"` + CreatedAt time.Time `db:"created_at" json:"created_at"` } type Question struct { diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 54a157b..3bd32e0 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -173,9 +173,9 @@ WHERE privilege_name = $2 ` type CheckAndAddDefaultParams struct { - Amount sql.NullInt32 `db:"amount" json:"amount"` - PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"` - Amount_2 sql.NullInt32 `db:"amount_2" json:"amount_2"` + Amount int32 `db:"amount" json:"amount"` + PrivilegeName string `db:"privilege_name" json:"privilege_name"` + Amount_2 int32 `db:"amount_2" json:"amount_2"` } func (q *Queries) CheckAndAddDefault(ctx context.Context, arg CheckAndAddDefaultParams) error { @@ -655,10 +655,10 @@ INSERT INTO account (id, user_id, created_at, deleted) VALUES ($1, $2, $3, $4) R ` type CreateAccountParams struct { - ID uuid.UUID `db:"id" json:"id"` - UserID sql.NullString `db:"user_id" json:"user_id"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` + ID uuid.UUID `db:"id" json:"id"` + UserID string `db:"user_id" json:"user_id"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + Deleted bool `db:"deleted" json:"deleted"` } func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error) { @@ -799,8 +799,8 @@ RETURNING p.id, p.privilegeID, p.account_id, p.privilege_name, p.amount, p.creat ` type DecrementManualParams struct { - UserID sql.NullString `db:"user_id" json:"user_id"` - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` + UserID string `db:"user_id" json:"user_id"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` } func (q *Queries) DecrementManual(ctx context.Context, arg DecrementManualParams) (Privilege, error) { @@ -857,7 +857,7 @@ const deletePrivilegeByAccID = `-- name: DeletePrivilegeByAccID :exec DELETE FROM privileges WHERE account_id = $1 ` -func (q *Queries) DeletePrivilegeByAccID(ctx context.Context, accountID uuid.NullUUID) error { +func (q *Queries) DeletePrivilegeByAccID(ctx context.Context, accountID uuid.UUID) error { _, err := q.db.ExecContext(ctx, deletePrivilegeByAccID, accountID) return err } @@ -1279,16 +1279,16 @@ WHERE ` type GetAccAndPrivilegeByEmailRow struct { - ID uuid.UUID `db:"id" json:"id"` - UserID sql.NullString `db:"user_id" json:"user_id"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - ID_2 int32 `db:"id_2" json:"id_2"` - Privilegeid string `db:"privilegeid" json:"privilegeid"` - Amount int32 `db:"amount" json:"amount"` - CreatedAt_2 sql.NullTime `db:"created_at_2" json:"created_at_2"` + ID uuid.UUID `db:"id" json:"id"` + UserID string `db:"user_id" json:"user_id"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + ID_2 int32 `db:"id_2" json:"id_2"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + Amount int32 `db:"amount" json:"amount"` + CreatedAt_2 time.Time `db:"created_at_2" json:"created_at_2"` } -func (q *Queries) GetAccAndPrivilegeByEmail(ctx context.Context, userID sql.NullString) (GetAccAndPrivilegeByEmailRow, error) { +func (q *Queries) GetAccAndPrivilegeByEmail(ctx context.Context, userID string) (GetAccAndPrivilegeByEmailRow, error) { row := q.db.QueryRowContext(ctx, getAccAndPrivilegeByEmail, userID) var i GetAccAndPrivilegeByEmailRow err := row.Scan( @@ -1315,18 +1315,18 @@ WHERE a.user_id = $1 ` type GetAccountWithPrivilegesRow struct { - ID uuid.UUID `db:"id" json:"id"` - UserID sql.NullString `db:"user_id" json:"user_id"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - Deleted sql.NullBool `db:"deleted" json:"deleted"` - PrivilegeID int32 `db:"privilege_id" json:"privilege_id"` - Privilegeid string `db:"privilegeid" json:"privilegeid"` - PrivilegeName string `db:"privilege_name" json:"privilege_name"` - Amount int32 `db:"amount" json:"amount"` - PrivilegeCreatedAt sql.NullTime `db:"privilege_created_at" json:"privilege_created_at"` + ID uuid.UUID `db:"id" json:"id"` + UserID string `db:"user_id" json:"user_id"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + Deleted bool `db:"deleted" json:"deleted"` + PrivilegeID int32 `db:"privilege_id" json:"privilege_id"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + PrivilegeName string `db:"privilege_name" json:"privilege_name"` + Amount int32 `db:"amount" json:"amount"` + PrivilegeCreatedAt time.Time `db:"privilege_created_at" json:"privilege_created_at"` } -func (q *Queries) GetAccountWithPrivileges(ctx context.Context, userID sql.NullString) ([]GetAccountWithPrivilegesRow, error) { +func (q *Queries) GetAccountWithPrivileges(ctx context.Context, userID string) ([]GetAccountWithPrivilegesRow, error) { rows, err := q.db.QueryContext(ctx, getAccountWithPrivileges, userID) if err != nil { return nil, err @@ -1590,15 +1590,15 @@ WHERE p.amount = 0 ` type GetExpiredCountPrivilegeRow struct { - ID int32 `db:"id" json:"id"` - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` - PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"` - Amount sql.NullInt32 `db:"amount" json:"amount"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - UserID sql.NullString `db:"user_id" json:"user_id"` + ID int32 `db:"id" json:"id"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + PrivilegeName string `db:"privilege_name" json:"privilege_name"` + Amount int32 `db:"amount" json:"amount"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + UserID string `db:"user_id" json:"user_id"` } -func (q *Queries) GetExpiredCountPrivilege(ctx context.Context, privilegeid sql.NullString) ([]GetExpiredCountPrivilegeRow, error) { +func (q *Queries) GetExpiredCountPrivilege(ctx context.Context, privilegeid string) ([]GetExpiredCountPrivilegeRow, error) { rows, err := q.db.QueryContext(ctx, getExpiredCountPrivilege, privilegeid) if err != nil { return nil, err @@ -1637,15 +1637,15 @@ WHERE p.created_at + p.amount * interval '1 day' < NOW() ` type GetExpiredDayPrivilegeRow struct { - ID int32 `db:"id" json:"id"` - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` - PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"` - Amount sql.NullInt32 `db:"amount" json:"amount"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - UserID sql.NullString `db:"user_id" json:"user_id"` + ID int32 `db:"id" json:"id"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + PrivilegeName string `db:"privilege_name" json:"privilege_name"` + Amount int32 `db:"amount" json:"amount"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + UserID string `db:"user_id" json:"user_id"` } -func (q *Queries) GetExpiredDayPrivilege(ctx context.Context, privilegeid sql.NullString) ([]GetExpiredDayPrivilegeRow, error) { +func (q *Queries) GetExpiredDayPrivilege(ctx context.Context, privilegeid string) ([]GetExpiredDayPrivilegeRow, error) { rows, err := q.db.QueryContext(ctx, getExpiredDayPrivilege, privilegeid) if err != nil { return nil, err @@ -1920,14 +1920,14 @@ SELECT id,privilegeID,privilege_name,amount, created_at FROM privileges WHERE ac ` type GetPrivilegesByAccountIDRow struct { - ID int32 `db:"id" json:"id"` - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` - PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"` - Amount sql.NullInt32 `db:"amount" json:"amount"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + ID int32 `db:"id" json:"id"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + PrivilegeName string `db:"privilege_name" json:"privilege_name"` + Amount int32 `db:"amount" json:"amount"` + CreatedAt time.Time `db:"created_at" json:"created_at"` } -func (q *Queries) GetPrivilegesByAccountID(ctx context.Context, accountID uuid.NullUUID) ([]GetPrivilegesByAccountIDRow, error) { +func (q *Queries) GetPrivilegesByAccountID(ctx context.Context, accountID uuid.UUID) ([]GetPrivilegesByAccountIDRow, error) { rows, err := q.db.QueryContext(ctx, getPrivilegesByAccountID, accountID) if err != nil { return nil, err @@ -1961,14 +1961,14 @@ SELECT p.id,p.privilegeID,p.privilege_name,p.amount, p.created_at FROM privilege ` type GetPrivilegesByAccountIDWCRow struct { - ID int32 `db:"id" json:"id"` - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` - PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"` - Amount sql.NullInt32 `db:"amount" json:"amount"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + ID int32 `db:"id" json:"id"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + PrivilegeName string `db:"privilege_name" json:"privilege_name"` + Amount int32 `db:"amount" json:"amount"` + CreatedAt time.Time `db:"created_at" json:"created_at"` } -func (q *Queries) GetPrivilegesByAccountIDWC(ctx context.Context, userID sql.NullString) ([]GetPrivilegesByAccountIDWCRow, error) { +func (q *Queries) GetPrivilegesByAccountIDWC(ctx context.Context, userID string) ([]GetPrivilegesByAccountIDWCRow, error) { rows, err := q.db.QueryContext(ctx, getPrivilegesByAccountIDWC, userID) if err != nil { return nil, err @@ -2014,10 +2014,10 @@ WHERE ` type GetPrivilegesQuizAccountRow struct { - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` - PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"` - Amount sql.NullInt32 `db:"amount" json:"amount"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + PrivilegeName string `db:"privilege_name" json:"privilege_name"` + Amount int32 `db:"amount" json:"amount"` + CreatedAt time.Time `db:"created_at" json:"created_at"` ID uuid.UUID `db:"id" json:"id"` Config sql.NullString `db:"config" json:"config"` } @@ -3085,11 +3085,11 @@ INSERT INTO privileges (privilegeID, account_id, privilege_name, amount, created ` type InsertPrivilegeParams struct { - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` - AccountID uuid.NullUUID `db:"account_id" json:"account_id"` - PrivilegeName sql.NullString `db:"privilege_name" json:"privilege_name"` - Amount sql.NullInt32 `db:"amount" json:"amount"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` + AccountID uuid.UUID `db:"account_id" json:"account_id"` + PrivilegeName string `db:"privilege_name" json:"privilege_name"` + Amount int32 `db:"amount" json:"amount"` + CreatedAt time.Time `db:"created_at" json:"created_at"` } func (q *Queries) InsertPrivilege(ctx context.Context, arg InsertPrivilegeParams) error { @@ -3763,10 +3763,10 @@ UPDATE privileges SET amount = $1, created_at = $2 WHERE account_id = $3 AND pri ` type UpdatePrivilegeParams struct { - Amount sql.NullInt32 `db:"amount" json:"amount"` - CreatedAt sql.NullTime `db:"created_at" json:"created_at"` - AccountID uuid.NullUUID `db:"account_id" json:"account_id"` - Privilegeid sql.NullString `db:"privilegeid" json:"privilegeid"` + Amount int32 `db:"amount" json:"amount"` + CreatedAt time.Time `db:"created_at" json:"created_at"` + AccountID uuid.UUID `db:"account_id" json:"account_id"` + Privilegeid string `db:"privilegeid" json:"privilegeid"` } func (q *Queries) UpdatePrivilege(ctx context.Context, arg UpdatePrivilegeParams) error { @@ -3784,8 +3784,8 @@ UPDATE privileges SET amount = $1 WHERE id = $2 ` type UpdatePrivilegeAmountParams struct { - Amount sql.NullInt32 `db:"amount" json:"amount"` - ID int32 `db:"id" json:"id"` + Amount int32 `db:"amount" json:"amount"` + ID int32 `db:"id" json:"id"` } func (q *Queries) UpdatePrivilegeAmount(ctx context.Context, arg UpdatePrivilegeAmountParams) error { From dea8b7c136904976fc71ae033f90130ee100eeb8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 14 Jul 2024 10:46:28 +0300 Subject: [PATCH 44/45] update repo methods without nil pointers --- repository/account/account.go | 111 +++++++++++++++++----------------- 1 file changed, 54 insertions(+), 57 deletions(-) diff --git a/repository/account/account.go b/repository/account/account.go index 163ea10..9f10fb9 100644 --- a/repository/account/account.go +++ b/repository/account/account.go @@ -30,9 +30,7 @@ func NewAccountRepository(deps Deps) *AccountRepository { // test + func (r *AccountRepository) GetAccountByID(ctx context.Context, userID string) (model.Account, error) { - userIDSql := sql.NullString{String: userID, Valid: userID != ""} - - accountRows, err := r.queries.GetAccountWithPrivileges(ctx, userIDSql) + accountRows, err := r.queries.GetAccountWithPrivileges(ctx, userID) if err != nil { return model.Account{}, err } @@ -43,9 +41,9 @@ func (r *AccountRepository) GetAccountByID(ctx context.Context, userID string) ( for _, row := range accountRows { if account.ID == "" { account.ID = row.ID.String() - account.UserID = row.UserID.String - account.CreatedAt = row.CreatedAt.Time - account.Deleted = row.Deleted.Bool + account.UserID = row.UserID + account.CreatedAt = row.CreatedAt + account.Deleted = row.Deleted } if row.PrivilegeID != 0 { @@ -54,7 +52,7 @@ func (r *AccountRepository) GetAccountByID(ctx context.Context, userID string) ( PrivilegeID: row.Privilegeid, PrivilegeName: row.PrivilegeName, Amount: uint64(row.Amount), - CreatedAt: row.PrivilegeCreatedAt.Time, + CreatedAt: row.PrivilegeCreatedAt, } privileges[privilege.PrivilegeID] = privilege } @@ -70,9 +68,8 @@ func (r *AccountRepository) GetAccountByID(ctx context.Context, userID string) ( // test + func (r *AccountRepository) GetPrivilegesByAccountID(ctx context.Context, userID string) ([]model.ShortPrivilege, error) { - userIDSql := sql.NullString{String: userID, Valid: userID != ""} - privilegeRows, err := r.queries.GetPrivilegesByAccountIDWC(ctx, userIDSql) + privilegeRows, err := r.queries.GetPrivilegesByAccountIDWC(ctx, userID) if err != nil { return nil, err } @@ -82,10 +79,10 @@ func (r *AccountRepository) GetPrivilegesByAccountID(ctx context.Context, userID for _, row := range privilegeRows { privilege := model.ShortPrivilege{ ID: fmt.Sprintf("%d", row.ID), - PrivilegeID: row.Privilegeid.String, - PrivilegeName: row.PrivilegeName.String, - Amount: uint64(row.Amount.Int32), - CreatedAt: row.CreatedAt.Time, + PrivilegeID: row.Privilegeid, + PrivilegeName: row.PrivilegeName, + Amount: uint64(row.Amount), + CreatedAt: row.CreatedAt, } privileges = append(privileges, privilege) } @@ -98,9 +95,9 @@ func (r *AccountRepository) CreateAccount(ctx context.Context, data *model.Accou data.ID = uuid.NewString() row, err := r.queries.CreateAccount(ctx, sqlcgen.CreateAccountParams{ ID: uuid.MustParse(data.ID), - UserID: sql.NullString{String: data.UserID, Valid: data.UserID != ""}, - CreatedAt: sql.NullTime{Time: data.CreatedAt, Valid: !data.CreatedAt.IsZero()}, - Deleted: sql.NullBool{Bool: data.Deleted, Valid: true}, + UserID: data.UserID, + CreatedAt: data.CreatedAt, + Deleted: data.Deleted, }) if err != nil { return model.Account{}, fmt.Errorf("failed to create account: %w", err) @@ -108,16 +105,16 @@ func (r *AccountRepository) CreateAccount(ctx context.Context, data *model.Accou createdAccount := model.Account{ ID: row.ID.String(), - UserID: row.UserID.String, + UserID: row.UserID, } for _, privilege := range data.Privileges { err := r.queries.InsertPrivilege(ctx, sqlcgen.InsertPrivilegeParams{ - Privilegeid: sql.NullString{String: privilege.PrivilegeID, Valid: privilege.PrivilegeID != ""}, - AccountID: uuid.NullUUID{UUID: uuid.MustParse(data.ID), Valid: true}, - PrivilegeName: sql.NullString{String: privilege.PrivilegeName, Valid: privilege.PrivilegeName != ""}, - Amount: sql.NullInt32{Int32: int32(privilege.Amount), Valid: true}, - CreatedAt: sql.NullTime{Time: privilege.CreatedAt, Valid: !privilege.CreatedAt.IsZero()}, + Privilegeid: privilege.PrivilegeID, + AccountID: uuid.MustParse(data.ID), + PrivilegeName: privilege.PrivilegeName, + Amount: int32(privilege.Amount), + CreatedAt: privilege.CreatedAt, }) if err != nil { @@ -140,7 +137,7 @@ func (r *AccountRepository) DeleteAccount(ctx context.Context, accountID string) return err } - err = r.queries.DeletePrivilegeByAccID(ctx, uuid.NullUUID{UUID: uuid.MustParse(accountID), Valid: true}) + err = r.queries.DeletePrivilegeByAccID(ctx, uuid.MustParse(accountID)) if err != nil { tx.Rollback() return err @@ -164,9 +161,9 @@ func (r *AccountRepository) GetAccounts(ctx context.Context, limit uint64, offse for _, row := range rows { account := model.Account{ ID: row.ID.String(), - UserID: row.UserID.String, - CreatedAt: row.CreatedAt.Time, - Deleted: row.Deleted.Bool, + UserID: row.UserID, + CreatedAt: row.CreatedAt, + Deleted: row.Deleted, } accounts = append(accounts, account) @@ -178,10 +175,10 @@ func (r *AccountRepository) GetAccounts(ctx context.Context, limit uint64, offse // test + func (r *AccountRepository) UpdatePrivilege(ctx context.Context, privilege *model.ShortPrivilege, accountID string) error { err := r.queries.UpdatePrivilege(ctx, sqlcgen.UpdatePrivilegeParams{ - Amount: sql.NullInt32{Int32: int32(privilege.Amount), Valid: true}, - CreatedAt: sql.NullTime{Time: privilege.CreatedAt, Valid: !privilege.CreatedAt.IsZero()}, - AccountID: uuid.NullUUID{UUID: uuid.MustParse(accountID), Valid: true}, - Privilegeid: sql.NullString{String: privilege.PrivilegeID, Valid: privilege.PrivilegeID != ""}, + Amount: int32(privilege.Amount), + CreatedAt: privilege.CreatedAt, + AccountID: uuid.MustParse(accountID), + Privilegeid: privilege.PrivilegeID, }) if err != nil { @@ -194,11 +191,11 @@ func (r *AccountRepository) UpdatePrivilege(ctx context.Context, privilege *mode // test + func (r *AccountRepository) InsertPrivilege(ctx context.Context, privilege *model.ShortPrivilege, accountID string) error { err := r.queries.InsertPrivilege(ctx, sqlcgen.InsertPrivilegeParams{ - Amount: sql.NullInt32{Int32: int32(privilege.Amount), Valid: true}, - CreatedAt: sql.NullTime{Time: privilege.CreatedAt, Valid: !privilege.CreatedAt.IsZero()}, - AccountID: uuid.NullUUID{UUID: uuid.MustParse(accountID), Valid: true}, - Privilegeid: sql.NullString{String: privilege.PrivilegeID, Valid: privilege.PrivilegeID != ""}, - PrivilegeName: sql.NullString{String: privilege.PrivilegeName, Valid: privilege.PrivilegeName != ""}, + Amount: int32(privilege.Amount), + CreatedAt: privilege.CreatedAt, + AccountID: uuid.MustParse(accountID), + Privilegeid: privilege.PrivilegeID, + PrivilegeName: privilege.PrivilegeName, }) if err != nil { @@ -210,7 +207,7 @@ func (r *AccountRepository) InsertPrivilege(ctx context.Context, privilege *mode // test + func (r *AccountRepository) GetExpired(ctx context.Context, privilegeID string) ([]model.ExpiredPrivileges, error) { - rows, err := r.queries.GetExpiredDayPrivilege(ctx, sql.NullString{String: privilegeID, Valid: privilegeID != ""}) + rows, err := r.queries.GetExpiredDayPrivilege(ctx, privilegeID) if err != nil { return nil, err } @@ -220,13 +217,13 @@ func (r *AccountRepository) GetExpired(ctx context.Context, privilegeID string) for _, row := range rows { privilege := model.ShortPrivilege{ ID: fmt.Sprintf("%d", row.ID), - PrivilegeID: row.Privilegeid.String, - PrivilegeName: row.PrivilegeName.String, - Amount: uint64(row.Amount.Int32), - CreatedAt: row.CreatedAt.Time, + PrivilegeID: row.Privilegeid, + PrivilegeName: row.PrivilegeName, + Amount: uint64(row.Amount), + CreatedAt: row.CreatedAt, } expiredRecords = append(expiredRecords, model.ExpiredPrivileges{ - UserID: row.UserID.String, + UserID: row.UserID, Privilege: privilege, }) @@ -236,7 +233,7 @@ func (r *AccountRepository) GetExpired(ctx context.Context, privilegeID string) } func (r *AccountRepository) GetExpiredCount(ctx context.Context, privilegeID string) ([]model.ExpiredPrivileges, error) { - rows, err := r.queries.GetExpiredCountPrivilege(ctx, sql.NullString{String: privilegeID, Valid: privilegeID != ""}) + rows, err := r.queries.GetExpiredCountPrivilege(ctx, privilegeID) if err != nil { return nil, err } @@ -246,13 +243,13 @@ func (r *AccountRepository) GetExpiredCount(ctx context.Context, privilegeID str for _, row := range rows { privilege := model.ShortPrivilege{ ID: fmt.Sprintf("%d", row.ID), - PrivilegeID: row.Privilegeid.String, - PrivilegeName: row.PrivilegeName.String, - Amount: uint64(row.Amount.Int32), - CreatedAt: row.CreatedAt.Time, + PrivilegeID: row.Privilegeid, + PrivilegeName: row.PrivilegeName, + Amount: uint64(row.Amount), + CreatedAt: row.CreatedAt, } expiredRecords = append(expiredRecords, model.ExpiredPrivileges{ - UserID: row.UserID.String, + UserID: row.UserID, Privilege: privilege, }) @@ -263,9 +260,9 @@ func (r *AccountRepository) GetExpiredCount(ctx context.Context, privilegeID str func (r *AccountRepository) CheckAndAddDefault(ctx context.Context, amount uint64, privilegeID string, zeroAmount uint64) error { err := r.queries.CheckAndAddDefault(ctx, sqlcgen.CheckAndAddDefaultParams{ - Amount: sql.NullInt32{Int32: int32(amount), Valid: true}, - PrivilegeName: sql.NullString{String: privilegeID, Valid: privilegeID != ""}, - Amount_2: sql.NullInt32{Int32: int32(zeroAmount), Valid: true}, + Amount: int32(amount), + PrivilegeName: privilegeID, + Amount_2: int32(zeroAmount), }) if err != nil { @@ -293,7 +290,7 @@ func (r *AccountRepository) UpdatePrivilegeAmount(ctx context.Context, ID string } err = r.queries.UpdatePrivilegeAmount(ctx, sqlcgen.UpdatePrivilegeAmountParams{ - Amount: sql.NullInt32{Int32: int32(newAmount), Valid: true}, + Amount: int32(newAmount), ID: int32(intID), }) @@ -309,21 +306,21 @@ func (r *AccountRepository) GetAccAndPrivilegeByEmail(ctx context.Context, email var account model.Account var privileges []model.ShortPrivilege - row, err := r.queries.GetAccAndPrivilegeByEmail(ctx, sql.NullString{String: email, Valid: true}) + row, err := r.queries.GetAccAndPrivilegeByEmail(ctx, email) if err != nil { return account, privileges, err } account.ID = row.ID.String() - account.UserID = row.UserID.String - account.CreatedAt = row.CreatedAt.Time + account.UserID = row.UserID + account.CreatedAt = row.CreatedAt if row.ID_2 != 0 { privilege := model.ShortPrivilege{ ID: fmt.Sprint(row.ID_2), PrivilegeID: row.Privilegeid, Amount: uint64(row.Amount), - CreatedAt: row.CreatedAt_2.Time, + CreatedAt: row.CreatedAt_2, } privileges = append(privileges, privilege) } @@ -349,8 +346,8 @@ func (r *AccountRepository) GetQidOwner(ctx context.Context, qId string) (string func (r *AccountRepository) ManualDone(ctx context.Context, userID string) error { _, err := r.queries.DecrementManual(ctx, sqlcgen.DecrementManualParams{ - UserID: sql.NullString{String: userID, Valid: true}, - Privilegeid: sql.NullString{String: "quizManual", Valid: true}, + UserID: userID, + Privilegeid: "quizManual", }) if err != nil { From bc61e20eba19e8fe7e5ac93c3571632480d66a13 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 16 Jul 2024 17:18:35 +0300 Subject: [PATCH 45/45] fix type --- model/model.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/model/model.go b/model/model.go index 5477142..5db3a91 100644 --- a/model/model.go +++ b/model/model.go @@ -322,9 +322,9 @@ type LeadTarget struct { type LeadTargetType string const ( - LeadTargetEmail LeadTargetType = "mail" - LeadTargetTg LeadTargetType = "telegram" - LeadTargetTgWhatsapp LeadTargetType = "whatsapp" + LeadTargetEmail LeadTargetType = "mail" + LeadTargetTg LeadTargetType = "telegram" + LeadTargetWhatsapp LeadTargetType = "whatsapp" ) var ValidLeadTargetTypes = map[string]bool{