-
Notifications
You must be signed in to change notification settings - Fork 22
Leave tracking from amD #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 3 commits
5148574
f09c779
f7b758b
013cdef
ad8c10d
6cd566b
0746618
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| -- Leave table for tracking leaves | ||
| CREATE TABLE Leave ( | ||
| leave_id SERIAL PRIMARY KEY, | ||
| discord_id VARCHAR(255) REFERENCES Member(discord_id) ON DELETE CASCADE, | ||
| date DATE DEFAULT CURRENT_DATE, | ||
| duration INT DEFAULt 1, | ||
|
naveensrinivas282 marked this conversation as resolved.
Outdated
|
||
| reason TEXT NOT NULL, | ||
| approved_by VARCHAR(255) REFERENCES Member(discord_id), | ||
|
naveensrinivas282 marked this conversation as resolved.
Outdated
|
||
| CHECK (approved_by IS NULL OR approved_by <> discord_id), | ||
|
naveensrinivas282 marked this conversation as resolved.
|
||
| UNIQUE (date, discord_id) | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| use crate::auth::guards::AuthGuard; | ||
| use crate::auth::AuthContext; | ||
| use crate::models::{attendance::AttendanceRecord, status_update::StatusUpdateRecord}; | ||
| use crate::models::{ | ||
| attendance::{AttendanceRecord, LeaveCountOutput}, | ||
| status_update::StatusUpdateRecord, | ||
| }; | ||
| use async_graphql::{ComplexObject, Context, Object, Result}; | ||
| use chrono::NaiveDate; | ||
| use sqlx::PgPool; | ||
|
|
@@ -57,31 +60,39 @@ impl MemberQueries { | |
| ctx: &Context<'_>, | ||
| member_id: Option<i32>, | ||
| email: Option<String>, | ||
| discord_id: Option<String>, | ||
| ) -> Result<Option<Member>> { | ||
| let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context."); | ||
|
|
||
| match (member_id, email) { | ||
| (Some(id), None) => { | ||
| match (member_id, email, discord_id) { | ||
| (Some(id), None, None) => { | ||
| let member = | ||
| sqlx::query_as::<_, Member>("SELECT * FROM Member WHERE member_id = $1") | ||
| .bind(id) | ||
| .fetch_optional(pool.as_ref()) | ||
| .await?; | ||
| Ok(member) | ||
| } | ||
| (None, Some(email)) => { | ||
| (None, Some(email), None) => { | ||
| let member = sqlx::query_as::<_, Member>("SELECT * FROM Member WHERE email = $1") | ||
| .bind(email) | ||
| .fetch_optional(pool.as_ref()) | ||
| .await?; | ||
| Ok(member) | ||
| } | ||
| (Some(_), Some(_)) => Err("Provide only one of member_id or email".into()), | ||
| (None, None) => Err("Provide either member_id or email".into()), | ||
| (None, None, Some(discord_id)) => { | ||
| let member = | ||
| sqlx::query_as::<_, Member>("SELECT * FROM Member WHERE discord_id = $1") | ||
| .bind(discord_id) | ||
| .fetch_optional(pool.as_ref()) | ||
| .await?; | ||
| Ok(member) | ||
| } | ||
| _ => Err("Provide exactly one of member_id, email, or discord_id".into()), | ||
| } | ||
| } | ||
|
|
||
| /// Fetch the details of the currently logged in member | ||
| // Fetch the details of the currently logged in member | ||
|
naveensrinivas282 marked this conversation as resolved.
Outdated
naveensrinivas282 marked this conversation as resolved.
Outdated
|
||
| #[graphql(guard = "AuthGuard")] | ||
| async fn me(&self, ctx: &Context<'_>) -> Result<Member> { | ||
| let auth = ctx.data::<AuthContext>()?; | ||
|
|
@@ -389,4 +400,37 @@ impl Member { | |
| member_id: self.member_id, | ||
| } | ||
| } | ||
|
|
||
| async fn leave_count( | ||
| &self, | ||
| ctx: &Context<'_>, | ||
| start_date: NaiveDate, | ||
| end_date: NaiveDate, | ||
| ) -> Result<LeaveCountOutput> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
See the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still unresolved, i don't see a nested object or the records query
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context."); | ||
|
naveensrinivas282 marked this conversation as resolved.
|
||
|
|
||
| if end_date < start_date { | ||
| return Err("end_date must be >= start_date".into()); | ||
| } | ||
| let leave = sqlx::query_as::<_, LeaveCountOutput>( | ||
| r#" | ||
| SELECT discord_id, SUM(duration) AS count | ||
| FROM "Leave" | ||
|
hrideshmg marked this conversation as resolved.
Outdated
|
||
| WHERE date > $1 | ||
| AND (date + duration) < $2 | ||
|
naveensrinivas282 marked this conversation as resolved.
Outdated
|
||
| AND discord_id = $3 | ||
| GROUP BY discord_id | ||
| "#, | ||
| ) | ||
| .bind(start_date) | ||
| .bind(end_date) | ||
| .bind( | ||
| self.discord_id | ||
| .as_ref() | ||
| .expect("Leave count needs discord_id"), | ||
| ) | ||
|
naveensrinivas282 marked this conversation as resolved.
Outdated
|
||
| .fetch_one(pool.as_ref()) | ||
| .await?; | ||
|
naveensrinivas282 marked this conversation as resolved.
Outdated
|
||
| Ok(leave) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.