headless_lms_models/
chatbot_configurations_models.rs

1use crate::prelude::*;
2
3#[derive(Clone, PartialEq, Deserialize, Serialize)]
4#[cfg_attr(feature = "ts_rs", derive(TS))]
5pub struct ChatbotConfigurationModel {
6    pub id: Uuid,
7    pub created_at: DateTime<Utc>,
8    pub updated_at: DateTime<Utc>,
9    pub deleted_at: Option<DateTime<Utc>>,
10    pub model: String,
11    pub thinking: bool,
12    pub default_model: bool,
13    pub deployment_name: String,
14}
15
16#[derive(Clone, PartialEq, Deserialize, Serialize)]
17pub struct NewChatbotConfigurationModel {
18    pub id: Uuid,
19    pub model: String,
20    pub thinking: bool,
21    pub default_model: bool,
22    pub deployment_name: String,
23}
24
25pub async fn get_by_id(
26    conn: &mut PgConnection,
27    id: Uuid,
28) -> ModelResult<ChatbotConfigurationModel> {
29    let res = sqlx::query_as!(
30        ChatbotConfigurationModel,
31        r#"
32SELECT * FROM chatbot_configurations_models
33WHERE id = $1
34AND deleted_at IS NULL
35        "#,
36        id
37    )
38    .fetch_one(conn)
39    .await?;
40    Ok(res)
41}
42
43pub async fn get_all(conn: &mut PgConnection) -> ModelResult<Vec<ChatbotConfigurationModel>> {
44    let res = sqlx::query_as!(
45        ChatbotConfigurationModel,
46        r#"
47SELECT * FROM chatbot_configurations_models
48WHERE deleted_at IS NULL
49        "#,
50    )
51    .fetch_all(conn)
52    .await?;
53    Ok(res)
54}
55
56pub async fn get_default(conn: &mut PgConnection) -> ModelResult<ChatbotConfigurationModel> {
57    let res = sqlx::query_as!(
58        ChatbotConfigurationModel,
59        r#"
60SELECT * FROM chatbot_configurations_models
61WHERE default_model = true
62AND deleted_at IS NULL
63        "#,
64    )
65    .fetch_one(conn)
66    .await?;
67    Ok(res)
68}
69
70pub async fn get_by_chatbot_configuration_id(
71    conn: &mut PgConnection,
72    chatbotconf_id: Uuid,
73) -> ModelResult<ChatbotConfigurationModel> {
74    let res = sqlx::query_as!(
75        ChatbotConfigurationModel,
76        r#"
77SELECT * FROM chatbot_configurations_models
78WHERE id = (
79    SELECT model FROM chatbot_configurations WHERE id = $1
80)
81AND deleted_at IS NULL
82        "#,
83        chatbotconf_id,
84    )
85    .fetch_one(conn)
86    .await?;
87    Ok(res)
88}
89
90pub async fn insert(
91    conn: &mut PgConnection,
92    input: NewChatbotConfigurationModel,
93) -> ModelResult<ChatbotConfigurationModel> {
94    let res = sqlx::query_as!(
95        ChatbotConfigurationModel,
96        r#"
97INSERT INTO chatbot_configurations_models (id, model, thinking, deployment_name, default_model) VALUES ($1, $2, $3, $4, $5) RETURNING *
98        "#,
99        input.id,
100        input.model,
101        input.thinking,
102        input.deployment_name,
103        input.default_model,
104    )
105    .fetch_one(conn)
106    .await?;
107    Ok(res)
108}