1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use futures::Stream;

use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct ResearchForm {
    pub id: Uuid,
    pub course_id: Uuid,
    pub content: serde_json::Value,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deleted_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct NewResearchForm {
    pub course_id: Uuid,
    pub content: serde_json::Value,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct ResearchFormQuestion {
    pub id: Uuid,
    pub course_id: Uuid,
    pub research_consent_form_id: Uuid,
    pub question: String,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deleted_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct NewResearchFormQuestion {
    pub question_id: Uuid,
    pub course_id: Uuid,
    pub research_consent_form_id: Uuid,
    pub question: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct NewResearchFormQuestionAnswer {
    pub user_id: Uuid,
    pub research_form_question_id: Uuid,
    pub research_consent: bool,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct ResearchFormQuestionAnswer {
    pub id: Uuid,
    pub user_id: Uuid,
    pub course_id: Uuid,
    pub research_form_question_id: Uuid,
    pub research_consent: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deleted_at: Option<DateTime<Utc>>,
}

impl NewResearchForm {
    /// Creates `NewResearchForm` with provided values that is public by default.
    pub fn new(course_id: Uuid) -> Self {
        Self {
            course_id,
            content: Default::default(),
        }
    }

    /// Sets the content of this research form.
    pub fn set_content(mut self, content: serde_json::Value) -> Self {
        self.content = content;
        self
    }
}

pub async fn upsert_research_form(
    conn: &mut PgConnection,
    pkey_policy: PKeyPolicy<Uuid>,
    new_research_form: &NewResearchForm,
) -> ModelResult<ResearchForm> {
    let form_res = sqlx::query_as!(
        ResearchForm,
        "
INSERT INTO course_specific_research_consent_forms (
    id,
    course_id,
    content
  )
VALUES ($1, $2, $3) ON CONFLICT (course_id, deleted_at)
DO UPDATE SET content = $3
RETURNING *
",
        pkey_policy.into_uuid(),
        new_research_form.course_id,
        serde_json::to_value(new_research_form.content.clone())?,
    )
    .fetch_one(conn)
    .await?;
    Ok(form_res)
}

pub async fn get_research_form_with_course_id(
    conn: &mut PgConnection,
    course_id: Uuid,
) -> ModelResult<ResearchForm> {
    let form_res = sqlx::query_as!(
        ResearchForm,
        "
SELECT * FROM course_specific_research_consent_forms
WHERE course_id = $1
AND deleted_at IS NULL
",
        course_id,
    )
    .fetch_one(conn)
    .await?;
    Ok(form_res)
}

pub async fn upsert_research_form_questions(
    conn: &mut PgConnection,
    questions: &[NewResearchFormQuestion],
) -> ModelResult<Vec<ResearchFormQuestion>> {
    let mut tx = conn.begin().await?;

    let mut inserted_questions = Vec::new();

    for question in questions {
        let form_res = sqlx::query_as!(
            ResearchFormQuestion,
            "
INSERT INTO course_specific_consent_form_questions (
    id,
    course_id,
    research_consent_form_id,
    question
  )
VALUES ($1, $2, $3, $4) ON CONFLICT (id)
DO UPDATE SET question = $4,
deleted_at = NULL
RETURNING *
",
            question.question_id,
            question.course_id,
            question.research_consent_form_id,
            question.question
        )
        .fetch_one(&mut *tx)
        .await?;

        inserted_questions.push(form_res);
    }

    tx.commit().await?;

    Ok(inserted_questions)
}

pub async fn get_research_form_questions_with_course_id(
    conn: &mut PgConnection,
    course_id: Uuid,
) -> ModelResult<Vec<ResearchFormQuestion>> {
    let form_res = sqlx::query_as!(
        ResearchFormQuestion,
        "
SELECT * FROM course_specific_consent_form_questions
WHERE course_id = $1
AND deleted_at IS NULL
",
        course_id,
    )
    .fetch_all(conn)
    .await?;
    Ok(form_res)
}

pub struct ExportedCourseResearchFormQustionAnswer {
    pub course_id: Uuid,
    pub research_consent_form_id: Uuid,
    pub research_form_question_id: Uuid,
    pub question: String,
    pub user_id: Uuid,
    pub research_consent: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

pub fn stream_course_research_form_user_answers(
    conn: &mut PgConnection,
    course_id: Uuid,
) -> impl Stream<Item = sqlx::Result<ExportedCourseResearchFormQustionAnswer>> + '_ {
    sqlx::query_as!(
        ExportedCourseResearchFormQustionAnswer,
        r#"
    SELECT DISTINCT ON (a.research_form_question_id, a.user_id)
        q.course_id,
        q.research_consent_form_id,
        a.research_form_question_id,
        q.question,
        a.user_id,
        a.research_consent,
        a.created_at,
        a.updated_at
        FROM course_specific_consent_form_answers a
    LEFT JOIN course_specific_consent_form_questions q ON a.research_form_question_id = q.id
    WHERE a.course_id = $1
    AND a.deleted_at IS NULL
    AND q.deleted_at IS NULL
    ORDER BY a.user_id, a.research_form_question_id, a.updated_at DESC
    "#,
        course_id
    )
    .fetch(conn)
}

pub async fn upsert_research_form_anwser(
    conn: &mut PgConnection,
    course_id: Uuid,
    answer: &NewResearchFormQuestionAnswer,
) -> ModelResult<Uuid> {
    let form_res = sqlx::query!(
        "
INSERT INTO course_specific_consent_form_answers (
    user_id,
    course_id,
    research_form_question_id,
    research_consent
  )
VALUES ($1, $2, $3, $4) ON CONFLICT (user_id, research_form_question_id)
DO UPDATE SET research_consent = $4
RETURNING *
",
        answer.user_id,
        course_id,
        answer.research_form_question_id,
        answer.research_consent
    )
    .fetch_one(conn)
    .await?;
    Ok(form_res.id)
}

pub async fn get_research_form_answers_with_user_id(
    conn: &mut PgConnection,
    course_id: Uuid,
    user_id: Uuid,
) -> ModelResult<Vec<ResearchFormQuestionAnswer>> {
    let form_res = sqlx::query_as!(
        ResearchFormQuestionAnswer,
        "
SELECT * FROM course_specific_consent_form_answers
WHERE course_id = $1 AND user_id = $2
AND deleted_at IS NULL
",
        course_id,
        user_id
    )
    .fetch_all(conn)
    .await?;
    Ok(form_res)
}

pub async fn get_all_research_form_answers_with_user_id(
    conn: &mut PgConnection,
    user_id: Uuid,
) -> ModelResult<Vec<ResearchFormQuestionAnswer>> {
    let form_res = sqlx::query_as!(
        ResearchFormQuestionAnswer,
        "
SELECT * FROM course_specific_consent_form_answers
WHERE user_id = $1
AND deleted_at IS NULL
",
        user_id
    )
    .fetch_all(conn)
    .await?;
    Ok(form_res)
}