Skip to content

Commit

Permalink
feat: registration by tg
Browse files Browse the repository at this point in the history
  • Loading branch information
brizzinck committed Jan 8, 2025
1 parent 65bd2c8 commit 3861bfc
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 40 deletions.
46 changes: 11 additions & 35 deletions src/api/hackathon_2024/user/post.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,33 @@
use crate::dto::request::hackathon_2024::user::RegistrationData;
use crate::models::hackathon_2024::user::UserJwt;
use crate::utils::actions;
use crate::utils::prelude_api::*;
use crate::utils::security::{encoded_data, verify_password};
use crate::utils::security::verify_password;
use crate::utils::validation;
use rocket::post;

#[post("/hackathon_2024/user/try_registration", data = "<registration_data>")]
pub async fn try_registration(
#[post("/hackathon_2024/user/registration_by_tg", data = "<data>")]
pub async fn registration_by_tg(
db_pool: &DbState,
registration_data: Json<RegistrationData>,
data: Json<RegistrationData>,
) -> Result<(), ApiError> {
let registration_data = registration_data.into_inner();
let data = data.into_inner();

let RegistrationData {
user_data,
team_data,
..
} = registration_data;
} = data;

validation::data::hackathon_2024::team::check_team_password(team_data.password.as_str())?;
validation::data::hackathon_2024::user::field(&user_data)?;

let team = crate::diesel::utils::hackathon_2024::team::fetch::by_id(db_pool, team_data.id)?;
verify_password(team_data.password, team.password_registration.as_str())?;

let _ = crate::diesel::utils::hackathon_2024::university::fetch::by_id(
db_pool,
user_data.university_id,
verify_password(
team_data.password.as_str(),
team.password_registration.as_str(),
)?;

let jwt_user = UserJwt::from(&user_data, 5);

let token = encoded_data(&jwt_user)?;
let id = crate::diesel::utils::hackathon_2024::user::insert::new(db_pool, user_data)?;

actions::send_letter::send_letter(
"HACKATHON".to_owned(),
format!(
"<html>
<body>
<p>Hello!</p>
<a href=\"{}\">Accept</a>
</body>
</html>",
token
)
.to_string(),
user_data.nickname_tg.to_owned(),
)?;
info!("Succeed create user with id {id}");

info!(
"verify email sent to {}, jwt token - {token}",
user_data.nickname_tg
);
Ok(())
}
1 change: 1 addition & 0 deletions src/dto/request/hackathon_2024/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::diesel::models::hackathon_2024::user::HackathonUser2024Insertable;
use crate::dto::request::hackathon_2024::team::TeamRegistrationData;
use serde::Deserialize;

#[non_exhaustive]
#[derive(Deserialize)]
pub struct RegistrationData {
pub user_data: HackathonUser2024Insertable,
Expand Down
3 changes: 3 additions & 0 deletions src/models/hackathon_2024/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::diesel::models::hackathon_2024::user::HackathonUser2024Insertable;
use serde::{Deserialize, Serialize};

#[allow(dead_code)]
#[derive(Debug, Serialize, Deserialize)]
pub struct UserJwt {
pub first_name: String,
Expand All @@ -13,6 +14,7 @@ pub struct UserJwt {
}

impl UserJwt {
#[allow(dead_code)]
pub fn from(data: &HackathonUser2024Insertable, exp: i64) -> Self {
let exp = chrono::Utc::now()
.checked_add_signed(chrono::Duration::minutes(exp))
Expand All @@ -28,6 +30,7 @@ impl UserJwt {
exp,
}
}
#[allow(dead_code)]
pub fn into(self) -> HackathonUser2024Insertable {
HackathonUser2024Insertable {
first_name: self.first_name,
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Server {
// /test/*
api::test::get::ping,
// /hackathon_2024/user/*
api::hackathon_2024::user::post::try_registration,
api::hackathon_2024::user::post::registration_by_tg,
api::hackathon_2024::user::get::confirm_new_user,
api::hackathon_2024::user::get::all,
api::hackathon_2024::user::put::by_id,
Expand Down
1 change: 1 addition & 0 deletions src/utils/actions/send_letter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::utils::env_configuration::EnvConfiguration;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};

#[allow(dead_code)]
pub fn send_letter(
title_letter: String,
body_letter: String,
Expand Down
1 change: 1 addition & 0 deletions src/utils/env_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct EnvConfiguration {
pub main_url: String,
#[allow(dead_code)]
pub smtp_email: String,
#[allow(dead_code)]
pub smtp_password: String,
pub server_port: u16,
/* WILL UNCOMMENT WHEN IN SCHEMA.RS EXISTS USER_ROLE!!!
Expand Down
1 change: 1 addition & 0 deletions src/utils/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn decoded_data<T: DeserializeOwned>(
.map_err(|err| ApiError::TokenDecodeError(err.to_string()))
}

#[allow(dead_code)]
pub fn encoded_data<T: Serialize>(value: &T) -> Result<String, ApiError> {
encode(
&Header::new(Algorithm::HS512),
Expand Down
12 changes: 12 additions & 0 deletions src/utils/validation/data/fields.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::api_error::ApiError;
use crate::utils::validation::validation_string::Validate;

#[allow(dead_code)]
pub fn check_email(email: impl AsRef<str>, error_message: impl AsRef<str>) -> Result<(), ApiError> {
if email.as_ref().is_email() {
Ok(())
Expand All @@ -9,6 +10,17 @@ pub fn check_email(email: impl AsRef<str>, error_message: impl AsRef<str>) -> Re
}
}

pub fn check_nickname_tg(
nickname_tg: impl AsRef<str>,
error_message: impl AsRef<str>,
) -> Result<(), ApiError> {
if nickname_tg.as_ref().is_nickname_tg() {
Ok(())
} else {
Err(ApiError::ValidationError(error_message.as_ref().to_owned()))
}
}

pub fn check_name(
name: impl AsRef<str>,
length: usize,
Expand Down
5 changes: 3 additions & 2 deletions src/utils/validation/data/hackathon_2024/team.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::diesel::models::hackathon_2024::team::HackathonTeam2024Insertable;
use crate::error::api_error::ApiError;
use crate::utils::validation::data::fields::{check_email, check_name, check_password};
use crate::utils::validation::data::fields::{check_name, check_nickname_tg, check_password};

#[allow(dead_code)]
pub fn field(new_team: &HackathonTeam2024Insertable) -> Result<(), ApiError> {
check_email(
check_nickname_tg(
&new_team.nickname_tg,
format!("Email don't correct {}", new_team.nickname_tg).as_str(),
)?;
Expand All @@ -30,5 +30,6 @@ pub fn check_team_password(password: impl AsRef<str>) -> Result<(), ApiError> {
)
.as_str(),
)?;

Ok(())
}
4 changes: 2 additions & 2 deletions src/utils/validation/data/hackathon_2024/user.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::diesel::models::hackathon_2024::user::HackathonUser2024Insertable;
use crate::error::api_error::ApiError;
use crate::utils::validation::data::fields::{check_email, check_name, check_phone};
use crate::utils::validation::data::fields::{check_name, check_nickname_tg, check_phone};

pub fn field(new_user: &HackathonUser2024Insertable) -> Result<(), ApiError> {
check_email(
check_nickname_tg(
new_user.nickname_tg.as_str(),
format!("Email don't correct {}", new_user.nickname_tg),
)?;
Expand Down
10 changes: 10 additions & 0 deletions src/utils/validation/validation_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub trait Validate {
#[allow(dead_code)]
fn is_email(&self) -> bool;

#[allow(dead_code)]
fn is_nickname_tg(&self) -> bool;

#[allow(dead_code)]
fn is_phone(&self) -> bool;

Expand All @@ -33,6 +36,13 @@ impl<T: AsRef<str>> Validate for T {
}
}

fn is_nickname_tg(&self) -> bool {
match Regex::new(r"(?i)^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$") {
Ok(nickname_regex) => nickname_regex.is_match(self.as_ref()),
Err(_) => false,
}
}

fn is_phone(&self) -> bool {
match Regex::new(r"^(\+380|0)\d{9}$") {
Ok(phone_regex) => phone_regex.is_match(self.as_ref()),
Expand Down

0 comments on commit 3861bfc

Please sign in to comment.