Skip to content

Commit 3e84d94

Browse files
authored
Merge pull request #1 from jlefkoff/j/fix-sort
Fix sorting of ratings and training notes.
2 parents 8d285bc + bfce174 commit 3e84d94

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

Diff for: vzdv-site/src/endpoints/controller.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use axum::{
1313
routing::{delete, get, post},
1414
Form, Router,
1515
};
16-
use chrono::{DateTime, Utc};
16+
use chrono::{DateTime, NaiveDateTime, Utc};
1717
use itertools::Itertools;
1818
use log::{error, info, warn};
1919
use minijinja::{context, Environment};
@@ -416,7 +416,7 @@ async fn snippet_get_training_records(
416416
let all_training_records = get_training_records(&state.config.vatsim.vatusa_api_key, cid)
417417
.await
418418
.map_err(|e| AppError::GenericFallback("getting VATUSA training records", e))?;
419-
let training_records: Vec<_> = all_training_records
419+
let mut training_records: Vec<_> = all_training_records
420420
.iter()
421421
.filter(|record| record.facility_id == "ZDV")
422422
.map(|record| {
@@ -427,6 +427,14 @@ async fn snippet_get_training_records(
427427
}
428428
})
429429
.collect();
430+
431+
// Sort by session_date in descending order (newest first)
432+
training_records.sort_by(|a, b| {
433+
let date_a = NaiveDateTime::parse_from_str(&a.session_date, "%Y-%m-%d %H:%M:%S").unwrap_or_else(|_| NaiveDateTime::from_timestamp(0, 0));
434+
let date_b = NaiveDateTime::parse_from_str(&b.session_date, "%Y-%m-%d %H:%M:%S").unwrap_or_else(|_| NaiveDateTime::from_timestamp(0, 0));
435+
date_b.cmp(&date_a) // Sort newest first
436+
});
437+
430438
let instructor_cids: Vec<u32> = training_records
431439
.iter()
432440
.map(|record| record.instructor_id)

Diff for: vzdv-site/src/endpoints/facility.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ async fn page_roster(
157157
.fetch_all(&state.db)
158158
.await?;
159159

160+
let certification_order = &state.config.training.certifications;
161+
let cert_order_map: HashMap<&String, usize> = certification_order
162+
.into_iter()
163+
.enumerate()
164+
.map(|(index, cert)| (cert, index))
165+
.collect();
166+
160167
let controllers_with_certs: Vec<_> = controllers
161168
.iter()
162169
.map(|controller| {
@@ -165,12 +172,15 @@ async fn page_roster(
165172
None => "",
166173
};
167174
let roles = determine_staff_positions(controller).join(", ");
168-
let certs = certifications
175+
let mut certs = certifications
169176
.iter()
170177
.filter(|cert| cert.cid == controller.cid)
171178
.cloned()
172179
.collect::<Vec<_>>();
173180

181+
// Sort certifications based on the order in the TOML file
182+
certs.sort_by_key(|cert| cert_order_map.get(&cert.name).cloned().unwrap_or(usize::MAX));
183+
174184
ControllerWithCerts {
175185
cid: controller.cid,
176186
first_name: &controller.first_name,

0 commit comments

Comments
 (0)