save discount profile is now working perfectly well
This commit is contained in:
@@ -432,6 +432,62 @@ fn percent_off(regular_cents: i64, sale_cents: i64) -> i64 {
|
||||
off.round() as i64
|
||||
}
|
||||
|
||||
/// Preview the effective prices that the submitted (unsaved) checkbox set would
|
||||
/// produce, without persisting anything. Returns OOB `<span>`s that htmx swaps
|
||||
/// into the effective-price column so the admin sees the effect before Save.
|
||||
#[debug_handler]
|
||||
async fn profiles_preview(
|
||||
auth: auth::JWT,
|
||||
jar: CookieJar,
|
||||
ViewEngine(v): ViewEngine<TeraView>,
|
||||
Query(params): Query<HashMap<String, String>>,
|
||||
State(ctx): State<AppContext>,
|
||||
body: String,
|
||||
) -> Result<Response> {
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let audience = read_audience(¶ms);
|
||||
|
||||
let profile_ids: Vec<i32> = form_urlencoded::parse(body.as_bytes())
|
||||
.filter(|(k, _)| k == "profile_ids")
|
||||
.filter_map(|(_, value)| value.parse::<i32>().ok())
|
||||
.collect();
|
||||
|
||||
let all_categories = categories::Entity::find()
|
||||
.order_by_asc(categories::Column::Position)
|
||||
.order_by_asc(categories::Column::Name)
|
||||
.all(&ctx.db)
|
||||
.await?;
|
||||
let list = products::Entity::find()
|
||||
.order_by_desc(products::Column::CreatedAt)
|
||||
.all(&ctx.db)
|
||||
.await?;
|
||||
let effective =
|
||||
pricing::audience_price_many_preview(&ctx, &list, audience, profile_ids).await?;
|
||||
|
||||
let selected_category = params.get("category").map(String::as_str).unwrap_or("all");
|
||||
let filter = view::category_filter_ids(&all_categories, selected_category);
|
||||
|
||||
let mut rows = Vec::new();
|
||||
for (product, priced) in list.iter().zip(effective.iter()) {
|
||||
if !view::category_filter_keep(&filter, product.category_id) {
|
||||
continue;
|
||||
}
|
||||
rows.push(json!({
|
||||
"id": product.id,
|
||||
"currency": product.currency,
|
||||
"effective_price": format_price(priced.price_cents),
|
||||
"effective_reduced": priced.is_reduced(),
|
||||
"effective_percent_off": percent_off(product.price_cents, priced.price_cents),
|
||||
}));
|
||||
}
|
||||
|
||||
format::view(
|
||||
&v,
|
||||
"admin/catalog/_price_preview.html",
|
||||
json!({ "products": rows, "lang": current_lang(&jar) }),
|
||||
)
|
||||
}
|
||||
|
||||
/// Replace the profiles applied to this audience with the submitted checkbox set
|
||||
/// (`profile_ids`, a repeated field parsed directly from the body).
|
||||
#[debug_handler]
|
||||
@@ -631,6 +687,10 @@ pub fn routes() -> Routes {
|
||||
post(create).layer(image_limit.clone()),
|
||||
)
|
||||
.add("/admin/catalog/products/profiles", post(sync_profiles))
|
||||
.add(
|
||||
"/admin/catalog/products/profiles/preview",
|
||||
post(profiles_preview),
|
||||
)
|
||||
.add("/admin/catalog/products/{id}/edit", get(edit))
|
||||
.add(
|
||||
"/admin/catalog/products/{id}",
|
||||
|
||||
Reference in New Issue
Block a user