Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/graphql/queries/member_queries.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::models::{attendance::AttendanceRecord, status_update::StatusUpdateRecord};
use crate::models::{
attendance::Aggregate, attendance::AttendanceRecord, status_update::StatusUpdateRecord,
};
use async_graphql::{ComplexObject, Context, Object, Result};
use chrono::NaiveDate;
use sqlx::PgPool;
Expand All @@ -17,8 +19,13 @@ pub struct AttendanceInfo {
member_id: i32,
}

pub struct Lab;

#[Object]
impl MemberQueries {
async fn lab(&self, _ctx: &Context<'_>) -> Lab {
Lab
}
pub async fn all_members(
&self,
ctx: &Context<'_>,
Expand Down Expand Up @@ -336,3 +343,49 @@ impl Member {
}
}
}

#[Object]

impl Lab {
pub async fn attendance(
&self,
ctx: &Context<'_>,
start_date: NaiveDate,
end_date: NaiveDate,
) -> Result<Vec<Aggregate>> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");

let results: Vec<Aggregate> = sqlx::query_as::<_, Aggregate>(
"
SELECT
all_dates.date,
p.present_count
FROM (
SELECT
date,
COUNT(*) AS total_count
FROM attendance
WHERE date BETWEEN $1 AND $2
GROUP BY date
) AS all_dates
Comment on lines +363 to +370
Copy link
Member

@hrideshmg hrideshmg Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This query is for the attendance summary widget on the Home dashboard which does not need total_count, moreover, total_count isn't even used in the rest of the query. I'm not sure why you need a join here.

LEFT JOIN (
SELECT
date,
COUNT(*) AS present_count
FROM attendance
WHERE is_present = TRUE
AND date BETWEEN $1 AND $2
GROUP BY date
) AS p
ON all_dates.date = p.date
ORDER BY all_dates.date;
",
)
.bind(start_date)
.bind(end_date)
.fetch_all(pool.as_ref())
.await?;

Ok(results)
}
}
6 changes: 6 additions & 0 deletions src/models/attendance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ pub struct MarkAttendanceInput {
pub date: NaiveDate,
pub hmac_signature: String,
}

#[derive(FromRow, SimpleObject)]
pub struct Aggregate {
pub date: NaiveDate,
pub present_count: Option<i64>,
}
Loading