feat: add logging for SurrealDB queries across multiple repositories
This commit is contained in:
@@ -7,6 +7,8 @@ use crate::{
|
||||
use anyhow::{Result, anyhow, bail};
|
||||
use chrono::{Duration, Utc};
|
||||
use surrealdb::sql::Thing;
|
||||
use tracing::instrument;
|
||||
use tracing::info;
|
||||
|
||||
pub struct AuthRepository<'a> {
|
||||
pub state: &'a AppState,
|
||||
@@ -17,6 +19,7 @@ impl<'a> AuthRepository<'a> {
|
||||
Self { state }
|
||||
}
|
||||
|
||||
#[instrument(skip(self, user), err)]
|
||||
pub async fn query_store_user(&self, user: UsersDetailQueryDto) -> Result<String> {
|
||||
if user.email.trim().is_empty() {
|
||||
bail!("Email is required");
|
||||
@@ -30,12 +33,14 @@ impl<'a> AuthRepository<'a> {
|
||||
permissions,
|
||||
};
|
||||
|
||||
info!(query = %format!("DELETE FROM {} WHERE id = '{}'", table, user_id), "Executing SurrealDB query");
|
||||
let _record: Option<UserCacheSchema> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
.delete::<Option<UserCacheSchema>>((table.clone(), user_id.clone()))
|
||||
.await?;
|
||||
|
||||
info!(query = %format!("CREATE {}:{}", table, user_id), "Executing SurrealDB query");
|
||||
let record: Option<UserCacheSchema> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
@@ -49,10 +54,12 @@ impl<'a> AuthRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email), err)]
|
||||
pub async fn query_get_stored_user(
|
||||
&self,
|
||||
email: String,
|
||||
) -> Result<UsersDetailQueryDto> {
|
||||
info!(query = %format!("SELECT FROM {} WHERE id = '{}'", ResourceEnum::UsersCache.to_string(), email), "Executing SurrealDB query");
|
||||
let user_cache: Option<UserCacheSchema> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
@@ -105,7 +112,9 @@ impl<'a> AuthRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email), err)]
|
||||
pub async fn query_delete_stored_user(&self, email: String) -> Result<String> {
|
||||
info!(query = %format!("DELETE FROM {} WHERE id = '{}'", ResourceEnum::UsersCache.to_string(), email), "Executing SurrealDB query");
|
||||
let record: Option<UsersDetailQueryDto> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
@@ -117,13 +126,16 @@ impl<'a> AuthRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email), err)]
|
||||
pub async fn query_get_stored_otp(&self, email: String) -> Result<u32> {
|
||||
let table = ResourceEnum::OtpCache.to_string();
|
||||
let key = (table.as_str(), email.as_str());
|
||||
info!(query = %format!("SELECT FROM {} WHERE id = '{}'", table, email), "Executing SurrealDB query");
|
||||
let result: Option<AuthOtpSchema> = self.state.surrealdb_mem.select(key).await?;
|
||||
match result {
|
||||
Some(data) => match Utc::now() > data.expires_at {
|
||||
true => {
|
||||
info!(query = %format!("DELETE FROM {} WHERE id = '{}'", table, email), "Executing SurrealDB query");
|
||||
let _ = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
@@ -137,9 +149,11 @@ impl<'a> AuthRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email, otp), err)]
|
||||
pub async fn query_store_otp(&self, email: String, otp: u32) -> Result<String> {
|
||||
let expires_at = Utc::now() + Duration::seconds(300);
|
||||
let table: String = ResourceEnum::OtpCache.to_string();
|
||||
info!(query = %format!("CREATE {}:{}", table, email), "Executing SurrealDB query");
|
||||
let record: Option<AuthOtpSchema> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
@@ -152,7 +166,9 @@ impl<'a> AuthRepository<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, email), err)]
|
||||
pub async fn query_delete_stored_otp(&self, email: String) -> Result<String> {
|
||||
info!(query = %format!("DELETE FROM {} WHERE id = '{}'", ResourceEnum::OtpCache.to_string(), email), "Executing SurrealDB query");
|
||||
let record: Option<AuthOtpSchema> = self
|
||||
.state
|
||||
.surrealdb_mem
|
||||
|
||||
Reference in New Issue
Block a user