add: single_use products
This commit is contained in:
parent
7fbc732290
commit
e5e6d5cddb
12 changed files with 149 additions and 7 deletions
|
@ -337,9 +337,11 @@ version = "1.0.0"
|
|||
"economy:label.create_new" = "Create new product"
|
||||
"economy:label.price" = "Price"
|
||||
"economy:label.on_sale" = "On sale"
|
||||
"economy:label.single_use" = "Only allow users to purchase once"
|
||||
"economy:label.stock" = "Stock"
|
||||
"economy:label.unlimited" = "Unlimited"
|
||||
"economy:label.fulfillment_style" = "Fulfillment style"
|
||||
"economy:label.use_automail" = "Use automail"
|
||||
"economy:label.automail_message" = "Automail message"
|
||||
"economy:action.buy" = "Buy"
|
||||
"economy:label.already_purchased" = "Already purchased"
|
||||
|
|
|
@ -77,6 +77,18 @@
|
|||
("oninput" "event.preventDefault(); update_on_sale_from_form(event.target.checked)"))
|
||||
(span
|
||||
(str (text "economy:label.on_sale"))))
|
||||
(label
|
||||
("for" "single_use")
|
||||
("class" "flex items_center gap_2")
|
||||
(input
|
||||
("type" "checkbox")
|
||||
("id" "single_use")
|
||||
("name" "single_use")
|
||||
("class" "w_content")
|
||||
("checked" "{{ product.single_use }}")
|
||||
("oninput" "event.preventDefault(); update_single_use_from_form(event.target.checked)"))
|
||||
(span
|
||||
(str (text "economy:label.single_use"))))
|
||||
(div
|
||||
("class" "flex flex_col gap_1")
|
||||
(label
|
||||
|
@ -240,6 +252,27 @@
|
|||
});
|
||||
}
|
||||
|
||||
async function update_single_use_from_form(single_use) {
|
||||
await trigger(\"atto::debounce\", [\"products::update\"]);
|
||||
|
||||
fetch(\"/api/v1/products/{{ product.id }}/single_use\", {
|
||||
method: \"POST\",
|
||||
headers: {
|
||||
\"Content-Type\": \"application/json\",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
single_use,
|
||||
}),
|
||||
})
|
||||
.then((res) => res.json())
|
||||
.then((res) => {
|
||||
trigger(\"atto::toast\", [
|
||||
res.ok ? \"success\" : \"error\",
|
||||
res.message,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
async function update_price_from_form(e) {
|
||||
e.preventDefault();
|
||||
await trigger(\"atto::debounce\", [\"products::update\"]);
|
||||
|
|
|
@ -28,12 +28,19 @@
|
|||
(icon (text "badge-cent"))
|
||||
(text "{{ product.price }}"))
|
||||
(text "{% if user.id != product.owner -%}")
|
||||
(text "{% if not already_purchased -%}")
|
||||
(button
|
||||
("onclick" "purchase()")
|
||||
("disabled" "{{ product.stock == 0 }}")
|
||||
(icon (text "piggy-bank"))
|
||||
(str (text "economy:action.buy")))
|
||||
(text "{% else %}")
|
||||
(span
|
||||
("class" "green flex items_center gap_2")
|
||||
(icon (text "circle-check"))
|
||||
(str (text "economy:label.already_purchased")))
|
||||
(text "{%- endif %}")
|
||||
(text "{% else %}")
|
||||
(a
|
||||
("class" "button")
|
||||
("href" "/product/{{ product.id }}/edit")
|
||||
|
|
|
@ -304,7 +304,7 @@
|
|||
|
||||
globalThis.request_transfer = async () => {
|
||||
await trigger(\"atto::debounce\", [\"economy::transfer\"]);
|
||||
const amount = Number.parseInt((await trigger(\"atto::prompt\", [\"Request amount:\"])) || \"\");
|
||||
const amount = Number.parseInt((await trigger(\"atto::prompt\", [\"Request amount:\"])) || \"0\");
|
||||
|
||||
if (amount === 0) {
|
||||
return;
|
||||
|
|
|
@ -732,6 +732,10 @@ pub fn routes() -> Router {
|
|||
"/products/{id}/on_sale",
|
||||
post(products::update_on_sale_request),
|
||||
)
|
||||
.route(
|
||||
"/products/{id}/single_use",
|
||||
post(products::update_single_use_request),
|
||||
)
|
||||
.route("/products/{id}/price", post(products::update_price_request))
|
||||
.route(
|
||||
"/products/{id}/method",
|
||||
|
@ -1275,6 +1279,11 @@ pub struct UpdateProductOnSale {
|
|||
pub on_sale: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateProductSingleUse {
|
||||
pub single_use: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateProductPrice {
|
||||
pub price: i32,
|
||||
|
|
|
@ -3,7 +3,7 @@ use axum::{extract::Path, response::IntoResponse, Extension, Json};
|
|||
use tetratto_core::model::{economy::Product, oauth, ApiReturn, Error};
|
||||
use super::{
|
||||
CreateProduct, UpdateProductDescription, UpdateProductMethod, UpdateProductOnSale,
|
||||
UpdateProductPrice, UpdateProductStock, UpdateProductTitle,
|
||||
UpdateProductPrice, UpdateProductSingleUse, UpdateProductStock, UpdateProductTitle,
|
||||
};
|
||||
|
||||
pub async fn create_request(
|
||||
|
@ -137,6 +137,31 @@ pub async fn update_on_sale_request(
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn update_single_use_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdateProductSingleUse>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageProducts) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
match data
|
||||
.update_product_single_use(id, &user, if req.single_use { 1 } else { 0 })
|
||||
.await
|
||||
{
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Product updated".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_price_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
|
|
|
@ -4,7 +4,7 @@ use axum::{
|
|||
Extension,
|
||||
};
|
||||
use crate::cookie::CookieJar;
|
||||
use tetratto_core::model::Error;
|
||||
use tetratto_core::model::{economy::CoinTransferMethod, Error};
|
||||
use crate::{assets::initial_context, get_lang, get_user_from_token, State};
|
||||
use super::{render_error, PaginatedQuery};
|
||||
|
||||
|
@ -138,11 +138,21 @@ pub async fn product_request(
|
|||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
};
|
||||
|
||||
let already_purchased = if product.single_use {
|
||||
data.0
|
||||
.get_transfer_by_sender_method(user.id, CoinTransferMethod::Purchase(product.id))
|
||||
.await
|
||||
.is_ok()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
|
||||
|
||||
context.insert("product", &product);
|
||||
context.insert("owner", &owner);
|
||||
context.insert("already_purchased", &already_purchased);
|
||||
|
||||
// return
|
||||
Ok(Html(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue