Skip to content

Commit

Permalink
Fix: first review special case - fix on user rate update for sqlx iss…
Browse files Browse the repository at this point in the history
…ue documented
  • Loading branch information
arkanoider committed Dec 23, 2024
1 parent 6f4f4b7 commit 8c2b063
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
10 changes: 10 additions & 0 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
},
"query": "\n UPDATE orders\n SET\n master_seller_pubkey = ?1\n WHERE id = ?2\n "
},
"071917afd7e1d00e2ae860779a1471d281f7aa45da61f7a7bdbd4625a3966a5d": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 6
}
},
"query": "\n UPDATE users SET last_rating = ?1, min_rating = ?2, max_rating = ?3, total_reviews = ?4, total_rating = ?5 WHERE pubkey = ?6\n "
},
"77ea98f6af16fa6e5a7d604965593700c563f88d88cb10b348bdc4200c87ad1d": {
"describe": {
"columns": [],
Expand Down
13 changes: 9 additions & 4 deletions src/app/rate_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use std::time::Duration;
use tokio::sync::Mutex;
use tracing::error;

const MAX_RATING: u8 = 5;
const MIN_RATING: u8 = 1;
pub const MAX_RATING: u8 = 5;
pub const MIN_RATING: u8 = 1;

pub async fn get_user_reputation(user: &str, my_keys: &Keys) -> Result<Option<Rating>> {
// Request NIP33 of the counterparts
Expand Down Expand Up @@ -151,8 +151,13 @@ pub async fn update_user_reputation_action(
user_to_vote.total_reviews += 1;
let old_rating = user_to_vote.total_rating as f64;
// recompute new rating
user_to_vote.total_rating = old_rating
+ ((user_to_vote.last_rating as f64) - old_rating) / (user_to_vote.total_reviews as f64);
if user_to_vote.total_reviews <= 1 {
user_to_vote.total_rating = rating.into();
} else {
user_to_vote.total_rating = old_rating
+ ((user_to_vote.last_rating as f64) - old_rating)
/ (user_to_vote.total_reviews as f64);
}
// Store last rating
user_to_vote.last_rating = rating.into();
// Create new rating event
Expand Down
32 changes: 15 additions & 17 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::app::rate_user::{MAX_RATING, MIN_RATING};
use mostro_core::dispute::Dispute;
use mostro_core::order::Order;
use mostro_core::order::Status;
Expand Down Expand Up @@ -391,7 +392,7 @@ pub async fn update_user_rating(
max_rating: i64,
total_reviews: i64,
total_rating: f64,
) -> anyhow::Result<User> {
) -> anyhow::Result<bool> {
// Validate public key format (32-bytes hex)
if !public_key.chars().all(|c| c.is_ascii_hexdigit()) || public_key.len() != 64 {
return Err(anyhow::anyhow!("Invalid public key format"));
Expand All @@ -403,7 +404,7 @@ pub async fn update_user_rating(
if !(0..=5).contains(&min_rating) || !(0..=5).contains(&max_rating) {
return Err(anyhow::anyhow!("Invalid min/max rating values"));
}
if min_rating > last_rating || last_rating > max_rating {
if MIN_RATING as i64 > last_rating || last_rating > MAX_RATING as i64 {
return Err(anyhow::anyhow!(
"Rating values must satisfy: min_rating <= last_rating <= max_rating"
));
Expand All @@ -414,25 +415,22 @@ pub async fn update_user_rating(
if total_rating < 0.0 || total_rating > (total_reviews * 5) as f64 {
return Err(anyhow::anyhow!("Invalid total rating"));
}
if let Ok(user) = sqlx::query_as::<_, User>(
let rows_affected = sqlx::query!(
r#"
UPDATE users SET last_rating = ?1, min_rating = ?2, max_rating = ?3, total_reviews = ?4, total_rating = ?5 WHERE pubkey = ?6
RETURNING *
"#,
last_rating,
min_rating,
max_rating,
total_reviews,
total_rating,
public_key,
)
.bind(last_rating)
.bind(min_rating)
.bind(max_rating)
.bind(total_reviews)
.bind(total_rating)
.bind(public_key)
.fetch_one(pool)
.await{
Ok(user)
}
else {
Err(anyhow::anyhow!("No user found"))
}
.execute(pool)
.await?
.rows_affected();

Ok(rows_affected > 0)
}

pub async fn is_assigned_solver(
Expand Down

0 comments on commit 8c2b063

Please sign in to comment.