357 lines
12 KiB
Rust
357 lines
12 KiB
Rust
use anyhow::Result;
|
|
use celery::error::TaskError;
|
|
use celery::task::TaskResult;
|
|
use chrono::prelude::*;
|
|
use diesel::prelude::*;
|
|
use now::DateTimeNow;
|
|
|
|
use crate::config;
|
|
use crate::db;
|
|
use crate::untis;
|
|
|
|
async fn fetch_schoolyears(client: &untis::Client, conn: &mut PgConnection) -> Result<i32> {
|
|
let existing_schoolyears = db::schema::schoolyears::table
|
|
.select(db::schema::schoolyears::untis_id)
|
|
.load::<i32>(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,
|
|
start_date: y.start_date,
|
|
end_date: y.end_date,
|
|
})
|
|
.collect::<Vec<db::models::NewSchoolyear>>(),
|
|
)
|
|
.execute(conn)?;
|
|
|
|
Ok(db::schema::schoolyears::table
|
|
.filter(db::schema::schoolyears::untis_id.eq(client.current_schoolyear().await?.id))
|
|
.select(db::schema::schoolyears::id)
|
|
.first(conn)?)
|
|
}
|
|
|
|
async fn fetch_current_tenant(
|
|
client: &untis::Client,
|
|
conn: &mut PgConnection,
|
|
schoolyear_id: i32,
|
|
) -> Result<()> {
|
|
let tenant = client.current_tenant().await?;
|
|
if diesel::select(diesel::dsl::not(diesel::expression::exists::exists(
|
|
db::schema::tenants::table.filter(db::schema::tenants::untis_id.eq(tenant.id)),
|
|
)))
|
|
.get_result::<bool>(conn)?
|
|
{
|
|
diesel::update(db::schema::tenants::table)
|
|
.filter(db::schema::tenants::active)
|
|
.set(db::schema::tenants::active.eq(false))
|
|
.execute(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(conn)?;
|
|
} else if diesel::select(diesel::expression::exists::exists(
|
|
db::schema::tenants::table
|
|
.filter(db::schema::tenants::untis_id.eq(tenant.id))
|
|
.filter(db::schema::tenants::active.eq(false)),
|
|
))
|
|
.get_result::<bool>(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(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(conn)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_teachers(
|
|
client: &untis::Client,
|
|
conn: &mut PgConnection,
|
|
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>(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(conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_classes(
|
|
client: &untis::Client,
|
|
conn: &mut PgConnection,
|
|
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>(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(conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_subjects(
|
|
client: &untis::Client,
|
|
conn: &mut PgConnection,
|
|
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>(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(conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_rooms(
|
|
client: &untis::Client,
|
|
conn: &mut PgConnection,
|
|
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>(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(conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_departments(
|
|
client: &untis::Client,
|
|
conn: &mut PgConnection,
|
|
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>(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(conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_holidays(
|
|
client: &untis::Client,
|
|
conn: &mut PgConnection,
|
|
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>(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(conn)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn fetch_substitutions(
|
|
client: &untis::Client,
|
|
conn: &mut PgConnection,
|
|
schoolyear_id: i32,
|
|
) -> Result<()> {
|
|
let now = Utc::now();
|
|
let beginning_of_week = now.beginning_of_week().date_naive();
|
|
let end_of_week = now.end_of_week().date_naive();
|
|
// if diesel::select(diesel::dsl::not(diesel::expression::exists::exists(
|
|
// db::schema::substitution_queries::table
|
|
// .filter(db::schema::substitution_queries::active)
|
|
// .filter(db::schema::substitution_queries::end_date.eq(end_of_week)),
|
|
// )))
|
|
// .get_result::<bool>(conn)?
|
|
// {}
|
|
|
|
// for query in db::schema::substitution_queries::table
|
|
// .filter(db::schema::substitution_queries::active)
|
|
// .load::<db::models::SubstitutionQuery>(conn)?
|
|
// {
|
|
// dbg!(&query);
|
|
// }
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[celery::task]
|
|
pub async fn update_info() -> TaskResult<()> {
|
|
let mut client = untis::Client {
|
|
api_url: match url::Url::parse(&config::CONFIG.untis_api_url) {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(TaskError::UnexpectedError(e.to_string())),
|
|
},
|
|
rpc_url: match url::Url::parse(&config::CONFIG.untis_rpc_url) {
|
|
Ok(x) => untis::Client::gen_rpc_url(x, &config::CONFIG.untis_school),
|
|
Err(e) => return Err(TaskError::UnexpectedError(e.to_string())),
|
|
},
|
|
username: config::CONFIG.untis_username.to_owned(),
|
|
password: config::CONFIG.untis_password.to_owned(),
|
|
session: None,
|
|
authorization: None,
|
|
};
|
|
|
|
if let Err(e) = client.login().await {
|
|
return Err(TaskError::UnexpectedError(e.to_string()));
|
|
}
|
|
|
|
let conn = &mut match db::POOL.get() {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(TaskError::UnexpectedError(e.to_string())),
|
|
};
|
|
|
|
let schoolyear_id = match fetch_schoolyears(&client, conn).await {
|
|
Ok(x) => x,
|
|
Err(e) => return Err(TaskError::UnexpectedError(e.to_string())),
|
|
};
|
|
if let Err(e) = fetch_current_tenant(&client, conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(e.to_string()));
|
|
}
|
|
if let Err(e) = fetch_teachers(&client, conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(e.to_string()));
|
|
}
|
|
if let Err(e) = fetch_classes(&client, conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(e.to_string()));
|
|
}
|
|
if let Err(e) = fetch_subjects(&client, conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(e.to_string()));
|
|
}
|
|
if let Err(e) = fetch_rooms(&client, conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(e.to_string()));
|
|
}
|
|
if let Err(e) = fetch_departments(&client, conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(e.to_string()));
|
|
}
|
|
if let Err(e) = fetch_holidays(&client, conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(e.to_string()));
|
|
}
|
|
if let Err(e) = fetch_substitutions(&client, conn, schoolyear_id).await {
|
|
return Err(TaskError::UnexpectedError(e.to_string()));
|
|
}
|
|
|
|
if let Err(e) = client.logout().await {
|
|
return Err(TaskError::UnexpectedError(e.to_string()));
|
|
}
|
|
|
|
Ok(())
|
|
}
|