1
//! The picker options of the shared forms, served from `rpc::forms::options`.
2

            
3
use axum::{Extension, Json, extract::Path};
4
use forms::Source;
5
use rpc::forms::FormOption;
6
use serde::Serialize;
7
use uuid::Uuid;
8

            
9
use super::failure_error;
10
use crate::{jwt_auth::JWTAuthMiddleware, response::WebError};
11

            
12
#[derive(Serialize)]
13
pub 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

            
22
impl From<FormOption> for OptionView {
23
7
    fn from(option: FormOption) -> Self {
24
7
        Self {
25
7
            id: option.id,
26
7
            label: option.label,
27
7
            alias: option.alias,
28
7
            path: option.path,
29
7
            commodity: option.commodity,
30
7
        }
31
7
    }
32
}
33

            
34
5
pub async fn form_options(
35
5
    Path(source): Path<String>,
36
5
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
37
5
) -> Result<Json<Vec<OptionView>>, WebError> {
38
5
    let source = Source::by_name(&source)
39
1
        .ok_or_else(|| WebError::bad_request(format!("unknown option source '{source}'")))?;
40
4
    let options = rpc::forms::options(jwt_auth.user.id, source)
41
4
        .await
42
        .map_err(|failure| failure_error("Failed to load form options", failure))?;
43
4
    Ok(Json(options.into_iter().map(OptionView::from).collect()))
44
5
}