add logic for save quiz settings

This commit is contained in:
Pavel 2024-09-06 14:29:19 +03:00
parent a1a88d3eed
commit d52e972df9
3 changed files with 76 additions and 24 deletions

@ -16,5 +16,44 @@ pub mut:
pub struct QuizConfig {
pub mut:
have_root string @[json: 'haveRoot']
have_root string @[json: 'haveRoot']
form_contact FormContact @[json: 'formContact']
result_info ResultInfo @[json: 'resultInfo']
}
pub struct FormContact {
pub mut:
title string @[json: 'title']
desc string @[json: 'desc']
fields FormFields @[json: 'fields']
button string @[json: 'button']
}
pub struct FormFields {
pub mut:
name ContactField @[json: 'name']
email ContactField @[json: 'email']
phone ContactField @[json: 'phone']
text ContactField @[json: 'text']
address ContactField @[json: 'address']
}
pub struct ContactField {
pub mut:
text string @[json: 'text']
inner_text string @[json: 'innerText']
key string @[json: 'key']
required bool @[json: 'required']
used bool @[json: 'used']
}
pub struct ResultInfo {
pub mut:
when string @[json: 'when']
share bool @[json: 'share']
replay bool @[json: 'replay']
theme string @[json: 'theme']
reply string @[json: 'reply']
repl_name string @[json: 'replname']
show_result_form string @[json: 'showResultForm']
}

@ -81,7 +81,7 @@ pub fn (mut r Repo) finish_state(id i64) ! {
}
}
pub fn (r Repo) get_state_by_tg_quiz_id(tg_id int,quiz_id i64)!models.RespondentState {
pub fn (mut r Repo) get_state_by_tg_quiz_id(tg_id int,quiz_id i64)!models.RespondentState {
result := sql r.pg_db {
select from models.RespondentState where quiz_id == quiz_id && telegram_id == tg_id && finish == false
}or{
@ -125,7 +125,7 @@ pub fn (mut r Repo) get_questions_by_quiz_id(quiz_id i64) ![]models.QuizQuestion
return question
}
pub fn (r Repo) get_all_results(id i64)![]models.QuizQuestion {
pub fn (mut r Repo) get_all_results(id i64)![]models.QuizQuestion {
result := sql r.pg_db {
select from models.QuizQuestion where quiz_id == id && question_type == models.question_type_result.str() && deleted ==false
} or {
@ -137,7 +137,7 @@ pub fn (r Repo) get_all_results(id i64)![]models.QuizQuestion {
// методы Quiz
pub fn (r Repo) get_quiz(quiz_id i64)!models.Quiz {
pub fn (mut r Repo) get_quiz(quiz_id i64)!models.Quiz {
result := sql r.pg_db{
select from models.Quiz where id == quiz_id && deleted == false && archived == false
}or{

@ -15,6 +15,8 @@ pub struct TgBot {
bot_id i64
liner bool
quiz_id i64
quiz_config models.QuizConfig
flag bool
}
// todo нужно наверное ограничить возврат пользователя на предыдущие вопросы, потому что храним только
@ -28,17 +30,13 @@ pub fn new_tg_bot(repo repository.Repo,bot_data models.TelegramIntegration) &TgB
stop_ch: chan int{}
bot_id: bot_data.id
quiz_id: bot_data.quiz_id
flag: false
}
spawn b.start()
return b
}
fn (mut b TgBot) start() {
bot_data := b.bot.get_me()
b.repo.update_bot_name(b.bot_id,bot_data.username)or{
println('$err')
return
}
mut last_offset :=0
for {
select {
@ -66,6 +64,14 @@ fn (mut b TgBot) start() {
}
fn (mut b TgBot) start_handler(update vgram.Update) {
if !b.flag{
b.add_settings()or{
println('$err')
return
}
b.flag = true
}
session := utils.generate_signature(b.quiz_id.str(),update.message.from.id.str())
chat_id_str := update.message.from.id.str()
// todo приветствие такое как дима писал надо
@ -76,20 +82,6 @@ fn (mut b TgBot) start_handler(update vgram.Update) {
return
}
quiz := b.repo.get_quiz(integration_data.quiz_id)or{
b.bot.send_message(chat_id: chat_id_str, text: 'Ошибка при получении анных опроса')
return
}
root := json.decode(models.QuizConfig, quiz.config) or {
b.bot.send_message(chat_id: chat_id_str, text: 'Ошибка при обработке контента опроса')
return
}
if root.have_root == '' {
b.liner = true
}
questions := b.repo.get_questions_by_quiz_id(integration_data.quiz_id) or {
b.bot.send_message(chat_id: chat_id_str, text: 'Ошибка при получении первого вопроса')
return
@ -902,7 +894,7 @@ fn (mut b TgBot)render_message(state_id i64,chat_id_str string, question models.
}
fn (mut b TgBot)render_result_question(state_id i64,chat_id_str string, question models.QuizQuestion, content models.QuizQuestionResult){
fn (mut b TgBot)render_result_question(state_id i64,chat_id_str string, question models.QuizQuestion, content models.QuizQuestionResult ){
b.repo.finish_state(state_id)or{
b.bot.send_message(chat_id: chat_id_str, text: 'Ошибка при завершении состояния респондента')
return
@ -929,4 +921,25 @@ fn validate_uploaded_file(file_name string, expected string) bool {
}
return false
}
fn (mut b TgBot)add_settings()!{
bot_data := b.bot.get_me()
b.repo.update_bot_name(b.bot_id,bot_data.username)or{
return error('$err')
}
quiz := b.repo.get_quiz(b.quiz_id)or{
return error('$err')
}
quiz_config := json.decode(models.QuizConfig, quiz.config) or {
return error('$err')
}
if quiz_config.have_root == '' {
b.liner = true
}
b.quiz_config = quiz_config
}