use find_with_related, not find_also_related

find_also_related is 1:1, but we want 1:Many
This commit is contained in:
Bryan Stitt 2023-06-21 10:37:01 -07:00
parent 0f7b03fd70
commit d962b749a2

@ -14,7 +14,6 @@ use entities::{referee, referrer, user};
use ethers::types::Address; use ethers::types::Address;
use hashbrown::HashMap; use hashbrown::HashMap;
use http::StatusCode; use http::StatusCode;
use log::warn;
use migration::sea_orm; use migration::sea_orm;
use migration::sea_orm::prelude::{DateTime, Decimal}; use migration::sea_orm::prelude::{DateTime, Decimal};
use migration::sea_orm::ActiveModelTrait; use migration::sea_orm::ActiveModelTrait;
@ -148,25 +147,20 @@ pub async fn user_shared_referral_stats(
.context("getting replica db for user's revert logs")?; .context("getting replica db for user's revert logs")?;
// Get all referral records associated with this user // Get all referral records associated with this user
let referrals = referrer::Entity::find() let query_result = referrer::Entity::find()
.filter(referrer::Column::UserId.eq(user.id)) .filter(referrer::Column::UserId.eq(user.id))
.find_also_related(referee::Entity) .find_with_related(referee::Entity)
.all(db_replica.as_ref()) .all(db_replica.as_ref())
.await?; .await?;
// Return early if the user does not have any referred entities debug_assert_eq!(query_result.len(), 1);
if referrals.is_empty() {
let response_json = json!({
"referrals": [],
"used_referral_code": None::<()>,
"user": user,
});
let response = (StatusCode::OK, Json(response_json)).into_response(); let (referrer_record, referral_records) = query_result.into_iter().next().unwrap();
return Ok(response);
}
// For each related referral person, find the corresponding user-address let mut used_referral_code = None;
let mut referral_info = vec![];
// collect info about each referral
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct Info { struct Info {
credits_applied_for_referee: bool, credits_applied_for_referee: bool,
@ -175,11 +169,9 @@ pub async fn user_shared_referral_stats(
referred_address: Address, referred_address: Address,
} }
let mut out: Vec<Info> = Vec::new(); for referral_record in referral_records.into_iter() {
let mut used_referral_code = "".to_owned(); // This is only for safety purposes, because of the condition above we always know that there is at least one record used_referral_code = Some(referrer_record.referral_code.as_str());
for (referrer, referral_record) in referrals.into_iter() {
if let Some(referral_record) = referral_record {
used_referral_code = referrer.referral_code;
// The foreign key is never optional // The foreign key is never optional
let referred_user = user::Entity::find_by_id(referral_record.user_id) let referred_user = user::Entity::find_by_id(referral_record.user_id)
.one(db_replica.as_ref()) .one(db_replica.as_ref())
@ -194,16 +186,12 @@ pub async fn user_shared_referral_stats(
}; };
// Start inserting json's into this // Start inserting json's into this
out.push(info); referral_info.push(info);
} else {
// TODO: i don't think we want a warn. i think we want to change the query to return the data differently
warn!("no referral record for referrer: {:#?}", referrer);
}
} }
// Turn this into a response // Turn this into a response
let response_json = json!({ let response_json = json!({
"referrals": out, "referrals": referral_info,
"used_referral_code": used_referral_code, "used_referral_code": used_referral_code,
"user": user, "user": user,
}); });