Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

4.9 KB
Raw Blame
//! A form as its page reads it: its facts and layout, each question joined
//! with its column's title, kind and options. Never any rows.

use models_databases::views::FilterGroup;
use models_databases::{ColumnId, ColumnKind, OptionId};
use serde::{Deserialize, Serialize};

use crate::form::{Form, FormAccess};
use crate::ids::{FormQuestionId, FormSectionId};
use crate::layout::BookingTarget;
use crate::widget::Widget;

/// A form with its layout, as the caller may see it.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, utoipa::ToSchema, specta::Type)]
#[serde(rename_all = "camelCase")]
pub struct FormDetail {
    /// The form's facts.
    pub form: Form,
    /// The caller's level on it.
    pub access: FormAccess,
    /// Whether its database is in the trash, so it has no table to show or
    /// write: its sections keep no questions and it takes no responses.
    pub table_gone: bool,
    /// Its sections, in order.
    pub sections: Vec<FormSectionDetail>,
}

/// One section of a form as it reads.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, utoipa::ToSchema, specta::Type)]
#[serde(
    tag = "kind",
    rename_all = "camelCase",
    rename_all_fields = "camelCase"
)]
pub enum FormSectionDetail {
    /// Questions asked together.
    Questions {
        /// The section.
        #[schema(value_type = Uuid)]
        id: FormSectionId,
        /// Its title.
        title: String,
        /// Its description.
        description: String,
        /// Its questions, in order.
        questions: Vec<FormQuestionDetail>,
        /// When to show it, from earlier shown sections' answers; absent,
        /// it is always shown. Sent to every caller so a respondent's page
        /// can skip it.
        // utoipa does not apply `rename_all_fields`, so the wire name is explicit.
        #[serde(rename = "showWhen", default, skip_serializing_if = "Option::is_none")]
        #[schema(nullable = false)]
        #[specta(optional)]
        show_when: Option<FilterGroup>,
    },
    /// A check of earlier answers. The rules are sent to every caller so a
    /// respondent's page can stop them before submitting; respondents are
    /// never shown them.
    Gate {
        /// The section.
        #[schema(value_type = Uuid)]
        id: FormSectionId,
        /// Its title.
        title: String,
        /// Its description.
        description: String,
        /// The rules.
        rules: FilterGroup,
        /// What a stopped respondent reads.
        message: String,
    },
    /// A booking step. Its destination is withheld from respondents until
    /// their response has passed every gate and been accepted.
    Booking {
        /// The section.
        #[schema(value_type = Uuid)]
        id: FormSectionId,
        /// Its title.
        title: String,
        /// Its description.
        description: String,
        /// Editors can configure the destination; respondent layouts omit it.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        #[schema(nullable = false)]
        #[specta(optional)]
        target: Option<BookingTarget>,
    },
}

impl FormSectionDetail {
    /// The section's id.
    pub fn id(&self) -> FormSectionId {
        match self {
            FormSectionDetail::Questions { id, .. }
            | FormSectionDetail::Gate { id, .. }
            | FormSectionDetail::Booking { id, .. } => *id,
        }
    }
}

/// A question with its column's facts.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, utoipa::ToSchema, specta::Type)]
#[serde(rename_all = "camelCase")]
pub struct FormQuestionDetail {
    /// The question.
    #[schema(value_type = Uuid)]
    pub id: FormQuestionId,
    /// The column it writes.
    #[schema(value_type = Uuid)]
    pub column: ColumnId,
    /// The column's name.
    pub title: String,
    /// The column's type.
    pub kind: ColumnKind,
    /// The column's options, in order, for a select or tag column.
    pub options: Vec<QuestionOption>,
    /// What respondents read under the title.
    pub help_text: String,
    /// Whether a response must answer it.
    pub required: bool,
    /// How it is asked: the question's widget or the kind's default; `null`
    /// for a kind asked one way only.
    #[schema(required = true)]
    pub widget: Option<Widget>,
}

/// One option of a question's column.
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema, specta::Type)]
#[serde(rename_all = "camelCase")]
pub struct QuestionOption {
    /// The option.
    #[schema(value_type = Uuid)]
    pub id: OptionId,
    /// Its label.
    pub label: String,
    /// Its colour, a hex string, if it has one.
    #[schema(required = true)]
    pub color: Option<String>,
}