382 lines
12 KiB
Rust
382 lines
12 KiB
Rust
use anyhow::Result;
|
|
use celery::{error::TaskError, task::TaskResult};
|
|
use diesel::prelude::*;
|
|
use std::thread;
|
|
use std::time::Duration;
|
|
|
|
use crate::config;
|
|
use crate::db;
|
|
|
|
async fn fetch_schoolyears(client: &untis::Client, db_conn: &mut db::Connection) -> Result<i32> {
|
|
let existing_schoolyears = db::schema::schoolyears::table
|
|
.select(db::schema::schoolyears::untis_id)
|
|
.load::<i32>(db_conn)?;
|
|
diesel::insert_into(db::schema::schoolyears::table)
|
|
.values(
|
|
&client
|
|
.schoolyears()
|
|
.await?
|
|
.iter()
|
|
.filter(|y| !existing_schoolyears.contains(&y.id))
|
|
.map(|y| db::models::NewSchoolyear {
|
|
untis_id: y.id,
|
|
name: &y.name,
|
|
active: false,
|
|
})
|
|
.collect::<Vec<db::models::NewSchoolyear>>(),
|
|
)
|
|
.execute(db_conn)?;
|
|
|
|
let id = db::schema::schoolyears::table
|
|
.filter(db::schema::schoolyears::untis_id.eq(client.current_schoolyear().await?.id))
|
|
.select(db::schema::schoolyears::id)
|
|
.first(db_conn)?;
|
|
|
|
diesel::update(db::schema::schoolyears::table)
|
|
.set(db::schema::schoolyears::active.eq(false))
|
|
.execute(db_conn)?;
|
|
diesel::update(db::schema::schoolyears::table)
|
|
.filter(db::schema::schoolyears::id.eq(id))
|
|
.set(db::schema::schoolyears::active.eq(true))
|
|
.execute(db_conn)?;
|
|
|
|
Ok(id)
|
|
}
|
|
|
|
async fn fetch_current_tenant(
|
|
client: &untis::Client,
|
|
db_conn: &mut db::Connection,
|
|
schoolyear_id: i32,
|
|
) -> Result<()> {
|
|
let tenant = client.current_tenant().await?;
|
|
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>(db_conn)?
|
|
{
|
|
diesel::update(db::schema::tenants::table)
|
|
.filter(db::schema::tenants::active)
|
|
.set(db::schema::tenants::active.eq(false))
|
|
.execute(db_conn)?;
|
|
diesel::insert_into(db::schema::tenants::table)
|
|
.values(db::models::NewTenant {
|
|
untis_id: tenant.id,
|
|
schoolyear_id,
|
|
name: &tenant.display_name,
|
|
active: true,
|
|
})
|
|
.execute(db_conn)?;
|
|
} 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)),
|
|
))
|
|
.get_result::<bool>(db_conn)?
|
|
{
|
|
diesel::update(db::schema::tenants::table)
|
|
.filter(db::schema::tenants::active)
|
|
.set((
|
|
db::schema::tenants::active.eq(false),
|
|
db::schema::tenants::updated_at.eq(diesel::dsl::now),
|
|
))
|
|
.execute(db_conn)?;
|
|
diesel::update(db::schema::tenants::table)
|
|
.filter(db::schema::tenants::untis_id.eq(tenant.id))
|
|
.set((
|
|
db::schema::tenants::active.eq(true),
|
|
db::schema::tenants::updated_at.eq(diesel::dsl::now),
|
|
))
|
|
.execute(db_conn)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_timegrid(
|
|
client: &untis::Client,
|
|
db_conn: &mut db::Connection,
|
|
schoolyear_id: i32,
|
|
) -> Result<()> {
|
|
let days = client.timegrid().await?;
|
|
|
|
diesel::update(db::schema::timegrids::table)
|
|
.filter(db::schema::timegrids::active)
|
|
.set(db::schema::timegrids::active.eq(false))
|
|
.execute(db_conn)?;
|
|
let timegrid_id = diesel::insert_into(db::schema::timegrids::table)
|
|
.values(db::models::NewTimegrid {
|
|
schoolyear_id,
|
|
active: true,
|
|
})
|
|
.returning(db::schema::timegrids::id)
|
|
.get_result::<i32>(db_conn)?;
|
|
|
|
for day in days {
|
|
let timegrid_day_id = diesel::insert_into(db::schema::timegrid_days::table)
|
|
.values(db::models::NewTimegridDay {
|
|
timegrid_id,
|
|
weekday: day.day.try_into()?,
|
|
})
|
|
.returning(db::schema::timegrid_days::id)
|
|
.get_result::<i32>(db_conn)?;
|
|
diesel::insert_into(db::schema::timegrid_time_unit::table)
|
|
.values(
|
|
day.time_units
|
|
.into_iter()
|
|
.map(|x| db::models::NewTimegridTimeUnit {
|
|
timegrid_day_id,
|
|
start_time: x.start_time,
|
|
end_time: x.end_time,
|
|
})
|
|
.collect::<Vec<_>>(),
|
|
)
|
|
.execute(db_conn)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_teachers(
|
|
client: &untis::Client,
|
|
db_conn: &mut db::Connection,
|
|
schoolyear_id: i32,
|
|
) -> Result<()> {
|
|
let existing_teachers = db::schema::teachers::table
|
|
.select(db::schema::teachers::untis_id)
|
|
.filter(db::schema::teachers::schoolyear_id.eq(schoolyear_id))
|
|
.load::<i32>(db_conn)?;
|
|
diesel::insert_into(db::schema::teachers::table)
|
|
.values(
|
|
&client
|
|
.teachers()
|
|
.await?
|
|
.iter()
|
|
.filter(|t| !existing_teachers.contains(&t.id))
|
|
.map(|t| db::models::NewTeacher {
|
|
untis_id: t.id,
|
|
schoolyear_id,
|
|
name: &t.name,
|
|
forename: if t.forename.is_empty() {
|
|
None
|
|
} else {
|
|
Some(&t.forename)
|
|
},
|
|
display_name: &t.display_name,
|
|
})
|
|
.collect::<Vec<db::models::NewTeacher>>(),
|
|
)
|
|
.execute(db_conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_classes(
|
|
client: &untis::Client,
|
|
db_conn: &mut db::Connection,
|
|
schoolyear_id: i32,
|
|
) -> Result<()> {
|
|
let existing_classes = db::schema::classes::table
|
|
.select(db::schema::classes::untis_id)
|
|
.filter(db::schema::classes::schoolyear_id.eq(schoolyear_id))
|
|
.load::<i32>(db_conn)?;
|
|
diesel::insert_into(db::schema::classes::table)
|
|
.values(
|
|
&client
|
|
.classes()
|
|
.await?
|
|
.iter()
|
|
.filter(|c| !existing_classes.contains(&c.id))
|
|
.map(|c| db::models::NewClass {
|
|
untis_id: c.id,
|
|
schoolyear_id,
|
|
name: &c.name,
|
|
long_name: &c.long_name,
|
|
active: c.active,
|
|
})
|
|
.collect::<Vec<db::models::NewClass>>(),
|
|
)
|
|
.execute(db_conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_subjects(
|
|
client: &untis::Client,
|
|
db_conn: &mut db::Connection,
|
|
schoolyear_id: i32,
|
|
) -> Result<()> {
|
|
let existing_classes = db::schema::subjects::table
|
|
.select(db::schema::subjects::untis_id)
|
|
.filter(db::schema::subjects::schoolyear_id.eq(schoolyear_id))
|
|
.load::<i32>(db_conn)?;
|
|
diesel::insert_into(db::schema::subjects::table)
|
|
.values(
|
|
&client
|
|
.subjects()
|
|
.await?
|
|
.iter()
|
|
.filter(|c| !existing_classes.contains(&c.id))
|
|
.map(|c| db::models::NewSubject {
|
|
untis_id: c.id,
|
|
schoolyear_id,
|
|
name: &c.name,
|
|
long_name: &c.long_name,
|
|
})
|
|
.collect::<Vec<db::models::NewSubject>>(),
|
|
)
|
|
.execute(db_conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_rooms(
|
|
client: &untis::Client,
|
|
db_conn: &mut db::Connection,
|
|
schoolyear_id: i32,
|
|
) -> Result<()> {
|
|
let existing_classes = db::schema::rooms::table
|
|
.select(db::schema::rooms::untis_id)
|
|
.filter(db::schema::rooms::schoolyear_id.eq(schoolyear_id))
|
|
.load::<i32>(db_conn)?;
|
|
diesel::insert_into(db::schema::rooms::table)
|
|
.values(
|
|
&client
|
|
.rooms()
|
|
.await?
|
|
.iter()
|
|
.filter(|c| !existing_classes.contains(&c.id))
|
|
.map(|c| db::models::NewRoom {
|
|
untis_id: c.id,
|
|
schoolyear_id,
|
|
name: &c.name,
|
|
long_name: &c.long_name,
|
|
})
|
|
.collect::<Vec<db::models::NewRoom>>(),
|
|
)
|
|
.execute(db_conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_departments(
|
|
client: &untis::Client,
|
|
db_conn: &mut db::Connection,
|
|
schoolyear_id: i32,
|
|
) -> Result<()> {
|
|
let existing_classes = db::schema::departments::table
|
|
.select(db::schema::departments::untis_id)
|
|
.filter(db::schema::departments::schoolyear_id.eq(schoolyear_id))
|
|
.load::<i32>(db_conn)?;
|
|
diesel::insert_into(db::schema::departments::table)
|
|
.values(
|
|
&client
|
|
.departments()
|
|
.await?
|
|
.iter()
|
|
.filter(|c| !existing_classes.contains(&c.id))
|
|
.map(|c| db::models::NewDepartment {
|
|
untis_id: c.id,
|
|
schoolyear_id,
|
|
name: &c.name,
|
|
long_name: &c.long_name,
|
|
})
|
|
.collect::<Vec<db::models::NewDepartment>>(),
|
|
)
|
|
.execute(db_conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_holidays(
|
|
client: &untis::Client,
|
|
db_conn: &mut db::Connection,
|
|
schoolyear_id: i32,
|
|
) -> Result<()> {
|
|
let existing_classes = db::schema::holidays::table
|
|
.select(db::schema::holidays::untis_id)
|
|
.filter(db::schema::holidays::schoolyear_id.eq(schoolyear_id))
|
|
.load::<i32>(db_conn)?;
|
|
diesel::insert_into(db::schema::holidays::table)
|
|
.values(
|
|
&client
|
|
.holidays()
|
|
.await?
|
|
.iter()
|
|
.filter(|c| !existing_classes.contains(&c.id))
|
|
.map(|c| db::models::NewHoliday {
|
|
untis_id: c.id,
|
|
schoolyear_id,
|
|
name: &c.name,
|
|
long_name: &c.long_name,
|
|
start_date: c.start_date,
|
|
end_date: c.end_date,
|
|
})
|
|
.collect::<Vec<db::models::NewHoliday>>(),
|
|
)
|
|
.execute(db_conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[celery::task]
|
|
pub async fn update_meta() -> TaskResult<()> {
|
|
let dur = Duration::from_secs(2);
|
|
let mut client = match config::untis_from_env() {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(TaskError::UnexpectedError(format!("{:?}", e))),
|
|
};
|
|
if let Err(e) = client.login().await {
|
|
return Err(TaskError::UnexpectedError(format!("{:?}", e)));
|
|
}
|
|
thread::sleep(dur);
|
|
|
|
let db_conn = &mut match db::POOL.get() {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(TaskError::UnexpectedError(format!("{:?}", e))),
|
|
};
|
|
thread::sleep(dur);
|
|
|
|
let schoolyear_id = match fetch_schoolyears(&client, db_conn).await {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(TaskError::UnexpectedError(format!("{:?}", e))),
|
|
};
|
|
thread::sleep(dur);
|
|
if let Err(e) = fetch_current_tenant(&client, db_conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(format!("{:?}", e)));
|
|
}
|
|
thread::sleep(dur);
|
|
if let Err(e) = fetch_timegrid(&client, db_conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(format!("{:?}", e)));
|
|
}
|
|
thread::sleep(dur);
|
|
if let Err(e) = fetch_teachers(&client, db_conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(format!("{:?}", e)));
|
|
}
|
|
thread::sleep(dur);
|
|
if let Err(e) = fetch_classes(&client, db_conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(format!("{:?}", e)));
|
|
}
|
|
thread::sleep(dur);
|
|
if let Err(e) = fetch_subjects(&client, db_conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(format!("{:?}", e)));
|
|
}
|
|
thread::sleep(dur);
|
|
if let Err(e) = fetch_rooms(&client, db_conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(format!("{:?}", e)));
|
|
}
|
|
thread::sleep(dur);
|
|
if let Err(e) = fetch_departments(&client, db_conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(format!("{:?}", e)));
|
|
}
|
|
thread::sleep(dur);
|
|
if let Err(e) = fetch_holidays(&client, db_conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(format!("{:?}", e)));
|
|
}
|
|
|
|
if let Err(e) = client.logout().await {
|
|
return Err(TaskError::UnexpectedError(format!("{:?}", e)));
|
|
}
|
|
|
|
Ok(())
|
|
}
|