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