api for packeta required to enable it
This commit is contained in:
@@ -16,6 +16,7 @@ use crate::{
|
||||
shared::{
|
||||
guard,
|
||||
money::{format_price, parse_price_to_cents},
|
||||
shipping as shipping_rules,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -37,13 +38,17 @@ async fn index(
|
||||
State(ctx): State<AppContext>,
|
||||
) -> Result<Response> {
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
shipping_rules::disable_packeta_if_unconfigured(&ctx).await?;
|
||||
let methods = shipping_methods::Entity::find()
|
||||
.order_by_asc(shipping_methods::Column::Position)
|
||||
.all(&ctx.db)
|
||||
.await?;
|
||||
let packeta_ready = shipping_rules::packeta_ready(&ctx);
|
||||
let rows: Vec<serde_json::Value> = methods
|
||||
.iter()
|
||||
.map(|m| {
|
||||
let packeta_not_ready = m.carrier == "packeta" && !packeta_ready;
|
||||
let locked = packeta_not_ready && !m.enabled;
|
||||
json!({
|
||||
"id": m.id,
|
||||
"code": m.code,
|
||||
@@ -52,6 +57,9 @@ async fn index(
|
||||
"carrier": m.carrier,
|
||||
"requires_pickup_point": m.requires_pickup_point,
|
||||
"enabled": m.enabled,
|
||||
"packeta_not_ready": packeta_not_ready,
|
||||
"locked": locked,
|
||||
"lock_reason": if packeta_not_ready { Some("shipping-packeta-missing-settings") } else { None::<&str> },
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
@@ -74,9 +82,15 @@ async fn update(
|
||||
.one(&ctx.db)
|
||||
.await?
|
||||
.ok_or_else(|| Error::NotFound)?;
|
||||
let requested_enabled = is_checked(&form.enabled);
|
||||
if requested_enabled && method.carrier == "packeta" && !shipping_rules::packeta_ready(&ctx) {
|
||||
return Err(Error::BadRequest(
|
||||
"Packeta cannot be enabled until PACKETA_API_KEY, PACKETA_API_PASSWORD and PACKETA_SENDER_LABEL are configured.".to_string(),
|
||||
));
|
||||
}
|
||||
let mut active = method.into_active_model();
|
||||
active.price_cents = Set(parse_price_to_cents(&form.price)?);
|
||||
active.enabled = Set(is_checked(&form.enabled));
|
||||
active.enabled = Set(requested_enabled);
|
||||
active.update(&ctx.db).await?;
|
||||
format::redirect("/admin/shipping")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user