Skip to content

Commit 226f68e

Browse files
authored
attendance response restructured to be an array (#45)
this PR changes structure of the /attendance response to be an array of objects. This way it is easier to use airbyte for loading data into db Co-authored-by: AnnaFYZ <[email protected]>
1 parent 9ae50ef commit 226f68e

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

src/endpoints.rs

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::BTreeMap;
2-
31
use ::octocrab::models::{Author, teams::RequestedTeam};
42
use anyhow::Context;
53
use axum::{
@@ -218,22 +216,21 @@ pub async fn get_region(
218216
}))
219217
}
220218

221-
type SprintAttendance = BTreeMap<String, Vec<Attendance>>;
222-
type ModuleAttendance = BTreeMap<String, SprintAttendance>;
223-
type BatchAttendance = BTreeMap<String, ModuleAttendance>;
224-
type CourseAttendance = BTreeMap<String, BatchAttendance>;
225-
226219
#[derive(Serialize)]
227220
pub struct AttendanceResponse {
228-
courses: CourseAttendance,
221+
#[serde(flatten)]
222+
attendance: Attendance,
223+
sprint: String,
224+
module: String,
225+
batch: String,
229226
}
230227

231228
pub async fn fetch_attendance(
232229
session: Session,
233230
headers: HeaderMap,
234231
State(server_state): State<ServerState>,
235232
OriginalUri(original_uri): OriginalUri,
236-
) -> Result<Json<AttendanceResponse>, Error> {
233+
) -> Result<Json<Vec<AttendanceResponse>>, Error> {
237234
let all_courses = &server_state.config.courses;
238235
let sheets_client = sheets_client(
239236
&session,
@@ -243,7 +240,6 @@ pub async fn fetch_attendance(
243240
)
244241
.await?;
245242

246-
let mut courses: CourseAttendance = BTreeMap::new();
247243
let mut register_futures = Vec::new();
248244
for (course_name, course_info) in all_courses {
249245
for batch_name in course_info.batches.keys() {
@@ -268,32 +264,23 @@ pub async fn fetch_attendance(
268264
}
269265
let register_info = join_all(register_futures).await;
270266

271-
for (course_name, batch_name, register_result) in register_info {
267+
let mut registered_attendance = Vec::new();
268+
269+
for (_course_name, batch_name, register_result) in register_info {
272270
let register = register_result?;
273-
let modules = register
274-
.modules
275-
.into_iter()
276-
.map(|(module_name, sprint_info)| {
277-
(
278-
module_name,
279-
sprint_info
280-
.attendance
281-
.into_iter()
282-
.enumerate()
283-
.map(|(sprint_number, sprint_info)| {
284-
(
285-
format!("Sprint-{}", sprint_number + 1),
286-
sprint_info.into_values().collect(),
287-
)
288-
})
289-
.collect(),
290-
)
291-
})
292-
.collect();
293-
courses
294-
.entry(course_name)
295-
.or_default()
296-
.insert(batch_name, modules);
271+
for (module_name, sprint_info) in register.modules {
272+
for (sprint_number, attendance_info) in sprint_info.attendance.iter().enumerate() {
273+
let sprint_name = format!("Sprint-{}", sprint_number + 1);
274+
for attendance in attendance_info.values() {
275+
registered_attendance.push(AttendanceResponse {
276+
attendance: attendance.clone(),
277+
sprint: sprint_name.clone(),
278+
module: module_name.clone(),
279+
batch: batch_name.clone(),
280+
});
281+
}
282+
}
283+
}
297284
}
298-
Ok(Json(AttendanceResponse { courses }))
285+
Ok(Json(registered_attendance))
299286
}

0 commit comments

Comments
 (0)