-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtally.rs
More file actions
75 lines (61 loc) · 1.87 KB
/
Copy pathtally.rs
File metadata and controls
75 lines (61 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use async_trait::async_trait;
use axum::{
Json,
extract::{FromRequest, State},
http::StatusCode,
};
use tracing::{error, info};
use rustsystem_core::{APIError, APIHandler, Method};
use crate::{AppState, tally_encrypt::save_encrypted_tally, vote_auth};
use super::auth::AuthHost;
#[derive(FromRequest)]
pub struct TallyRequest {
auth: AuthHost,
state: State<AppState>,
}
pub struct Tally;
#[async_trait]
impl APIHandler for Tally {
type State = AppState;
type Request = TallyRequest;
type SuccessResponse = Json<vote_auth::Tally>;
const METHOD: Method = Method::Post;
const PATH: &'static str = "/tally";
const SUCCESS_CODE: StatusCode = StatusCode::OK;
async fn route(request: Self::Request) -> Result<Self::SuccessResponse, APIError> {
let TallyRequest {
auth,
state: State(state),
} = request;
let meeting = state.get_meeting(auth.muuid).await?;
let round_name = meeting
.vote_auth
.read()
.await
.get_current_vote_name()
.cloned()
.unwrap_or_default();
let tally_result = meeting.vote_auth.write().await.finalize_round()?;
// vote_auth read guard released; now safe to read voters independently.
let voter_names: Vec<String> = meeting
.voters
.read()
.await
.values()
.map(|v| v.name.clone())
.collect();
if let Err(e) = save_encrypted_tally(&auth.muuid, &tally_result, voter_names) {
error!(
muuid = %auth.muuid,
round = %round_name,
"Failed to save encrypted tally: {e}"
);
}
info!(
muuid = %auth.muuid,
round = %round_name,
"Vote round tallied"
);
Ok(Json(tally_result))
}
}