Handle records with sheet ids
use anyhow::Result;
+use async_bb8_diesel::AsyncRunQueryDsl;
use chrono::{DateTime, Utc};
-use cio_api::applicants::Applicant;
+use cio_api::{applicants::Applicant, schema::applicants};
+use diesel::{ExpressionMethods, QueryDsl};
use dropshot::RequestContext;
use schemars::JsonSchema;
use serde::Serialize;
@@ -24,12 +26,18 @@ pub struct ApplicantInfo {
pub async fn handle_applicant_info(rqctx: Arc<RequestContext<Context>>, email: String) -> Result<ApplicantInfo> {
let ctx = rqctx.context();
- // Third argument is a Google Sheet id. This is a no-longer supported argument
- let applicant = Applicant::get_from_db(&ctx.db, email.clone(), "".to_string()).await;
+ // Applicants is unfortunately not unique on the email column. We need to return the newest
+ // record to work around a few cases where ignoring the Google sheet id resulted in multiple
+ // records being created
+ let applicants = applicants::dsl::applicants
+ .filter(applicants::dsl::email.eq(email.clone()))
+ .order_by(applicants::dsl::id.desc())
+ .load_async::<Applicant>(ctx.db.pool())
+ .await?;
Ok(ApplicantInfo {
email,
- application: applicant.map(|applicant| ApplicationView {
+ application: applicants.into_iter().next().map(|applicant| ApplicationView {
role: applicant.role,
submitted_at: applicant.submitted_time,
status: applicant.status,
@@ -49,7 +57,6 @@ pub async fn handle_applicant_upload_token(
) -> Result<ApplicantUploadToken> {
let ctx = rqctx.context();
- // Third argument is a Google Sheet id. This is a no-longer supported argument
let token = ctx.upload_token_store.get(&email).await?;
Ok(ApplicantUploadToken {
