Skip to content

Commit d9d2ffe

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

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/graphql/queries/member_queries.rs

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

src/models/attendance.rs

Lines changed: 6 additions & 0 deletions
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+
}
41+
42+
#[derive(SimpleObject, FromRow)]
43+
pub struct LeaveCountOutput {
44+
pub discord_id: String,
45+
pub count: i32
4046
}

0 commit comments

Comments
 (0)