Skip to main content

headless_lms_server/controllers/main_frontend/
chatbot_embed.rs

1//! Controllers for requests starting with `/api/v0/main-frontend/chatbot-embed/`.
2use crate::prelude::*;
3use headless_lms_authorization::authorize_access_to_chatbot;
4use utoipa::OpenApi;
5
6#[derive(OpenApi)]
7#[openapi(paths(get_chatbot))]
8pub(crate) struct MainFrontendChatbotEmbedApiDoc;
9
10#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
11
12pub struct CourseInfo {
13    course_id: Option<Uuid>,
14}
15
16/// GET `/api/v0/main-frontend/chatbot-embed/{chatbot_configuration_id}`
17#[utoipa::path(
18    get,
19    path = "/{chatbot_id}",
20    operation_id = "getChatbotEmbedConf",
21    tag = "chatbot-embed",
22    params(
23        ("chatbot_id" = Uuid, Path, description = "Chatbot id")
24    ),
25    responses(
26        (status = 204, description = "Chatbot is embeddable")
27    )
28)]
29#[instrument(skip(pool))]
30async fn get_chatbot(
31    chatbot_id: web::Path<Uuid>,
32    pool: web::Data<PgPool>,
33) -> ControllerResult<HttpResponse> {
34    let mut conn = pool.acquire().await?;
35    let chatbot = models::chatbot_configurations::get_by_id(&mut conn, *chatbot_id).await?;
36    let token = authorize_access_to_chatbot(&mut conn, None, &chatbot).await?;
37
38    if !chatbot.embeddable {
39        return Err(controller_err!(
40            Forbidden,
41            "Chatbot is not embeddable".to_string()
42        ));
43    }
44
45    token.authorized_ok(HttpResponse::NoContent().finish())
46}
47
48pub fn _add_routes(cfg: &mut web::ServiceConfig) {
49    cfg.route("/{chatbot_id}", web::get().to(get_chatbot));
50}