|
8 | 8 | //! - Logo image handling |
9 | 9 |
|
10 | 10 | use crate::models::app::AppState; |
11 | | -use crate::models::auth::SuperUser; |
| 11 | +use crate::models::auth::{OrganisationAdminOrSuperUser, SuperUser}; |
12 | 12 | use crate::models::auth::{AuthUser, OrganisationAdmin}; |
13 | 13 | use crate::models::campaign::{Campaign, NewCampaign}; |
14 | 14 | use crate::models::email_template::{EmailTemplate, NewEmailTemplate}; |
15 | 15 | use crate::models::error::ChaosError; |
16 | | -use crate::models::organisation::{AdminToRemove, AdminUpdateList, NewOrganisation, Organisation, OrganisationDetails, SlugCheck}; |
| 16 | +use crate::models::organisation::{ |
| 17 | + AdminToRemove, AdminUpdateList, NewOrganisation, Organisation, OrganisationDetails, SlugCheck, EmailRoleBody, IdRoleBody, IdBody |
| 18 | +}; |
| 19 | +use crate::service::user::{user_exists_by_email}; |
| 20 | +use crate::service::organisation::{assert_user_is_not_in_organisation}; |
17 | 21 | use crate::models::transaction::DBTransaction; |
18 | 22 | use crate::service::auth::assert_is_super_user; |
19 | 23 | use axum::extract::{Json, Path, State}; |
@@ -220,7 +224,7 @@ impl OrganisationHandler { |
220 | 224 | pub async fn get_members( |
221 | 225 | mut transaction: DBTransaction<'_>, |
222 | 226 | Path(id): Path<i64>, |
223 | | - _admin: OrganisationAdmin, |
| 227 | + _admin: OrganisationAdminOrSuperUser, |
224 | 228 | ) -> Result<impl IntoResponse, ChaosError> { |
225 | 229 | let members = Organisation::get_members(id, &mut transaction.tx).await?; |
226 | 230 |
|
@@ -327,9 +331,11 @@ impl OrganisationHandler { |
327 | 331 | mut transaction: DBTransaction<'_>, |
328 | 332 | Path(id): Path<i64>, |
329 | 333 | _admin: OrganisationAdmin, |
330 | | - Json(request_body): Json<AdminToRemove>, |
| 334 | + Json(req): Json<IdBody>, |
331 | 335 | ) -> Result<impl IntoResponse, ChaosError> { |
332 | | - Organisation::remove_member(id, request_body.user_id, &mut transaction.tx).await?; |
| 336 | + let tx = &mut transaction.tx; |
| 337 | + //let user_id = user_exists_by_email(req.email, tx).await?; |
| 338 | + Organisation::remove_member(id, req.user_id, tx).await?; |
333 | 339 |
|
334 | 340 | transaction.tx.commit().await?; |
335 | 341 | Ok(( |
@@ -508,4 +514,37 @@ impl OrganisationHandler { |
508 | 514 | transaction.tx.commit().await?; |
509 | 515 | Ok((StatusCode::OK, Json(email_templates))) |
510 | 516 | } |
| 517 | + |
| 518 | + pub async fn add_member( |
| 519 | + _user: OrganisationAdminOrSuperUser, |
| 520 | + mut transaction: DBTransaction<'_>, |
| 521 | + Path(org_id): Path<i64>, |
| 522 | + Json(req): Json<EmailRoleBody>, |
| 523 | + ) -> Result<impl IntoResponse, ChaosError> { |
| 524 | + let tx = &mut transaction.tx; |
| 525 | + let user_id = user_exists_by_email(req.email, tx).await?; |
| 526 | + assert_user_is_not_in_organisation(user_id, org_id, tx).await?; |
| 527 | + Organisation::add_member_or_admin_to_organisation(org_id, user_id, req.role.clone(), tx).await?; |
| 528 | + |
| 529 | + transaction.tx.commit().await?; |
| 530 | + Ok((StatusCode::OK, "Member added to organisation")) |
| 531 | + } |
| 532 | + |
| 533 | + pub async fn update_member_role( |
| 534 | + _user: OrganisationAdmin, |
| 535 | + mut transaction: DBTransaction<'_>, |
| 536 | + Path(org_id): Path<i64>, |
| 537 | + Json(req): Json<IdRoleBody>, |
| 538 | + ) -> Result<impl IntoResponse, ChaosError> { |
| 539 | + let tx = &mut transaction.tx; |
| 540 | + Organisation::change_member_role( |
| 541 | + org_id, |
| 542 | + req.user_id, |
| 543 | + req.role, |
| 544 | + tx |
| 545 | + ) |
| 546 | + .await?; |
| 547 | + transaction.tx.commit().await?; |
| 548 | + Ok(StatusCode::OK) |
| 549 | + } |
511 | 550 | } |
0 commit comments