pub async fn two_week_schedule(user_id: i32, conn: &Database) -> Result<(), SchedulingError> {
    let (_user, calendar) = conn
        .run(move |c| {
            users::table
                .filter(users::id.eq(user_id))
                .inner_join(calendar::table)
                .select((users::all_columns, calendar::all_columns))
                .first::<(User, crate::models::calendar::Calendar)>(c)
        })
        .await?;
    let r#type = calendar.calendar_type;
    let (lovelace_client, user_client) = match parse_calendar_type(r#type) {
        crate::models::calendar::CalendarType::GoogleCalendar => {
            let gcal = conn
                .run(move |c| {
                    google_calendar::table
                        .filter(google_calendar::id.eq(calendar.id))
                        .first::<GoogleCalendar>(c)
                })
                .await?;

            cfg_if! {
                if #[cfg(test)] {
                    let user_calendar_url = "http://localhost:8080/user/calendars/calendar".to_string();
                } else {
                    let user_calendar_url = format!("https://apidata.googleusercontent.com/caldav/v2/{}/events", gcal.calendar_id);
                }
            };

            cfg_if! {
                if #[cfg(test)] {
                    (
                        DavClient::new_unauthenticated(
                            gcal.lovelace_calendar_id,
                        ),
                        DavClient::new_unauthenticated(
                            user_calendar_url,
                        ),
                    )
                } else {
                    (
                        DavClient::new_oauth(
                            gcal.lovelace_calendar_id,
                            gcal.access_token.clone(),
                        ),
                        DavClient::new_oauth(
                            user_calendar_url,
                            gcal.access_token,
                        ),
                    )
                }
            }
        }
        crate::models::calendar::CalendarType::CalDav => {
            todo!()
        }
        crate::models::calendar::CalendarType::CalDavUnauthenticated => {
            todo!()
        }
    };

    let lovelace_controller = lovelace_client.calendar();
    let user_controller = user_client.calendar();
    let user_events = user_controller
        .date_search(
            chrono::Utc::now(),
            chrono::Utc::now() + chrono::Duration::days(14),
        )
        .await
        .unwrap();
    let set_events = lovelace_controller
        .date_search(Utc::now(), Utc::now() + Duration::days(14))
        .await?;
    let mut tasks = conn
        .run(move |c| {
            student_class_asynchronous_task::table
                .inner_join(class_student::table.inner_join(users::table))
                .inner_join(class_asynchronous_task::table)
                .filter(users::id.eq(user_id))
                .filter(class_asynchronous_task::due_date.ge(chrono::Utc::now().naive_utc()))
                .filter(
                    class_asynchronous_task::due_date
                        .le((chrono::Utc::now() + Duration::days(14)).naive_utc()),
                )
                .select((
                    class_asynchronous_task::all_columns,
                    student_class_asynchronous_task::all_columns,
                ))
                .load::<(ClassAsynchronousTask, StudentClassAsynchronousTask)>(c)
        })
        .await
        .unwrap();
    let free_slots = map_user_events_to_free_time(user_events).await;

    let mut events_to_add = vec![];

    for slot in free_slots {
        if tasks.is_empty() {
            break;
        }
        fill_slot(slot, &mut tasks, &mut events_to_add);
    }

    for event in set_events {
        event.delete().await?;
    }

    for event in events_to_add {
        lovelace_controller.save_event(event).await?;
    }

    Ok(())
}
/// Creates a schedule for the next two weeks.
///
/// Schedules cannot be created for people who have not connected a calendar.
Creates a schedule for the next two weeks.
Schedules cannot be created for people who have not connected a calendar.
Creates a schedule for the next two weeks . Schedules cannot be created for people who have not connected a calendar .
pub async fn two_week_schedule(user_id: i32, conn: &Database) -> Result<(), SchedulingError> {
    let (_user, calendar) = conn
        .run(move |c| {
            users::table
                .filter(users::id.eq(user_id))
                .inner_join(calendar::table)
                .select((users::all_columns, calendar::all_columns))
                .first::<(User, crate::models::calendar::Calendar)>(c)
        })
        .await?;
    let r#type = calendar.calendar_type;
    let (lovelace_client, user_client) = match parse_calendar_type(r#type) {
        crate::models::calendar::CalendarType::GoogleCalendar => {
            let gcal = conn
                .run(move |c| {
                    google_calendar::table
                        .filter(google_calendar::id.eq(calendar.id))
                        .first::<GoogleCalendar>(c)
                })
                .await?;
            cfg_if! {
                if #[cfg(test)] {
                    let user_calendar_url = "http://localhost:8080/user/calendars/calendar".to_string();
                } else {
                    let user_calendar_url = format!("https://apidata.googleusercontent.com/caldav/v2/{}/events", gcal.calendar_id);
                }
            };
            cfg_if! {
                if #[cfg(test)] {
                    (
                        DavClient::new_unauthenticated(
                            gcal.lovelace_calendar_id,
                        ),
                        DavClient::new_unauthenticated(
                            user_calendar_url,
                        ),
                    )
                } else {
                    (
                        DavClient::new_oauth(
                            gcal.lovelace_calendar_id,
                            gcal.access_token.clone(),
                        ),
                        DavClient::new_oauth(
                            user_calendar_url,
                            gcal.access_token,
                        ),
                    )
                }
            }
        }
        crate::models::calendar::CalendarType::CalDav => {
            todo!()
        }
        crate::models::calendar::CalendarType::CalDavUnauthenticated => {
            todo!()
        }
    };
    let lovelace_controller = lovelace_client.calendar();
    let user_controller = user_client.calendar();
    let user_events = user_controller
        .date_search(
            chrono::Utc::now(),
            chrono::Utc::now() + chrono::Duration::days(14),
        )
        .await
        .unwrap();
    let set_events = lovelace_controller
        .date_search(Utc::now(), Utc::now() + Duration::days(14))
        .await?;
    let mut tasks = conn
        .run(move |c| {
            student_class_asynchronous_task::table
                .inner_join(class_student::table.inner_join(users::table))
                .inner_join(class_asynchronous_task::table)
                .filter(users::id.eq(user_id))
                .filter(class_asynchronous_task::due_date.ge(chrono::Utc::now().naive_utc()))
                .filter(
                    class_asynchronous_task::due_date
                        .le((chrono::Utc::now() + Duration::days(14)).naive_utc()),
                )
                .select((
                    class_asynchronous_task::all_columns,
                    student_class_asynchronous_task::all_columns,
                ))
                .load::<(ClassAsynchronousTask, StudentClassAsynchronousTask)>(c)
        })
        .await
        .unwrap();
    let free_slots = map_user_events_to_free_time(user_events).await;
    let mut events_to_add = vec![];
    for slot in free_slots {
        if tasks.is_empty() {
            break;
        }
        fill_slot(slot, &mut tasks, &mut events_to_add);
    }
    for event in set_events {
        event.delete().await?;
    }
    for event in events_to_add {
        lovelace_controller.save_event(event).await?;
    }
    Ok(())
}
pub async fn two_week_schedule ( user_id : i32 , conn : & Database ) -> Result < ( ) , SchedulingError > { let ( _user , calendar ) = conn . run ( move | c | { users :: table . filter ( users :: id . eq ( user_id ) ) . inner_join ( calendar :: table ) . select ( ( users :: all_columns , calendar :: all_columns ) ) . first :: < ( User , crate :: models :: calendar :: Calendar ) > ( c ) } ) . await ? ; let r#type = calendar . calendar_type ; let ( lovelace_client , user_client ) = match parse_calendar_type ( r#type ) { crate :: models :: calendar :: CalendarType :: GoogleCalendar => { let gcal = conn . run ( move | c | { google_calendar :: table . filter ( google_calendar :: id . eq ( calendar . id ) ) . first :: < GoogleCalendar > ( c ) } ) . await ? ; cfg_if ! { if [ cfg ( test ) ] { let user_calendar_url " " to_string ( ) } else { let user_calendar_url format ( " " gcal calendar_id ) } } ; cfg_if ! { if [ cfg ( test ) ] { ( DavClient new_unauthenticated ( gcal lovelace_calendar_id ) DavClient new_unauthenticated ( user_calendar_url ) ) } else { ( DavClient new_oauth ( gcal lovelace_calendar_id gcal access_token clone ( ) ) DavClient new_oauth ( user_calendar_url gcal access_token ) ) } } } crate :: models :: calendar :: CalendarType :: CalDav => { todo ! ( ) } crate :: models :: calendar :: CalendarType :: CalDavUnauthenticated => { todo ! ( ) } } ; let lovelace_controller = lovelace_client . calendar ( ) ; let user_controller = user_client . calendar ( ) ; let user_events = user_controller . date_search ( chrono :: Utc :: now ( ) , chrono :: Utc :: now ( ) + chrono :: Duration :: days ( 14 ) , ) . await . unwrap ( ) ; let set_events = lovelace_controller . date_search ( Utc :: now ( ) , Utc :: now ( ) + Duration :: days ( 14 ) ) . await ? ; let mut tasks = conn . run ( move | c | { student_class_asynchronous_task :: table . inner_join ( class_student :: table . inner_join ( users :: table ) ) . inner_join ( class_asynchronous_task :: table ) . filter ( users :: id . eq ( user_id ) ) . filter ( class_asynchronous_task :: due_date . ge ( chrono :: Utc :: now ( ) . naive_utc ( ) ) ) . filter ( class_asynchronous_task :: due_date . le ( ( chrono :: Utc :: now ( ) + Duration :: days ( 14 ) ) . naive_utc ( ) ) , ) . select ( ( class_asynchronous_task :: all_columns , student_class_asynchronous_task :: all_columns , ) ) . load :: < ( ClassAsynchronousTask , StudentClassAsynchronousTask ) > ( c ) } ) . await . unwrap ( ) ; let free_slots = map_user_events_to_free_time ( user_events ) . await ; let mut events_to_add = vec ! [ ] ; for slot in free_slots { if tasks . is_empty ( ) { break ; } fill_slot ( slot , & mut tasks , & mut events_to_add ) ; } for event in set_events { event . delete ( ) . await ? ; } for event in events_to_add { lovelace_controller . save_event ( event ) . await ? ; } Ok ( ( ) ) }
Creates a schedule for the next two weeks.
Creates a schedule for the next two weeks .
user_id
i32
conn
&Database