api for packeta required to enable it
This commit is contained in:
@@ -484,6 +484,7 @@ bank-variable-symbol = Variable symbol
|
||||
bank-amount = Amount
|
||||
admin-shipping = Shipping
|
||||
admin-shipping-desc = set the price and availability of each delivery option.
|
||||
shipping-packeta-missing-settings = Packeta can be enabled after PACKETA_API_KEY, PACKETA_API_PASSWORD and PACKETA_SENDER_LABEL are configured.
|
||||
admin-payments = Payments
|
||||
admin-payments-desc = enable or disable payment methods and edit bank-transfer details.
|
||||
payment-methods = Payment methods
|
||||
|
||||
@@ -484,6 +484,7 @@ bank-variable-symbol = Variabilný symbol
|
||||
bank-amount = Suma
|
||||
admin-shipping = Doprava
|
||||
admin-shipping-desc = nastaviť cenu a dostupnosť jednotlivých možností dopravy.
|
||||
shipping-packeta-missing-settings = Packeta sa dá zapnúť až po nastavení PACKETA_API_KEY, PACKETA_API_PASSWORD a PACKETA_SENDER_LABEL.
|
||||
admin-payments = Platby
|
||||
admin-payments-desc = zapnite alebo vypnite spôsoby platby a upravte údaje pre prevod na účet.
|
||||
payment-methods = Spôsoby platby
|
||||
|
||||
@@ -18,12 +18,21 @@
|
||||
<div class="min-w-40">
|
||||
<p class="font-semibold text-on-surface-strong dark:text-on-surface-dark-strong">{{ method.name }}</p>
|
||||
<p class="text-xs text-on-surface/60 dark:text-on-surface-dark/60">{{ method.carrier | upper }}{% if method.requires_pickup_point %} · {{ t(key="checkout-pickup-point", lang=lang | default(value='sk')) }}{% endif %}</p>
|
||||
{% if method.packeta_not_ready %}
|
||||
<p class="mt-1 text-xs text-warning">{{ t(key=method.lock_reason, lang=lang | default(value='sk')) }}</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="space-y-1.5">
|
||||
<label for="price-{{ method.id }}" class="text-sm font-medium text-on-surface-strong dark:text-on-surface-dark-strong">{{ t(key="price", lang=lang | default(value='sk')) }}</label>
|
||||
{{ ui::input(name="price", id="price-" ~ method.id, value=method.price, width="w-28", attrs='inputmode="decimal"') }}
|
||||
</div>
|
||||
<div class="pb-2">{{ ui::checkbox(name="enabled", label=t(key="shipping-enabled", lang=lang | default(value='sk')), checked=method.enabled) }}</div>
|
||||
<div class="pb-2">
|
||||
{% if method.locked %}
|
||||
{{ ui::checkbox(name="enabled", label=t(key="shipping-enabled", lang=lang | default(value='sk')), checked=method.enabled, attrs='disabled') }}
|
||||
{% else %}
|
||||
{{ ui::checkbox(name="enabled", label=t(key="shipping-enabled", lang=lang | default(value='sk')), checked=method.enabled) }}
|
||||
{% endif %}
|
||||
</div>
|
||||
{{ ui::button(label=t(key="save", lang=lang | default(value='sk')), type="submit", extra="ml-auto") }}
|
||||
</form>
|
||||
{% endfor %}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ use crate::{
|
||||
users::{self, normalize_account_type},
|
||||
},
|
||||
controllers::i18n::current_lang,
|
||||
shared::{currency::Currency, guard, money::format_price, settings},
|
||||
shared::{currency::Currency, guard, money::format_price, settings, shipping as shipping_rules},
|
||||
views::checkout as view,
|
||||
};
|
||||
|
||||
@@ -67,11 +67,16 @@ fn cleared_cart_cookie() -> Cookie<'static> {
|
||||
}
|
||||
|
||||
async fn enabled_shipping_methods(ctx: &AppContext) -> Result<Vec<shipping_methods::Model>> {
|
||||
shipping_rules::disable_packeta_if_unconfigured(ctx).await?;
|
||||
let packeta_ready = shipping_rules::packeta_ready(ctx);
|
||||
Ok(shipping_methods::Entity::find()
|
||||
.filter(shipping_methods::Column::Enabled.eq(true))
|
||||
.order_by_asc(shipping_methods::Column::Position)
|
||||
.all(&ctx.db)
|
||||
.await?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter(|method| method.carrier != "packeta" || packeta_ready)
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn enabled_payment_methods(ctx: &AppContext) -> Result<Vec<payment_methods::Model>> {
|
||||
@@ -264,6 +269,9 @@ async fn place_order(
|
||||
.one(&ctx.db)
|
||||
.await?
|
||||
.ok_or_else(|| Error::BadRequest("invalid shipping method".to_string()))?;
|
||||
if method.carrier == "packeta" && !shipping_rules::packeta_ready(&ctx) {
|
||||
return Err(Error::BadRequest("invalid shipping method".to_string()));
|
||||
}
|
||||
|
||||
let (pickup_point_id, pickup_point_name) = if method.requires_pickup_point {
|
||||
let id = form
|
||||
|
||||
@@ -9,7 +9,7 @@ use async_trait::async_trait;
|
||||
use loco_rs::prelude::*;
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, Set};
|
||||
|
||||
use crate::models::shipping_methods;
|
||||
use crate::{models::shipping_methods, shared::shipping as shipping_rules};
|
||||
|
||||
/// `(code, name, carrier, requires_pickup_point, default_price_cents, position)`
|
||||
const BUILTINS: [(&str, &str, &str, bool, i64, i32); 2] = [
|
||||
@@ -49,6 +49,6 @@ impl Initializer for ShippingSeeder {
|
||||
.await?;
|
||||
tracing::info!(carrier = code, "seeded built-in delivery option");
|
||||
}
|
||||
Ok(())
|
||||
shipping_rules::disable_packeta_if_unconfigured(ctx).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,4 +7,5 @@ pub mod money;
|
||||
pub mod pricing;
|
||||
pub mod rbac;
|
||||
pub mod settings;
|
||||
pub mod shipping;
|
||||
pub mod slug;
|
||||
|
||||
31
src/shared/shipping.rs
Normal file
31
src/shared/shipping.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use loco_rs::prelude::*;
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, Set};
|
||||
|
||||
use crate::{
|
||||
models::shipping_methods,
|
||||
shared::settings,
|
||||
};
|
||||
|
||||
pub fn packeta_ready(ctx: &AppContext) -> bool {
|
||||
["packeta_api_key", "packeta_api_password", "packeta_sender_label"]
|
||||
.iter()
|
||||
.all(|key| settings::get(ctx, key).is_some_and(|value| !value.trim().is_empty()))
|
||||
}
|
||||
|
||||
pub async fn disable_packeta_if_unconfigured(ctx: &AppContext) -> Result<()> {
|
||||
if packeta_ready(ctx) {
|
||||
return Ok(());
|
||||
}
|
||||
let Some(method) = shipping_methods::Entity::find()
|
||||
.filter(shipping_methods::Column::Carrier.eq("packeta"))
|
||||
.filter(shipping_methods::Column::Enabled.eq(true))
|
||||
.one(&ctx.db)
|
||||
.await?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let mut active = method.into_active_model();
|
||||
active.enabled = Set(false);
|
||||
active.update(&ctx.db).await?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user