Skip to content

Commit

Permalink
removed the attendance fetch by optional date, use ? insted of match …
Browse files Browse the repository at this point in the history
…for error checking

added .idea to gitignore

removed the optinal fields and added comments on what struct does what
  • Loading branch information
sabarixr committed Feb 27, 2025
1 parent 8eec0d2 commit c1f3d8d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Secrets*.toml
backups/
.env
*.log
.idea/
70 changes: 19 additions & 51 deletions src/graphql/queries/attendance_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,10 @@ impl AttendanceQueries {
member_id: Option<i32>,
roll_no: Option<String>,
discord_id: Option<String>,
date: Option<String>,
) -> Result<Vec<Attendance>> {
let pool = ctx.data::<Arc<PgPool>>().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")
Expand All @@ -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)
Expand Down Expand Up @@ -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.",
Expand All @@ -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#"
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/models/attendance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ pub struct Attendance {
pub time_in: Option<NaiveTime>,
pub time_out: Option<NaiveTime>,

//optional for attendance report
pub name: Option<String>,
pub year: Option<i32>,

#[graphql(skip)] // Don't expose internal fields/meta-data
pub created_at: NaiveDateTime,
#[graphql(skip)]
Expand Down Expand Up @@ -46,27 +42,30 @@ 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,
pub date: NaiveDate,
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,
pub name: String,
pub present_days: i64,
}

// This struct is used for getting the combined Attendance report
#[derive(SimpleObject)]
pub struct AttendanceReport {
pub daily_count: Vec<DailyCount>,
Expand Down

0 comments on commit c1f3d8d

Please sign in to comment.