Skip to content

Commit 44aec13

Browse files
committed
Merge branch 'refactor' into production
2 parents 765ff3e + 9deda14 commit 44aec13

20 files changed

+195
-497
lines changed

docs/attendance.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@ struct Attendance {
1919
```
2020
The final two fields are not exposed in the interface for obvious reasons.
2121

22-
### AttendanceSummary
23-
Monthly attendance summary for each member.
24-
```rust
25-
struct AttendanceSummary {
26-
member_id: i32,
27-
year: i32,
28-
month: i32,
29-
days_attended: i32,
30-
}
31-
```
32-
3322
## Queries
3423

3524
### Get Attendance
@@ -85,19 +74,6 @@ mutation {
8574
}
8675
```
8776

88-
### Get Attendance Summary
89-
Get monthly attendance summary for a member.
90-
91-
```graphql
92-
query {
93-
attendanceSummary(memberId: 1) {
94-
year
95-
month
96-
daysAttended
97-
}
98-
}
99-
```
100-
10177
## Daily Task
10278

10379
The `src/daily_task/daily_task.rs` system automatically updates attendance summaries at midnight.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Add migration script here
2+
DROP TABLE statusupdatestreak;
3+
DROP TABLE project;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Add migration script here
2+
ALTER table statusupdatehistory
3+
RENAME COLUMN is_updated TO is_sent;

src/daily_task/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub async fn run_daily_task_at_midnight(pool: Arc<PgPool>) {
3636

3737
/// This function does a number of things, including:
3838
/// * Insert new attendance records everyday for [`presense`](https://www.github.com/amfoss/presense) to update them later in the day.
39-
/// * Update the AttendanceSummary table
4039
async fn execute_daily_task(pool: Arc<PgPool>) {
4140
// Members is queried outside of each function to avoid repetition
4241
let members = sqlx::query_as::<_, Member>("SELECT * FROM Member")
@@ -90,7 +89,6 @@ async fn update_attendance(members: &Vec<Member>, pool: &PgPool) {
9089
}
9190
}
9291
// This could have been called in `execute_daily_task()` but that would require us to loop through members twice.
93-
// Whether or not inserting attendance failed, Root will attempt to update AttendanceSummary. This can potentially fail too since insertion failed earlier. However, these two do not depend on each other and one of them failing is no reason to avoid trying the other.
9492
}
9593
}
9694

@@ -104,7 +102,7 @@ async fn update_status_history(members: &Vec<Member>, pool: &PgPool) {
104102

105103
for member in members {
106104
let status_update = sqlx::query(
107-
"INSERT INTO StatusUpdateHistory (member_id, date, is_updated)
105+
"INSERT INTO StatusUpdateHistory (member_id, date, is_sent)
108106
VALUES ($1, $2, $3)
109107
ON CONFLICT (member_id, date) DO NOTHING",
110108
)

src/database_seeder/seed.sql

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -58,60 +58,9 @@ WHERE (random() < 0.75)
5858
ON CONFLICT (member_id, date) DO NOTHING;
5959

6060

61-
-- AttendanceSummary
62-
INSERT INTO AttendanceSummary (
63-
member_id, year, month, days_attended
64-
)
65-
SELECT
66-
m.member_id,
67-
2025,
68-
(i % 12) + 1,
69-
FLOOR(random() * 26 + 3)::INT
70-
FROM generate_series(1, 400) AS i
71-
JOIN (
72-
SELECT generate_series(1, 60) AS idx, member_id
73-
FROM member
74-
) AS m ON (i % 60) + 1 = m.idx
75-
ON CONFLICT (member_id, year, month) DO NOTHING;
76-
77-
78-
-- StatusUpdateStreak
79-
INSERT INTO StatusUpdateStreak (
80-
member_id, current_streak, max_streak
81-
)
82-
SELECT
83-
member_id,
84-
FLOOR(random() * 10 + 1)::INT,
85-
FLOOR(random() * 30 + 10)::INT
86-
FROM member
87-
ON CONFLICT (member_id) DO NOTHING;
88-
89-
90-
-- Project
91-
INSERT INTO Project (
92-
member_id, title
93-
)
94-
SELECT
95-
(i % 60) + 1,
96-
CASE
97-
WHEN i % 3 = 0 THEN 'Machine Learning Project ' || i
98-
WHEN i % 3 = 1 THEN 'Web Development Project ' || i
99-
ELSE 'Data Analysis Project ' || i
100-
END
101-
FROM generate_series(1, 200) AS i
102-
WHERE NOT EXISTS (
103-
SELECT 1 FROM Project
104-
WHERE member_id = (i % 60) + 1 AND title = CASE
105-
WHEN i % 3 = 0 THEN 'Machine Learning Project ' || i
106-
WHEN i % 3 = 1 THEN 'Web Development Project ' || i
107-
ELSE 'Data Analysis Project ' || i
108-
END
109-
);
110-
111-
11261
-- StatusUpdateHistory
11362
INSERT INTO StatusUpdateHistory (
114-
member_id, date, is_updated
63+
member_id, date, is_sent
11564
)
11665
SELECT
11766
m.member_id,

src/graphql/mod.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
use async_graphql::MergedObject;
2-
use mutations::{AttendanceMutations, MemberMutations, ProjectMutations, StreakMutations};
3-
use queries::{AttendanceQueries, MemberQueries, ProjectQueries, StreakQueries};
2+
use mutations::{AttendanceMutations, MemberMutations, StatusMutations};
3+
use queries::MemberQueries;
44

55
pub mod mutations;
66
pub mod queries;
77

88
#[derive(MergedObject, Default)]
9-
pub struct Query(
10-
MemberQueries,
11-
AttendanceQueries,
12-
StreakQueries,
13-
ProjectQueries,
14-
);
9+
pub struct Query(MemberQueries);
1510

1611
#[derive(MergedObject, Default)]
17-
pub struct Mutation(
18-
MemberMutations,
19-
AttendanceMutations,
20-
StreakMutations,
21-
ProjectMutations,
22-
);
12+
pub struct Mutation(MemberMutations, AttendanceMutations, StatusMutations);

src/graphql/mutations/attendance_mutations.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use hmac::{Hmac, Mac};
77
use sha2::Sha256;
88
use sqlx::PgPool;
99

10-
use crate::models::attendance::{Attendance, MarkAttendanceInput};
10+
use crate::models::attendance::{AttendanceRecord, MarkAttendanceInput};
1111

1212
type HmacSha256 = Hmac<Sha256>;
1313

@@ -21,7 +21,7 @@ impl AttendanceMutations {
2121
&self,
2222
ctx: &Context<'_>,
2323
input: MarkAttendanceInput,
24-
) -> Result<Attendance> {
24+
) -> Result<AttendanceRecord> {
2525
let pool = ctx
2626
.data::<Arc<PgPool>>()
2727
.expect("Pool not found in context");
@@ -43,7 +43,7 @@ impl AttendanceMutations {
4343
}
4444

4545
let now = Local::now().with_timezone(&Kolkata).time();
46-
let attendance = sqlx::query_as::<_, Attendance>(
46+
let attendance = sqlx::query_as::<_, AttendanceRecord>(
4747
"UPDATE Attendance SET time_in = CASE
4848
WHEN time_in IS NULL THEN $1
4949
ELSE time_in END,

src/graphql/mutations/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
pub mod attendance_mutations;
22
pub mod member_mutations;
3-
pub mod project_mutations;
4-
pub mod streak_mutations;
3+
pub mod status_mutations;
54

65
pub use attendance_mutations::AttendanceMutations;
76
pub use member_mutations::MemberMutations;
8-
pub use project_mutations::ProjectMutations;
9-
pub use streak_mutations::StreakMutations;
7+
pub use status_mutations::StatusMutations;

src/graphql/mutations/project_mutations.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use async_graphql::{Context, Object, Result};
2+
use chrono_tz::Asia::Kolkata;
3+
use sqlx::PgPool;
4+
use std::sync::Arc;
5+
6+
use crate::models::status_update::StatusUpdateRecord;
7+
8+
#[derive(Default)]
9+
pub struct StatusMutations;
10+
11+
#[Object]
12+
impl StatusMutations {
13+
async fn mark_status_update(
14+
&self,
15+
ctx: &Context<'_>,
16+
emails: Vec<String>,
17+
) -> Result<Vec<StatusUpdateRecord>> {
18+
let pool = ctx.data::<Arc<PgPool>>().expect("Pool must be in context");
19+
#[allow(deprecated)]
20+
let yesterday = chrono::Utc::now()
21+
.with_timezone(&Kolkata)
22+
.date()
23+
.naive_local()
24+
- chrono::Duration::days(1);
25+
26+
let status = sqlx::query_as::<_, StatusUpdateRecord>(
27+
"UPDATE StatusUpdateHistory SET
28+
is_sent = true
29+
WHERE member_id IN (SELECT member_id from Member where email = ANY($1))
30+
AND date = $2
31+
RETURNING *
32+
",
33+
)
34+
.bind(emails)
35+
.bind(yesterday)
36+
.fetch_all(pool.as_ref())
37+
.await?;
38+
39+
Ok(status)
40+
}
41+
}

0 commit comments

Comments
 (0)