| 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_declare::declare; |
| 40 |
use quasi_router::screen::Tag; |
| 41 |
use quasi_router::{Method, Request, Response, RouteError}; |
| 42 |
use quasi_webview::Webview; |
| 43 |
|
| 44 |
use super::Viewer; |
| 45 |
use crate::db::{self, ItemId, TransactionId}; |
| 46 |
use crate::types::SaleRow; |
| 47 |
|
| 48 |
|
| 49 |
pub const REGION: &str = "item-sales"; |
| 50 |
|
| 51 |
|
| 52 |
pub const NEST: &str = "/dashboard/described/item-sales"; |
| 53 |
|
| 54 |
|
| 55 |
const REFUND: &str = "/{item}/{transaction}"; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
pub const WRITES: &[(Method, &str, super::Screen)] = &[(Method::Post, REFUND, refund)]; |
| 60 |
|
| 61 |
|
| 62 |
#[must_use] |
| 63 |
pub fn fragment(item: ItemId, sales: &[SaleRow], export_route: &str) -> String { |
| 64 |
use quasi_axum::Serves as _; |
| 65 |
|
| 66 |
Webview::new().fragment(&pane(item, sales, export_route)) |
| 67 |
} |
| 68 |
|
| 69 |
declare! { |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
shape pane(item: ItemId, sales: &[SaleRow], export_route: &str) -> Node; |
| 76 |
|
| 77 |
region REGION as Pane { |
| 78 |
section "Sales"; |
| 79 |
|
| 80 |
empty "No sales yet. Sales will appear here once your first purchase is completed." |
| 81 |
when sales.is_empty(); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
include super::export_act::act(export_route, "item-sales.csv") |
| 86 |
unless sales.is_empty(); |
| 87 |
include table(item, sales) unless sales.is_empty(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
declare! { |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
shape table(item: ItemId, sales: &[SaleRow]) -> Node; |
| 100 |
|
| 101 |
table { |
| 102 |
column COL_DATE { |
| 103 |
width Content; |
| 104 |
priority Essential; |
| 105 |
} |
| 106 |
column COL_BUYER { |
| 107 |
width Fill; |
| 108 |
} |
| 109 |
column COL_AMOUNT { |
| 110 |
width Content; |
| 111 |
} |
| 112 |
column COL_STATUS { |
| 113 |
width Content; |
| 114 |
} |
| 115 |
column COL_ACTS { |
| 116 |
width Content; |
| 117 |
} |
| 118 |
|
| 119 |
for sale in sales.iter() { |
| 120 |
include row(item, sale); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
const COL_DATE: &str = "Date"; |
| 132 |
const COL_BUYER: &str = "Buyer"; |
| 133 |
const COL_AMOUNT: &str = "Amount"; |
| 134 |
const COL_STATUS: &str = "Status"; |
| 135 |
const COL_ACTS: &str = ""; |
| 136 |
|
| 137 |
declare! { |
| 138 |
|
| 139 |
shape row(item: ItemId, sale: &SaleRow) -> Row; |
| 140 |
|
| 141 |
cells { |
| 142 |
cell at COL_DATE sale.date.clone(); |
| 143 |
cell at COL_BUYER sale.buyer.clone(); |
| 144 |
cell at COL_AMOUNT sale.amount_display.clone(); |
| 145 |
cell at COL_STATUS "" { |
| 146 |
token badge(sale); |
| 147 |
} |
| 148 |
cell at COL_ACTS "" { |
| 149 |
act "Refund" |
| 150 |
to post "{NEST}/{item}/{sale.transaction_id}" awaiting |
| 151 |
when sale.refundable |
| 152 |
{ |
| 153 |
tone Danger; |
| 154 |
confirm "Issue a full refund for {sale.amount_display}? This cannot be undone."; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
fn badge(sale: &SaleRow) -> Tag { |
| 165 |
Tag::badge(sale.status.clone()).tone(tone(sale.status_tone)) |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
fn tone(status_tone: &str) -> layout::Tone { |
| 170 |
match status_tone { |
| 171 |
"success" => layout::Tone::Success, |
| 172 |
"warning" => layout::Tone::Warning, |
| 173 |
"danger" => layout::Tone::Danger, |
| 174 |
_ => layout::Tone::Neutral, |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
#[must_use] |
| 184 |
pub fn rows(sales: &[db::DbTransaction]) -> Vec<SaleRow> { |
| 185 |
sales |
| 186 |
.iter() |
| 187 |
.map(|tx| { |
| 188 |
let buyer_display = tx |
| 189 |
.guest_email |
| 190 |
.clone() |
| 191 |
.or_else(|| tx.buyer_id.map(|_| "Registered user".to_string())) |
| 192 |
.unwrap_or_else(|| "Unknown".to_string()); |
| 193 |
let cents = tx.amount_cents.as_i64(); |
| 194 |
SaleRow { |
| 195 |
transaction_id: tx.id.to_string(), |
| 196 |
buyer: buyer_display, |
| 197 |
amount_display: if cents == 0 { |
| 198 |
"Free".to_string() |
| 199 |
} else { |
| 200 |
|
| 201 |
|
| 202 |
crate::formatting::format_revenue(cents, tx.currency()) |
| 203 |
}, |
| 204 |
status: tx.status.to_string(), |
| 205 |
status_tone: tx.status.badge_status().tone(), |
| 206 |
date: tx.created_at.format("%Y-%m-%d %H:%M").to_string(), |
| 207 |
refundable: tx.status == db::TransactionStatus::Completed |
| 208 |
&& tx.stripe_payment_intent_id.is_some() |
| 209 |
&& cents > 0, |
| 210 |
} |
| 211 |
}) |
| 212 |
.collect() |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
pub fn refund(viewer: &Viewer, request: Request) -> Result<Response, RouteError> { |
| 217 |
let captures = request.captures; |
| 218 |
|
| 219 |
let item: ItemId = captures |
| 220 |
.get("item") |
| 221 |
.and_then(|id| id.parse::<uuid::Uuid>().ok()) |
| 222 |
.ok_or_else(|| RouteError::not_found("no such item"))? |
| 223 |
.into(); |
| 224 |
let transaction: TransactionId = captures |
| 225 |
.get("transaction") |
| 226 |
.and_then(|id| id.parse::<uuid::Uuid>().ok()) |
| 227 |
.ok_or_else(|| RouteError::not_found("no such transaction"))? |
| 228 |
.into(); |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
viewer |
| 233 |
.block_on(crate::payments::refund::refund( |
| 234 |
&viewer.app.db, |
| 235 |
viewer.app.payment_caps.refundable.as_ref(), |
| 236 |
viewer.reader()?, |
| 237 |
item, |
| 238 |
transaction, |
| 239 |
)) |
| 240 |
.map_err(refused)?; |
| 241 |
|
| 242 |
answer(viewer, item) |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
fn refused(error: crate::error::AppError) -> RouteError { |
| 253 |
use crate::error::AppError; |
| 254 |
|
| 255 |
match error { |
| 256 |
AppError::NotFound => RouteError::not_found("no such transaction"), |
| 257 |
AppError::Forbidden => RouteError::not_found("no such transaction"), |
| 258 |
AppError::BadRequest(said) => RouteError::conflict(said), |
| 259 |
AppError::ServiceUnavailable(said) => RouteError::conflict(said), |
| 260 |
_ => RouteError::internal("that refund could not be issued"), |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
fn answer(viewer: &Viewer, item: ItemId) -> Result<Response, RouteError> { |
| 266 |
let sales = viewer |
| 267 |
.block_on(db::transactions::get_sales_by_item( |
| 268 |
&viewer.app.db, |
| 269 |
item, |
| 270 |
viewer.reader()?.id, |
| 271 |
)) |
| 272 |
.map_err(|_| RouteError::internal("the sales could not be read"))?; |
| 273 |
|
| 274 |
let rows = rows(&sales); |
| 275 |
let export_route = format!("/api/export/items/{item}/sales"); |
| 276 |
|
| 277 |
Ok(Response::fragment(REGION, pane(item, &rows, &export_route))) |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
pub fn renderer(viewer: &Viewer) -> Webview { |
| 282 |
Webview::new().with_shell(viewer.shell()) |
| 283 |
} |
| 284 |
|
| 285 |
#[cfg(test)] |
| 286 |
mod tests { |
| 287 |
use super::*; |
| 288 |
|
| 289 |
const ITEM: &str = "00000000-0000-0000-0000-0000000000aa"; |
| 290 |
const TX: &str = "00000000-0000-0000-0000-0000000000bb"; |
| 291 |
|
| 292 |
fn item() -> ItemId { |
| 293 |
ITEM.parse::<uuid::Uuid>().unwrap().into() |
| 294 |
} |
| 295 |
|
| 296 |
fn sale(refundable: bool) -> SaleRow { |
| 297 |
SaleRow { |
| 298 |
transaction_id: TX.into(), |
| 299 |
buyer: "buyer@example.com".into(), |
| 300 |
amount_display: "$9.99".into(), |
| 301 |
status: "completed".into(), |
| 302 |
status_tone: "success", |
| 303 |
date: "2026-08-26 10:00".into(), |
| 304 |
refundable, |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
fn render(sales: &[SaleRow]) -> String { |
| 309 |
fragment(item(), sales, &format!("/api/export/items/{ITEM}/sales")) |
| 310 |
} |
| 311 |
|
| 312 |
#[test] |
| 313 |
fn the_panel_carries_the_region_the_strip_draws() { |
| 314 |
|
| 315 |
|
| 316 |
let html = render(&[sale(true)]); |
| 317 |
assert!(html.contains(&format!("id=\"{REGION}\"")), "{html}"); |
| 318 |
} |
| 319 |
|
| 320 |
#[test] |
| 321 |
fn the_refund_addresses_the_nest_and_not_the_api_route() { |
| 322 |
let html = render(&[sale(true)]); |
| 323 |
|
| 324 |
assert!( |
| 325 |
html.contains(&format!("hx-post=\"{NEST}/{ITEM}/{TX}\"")), |
| 326 |
"{html}" |
| 327 |
); |
| 328 |
|
| 329 |
|
| 330 |
assert!(!html.contains("/api/items/"), "{html}"); |
| 331 |
} |
| 332 |
|
| 333 |
#[test] |
| 334 |
fn nothing_here_goes_through_the_dispatcher() { |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
let html = render(&[sale(true)]); |
| 339 |
|
| 340 |
assert!(!html.contains("data-after"), "{html}"); |
| 341 |
assert!(!html.contains("data-arg"), "{html}"); |
| 342 |
assert!(!html.contains("data-action"), "{html}"); |
| 343 |
|
| 344 |
|
| 345 |
assert!(!html.contains("hx-swap=\"outerHTML\""), "{html}"); |
| 346 |
} |
| 347 |
|
| 348 |
#[test] |
| 349 |
fn a_sale_that_cannot_be_refunded_offers_no_button() { |
| 350 |
let html = render(&[sale(false)]); |
| 351 |
assert!(!html.contains("Refund"), "{html}"); |
| 352 |
assert!(!html.contains(NEST), "{html}"); |
| 353 |
} |
| 354 |
|
| 355 |
#[test] |
| 356 |
fn the_refund_still_asks_before_it_moves_money() { |
| 357 |
let html = render(&[sale(true)]); |
| 358 |
assert!(html.contains("This cannot be undone."), "{html}"); |
| 359 |
assert!(html.contains("$9.99"), "{html}"); |
| 360 |
} |
| 361 |
|
| 362 |
#[test] |
| 363 |
fn an_empty_panel_offers_no_export() { |
| 364 |
|
| 365 |
|
| 366 |
let html = render(&[]); |
| 367 |
|
| 368 |
assert!(html.contains("No sales yet."), "{html}"); |
| 369 |
assert!(!html.contains("Export CSV"), "{html}"); |
| 370 |
assert!(!html.contains("role=\"table\""), "{html}"); |
| 371 |
} |
| 372 |
|
| 373 |
#[test] |
| 374 |
fn a_populated_panel_offers_the_export() { |
| 375 |
let html = render(&[sale(true)]); |
| 376 |
assert!(html.contains("Export CSV"), "{html}"); |
| 377 |
assert!( |
| 378 |
html.contains(&format!("/api/export/items/{ITEM}/sales")), |
| 379 |
"{html}" |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
#[test] |
| 386 |
fn every_column_the_table_had_is_still_named() { |
| 387 |
let html = render(&[sale(true)]); |
| 388 |
|
| 389 |
for heading in [COL_DATE, COL_BUYER, COL_AMOUNT, COL_STATUS] { |
| 390 |
assert!(html.contains(heading), "{heading} is gone from {html}"); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
#[test] |
| 398 |
fn a_sale_status_keeps_the_tone_the_template_set() { |
| 399 |
let mut refunded = sale(false); |
| 400 |
refunded.status = "refunded".to_owned(); |
| 401 |
refunded.status_tone = "warning"; |
| 402 |
|
| 403 |
let html = render(&[refunded]); |
| 404 |
|
| 405 |
assert!(html.contains("refunded"), "{html}"); |
| 406 |
assert!(html.contains(r#"data-tone="warning""#), "{html}"); |
| 407 |
} |
| 408 |
|
| 409 |
#[test] |
| 410 |
fn a_buyer_cannot_smuggle_markup() { |
| 411 |
let mut hostile = sale(true); |
| 412 |
hostile.buyer = "<script>x()</script>".into(); |
| 413 |
assert!(!render(&[hostile]).contains("<script>x()")); |
| 414 |
} |
| 415 |
} |
| 416 |
|