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 @yarn install
@-rm -rf ./static/cache/bundle/ @-rm -rf ./static/cache/bundle/
@-mkdir ./static/cache/bundle/css/ @-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 lint: ## Lint codebase
cargo fmt -v --all -- --emit files cargo fmt -v --all -- --emit files

View file

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

View file

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

View file

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