-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapplication.rs
More file actions
387 lines (365 loc) · 14.7 KB
/
application.rs
File metadata and controls
387 lines (365 loc) · 14.7 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! Application handler for the Chaos application.
//!
//! This module provides HTTP request handlers for managing applications, including:
//! - Creating and retrieving applications
//! - Updating application status and roles
//! - Submitting applications
//! - Managing application ratings
use crate::models::app::{AppMessage, AppState};
use crate::models::application::{Application, ApplicationRoleUpdate, ApplicationStatus, OpenApplicationByApplicationId};
use crate::models::auth::{ApplicationAdmin, ApplicationOwner, ApplicationOwnerOrReviewer, ApplicationReviewerGivenApplicationId, AuthUser, CampaignAdmin, CampaignOrgMember};
use crate::models::error::ChaosError;
use crate::models::transaction::DBTransaction;
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use serde_json::json;
use crate::models::rating::{NewRating, Rating};
/// Handler for application-related HTTP requests.
pub struct ApplicationHandler;
impl ApplicationHandler {
/// Creates a new application if it doesn't exist, otherwise returns the existing application ID.
///
/// # Arguments
///
/// * `campaign_id` - ID of the campaign to apply to
/// * `user_id` - ID of the user submitting the application
/// * `snowflake_generator` - Generator for creating unique IDs
/// * `transaction` - Database transaction to use
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Application details or error
pub async fn create_or_get(
Path(campaign_id): Path<i64>,
user: AuthUser,
State(mut state): State<AppState>,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let application_id = Application::create_or_get(campaign_id, user.user_id, &mut state.snowflake_generator, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(Json(json!({ "application_id": application_id.to_string() })))
}
/// Checks if an application exists for a given campaign and user.
///
/// # Arguments
///
/// * `campaign_id` - ID of the campaign to check
/// * `user` - The authenticated user
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - True if application exists, false otherwise
pub async fn check_application_exists(
Path(campaign_id): Path<i64>,
user: AuthUser,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let application_exists = Application::check_application_exists(campaign_id, user.user_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(Json(json!({ "application_exists": application_exists })))
}
/// Retrieves the details of a specific application.
///
/// This handler allows application admins to view application details.
///
/// # Arguments
///
/// * `application_id` - The ID of the application to retrieve
/// * `_admin` - The authenticated user (must be an application admin)
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Application details or error
pub async fn get(
Path(application_id): Path<i64>,
admin: ApplicationAdmin,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let application = Application::get(application_id, admin.user_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(application)))
}
/// Retrieves the details of a specific application regardless of submission status.
///
/// This handler allows regular applicants to view application details in the answer screen.
///
/// # Arguments
///
/// * `application_id` - The ID of the application to retrieve
/// * `_admin` - The authenticated user (must be an application admin)
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Application details or error
pub async fn get_in_progress(
Path(application_id): Path<i64>,
user: AuthUser,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let application = Application::get_in_progress(application_id, user.user_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(Json(application))
}
/// Updates the status of an application.
///
/// This handler allows application admins to update the application's status.
///
/// # Arguments
///
/// * `state` - The application state
/// * `application_id` - The ID of the application to update
/// * `_admin` - The authenticated user (must be an application admin)
/// * `data` - The new application status
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn set_status(
Path(application_id): Path<i64>,
_admin: ApplicationAdmin,
mut transaction: DBTransaction<'_>,
Json(data): Json<ApplicationStatus>,
) -> Result<impl IntoResponse, ChaosError> {
Application::set_status(application_id, data, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Status successfully updated"))
}
/// Updates the private status of an application.
///
/// This handler allows application admins to update the application's private status.
///
/// # Arguments
///
/// * `state` - The application state
/// * `application_id` - The ID of the application to update
/// * `_admin` - The authenticated user (must be an application admin)
/// * `data` - The new private status
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn set_private_status(
Path(application_id): Path<i64>,
_admin: ApplicationAdmin,
mut transaction: DBTransaction<'_>,
Json(data): Json<ApplicationStatus>,
) -> Result<impl IntoResponse, ChaosError> {
Application::set_private_status(application_id, data, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Private Status successfully updated"))
}
/// Retrieves all applications for the current user.
///
/// This handler returns all applications created by the authenticated user.
///
/// # Arguments
///
/// * `user` - The authenticated user
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - List of applications or error
pub async fn get_from_curr_user(
user: AuthUser,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let applications = Application::get_from_user_id(user.user_id, user.user_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(Json(applications))
}
/// Retrieves all roles associated with a specific application.
///
/// This handler allows application owners to view all roles they have applied for
/// in a specific application, including their preference rankings.
///
/// # Arguments
///
/// * `_user` - The authenticated user (must be the application owner)
/// * `application_id` - The ID of the application to retrieve roles for
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - List of application roles with preferences or error
pub async fn get_roles(
_user: ApplicationOwnerOrReviewer,
Path(application_id): Path<i64>,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let roles = Application::get_roles(application_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(Json(roles))
}
/// Updates the roles associated with an application.
///
/// This handler allows application owners to update the roles they're applying for.
///
/// # Arguments
///
/// * `_user` - The authenticated user (must be the application owner)
/// * `application_id` - The ID of the application to update
/// * `transaction` - Database transaction
/// * `data` - The new role assignments
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn update_roles(
_user: ApplicationOwner,
_: OpenApplicationByApplicationId,
Path(application_id): Path<i64>,
mut transaction: DBTransaction<'_>,
Json(data): Json<ApplicationRoleUpdate>,
) -> Result<impl IntoResponse, ChaosError> {
Application::update_roles(application_id, data.roles, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully updated application roles"))
}
/// Submits an application for review.
///
/// This handler allows application owners to submit their application.
/// The application must be open and not already submitted.
///
/// # Arguments
///
/// * `_user` - The authenticated user (must be the application owner)
/// * `_` - Ensures the application is open
/// * `application_id` - The ID of the application to submit
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn submit(
_user: ApplicationOwner,
_: OpenApplicationByApplicationId,
Path(application_id): Path<i64>,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
Application::submit(application_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully submitted application"))
}
/// Retrieves the rating for an application given by the current user.
///
/// This handler allows application reviewers to view the rating for an application.
///
/// # Arguments
///
/// * `state` - The application state
/// * `application_id` - The ID of the application to get the rating for
/// * `admin` - The authenticated user (must be an application reviewer)
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Rating or error
pub async fn get_rating_by_current_user(
Path(application_id): Path<i64>,
admin: ApplicationReviewerGivenApplicationId,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let rating = Rating::get_rating_by_rater_id(application_id, admin.user_id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(rating)))
}
/// Creates a new rating for an application.
///
/// This handler allows application reviewers to create ratings.
///
/// # Arguments
///
/// * `state` - The application state
/// * `application_id` - The ID of the application to rate
/// * `admin` - The authenticated user (must be an application reviewer)
/// * `transaction` - Database transaction
/// * `new_rating` - The rating details
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - Success message or error
pub async fn create_rating(
State(mut state): State<AppState>,
Path(application_id): Path<i64>,
admin: ApplicationReviewerGivenApplicationId,
mut transaction: DBTransaction<'_>,
Json(new_rating): Json<NewRating>,
) -> Result<impl IntoResponse, ChaosError> {
Rating::create(
new_rating,
application_id,
admin.user_id,
&mut state.snowflake_generator,
&mut transaction.tx,
)
.await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully created rating"))
}
pub async fn update_rating(
Path(application_id): Path<i64>,
admin: ApplicationReviewerGivenApplicationId,
mut transaction: DBTransaction<'_>,
Json(updated_rating): Json<NewRating>,
) -> Result<impl IntoResponse, ChaosError> {
let rating = Rating::get_rating_by_rater_id(application_id, admin.user_id, &mut transaction.tx).await?;
Rating::update(rating.id, updated_rating, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok(AppMessage::OkMessage("Successfully updated rating"))
}
/// Retrieves all ratings for an application.
///
/// This handler allows application reviewers to view all ratings for an application.
///
/// # Arguments
///
/// * `_state` - The application state
/// * `application_id` - The ID of the application
/// * `_admin` - The authenticated user (must be an application reviewer)
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - List of ratings or error
pub async fn get_ratings(
State(_state): State<AppState>,
Path(application_id): Path<i64>,
_admin: ApplicationReviewerGivenApplicationId,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let ratings =
Rating::get_all_ratings_from_application_id(application_id, &mut transaction.tx)
.await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(ratings)))
}
/// Retrieves the average ratings for all users in an application.
///
/// This handler allows application reviewers to view the average ratings for all users in an application.
///
/// # Arguments
///
/// * `_user` - The authenticated user (must be an application reviewer)
/// * `application_id` - The ID of the application
/// * `transaction` - Database transaction
///
/// # Returns
///
/// * `Result<impl IntoResponse, ChaosError>` - List of average ratings or error
pub async fn get_application_ratings_summary(
_: CampaignOrgMember,
Path(campaign_id): Path<i64>,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let avg_applications_ratings =
Application::get_application_ratings_summary(campaign_id, &mut transaction.tx)
.await?;
transaction.tx.commit().await?;
Ok(Json(avg_applications_ratings))
}
}