diff --git a/dal/db_query/queries.sql b/dal/db_query/queries.sql index f84d215..716d1c7 100644 --- a/dal/db_query/queries.sql +++ b/dal/db_query/queries.sql @@ -1465,4 +1465,13 @@ INSERT INTO bitrixContact (AccountID, BitrixID, Field) VALUES ($1, $2, $3) RETUR UPDATE bitrixContact SET Field = $1,BitrixID=$3 WHERE ID = $2; -- name: UpdateGigaChatQuizFlag :exec -UPDATE quiz SET gigachat = true where id = $1 AND accountid = $2 AND deleted = false; \ No newline at end of file +UPDATE quiz SET gigachat = true where id = $1 AND accountid = $2 AND deleted = false; + +-- name: GetAllQuizUtms :many +SELECT * from quiz_utm where quizID = $1 and deleted=false; + +-- name: CreateQuizUtm :one +INSERT into quiz_utm (quizID,utm) values ($1,$2) RETURNING *; + +-- name: SoftDeleteQuizUtm :exec +UPDATE quiz_utm set deleted = true where id = $1; \ No newline at end of file diff --git a/dal/schema/000025_init.down.sql b/dal/schema/000025_init.down.sql new file mode 100644 index 0000000..b1b24af --- /dev/null +++ b/dal/schema/000025_init.down.sql @@ -0,0 +1 @@ +drop table if exists quiz_utm; \ No newline at end of file diff --git a/dal/schema/000025_init.up.sql b/dal/schema/000025_init.up.sql new file mode 100644 index 0000000..a36273f --- /dev/null +++ b/dal/schema/000025_init.up.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS quiz_utm ( + id bigserial UNIQUE NOT NULL PRIMARY KEY, + quizID bigint not null, + utm text not null default '', + deleted boolean not null default false, + created_at TIMESTAMP not null DEFAULT CURRENT_TIMESTAMP +); \ No newline at end of file diff --git a/dal/sqlcgen/models.go b/dal/sqlcgen/models.go index d4f2673..f90abbc 100644 --- a/dal/sqlcgen/models.go +++ b/dal/sqlcgen/models.go @@ -259,6 +259,14 @@ type Quiz struct { Gigachat bool `db:"gigachat" json:"gigachat"` } +type QuizUtm struct { + ID int64 `db:"id" json:"id"` + Quizid int64 `db:"quizid" json:"quizid"` + Utm string `db:"utm" json:"utm"` + Deleted bool `db:"deleted" json:"deleted"` + CreatedAt time.Time `db:"created_at" json:"created_at"` +} + type RespondentState struct { ID int64 `db:"id" json:"id"` TelegramID int32 `db:"telegram_id" json:"telegram_id"` diff --git a/dal/sqlcgen/queries.sql.go b/dal/sqlcgen/queries.sql.go index 07e1065..647a059 100644 --- a/dal/sqlcgen/queries.sql.go +++ b/dal/sqlcgen/queries.sql.go @@ -1190,6 +1190,28 @@ func (q *Queries) CreateQuizAudience(ctx context.Context, arg CreateQuizAudience return id, err } +const createQuizUtm = `-- name: CreateQuizUtm :one +INSERT into quiz_utm (quizID,utm) values ($1,$2) RETURNING id, quizid, utm, deleted, created_at +` + +type CreateQuizUtmParams struct { + Quizid int64 `db:"quizid" json:"quizid"` + Utm string `db:"utm" json:"utm"` +} + +func (q *Queries) CreateQuizUtm(ctx context.Context, arg CreateQuizUtmParams) (QuizUtm, error) { + row := q.db.QueryRowContext(ctx, createQuizUtm, arg.Quizid, arg.Utm) + var i QuizUtm + err := row.Scan( + &i.ID, + &i.Quizid, + &i.Utm, + &i.Deleted, + &i.CreatedAt, + ) + return i, err +} + const createTgAccount = `-- name: CreateTgAccount :one INSERT INTO tgAccounts (ApiID, ApiHash, PhoneNumber,Password, Status) VALUES ($1, $2, $3, $4, $5) RETURNING id @@ -2005,6 +2027,39 @@ func (q *Queries) GetAllCompanyUsers(ctx context.Context, amoid int32) ([]Usersa return items, nil } +const getAllQuizUtms = `-- name: GetAllQuizUtms :many +SELECT id, quizid, utm, deleted, created_at from quiz_utm where quizID = $1 and deleted=false +` + +func (q *Queries) GetAllQuizUtms(ctx context.Context, quizid int64) ([]QuizUtm, error) { + rows, err := q.db.QueryContext(ctx, getAllQuizUtms, quizid) + if err != nil { + return nil, err + } + defer rows.Close() + var items []QuizUtm + for rows.Next() { + var i QuizUtm + if err := rows.Scan( + &i.ID, + &i.Quizid, + &i.Utm, + &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 getAllTgAccounts = `-- name: GetAllTgAccounts :many SELECT id, apiid, apihash, phonenumber, password, status, deleted, createdat FROM tgAccounts WHERE Deleted = false ` @@ -5019,6 +5074,15 @@ func (q *Queries) SoftDeleteBitrixAccount(ctx context.Context, accountid string) return err } +const softDeleteQuizUtm = `-- name: SoftDeleteQuizUtm :exec +UPDATE quiz_utm set deleted = true where id = $1 +` + +func (q *Queries) SoftDeleteQuizUtm(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, softDeleteQuizUtm, id) + return err +} + const softDeleteResultByID = `-- name: SoftDeleteResultByID :exec UPDATE answer SET deleted = TRUE WHERE id = $1 AND deleted = FALSE ` diff --git a/model/model.go b/model/model.go index 4c12ee3..8b908ec 100644 --- a/model/model.go +++ b/model/model.go @@ -349,3 +349,11 @@ var ValidLeadTargetTypes = map[string]bool{ "telegram": true, "whatsapp": true, } + +type QuizUTM struct { + ID int64 `json:"id"` + QuizID int64 `json:"quizID"` + UTM string `json:"utm"` + Deleted bool `json:"deleted"` + CreatedAt time.Time `json:"createdAt"` +} diff --git a/repository/quiz/quiz.go b/repository/quiz/quiz.go index a029012..d34156c 100644 --- a/repository/quiz/quiz.go +++ b/repository/quiz/quiz.go @@ -766,3 +766,46 @@ func (r *QuizRepository) UpdateGigaChatQuizFlag(ctx context.Context, quizID int6 } return nil } + +func (r *QuizRepository) CreateQuizUTM(ctx context.Context, quizID int64, utm string) (model.QuizUTM, error) { + result, err := r.queries.CreateQuizUtm(ctx, sqlcgen.CreateQuizUtmParams{ + Quizid: quizID, + Utm: utm, + }) + if err != nil { + return model.QuizUTM{}, err + } + + return model.QuizUTM{ + ID: result.ID, + QuizID: result.Quizid, + UTM: result.Utm, + Deleted: result.Deleted, + CreatedAt: result.CreatedAt, + }, nil +} + +func (r *QuizRepository) DeleteQuizUTM(ctx context.Context, utmID int64) error { + return r.queries.SoftDeleteQuizUtm(ctx, utmID) +} + +func (r *QuizRepository) GetAllQuizUtms(ctx context.Context, quizID int64) ([]model.QuizUTM, error) { + rows, err := r.queries.GetAllQuizUtms(ctx, quizID) + if err != nil { + return nil, err + } + + result := make([]model.QuizUTM, 0, len(rows)) + + for _, row := range rows { + result = append(result, model.QuizUTM{ + ID: row.ID, + QuizID: row.Quizid, + UTM: row.Utm, + Deleted: row.Deleted, + CreatedAt: row.CreatedAt, + }) + } + + return result, nil +} diff --git a/sqlc.yaml b/sqlc.yaml index 1bfd7dd..7a04234 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -50,6 +50,8 @@ packages: - "./dal/schema/000023_init.down.sql" - "./dal/schema/000024_init.up.sql" - "./dal/schema/000024_init.down.sql" + - "./dal/schema/000025_init.up.sql" + - "./dal/schema/000025_init.down.sql" engine: "postgresql" emit_json_tags: true emit_db_tags: true