// rocie - An enterprise grocery management system - Web app // // Copyright (C) 2026 Benedikt Peetz // SPDX-License-Identifier: GPL-3.0-or-later // // This file is part of Rocie. // // You should have received a copy of the License along with this program. // If not, see . use leptos::error::Error; use rocie_client::{ apis::{ api_get_auth_inventory_api::amount_by_id, api_get_auth_product_api::{ product_by_id, product_by_name, product_suggestion_by_name, products_by_product_parent_id_direct, products_by_product_parent_id_indirect, products_in_storage, products_registered, products_without_product_parent, }, api_get_auth_product_parent_api::{ product_parents, product_parents_toplevel, product_parents_under, }, api_get_auth_recipe_api::{ recipe_by_id, recipe_by_name, recipes, recipes_by_recipe_parent_id_direct, recipes_by_recipe_parent_id_indirect, recipes_without_recipe_parent, }, api_get_auth_recipe_parent_api::{ recipe_parents, recipe_parents_toplevel, recipe_parents_under, }, api_get_auth_unit_api::{unit_by_id, units, units_by_property_id}, api_get_auth_unit_property_api::{unit_properties, unit_property_by_id}, api_get_no_auth_state_api::{can_be_provisioned, is_logged_in}, api_set_auth_barcode_api::buy_barcode, api_set_auth_product_api::{associate_barcode, register_product}, api_set_auth_product_parent_api::register_product_parent, api_set_auth_recipe_api::add_recipe, api_set_auth_recipe_parent_api::register_recipe_parent, api_set_no_auth_user_api::{login, provision}, configuration::Configuration, }, models::{ Barcode, BarcodeId, LoginInfo, Product, ProductAmount, ProductId, ProductParent, ProductParentId, ProductParentStub, ProductStub, ProvisionInfo, Recipe, RecipeId, RecipeParent, RecipeParentId, RecipeParentStub, RecipeStub, Unit, UnitId, UnitProperty, UnitPropertyId, UserId, UserStub, }, }; macro_rules! get_config { () => {{ use crate::ConfigStateStoreFields; use leptos::prelude::Read; let config = leptos::prelude::expect_context::>(); config.config().read() }}; } pub(crate) use get_config; macro_rules! mk_wrapper { ( $orig:ident($($arg_name:ident : $arg_type:ty),*) -> $output_type:ty as $new_name:ident ) => { pub(crate) async fn $new_name( $($arg_name : $arg_type),* ) -> Result<$output_type, Error> { let config = get_config!(); $orig(&config, $($arg_name),*) .await .map_err(Into::::into) } }; ( @treat_404_as_None $orig:ident($($arg_name:ident : $arg_type:ty),*) -> Option<$output_type:ty> as $new_name:ident ) => { pub(crate) async fn $new_name( $($arg_name : $arg_type),* ) -> Result, Error> { let config = get_config!(); match $orig(&config, $($arg_name),*).await { Ok(ok) => Ok::<_, leptos::error::Error>(Some(ok)), Err(err) => match err { rocie_client::apis::Error::ResponseError(ref response_content) => { match response_content.status.as_u16() { 404 => Ok(None), _ => Err(err.into()), } } err => Err(err.into()), }, } } }; ( @external_config $orig:ident(&config, $($arg_name:ident : $arg_type:ty),*) -> $output_type:ty as $new_name:ident ) => { pub(crate) async fn $new_name( config: &Configuration, $($arg_name : $arg_type),* ) -> Result<$output_type, Error> { $orig(config, $($arg_name),*) .await .map_err(Into::::into) } } } mk_wrapper!( is_logged_in() -> bool as is_logged_in_wrapped ); mk_wrapper!( can_be_provisioned() -> bool as can_be_provisioned_wrapped ); mk_wrapper!( @external_config login(&config, login_info: LoginInfo) -> () as login_external_wrapped ); mk_wrapper!( @external_config provision(&config, provsion_info: ProvisionInfo) -> UserId as provision_external_wrapped ); mk_wrapper!( product_by_id(product_id: ProductId) -> Product as product_by_id_wrapped ); mk_wrapper!( product_by_name(name: &str) -> Product as product_by_name_wrapped ); mk_wrapper!( @treat_404_as_None product_by_name(name: &str) -> Option as product_by_name_404_wrapped ); mk_wrapper!( @external_config product_by_name(&config, name: &str) -> Product as product_by_name_external_wrapped ); mk_wrapper!( product_suggestion_by_name(part_name: &str) -> Vec as product_suggestion_by_name_wrapped ); mk_wrapper!( units() -> Vec as units_wrapped ); mk_wrapper!( units_by_property_id(id: UnitPropertyId) -> Vec as units_by_property_id_wrapped ); mk_wrapper!( unit_by_id(unit_id: UnitId) -> Unit as unit_by_id_wrapped ); mk_wrapper!( unit_property_by_id(unit_id: UnitPropertyId) -> UnitProperty as unit_property_by_id_wrapped ); mk_wrapper!( unit_properties() -> Vec as unit_properties_wrapped ); mk_wrapper!( @treat_404_as_None amount_by_id(product_id: ProductId) -> Option as amount_by_id_404_wrapped ); mk_wrapper!( products_registered() -> Vec as products_registered_wrapped ); mk_wrapper!( products_in_storage() -> Vec as products_in_storage_wrapped ); mk_wrapper!( products_without_product_parent() -> Vec as products_without_product_parent_wrapped ); mk_wrapper!( products_by_product_parent_id_indirect(product_parent_id: ProductParentId) -> Vec as products_by_product_parent_id_indirect_wrapped ); mk_wrapper!( products_by_product_parent_id_direct(product_parent_id: ProductParentId) -> Vec as products_by_product_parent_id_direct_wrapped ); mk_wrapper!( @external_config register_product_parent(&config, product_parent_stub: ProductParentStub) -> ProductParentId as register_product_parent_external_wrapped ); mk_wrapper!( product_parents_toplevel() -> Vec as product_parents_toplevel_wrapped ); mk_wrapper!( @treat_404_as_None product_parents_under(id: ProductParentId) -> Option> as product_parents_under_404_wrapped ); mk_wrapper!( product_parents() -> Vec as product_parents_wrapped ); mk_wrapper!( recipes() -> Vec as recipes_wrapped ); mk_wrapper!( recipes_without_recipe_parent() -> Vec as recipes_without_recipe_parent_wrapped ); mk_wrapper!( @external_config add_recipe(&config, stub: RecipeStub) -> RecipeId as add_recipe_external_wrapped ); mk_wrapper!( recipes_by_recipe_parent_id_indirect(recipe_parent_id: RecipeParentId) -> Vec as recipes_by_recipe_parent_id_indirect_wrapped ); mk_wrapper!( recipes_by_recipe_parent_id_direct(recipe_parent_id: RecipeParentId) -> Vec as recipes_by_recipe_parent_id_direct_wrapped ); mk_wrapper!( recipe_by_name(name: &str) -> Recipe as recipe_by_name_wrapped ); mk_wrapper!( recipe_by_id(id: RecipeId) -> Recipe as recipe_by_id_wrapped ); mk_wrapper!( @external_config register_recipe_parent(&config, recipe_parent_stub: RecipeParentStub) -> RecipeParentId as register_recipe_parent_external_wrapped ); mk_wrapper!( recipe_parents_toplevel() -> Vec as recipe_parents_toplevel_wrapped ); mk_wrapper!( @treat_404_as_None recipe_parents_under(id: RecipeParentId) -> Option> as recipe_parents_under_404_wrapped ); mk_wrapper!( recipe_parents() -> Vec as recipe_parents_wrapped ); mk_wrapper!( @external_config buy_barcode(&config, barcode_number: BarcodeId, times: u32) -> () as buy_barcode_external_wrapped ); mk_wrapper!( @external_config register_product(&config, product_stub: ProductStub) -> ProductId as register_product_external_wrapped ); mk_wrapper!( @external_config associate_barcode(&config, id: ProductId, barcode: Barcode) -> () as associate_barcode_external_wrapped );