Files
imphnen-backend-service/src/apps/v1/auth/auth_repository.rs
T

103 lines
2.6 KiB
Rust
Raw Normal View History

2025-03-21 17:00:19 +07:00
use crate::{AppState, ResourceEnum, UsersSchema};
2025-03-19 23:17:50 +07:00
use anyhow::{anyhow, bail, Result};
2025-03-21 17:00:19 +07:00
use chrono::{Duration, Utc};
use super::AuthOtpSchema;
2025-02-08 00:58:54 +07:00
2025-03-14 09:53:20 +07:00
pub struct AuthRepository<'a> {
state: &'a AppState,
2025-02-08 00:58:54 +07:00
}
2025-02-26 15:45:05 +07:00
2025-03-14 09:53:20 +07:00
impl<'a> AuthRepository<'a> {
pub fn new(state: &'a AppState) -> Self {
Self { state }
}
2025-02-26 15:45:05 +07:00
2025-03-21 17:00:19 +07:00
pub async fn query_store_user(&self, user: UsersSchema) -> Result<String> {
let user_clone = user.clone();
let record: Option<UsersSchema> = self
.state
.surrealdb_mem
.update((ResourceEnum::UsersCache.to_string(), user.email))
.content(user_clone)
.await?;
match record {
Some(_) => Ok("Success store user data".to_string()),
None => bail!("Failed store user data"),
2025-03-14 17:50:37 +07:00
}
}
2025-03-21 17:00:19 +07:00
pub async fn query_get_stored_user(&self, email: String) -> Result<UsersSchema> {
let user: Option<UsersSchema> = self
.state
.surrealdb_mem
.select((ResourceEnum::UsersCache.to_string(), email))
.await?;
match user {
Some(u) => Ok(u),
2025-03-18 14:13:53 +07:00
None => bail!("No stored user data found"),
2025-03-14 17:50:37 +07:00
}
}
2025-03-21 17:00:19 +07:00
pub async fn query_delete_stored_user(&self, email: String) -> Result<String> {
let record: Option<String> = self
.state
.surrealdb_mem
.delete((ResourceEnum::UsersCache.to_string(), email))
.await?;
match record {
Some(_) => Ok("Success delete stored user".to_string()),
None => bail!("Failed delete stored user"),
}
}
pub async fn query_get_stored_otp(&self, email: String) -> Result<u32> {
let otp: Option<AuthOtpSchema> = self
.state
.surrealdb_mem
.select((ResourceEnum::OtpCache.to_string(), &email))
.await?;
match otp {
Some(data) => {
if Utc::now() > data.expires_at {
let _: Option<AuthOtpSchema> = self
.state
.surrealdb_mem
.delete((ResourceEnum::OtpCache.to_string(), &email))
.await?;
Err(anyhow!("OTP expired"))
} else {
Ok(data.otp)
}
2025-03-19 23:17:50 +07:00
}
2025-03-21 17:00:19 +07:00
None => Err(anyhow!("No stored OTP found")),
2025-03-19 23:17:50 +07:00
}
}
2025-03-21 17:00:19 +07:00
pub async fn query_store_otp(&self, email: String, otp: u32) -> Result<String> {
let expires_at = Utc::now() + Duration::seconds(300); // 5 menit
let record: Option<AuthOtpSchema> = self
.state
.surrealdb_mem
.create((ResourceEnum::OtpCache.to_string(), email))
.content(AuthOtpSchema { otp, expires_at })
.await?;
match record {
Some(_) => Ok("Success store otp".to_string()),
None => bail!("Failed store otp"),
2025-03-19 23:17:50 +07:00
}
}
2025-03-21 17:00:19 +07:00
pub async fn query_delete_stored_otp(&self, email: String) -> Result<String> {
let record: Option<String> = self
.state
.surrealdb_mem
.delete((ResourceEnum::OtpCache.to_string(), email))
.await?;
match record {
Some(_) => Ok("Success delete stored otp".to_string()),
None => bail!("Failed delete stored otp"),
2025-03-19 23:17:50 +07:00
}
}
2025-02-26 15:45:05 +07:00
}