forked from Trustless-OSS/Toss-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.rs
More file actions
92 lines (81 loc) · 2.77 KB
/
Copy pathroutes.rs
File metadata and controls
92 lines (81 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use axum::{
extract::State,
routing::{get, post},
Json, Router,
};
use crate::{
error::AppError,
middleware::auth::AuthedUser,
modules::contributor::model::{ConnectWalletBody, ContributorMeResponse, OkResponse},
modules::contributor::repository::{
get_contributor_by_github_id, list_assignments_for_contributor, upsert_contributor_wallet,
},
state::AppState,
};
async fn connect_wallet(
State(state): State<AppState>,
user: AuthedUser,
Json(body): Json<ConnectWalletBody>,
) -> Result<Json<OkResponse>, AppError> {
let payout_chain = body
.payout_chain
.as_deref()
.unwrap_or("stellar")
.to_string();
let payout_address = body
.payout_address
.as_deref()
.unwrap_or(&body.wallet)
.to_string();
upsert_contributor_wallet(
&state,
user.github_id,
user.github_username.as_deref().unwrap_or(""),
&payout_chain,
&payout_address,
)
.await?;
Ok(Json(OkResponse { ok: true }))
}
async fn get_contributor_me(
State(state): State<AppState>,
user: AuthedUser,
) -> Result<Json<ContributorMeResponse>, AppError> {
let contributor = get_contributor_by_github_id(&state, user.github_id).await?;
let Some(contributor) = contributor else {
return Ok(Json(ContributorMeResponse { contributor: None }));
};
let assignments = list_assignments_for_contributor(&state, contributor.id).await?;
let mut assignment_rows = Vec::with_capacity(assignments.len());
for (assignment, issue) in assignments {
assignment_rows.push(serde_json::json!({
"id": assignment.id,
"issue_id": assignment.issue_id,
"contributor_id": assignment.contributor_id,
"assigned_at": assignment.assigned_at,
"pr_number": assignment.pr_number,
"pr_merged_at": assignment.pr_merged_at,
"payout_status": assignment.payout_status,
"completion_percentage": assignment.completion_percentage,
"issues": issue,
}));
}
let contributor_json = serde_json::json!({
"id": contributor.id,
"github_user_id": contributor.github_user_id,
"github_username": contributor.github_username,
"stellar_wallet": contributor.stellar_wallet,
"payout_chain": contributor.payout_chain,
"payout_address": contributor.payout_address,
"created_at": contributor.created_at,
"assignments": assignment_rows,
});
Ok(Json(ContributorMeResponse {
contributor: Some(contributor_json),
}))
}
pub fn router() -> Router<AppState> {
Router::new()
.route("/api/wallet/connect", post(connect_wallet))
.route("/api/contributor/me", get(get_contributor_me))
}