|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use axum::{ |
| 4 | + extract::{Json, Path, State}, |
| 5 | + http::StatusCode, |
| 6 | + response::IntoResponse, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::models::{ |
| 10 | + app::{AppMessage, AppState, AvailabilitiesMessage}, |
| 11 | + auth::AuthUser, |
| 12 | + availabilities::{Availabilities, Availability}, |
| 13 | + error::ChaosError, |
| 14 | + transaction::DBTransaction, |
| 15 | +}; |
| 16 | + |
| 17 | +pub struct AvailabilitiesHandler; |
| 18 | + |
| 19 | +/// Gets or creates the user-campaign id, helper function |
| 20 | +/// |
| 21 | +/// # Arguments |
| 22 | +/// * `user_id` - ID of the user for the UC pair |
| 23 | +/// * `campaign_id` - ID of the campaign for the UC pair |
| 24 | +/// * `state` - current app state |
| 25 | +/// * `transaction` - the transaction |
| 26 | +/// |
| 27 | +/// # Returns |
| 28 | +/// Returns a `Result` containing either |
| 29 | +/// * `Ok(i64)` - the uc id |
| 30 | +/// * `Err(ChaosError)` - If no availabilities are found |
| 31 | +
|
| 32 | +async fn get_or_create_uc_id( |
| 33 | + user_id: i64, |
| 34 | + campaign_id: i64, |
| 35 | + state: &mut AppState, |
| 36 | + transaction: &mut DBTransaction<'_>, |
| 37 | +) -> Result<i64, ChaosError> { |
| 38 | + let uc_id_res = |
| 39 | + Availabilities::get_user_campaign_id(user_id, campaign_id, &mut transaction.tx).await; |
| 40 | + Ok(match uc_id_res { |
| 41 | + Err(_) => { |
| 42 | + Availabilities::create_user_campaign_availability( |
| 43 | + user_id, |
| 44 | + campaign_id, |
| 45 | + &mut state.snowflake_generator, |
| 46 | + &mut transaction.tx, |
| 47 | + ) |
| 48 | + .await? |
| 49 | + } |
| 50 | + Ok(uc) => uc.id, |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +impl AvailabilitiesHandler { |
| 55 | + // TODO: change auth_user to an availabilities specific extractor |
| 56 | + |
| 57 | + /// Retrieves all availability slots for a given user_id and campaign_id |
| 58 | + /// |
| 59 | + /// # Arguments |
| 60 | + /// |
| 61 | + /// * `user_id` - ID of the interviewer |
| 62 | + /// * `campaign_id` - ID of the campaign in which the user will be interviewing |
| 63 | + /// * `state` - The application state |
| 64 | + /// * `_auth_user` - The authenticated user |
| 65 | + /// * `transaction` - Database transaction |
| 66 | + /// |
| 67 | + /// # Returns |
| 68 | + /// Returns a `Result` containing either |
| 69 | + /// * `Ok(Vec<Availability>)` - Vec of availabilities |
| 70 | + /// * `Err(ChaosError)` - If no availabilities are found |
| 71 | +
|
| 72 | + pub async fn get( |
| 73 | + Path((user_id, campaign_id)): Path<(i64, i64)>, |
| 74 | + State(mut state): State<AppState>, |
| 75 | + _auth_user: AuthUser, |
| 76 | + mut transaction: DBTransaction<'_>, |
| 77 | + ) -> Result<impl IntoResponse, ChaosError> { |
| 78 | + let uc_id = get_or_create_uc_id(user_id, campaign_id, &mut state, &mut transaction).await?; |
| 79 | + |
| 80 | + let res = Availabilities::get_availability_slots(uc_id, &mut transaction.tx).await?; |
| 81 | + |
| 82 | + transaction.tx.commit().await?; |
| 83 | + |
| 84 | + Ok(( |
| 85 | + StatusCode::OK, |
| 86 | + Json(AvailabilitiesMessage { |
| 87 | + availabilities: res, |
| 88 | + }), |
| 89 | + )) |
| 90 | + } |
| 91 | + |
| 92 | + // TODO: change auth_user to an availabilities specific extractor |
| 93 | + |
| 94 | + /// Modifies availabilities for a given user in a campaign |
| 95 | + /// |
| 96 | + /// # Arguments |
| 97 | + /// |
| 98 | + /// * `user_id` - ID of the interviewer |
| 99 | + /// * `campaign_id` - ID of the campaign in which the user will be interviewing |
| 100 | + /// * `state` - The application state |
| 101 | + /// * `availabilities` - the set of ALL availabilities the user now has |
| 102 | + /// * `_auth_user` - The authenticated user |
| 103 | + /// * `transaction` - Database transaction |
| 104 | + /// |
| 105 | + /// # Returns |
| 106 | + /// Returns a `Result` containing either |
| 107 | + /// * `Ok(())` - If successful |
| 108 | + /// * `Err(ChaosError)` - Otherwise |
| 109 | +
|
| 110 | + pub async fn update( |
| 111 | + Path((user_id, campaign_id)): Path<(i64, i64)>, |
| 112 | + State(mut state): State<AppState>, |
| 113 | + _auth_user: AuthUser, |
| 114 | + mut transaction: DBTransaction<'_>, |
| 115 | + Json(availabilities): Json<Vec<Availability>>, |
| 116 | + ) -> Result<impl IntoResponse, ChaosError> { |
| 117 | + let uc_id = get_or_create_uc_id(user_id, campaign_id, &mut state, &mut transaction).await?; |
| 118 | + |
| 119 | + let curr_availabilities = |
| 120 | + Availabilities::get_availability_slots(uc_id, &mut transaction.tx).await?; |
| 121 | + |
| 122 | + // Diff is determined by assuming all current timeslots are to be deleted and none are to be added |
| 123 | + // Since if any availability slot is passed in, then it will either be added (if not already in the db) |
| 124 | + // or it won't be removed (if it is already in the db) |
| 125 | + let mut to_delete: HashSet<Availability> = curr_availabilities.into_iter().collect(); |
| 126 | + let mut to_add: Vec<Availability> = Vec::new(); |
| 127 | + |
| 128 | + availabilities.into_iter().for_each(|a| { |
| 129 | + if !to_delete.contains(&a) { |
| 130 | + to_add.push(a); |
| 131 | + } else { |
| 132 | + to_delete.remove(&a); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + Availabilities::delete_availabilities( |
| 137 | + user_id, |
| 138 | + campaign_id, |
| 139 | + to_delete.into_iter().collect(), |
| 140 | + &mut transaction.tx, |
| 141 | + ) |
| 142 | + .await?; |
| 143 | + |
| 144 | + Availabilities::create_availability_slots(uc_id, to_add, &mut transaction.tx).await?; |
| 145 | + |
| 146 | + transaction.tx.commit().await?; |
| 147 | + Ok(AppMessage::OkMessage("Successfully updated availabilities")) |
| 148 | + } |
| 149 | +} |
0 commit comments