Skip to content

Commit f7b758b

Browse files
feat: add query for leave count
1 parent f09c779 commit f7b758b

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

src/graphql/mutations/attendance_mutations.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use sha2::Sha256;
77
use sqlx::PgPool;
88

99
use crate::auth::guards::AdminOrBotGuard;
10-
use crate::models::attendance::{AttendanceRecord, MarkAttendanceInput, MarkLeaveInput, MarkLeaveOutput};
10+
use crate::models::attendance::{
11+
AttendanceRecord, MarkAttendanceInput, MarkLeaveInput, MarkLeaveOutput,
12+
};
1113

1214
type HmacSha256 = Hmac<Sha256>;
1315

@@ -71,7 +73,7 @@ impl AttendanceMutations {
7173
.data::<Arc<PgPool>>()
7274
.expect("Pool not found in context");
7375
let now = chrono::Utc::now().with_timezone(&Kolkata);
74-
76+
7577
let leave: MarkLeaveOutput = sqlx::query_as::<_, MarkLeaveOutput>(
7678
"INSERT INTO Leave
7779
(discord_id, date, duration, reason, approved_by)

src/graphql/queries/member_queries.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::auth::guards::AuthGuard;
22
use crate::auth::AuthContext;
3-
use crate::models::{attendance::AttendanceRecord, status_update::StatusUpdateRecord};
3+
use crate::models::{
4+
attendance::{AttendanceRecord, LeaveCountOutput},
5+
status_update::StatusUpdateRecord,
6+
};
47
use async_graphql::{ComplexObject, Context, Object, Result};
58
use chrono::NaiveDate;
69
use sqlx::PgPool;
@@ -397,4 +400,37 @@ impl Member {
397400
member_id: self.member_id,
398401
}
399402
}
403+
404+
async fn leave_count(
405+
&self,
406+
ctx: &Context<'_>,
407+
start_date: NaiveDate,
408+
end_date: NaiveDate,
409+
) -> Result<LeaveCountOutput> {
410+
let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context.");
411+
412+
if end_date < start_date {
413+
return Err("end_date must be >= start_date".into());
414+
}
415+
let leave = sqlx::query_as::<_, LeaveCountOutput>(
416+
r#"
417+
SELECT discord_id, SUM(duration) AS count
418+
FROM "Leave"
419+
WHERE date > $1
420+
AND (date + duration) < $2
421+
AND discord_id = $3
422+
GROUP BY discord_id
423+
"#,
424+
)
425+
.bind(start_date)
426+
.bind(end_date)
427+
.bind(
428+
self.discord_id
429+
.as_ref()
430+
.expect("Leave count needs discord_id"),
431+
)
432+
.fetch_one(pool.as_ref())
433+
.await?;
434+
Ok(leave)
435+
}
400436
}

src/models/attendance.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,10 @@ pub struct MarkLeaveOutput {
3737
pub reason: String,
3838
pub duration: i32,
3939
pub approved_by: Option<String>,
40-
}
40+
}
41+
42+
#[derive(SimpleObject, FromRow)]
43+
pub struct LeaveCountOutput {
44+
pub discord_id: String,
45+
pub count: i32,
46+
}

0 commit comments

Comments
 (0)