| 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 quasi_declare::declare; |
| 39 |
use quasi_router::{Request, Response, RouteError}; |
| 40 |
use quasi_webview::Webview; |
| 41 |
|
| 42 |
use super::Viewer; |
| 43 |
use crate::db; |
| 44 |
|
| 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 |
|
| 57 |
|
| 58 |
|
| 59 |
pub const REGION: &str = "contacts-section"; |
| 60 |
|
| 61 |
|
| 62 |
pub(crate) struct BuyerView { |
| 63 |
username: String, |
| 64 |
email: String, |
| 65 |
purchases: String, |
| 66 |
spent: String, |
| 67 |
last_purchase: String, |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
pub(crate) fn reading(viewer: &Viewer) -> Result<Vec<BuyerView>, RouteError> { |
| 73 |
let contacts = viewer |
| 74 |
.block_on(db::transactions::get_seller_contacts( |
| 75 |
&viewer.app.db, |
| 76 |
viewer.reader()?.id, |
| 77 |
)) |
| 78 |
.map_err(|_| RouteError::internal("your contacts could not be read"))?; |
| 79 |
|
| 80 |
let currency = viewer.reader()?.settlement_currency; |
| 81 |
Ok(contacts |
| 82 |
.into_iter() |
| 83 |
.map(|contact| BuyerView { |
| 84 |
username: contact.username, |
| 85 |
email: contact.email, |
| 86 |
purchases: contact.total_purchases.to_string(), |
| 87 |
spent: crate::formatting::format_revenue(contact.total_spent_cents, currency), |
| 88 |
last_purchase: contact.last_purchase_at.format("%b %-d, %Y").to_string(), |
| 89 |
}) |
| 90 |
.collect()) |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
pub fn screen(viewer: &Viewer, _request: Request) -> Result<Response, RouteError> { |
| 95 |
Ok(Response::fragment(REGION, pane(&reading(viewer)?))) |
| 96 |
} |
| 97 |
|
| 98 |
declare! { |
| 99 |
|
| 100 |
#[staged] |
| 101 |
pub(crate) shape pane(buyers: &[BuyerView]) -> Node; |
| 102 |
|
| 103 |
region REGION as Pane { |
| 104 |
section "Shared Contacts ({buyers.len()})"; |
| 105 |
text "Buyers who opted to share their email at checkout. \ |
| 106 |
They can revoke sharing from their library."; |
| 107 |
|
| 108 |
empty "No shared contacts yet. When buyers opt to share their email at checkout, \ |
| 109 |
they will appear here." when buyers.is_empty(); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
include export() unless buyers.is_empty(); |
| 114 |
include table(buyers) unless buyers.is_empty(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
declare! { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
#[must_use] |
| 134 |
#[constant] |
| 135 |
shape export() -> Node; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
include super::export_act::act("/api/export/contacts", "contacts.csv"); |
| 143 |
} |
| 144 |
|
| 145 |
declare! { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
#[staged] |
| 154 |
shape table(buyers: &[BuyerView]) -> Node; |
| 155 |
|
| 156 |
table { |
| 157 |
column "Username" { |
| 158 |
width Content; |
| 159 |
priority Essential; |
| 160 |
} |
| 161 |
column "Email" { |
| 162 |
width Fill; |
| 163 |
priority Essential; |
| 164 |
} |
| 165 |
column "Purchases" { |
| 166 |
width Content; |
| 167 |
} |
| 168 |
column "Total Spent" { |
| 169 |
width Content; |
| 170 |
} |
| 171 |
column "Last Purchase" { |
| 172 |
width Content; |
| 173 |
priority Optional; |
| 174 |
} |
| 175 |
|
| 176 |
for buyer in buyers { |
| 177 |
cells { |
| 178 |
cell buyer.username.clone() { |
| 179 |
activate to get "/u/{buyer.username}" navigating; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
cell buyer.email.clone(); |
| 187 |
cell buyer.purchases.clone(); |
| 188 |
cell buyer.spent.clone(); |
| 189 |
cell buyer.last_purchase.clone(); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
pub fn renderer(viewer: &Viewer) -> Webview { |
| 197 |
Webview::new().with_shell(viewer.shell()) |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
#[cfg(test)] |
| 205 |
pub(crate) fn sample(username: &str) -> BuyerView { |
| 206 |
BuyerView { |
| 207 |
username: username.into(), |
| 208 |
email: format!("{username}@example.com"), |
| 209 |
purchases: "3".into(), |
| 210 |
spent: "$42.00".into(), |
| 211 |
last_purchase: "Aug 10, 2026".into(), |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
#[cfg(test)] |
| 216 |
mod tests { |
| 217 |
use quasi_axum::Serves; |
| 218 |
use quasi_router::Node; |
| 219 |
|
| 220 |
use super::*; |
| 221 |
|
| 222 |
use super::sample as buyer; |
| 223 |
|
| 224 |
fn render(node: &Node) -> String { |
| 225 |
Webview::new().fragment(node) |
| 226 |
} |
| 227 |
|
| 228 |
#[test] |
| 229 |
fn the_region_is_the_one_the_payments_tab_leaves_empty() { |
| 230 |
|
| 231 |
|
| 232 |
let payments = include_str!("../../templates/partials/tabs/user_payments.html"); |
| 233 |
assert!(payments.contains(&format!("id=\"{REGION}\"")), "{REGION}"); |
| 234 |
assert!(payments.contains(&format!("hx-get=\"{PATH}\""))); |
| 235 |
} |
| 236 |
|
| 237 |
#[test] |
| 238 |
fn the_export_says_what_it_produces_rather_than_naming_a_behaviour() { |
| 239 |
let html = render(&export()); |
| 240 |
|
| 241 |
assert!(html.contains("data-saves=\"contacts.csv\""), "{html}"); |
| 242 |
assert!(html.contains("hx-post=\"/api/export/contacts\""), "{html}"); |
| 243 |
|
| 244 |
|
| 245 |
assert!(!html.contains("data-action"), "{html}"); |
| 246 |
assert!(!html.contains("exportCsvButton"), "{html}"); |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn the_export_address_is_one_the_api_answers() { |
| 251 |
|
| 252 |
|
| 253 |
let api = include_str!("../routes/api/mod.rs"); |
| 254 |
assert!(api.contains("/api/export/contacts"), "registered route"); |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
fn an_empty_list_offers_no_export_of_nothing() { |
| 259 |
|
| 260 |
|
| 261 |
let html = render(&pane(&[])); |
| 262 |
|
| 263 |
assert!(html.contains("Shared Contacts (0)"), "{html}"); |
| 264 |
assert!(html.contains("No shared contacts yet."), "{html}"); |
| 265 |
assert!(!html.contains("data-saves"), "{html}"); |
| 266 |
assert!(!html.contains("role=\"table\""), "{html}"); |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn a_buyers_name_goes_to_their_profile() { |
| 271 |
let html = render(&table(&[buyer("ada")])); |
| 272 |
|
| 273 |
assert!(html.contains("href=\"/u/ada\""), "{html}"); |
| 274 |
assert!( |
| 275 |
!html.contains("hx-get=\"/u/ada\""), |
| 276 |
"a navigation carries no verb: {html}" |
| 277 |
); |
| 278 |
assert!( |
| 279 |
html.contains("Shared Contacts") || html.contains("ada@example.com"), |
| 280 |
"{html}" |
| 281 |
); |
| 282 |
|
| 283 |
|
| 284 |
assert!(!html.contains("mailto:"), "{html}"); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
#[test] |
| 291 |
fn every_column_the_table_had_is_still_named() { |
| 292 |
let html = render(&table(&[buyer("ada")])); |
| 293 |
|
| 294 |
for heading in [ |
| 295 |
"Username", |
| 296 |
"Email", |
| 297 |
"Purchases", |
| 298 |
"Total Spent", |
| 299 |
"Last Purchase", |
| 300 |
] { |
| 301 |
assert!(html.contains(heading), "{heading} is gone from {html}"); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
#[test] |
| 306 |
fn a_username_cannot_smuggle_markup() { |
| 307 |
let html = render(&table(&[buyer("<script>x()</script>")])); |
| 308 |
assert!(!html.contains("<script>x()"), "{html}"); |
| 309 |
} |
| 310 |
} |
| 311 |
|