Add POST /family/{id}/renew_invitation_code route

This commit is contained in:
Pierre HUBERT 2023-06-22 15:18:49 +02:00
parent b655ac5910
commit a66a12d2a6
2 changed files with 21 additions and 1 deletions

View File

@ -1,9 +1,10 @@
use crate::constants::StaticConstraints;
use crate::constants::{StaticConstraints, FAMILY_INVITATION_CODE_LEN};
use crate::controllers::HttpResult;
use crate::extractors::family_extractor::{FamilyInPath, FamilyInPathWithAdminMembership};
use crate::services::login_token_service::LoginToken;
use crate::services::rate_limiter_service::RatedAction;
use crate::services::{families_service, rate_limiter_service};
use crate::utils::string_utils::rand_str;
use actix_remote_ip::RemoteIP;
use actix_web::{web, HttpResponse};
@ -113,6 +114,21 @@ pub async fn delete(f: FamilyInPathWithAdminMembership) -> HttpResult {
Ok(HttpResponse::Accepted().finish())
}
/// Renew (change) invitation code
pub async fn renew_invitation_code(f: FamilyInPathWithAdminMembership) -> HttpResult {
let mut family = families_service::get_by_id(f.family_id()).await?;
family.invitation_code = rand_str(FAMILY_INVITATION_CODE_LEN);
families_service::update_family(&family).await?;
log::info!(
"User {:?} changed family {:?} invitation code",
f.user_id(),
f.family_id()
);
Ok(HttpResponse::Accepted().finish())
}
/// Get the list of users who belongs to a family
pub async fn users(f: FamilyInPath) -> HttpResult {
Ok(HttpResponse::Ok().json(families_service::get_memberships_of_family(f.family_id()).await?))

View File

@ -106,6 +106,10 @@ async fn main() -> std::io::Result<()> {
"/family/{id}",
web::delete().to(families_controller::delete),
)
.route(
"/family/{id}/renew_invitation_code",
web::post().to(families_controller::renew_invitation_code),
)
.route(
"/family/{id}/users",
web::get().to(families_controller::users),