fix: store and get submission ID

DESCRIPTION
    If the same survey_user submits the survey many times, their first
    submission ID was being used to record benchmarks from their future
    submissions. Which resulted in submissions with empty benchmarks.

    This patch gets the submission ID as the record is being created and
    records benches using that. Bug fix and one less query.
This commit is contained in:
Aravinth Manivannan 2023-01-26 20:55:23 +05:30
parent e9709ed744
commit c576487333
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -215,43 +215,29 @@ async fn submit(
let user_id = Uuid::from_str(&username).unwrap();
let payload = payload.into_inner();
sqlx::query!(
struct ID {
id: i32,
}
let resp_id = sqlx::query_as!(
ID,
"INSERT INTO survey_responses (
user_id,
campaign_id,
device_user_provided,
device_software_recognised,
threads
) VALUES ($1, $2, $3, $4, $5);",
) VALUES ($1, $2, $3, $4, $5)
RETURNING ID;",
&user_id,
&campaign_id,
&payload.device_user_provided,
&payload.device_software_recognised,
&payload.threads
)
.execute(&data.db)
.await?;
struct ID {
id: i32,
}
let resp_id = sqlx::query_as!(
ID,
"SELECT ID
FROM survey_responses
WHERE
user_id = $1
AND
device_software_recognised = $2;",
&user_id,
&payload.device_software_recognised
)
.fetch_one(&data.db)
.await?;
let mut futs = Vec::with_capacity(payload.benches.len());
for bench in payload.benches.iter() {
let fut = sqlx::query!(
"INSERT INTO survey_benches
@ -261,8 +247,7 @@ async fn submit(
&bench.difficulty,
bench.duration
)
.execute(&data.db);
.execute(&data.db); //.await?;
futs.push(fut);
}