Function can_user_make_suggestion

Source
pub async fn can_user_make_suggestion(
    collection: &Collection<User>,
    user_id: &str,
) -> Result<bool>
Expand description

⚙️ Checks if a user can make a new suggestion based on the time of their last suggestion.

This asynchronous function queries a MongoDB collection to check the last_suggestion_at timestamp of a user. It returns true if the user can make a new suggestion (i.e., more than an hour has passed since their last suggestion), and false otherwise.

§Parameters:

  • collection: A reference to a MongoDB collection of User documents.
  • user_id: A string slice representing the ID of the user to check.

§Returns:

  • Result<bool, mongodb::error::Error>: On success, returns true if the user can make a new suggestion, false otherwise. On failure, returns an Error from MongoDB detailing the issue.

§Example:

let can_suggest = can_user_make_suggestion(&collection, "123456789012345678").await?;
if can_suggest {
    println!("User can make a new suggestion.");
} else {
    println!("User cannot make a new suggestion yet.");
}

§⚠️ Notes:

  • The function uses MongoDB’s find_one method to search for a document with a matching user_id.
  • If no matching document is found, the function returns false.
  • The function checks if the last_suggestion_at timestamp is more than an hour ago.
  • User: Represents the structure of a user in the database.

§Dependencies:

  • Requires a MongoDB collection containing User documents.
  • Uses the chrono crate to handle time calculations.