headless_lms_models/
user_research_consents.rs

1use crate::prelude::*;
2
3#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
4#[cfg_attr(feature = "ts_rs", derive(TS))]
5pub struct UserResearchConsent {
6    pub id: Uuid,
7    pub user_id: Uuid,
8    pub research_consent: bool,
9    pub created_at: DateTime<Utc>,
10    pub updated_at: DateTime<Utc>,
11    pub deleted_at: Option<DateTime<Utc>>,
12}
13
14pub async fn upsert(
15    conn: &mut PgConnection,
16    pkey_policy: PKeyPolicy<Uuid>,
17    user_id: Uuid,
18    research_consent: bool,
19) -> ModelResult<UserResearchConsent> {
20    let res = sqlx::query_as!(
21        UserResearchConsent,
22        "
23INSERT INTO user_research_consents (
24    id,
25    user_id,
26    research_consent
27)
28VALUES ($1, $2, $3) ON CONFLICT (user_id, deleted_at)
29DO UPDATE SET research_consent = $3
30RETURNING *;
31    ",
32        pkey_policy.into_uuid(),
33        user_id,
34        research_consent,
35    )
36    .fetch_one(conn)
37    .await?;
38    Ok(res)
39}
40
41pub async fn get_research_consent_by_user_id(
42    conn: &mut PgConnection,
43    user_id: Uuid,
44) -> ModelResult<UserResearchConsent> {
45    let res = sqlx::query_as!(
46        UserResearchConsent,
47        "
48SELECT *
49FROM user_research_consents
50WHERE user_id = $1
51AND deleted_at IS NULL
52    ",
53        user_id,
54    )
55    .fetch_one(conn)
56    .await?;
57    Ok(res)
58}