Skip to main content

headless_lms_models/
user_ai_usage_notice_acknowledgements.rs

1use crate::prelude::*;
2
3/// Records that the given user has acknowledged the AI-usage / academic-integrity notice for the
4/// course language group the given course belongs to. Acknowledgement is stored per course language
5/// group, so accepting the notice on one language version covers all language versions. Idempotent:
6/// a second call for the same (user, course language group) does nothing.
7pub async fn acknowledge(
8    conn: &mut PgConnection,
9    user_id: Uuid,
10    course_id: Uuid,
11) -> ModelResult<()> {
12    sqlx::query!(
13        "
14INSERT INTO user_ai_usage_notice_acknowledgements (user_id, course_language_group_id)
15SELECT $1, course_language_group_id
16FROM courses
17WHERE id = $2
18ON CONFLICT (user_id, course_language_group_id) WHERE deleted_at IS NULL DO NOTHING
19        ",
20        user_id,
21        course_id,
22    )
23    .execute(conn)
24    .await?;
25    Ok(())
26}
27
28/// Whether the given user has acknowledged the AI-usage notice for the course language group the
29/// given course belongs to (any language version of the course counts).
30pub async fn has_acknowledged(
31    conn: &mut PgConnection,
32    user_id: Uuid,
33    course_id: Uuid,
34) -> ModelResult<bool> {
35    let acknowledged = sqlx::query_scalar!(
36        r#"
37SELECT EXISTS(
38    SELECT 1
39    FROM user_ai_usage_notice_acknowledgements
40    WHERE user_id = $1
41      AND course_language_group_id = (
42        SELECT course_language_group_id
43        FROM courses
44        WHERE id = $2
45      )
46      AND deleted_at IS NULL
47) AS "exists!"
48        "#,
49        user_id,
50        course_id,
51    )
52    .fetch_one(conn)
53    .await?;
54    Ok(acknowledged)
55}