2024-02-19 16:33:15 +00:00
|
|
|
// Code generated by sqlc. DO NOT EDIT.
|
|
|
|
// versions:
|
2024-04-16 11:07:18 +00:00
|
|
|
// sqlc v1.26.0
|
2024-02-19 16:33:15 +00:00
|
|
|
// source: queries.sql
|
|
|
|
|
|
|
|
package sqlcgen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-04-18 10:16:03 +00:00
|
|
|
"encoding/json"
|
2024-03-16 19:56:08 +00:00
|
|
|
"time"
|
2024-02-19 16:33:15 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
const accountPagination = `-- name: AccountPagination :many
|
|
|
|
SELECT a.id, a.user_id, a.created_at, a.deleted
|
|
|
|
FROM account a ORDER BY a.created_at DESC LIMIT $1 OFFSET $2
|
|
|
|
`
|
|
|
|
|
|
|
|
type AccountPaginationParams struct {
|
|
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
|
|
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) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, accountPagination, arg.Limit, arg.Offset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []AccountPaginationRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i AccountPaginationRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.UserID,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.Deleted,
|
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-03-25 08:34:07 +00:00
|
|
|
const allServiceStatistics = `-- name: AllServiceStatistics :one
|
|
|
|
WITH Registrations AS (
|
|
|
|
SELECT COUNT(*) AS registration_count
|
|
|
|
FROM account
|
|
|
|
WHERE created_at >= to_timestamp($1) AND created_at <= to_timestamp($2)
|
|
|
|
),
|
|
|
|
Quizes AS (
|
|
|
|
SELECT COUNT(*) AS quiz_count
|
|
|
|
FROM quiz
|
|
|
|
WHERE deleted = false AND created_at >= to_timestamp($1) AND created_at <= to_timestamp($2)
|
|
|
|
),
|
|
|
|
Results AS (
|
|
|
|
SELECT COUNT(*) AS result_count
|
|
|
|
FROM answer
|
|
|
|
WHERE result = true AND created_at >= to_timestamp($1) AND created_at <= to_timestamp($2)
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
(SELECT registration_count FROM Registrations) AS registrations,
|
|
|
|
(SELECT quiz_count FROM Quizes) AS quizes,
|
|
|
|
(SELECT result_count FROM Results) AS results
|
|
|
|
`
|
|
|
|
|
|
|
|
type AllServiceStatisticsParams struct {
|
|
|
|
ToTimestamp float64 `db:"to_timestamp" json:"to_timestamp"`
|
|
|
|
ToTimestamp_2 float64 `db:"to_timestamp_2" json:"to_timestamp_2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AllServiceStatisticsRow struct {
|
|
|
|
Registrations int64 `db:"registrations" json:"registrations"`
|
|
|
|
Quizes int64 `db:"quizes" json:"quizes"`
|
|
|
|
Results int64 `db:"results" json:"results"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) AllServiceStatistics(ctx context.Context, arg AllServiceStatisticsParams) (AllServiceStatisticsRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, allServiceStatistics, arg.ToTimestamp, arg.ToTimestamp_2)
|
|
|
|
var i AllServiceStatisticsRow
|
|
|
|
err := row.Scan(&i.Registrations, &i.Quizes, &i.Results)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const archiveQuiz = `-- name: ArchiveQuiz :exec
|
|
|
|
UPDATE quiz SET archived = true WHERE id=$1 AND accountId=$2
|
|
|
|
`
|
|
|
|
|
|
|
|
type ArchiveQuizParams struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) ArchiveQuiz(ctx context.Context, arg ArchiveQuizParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, archiveQuiz, arg.ID, arg.Accountid)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkAndAddDefault = `-- name: CheckAndAddDefault :exec
|
|
|
|
UPDATE privileges
|
|
|
|
SET amount = $1, created_at = NOW()
|
|
|
|
WHERE privilege_name = $2
|
|
|
|
AND (amount < $3 OR created_at <= NOW() - INTERVAL '1 month')
|
|
|
|
`
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CheckAndAddDefault(ctx context.Context, arg CheckAndAddDefaultParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, checkAndAddDefault, arg.Amount, arg.PrivilegeName, arg.Amount_2)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-17 17:18:52 +00:00
|
|
|
const checkExpired = `-- name: CheckExpired :many
|
2024-04-18 10:16:03 +00:00
|
|
|
SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokens WHERE Expiration <= TO_TIMESTAMP(EXTRACT(EPOCH FROM NOW()) + (10 * 60))
|
2024-04-17 17:18:52 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) CheckExpired(ctx context.Context) ([]Token, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, checkExpired)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []Token
|
|
|
|
for rows.Next() {
|
|
|
|
var i Token
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Refreshtoken,
|
|
|
|
&i.Accesstoken,
|
|
|
|
&i.Authcode,
|
|
|
|
&i.Expiration,
|
|
|
|
&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
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:02:20 +00:00
|
|
|
const checkFields = `-- name: CheckFields :many
|
|
|
|
WITH user_data AS (
|
|
|
|
SELECT AmoID
|
|
|
|
FROM users
|
|
|
|
WHERE users.AccountID = $1
|
|
|
|
), new_fields AS (
|
2024-04-19 08:31:54 +00:00
|
|
|
SELECT (field->>'AmoID')::INT AS amoID,
|
|
|
|
COALESCE(field->>'Code', '')::varchar(255) AS code,
|
|
|
|
COALESCE(field->>'Name', '')::varchar(50) AS name,
|
|
|
|
CAST(field->>'Entity' AS entitytype) AS Entity,
|
|
|
|
COALESCE(field->>'Type', '')::varchar(50) AS type,
|
|
|
|
CURRENT_TIMESTAMP AS createdAt
|
2024-04-19 08:02:20 +00:00
|
|
|
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,
|
2024-04-19 08:31:54 +00:00
|
|
|
nf.createdAt
|
2024-04-19 08:02:20 +00:00
|
|
|
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 id, amoid, code, accountid, name, entity, type, deleted, createdat from inserted_fields
|
|
|
|
`
|
|
|
|
|
|
|
|
type CheckFieldsParams struct {
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
Column2 json.RawMessage `db:"column_2" json:"column_2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CheckFieldsRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Code string `db:"code" json:"code"`
|
|
|
|
Accountid int32 `db:"accountid" json:"accountid"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Entity interface{} `db:"entity" json:"entity"`
|
|
|
|
Type string `db:"type" json:"type"`
|
|
|
|
Deleted bool `db:"deleted" json:"deleted"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CheckFields(ctx context.Context, arg CheckFieldsParams) ([]CheckFieldsRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, checkFields, arg.Accountid, arg.Column2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []CheckFieldsRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i CheckFieldsRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Amoid,
|
|
|
|
&i.Code,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Name,
|
|
|
|
&i.Entity,
|
|
|
|
&i.Type,
|
|
|
|
&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
|
|
|
|
}
|
|
|
|
|
2024-04-20 09:01:25 +00:00
|
|
|
const checkMainUser = `-- name: CheckMainUser :exec
|
|
|
|
UPDATE users SET Name = $1, "Group" = $2, Email = $3, Role = $4 WHERE AmoID = $5
|
|
|
|
`
|
|
|
|
|
|
|
|
type CheckMainUserParams struct {
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Group int32 `db:"Group" json:"Group"`
|
|
|
|
Email string `db:"email" json:"email"`
|
|
|
|
Role int32 `db:"role" json:"role"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CheckMainUser(ctx context.Context, arg CheckMainUserParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, checkMainUser,
|
|
|
|
arg.Name,
|
|
|
|
arg.Group,
|
|
|
|
arg.Email,
|
|
|
|
arg.Role,
|
|
|
|
arg.Amoid,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:02:20 +00:00
|
|
|
const checkPipelines = `-- name: CheckPipelines :many
|
|
|
|
WITH new_pipelines AS (
|
2024-04-19 08:31:54 +00:00
|
|
|
SELECT (pipeline->>'AmoID')::INT AS amoID,
|
|
|
|
(pipeline->>'AccountID')::INT AS accountID,
|
|
|
|
COALESCE(pipeline->>'Name', '')::varchar(50) AS name,
|
|
|
|
CASE WHEN (pipeline->>'IsArchive') = 'true' THEN TRUE ELSE FALSE END AS isArchive,
|
|
|
|
CURRENT_TIMESTAMP AS createdAt
|
2024-04-19 08:02:20 +00:00
|
|
|
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,
|
2024-04-19 08:31:54 +00:00
|
|
|
np.createdAt
|
2024-04-19 08:02:20 +00:00
|
|
|
FROM new_pipelines np
|
|
|
|
ON CONFLICT (amoID, accountID) DO NOTHING
|
|
|
|
RETURNING id, amoid, accountid, name, isarchive, deleted, createdat
|
|
|
|
)
|
|
|
|
SELECT id, amoid, accountid, name, isarchive, deleted, createdat FROM inserted_pipelines
|
|
|
|
`
|
|
|
|
|
|
|
|
type CheckPipelinesRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Accountid int32 `db:"accountid" json:"accountid"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Isarchive bool `db:"isarchive" json:"isarchive"`
|
|
|
|
Deleted bool `db:"deleted" json:"deleted"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CheckPipelines(ctx context.Context, dollar_1 json.RawMessage) ([]CheckPipelinesRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, checkPipelines, dollar_1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []CheckPipelinesRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i CheckPipelinesRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Amoid,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Name,
|
|
|
|
&i.Isarchive,
|
|
|
|
&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
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const checkResultOwner = `-- name: CheckResultOwner :one
|
2024-03-15 11:32:26 +00:00
|
|
|
SELECT q.accountid FROM answer a JOIN quiz q ON a.quiz_id = q.id WHERE a.id = $1 AND a.deleted = FALSE AND a.start = false
|
2024-02-19 16:33:15 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) CheckResultOwner(ctx context.Context, id int64) (string, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, checkResultOwner, id)
|
|
|
|
var accountid string
|
|
|
|
err := row.Scan(&accountid)
|
|
|
|
return accountid, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkResultsOwner = `-- name: CheckResultsOwner :many
|
|
|
|
SELECT a.id
|
|
|
|
FROM answer a
|
|
|
|
JOIN quiz q ON a.quiz_id = q.id
|
2024-03-15 11:32:26 +00:00
|
|
|
WHERE a.id = ANY($1::bigint[]) AND a.deleted = FALSE AND q.accountid = $2 AND a.start = false
|
2024-02-19 16:33:15 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type CheckResultsOwnerParams struct {
|
|
|
|
Column1 []int64 `db:"column_1" json:"column_1"`
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CheckResultsOwner(ctx context.Context, arg CheckResultsOwnerParams) ([]int64, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, checkResultsOwner, pq.Array(arg.Column1), arg.Accountid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []int64
|
|
|
|
for rows.Next() {
|
|
|
|
var id int64
|
|
|
|
if err := rows.Scan(&id); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
items = append(items, id)
|
|
|
|
}
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:02:20 +00:00
|
|
|
const checkSteps = `-- name: CheckSteps :many
|
|
|
|
WITH new_steps AS (
|
2024-04-19 08:31:54 +00:00
|
|
|
SELECT (step->>'AmoID')::INT AS amoID,
|
|
|
|
(step->>'PipelineID')::INT AS pipelineID,
|
|
|
|
(step->>'AccountID')::INT AS accountID,
|
|
|
|
COALESCE(step->>'Name', '')::varchar(50) AS name,
|
|
|
|
COALESCE(step->>'Color', '')::varchar(50) AS color,
|
|
|
|
CURRENT_TIMESTAMP AS createdAt
|
2024-04-19 08:02:20 +00:00
|
|
|
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,
|
2024-04-19 08:31:54 +00:00
|
|
|
ns.createdAt
|
2024-04-19 08:02:20 +00:00
|
|
|
FROM new_steps ns
|
|
|
|
ON CONFLICT (amoID, accountID, PipelineID) DO NOTHING
|
|
|
|
RETURNING id, amoid, pipelineid, accountid, name, color, deleted, createdat
|
|
|
|
)
|
|
|
|
SELECT id, amoid, pipelineid, accountid, name, color, deleted, createdat FROM inserted_steps
|
|
|
|
`
|
|
|
|
|
|
|
|
type CheckStepsRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Pipelineid int32 `db:"pipelineid" json:"pipelineid"`
|
|
|
|
Accountid int32 `db:"accountid" json:"accountid"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Color string `db:"color" json:"color"`
|
|
|
|
Deleted bool `db:"deleted" json:"deleted"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CheckSteps(ctx context.Context, dollar_1 json.RawMessage) ([]CheckStepsRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, checkSteps, dollar_1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []CheckStepsRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i CheckStepsRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Amoid,
|
|
|
|
&i.Pipelineid,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Name,
|
|
|
|
&i.Color,
|
|
|
|
&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
|
|
|
|
}
|
|
|
|
|
2024-04-18 10:16:03 +00:00
|
|
|
const checkTags = `-- name: CheckTags :many
|
|
|
|
WITH user_data AS (
|
|
|
|
SELECT AmoID
|
|
|
|
FROM users
|
|
|
|
WHERE users.AccountID = $1
|
2024-04-18 20:05:49 +00:00
|
|
|
), new_tags AS (
|
|
|
|
SELECT (tag->>'AmoID')::INT AS amoID,
|
|
|
|
(tag->>'Entity')::entitytype AS Entity,
|
|
|
|
COALESCE(tag->>'Name', '')::VARCHAR(50) AS name,
|
|
|
|
COALESCE(tag->>'Color', '')::VARCHAR(50) AS color
|
|
|
|
FROM json_array_elements($2::json) AS tag
|
|
|
|
), inserted_tags AS (
|
|
|
|
INSERT INTO tags (amoID, accountID, Entity, name, color, createdAt)
|
|
|
|
SELECT nt.amoID,
|
|
|
|
ud.AmoID,
|
|
|
|
nt.Entity,
|
|
|
|
nt.name,
|
|
|
|
nt.color,
|
|
|
|
CURRENT_TIMESTAMP
|
|
|
|
FROM new_tags nt
|
|
|
|
JOIN user_data ud ON true
|
|
|
|
ON CONFLICT (amoID, accountID, Entity) DO NOTHING
|
|
|
|
RETURNING id, amoid, accountid, entity, name, color, deleted, createdat
|
2024-04-18 10:16:03 +00:00
|
|
|
)
|
2024-04-18 20:05:49 +00:00
|
|
|
SELECT id, amoid, accountid, entity, name, color, deleted, createdat FROM inserted_tags
|
2024-04-18 10:16:03 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type CheckTagsParams struct {
|
2024-04-18 13:50:25 +00:00
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
Column2 json.RawMessage `db:"column_2" json:"column_2"`
|
2024-04-18 10:16:03 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 20:05:49 +00:00
|
|
|
type CheckTagsRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Accountid int32 `db:"accountid" json:"accountid"`
|
|
|
|
Entity interface{} `db:"entity" json:"entity"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Color string `db:"color" json:"color"`
|
|
|
|
Deleted bool `db:"deleted" json:"deleted"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CheckTags(ctx context.Context, arg CheckTagsParams) ([]CheckTagsRow, error) {
|
2024-04-18 13:50:25 +00:00
|
|
|
rows, err := q.db.QueryContext(ctx, checkTags, arg.Accountid, arg.Column2)
|
2024-04-18 10:16:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2024-04-18 20:05:49 +00:00
|
|
|
var items []CheckTagsRow
|
2024-04-18 10:16:03 +00:00
|
|
|
for rows.Next() {
|
2024-04-18 20:05:49 +00:00
|
|
|
var i CheckTagsRow
|
2024-04-18 10:16:03 +00:00
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Amoid,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Entity,
|
|
|
|
&i.Name,
|
|
|
|
&i.Color,
|
|
|
|
&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
|
|
|
|
}
|
|
|
|
|
2024-04-17 17:18:52 +00:00
|
|
|
const checkUsers = `-- name: CheckUsers :exec
|
2024-04-20 09:01:25 +00:00
|
|
|
INSERT INTO users (AmoID, Name, Email, Role, "Group", AmoUserID)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
|
|
ON CONFLICT (AmoID) DO NOTHING
|
2024-04-17 17:18:52 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type CheckUsersParams struct {
|
2024-04-20 09:01:25 +00:00
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Email string `db:"email" json:"email"`
|
|
|
|
Role int32 `db:"role" json:"role"`
|
|
|
|
Group int32 `db:"Group" json:"Group"`
|
|
|
|
Amouserid int32 `db:"amouserid" json:"amouserid"`
|
2024-04-17 17:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CheckUsers(ctx context.Context, arg CheckUsersParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, checkUsers,
|
2024-04-20 09:01:25 +00:00
|
|
|
arg.Amoid,
|
2024-04-17 17:18:52 +00:00
|
|
|
arg.Name,
|
|
|
|
arg.Email,
|
|
|
|
arg.Role,
|
2024-04-20 09:01:25 +00:00
|
|
|
arg.Group,
|
|
|
|
arg.Amouserid,
|
2024-04-17 17:18:52 +00:00
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const copyQuestion = `-- name: CopyQuestion :one
|
|
|
|
INSERT INTO question(
|
|
|
|
quiz_id, title, description, questiontype, required,
|
|
|
|
page, content, version, parent_ids
|
|
|
|
)
|
|
|
|
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
|
|
|
|
`
|
|
|
|
|
|
|
|
type CopyQuestionParams struct {
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CopyQuestionRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
|
|
|
|
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CopyQuestion(ctx context.Context, arg CopyQuestionParams) (CopyQuestionRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, copyQuestion, arg.QuizID, arg.ID)
|
|
|
|
var i CopyQuestionRow
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.QuizID,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2024-03-19 16:13:03 +00:00
|
|
|
const copyQuestionQuizID = `-- name: CopyQuestionQuizID :exec
|
|
|
|
INSERT INTO question (
|
|
|
|
quiz_id, title, description, questiontype, required,
|
|
|
|
page, content, version, parent_ids, created_at, updated_at
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
$2, title, description, questiontype, required,
|
|
|
|
page, content, version, parent_ids, created_at, updated_at
|
|
|
|
FROM
|
|
|
|
question
|
|
|
|
WHERE
|
|
|
|
question.quiz_id = $1 AND deleted = false
|
|
|
|
`
|
|
|
|
|
|
|
|
type CopyQuestionQuizIDParams struct {
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
QuizID_2 int64 `db:"quiz_id_2" json:"quiz_id_2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CopyQuestionQuizID(ctx context.Context, arg CopyQuestionQuizIDParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, copyQuestionQuizID, arg.QuizID, arg.QuizID_2)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const copyQuiz = `-- name: CopyQuiz :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 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 id, qid,created_at, updated_at
|
|
|
|
`
|
|
|
|
|
|
|
|
type CopyQuizParams struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CopyQuizRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Qid uuid.NullUUID `db:"qid" json:"qid"`
|
|
|
|
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
|
|
|
|
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CopyQuiz(ctx context.Context, arg CopyQuizParams) (CopyQuizRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, copyQuiz, arg.ID, arg.Accountid)
|
|
|
|
var i CopyQuizRow
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Qid,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const copyQuizQuestions = `-- name: CopyQuizQuestions :exec
|
|
|
|
INSERT INTO question(
|
|
|
|
quiz_id, title, description, questiontype, required, page, content, version, parent_ids
|
|
|
|
)
|
|
|
|
SELECT $2, title, description, questiontype, required, page, content, version, parent_ids
|
|
|
|
FROM question WHERE question.quiz_id=$1 AND deleted=false
|
|
|
|
`
|
|
|
|
|
|
|
|
type CopyQuizQuestionsParams struct {
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
QuizID_2 int64 `db:"quiz_id_2" json:"quiz_id_2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CopyQuizQuestions(ctx context.Context, arg CopyQuizQuestionsParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, copyQuizQuestions, arg.QuizID, arg.QuizID_2)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const createAccount = `-- name: CreateAccount :exec
|
|
|
|
INSERT INTO account (id, user_id, email, created_at, deleted) VALUES ($1, $2, $3, $4, $5)
|
|
|
|
`
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, createAccount,
|
|
|
|
arg.ID,
|
|
|
|
arg.UserID,
|
|
|
|
arg.Email,
|
|
|
|
arg.CreatedAt,
|
|
|
|
arg.Deleted,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-17 17:18:52 +00:00
|
|
|
const createAmoAccount = `-- name: CreateAmoAccount :exec
|
|
|
|
|
|
|
|
INSERT INTO users (AccountID, AmoID, Name, Email, Role, "Group", Deleted, CreatedAt, Subdomain, AmoUserID, Country)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
|
|
|
`
|
|
|
|
|
|
|
|
type CreateAmoAccountParams struct {
|
2024-04-20 09:01:25 +00:00
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Email string `db:"email" json:"email"`
|
|
|
|
Role int32 `db:"role" json:"role"`
|
|
|
|
Group int32 `db:"Group" json:"Group"`
|
|
|
|
Deleted bool `db:"deleted" json:"deleted"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
Subdomain string `db:"subdomain" json:"subdomain"`
|
|
|
|
Amouserid int32 `db:"amouserid" json:"amouserid"`
|
|
|
|
Country string `db:"country" json:"country"`
|
2024-04-17 17:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// amo methods:
|
|
|
|
func (q *Queries) CreateAmoAccount(ctx context.Context, arg CreateAmoAccountParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, createAmoAccount,
|
|
|
|
arg.Accountid,
|
|
|
|
arg.Amoid,
|
|
|
|
arg.Name,
|
|
|
|
arg.Email,
|
|
|
|
arg.Role,
|
|
|
|
arg.Group,
|
|
|
|
arg.Deleted,
|
|
|
|
arg.Createdat,
|
|
|
|
arg.Subdomain,
|
|
|
|
arg.Amouserid,
|
|
|
|
arg.Country,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const createWebHook = `-- name: CreateWebHook :exec
|
|
|
|
INSERT INTO tokens (AccountID, RefreshToken, AccessToken, AuthCode, Expiration, CreatedAt)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
|
|
`
|
|
|
|
|
|
|
|
type CreateWebHookParams struct {
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
Refreshtoken string `db:"refreshtoken" json:"refreshtoken"`
|
|
|
|
Accesstoken string `db:"accesstoken" json:"accesstoken"`
|
|
|
|
Authcode string `db:"authcode" json:"authcode"`
|
|
|
|
Expiration time.Time `db:"expiration" json:"expiration"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CreateWebHook(ctx context.Context, arg CreateWebHookParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, createWebHook,
|
|
|
|
arg.Accountid,
|
|
|
|
arg.Refreshtoken,
|
|
|
|
arg.Accesstoken,
|
|
|
|
arg.Authcode,
|
|
|
|
arg.Expiration,
|
|
|
|
arg.Createdat,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const deleteAccountById = `-- name: DeleteAccountById :exec
|
|
|
|
DELETE FROM account WHERE id = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) DeleteAccountById(ctx context.Context, id uuid.UUID) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, deleteAccountById, id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const deletePrivilegeByAccID = `-- name: DeletePrivilegeByAccID :exec
|
|
|
|
DELETE FROM privileges WHERE account_id = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) DeletePrivilegeByAccID(ctx context.Context, accountID uuid.NullUUID) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, deletePrivilegeByAccID, accountID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const deletePrivilegeByID = `-- name: DeletePrivilegeByID :exec
|
|
|
|
DELETE FROM privileges WHERE id = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) DeletePrivilegeByID(ctx context.Context, id int32) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, deletePrivilegeByID, id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteQuestion = `-- name: DeleteQuestion :one
|
2024-04-18 10:16:03 +00:00
|
|
|
UPDATE question SET deleted=true WHERE id=$1 RETURNING question.id, question.quiz_id, question.title, question.description, question.questiontype, question.required, question.deleted, question.page, question.content, question.version, question.parent_ids, question.created_at, question.updated_at
|
2024-02-19 16:33:15 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) DeleteQuestion(ctx context.Context, id int64) (Question, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, deleteQuestion, id)
|
|
|
|
var i Question
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.QuizID,
|
|
|
|
&i.Title,
|
|
|
|
&i.Description,
|
|
|
|
&i.Questiontype,
|
|
|
|
&i.Required,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Page,
|
|
|
|
&i.Content,
|
|
|
|
&i.Version,
|
|
|
|
pq.Array(&i.ParentIds),
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteQuizByID = `-- name: DeleteQuizByID :one
|
2024-04-18 10:16:03 +00:00
|
|
|
UPDATE quiz SET deleted=true WHERE quiz.id=$1 AND accountid=$2 RETURNING quiz.id, quiz.qid, quiz.accountid, quiz.deleted, quiz.archived, quiz.fingerprinting, quiz.repeatable, quiz.note_prevented, quiz.mail_notifications, quiz.unique_answers, quiz.super, quiz.group_id, quiz.name, quiz.description, quiz.config, quiz.status, quiz.limit_answers, quiz.due_to, quiz.time_of_passing, quiz.pausable, quiz.version, quiz.version_comment, quiz.parent_ids, quiz.created_at, quiz.updated_at, quiz.questions_count, quiz.answers_count, quiz.average_time_passing, quiz.sessions_count, quiz.utm, quiz.rules
|
2024-02-19 16:33:15 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type DeleteQuizByIDParams struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) DeleteQuizByID(ctx context.Context, arg DeleteQuizByIDParams) (Quiz, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, deleteQuizByID, arg.ID, arg.Accountid)
|
|
|
|
var i Quiz
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Qid,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Archived,
|
|
|
|
&i.Fingerprinting,
|
|
|
|
&i.Repeatable,
|
|
|
|
&i.NotePrevented,
|
|
|
|
&i.MailNotifications,
|
|
|
|
&i.UniqueAnswers,
|
|
|
|
&i.Super,
|
|
|
|
&i.GroupID,
|
|
|
|
&i.Name,
|
|
|
|
&i.Description,
|
|
|
|
&i.Config,
|
|
|
|
&i.Status,
|
|
|
|
&i.LimitAnswers,
|
|
|
|
&i.DueTo,
|
|
|
|
&i.TimeOfPassing,
|
|
|
|
&i.Pausable,
|
|
|
|
&i.Version,
|
|
|
|
&i.VersionComment,
|
|
|
|
pq.Array(&i.ParentIds),
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
&i.QuestionsCount,
|
|
|
|
&i.AnswersCount,
|
|
|
|
&i.AverageTimePassing,
|
|
|
|
&i.SessionsCount,
|
2024-04-18 10:16:03 +00:00
|
|
|
&i.Utm,
|
|
|
|
&i.Rules,
|
2024-02-19 16:33:15 +00:00
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2024-03-15 13:11:42 +00:00
|
|
|
const deviceStatistics = `-- name: DeviceStatistics :many
|
|
|
|
WITH DeviceStats AS (
|
|
|
|
SELECT
|
|
|
|
device_type,
|
|
|
|
COUNT(*) AS device_count
|
|
|
|
FROM
|
|
|
|
answer
|
|
|
|
WHERE
|
|
|
|
answer.quiz_id = $1
|
|
|
|
AND created_at >= to_timestamp($2)
|
|
|
|
AND created_at <= to_timestamp($3)
|
|
|
|
AND result = TRUE
|
|
|
|
GROUP BY
|
|
|
|
device_type
|
|
|
|
),
|
|
|
|
OSStats AS (
|
|
|
|
SELECT
|
|
|
|
os,
|
|
|
|
COUNT(*) AS os_count
|
|
|
|
FROM
|
|
|
|
answer
|
|
|
|
WHERE
|
|
|
|
answer.quiz_id = $1
|
|
|
|
AND created_at >= to_timestamp($2)
|
|
|
|
AND created_at <= to_timestamp($3)
|
|
|
|
AND result = TRUE
|
|
|
|
GROUP BY
|
|
|
|
os
|
|
|
|
),
|
|
|
|
BrowserStats AS (
|
|
|
|
SELECT
|
|
|
|
browser,
|
|
|
|
COUNT(*) AS browser_count
|
|
|
|
FROM
|
|
|
|
answer
|
|
|
|
WHERE
|
|
|
|
answer.quiz_id = $1
|
|
|
|
AND created_at >= to_timestamp($2)
|
|
|
|
AND created_at <= to_timestamp($3)
|
|
|
|
AND result = TRUE
|
|
|
|
GROUP BY
|
|
|
|
browser
|
|
|
|
),
|
|
|
|
TotalStats AS (
|
|
|
|
SELECT
|
|
|
|
COUNT(*) AS total_count
|
|
|
|
FROM
|
|
|
|
answer
|
|
|
|
WHERE
|
|
|
|
answer.quiz_id = $1
|
|
|
|
AND created_at >= to_timestamp($2)
|
|
|
|
AND created_at <= to_timestamp($3)
|
|
|
|
AND result = TRUE
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
DeviceStats.device_type,
|
2024-03-17 20:20:55 +00:00
|
|
|
CAST((DeviceStats.device_count::FLOAT / TotalStats.total_count) * 100.0 AS FLOAT8) AS device_percentage,
|
2024-03-15 13:11:42 +00:00
|
|
|
OSStats.os,
|
2024-03-17 20:20:55 +00:00
|
|
|
CAST((OSStats.os_count::FLOAT / TotalStats.total_count) * 100.0 AS FLOAT8) AS os_percentage,
|
2024-03-15 13:11:42 +00:00
|
|
|
BrowserStats.browser,
|
2024-03-17 20:20:55 +00:00
|
|
|
CAST((BrowserStats.browser_count::FLOAT / TotalStats.total_count) * 100.0 AS FLOAT8) AS browser_percentage
|
2024-03-15 13:11:42 +00:00
|
|
|
FROM
|
2024-03-16 19:46:28 +00:00
|
|
|
DeviceStats,
|
|
|
|
OSStats,
|
|
|
|
BrowserStats,
|
|
|
|
TotalStats
|
2024-03-15 13:11:42 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type DeviceStatisticsParams struct {
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
ToTimestamp float64 `db:"to_timestamp" json:"to_timestamp"`
|
|
|
|
ToTimestamp_2 float64 `db:"to_timestamp_2" json:"to_timestamp_2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeviceStatisticsRow struct {
|
2024-03-17 20:20:55 +00:00
|
|
|
DeviceType string `db:"device_type" json:"device_type"`
|
|
|
|
DevicePercentage float64 `db:"device_percentage" json:"device_percentage"`
|
|
|
|
Os string `db:"os" json:"os"`
|
|
|
|
OsPercentage float64 `db:"os_percentage" json:"os_percentage"`
|
|
|
|
Browser string `db:"browser" json:"browser"`
|
|
|
|
BrowserPercentage float64 `db:"browser_percentage" json:"browser_percentage"`
|
2024-03-15 13:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) DeviceStatistics(ctx context.Context, arg DeviceStatisticsParams) ([]DeviceStatisticsRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, deviceStatistics, arg.QuizID, arg.ToTimestamp, arg.ToTimestamp_2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []DeviceStatisticsRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i DeviceStatisticsRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.DeviceType,
|
|
|
|
&i.DevicePercentage,
|
|
|
|
&i.Os,
|
|
|
|
&i.OsPercentage,
|
|
|
|
&i.Browser,
|
|
|
|
&i.BrowserPercentage,
|
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const duplicateQuestion = `-- name: DuplicateQuestion :one
|
|
|
|
INSERT INTO question(
|
|
|
|
quiz_id, title, description, questiontype, required,
|
|
|
|
page, content, version, parent_ids
|
|
|
|
)
|
|
|
|
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
|
|
|
|
`
|
|
|
|
|
|
|
|
type DuplicateQuestionRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
|
|
|
|
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) DuplicateQuestion(ctx context.Context, id int64) (DuplicateQuestionRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, duplicateQuestion, id)
|
|
|
|
var i DuplicateQuestionRow
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.QuizID,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2024-03-15 17:02:01 +00:00
|
|
|
const generalStatistics = `-- name: GeneralStatistics :many
|
|
|
|
WITH TimeBucket AS (
|
|
|
|
SELECT
|
2024-04-07 00:15:31 +00:00
|
|
|
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
|
2024-03-15 17:02:01 +00:00
|
|
|
FROM
|
2024-04-07 00:15:31 +00:00
|
|
|
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
|
2024-03-15 17:02:01 +00:00
|
|
|
),
|
|
|
|
OpenStats AS (
|
|
|
|
SELECT
|
2024-03-18 10:01:58 +00:00
|
|
|
tb.time_interval_start,
|
|
|
|
tb.time_interval_end,
|
2024-03-15 17:02:01 +00:00
|
|
|
COUNT(DISTINCT session) AS open_count
|
2024-03-18 10:01:58 +00:00
|
|
|
FROM
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
session,
|
|
|
|
MIN(created_at) AS first_start_time
|
|
|
|
FROM
|
|
|
|
answer
|
|
|
|
WHERE
|
|
|
|
answer.quiz_id = $3
|
|
|
|
AND start = TRUE
|
2024-04-07 00:15:31 +00:00
|
|
|
AND created_at >= TO_TIMESTAMP($1)
|
|
|
|
AND created_at <= TO_TIMESTAMP($2)
|
2024-03-18 10:01:58 +00:00
|
|
|
GROUP BY
|
|
|
|
session
|
|
|
|
) AS first_starts
|
|
|
|
JOIN TimeBucket tb ON date_trunc('hour', first_starts.first_start_time) >= tb.time_interval_start
|
|
|
|
AND date_trunc('hour', first_starts.first_start_time) < tb.time_interval_end
|
2024-03-15 17:02:01 +00:00
|
|
|
GROUP BY
|
2024-03-18 10:01:58 +00:00
|
|
|
tb.time_interval_start, tb.time_interval_end
|
2024-03-15 17:02:01 +00:00
|
|
|
),
|
|
|
|
ResultStats AS (
|
|
|
|
SELECT
|
2024-03-18 10:01:58 +00:00
|
|
|
tb.time_interval_start,
|
|
|
|
tb.time_interval_end,
|
2024-03-18 11:13:31 +00:00
|
|
|
COUNT(DISTINCT session) AS true_result_count
|
2024-03-15 17:02:01 +00:00
|
|
|
FROM
|
2024-03-18 11:13:31 +00:00
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
session,
|
|
|
|
MIN(created_at) AS first_result_time
|
|
|
|
FROM
|
|
|
|
answer
|
|
|
|
WHERE
|
|
|
|
answer.quiz_id = $3
|
|
|
|
AND result = TRUE
|
2024-04-07 00:15:31 +00:00
|
|
|
AND created_at >= TO_TIMESTAMP($1)
|
|
|
|
AND created_at <= TO_TIMESTAMP($2)
|
2024-03-18 11:13:31 +00:00
|
|
|
GROUP BY
|
|
|
|
session
|
|
|
|
) AS first_results
|
|
|
|
JOIN TimeBucket tb ON date_trunc('hour', first_results.first_result_time) >= tb.time_interval_start
|
|
|
|
AND date_trunc('hour', first_results.first_result_time) < tb.time_interval_end
|
2024-03-15 17:02:01 +00:00
|
|
|
GROUP BY
|
2024-03-18 10:01:58 +00:00
|
|
|
tb.time_interval_start, tb.time_interval_end
|
2024-03-15 17:02:01 +00:00
|
|
|
),
|
|
|
|
AvTimeStats AS (
|
|
|
|
SELECT
|
2024-03-18 10:01:58 +00:00
|
|
|
tb.time_interval_start,
|
|
|
|
tb.time_interval_end,
|
2024-04-07 00:15:31 +00:00
|
|
|
AVG(EXTRACT(epoch FROM (a.created_at)) - EXTRACT(epoch FROM (b.created_at))) AS avg_time
|
2024-03-15 17:02:01 +00:00
|
|
|
FROM
|
|
|
|
answer a
|
2024-03-18 10:01:58 +00:00
|
|
|
JOIN answer b ON a.session = b.session
|
|
|
|
JOIN TimeBucket tb ON date_trunc('hour', a.created_at) >= tb.time_interval_start
|
|
|
|
AND date_trunc('hour', a.created_at) < tb.time_interval_end
|
2024-03-15 17:02:01 +00:00
|
|
|
WHERE
|
|
|
|
a.quiz_id = $3
|
|
|
|
AND a.result = TRUE
|
|
|
|
AND b.start = TRUE
|
2024-03-18 10:01:58 +00:00
|
|
|
AND b.quiz_id = $3
|
2024-04-07 00:15:31 +00:00
|
|
|
AND a.created_at >= TO_TIMESTAMP($1)
|
|
|
|
AND a.created_at <= TO_TIMESTAMP($2)
|
|
|
|
AND b.created_at >= TO_TIMESTAMP($1)
|
|
|
|
AND b.created_at <= TO_TIMESTAMP($2)
|
2024-03-15 17:02:01 +00:00
|
|
|
GROUP BY
|
2024-03-18 10:01:58 +00:00
|
|
|
tb.time_interval_start, tb.time_interval_end
|
2024-03-15 17:02:01 +00:00
|
|
|
)
|
|
|
|
SELECT
|
2024-03-18 10:01:58 +00:00
|
|
|
tb.time_interval_start AS time_bucket,
|
2024-03-15 17:02:01 +00:00
|
|
|
COALESCE(os.open_count, 0) AS open_count,
|
2024-03-18 10:01:58 +00:00
|
|
|
COALESCE(rs.true_result_count, 0) AS true_result_count,
|
2024-03-15 17:02:01 +00:00
|
|
|
CASE
|
2024-04-07 00:15:31 +00:00
|
|
|
WHEN COALESCE(os.open_count, 0) > 0 THEN COALESCE(rs.true_result_count, 0)::float / COALESCE(os.open_count, 0)::float
|
2024-03-15 17:02:01 +00:00
|
|
|
ELSE 0
|
2024-04-07 00:15:31 +00:00
|
|
|
END::float AS conversion,
|
2024-03-18 10:01:58 +00:00
|
|
|
COALESCE(at.avg_time, 0) AS avg_time
|
2024-03-15 17:02:01 +00:00
|
|
|
FROM
|
|
|
|
TimeBucket tb
|
|
|
|
LEFT JOIN
|
2024-03-18 10:01:58 +00:00
|
|
|
OpenStats os ON tb.time_interval_start = os.time_interval_start
|
|
|
|
AND tb.time_interval_end = os.time_interval_end
|
2024-03-15 17:02:01 +00:00
|
|
|
LEFT JOIN
|
2024-03-18 10:01:58 +00:00
|
|
|
ResultStats rs ON tb.time_interval_start = rs.time_interval_start
|
|
|
|
AND tb.time_interval_end = rs.time_interval_end
|
2024-03-15 17:02:01 +00:00
|
|
|
LEFT JOIN
|
2024-03-18 10:01:58 +00:00
|
|
|
AvTimeStats at ON tb.time_interval_start = at.time_interval_start
|
|
|
|
AND tb.time_interval_end = at.time_interval_end
|
2024-03-15 17:02:01 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type GeneralStatisticsParams struct {
|
2024-04-07 00:15:31 +00:00
|
|
|
ToTimestamp float64 `db:"to_timestamp" json:"to_timestamp"`
|
|
|
|
ToTimestamp_2 float64 `db:"to_timestamp_2" json:"to_timestamp_2"`
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
2024-03-15 17:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GeneralStatisticsRow struct {
|
2024-03-18 10:01:58 +00:00
|
|
|
TimeBucket time.Time `db:"time_bucket" json:"time_bucket"`
|
|
|
|
OpenCount int64 `db:"open_count" json:"open_count"`
|
|
|
|
TrueResultCount int64 `db:"true_result_count" json:"true_result_count"`
|
2024-04-07 00:15:31 +00:00
|
|
|
Conversion float64 `db:"conversion" json:"conversion"`
|
2024-03-18 10:01:58 +00:00
|
|
|
AvgTime float64 `db:"avg_time" json:"avg_time"`
|
2024-03-15 17:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GeneralStatistics(ctx context.Context, arg GeneralStatisticsParams) ([]GeneralStatisticsRow, error) {
|
2024-04-07 00:15:31 +00:00
|
|
|
rows, err := q.db.QueryContext(ctx, generalStatistics, arg.ToTimestamp, arg.ToTimestamp_2, arg.QuizID)
|
2024-03-15 17:02:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GeneralStatisticsRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GeneralStatisticsRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.TimeBucket,
|
|
|
|
&i.OpenCount,
|
2024-03-18 10:01:58 +00:00
|
|
|
&i.TrueResultCount,
|
2024-03-15 17:02:01 +00:00
|
|
|
&i.Conversion,
|
2024-03-18 10:01:58 +00:00
|
|
|
&i.AvgTime,
|
2024-03-15 17:02:01 +00:00
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const getAccAndPrivilegeByEmail = `-- name: GetAccAndPrivilegeByEmail :one
|
|
|
|
SELECT
|
|
|
|
a.id,
|
|
|
|
a.user_id,
|
|
|
|
a.email,
|
|
|
|
a.created_at,
|
2024-03-13 16:21:37 +00:00
|
|
|
COALESCE(p.ID,0),
|
|
|
|
coalesce(p.privilegeid,''),
|
|
|
|
coalesce(p.amount,0),
|
|
|
|
coalesce(p.created_at,Now())
|
2024-02-19 16:33:15 +00:00
|
|
|
FROM
|
|
|
|
account AS a
|
|
|
|
LEFT JOIN privileges AS p ON a.id = p.account_id
|
|
|
|
WHERE
|
|
|
|
a.user_id = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
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"`
|
2024-03-13 16:21:37 +00:00
|
|
|
ID_2 int32 `db:"id_2" json:"id_2"`
|
|
|
|
Privilegeid string `db:"privilegeid" json:"privilegeid"`
|
|
|
|
Amount int32 `db:"amount" json:"amount"`
|
2024-02-19 16:33:15 +00:00
|
|
|
CreatedAt_2 sql.NullTime `db:"created_at_2" json:"created_at_2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetAccAndPrivilegeByEmail(ctx context.Context, userID sql.NullString) (GetAccAndPrivilegeByEmailRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getAccAndPrivilegeByEmail, userID)
|
|
|
|
var i GetAccAndPrivilegeByEmailRow
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.UserID,
|
|
|
|
&i.Email,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.ID_2,
|
|
|
|
&i.Privilegeid,
|
|
|
|
&i.Amount,
|
|
|
|
&i.CreatedAt_2,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const getAccountWithPrivileges = `-- name: GetAccountWithPrivileges :many
|
|
|
|
SELECT a.id, a.user_id, a.created_at, a.deleted,
|
2024-03-13 16:21:37 +00:00
|
|
|
coalesce(p.id,0) AS privilege_id,
|
|
|
|
coalesce(p.privilegeID,''),
|
|
|
|
coalesce(p.privilege_name,''),
|
|
|
|
coalesce(p.amount,0), coalesce(p.created_at,Now()) AS privilege_created_at
|
2024-02-19 16:33:15 +00:00
|
|
|
FROM account a
|
|
|
|
LEFT JOIN privileges AS p ON a.id = p.account_id
|
|
|
|
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"`
|
2024-03-13 16:21:37 +00:00
|
|
|
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"`
|
2024-02-19 16:33:15 +00:00
|
|
|
PrivilegeCreatedAt sql.NullTime `db:"privilege_created_at" json:"privilege_created_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetAccountWithPrivileges(ctx context.Context, userID sql.NullString) ([]GetAccountWithPrivilegesRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getAccountWithPrivileges, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetAccountWithPrivilegesRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetAccountWithPrivilegesRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.UserID,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.PrivilegeID,
|
|
|
|
&i.Privilegeid,
|
|
|
|
&i.PrivilegeName,
|
|
|
|
&i.Amount,
|
|
|
|
&i.PrivilegeCreatedAt,
|
|
|
|
); 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 getAllAnswersByQuizID = `-- name: GetAllAnswersByQuizID :many
|
2024-03-28 10:37:43 +00:00
|
|
|
SELECT DISTINCT ON (a.question_id)
|
2024-04-11 16:26:02 +00:00
|
|
|
a.content, a.created_at, a.question_id, a.id, q.questiontype::Text as questiontype, quiz.qid
|
2024-03-28 10:37:43 +00:00
|
|
|
FROM
|
|
|
|
answer a
|
|
|
|
JOIN
|
|
|
|
question q ON a.question_id = q.id
|
|
|
|
JOIN
|
|
|
|
quiz ON q.quiz_id = quiz.id
|
|
|
|
WHERE
|
|
|
|
a.session = $1 AND a.start = false AND a.deleted = false
|
|
|
|
ORDER BY
|
|
|
|
a.question_id ASC, a.created_at DESC
|
2024-02-19 16:33:15 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type GetAllAnswersByQuizIDRow struct {
|
2024-03-28 10:37:43 +00:00
|
|
|
Content sql.NullString `db:"content" json:"content"`
|
|
|
|
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
|
|
|
|
QuestionID int64 `db:"question_id" json:"question_id"`
|
|
|
|
ID int64 `db:"id" json:"id"`
|
2024-04-11 16:26:02 +00:00
|
|
|
Questiontype string `db:"questiontype" json:"questiontype"`
|
2024-03-28 10:37:43 +00:00
|
|
|
Qid uuid.NullUUID `db:"qid" json:"qid"`
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetAllAnswersByQuizID(ctx context.Context, session sql.NullString) ([]GetAllAnswersByQuizIDRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getAllAnswersByQuizID, session)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetAllAnswersByQuizIDRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetAllAnswersByQuizIDRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.Content,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.QuestionID,
|
|
|
|
&i.ID,
|
2024-03-28 10:37:43 +00:00
|
|
|
&i.Questiontype,
|
|
|
|
&i.Qid,
|
2024-02-19 16:33:15 +00:00
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-04-17 17:18:52 +00:00
|
|
|
const getAllTokens = `-- name: GetAllTokens :many
|
|
|
|
SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokens
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetAllTokens(ctx context.Context) ([]Token, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getAllTokens)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []Token
|
|
|
|
for rows.Next() {
|
|
|
|
var i Token
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Refreshtoken,
|
|
|
|
&i.Accesstoken,
|
|
|
|
&i.Authcode,
|
|
|
|
&i.Expiration,
|
|
|
|
&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
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:10:42 +00:00
|
|
|
const getCurrentAccount = `-- name: GetCurrentAccount :one
|
2024-04-17 17:18:52 +00:00
|
|
|
SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country FROM users WHERE AccountID = $1
|
|
|
|
`
|
|
|
|
|
2024-04-17 18:10:42 +00:00
|
|
|
func (q *Queries) GetCurrentAccount(ctx context.Context, accountid string) (User, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getCurrentAccount, accountid)
|
|
|
|
var i User
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Amoid,
|
|
|
|
&i.Name,
|
|
|
|
&i.Email,
|
|
|
|
&i.Role,
|
|
|
|
&i.Group,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Createdat,
|
|
|
|
&i.Subdomain,
|
|
|
|
&i.Amouserid,
|
|
|
|
&i.Country,
|
|
|
|
)
|
|
|
|
return i, err
|
2024-04-17 17:18:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const getExpiredPrivilege = `-- name: GetExpiredPrivilege :many
|
|
|
|
SELECT id, privilegeID, privilege_name, amount, created_at
|
|
|
|
FROM privileges
|
|
|
|
WHERE created_at + amount * interval '1 day' < NOW()
|
|
|
|
AND privilegeid = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetExpiredPrivilegeRow 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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetExpiredPrivilege(ctx context.Context, privilegeid sql.NullString) ([]GetExpiredPrivilegeRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getExpiredPrivilege, privilegeid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetExpiredPrivilegeRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetExpiredPrivilegeRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Privilegeid,
|
|
|
|
&i.PrivilegeName,
|
|
|
|
&i.Amount,
|
|
|
|
&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
|
|
|
|
}
|
|
|
|
|
2024-04-18 10:16:03 +00:00
|
|
|
const getFieldsWithPagination = `-- name: GetFieldsWithPagination :many
|
|
|
|
SELECT id, amoid, code, accountid, name, entity, type, deleted, createdat, COUNT(*) OVER() as total_count FROM fields WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetFieldsWithPaginationParams struct {
|
|
|
|
Column1 interface{} `db:"column_1" json:"column_1"`
|
|
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetFieldsWithPaginationRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Code string `db:"code" json:"code"`
|
|
|
|
Accountid int32 `db:"accountid" json:"accountid"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Entity interface{} `db:"entity" json:"entity"`
|
|
|
|
Type string `db:"type" json:"type"`
|
|
|
|
Deleted bool `db:"deleted" json:"deleted"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
TotalCount int64 `db:"total_count" json:"total_count"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetFieldsWithPagination(ctx context.Context, arg GetFieldsWithPaginationParams) ([]GetFieldsWithPaginationRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getFieldsWithPagination, arg.Column1, arg.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetFieldsWithPaginationRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetFieldsWithPaginationRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Amoid,
|
|
|
|
&i.Code,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Name,
|
|
|
|
&i.Entity,
|
|
|
|
&i.Type,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Createdat,
|
|
|
|
&i.TotalCount,
|
|
|
|
); 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 getPipelinesWithPagination = `-- name: GetPipelinesWithPagination :many
|
|
|
|
SELECT id, amoid, accountid, name, isarchive, deleted, createdat, COUNT(*) OVER() as total_count FROM pipelines WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetPipelinesWithPaginationParams struct {
|
|
|
|
Column1 interface{} `db:"column_1" json:"column_1"`
|
|
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetPipelinesWithPaginationRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Accountid int32 `db:"accountid" json:"accountid"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Isarchive bool `db:"isarchive" json:"isarchive"`
|
|
|
|
Deleted bool `db:"deleted" json:"deleted"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
TotalCount int64 `db:"total_count" json:"total_count"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetPipelinesWithPagination(ctx context.Context, arg GetPipelinesWithPaginationParams) ([]GetPipelinesWithPaginationRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getPipelinesWithPagination, arg.Column1, arg.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetPipelinesWithPaginationRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetPipelinesWithPaginationRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Amoid,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Name,
|
|
|
|
&i.Isarchive,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Createdat,
|
|
|
|
&i.TotalCount,
|
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const getPrivilegesByAccountID = `-- name: GetPrivilegesByAccountID :many
|
|
|
|
SELECT id,privilegeID,privilege_name,amount, created_at FROM privileges WHERE account_id = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetPrivilegesByAccountID(ctx context.Context, accountID uuid.NullUUID) ([]GetPrivilegesByAccountIDRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getPrivilegesByAccountID, accountID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetPrivilegesByAccountIDRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetPrivilegesByAccountIDRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Privilegeid,
|
|
|
|
&i.PrivilegeName,
|
|
|
|
&i.Amount,
|
|
|
|
&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 getPrivilegesByAccountIDWC = `-- name: GetPrivilegesByAccountIDWC :many
|
|
|
|
SELECT p.id,p.privilegeID,p.privilege_name,p.amount, p.created_at FROM privileges as p JOIN account as a on p.account_id = a.id WHERE a.user_id = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetPrivilegesByAccountIDWC(ctx context.Context, userID sql.NullString) ([]GetPrivilegesByAccountIDWCRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getPrivilegesByAccountIDWC, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetPrivilegesByAccountIDWCRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetPrivilegesByAccountIDWCRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Privilegeid,
|
|
|
|
&i.PrivilegeName,
|
|
|
|
&i.Amount,
|
|
|
|
&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 getPrivilegesQuizAccount = `-- name: GetPrivilegesQuizAccount :many
|
|
|
|
SELECT
|
|
|
|
p.privilegeID,
|
|
|
|
p.privilege_name,
|
|
|
|
p.amount,
|
|
|
|
p.created_at,
|
|
|
|
a.id,
|
|
|
|
a.email,
|
|
|
|
qz.config
|
|
|
|
FROM
|
|
|
|
privileges AS p
|
|
|
|
INNER JOIN account AS a ON p.account_id = a.id
|
|
|
|
INNER JOIN quiz AS qz ON qz.accountid = a.user_id
|
|
|
|
WHERE
|
|
|
|
qz.id = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
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"`
|
|
|
|
ID uuid.UUID `db:"id" json:"id"`
|
|
|
|
Email sql.NullString `db:"email" json:"email"`
|
|
|
|
Config sql.NullString `db:"config" json:"config"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetPrivilegesQuizAccount(ctx context.Context, id int64) ([]GetPrivilegesQuizAccountRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getPrivilegesQuizAccount, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetPrivilegesQuizAccountRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetPrivilegesQuizAccountRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.Privilegeid,
|
|
|
|
&i.PrivilegeName,
|
|
|
|
&i.Amount,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.ID,
|
|
|
|
&i.Email,
|
|
|
|
&i.Config,
|
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-03-19 17:09:44 +00:00
|
|
|
const getQidOwner = `-- name: GetQidOwner :one
|
|
|
|
SELECT accountid FROM quiz where qid=$1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetQidOwner(ctx context.Context, qid uuid.NullUUID) (string, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getQidOwner, qid)
|
|
|
|
var accountid string
|
|
|
|
err := row.Scan(&accountid)
|
|
|
|
return accountid, err
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const getQuestionHistory = `-- name: GetQuestionHistory :many
|
2024-04-18 10:16:03 +00:00
|
|
|
SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at FROM question WHERE question.id = $1 OR question.id = ANY(
|
2024-02-19 16:33:15 +00:00
|
|
|
SELECT unnest(parent_ids) FROM question WHERE id = $1
|
|
|
|
) ORDER BY question.id DESC LIMIT $2 OFFSET $3
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetQuestionHistoryParams struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
|
|
Offset int32 `db:"offset" json:"offset"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetQuestionHistory(ctx context.Context, arg GetQuestionHistoryParams) ([]Question, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getQuestionHistory, arg.ID, arg.Limit, arg.Offset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []Question
|
|
|
|
for rows.Next() {
|
|
|
|
var i Question
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.QuizID,
|
|
|
|
&i.Title,
|
|
|
|
&i.Description,
|
|
|
|
&i.Questiontype,
|
|
|
|
&i.Required,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Page,
|
|
|
|
&i.Content,
|
|
|
|
&i.Version,
|
|
|
|
pq.Array(&i.ParentIds),
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
); 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 getQuestionTitle = `-- name: GetQuestionTitle :one
|
2024-04-16 11:07:18 +00:00
|
|
|
SELECT title, questiontype,page FROM question WHERE id = $1
|
2024-02-19 16:33:15 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type GetQuestionTitleRow struct {
|
2024-04-16 11:07:18 +00:00
|
|
|
Title string `db:"title" json:"title"`
|
|
|
|
Questiontype interface{} `db:"questiontype" json:"questiontype"`
|
|
|
|
Page sql.NullInt16 `db:"page" json:"page"`
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetQuestionTitle(ctx context.Context, id int64) (GetQuestionTitleRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getQuestionTitle, id)
|
|
|
|
var i GetQuestionTitleRow
|
2024-04-16 11:07:18 +00:00
|
|
|
err := row.Scan(&i.Title, &i.Questiontype, &i.Page)
|
2024-02-19 16:33:15 +00:00
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const getQuestions = `-- name: GetQuestions :many
|
2024-04-16 08:43:59 +00:00
|
|
|
SELECT id, quiz_id, title, description, questiontype, required, deleted, page, content, version, parent_ids, created_at, updated_at FROM question WHERE quiz_id = $1 AND deleted = FALSE ORDER BY page ASC
|
2024-02-19 16:33:15 +00:00
|
|
|
`
|
|
|
|
|
2024-04-18 10:16:03 +00:00
|
|
|
func (q *Queries) GetQuestions(ctx context.Context, quizID int64) ([]Question, error) {
|
2024-02-19 16:33:15 +00:00
|
|
|
rows, err := q.db.QueryContext(ctx, getQuestions, quizID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2024-04-18 10:16:03 +00:00
|
|
|
var items []Question
|
2024-02-19 16:33:15 +00:00
|
|
|
for rows.Next() {
|
2024-04-18 10:16:03 +00:00
|
|
|
var i Question
|
2024-02-19 16:33:15 +00:00
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.QuizID,
|
|
|
|
&i.Title,
|
|
|
|
&i.Description,
|
|
|
|
&i.Questiontype,
|
|
|
|
&i.Required,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Page,
|
|
|
|
&i.Content,
|
|
|
|
&i.Version,
|
|
|
|
pq.Array(&i.ParentIds),
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
); 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 getQuizById = `-- name: GetQuizById :one
|
2024-04-18 10:16:03 +00:00
|
|
|
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, utm, rules FROM quiz WHERE id=$1 AND accountId=$2
|
2024-02-19 16:33:15 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type GetQuizByIdParams struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetQuizById(ctx context.Context, arg GetQuizByIdParams) (Quiz, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getQuizById, arg.ID, arg.Accountid)
|
|
|
|
var i Quiz
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Qid,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Archived,
|
|
|
|
&i.Fingerprinting,
|
|
|
|
&i.Repeatable,
|
|
|
|
&i.NotePrevented,
|
|
|
|
&i.MailNotifications,
|
|
|
|
&i.UniqueAnswers,
|
|
|
|
&i.Super,
|
|
|
|
&i.GroupID,
|
|
|
|
&i.Name,
|
|
|
|
&i.Description,
|
|
|
|
&i.Config,
|
|
|
|
&i.Status,
|
|
|
|
&i.LimitAnswers,
|
|
|
|
&i.DueTo,
|
|
|
|
&i.TimeOfPassing,
|
|
|
|
&i.Pausable,
|
|
|
|
&i.Version,
|
|
|
|
&i.VersionComment,
|
|
|
|
pq.Array(&i.ParentIds),
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
&i.QuestionsCount,
|
|
|
|
&i.AnswersCount,
|
|
|
|
&i.AverageTimePassing,
|
|
|
|
&i.SessionsCount,
|
2024-04-18 10:16:03 +00:00
|
|
|
&i.Utm,
|
|
|
|
&i.Rules,
|
2024-02-19 16:33:15 +00:00
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const getQuizByQid = `-- name: GetQuizByQid :one
|
2024-04-18 10:16:03 +00:00
|
|
|
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, utm, rules FROM quiz
|
2024-02-19 16:33:15 +00:00
|
|
|
WHERE
|
|
|
|
deleted = false AND
|
|
|
|
archived = false AND
|
|
|
|
status = 'start' AND
|
|
|
|
qid = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetQuizByQid(ctx context.Context, qid uuid.NullUUID) (Quiz, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getQuizByQid, qid)
|
|
|
|
var i Quiz
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Qid,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Archived,
|
|
|
|
&i.Fingerprinting,
|
|
|
|
&i.Repeatable,
|
|
|
|
&i.NotePrevented,
|
|
|
|
&i.MailNotifications,
|
|
|
|
&i.UniqueAnswers,
|
|
|
|
&i.Super,
|
|
|
|
&i.GroupID,
|
|
|
|
&i.Name,
|
|
|
|
&i.Description,
|
|
|
|
&i.Config,
|
|
|
|
&i.Status,
|
|
|
|
&i.LimitAnswers,
|
|
|
|
&i.DueTo,
|
|
|
|
&i.TimeOfPassing,
|
|
|
|
&i.Pausable,
|
|
|
|
&i.Version,
|
|
|
|
&i.VersionComment,
|
|
|
|
pq.Array(&i.ParentIds),
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
&i.QuestionsCount,
|
|
|
|
&i.AnswersCount,
|
|
|
|
&i.AverageTimePassing,
|
|
|
|
&i.SessionsCount,
|
2024-04-18 10:16:03 +00:00
|
|
|
&i.Utm,
|
|
|
|
&i.Rules,
|
2024-02-19 16:33:15 +00:00
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const getQuizConfig = `-- name: GetQuizConfig :one
|
|
|
|
SELECT config, accountid FROM quiz WHERE id = $1 AND deleted = false
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetQuizConfigRow struct {
|
|
|
|
Config sql.NullString `db:"config" json:"config"`
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetQuizConfig(ctx context.Context, id int64) (GetQuizConfigRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getQuizConfig, id)
|
|
|
|
var i GetQuizConfigRow
|
|
|
|
err := row.Scan(&i.Config, &i.Accountid)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const getQuizHistory = `-- name: GetQuizHistory :many
|
2024-04-18 10:16:03 +00:00
|
|
|
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, utm, rules FROM quiz WHERE quiz.id = $1 AND quiz.accountId = $4 OR quiz.id = ANY(
|
2024-02-19 16:33:15 +00:00
|
|
|
SELECT unnest(parent_ids) FROM quiz WHERE id = $1
|
|
|
|
) ORDER BY quiz.id DESC LIMIT $2 OFFSET $3
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetQuizHistoryParams struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
|
|
Offset int32 `db:"offset" json:"offset"`
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetQuizHistory(ctx context.Context, arg GetQuizHistoryParams) ([]Quiz, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getQuizHistory,
|
|
|
|
arg.ID,
|
|
|
|
arg.Limit,
|
|
|
|
arg.Offset,
|
|
|
|
arg.Accountid,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []Quiz
|
|
|
|
for rows.Next() {
|
|
|
|
var i Quiz
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Qid,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Archived,
|
|
|
|
&i.Fingerprinting,
|
|
|
|
&i.Repeatable,
|
|
|
|
&i.NotePrevented,
|
|
|
|
&i.MailNotifications,
|
|
|
|
&i.UniqueAnswers,
|
|
|
|
&i.Super,
|
|
|
|
&i.GroupID,
|
|
|
|
&i.Name,
|
|
|
|
&i.Description,
|
|
|
|
&i.Config,
|
|
|
|
&i.Status,
|
|
|
|
&i.LimitAnswers,
|
|
|
|
&i.DueTo,
|
|
|
|
&i.TimeOfPassing,
|
|
|
|
&i.Pausable,
|
|
|
|
&i.Version,
|
|
|
|
&i.VersionComment,
|
|
|
|
pq.Array(&i.ParentIds),
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
&i.QuestionsCount,
|
|
|
|
&i.AnswersCount,
|
|
|
|
&i.AverageTimePassing,
|
|
|
|
&i.SessionsCount,
|
2024-04-18 10:16:03 +00:00
|
|
|
&i.Utm,
|
|
|
|
&i.Rules,
|
2024-02-19 16:33:15 +00:00
|
|
|
); 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 getResultAnswers = `-- name: GetResultAnswers :many
|
2024-03-31 22:30:38 +00:00
|
|
|
SELECT DISTINCT on (question_id) id, content, quiz_id, question_id, fingerprint, session,created_at, result, new,deleted, device_type,device,os,browser,ip FROM answer WHERE session = (
|
|
|
|
SELECT session FROM answer WHERE answer.id = $1) ORDER BY question_id, created_at DESC
|
2024-02-19 16:33:15 +00:00
|
|
|
`
|
|
|
|
|
2024-03-13 16:21:37 +00:00
|
|
|
type GetResultAnswersRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Content sql.NullString `db:"content" json:"content"`
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
QuestionID int64 `db:"question_id" json:"question_id"`
|
|
|
|
Fingerprint sql.NullString `db:"fingerprint" json:"fingerprint"`
|
|
|
|
Session sql.NullString `db:"session" json:"session"`
|
|
|
|
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
|
|
|
|
Result sql.NullBool `db:"result" json:"result"`
|
|
|
|
New sql.NullBool `db:"new" json:"new"`
|
|
|
|
Deleted sql.NullBool `db:"deleted" json:"deleted"`
|
2024-03-31 22:30:38 +00:00
|
|
|
DeviceType string `db:"device_type" json:"device_type"`
|
|
|
|
Device string `db:"device" json:"device"`
|
|
|
|
Os string `db:"os" json:"os"`
|
|
|
|
Browser string `db:"browser" json:"browser"`
|
|
|
|
Ip string `db:"ip" json:"ip"`
|
2024-03-13 16:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetResultAnswers(ctx context.Context, id int64) ([]GetResultAnswersRow, error) {
|
2024-02-19 16:33:15 +00:00
|
|
|
rows, err := q.db.QueryContext(ctx, getResultAnswers, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2024-03-13 16:21:37 +00:00
|
|
|
var items []GetResultAnswersRow
|
2024-02-19 16:33:15 +00:00
|
|
|
for rows.Next() {
|
2024-03-13 16:21:37 +00:00
|
|
|
var i GetResultAnswersRow
|
2024-02-19 16:33:15 +00:00
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Content,
|
|
|
|
&i.QuizID,
|
|
|
|
&i.QuestionID,
|
|
|
|
&i.Fingerprint,
|
|
|
|
&i.Session,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.Result,
|
|
|
|
&i.New,
|
|
|
|
&i.Deleted,
|
2024-03-31 22:30:38 +00:00
|
|
|
&i.DeviceType,
|
|
|
|
&i.Device,
|
|
|
|
&i.Os,
|
|
|
|
&i.Browser,
|
|
|
|
&i.Ip,
|
2024-02-19 16:33:15 +00:00
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-04-18 10:16:03 +00:00
|
|
|
const getStepsWithPagination = `-- name: GetStepsWithPagination :many
|
|
|
|
SELECT id, amoid, pipelineid, accountid, name, color, deleted, createdat, COUNT(*) OVER() as total_count FROM steps WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetStepsWithPaginationParams struct {
|
|
|
|
Column1 interface{} `db:"column_1" json:"column_1"`
|
|
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetStepsWithPaginationRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Pipelineid int32 `db:"pipelineid" json:"pipelineid"`
|
|
|
|
Accountid int32 `db:"accountid" json:"accountid"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Color string `db:"color" json:"color"`
|
|
|
|
Deleted bool `db:"deleted" json:"deleted"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
TotalCount int64 `db:"total_count" json:"total_count"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetStepsWithPagination(ctx context.Context, arg GetStepsWithPaginationParams) ([]GetStepsWithPaginationRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getStepsWithPagination, arg.Column1, arg.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetStepsWithPaginationRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetStepsWithPaginationRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Amoid,
|
|
|
|
&i.Pipelineid,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Name,
|
|
|
|
&i.Color,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Createdat,
|
|
|
|
&i.TotalCount,
|
|
|
|
); 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 getTagsWithPagination = `-- name: GetTagsWithPagination :many
|
|
|
|
SELECT id, amoid, accountid, entity, name, color, deleted, createdat, COUNT(*) OVER() as total_count FROM tags WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetTagsWithPaginationParams struct {
|
|
|
|
Column1 interface{} `db:"column_1" json:"column_1"`
|
|
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetTagsWithPaginationRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Accountid int32 `db:"accountid" json:"accountid"`
|
|
|
|
Entity interface{} `db:"entity" json:"entity"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Color string `db:"color" json:"color"`
|
|
|
|
Deleted bool `db:"deleted" json:"deleted"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
TotalCount int64 `db:"total_count" json:"total_count"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetTagsWithPagination(ctx context.Context, arg GetTagsWithPaginationParams) ([]GetTagsWithPaginationRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getTagsWithPagination, arg.Column1, arg.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetTagsWithPaginationRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetTagsWithPaginationRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Amoid,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Entity,
|
|
|
|
&i.Name,
|
|
|
|
&i.Color,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Createdat,
|
|
|
|
&i.TotalCount,
|
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-04-21 13:57:43 +00:00
|
|
|
const getTokenById = `-- name: GetTokenById :one
|
2024-04-21 13:52:25 +00:00
|
|
|
SELECT accountid, refreshtoken, accesstoken, authcode, expiration, createdat FROM tokens WHERE accountID = $1
|
|
|
|
`
|
|
|
|
|
2024-04-21 13:57:43 +00:00
|
|
|
func (q *Queries) GetTokenById(ctx context.Context, accountid string) (Token, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getTokenById, accountid)
|
|
|
|
var i Token
|
|
|
|
err := row.Scan(
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Refreshtoken,
|
|
|
|
&i.Accesstoken,
|
|
|
|
&i.Authcode,
|
|
|
|
&i.Expiration,
|
|
|
|
&i.Createdat,
|
|
|
|
)
|
|
|
|
return i, err
|
2024-04-21 13:52:25 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 17:18:52 +00:00
|
|
|
const getUsersWithPagination = `-- name: GetUsersWithPagination :many
|
2024-04-17 17:43:28 +00:00
|
|
|
SELECT id, accountid, amoid, name, email, role, "Group", deleted, createdat, subdomain, amouserid, country, COUNT(*) OVER() as total_count FROM users WHERE Deleted = false ORDER BY ID OFFSET ($1 - 1) * $2 LIMIT $2
|
2024-04-17 17:18:52 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type GetUsersWithPaginationParams struct {
|
|
|
|
Column1 interface{} `db:"column_1" json:"column_1"`
|
|
|
|
Limit int32 `db:"limit" json:"limit"`
|
|
|
|
}
|
|
|
|
|
2024-04-17 17:43:28 +00:00
|
|
|
type GetUsersWithPaginationRow struct {
|
2024-04-20 09:01:25 +00:00
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Email string `db:"email" json:"email"`
|
|
|
|
Role int32 `db:"role" json:"role"`
|
|
|
|
Group int32 `db:"Group" json:"Group"`
|
|
|
|
Deleted bool `db:"deleted" json:"deleted"`
|
|
|
|
Createdat sql.NullTime `db:"createdat" json:"createdat"`
|
|
|
|
Subdomain string `db:"subdomain" json:"subdomain"`
|
|
|
|
Amouserid int32 `db:"amouserid" json:"amouserid"`
|
|
|
|
Country string `db:"country" json:"country"`
|
|
|
|
TotalCount int64 `db:"total_count" json:"total_count"`
|
2024-04-17 17:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetUsersWithPagination(ctx context.Context, arg GetUsersWithPaginationParams) ([]GetUsersWithPaginationRow, error) {
|
2024-04-17 17:18:52 +00:00
|
|
|
rows, err := q.db.QueryContext(ctx, getUsersWithPagination, arg.Column1, arg.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2024-04-17 17:43:28 +00:00
|
|
|
var items []GetUsersWithPaginationRow
|
2024-04-17 17:18:52 +00:00
|
|
|
for rows.Next() {
|
2024-04-17 17:43:28 +00:00
|
|
|
var i GetUsersWithPaginationRow
|
2024-04-17 17:18:52 +00:00
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Accountid,
|
|
|
|
&i.Amoid,
|
|
|
|
&i.Name,
|
|
|
|
&i.Email,
|
|
|
|
&i.Role,
|
|
|
|
&i.Group,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.Createdat,
|
|
|
|
&i.Subdomain,
|
|
|
|
&i.Amouserid,
|
|
|
|
&i.Country,
|
2024-04-17 17:43:28 +00:00
|
|
|
&i.TotalCount,
|
2024-04-17 17:18:52 +00:00
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const insertAnswers = `-- name: InsertAnswers :exec
|
|
|
|
INSERT INTO answer(
|
|
|
|
content,
|
|
|
|
quiz_id,
|
|
|
|
question_id,
|
|
|
|
fingerprint,
|
|
|
|
session,
|
2024-03-13 16:21:37 +00:00
|
|
|
result,
|
2024-03-14 13:32:20 +00:00
|
|
|
email,
|
|
|
|
device_type,
|
|
|
|
device,
|
|
|
|
os,
|
|
|
|
browser,
|
2024-03-15 11:32:26 +00:00
|
|
|
ip,
|
|
|
|
start
|
|
|
|
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
|
2024-02-19 16:33:15 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type InsertAnswersParams struct {
|
|
|
|
Content sql.NullString `db:"content" json:"content"`
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
QuestionID int64 `db:"question_id" json:"question_id"`
|
|
|
|
Fingerprint sql.NullString `db:"fingerprint" json:"fingerprint"`
|
|
|
|
Session sql.NullString `db:"session" json:"session"`
|
|
|
|
Result sql.NullBool `db:"result" json:"result"`
|
2024-03-13 16:21:37 +00:00
|
|
|
Email string `db:"email" json:"email"`
|
2024-03-14 13:32:20 +00:00
|
|
|
DeviceType string `db:"device_type" json:"device_type"`
|
|
|
|
Device string `db:"device" json:"device"`
|
|
|
|
Os string `db:"os" json:"os"`
|
|
|
|
Browser string `db:"browser" json:"browser"`
|
|
|
|
Ip string `db:"ip" json:"ip"`
|
2024-03-15 11:32:26 +00:00
|
|
|
Start bool `db:"start" json:"start"`
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) InsertAnswers(ctx context.Context, arg InsertAnswersParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, insertAnswers,
|
|
|
|
arg.Content,
|
|
|
|
arg.QuizID,
|
|
|
|
arg.QuestionID,
|
|
|
|
arg.Fingerprint,
|
|
|
|
arg.Session,
|
|
|
|
arg.Result,
|
2024-03-13 16:21:37 +00:00
|
|
|
arg.Email,
|
2024-03-14 13:32:20 +00:00
|
|
|
arg.DeviceType,
|
|
|
|
arg.Device,
|
|
|
|
arg.Os,
|
|
|
|
arg.Browser,
|
|
|
|
arg.Ip,
|
2024-03-15 11:32:26 +00:00
|
|
|
arg.Start,
|
2024-02-19 16:33:15 +00:00
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const insertPrivilege = `-- name: InsertPrivilege :exec
|
|
|
|
INSERT INTO privileges (privilegeID, account_id, privilege_name, amount, created_at) VALUES ($1, $2, $3, $4, $5)
|
|
|
|
`
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) InsertPrivilege(ctx context.Context, arg InsertPrivilegeParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, insertPrivilege,
|
|
|
|
arg.Privilegeid,
|
|
|
|
arg.AccountID,
|
|
|
|
arg.PrivilegeName,
|
|
|
|
arg.Amount,
|
|
|
|
arg.CreatedAt,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const insertQuestion = `-- name: InsertQuestion :one
|
|
|
|
INSERT INTO question (
|
|
|
|
quiz_id,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
questiontype,
|
|
|
|
required,
|
|
|
|
page,
|
|
|
|
content,
|
|
|
|
parent_ids,
|
|
|
|
updated_at
|
|
|
|
)
|
|
|
|
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)
|
|
|
|
RETURNING id, created_at, updated_at
|
|
|
|
`
|
|
|
|
|
|
|
|
type InsertQuestionParams struct {
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
Title string `db:"title" json:"title"`
|
|
|
|
Description sql.NullString `db:"description" json:"description"`
|
|
|
|
Questiontype interface{} `db:"questiontype" json:"questiontype"`
|
|
|
|
Required sql.NullBool `db:"required" json:"required"`
|
|
|
|
Page sql.NullInt16 `db:"page" json:"page"`
|
|
|
|
Content sql.NullString `db:"content" json:"content"`
|
|
|
|
ParentIds []int32 `db:"parent_ids" json:"parent_ids"`
|
|
|
|
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type InsertQuestionRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
|
|
|
|
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) InsertQuestion(ctx context.Context, arg InsertQuestionParams) (InsertQuestionRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, insertQuestion,
|
|
|
|
arg.QuizID,
|
|
|
|
arg.Title,
|
|
|
|
arg.Description,
|
|
|
|
arg.Questiontype,
|
|
|
|
arg.Required,
|
|
|
|
arg.Page,
|
|
|
|
arg.Content,
|
|
|
|
pq.Array(arg.ParentIds),
|
|
|
|
arg.UpdatedAt,
|
|
|
|
)
|
|
|
|
var i InsertQuestionRow
|
|
|
|
err := row.Scan(&i.ID, &i.CreatedAt, &i.UpdatedAt)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const insertQuiz = `-- name: InsertQuiz :one
|
|
|
|
INSERT INTO quiz (accountid,
|
|
|
|
fingerprinting,
|
|
|
|
repeatable,
|
|
|
|
note_prevented,
|
|
|
|
mail_notifications,
|
|
|
|
unique_answers,
|
|
|
|
super,
|
|
|
|
group_id,
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
config,
|
|
|
|
status,
|
|
|
|
limit_answers,
|
|
|
|
due_to,
|
|
|
|
time_of_passing,
|
|
|
|
pausable,
|
|
|
|
parent_ids,
|
|
|
|
questions_count,
|
|
|
|
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
|
|
|
|
`
|
|
|
|
|
|
|
|
type InsertQuizParams struct {
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
Fingerprinting sql.NullBool `db:"fingerprinting" json:"fingerprinting"`
|
|
|
|
Repeatable sql.NullBool `db:"repeatable" json:"repeatable"`
|
|
|
|
NotePrevented sql.NullBool `db:"note_prevented" json:"note_prevented"`
|
|
|
|
MailNotifications sql.NullBool `db:"mail_notifications" json:"mail_notifications"`
|
|
|
|
UniqueAnswers sql.NullBool `db:"unique_answers" json:"unique_answers"`
|
|
|
|
Super sql.NullBool `db:"super" json:"super"`
|
|
|
|
GroupID sql.NullInt64 `db:"group_id" json:"group_id"`
|
|
|
|
Name sql.NullString `db:"name" json:"name"`
|
|
|
|
Description sql.NullString `db:"description" json:"description"`
|
|
|
|
Config sql.NullString `db:"config" json:"config"`
|
|
|
|
Status interface{} `db:"status" json:"status"`
|
|
|
|
LimitAnswers sql.NullInt32 `db:"limit_answers" json:"limit_answers"`
|
|
|
|
DueTo sql.NullInt32 `db:"due_to" json:"due_to"`
|
|
|
|
TimeOfPassing sql.NullInt32 `db:"time_of_passing" json:"time_of_passing"`
|
|
|
|
Pausable sql.NullBool `db:"pausable" json:"pausable"`
|
|
|
|
ParentIds []int32 `db:"parent_ids" json:"parent_ids"`
|
|
|
|
QuestionsCount sql.NullInt32 `db:"questions_count" json:"questions_count"`
|
|
|
|
Qid uuid.NullUUID `db:"qid" json:"qid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type InsertQuizRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
CreatedAt sql.NullTime `db:"created_at" json:"created_at"`
|
|
|
|
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
|
|
|
|
Qid uuid.NullUUID `db:"qid" json:"qid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) InsertQuiz(ctx context.Context, arg InsertQuizParams) (InsertQuizRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, insertQuiz,
|
|
|
|
arg.Accountid,
|
|
|
|
arg.Fingerprinting,
|
|
|
|
arg.Repeatable,
|
|
|
|
arg.NotePrevented,
|
|
|
|
arg.MailNotifications,
|
|
|
|
arg.UniqueAnswers,
|
|
|
|
arg.Super,
|
|
|
|
arg.GroupID,
|
|
|
|
arg.Name,
|
|
|
|
arg.Description,
|
|
|
|
arg.Config,
|
|
|
|
arg.Status,
|
|
|
|
arg.LimitAnswers,
|
|
|
|
arg.DueTo,
|
|
|
|
arg.TimeOfPassing,
|
|
|
|
arg.Pausable,
|
|
|
|
pq.Array(arg.ParentIds),
|
|
|
|
arg.QuestionsCount,
|
|
|
|
arg.Qid,
|
|
|
|
)
|
|
|
|
var i InsertQuizRow
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
&i.Qid,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const moveToHistory = `-- name: MoveToHistory :one
|
|
|
|
INSERT INTO question(
|
|
|
|
quiz_id, title, description, questiontype, required,
|
|
|
|
page, content, version, parent_ids, deleted
|
|
|
|
)
|
|
|
|
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
|
|
|
|
`
|
|
|
|
|
|
|
|
type MoveToHistoryRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
ParentIds []int32 `db:"parent_ids" json:"parent_ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) MoveToHistory(ctx context.Context, id int64) (MoveToHistoryRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, moveToHistory, id)
|
|
|
|
var i MoveToHistoryRow
|
|
|
|
err := row.Scan(&i.ID, &i.QuizID, pq.Array(&i.ParentIds))
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const moveToHistoryQuiz = `-- name: MoveToHistoryQuiz :one
|
|
|
|
INSERT INTO quiz(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
|
|
|
|
)
|
|
|
|
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
|
|
|
|
`
|
|
|
|
|
|
|
|
type MoveToHistoryQuizParams struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MoveToHistoryQuizRow struct {
|
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
Qid uuid.NullUUID `db:"qid" json:"qid"`
|
|
|
|
ParentIds []int32 `db:"parent_ids" json:"parent_ids"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) MoveToHistoryQuiz(ctx context.Context, arg MoveToHistoryQuizParams) (MoveToHistoryQuizRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, moveToHistoryQuiz, arg.ID, arg.Accountid)
|
|
|
|
var i MoveToHistoryQuizRow
|
|
|
|
err := row.Scan(&i.ID, &i.Qid, pq.Array(&i.ParentIds))
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2024-03-17 14:55:20 +00:00
|
|
|
const questionsStatistics = `-- name: QuestionsStatistics :many
|
|
|
|
WITH Funnel AS (
|
|
|
|
SELECT
|
2024-03-17 15:25:46 +00:00
|
|
|
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
|
2024-03-17 14:55:20 +00:00
|
|
|
FROM
|
|
|
|
answer a
|
|
|
|
LEFT JOIN (
|
|
|
|
SELECT DISTINCT a.session, q.id AS qid_true_result
|
|
|
|
FROM answer a
|
|
|
|
JOIN question q ON a.question_id = q.id
|
|
|
|
WHERE a.result = TRUE
|
|
|
|
) AS q ON a.session = q.session
|
|
|
|
WHERE
|
|
|
|
a.quiz_id = $1
|
|
|
|
AND a.created_at >= TO_TIMESTAMP($2)
|
|
|
|
AND a.created_at <= TO_TIMESTAMP($3)
|
|
|
|
),
|
|
|
|
Results AS (
|
2024-03-17 19:21:12 +00:00
|
|
|
SELECT
|
2024-04-07 10:59:53 +00:00
|
|
|
COALESCE(q.title, '') AS question_title,
|
2024-03-17 19:21:12 +00:00
|
|
|
COUNT(*) AS total_answers,
|
2024-03-17 19:25:41 +00:00
|
|
|
CAST(COUNT(*) * 100.0 / NULLIF(SUM(COUNT(*)) FILTER (WHERE a.result = TRUE) OVER (PARTITION BY a.quiz_id), 0) AS FLOAT8) AS percentage
|
2024-03-17 19:21:12 +00:00
|
|
|
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)
|
2024-03-17 19:28:28 +00:00
|
|
|
AND a.result = TRUE
|
2024-03-17 19:21:12 +00:00
|
|
|
GROUP BY
|
2024-03-17 19:23:30 +00:00
|
|
|
q.title, a.quiz_id, a.result
|
2024-03-17 19:21:12 +00:00
|
|
|
HAVING
|
|
|
|
COUNT(*) >= 1
|
|
|
|
),
|
2024-04-05 13:27:04 +00:00
|
|
|
LastContent AS (
|
|
|
|
SELECT
|
|
|
|
a.question_id,
|
|
|
|
a.content AS last_answer_content
|
|
|
|
FROM
|
|
|
|
answer a
|
|
|
|
JOIN (
|
|
|
|
SELECT
|
|
|
|
session,
|
|
|
|
question_id,
|
|
|
|
MAX(created_at) AS last_created_at
|
|
|
|
FROM
|
|
|
|
answer
|
|
|
|
WHERE
|
|
|
|
quiz_id = $1
|
2024-04-07 15:32:36 +00:00
|
|
|
AND start != true
|
2024-04-05 13:27:04 +00:00
|
|
|
AND created_at >= TO_TIMESTAMP($2)
|
|
|
|
AND created_at <= TO_TIMESTAMP($3)
|
|
|
|
GROUP BY
|
|
|
|
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
|
|
|
|
),
|
2024-03-17 14:55:20 +00:00
|
|
|
Questions AS (
|
2024-03-17 19:10:11 +00:00
|
|
|
SELECT
|
|
|
|
q.title AS question_title,
|
2024-04-08 18:28:39 +00:00
|
|
|
q.page AS question_page,
|
2024-04-05 13:27:04 +00:00
|
|
|
lc.last_answer_content AS answer_content,
|
2024-03-17 19:10:11 +00:00
|
|
|
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
|
2024-04-05 13:27:04 +00:00
|
|
|
JOIN LastContent lc ON q.id = lc.question_id
|
2024-03-17 19:10:11 +00:00
|
|
|
JOIN answer a ON q.id = a.question_id
|
|
|
|
WHERE
|
|
|
|
a.quiz_id = $1
|
2024-04-07 15:32:36 +00:00
|
|
|
AND a.start != true
|
2024-03-17 19:10:11 +00:00
|
|
|
AND a.created_at >= TO_TIMESTAMP($2)
|
|
|
|
AND a.created_at <= TO_TIMESTAMP($3)
|
|
|
|
GROUP BY
|
2024-04-05 13:27:04 +00:00
|
|
|
q.id, q.title, lc.last_answer_content
|
2024-03-17 19:10:11 +00:00
|
|
|
HAVING
|
|
|
|
COUNT(*) >= 1
|
2024-03-17 18:10:10 +00:00
|
|
|
)
|
2024-03-17 14:55:20 +00:00
|
|
|
SELECT
|
|
|
|
Funnel.count_start_false,
|
|
|
|
Funnel.count_start_true,
|
|
|
|
Funnel.count_f_result_with_t_question,
|
|
|
|
Funnel.count_t_result,
|
2024-04-07 10:59:53 +00:00
|
|
|
COALESCE(Results.question_title, '') AS results_title,
|
|
|
|
COALESCE(Results.percentage, 0) AS results_percentage,
|
|
|
|
COALESCE(Questions.question_title, '') AS questions_title,
|
2024-04-08 18:28:39 +00:00
|
|
|
COALESCE(Questions.question_page, 0) AS questions_page,
|
2024-04-07 10:59:53 +00:00
|
|
|
COALESCE(Questions.answer_content, '') AS answer_content,
|
|
|
|
COALESCE(Questions.percentage, 0) AS questions_percentage
|
2024-03-17 14:55:20 +00:00
|
|
|
FROM
|
2024-04-07 10:59:53 +00:00
|
|
|
Funnel
|
|
|
|
LEFT JOIN Results ON true
|
|
|
|
LEFT JOIN Questions ON Questions.percentage >= 1
|
2024-03-17 14:55:20 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type QuestionsStatisticsParams struct {
|
|
|
|
QuizID int64 `db:"quiz_id" json:"quiz_id"`
|
|
|
|
ToTimestamp float64 `db:"to_timestamp" json:"to_timestamp"`
|
|
|
|
ToTimestamp_2 float64 `db:"to_timestamp_2" json:"to_timestamp_2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type QuestionsStatisticsRow struct {
|
2024-04-07 10:59:53 +00:00
|
|
|
CountStartFalse int64 `db:"count_start_false" json:"count_start_false"`
|
|
|
|
CountStartTrue int64 `db:"count_start_true" json:"count_start_true"`
|
|
|
|
CountFResultWithTQuestion int64 `db:"count_f_result_with_t_question" json:"count_f_result_with_t_question"`
|
|
|
|
CountTResult int64 `db:"count_t_result" json:"count_t_result"`
|
|
|
|
ResultsTitle string `db:"results_title" json:"results_title"`
|
|
|
|
ResultsPercentage float64 `db:"results_percentage" json:"results_percentage"`
|
|
|
|
QuestionsTitle string `db:"questions_title" json:"questions_title"`
|
|
|
|
QuestionsPage int16 `db:"questions_page" json:"questions_page"`
|
|
|
|
AnswerContent string `db:"answer_content" json:"answer_content"`
|
|
|
|
QuestionsPercentage float64 `db:"questions_percentage" json:"questions_percentage"`
|
2024-03-17 14:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) QuestionsStatistics(ctx context.Context, arg QuestionsStatisticsParams) ([]QuestionsStatisticsRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, questionsStatistics, arg.QuizID, arg.ToTimestamp, arg.ToTimestamp_2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []QuestionsStatisticsRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i QuestionsStatisticsRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.CountStartFalse,
|
|
|
|
&i.CountStartTrue,
|
|
|
|
&i.CountFResultWithTQuestion,
|
|
|
|
&i.CountTResult,
|
2024-03-17 14:58:18 +00:00
|
|
|
&i.ResultsTitle,
|
2024-03-17 14:55:20 +00:00
|
|
|
&i.ResultsPercentage,
|
2024-03-17 14:58:18 +00:00
|
|
|
&i.QuestionsTitle,
|
2024-04-08 18:28:39 +00:00
|
|
|
&i.QuestionsPage,
|
2024-03-17 14:58:18 +00:00
|
|
|
&i.AnswerContent,
|
2024-03-17 14:55:20 +00:00
|
|
|
&i.QuestionsPercentage,
|
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-03-19 16:13:03 +00:00
|
|
|
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
|
2024-03-28 22:23:08 +00:00
|
|
|
$2, archived, fingerprinting, repeatable, note_prevented, mail_notifications, unique_answers, name, description, config,
|
2024-03-19 16:13:03 +00:00
|
|
|
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
|
2024-03-28 22:23:08 +00:00
|
|
|
quiz as q
|
2024-03-19 16:13:03 +00:00
|
|
|
WHERE
|
2024-03-28 22:23:08 +00:00
|
|
|
q.qid = $1
|
2024-03-29 08:37:16 +00:00
|
|
|
RETURNING (select id from quiz where qid = $1),id, qid
|
2024-03-19 16:13:03 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type QuizCopyQidParams struct {
|
|
|
|
Qid uuid.NullUUID `db:"qid" json:"qid"`
|
|
|
|
Accountid string `db:"accountid" json:"accountid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type QuizCopyQidRow struct {
|
2024-03-28 23:51:52 +00:00
|
|
|
ID int64 `db:"id" json:"id"`
|
|
|
|
ID_2 int64 `db:"id_2" json:"id_2"`
|
|
|
|
Qid uuid.NullUUID `db:"qid" json:"qid"`
|
2024-03-19 16:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) QuizCopyQid(ctx context.Context, arg QuizCopyQidParams) (QuizCopyQidRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, quizCopyQid, arg.Qid, arg.Accountid)
|
|
|
|
var i QuizCopyQidRow
|
2024-03-28 23:51:52 +00:00
|
|
|
err := row.Scan(&i.ID, &i.ID_2, &i.Qid)
|
2024-03-19 16:13:03 +00:00
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2024-04-17 17:18:52 +00:00
|
|
|
const softDeleteAccount = `-- name: SoftDeleteAccount :exec
|
|
|
|
UPDATE users SET Deleted = TRUE WHERE AccountID = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) SoftDeleteAccount(ctx context.Context, accountid string) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, softDeleteAccount, accountid)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const softDeleteResultByID = `-- name: SoftDeleteResultByID :exec
|
|
|
|
UPDATE answer SET deleted = TRUE WHERE id = $1 AND deleted = FALSE
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) SoftDeleteResultByID(ctx context.Context, id int64) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, softDeleteResultByID, id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-18 20:05:49 +00:00
|
|
|
const updateFields = `-- name: UpdateFields :exec
|
|
|
|
UPDATE fields AS f
|
|
|
|
SET name = (update_data ->> 'Name')::varchar(50),
|
|
|
|
code = (update_data ->> 'Code')::varchar(255),
|
|
|
|
type = (update_data ->> 'Type')::varchar(50),
|
|
|
|
createdAt = CURRENT_TIMESTAMP
|
|
|
|
FROM json_array_elements($1::json) AS update_data
|
|
|
|
WHERE f.amoID = (update_data ->> 'AmoID')::INT
|
|
|
|
AND f.accountID = (update_data ->> 'AccountID')::INT
|
|
|
|
AND f.Entity = (update_data ->> 'Entity')::entitytype
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) UpdateFields(ctx context.Context, dollar_1 json.RawMessage) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, updateFields, dollar_1)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatePipelines = `-- name: UpdatePipelines :exec
|
|
|
|
UPDATE pipelines AS p
|
|
|
|
SET name = (update_data ->> 'Name')::varchar(50),
|
|
|
|
isArchive = CASE WHEN (update_data ->> 'IsArchive') = 'true' THEN TRUE ELSE FALSE END,
|
|
|
|
createdAt = CURRENT_TIMESTAMP
|
|
|
|
FROM json_array_elements($1::json) AS update_data
|
|
|
|
WHERE p.amoID = (update_data ->> 'AmoID')::INT
|
|
|
|
AND p.accountID = (update_data ->> 'AccountID')::INT
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) UpdatePipelines(ctx context.Context, dollar_1 json.RawMessage) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, updatePipelines, dollar_1)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const updatePrivilege = `-- name: UpdatePrivilege :exec
|
|
|
|
UPDATE privileges SET amount = $1, created_at = $2 WHERE account_id = $3 AND privilegeID = $4
|
|
|
|
`
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) UpdatePrivilege(ctx context.Context, arg UpdatePrivilegeParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, updatePrivilege,
|
|
|
|
arg.Amount,
|
|
|
|
arg.CreatedAt,
|
|
|
|
arg.AccountID,
|
|
|
|
arg.Privilegeid,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatePrivilegeAmount = `-- name: UpdatePrivilegeAmount :exec
|
|
|
|
UPDATE privileges SET amount = $1 WHERE id = $2
|
|
|
|
`
|
|
|
|
|
|
|
|
type UpdatePrivilegeAmountParams struct {
|
|
|
|
Amount sql.NullInt32 `db:"amount" json:"amount"`
|
|
|
|
ID int32 `db:"id" json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) UpdatePrivilegeAmount(ctx context.Context, arg UpdatePrivilegeAmountParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, updatePrivilegeAmount, arg.Amount, arg.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-18 20:05:49 +00:00
|
|
|
const updateSteps = `-- name: UpdateSteps :exec
|
|
|
|
UPDATE steps AS s
|
|
|
|
SET name = (update_data ->> 'Name')::varchar(50),
|
|
|
|
color = (update_data ->> 'Color')::varchar(50),
|
|
|
|
createdAt = CURRENT_TIMESTAMP
|
|
|
|
FROM json_array_elements($1::json) AS update_data
|
|
|
|
WHERE s.amoID = (update_data ->> 'AmoID')::INT
|
|
|
|
AND s.accountID = (update_data ->> 'AccountID')::INT
|
|
|
|
AND s.pipelineID = (update_data ->> 'PipelineID')::INT
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) UpdateSteps(ctx context.Context, dollar_1 json.RawMessage) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, updateSteps, dollar_1)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateTags = `-- name: UpdateTags :exec
|
|
|
|
UPDATE tags AS t
|
|
|
|
SET name = (update_data ->> 'Name')::varchar(50),
|
|
|
|
color = (update_data ->> 'Color')::varchar(50),
|
|
|
|
createdAt = CURRENT_TIMESTAMP
|
|
|
|
FROM json_array_elements($1::json) AS update_data
|
|
|
|
WHERE t.amoID = (update_data ->> 'AmoID')::INT
|
|
|
|
AND t.accountID = (update_data ->> 'AccountID')::INT
|
|
|
|
AND t.Entity = (update_data ->> 'Entity')::entitytype
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) UpdateTags(ctx context.Context, dollar_1 json.RawMessage) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, updateTags, dollar_1)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-20 09:01:25 +00:00
|
|
|
const updateUsers = `-- name: UpdateUsers :exec
|
|
|
|
UPDATE users SET Name = $2, Email = $3, Role = $4, "Group" = $5, AmoUserID = $6 WHERE AmoID = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
type UpdateUsersParams struct {
|
|
|
|
Amoid int32 `db:"amoid" json:"amoid"`
|
|
|
|
Name string `db:"name" json:"name"`
|
|
|
|
Email string `db:"email" json:"email"`
|
|
|
|
Role int32 `db:"role" json:"role"`
|
|
|
|
Group int32 `db:"Group" json:"Group"`
|
|
|
|
Amouserid int32 `db:"amouserid" json:"amouserid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) UpdateUsers(ctx context.Context, arg UpdateUsersParams) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, updateUsers,
|
|
|
|
arg.Amoid,
|
|
|
|
arg.Name,
|
|
|
|
arg.Email,
|
|
|
|
arg.Role,
|
|
|
|
arg.Group,
|
|
|
|
arg.Amouserid,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-17 17:18:52 +00:00
|
|
|
const webhookDelete = `-- name: WebhookDelete :exec
|
|
|
|
DELETE FROM tokens WHERE AccountID = $1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) WebhookDelete(ctx context.Context, accountid string) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, webhookDelete, accountid)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const webhookUpdate = `-- name: WebhookUpdate :exec
|
2024-04-21 10:07:44 +00:00
|
|
|
UPDATE tokens AS t
|
|
|
|
SET AccessToken = (update_data ->> 'access_token')::varchar(50),
|
|
|
|
RefreshToken = (update_data ->> 'refresh_token')::varchar(50),
|
|
|
|
Expiration = to_timestamp((update_data ->> 'expiration')::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours',
|
|
|
|
CreatedAt = to_timestamp((update_data ->> 'created_at')::bigint) AT TIME ZONE 'UTC' + INTERVAL '3 hours'
|
|
|
|
FROM json_array_elements($1::json) AS update_data
|
|
|
|
WHERE t.accountID = (update_data ->> 'account_id')::VARCHAR(30)
|
2024-04-17 17:18:52 +00:00
|
|
|
`
|
|
|
|
|
2024-04-20 09:22:38 +00:00
|
|
|
func (q *Queries) WebhookUpdate(ctx context.Context, dollar_1 json.RawMessage) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, webhookUpdate, dollar_1)
|
2024-04-17 17:18:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:33:15 +00:00
|
|
|
const workerStatProcess = `-- name: WorkerStatProcess :exec
|
|
|
|
WITH answer_aggregates AS (
|
|
|
|
SELECT
|
|
|
|
quiz_id,
|
|
|
|
COUNT(DISTINCT session) AS unique_true_answers_count
|
|
|
|
FROM
|
|
|
|
answer
|
|
|
|
WHERE
|
|
|
|
result = TRUE
|
|
|
|
GROUP BY
|
|
|
|
quiz_id
|
|
|
|
),
|
|
|
|
question_aggregates AS (
|
|
|
|
SELECT
|
|
|
|
q.id AS quiz_id,
|
|
|
|
COUNT(qs.id) AS total_questions
|
|
|
|
FROM
|
|
|
|
quiz q
|
|
|
|
INNER JOIN
|
|
|
|
question qs ON q.id = qs.quiz_id
|
|
|
|
WHERE
|
|
|
|
q.deleted = false
|
|
|
|
AND q.archived = false
|
|
|
|
AND qs.deleted = false
|
|
|
|
GROUP BY
|
|
|
|
q.id
|
|
|
|
),
|
|
|
|
session_times_aggregates AS (
|
|
|
|
SELECT
|
|
|
|
quiz_id, COUNT(session) as sess,
|
|
|
|
AVG(extract(epoch FROM session_time)) AS average_session_time
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
quiz_id,
|
|
|
|
session,
|
|
|
|
(MAX(created_at) - MIN(created_at)) AS session_time
|
|
|
|
FROM
|
|
|
|
answer
|
|
|
|
GROUP BY
|
|
|
|
quiz_id,
|
|
|
|
session
|
|
|
|
) AS all_sessions
|
|
|
|
GROUP BY
|
|
|
|
quiz_id
|
|
|
|
)
|
|
|
|
UPDATE quiz q
|
|
|
|
SET
|
|
|
|
questions_count = COALESCE(qa.total_questions, 0),
|
|
|
|
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
|
2024-04-18 10:16:03 +00:00
|
|
|
(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, utm, rules FROM quiz WHERE deleted = FALSE AND archived = FALSE) q_sub
|
2024-02-19 16:33:15 +00:00
|
|
|
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
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) WorkerStatProcess(ctx context.Context) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, workerStatProcess)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
const workerTimeoutProcess = `-- name: WorkerTimeoutProcess :exec
|
|
|
|
UPDATE quiz SET status = 'timeout' WHERE deleted = false AND due_to <> 0 AND due_to < EXTRACT(epoch FROM CURRENT_TIMESTAMP)
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) WorkerTimeoutProcess(ctx context.Context) error {
|
|
|
|
_, err := q.db.ExecContext(ctx, workerTimeoutProcess)
|
|
|
|
return err
|
|
|
|
}
|