diff --git a/.gitignore b/.gitignore index c12db7a..26e5764 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ Secrets*.toml backups/ .env *.log +.idea/ \ No newline at end of file diff --git a/src/graphql/queries/attendance_queries.rs b/src/graphql/queries/attendance_queries.rs index 59fbb6e..6b9533d 100644 --- a/src/graphql/queries/attendance_queries.rs +++ b/src/graphql/queries/attendance_queries.rs @@ -20,28 +20,10 @@ impl AttendanceQueries { member_id: Option, roll_no: Option, discord_id: Option, - date: Option, ) -> Result> { let pool = ctx.data::>().expect("Pool must be in context."); - if let Some(d) = &date { - let query = " - SELECT - A.*, - M.name, - M.year - FROM Attendance A - JOIN Member M ON A.member_id = M.member_id - WHERE A.date = $1::DATE"; - - let attendance_query = sqlx::query_as::<_, Attendance>(query) - .bind(d) - .fetch_all(pool.as_ref()) - .await?; - - return Ok(attendance_query); - } - + // member_id is given, simple query if let Some(id) = member_id { let attendance_query = sqlx::query_as::<_, Attendance>("SELECT * FROM Attendance WHERE member_id = $1") @@ -52,6 +34,7 @@ impl AttendanceQueries { return Ok(attendance_query); } + // Get the member using their roll_no or discord_id let member_query = if let Some(roll) = roll_no { sqlx::query_as::<_, Member>("SELECT * FROM Member WHERE roll_no = $1") .bind(roll) @@ -98,7 +81,6 @@ impl AttendanceQueries { .map_err(|_| async_graphql::Error::new("Invalid start_date format. Use YYYY-MM-DD"))?; let end = NaiveDate::parse_from_str(&end_date, "%Y-%m-%d") .map_err(|_| async_graphql::Error::new("Invalid end_date format. Use YYYY-MM-DD"))?; - print!("{}", start); if start > end { return Err(async_graphql::Error::new( "startDate cannot be greater than endDate.", @@ -121,21 +103,15 @@ impl AttendanceQueries { .fetch_all(pool.as_ref()) .await; - let daily_count = match daily_count_result { - Ok(rows) => rows - .into_iter() - .map(|row| DailyCount { - date: row.date.to_string(), - count: row.total_present.unwrap_or(0), - }) - .collect(), - Err(e) => { - return Err(async_graphql::Error::new(format!( - "Failed to fetch daily attendance: {}", - e - ))) - } - }; + let daily_count_rows = daily_count_result?; + + let daily_count = daily_count_rows + .into_iter() + .map(|row| DailyCount { + date: row.date.to_string(), + count: row.total_present.unwrap_or(0), + }) + .collect(); let member_attendance_query = sqlx::query!( r#" @@ -152,22 +128,14 @@ impl AttendanceQueries { .fetch_all(pool.as_ref()) .await; - let member_attendance = match member_attendance_query { - Ok(rows) => rows - .into_iter() - .map(|row| MemberAttendanceSummary { - id: row.id, - name: row.name, - present_days: row.present_days as i64, - }) - .collect(), - Err(e) => { - return Err(async_graphql::Error::new(format!( - "Failed to fetch member attendance summary: {}", - e - ))) - } - }; + let member_attendance = member_attendance_query? + .into_iter() + .map(|row| MemberAttendanceSummary { + id: row.id, + name: row.name, + present_days: row.present_days as i64, + }) + .collect(); let max_days = match sqlx::query_scalar::<_, i64>( "SELECT COUNT(DISTINCT date) FROM Attendance diff --git a/src/models/attendance.rs b/src/models/attendance.rs index 394fcd5..82ba74b 100644 --- a/src/models/attendance.rs +++ b/src/models/attendance.rs @@ -11,10 +11,6 @@ pub struct Attendance { pub time_in: Option, pub time_out: Option, - //optional for attendance report - pub name: Option, - pub year: Option, - #[graphql(skip)] // Don't expose internal fields/meta-data pub created_at: NaiveDateTime, #[graphql(skip)] @@ -46,7 +42,7 @@ pub struct AttendanceSummaryInfo { pub days_attended: i32, } -/// This struct is used to deserialize the input recieved for mutations on attendance. +/// This struct is used to deserialize the input received for mutations on attendance. #[derive(InputObject)] pub struct MarkAttendanceInput { pub member_id: i32, @@ -54,12 +50,14 @@ pub struct MarkAttendanceInput { pub hmac_signature: String, } +// This struct is used to get the Lab count of a date #[derive(SimpleObject)] pub struct DailyCount { pub date: String, pub count: i64, } +// This struct is used to fetch the attended lab of each member #[derive(SimpleObject)] pub struct MemberAttendanceSummary { pub id: i32, @@ -67,6 +65,7 @@ pub struct MemberAttendanceSummary { pub present_days: i64, } +// This struct is used for getting the combined Attendance report #[derive(SimpleObject)] pub struct AttendanceReport { pub daily_count: Vec,