Skip to content

Commit

Permalink
Organization payouts (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometrically authored Jan 11, 2024
1 parent 9c70c35 commit 05d2a96
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 32 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 51 additions & 24 deletions src/queue/payouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,6 @@ pub async fn process_payout(
redis: &RedisPool,
client: &clickhouse::Client,
) -> Result<(), ApiError> {
return Ok(());

let start: DateTime<Utc> = DateTime::from_naive_utc_and_offset(
(Utc::now() - Duration::days(1))
.date_naive()
Expand Down Expand Up @@ -621,40 +619,69 @@ pub async fn process_payout(

let mut projects_map: HashMap<i64, Project> = HashMap::new();

use futures::TryStreamExt;
let project_ids = multipliers
.values
.keys()
.map(|x| *x as i64)
.collect::<Vec<i64>>();

sqlx::query!(
let project_org_members = sqlx::query!(
"
SELECT m.id id, tm.user_id user_id, tm.payouts_split payouts_split
FROM mods m
INNER JOIN organizations o ON m.organization_id = o.id
INNER JOIN team_members tm on o.team_id = tm.team_id AND tm.accepted = TRUE
WHERE m.id = ANY($1) AND m.monetization_status = $2 AND m.organization_id IS NOT NULL
",
&project_ids,
MonetizationStatus::Monetized.as_str(),
)
.fetch_all(&mut *transaction)
.await?;

let project_team_members = sqlx::query!(
"
SELECT m.id id, tm.user_id user_id, tm.payouts_split payouts_split
FROM mods m
INNER JOIN team_members tm on m.team_id = tm.team_id AND tm.accepted = TRUE
WHERE m.id = ANY($1) AND m.monetization_status = $2
",
&multipliers
.values
.keys()
.map(|x| *x as i64)
.collect::<Vec<i64>>(),
&project_ids,
MonetizationStatus::Monetized.as_str(),
)
.fetch_many(&mut *transaction)
.try_for_each(|e| {
if let Some(row) = e.right() {
if let Some(project) = projects_map.get_mut(&row.id) {
project.team_members.push((row.user_id, row.payouts_split));
} else {
projects_map.insert(
row.id,
Project {
team_members: vec![(row.user_id, row.payouts_split)],
},
);
.fetch_all(&mut *transaction)
.await?;

for project_id in project_ids {
let team_members: HashMap<i64, Decimal> = project_team_members
.iter()
.filter(|r| r.id == project_id)
.map(|r| (r.user_id, r.payouts_split))
.collect();
let org_team_members: HashMap<i64, Decimal> = project_org_members
.iter()
.filter(|r| r.id == project_id)
.map(|r| (r.user_id, r.payouts_split))
.collect();

let mut all_team_members = vec![];

for (user_id, payouts_split) in org_team_members {
if !team_members.contains_key(&user_id) {
all_team_members.push((user_id, payouts_split));
}
}
for (user_id, payouts_split) in team_members {
all_team_members.push((user_id, payouts_split));
}

futures::future::ready(Ok(()))
})
.await?;
projects_map.insert(
project_id,
Project {
team_members: all_team_members,
},
);
}

let amount = Decimal::from(parse_var::<u64>("PAYOUTS_BUDGET").unwrap_or(0));

Expand Down
19 changes: 12 additions & 7 deletions src/routes/v2/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,17 @@ pub async fn project_search(
.into_iter()
.map(|facet| {
if let Some((key, operator, val)) = parse_facet(&facet) {
format!("{}{}{}", match key.as_str() {
"versions" => "game_versions",
"project_type" => "project_types",
"title" => "name",
x => x,
}, operator, val)
format!(
"{}{}{}",
match key.as_str() {
"versions" => "game_versions",
"project_type" => "project_types",
"title" => "name",
x => x,
},
operator,
val
)
} else {
facet.to_string()
}
Expand Down Expand Up @@ -127,7 +132,7 @@ pub async fn project_search(
}

/// Parses a facet into a key, operator, and value
fn parse_facet(facet: &String) -> Option<(String, String, String)> {
fn parse_facet(facet: &str) -> Option<(String, String, String)> {
let mut key = String::new();
let mut operator = String::new();
let mut val = String::new();
Expand Down
2 changes: 1 addition & 1 deletion src/routes/v2/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub async fn version_edit(
// Get the older version to get info from
let old_version = v3::versions::version_get_helper(
req.clone(),
info.clone().0,
(*info).0,
pool.clone(),
redis.clone(),
session_queue.clone(),
Expand Down

0 comments on commit 05d2a96

Please sign in to comment.