| 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 |
use makeover_layout as layout; |
| 55 |
use quasi_router::screen::Figure; |
| 56 |
use quasi_router::{Action, Node, RegionKind, Request, Response, RouteError, Slot}; |
| 57 |
use quasi_webview::Webview; |
| 58 |
|
| 59 |
use super::Viewer; |
| 60 |
use crate::db; |
| 61 |
|
| 62 |
|
| 63 |
pub const SCREEN: &str = "payout_summary"; |
| 64 |
|
| 65 |
|
| 66 |
pub const PATH: &str = "/dashboard/tabs/payout-summary"; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub const REGION: &str = "payout-summary-section"; |
| 74 |
|
| 75 |
|
| 76 |
const STRIPE_PAYOUTS: &str = "https://dashboard.stripe.com/payouts"; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
pub struct BalanceView { |
| 81 |
available: String, |
| 82 |
pending: String, |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
pub fn screen(viewer: &Viewer, _request: Request) -> Result<Response, RouteError> { |
| 87 |
let user = viewer |
| 88 |
.block_on(db::users::get_user_by_id(&viewer.app.db, viewer.user.id)) |
| 89 |
.map_err(|_| RouteError::internal("your account could not be read"))? |
| 90 |
.ok_or_else(|| RouteError::not_found("that account is gone"))?; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
let balance = match (&viewer.app.stripe, &user.stripe_account_id) { |
| 96 |
(Some(stripe), Some(account_id)) => { |
| 97 |
match viewer.block_on(stripe.get_balance(account_id, user.settlement_currency)) { |
| 98 |
Ok(balance) => Some(BalanceView { |
| 99 |
available: crate::formatting::format_revenue( |
| 100 |
balance.available_cents, |
| 101 |
user.settlement_currency, |
| 102 |
), |
| 103 |
pending: crate::formatting::format_revenue( |
| 104 |
balance.pending_cents, |
| 105 |
user.settlement_currency, |
| 106 |
), |
| 107 |
}), |
| 108 |
Err(e) => { |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
tracing::warn!(error = ?e, "failed to fetch Stripe balance for payout summary"); |
| 113 |
None |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
_ => None, |
| 118 |
}; |
| 119 |
|
| 120 |
Ok(Response::fragment( |
| 121 |
REGION, |
| 122 |
card(balance.as_ref(), user.stripe_payouts_enabled), |
| 123 |
)) |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
fn card(balance: Option<&BalanceView>, payouts_enabled: bool) -> Node { |
| 128 |
|
| 129 |
|
| 130 |
let slot = Slot::new(REGION, RegionKind::Pane) |
| 131 |
.with(Node::Heading { |
| 132 |
level: layout::Heading::Subsection, |
| 133 |
text: "Payout Summary".into(), |
| 134 |
}) |
| 135 |
.with(stripe_link()); |
| 136 |
|
| 137 |
let Some(balance) = balance else { |
| 138 |
return Node::Region(slot.with(Node::failed( |
| 139 |
"Unable to load balance. Check your Stripe dashboard for details.", |
| 140 |
))); |
| 141 |
}; |
| 142 |
|
| 143 |
let slot = slot |
| 144 |
.with(Node::text( |
| 145 |
"Payouts are processed automatically via Stripe Connect.", |
| 146 |
)) |
| 147 |
.with(Node::stats([ |
| 148 |
Figure::new(balance.available.clone(), "Available Balance"), |
| 149 |
Figure::new(balance.pending.clone(), "Pending"), |
| 150 |
])); |
| 151 |
|
| 152 |
Node::Region(if payouts_enabled { |
| 153 |
slot |
| 154 |
} else { |
| 155 |
slot.with(Node::banner( |
| 156 |
layout::Tone::Warning, |
| 157 |
"Payouts are not yet enabled. Complete your Stripe account setup above.", |
| 158 |
)) |
| 159 |
}) |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
fn stripe_link() -> Node { |
| 168 |
Node::act("View in Stripe", Action::external(STRIPE_PAYOUTS)) |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
pub fn renderer(viewer: &Viewer) -> Webview { |
| 173 |
Webview::new().with_shell(viewer.shell()) |
| 174 |
} |
| 175 |
|
| 176 |
#[cfg(test)] |
| 177 |
mod tests { |
| 178 |
use super::*; |
| 179 |
use quasi_axum::Serves; |
| 180 |
|
| 181 |
fn balance() -> BalanceView { |
| 182 |
BalanceView { |
| 183 |
available: "$1,240.00".into(), |
| 184 |
pending: "$310.50".into(), |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
fn render(node: &Node) -> String { |
| 189 |
Webview::new().fragment(node) |
| 190 |
} |
| 191 |
|
| 192 |
#[test] |
| 193 |
fn the_region_is_the_one_the_payments_tab_leaves_empty() { |
| 194 |
|
| 195 |
|
| 196 |
let payments = include_str!("../../templates/partials/tabs/user_payments.html"); |
| 197 |
assert!(payments.contains(&format!("id=\"{REGION}\"")), "{REGION}"); |
| 198 |
assert!(payments.contains(&format!("hx-get=\"{PATH}\"")), "{PATH}"); |
| 199 |
} |
| 200 |
|
| 201 |
#[test] |
| 202 |
fn the_balances_are_a_strip_of_two_figures() { |
| 203 |
let html = render(&card(Some(&balance()), true)); |
| 204 |
|
| 205 |
assert!(html.contains("$1,240.00"), "{html}"); |
| 206 |
assert!(html.contains("Available Balance"), "{html}"); |
| 207 |
assert!(html.contains("$310.50"), "{html}"); |
| 208 |
assert!(html.contains("Pending"), "{html}"); |
| 209 |
} |
| 210 |
|
| 211 |
#[test] |
| 212 |
fn a_creator_without_payouts_enabled_is_told_so() { |
| 213 |
let html = render(&card(Some(&balance()), false)); |
| 214 |
assert!(html.contains("Payouts are not yet enabled."), "{html}"); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn a_creator_with_payouts_enabled_is_not() { |
| 219 |
let html = render(&card(Some(&balance()), true)); |
| 220 |
assert!(!html.contains("Payouts are not yet enabled."), "{html}"); |
| 221 |
} |
| 222 |
|
| 223 |
#[test] |
| 224 |
fn a_balance_that_did_not_load_is_a_readiness_and_not_a_second_card() { |
| 225 |
let html = render(&card(None, true)); |
| 226 |
|
| 227 |
assert!(html.contains("Unable to load balance."), "{html}"); |
| 228 |
|
| 229 |
|
| 230 |
assert!(!html.contains("Available Balance"), "{html}"); |
| 231 |
|
| 232 |
|
| 233 |
assert!(html.contains(STRIPE_PAYOUTS), "{html}"); |
| 234 |
} |
| 235 |
|
| 236 |
#[test] |
| 237 |
fn the_stripe_link_leaves_and_carries_no_verb() { |
| 238 |
let html = render(&stripe_link()); |
| 239 |
|
| 240 |
assert!( |
| 241 |
html.contains(&format!("href=\"{STRIPE_PAYOUTS}\"")), |
| 242 |
"{html}" |
| 243 |
); |
| 244 |
assert!(html.contains("target=\"_blank\""), "{html}"); |
| 245 |
assert!(html.contains("rel=\"noopener noreferrer\""), "{html}"); |
| 246 |
|
| 247 |
assert!(!html.contains("hx-get"), "{html}"); |
| 248 |
} |
| 249 |
} |
| 250 |
|