Skip to main content

web/pages/forms/
options.rs

1//! The picker options of the shared forms, served from `rpc::forms::options`.
2
3use axum::{Extension, Json, extract::Path};
4use forms::Source;
5use rpc::forms::FormOption;
6use serde::Serialize;
7use uuid::Uuid;
8
9use super::failure_error;
10use crate::{jwt_auth::JWTAuthMiddleware, response::WebError};
11
12#[derive(Serialize)]
13pub struct OptionView {
14    id: Uuid,
15    label: String,
16    alias: String,
17    path: String,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    commodity: Option<Uuid>,
20}
21
22impl From<FormOption> for OptionView {
23    fn from(option: FormOption) -> Self {
24        Self {
25            id: option.id,
26            label: option.label,
27            alias: option.alias,
28            path: option.path,
29            commodity: option.commodity,
30        }
31    }
32}
33
34pub async fn form_options(
35    Path(source): Path<String>,
36    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
37) -> Result<Json<Vec<OptionView>>, WebError> {
38    let source = Source::by_name(&source)
39        .ok_or_else(|| WebError::bad_request(format!("unknown option source '{source}'")))?;
40    let options = rpc::forms::options(jwt_auth.user.id, source)
41        .await
42        .map_err(|failure| failure_error("Failed to load form options", failure))?;
43    Ok(Json(options.into_iter().map(OptionView::from).collect()))
44}