-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
534df8d
commit 9d346e0
Showing
8 changed files
with
255 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ pub mod rating; | |
pub mod role; | ||
pub mod user; | ||
pub mod email_template; | ||
pub mod offer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use axum::extract::{Path, Json}; | ||
use axum::http::StatusCode; | ||
use axum::response::IntoResponse; | ||
use crate::models::auth::{AuthUser, CampaignAdmin, OfferAdmin, OfferRecipient}; | ||
use crate::models::error::ChaosError; | ||
use crate::models::offer::{Offer, OfferReply}; | ||
use crate::models::transaction::DBTransaction; | ||
|
||
|
||
pub struct OfferHandler; | ||
impl OfferHandler { | ||
pub async fn get( | ||
mut transaction: DBTransaction<'_>, | ||
Path(id): Path<i64>, | ||
_user: OfferAdmin, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
let offer = Offer::get(id, &mut transaction.tx).await?; | ||
transaction.tx.commit().await?; | ||
|
||
Ok((StatusCode::OK, Json(offer))) | ||
} | ||
|
||
pub async fn delete( | ||
mut transaction: DBTransaction<'_>, | ||
Path(id): Path<i64>, | ||
_user: OfferAdmin | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Offer::delete(id, &mut transaction.tx).await?; | ||
transaction.tx.commit().await?; | ||
|
||
Ok((StatusCode::OK, "Successfully deleted offer")) | ||
} | ||
|
||
pub async fn reply( | ||
mut transaction: DBTransaction<'_>, | ||
Path(id): Path<i64>, | ||
_user: OfferRecipient, | ||
Json(reply): Json<OfferReply>, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Offer::reply(id, reply.accept, &mut transaction.tx).await?; | ||
transaction.tx.commit().await?; | ||
|
||
Ok((StatusCode::OK, "Successfully accepted offer")) | ||
} | ||
|
||
pub async fn preview_email( | ||
mut transaction: DBTransaction<'_>, | ||
Path(id): Path<i64>, | ||
_user: OfferAdmin, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
let string = Offer::preview_email(id, &mut transaction.tx).await?; | ||
transaction.tx.commit().await?; | ||
|
||
Ok((StatusCode::OK, string)) | ||
} | ||
|
||
pub async fn send_offer( | ||
mut transaction: DBTransaction<'_>, | ||
Path(id): Path<i64>, | ||
_user: OfferAdmin, | ||
) -> Result<impl IntoResponse, ChaosError> { | ||
Offer::send_offer(id, &mut transaction.tx).await?; | ||
transaction.tx.commit().await?; | ||
|
||
Ok((StatusCode::OK, "Successfully sent offer")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::models::error::ChaosError; | ||
use sqlx::{Pool, Postgres}; | ||
use crate::models::offer::Offer; | ||
|
||
pub async fn assert_user_is_offer_admin( | ||
user_id: i64, | ||
offer_id: i64, | ||
pool: &Pool<Postgres>, | ||
) -> Result<(), ChaosError> { | ||
let is_admin = sqlx::query!( | ||
" | ||
SELECT EXISTS( | ||
SELECT 1 FROM offers off | ||
JOIN campaigns c ON c.id = off.campaign_id | ||
JOIN organisation_members m on c.organisation_id = m.organisation_id | ||
WHERE off.id = $1 AND m.user_id = $2 AND m.role = 'Admin' | ||
) | ||
", | ||
offer_id, | ||
user_id | ||
) | ||
.fetch_one(pool) | ||
.await? | ||
.exists | ||
.expect("`exists` should always exist in this query result"); | ||
|
||
if !is_admin { | ||
return Err(ChaosError::Unauthorized); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn assert_user_is_offer_recipient( | ||
user_id: i64, | ||
offer_id: i64, | ||
pool: &Pool<Postgres>, | ||
) -> Result<(), ChaosError> { | ||
let tx = &mut pool.begin().await?; | ||
let offer = Offer::get(offer_id, tx).await?; | ||
|
||
if offer.user_id != user_id { | ||
return Err(ChaosError::Unauthorized) | ||
} | ||
|
||
Ok(()) | ||
} |