Skip to content

Commit d52ac93

Browse files
committed
feat: Add date field to attendance query
Fixes #72
1 parent 6588045 commit d52ac93

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

docs/attendance.md

+32
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,38 @@ struct AttendanceSummary {
3232

3333
## Queries
3434

35+
### Get Attendance
36+
Retrieve attendance records by member ID or date.
37+
38+
```graphql
39+
# Get attendance by member ID
40+
query {
41+
attendance(memberId: 1) {
42+
attendanceId
43+
date
44+
isPresent
45+
timeIn
46+
timeOut
47+
}
48+
}
49+
```
50+
51+
Get all attendance for a specific date
52+
53+
```graphql
54+
query {
55+
attendanceByDate(date: "2025-02-27") {
56+
attendanceId
57+
memberId
58+
name
59+
year
60+
isPresent
61+
timeIn
62+
timeOut
63+
}
64+
}
65+
```
66+
3567
### Mark Attendance
3668
Record a member's attendance for the day.
3769

src/graphql/queries/attendance_queries.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
use std::sync::Arc;
22

3+
use crate::models::{
4+
attendance::{Attendance, AttendanceWithMember},
5+
member::Member,
6+
};
37
use async_graphql::{Context, Object, Result};
8+
use chrono::NaiveDate;
49
use sqlx::PgPool;
510

6-
use crate::models::{attendance::Attendance, member::Member};
7-
811
/// Sub-query for the [`Attendance`] table. The queries are:
9-
/// * attendance - get a specific member's attendance details using their member_id, roll_no or discord_id
12+
/// * attendance - get a specific member's attendance details using their member_id, roll_no or discord_id, or by date for all members.
1013
#[derive(Default)]
1114
pub struct AttendanceQueries;
1215

@@ -66,4 +69,26 @@ impl AttendanceQueries {
6669

6770
Ok(attendance_query)
6871
}
72+
73+
// Query to get attendance by date
74+
async fn attendance_by_date(
75+
&self,
76+
ctx: &Context<'_>,
77+
date: NaiveDate,
78+
) -> Result<Vec<AttendanceWithMember>> {
79+
let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");
80+
81+
let records = sqlx::query_as::<_, AttendanceWithMember>(
82+
"SELECT a.attendance_id, a.member_id, a.date, a.is_present,
83+
a.time_in, a.time_out, m.name, m.year
84+
FROM Attendance a
85+
JOIN Member m ON a.member_id = m.member_id
86+
WHERE a.date = $1",
87+
)
88+
.bind(date)
89+
.fetch_all(pool.as_ref())
90+
.await?;
91+
92+
Ok(records)
93+
}
6994
}

src/models/attendance.rs

+14
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,17 @@ pub struct MarkAttendanceInput {
4848
pub date: NaiveDate,
4949
pub hmac_signature: String,
5050
}
51+
52+
/// This struct combines attendance data with member name for queries that need both.
53+
/// It joins the Attendance table with Member to include the member's name.
54+
#[derive(SimpleObject, FromRow)]
55+
pub struct AttendanceWithMember {
56+
pub attendance_id: i32,
57+
pub member_id: i32,
58+
pub date: NaiveDate,
59+
pub is_present: bool,
60+
pub time_in: Option<NaiveTime>,
61+
pub time_out: Option<NaiveTime>,
62+
pub name: String,
63+
pub year: i32,
64+
}

0 commit comments

Comments
 (0)