This commit is contained in:
Dominic Grimm 2022-12-20 19:00:12 +01:00
parent f13c9c905f
commit f614e606f4
No known key found for this signature in database
GPG key ID: 6F294212DEAAC530
15 changed files with 269 additions and 214 deletions

View file

@ -3,6 +3,8 @@ use celery::error::TaskError;
use celery::task::TaskResult;
use chrono::prelude::*;
use diesel::prelude::*;
use std::thread;
use std::time::Duration;
use crate::{config, db};
@ -39,7 +41,7 @@ async fn fetch_current_tenant(
schoolyear_id: i32,
) -> Result<()> {
let tenant = client.current_tenant().await?;
if diesel::select(diesel::dsl::not(diesel::expression::exists::exists(
if diesel::select(diesel::dsl::not(diesel::dsl::exists(
db::schema::tenants::table.filter(db::schema::tenants::untis_id.eq(tenant.id)),
)))
.get_result::<bool>(conn)?
@ -56,7 +58,7 @@ async fn fetch_current_tenant(
active: true,
})
.execute(conn)?;
} else if diesel::select(diesel::expression::exists::exists(
} else if diesel::select(diesel::dsl::exists(
db::schema::tenants::table
.filter(db::schema::tenants::untis_id.eq(tenant.id))
.filter(db::schema::tenants::active.eq(false)),
@ -270,7 +272,7 @@ async fn fetch_substitutions(
schoolyear_id: i32,
) -> Result<()> {
let today = Utc::now().date_naive();
if diesel::select(diesel::dsl::not(diesel::expression::exists::exists(
if diesel::select(diesel::dsl::not(diesel::dsl::exists(
db::schema::substitution_queries::table
.filter(db::schema::substitution_queries::date.eq(today)),
)))
@ -289,12 +291,19 @@ async fn fetch_substitutions(
.filter(db::schema::substitution_queries::active)
.load::<db::models::SubstitutionQuery>(conn)?
{
let now = Utc::now().naive_utc();
let query_result_id = diesel::insert_into(db::schema::substitution_query_results::table)
.values(db::models::NewSubstitutionQueryResult {
substitution_query_id: query.id,
queried_at: Utc::now().naive_utc(),
})
.returning(db::schema::substitution_query_results::id)
.get_result::<i32>(conn)?;
let substs = client.substitutions(&query.date, &query.date, None).await?;
for substitution in substs {
let substitution_id = diesel::insert_into(db::schema::substitutions::table)
.values(db::models::NewSubstitution {
substitution_query_id: query.id,
substitution_query_result_id: query_result_id,
subst_type: substitution.subst_type.into(),
lesson_id: substitution.lesson_id,
start_time: substitution.start_time,
@ -400,14 +409,19 @@ async fn fetch_substitutions(
#[celery::task]
pub async fn update_info() -> TaskResult<()> {
let dur = Duration::from_secs(10);
thread::sleep(dur);
dbg!("DONE!");
let mut client = match config::untis_from_env() {
Ok(x) => x,
Err(e) => return Err(TaskError::UnexpectedError(e.to_string())),
};
if let Err(e) = client.login().await {
return Err(TaskError::UnexpectedError(e.to_string()));
}
thread::sleep(dur);
dbg!("DONE!");
let conn = &mut match db::POOL.get() {
Ok(x) => x,
@ -418,34 +432,54 @@ pub async fn update_info() -> TaskResult<()> {
Ok(x) => x,
Err(e) => return Err(TaskError::UnexpectedError(e.to_string())),
};
thread::sleep(dur);
dbg!("DONE!");
if let Err(e) = fetch_current_tenant(&client, conn, schoolyear_id).await {
return Err(TaskError::UnexpectedError(e.to_string()));
}
thread::sleep(dur);
dbg!("DONE!");
if let Err(e) = fetch_teachers(&client, conn, schoolyear_id).await {
return Err(TaskError::UnexpectedError(e.to_string()));
}
thread::sleep(dur);
dbg!("DONE!");
if let Err(e) = fetch_classes(&client, conn, schoolyear_id).await {
return Err(TaskError::UnexpectedError(e.to_string()));
}
thread::sleep(dur);
dbg!("DONE!");
if let Err(e) = fetch_subjects(&client, conn, schoolyear_id).await {
return Err(TaskError::UnexpectedError(e.to_string()));
}
thread::sleep(dur);
dbg!("DONE!");
if let Err(e) = fetch_rooms(&client, conn, schoolyear_id).await {
return Err(TaskError::UnexpectedError(e.to_string()));
}
thread::sleep(dur);
dbg!("DONE!");
if let Err(e) = fetch_departments(&client, conn, schoolyear_id).await {
return Err(TaskError::UnexpectedError(e.to_string()));
}
thread::sleep(dur);
dbg!("DONE!");
if let Err(e) = fetch_holidays(&client, conn, schoolyear_id).await {
return Err(TaskError::UnexpectedError(e.to_string()));
}
thread::sleep(dur);
dbg!("DONE!");
if let Err(e) = fetch_substitutions(&client, conn, schoolyear_id).await {
return Err(TaskError::UnexpectedError(e.to_string()));
}
thread::sleep(dur);
dbg!("DONE!");
if let Err(e) = client.logout().await {
return Err(TaskError::UnexpectedError(e.to_string()));
}
thread::sleep(dur);
dbg!("DONE!");
Ok(())
}