register on first visit

This commit is contained in:
Aravinth Manivannan 2021-10-11 09:30:24 +05:30
parent 22eb4feb6f
commit 71199aa9ce
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
5 changed files with 91 additions and 57 deletions

View file

@ -30,7 +30,7 @@ frontend: ## Build frontend assets
@yarn install
@-rm -rf ./static/cache/bundle/
@-mkdir ./static/cache/bundle/css/
@yarn run dart-sass -s compressed templates/main.scss ./static/cache/bundle/css/main.css
#@yarn run dart-sass -s compressed templates/main.scss ./static/cache/bundle/css/main.css
lint: ## Lint codebase
cargo fmt -v --all -- --emit files

View file

@ -1,5 +1,4 @@
CREATE TABLE IF NOT EXISTS survey_users (
ID UUID PRIMARY KEY NOT NULL UNIQUE,
created_at TIMESTAMPTZ NOT NULL,
device VARCHAR(100) NOT NULL
created_at TIMESTAMPTZ NOT NULL
)

View file

@ -14,13 +14,12 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::borrow::Cow;
use actix_identity::Identity;
use actix_web::http::header;
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use sqlx::types::time::OffsetDateTime;
use super::get_random;
use super::get_uuid;
use crate::errors::*;
use crate::AppData;
@ -43,39 +42,33 @@ pub mod runners {
use super::*;
pub async fn register_runner() -> ServiceResult<uuid::Uuid> {
pub async fn register_runner(data: &AppData) -> ServiceResult<uuid::Uuid> {
let mut uuid;
let now = OffsetDateTime::now_utc();
loop {
uuid = get_uuid();
// let res= sqlx::query!(
// "INSERT INTO
// kaizen_feedbacks (helpful , description, uuid, campaign_id, time, page_url)
// VALUES ($1, $2, $3, $4, $5,
// (SELECT ID from kaizen_campaign_pages WHERE page_url = $6))",
// &payload.helpful,
// &payload.description,
// &uuid,
// &campaign_id,
// &now,
// &payload.page_url,
// )
// .execute(&data.db)
// .await;
//
// if res.is_ok() {
// break;
// } else if let Err(sqlx::Error::Database(err)) = res {
// if err.code() == Some(Cow::from("23505"))
// && err.message().contains("kaizen_campaign_uuid_key")
// {
// continue;
// } else {
// return Err(sqlx::Error::Database(err).into());
// }
// }
// }
let res = sqlx::query!(
"
INSERT INTO survey_users (created_at, id) VALUES($1, $2)",
&now,
&uuid
)
.execute(&data.db)
.await;
if res.is_ok() {
break;
} else if let Err(sqlx::Error::Database(err)) = res {
if err.code() == Some(Cow::from("23505"))
&& err.message().contains("survey_users_id_key")
{
continue;
} else {
return Err(sqlx::Error::Database(err).into());
}
}
}
Ok(uuid)
}
@ -86,7 +79,7 @@ pub fn services(cfg: &mut web::ServiceConfig) {
}
#[my_codegen::post(path = "crate::V1_API_ROUTES.auth.register")]
async fn register(data: AppData, id: Identity) -> ServiceResult<impl Responder> {
let uuid = runners::register_runner().await?;
let uuid = runners::register_runner(&data).await?;
id.remember(uuid.to_string());
Ok(HttpResponse::Ok())
}

View file

@ -14,6 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::borrow::Cow;
use std::str::FromStr;
use actix_identity::Identity;
@ -22,7 +23,6 @@ use futures::future::try_join_all;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::get_random;
use super::get_uuid;
use crate::errors::*;
use crate::AppData;
@ -58,6 +58,12 @@ struct Submission {
benches: Vec<Bench>,
}
#[derive(Serialize, Deserialize)]
struct SubmissionProof {
token: String,
proof: String,
}
#[my_codegen::post(
path = "crate::V1_API_ROUTES.benches.submit",
wrap = "crate::CheckLogin"
@ -72,9 +78,20 @@ async fn submit(
let user_id = Uuid::from_str(&username).unwrap();
let payload = payload.into_inner();
sqlx::query!("INSERT INTO survey_responses (user_id, device_user_provided, device_software_recognised,
threads) VALUES ($1, $2, $3, $4)", &user_id, &payload.device_user_provided,
&payload.device_software_recognised, &payload.threads).execute(&data.db).await?;
sqlx::query!(
"INSERT INTO survey_responses (
user_id,
device_user_provided,
device_software_recognised,
threads
) VALUES ($1, $2, $3, $4);",
&user_id,
&payload.device_user_provided,
&payload.device_software_recognised,
&payload.threads
)
.execute(&data.db)
.await?;
struct ID {
id: i32,
@ -82,8 +99,12 @@ async fn submit(
let resp_id = sqlx::query_as!(
ID,
"SELECT ID FROM survey_responses
WHERE user_id = $1 AND device_software_recognised = $2",
"SELECT ID
FROM survey_responses
WHERE
user_id = $1
AND
device_software_recognised = $2;",
&user_id,
&payload.device_software_recognised
)
@ -95,7 +116,8 @@ async fn submit(
for bench in payload.benches.iter() {
let fut = sqlx::query!(
"INSERT INTO survey_benches
(resp_id, difficulty, duration) VALUES ($1, $2, $3)",
(resp_id, difficulty, duration)
VALUES ($1, $2, $3);",
&resp_id.id,
&bench.difficulty,
bench.duration
@ -105,20 +127,40 @@ async fn submit(
futs.push(fut);
}
let submitions_id = get_uuid();
let fut = sqlx::query!(
"INSERT INTO survey_response_tokens (resp_id, user_id, id)
VALUES ($1, $2, $3)",
&resp_id.id,
&user_id,
&submitions_id
)
.execute(&data.db);
futs.push(fut);
let mut submitions_id;
try_join_all(futs).await?;
Ok(HttpResponse::Ok())
loop {
submitions_id = get_uuid();
let res = sqlx::query!(
"INSERT INTO survey_response_tokens
(resp_id, user_id, id)
VALUES ($1, $2, $3);",
&resp_id.id,
&user_id,
&submitions_id
)
.execute(&data.db)
.await;
if res.is_ok() {
break;
} else if let Err(sqlx::Error::Database(err)) = res {
if err.code() == Some(Cow::from("23505"))
&& err.message().contains("survey_response_tokens_id_key")
{
continue;
} else {
return Err(sqlx::Error::Database(err).into());
}
}
}
let resp = SubmissionProof {
token: username,
proof: submitions_id.to_string(),
};
Ok(HttpResponse::Ok().json(resp))
}

View file

@ -24,7 +24,7 @@ use actix_web::{http, Error, FromRequest, HttpResponse};
use futures::future::{ok, Either, Ready};
pub const AUTH: &str = "/login"; //crate::PAGES.auth.login;
pub const AUTH: &str = crate::V1_API_ROUTES.auth.register;
pub struct CheckLogin;