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