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
68 changes: 67 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,62 @@ 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.");

// will give the total count
let mut query = sqlx::QueryBuilder::new(
r#"
SELECT
all_dates.date,
p.present_count
FROM (
SELECT
date,
COUNT(*) AS total_count
FROM attendance
WHERE date BETWEEN "#,
);

query.push_bind(start_date);
query.push(" AND ");
query.push_bind(end_date);
query.push(" GROUP BY date ) AS all_dates ");

// sub query of present members joined with the above total countS
query.push(
"LEFT JOIN (
SELECT
date,
COUNT(*) AS present_count
FROM attendance
WHERE is_present = 't'
AND date BETWEEN ",
);
query.push_bind(start_date);
query.push(" AND ");
query.push_bind(end_date);
query.push(
" GROUP BY date
) AS p
ON all_dates.date = p.date
ORDER BY all_dates.date;",
Comment on lines +360 to +394
Copy link
Member

@hrideshmg hrideshmg Oct 14, 2025

Choose a reason for hiding this comment

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

Please use the $1, $2 with .bind() syntax to format your SQL. Refer to the other queries in member_queries for examples. The way its formatted right now with multiple push and push_bind's makes it very hard to read.

Will review the SQL logic after its formatted properly.

);

let results: Vec<Aggregate> = query
.build_query_as::<Aggregate>()
.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>,
}