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