(
invitation_id: String,
password: String,
pool: web::Data<Pool>,
)
| 33 | } |
| 34 | |
| 35 | fn query( |
| 36 | invitation_id: String, |
| 37 | password: String, |
| 38 | pool: web::Data<Pool>, |
| 39 | ) -> Result<SlimUser, crate::errors::ServiceError> { |
| 40 | use crate::schema::{invitations::dsl::*, users::dsl::*}; |
| 41 | |
| 42 | let mut conn = pool.get().unwrap(); |
| 43 | |
| 44 | let invitation_id = invitation_id.parse::<Uuid>()?; |
| 45 | |
| 46 | invitations |
| 47 | .filter(id.eq(invitation_id)) |
| 48 | .load::<Invitation>(&mut conn) |
| 49 | .map_err(|_db_error| ServiceError::BadRequest("Invalid Invitation".into())) |
| 50 | .and_then(|mut result| { |
| 51 | if let Some(invitation) = result.pop() { |
| 52 | // if invitation is not expired |
| 53 | if invitation.expires_at > chrono::Local::now().naive_local() { |
| 54 | // try hashing the password, else return the error that will be converted to ServiceError |
| 55 | let password: String = hash_password(&password)?; |
| 56 | dbg!(&password); |
| 57 | |
| 58 | let user = User::from_details(invitation.email, password); |
| 59 | let inserted_user: User = diesel::insert_into(users) |
| 60 | .values(&user) |
| 61 | .get_result(&mut conn)?; |
| 62 | dbg!(&inserted_user); |
| 63 | |
| 64 | return Ok(inserted_user.into()); |
| 65 | } |
| 66 | } |
| 67 | Err(ServiceError::BadRequest("Invalid Invitation".into())) |
| 68 | }) |
| 69 | } |
no test coverage detected