| 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 |
use makeover_layout as layout; |
| 39 |
use quasi_router::screen::{Cell, Cells, Column}; |
| 40 |
use quasi_router::{Action, Node, RegionKind, Request, Response, RouteError, Slot}; |
| 41 |
use quasi_webview::Webview; |
| 42 |
|
| 43 |
use super::Viewer; |
| 44 |
use crate::db; |
| 45 |
|
| 46 |
|
| 47 |
pub const SCREEN: &str = "buyer_contacts"; |
| 48 |
|
| 49 |
|
| 50 |
pub const PATH: &str = "/dashboard/tabs/contacts"; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
const REGION: &str = "contacts-section"; |
| 57 |
|
| 58 |
|
| 59 |
pub struct BuyerView { |
| 60 |
username: String, |
| 61 |
email: String, |
| 62 |
purchases: String, |
| 63 |
spent: String, |
| 64 |
last_purchase: String, |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
pub fn screen(viewer: &Viewer, _request: Request) -> Result<Response, RouteError> { |
| 69 |
let contacts = viewer |
| 70 |
.block_on(db::transactions::get_seller_contacts( |
| 71 |
&viewer.app.db, |
| 72 |
viewer.user.id, |
| 73 |
)) |
| 74 |
.map_err(|_| RouteError::internal("your contacts could not be read"))?; |
| 75 |
|
| 76 |
let buyers: Vec<BuyerView> = contacts |
| 77 |
.into_iter() |
| 78 |
.map(|contact| BuyerView { |
| 79 |
username: contact.username, |
| 80 |
email: contact.email, |
| 81 |
purchases: contact.total_purchases.to_string(), |
| 82 |
spent: crate::formatting::format_revenue( |
| 83 |
contact.total_spent_cents, |
| 84 |
viewer.user.settlement_currency, |
| 85 |
), |
| 86 |
last_purchase: contact.last_purchase_at.format("%b %-d, %Y").to_string(), |
| 87 |
}) |
| 88 |
.collect(); |
| 89 |
|
| 90 |
Ok(Response::fragment(REGION, pane(&buyers))) |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
fn pane(buyers: &[BuyerView]) -> Node { |
| 95 |
let slot = Slot::new(REGION, RegionKind::Pane) |
| 96 |
.with(Node::section(format!("Shared Contacts ({})", buyers.len()))) |
| 97 |
.with(Node::text( |
| 98 |
"Buyers who opted to share their email at checkout. \ |
| 99 |
They can revoke sharing from their library.", |
| 100 |
)); |
| 101 |
|
| 102 |
if buyers.is_empty() { |
| 103 |
return Node::Region(slot.with(Node::empty( |
| 104 |
"No shared contacts yet. When buyers opt to share their email at checkout, \ |
| 105 |
they will appear here.", |
| 106 |
))); |
| 107 |
} |
| 108 |
|
| 109 |
Node::Region(slot.with(export()).with(table(buyers))) |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
fn export() -> Node { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
super::export_act::act("/api/export/contacts", "contacts.csv") |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
fn table(buyers: &[BuyerView]) -> Node { |
| 137 |
Node::Table { |
| 138 |
columns: vec![ |
| 139 |
Column::new("Username") |
| 140 |
.width(layout::Width::Content) |
| 141 |
.priority(layout::Priority::Essential), |
| 142 |
Column::new("Email") |
| 143 |
.width(layout::Width::Fill) |
| 144 |
.priority(layout::Priority::Essential), |
| 145 |
Column::new("Purchases").width(layout::Width::Content), |
| 146 |
Column::new("Total Spent").width(layout::Width::Content), |
| 147 |
Column::new("Last Purchase") |
| 148 |
.width(layout::Width::Content) |
| 149 |
.priority(layout::Priority::Optional), |
| 150 |
], |
| 151 |
rows: buyers |
| 152 |
.iter() |
| 153 |
.map(|buyer| { |
| 154 |
Cells::new([ |
| 155 |
Cell::new(buyer.username.clone()) |
| 156 |
.activate(Action::get(format!("/u/{}", buyer.username))), |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
Cell::new(buyer.email.clone()), |
| 163 |
Cell::new(buyer.purchases.clone()), |
| 164 |
Cell::new(buyer.spent.clone()), |
| 165 |
Cell::new(buyer.last_purchase.clone()), |
| 166 |
]) |
| 167 |
}) |
| 168 |
.collect(), |
| 169 |
|
| 170 |
|
| 171 |
more: None, |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
pub fn renderer(viewer: &Viewer) -> Webview { |
| 177 |
Webview::new().with_shell(viewer.shell()) |
| 178 |
} |
| 179 |
|
| 180 |
#[cfg(test)] |
| 181 |
mod tests { |
| 182 |
use super::*; |
| 183 |
use quasi_axum::Serves; |
| 184 |
|
| 185 |
fn buyer(username: &str) -> BuyerView { |
| 186 |
BuyerView { |
| 187 |
username: username.into(), |
| 188 |
email: format!("{username}@example.com"), |
| 189 |
purchases: "3".into(), |
| 190 |
spent: "$42.00".into(), |
| 191 |
last_purchase: "Aug 10, 2026".into(), |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
fn render(node: &Node) -> String { |
| 196 |
Webview::new().fragment(node) |
| 197 |
} |
| 198 |
|
| 199 |
#[test] |
| 200 |
fn the_region_is_the_one_the_payments_tab_leaves_empty() { |
| 201 |
|
| 202 |
|
| 203 |
let payments = include_str!("../../templates/partials/tabs/user_payments.html"); |
| 204 |
assert!(payments.contains(&format!("id=\"{REGION}\"")), "{REGION}"); |
| 205 |
assert!(payments.contains(&format!("hx-get=\"{PATH}\""))); |
| 206 |
} |
| 207 |
|
| 208 |
#[test] |
| 209 |
fn the_export_says_what_it_produces_rather_than_naming_a_behaviour() { |
| 210 |
let html = render(&export()); |
| 211 |
|
| 212 |
assert!(html.contains("data-saves=\"contacts.csv\""), "{html}"); |
| 213 |
assert!(html.contains("hx-post=\"/api/export/contacts\""), "{html}"); |
| 214 |
|
| 215 |
|
| 216 |
assert!(!html.contains("data-action"), "{html}"); |
| 217 |
assert!(!html.contains("exportCsvButton"), "{html}"); |
| 218 |
} |
| 219 |
|
| 220 |
#[test] |
| 221 |
fn the_export_address_is_one_the_api_answers() { |
| 222 |
|
| 223 |
|
| 224 |
let api = include_str!("../routes/api/mod.rs"); |
| 225 |
assert!(api.contains("/api/export/contacts"), "registered route"); |
| 226 |
} |
| 227 |
|
| 228 |
#[test] |
| 229 |
fn an_empty_list_offers_no_export_of_nothing() { |
| 230 |
|
| 231 |
|
| 232 |
let html = render(&pane(&[])); |
| 233 |
|
| 234 |
assert!(html.contains("Shared Contacts (0)"), "{html}"); |
| 235 |
assert!(html.contains("No shared contacts yet."), "{html}"); |
| 236 |
assert!(!html.contains("data-saves"), "{html}"); |
| 237 |
assert!(!html.contains("role=\"table\""), "{html}"); |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn a_buyers_name_goes_to_their_profile() { |
| 242 |
let html = render(&table(&[buyer("ada")])); |
| 243 |
|
| 244 |
assert!(html.contains("href=\"/u/ada\""), "{html}"); |
| 245 |
assert!( |
| 246 |
html.contains("Shared Contacts") || html.contains("ada@example.com"), |
| 247 |
"{html}" |
| 248 |
); |
| 249 |
|
| 250 |
|
| 251 |
assert!(!html.contains("mailto:"), "{html}"); |
| 252 |
} |
| 253 |
|
| 254 |
#[test] |
| 255 |
fn a_username_cannot_smuggle_markup() { |
| 256 |
let html = render(&table(&[buyer("<script>x()</script>")])); |
| 257 |
assert!(!html.contains("<script>x()"), "{html}"); |
| 258 |
} |
| 259 |
} |
| 260 |
|