1
0
Fork 0
forked from 2sys/phonograph
phonograph/interim-server/src/routes/relations_single/form_handler.rs

166 lines
5.4 KiB
Rust
Raw Normal View History

2025-10-01 22:36:19 -07:00
use std::collections::HashMap;
use askama::Template;
use axum::{
debug_handler,
extract::{Path, State},
response::{Html, IntoResponse},
};
use interim_models::{
2025-10-22 00:43:53 -07:00
field::Field, field_form_prompt::FieldFormPrompt, form_transition::FormTransition,
language::Language, portal::Portal, workspace::Workspace,
2025-10-01 22:36:19 -07:00
};
2025-10-01 22:37:11 -07:00
use interim_pgtypes::pg_attribute::PgAttribute;
2025-10-01 22:36:19 -07:00
use serde::{Deserialize, Serialize};
use sqlx::postgres::types::Oid;
use strum::IntoEnumIterator as _;
use uuid::Uuid;
use crate::{
app::{App, AppDbConn},
2025-10-22 00:43:53 -07:00
errors::AppError,
2025-10-01 22:36:19 -07:00
field_info::FormFieldInfo,
2025-10-01 22:37:11 -07:00
navigator::{Navigator, NavigatorPage as _},
2025-10-01 22:36:19 -07:00
settings::Settings,
user::CurrentUser,
workspace_nav::{NavLocation, RelLocation, WorkspaceNav},
workspace_pooler::{RoleAssignment, WorkspacePooler},
workspace_utils::{RelationPortalSet, fetch_all_accessible_portals},
};
#[derive(Debug, Deserialize)]
pub(super) struct PathParams {
portal_id: Uuid,
rel_oid: u32,
workspace_id: Uuid,
}
/// HTTP GET handler for the top-level portal form builder page.
#[debug_handler(state = App)]
pub(super) async fn get(
State(settings): State<Settings>,
CurrentUser(user): CurrentUser,
AppDbConn(mut app_db): AppDbConn,
Path(PathParams {
portal_id,
rel_oid,
workspace_id,
}): Path<PathParams>,
navigator: Navigator,
State(mut pooler): State<WorkspacePooler>,
) -> Result<impl IntoResponse, AppError> {
2025-10-22 00:43:53 -07:00
// FIXME: Check workspace authorization.
2025-10-01 22:36:19 -07:00
// FIXME ensure workspace corresponds to rel/portal, and that user has
// permission to access/alter both as needed.
let portal = Portal::with_id(portal_id).fetch_one(&mut app_db).await?;
let workspace = Workspace::with_id(portal.workspace_id)
.fetch_one(&mut app_db)
.await?;
let mut workspace_client = pooler
.acquire_for(workspace.id, RoleAssignment::User(user.id))
.await?;
let attrs: HashMap<String, PgAttribute> = PgAttribute::all_for_rel(portal.class_oid)
.fetch_all(&mut workspace_client)
.await?
.into_iter()
.map(|attr| (attr.attname.clone(), attr))
.collect();
let fields: Vec<FormFieldInfo> = {
let fields: Vec<Field> = Field::belonging_to_portal(portal.id)
.fetch_all(&mut app_db)
.await?;
let mut field_info: Vec<FormFieldInfo> = Vec::with_capacity(fields.len());
for field in fields {
let attr = attrs.get(&field.name);
let prompts: HashMap<Language, String> = FieldFormPrompt::belonging_to_field(field.id)
.fetch_all(&mut app_db)
.await?
.into_iter()
.map(|value| (value.language, value.content))
.collect();
field_info.push(FormFieldInfo {
field,
column_present: attr.is_some(),
has_default: attr.map(|value| value.atthasdef).unwrap_or(false),
not_null: attr.and_then(|value| value.attnotnull).unwrap_or_default(),
prompts,
});
}
field_info
};
// FIXME: exclude portals user does not have access to, as well as
// unnecessary fields
let portal_sets =
fetch_all_accessible_portals(workspace_id, &mut app_db, &mut workspace_client).await?;
#[derive(Template)]
#[template(path = "relations_single/form_index.html")]
struct ResponseTemplate {
fields: Vec<FormFieldInfo>,
identifier_hints: Vec<String>,
languages: Vec<LanguageInfo>,
2025-10-01 22:37:11 -07:00
navigator: Navigator,
portal: Portal,
2025-10-01 22:36:19 -07:00
portals: Vec<PortalDisplay>,
settings: Settings,
transitions: Vec<FormTransition>,
workspace_nav: WorkspaceNav,
}
#[derive(Debug, Serialize)]
struct LanguageInfo {
code: String,
locale_str: String,
}
#[derive(Debug, Serialize)]
struct PortalDisplay {
id: Uuid,
display_name: String,
}
Ok(Html(
ResponseTemplate {
fields,
identifier_hints: attrs.keys().cloned().collect(),
languages: Language::iter()
.map(|value| LanguageInfo {
code: value.to_string(),
locale_str: value.as_locale_str().to_owned(),
})
.collect(),
2025-10-01 22:37:11 -07:00
portal,
2025-10-01 22:36:19 -07:00
portals: portal_sets
.iter()
.flat_map(|RelationPortalSet { rel, portals }| {
portals.iter().map(|portal| PortalDisplay {
id: portal.id,
display_name: format!(
"{rel_name}: {portal_name}",
rel_name = rel.relname,
portal_name = portal.name
),
})
})
.collect(),
transitions: FormTransition::with_source(portal_id)
.fetch_all(&mut app_db)
.await?,
workspace_nav: WorkspaceNav::builder()
2025-10-01 22:37:11 -07:00
.navigator(navigator.clone())
2025-10-01 22:36:19 -07:00
.workspace(workspace)
.populate_rels(&mut app_db, &mut workspace_client)
.await?
.current(NavLocation::Rel(
Oid(rel_oid),
Some(RelLocation::Portal(portal_id)),
))
.build()?,
2025-10-01 22:37:11 -07:00
navigator,
2025-10-01 22:36:19 -07:00
settings,
}
.render()?,
))
}