personal discounts to businesses done

This commit is contained in:
Priec
2026-06-21 23:21:24 +02:00
parent ed566b5347
commit c713627a2c
35 changed files with 912 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
use crate::{controllers::i18n::current_lang, shared::{guard, money::format_price}, models::products};
use crate::{controllers::i18n::current_lang, shared::{guard, money::format_price, pricing}, models::products};
use axum::{
http::{HeaderMap, StatusCode},
response::Redirect,
@@ -189,10 +189,10 @@ pub(crate) async fn resolve_cart(
ctx: &AppContext,
jar: &CookieJar,
) -> Result<(Vec<serde_json::Value>, Vec<(i32, i32)>, i64)> {
let mut lines = Vec::new();
let mut valid = Vec::new();
let mut total: i64 = 0;
// Resolve the cart entries to in-stock products first, then price them all
// for the current viewer in one batch (the price depends on who's logged in).
let user = guard::current_user(ctx, jar).await;
let mut items: Vec<(products::Model, i32)> = Vec::new();
for (id, qty) in parse_cart(jar) {
let Some(product) = published_product(ctx, id).await? else {
continue;
@@ -201,15 +201,26 @@ pub(crate) async fn resolve_cart(
if qty == 0 {
continue;
}
let unit_price = product.effective_price_cents();
let line_total = unit_price * i64::from(qty);
items.push((product, qty));
}
let products_only: Vec<products::Model> = items.iter().map(|(p, _)| p.clone()).collect();
let priced = pricing::price_many(ctx, &products_only, user.as_ref()).await?;
let mut lines = Vec::new();
let mut valid = Vec::new();
let mut total: i64 = 0;
for ((product, qty), priced) in items.iter().zip(priced.iter()) {
let unit_price = priced.price_cents;
let line_total = unit_price * i64::from(*qty);
total += line_total;
valid.push((product.id, qty));
valid.push((product.id, *qty));
lines.push(json!({
"id": product.id,
"name": product.name,
"slug": product.slug,
"price": format_price(unit_price),
"regular_price": format_price(priced.regular_cents),
"on_sale": priced.is_reduced(),
"currency": product.currency,
"quantity": qty,
"stock": product.stock,