feat: v0.3.0 — standardize codebase, centralize infra, merge QR into CMS
- Enforce axum best practices across all 13 workspace crates (max 200 LOC/file, no comments, no unwrap, clean architecture) - Fix domain→infrastructure dependency inversions in imphnen-iam and imphnen-dimentorin - Extract imphnen-storage (MinIO) and imphnen-email (Lettre) as standalone crates - Centralize all config in ENV struct: CDN_URL, CORS_ALLOWED_ORIGINS - Centralize SMTP through imphnen-email; remove dead HackathonConfig - Centralize database: QR crate now shares main DB pool (single DATABASE_URL) - Rename QR users table to qr_users to avoid collision with main users table - Merge imphnen-qr into imphnen-cms/src/qr (13 crates, down from 14) - Restructure imphnen-hackathon flat modules into clean architecture - Remove all stale env vars from .env.example (SurrealDB, QR_JWT, Hackathon infra) - Fix Dockerfile to include all current workspace crates - Bump all crate versions 0.2.0 → 0.3.0 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
2ae43b3bcc
commit
331a4a4e88
@@ -1,129 +1,184 @@
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
use async_trait::async_trait;
|
||||
use chrono::{Utc, TimeZone};
|
||||
use imphnen_utils::errors::AppError;
|
||||
use crate::invitations::domain::entity::*;
|
||||
use crate::invitations::domain::repository::InvitationRepository;
|
||||
use crate::invitations::domain::service::InvitationService;
|
||||
use async_trait::async_trait;
|
||||
use chrono::{TimeZone, Utc};
|
||||
use imphnen_utils::errors::AppError;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
fn is_team_features_closed() -> bool {
|
||||
let deadline = Utc.with_ymd_and_hms(2025, 11, 30, 16, 59, 0).unwrap();
|
||||
Utc::now() >= deadline
|
||||
let deadline = Utc
|
||||
.with_ymd_and_hms(2025, 11, 30, 16, 59, 0)
|
||||
.single()
|
||||
.expect("valid constant date");
|
||||
Utc::now() >= deadline
|
||||
}
|
||||
|
||||
pub struct InvitationServiceImpl {
|
||||
repo: Arc<dyn InvitationRepository>,
|
||||
repo: Arc<dyn InvitationRepository>,
|
||||
}
|
||||
|
||||
impl InvitationServiceImpl {
|
||||
pub fn new(repo: Arc<dyn InvitationRepository>) -> Self {
|
||||
Self { repo }
|
||||
}
|
||||
pub fn new(repo: Arc<dyn InvitationRepository>) -> Self {
|
||||
Self { repo }
|
||||
}
|
||||
|
||||
async fn do_invite(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
input: CreateInvitationInput,
|
||||
) -> Result<InvitationWithDetails, AppError> {
|
||||
if is_team_features_closed() {
|
||||
return Err(AppError::BadRequestError(
|
||||
"Team invitations are closed (deadline: November 30, 2025).".to_string(),
|
||||
));
|
||||
}
|
||||
let leader_id = self.repo.get_team_leader_id(team_id).await?
|
||||
.ok_or_else(|| AppError::NotFoundError("Team not found".to_string()))?;
|
||||
if leader_id != inviter_id {
|
||||
return Err(AppError::ForbiddenError("Only the team leader can send invitations".to_string()));
|
||||
}
|
||||
if self.repo.team_has_submission(team_id).await? {
|
||||
return Err(AppError::BadRequestError("Cannot invite after submitting a project".to_string()));
|
||||
}
|
||||
let count = self.repo.active_member_count(team_id).await?;
|
||||
if count >= 5 {
|
||||
return Err(AppError::BadRequestError("Team already has the maximum of 5 members".to_string()));
|
||||
}
|
||||
let team_name = self.repo.get_team_name(team_id).await?
|
||||
.ok_or_else(|| AppError::NotFoundError("Team not found".to_string()))?;
|
||||
let inviter_fullname = self.repo.get_inviter_name(inviter_id).await?
|
||||
.unwrap_or_else(|| "Unknown".to_string());
|
||||
let invitation_id = Uuid::new_v4();
|
||||
let entity = self.repo.create(invitation_id, team_id, inviter_id, &input.invitee_email).await?;
|
||||
tracing::warn!("Email sending is not available; invitation created for {}", input.invitee_email);
|
||||
Ok(InvitationWithDetails {
|
||||
id: entity.id,
|
||||
team_id: entity.team_id,
|
||||
team_name,
|
||||
inviter_id: entity.inviter_id,
|
||||
inviter_fullname,
|
||||
invitee_email: entity.invitee_email,
|
||||
status: entity.status,
|
||||
created_at: entity.created_at,
|
||||
})
|
||||
}
|
||||
async fn do_invite(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
input: CreateInvitationInput,
|
||||
) -> Result<InvitationWithDetails, AppError> {
|
||||
if is_team_features_closed() {
|
||||
return Err(AppError::BadRequestError(
|
||||
"Team invitations are closed (deadline: November 30, 2025).".to_string(),
|
||||
));
|
||||
}
|
||||
let leader_id = self
|
||||
.repo
|
||||
.get_team_leader_id(team_id)
|
||||
.await?
|
||||
.ok_or_else(|| AppError::NotFoundError("Team not found".to_string()))?;
|
||||
if leader_id != inviter_id {
|
||||
return Err(AppError::ForbiddenError(
|
||||
"Only the team leader can send invitations".to_string(),
|
||||
));
|
||||
}
|
||||
if self.repo.team_has_submission(team_id).await? {
|
||||
return Err(AppError::BadRequestError(
|
||||
"Cannot invite after submitting a project".to_string(),
|
||||
));
|
||||
}
|
||||
let count = self.repo.active_member_count(team_id).await?;
|
||||
if count >= 5 {
|
||||
return Err(AppError::BadRequestError(
|
||||
"Team already has the maximum of 5 members".to_string(),
|
||||
));
|
||||
}
|
||||
let team_name = self
|
||||
.repo
|
||||
.get_team_name(team_id)
|
||||
.await?
|
||||
.ok_or_else(|| AppError::NotFoundError("Team not found".to_string()))?;
|
||||
let inviter_fullname = self
|
||||
.repo
|
||||
.get_inviter_name(inviter_id)
|
||||
.await?
|
||||
.unwrap_or_else(|| "Unknown".to_string());
|
||||
let invitation_id = Uuid::new_v4();
|
||||
let entity = self
|
||||
.repo
|
||||
.create(invitation_id, team_id, inviter_id, &input.invitee_email)
|
||||
.await?;
|
||||
tracing::warn!(
|
||||
"Email sending is not available; invitation created for {}",
|
||||
input.invitee_email
|
||||
);
|
||||
Ok(InvitationWithDetails {
|
||||
id: entity.id,
|
||||
team_id: entity.team_id,
|
||||
team_name,
|
||||
inviter_id: entity.inviter_id,
|
||||
inviter_fullname,
|
||||
invitee_email: entity.invitee_email,
|
||||
status: entity.status,
|
||||
created_at: entity.created_at,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl InvitationService for InvitationServiceImpl {
|
||||
async fn invite_member(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
input: CreateInvitationInput,
|
||||
) -> Result<InvitationWithDetails, AppError> {
|
||||
self.do_invite(team_id, inviter_id, input).await
|
||||
}
|
||||
async fn invite_member(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
input: CreateInvitationInput,
|
||||
) -> Result<InvitationWithDetails, AppError> {
|
||||
self.do_invite(team_id, inviter_id, input).await
|
||||
}
|
||||
|
||||
async fn invite_member_for_team(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
input: CreateInvitationInput,
|
||||
) -> Result<InvitationWithDetails, AppError> {
|
||||
self.do_invite(team_id, inviter_id, input).await
|
||||
}
|
||||
async fn invite_member_for_team(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
input: CreateInvitationInput,
|
||||
) -> Result<InvitationWithDetails, AppError> {
|
||||
self.do_invite(team_id, inviter_id, input).await
|
||||
}
|
||||
|
||||
async fn get_my_invitations(&self, user_id: Uuid) -> Result<Vec<InvitationWithDetails>, AppError> {
|
||||
let email = self.repo.get_user_email(user_id).await?
|
||||
.ok_or_else(|| AppError::NotFoundError("User not found".to_string()))?;
|
||||
self.repo.find_pending_by_email(&email).await
|
||||
}
|
||||
async fn get_my_invitations(
|
||||
&self,
|
||||
user_id: Uuid,
|
||||
) -> Result<Vec<InvitationWithDetails>, AppError> {
|
||||
let email = self
|
||||
.repo
|
||||
.get_user_email(user_id)
|
||||
.await?
|
||||
.ok_or_else(|| AppError::NotFoundError("User not found".to_string()))?;
|
||||
self.repo.find_pending_by_email(&email).await
|
||||
}
|
||||
|
||||
async fn respond_to_invitation(
|
||||
&self,
|
||||
invitation_id: Uuid,
|
||||
user_id: Uuid,
|
||||
accept: bool,
|
||||
) -> Result<(), AppError> {
|
||||
let invitation = self.repo.find_by_id(invitation_id).await?
|
||||
.ok_or_else(|| AppError::NotFoundError("Invitation not found".to_string()))?;
|
||||
let user_email = self.repo.get_user_email(user_id).await?
|
||||
.ok_or_else(|| AppError::NotFoundError("User not found".to_string()))?;
|
||||
if invitation.invitee_email != user_email {
|
||||
return Err(AppError::ForbiddenError("This invitation is not for you".to_string()));
|
||||
}
|
||||
if invitation.status != "pending" {
|
||||
return Err(AppError::BadRequestError("Invitation is no longer pending".to_string()));
|
||||
}
|
||||
if accept {
|
||||
if self.repo.team_has_submission(invitation.team_id).await? {
|
||||
return Err(AppError::BadRequestError("Cannot join a team that has already submitted".to_string()));
|
||||
}
|
||||
if let Some(active_team) = self.repo.user_active_team_name(user_id).await? {
|
||||
return Err(AppError::ConflictError(format!("You are already a member of team '{}'", active_team)));
|
||||
}
|
||||
let count = self.repo.active_member_count(invitation.team_id).await?;
|
||||
if count >= 5 {
|
||||
return Err(AppError::BadRequestError("Team is already full".to_string()));
|
||||
}
|
||||
self.repo.update_status(invitation_id, "accepted").await?;
|
||||
self.repo.add_team_member(invitation.team_id, user_id).await?;
|
||||
self.repo.reject_pending_for_email_except(&user_email, invitation_id).await?;
|
||||
self.repo.reject_pending_join_requests_for_user(user_id).await?;
|
||||
} else {
|
||||
self.repo.update_status(invitation_id, "rejected").await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
async fn respond_to_invitation(
|
||||
&self,
|
||||
invitation_id: Uuid,
|
||||
user_id: Uuid,
|
||||
accept: bool,
|
||||
) -> Result<(), AppError> {
|
||||
let invitation =
|
||||
self.repo.find_by_id(invitation_id).await?.ok_or_else(|| {
|
||||
AppError::NotFoundError("Invitation not found".to_string())
|
||||
})?;
|
||||
let user_email = self
|
||||
.repo
|
||||
.get_user_email(user_id)
|
||||
.await?
|
||||
.ok_or_else(|| AppError::NotFoundError("User not found".to_string()))?;
|
||||
if invitation.invitee_email != user_email {
|
||||
return Err(AppError::ForbiddenError(
|
||||
"This invitation is not for you".to_string(),
|
||||
));
|
||||
}
|
||||
if invitation.status != "pending" {
|
||||
return Err(AppError::BadRequestError(
|
||||
"Invitation is no longer pending".to_string(),
|
||||
));
|
||||
}
|
||||
if accept {
|
||||
if self.repo.team_has_submission(invitation.team_id).await? {
|
||||
return Err(AppError::BadRequestError(
|
||||
"Cannot join a team that has already submitted".to_string(),
|
||||
));
|
||||
}
|
||||
if let Some(active_team) = self.repo.user_active_team_name(user_id).await? {
|
||||
return Err(AppError::ConflictError(format!(
|
||||
"You are already a member of team '{}'",
|
||||
active_team
|
||||
)));
|
||||
}
|
||||
let count = self.repo.active_member_count(invitation.team_id).await?;
|
||||
if count >= 5 {
|
||||
return Err(AppError::BadRequestError(
|
||||
"Team is already full".to_string(),
|
||||
));
|
||||
}
|
||||
self.repo.update_status(invitation_id, "accepted").await?;
|
||||
self
|
||||
.repo
|
||||
.add_team_member(invitation.team_id, user_id)
|
||||
.await?;
|
||||
self
|
||||
.repo
|
||||
.reject_pending_for_email_except(&user_email, invitation_id)
|
||||
.await?;
|
||||
self
|
||||
.repo
|
||||
.reject_pending_join_requests_for_user(user_id)
|
||||
.await?;
|
||||
} else {
|
||||
self.repo.update_status(invitation_id, "rejected").await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
use uuid::Uuid;
|
||||
use chrono::{DateTime, Utc};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct InvitationEntity {
|
||||
pub id: Uuid,
|
||||
pub team_id: Uuid,
|
||||
pub inviter_id: Uuid,
|
||||
pub invitee_email: String,
|
||||
pub status: String,
|
||||
pub created_at: Option<DateTime<Utc>>,
|
||||
pub id: Uuid,
|
||||
pub team_id: Uuid,
|
||||
pub inviter_id: Uuid,
|
||||
pub invitee_email: String,
|
||||
pub status: String,
|
||||
pub created_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct InvitationWithDetails {
|
||||
pub id: Uuid,
|
||||
pub team_id: Uuid,
|
||||
pub team_name: String,
|
||||
pub inviter_id: Uuid,
|
||||
pub inviter_fullname: String,
|
||||
pub invitee_email: String,
|
||||
pub status: String,
|
||||
pub created_at: Option<DateTime<Utc>>,
|
||||
pub id: Uuid,
|
||||
pub team_id: Uuid,
|
||||
pub team_name: String,
|
||||
pub inviter_id: Uuid,
|
||||
pub inviter_fullname: String,
|
||||
pub invitee_email: String,
|
||||
pub status: String,
|
||||
pub created_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct CreateInvitationInput {
|
||||
pub invitee_email: String,
|
||||
pub invitee_email: String,
|
||||
}
|
||||
|
||||
@@ -1,41 +1,65 @@
|
||||
use async_trait::async_trait;
|
||||
use uuid::Uuid;
|
||||
use imphnen_utils::errors::AppError;
|
||||
use super::entity::*;
|
||||
use async_trait::async_trait;
|
||||
use imphnen_utils::errors::AppError;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[async_trait]
|
||||
pub trait InvitationRepository: Send + Sync {
|
||||
async fn create(
|
||||
&self,
|
||||
invitation_id: Uuid,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
invitee_email: &str,
|
||||
) -> Result<InvitationEntity, AppError>;
|
||||
async fn create(
|
||||
&self,
|
||||
invitation_id: Uuid,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
invitee_email: &str,
|
||||
) -> Result<InvitationEntity, AppError>;
|
||||
|
||||
async fn find_by_id(&self, id: Uuid) -> Result<Option<InvitationEntity>, AppError>;
|
||||
async fn find_by_id(&self, id: Uuid)
|
||||
-> Result<Option<InvitationEntity>, AppError>;
|
||||
|
||||
async fn find_pending_by_email(&self, email: &str) -> Result<Vec<InvitationWithDetails>, AppError>;
|
||||
async fn find_pending_by_email(
|
||||
&self,
|
||||
email: &str,
|
||||
) -> Result<Vec<InvitationWithDetails>, AppError>;
|
||||
|
||||
async fn update_status(&self, id: Uuid, status: &str) -> Result<(), AppError>;
|
||||
async fn update_status(&self, id: Uuid, status: &str) -> Result<(), AppError>;
|
||||
|
||||
async fn reject_pending_for_email_except(&self, email: &str, except_id: Uuid) -> Result<(), AppError>;
|
||||
async fn reject_pending_for_email_except(
|
||||
&self,
|
||||
email: &str,
|
||||
except_id: Uuid,
|
||||
) -> Result<(), AppError>;
|
||||
|
||||
async fn add_team_member(&self, team_id: Uuid, user_id: Uuid) -> Result<(), AppError>;
|
||||
async fn add_team_member(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
user_id: Uuid,
|
||||
) -> Result<(), AppError>;
|
||||
|
||||
async fn reject_pending_join_requests_for_user(&self, user_id: Uuid) -> Result<(), AppError>;
|
||||
async fn reject_pending_join_requests_for_user(
|
||||
&self,
|
||||
user_id: Uuid,
|
||||
) -> Result<(), AppError>;
|
||||
|
||||
async fn get_team_leader_id(&self, team_id: Uuid) -> Result<Option<Uuid>, AppError>;
|
||||
async fn get_team_leader_id(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
) -> Result<Option<Uuid>, AppError>;
|
||||
|
||||
async fn get_team_name(&self, team_id: Uuid) -> Result<Option<String>, AppError>;
|
||||
async fn get_team_name(&self, team_id: Uuid) -> Result<Option<String>, AppError>;
|
||||
|
||||
async fn get_user_email(&self, user_id: Uuid) -> Result<Option<String>, AppError>;
|
||||
async fn get_user_email(&self, user_id: Uuid) -> Result<Option<String>, AppError>;
|
||||
|
||||
async fn get_inviter_name(&self, user_id: Uuid) -> Result<Option<String>, AppError>;
|
||||
async fn get_inviter_name(
|
||||
&self,
|
||||
user_id: Uuid,
|
||||
) -> Result<Option<String>, AppError>;
|
||||
|
||||
async fn active_member_count(&self, team_id: Uuid) -> Result<i64, AppError>;
|
||||
async fn active_member_count(&self, team_id: Uuid) -> Result<i64, AppError>;
|
||||
|
||||
async fn team_has_submission(&self, team_id: Uuid) -> Result<bool, AppError>;
|
||||
async fn team_has_submission(&self, team_id: Uuid) -> Result<bool, AppError>;
|
||||
|
||||
async fn user_active_team_name(&self, user_id: Uuid) -> Result<Option<String>, AppError>;
|
||||
async fn user_active_team_name(
|
||||
&self,
|
||||
user_id: Uuid,
|
||||
) -> Result<Option<String>, AppError>;
|
||||
}
|
||||
|
||||
@@ -1,30 +1,33 @@
|
||||
use async_trait::async_trait;
|
||||
use uuid::Uuid;
|
||||
use imphnen_utils::errors::AppError;
|
||||
use super::entity::*;
|
||||
use async_trait::async_trait;
|
||||
use imphnen_utils::errors::AppError;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[async_trait]
|
||||
pub trait InvitationService: Send + Sync {
|
||||
async fn invite_member(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
input: CreateInvitationInput,
|
||||
) -> Result<InvitationWithDetails, AppError>;
|
||||
async fn invite_member(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
input: CreateInvitationInput,
|
||||
) -> Result<InvitationWithDetails, AppError>;
|
||||
|
||||
async fn invite_member_for_team(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
input: CreateInvitationInput,
|
||||
) -> Result<InvitationWithDetails, AppError>;
|
||||
async fn invite_member_for_team(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
input: CreateInvitationInput,
|
||||
) -> Result<InvitationWithDetails, AppError>;
|
||||
|
||||
async fn get_my_invitations(&self, user_id: Uuid) -> Result<Vec<InvitationWithDetails>, AppError>;
|
||||
async fn get_my_invitations(
|
||||
&self,
|
||||
user_id: Uuid,
|
||||
) -> Result<Vec<InvitationWithDetails>, AppError>;
|
||||
|
||||
async fn respond_to_invitation(
|
||||
&self,
|
||||
invitation_id: Uuid,
|
||||
user_id: Uuid,
|
||||
accept: bool,
|
||||
) -> Result<(), AppError>;
|
||||
async fn respond_to_invitation(
|
||||
&self,
|
||||
invitation_id: Uuid,
|
||||
user_id: Uuid,
|
||||
accept: bool,
|
||||
) -> Result<(), AppError>;
|
||||
}
|
||||
|
||||
@@ -1,50 +1,50 @@
|
||||
use crate::invitations::domain::entity::*;
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
use uuid::Uuid;
|
||||
use chrono::{DateTime, Utc};
|
||||
use crate::invitations::domain::entity::*;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct InvitationResponse {
|
||||
pub id: Uuid,
|
||||
pub team_id: Uuid,
|
||||
pub team_name: String,
|
||||
pub inviter_id: Uuid,
|
||||
pub inviter_fullname: String,
|
||||
pub invitee_email: String,
|
||||
pub status: String,
|
||||
pub created_at: Option<DateTime<Utc>>,
|
||||
pub id: Uuid,
|
||||
pub team_id: Uuid,
|
||||
pub team_name: String,
|
||||
pub inviter_id: Uuid,
|
||||
pub inviter_fullname: String,
|
||||
pub invitee_email: String,
|
||||
pub status: String,
|
||||
pub created_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl From<InvitationWithDetails> for InvitationResponse {
|
||||
fn from(e: InvitationWithDetails) -> Self {
|
||||
Self {
|
||||
id: e.id,
|
||||
team_id: e.team_id,
|
||||
team_name: e.team_name,
|
||||
inviter_id: e.inviter_id,
|
||||
inviter_fullname: e.inviter_fullname,
|
||||
invitee_email: e.invitee_email,
|
||||
status: e.status,
|
||||
created_at: e.created_at,
|
||||
}
|
||||
}
|
||||
fn from(e: InvitationWithDetails) -> Self {
|
||||
Self {
|
||||
id: e.id,
|
||||
team_id: e.team_id,
|
||||
team_name: e.team_name,
|
||||
inviter_id: e.inviter_id,
|
||||
inviter_fullname: e.inviter_fullname,
|
||||
invitee_email: e.invitee_email,
|
||||
status: e.status,
|
||||
created_at: e.created_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct RespondToInvitationRequest {
|
||||
pub accept: bool,
|
||||
pub accept: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, ToSchema)]
|
||||
pub struct CreateInvitationRequest {
|
||||
pub invitee_email: String,
|
||||
pub invitee_email: String,
|
||||
}
|
||||
|
||||
impl From<CreateInvitationRequest> for CreateInvitationInput {
|
||||
fn from(r: CreateInvitationRequest) -> Self {
|
||||
Self {
|
||||
invitee_email: r.invitee_email,
|
||||
}
|
||||
}
|
||||
fn from(r: CreateInvitationRequest) -> Self {
|
||||
Self {
|
||||
invitee_email: r.invitee_email,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +1,49 @@
|
||||
use super::dto::*;
|
||||
use crate::invitations::domain::service::InvitationService;
|
||||
use crate::middleware::hackathon_auth::HackathonAuthUser;
|
||||
use axum::{Extension, Json, extract::Path, response::IntoResponse};
|
||||
use imphnen_utils::{
|
||||
errors::AppError,
|
||||
response_format::{ApiMessage, ApiSuccess},
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
use imphnen_utils::{errors::AppError, response_format::{ApiSuccess, ApiMessage}};
|
||||
use crate::middleware::hackathon_auth::HackathonAuthUser;
|
||||
use crate::invitations::domain::service::InvitationService;
|
||||
use super::dto::*;
|
||||
|
||||
pub async fn get_my_invitations_handler(
|
||||
Extension(service): Extension<Arc<dyn InvitationService>>,
|
||||
Extension(auth): Extension<HackathonAuthUser>,
|
||||
Extension(service): Extension<Arc<dyn InvitationService>>,
|
||||
Extension(auth): Extension<HackathonAuthUser>,
|
||||
) -> Result<axum::response::Response, AppError> {
|
||||
let list = service.get_my_invitations(auth.user_id).await?;
|
||||
let response: Vec<InvitationResponse> = list.into_iter().map(InvitationResponse::from).collect();
|
||||
Ok(ApiSuccess(response).into_response())
|
||||
let list = service.get_my_invitations(auth.user_id).await?;
|
||||
let response: Vec<InvitationResponse> =
|
||||
list.into_iter().map(InvitationResponse::from).collect();
|
||||
Ok(ApiSuccess(response).into_response())
|
||||
}
|
||||
|
||||
pub async fn respond_to_invitation_handler(
|
||||
Extension(service): Extension<Arc<dyn InvitationService>>,
|
||||
Extension(auth): Extension<HackathonAuthUser>,
|
||||
Path(invitation_id): Path<Uuid>,
|
||||
Json(body): Json<RespondToInvitationRequest>,
|
||||
Extension(service): Extension<Arc<dyn InvitationService>>,
|
||||
Extension(auth): Extension<HackathonAuthUser>,
|
||||
Path(invitation_id): Path<Uuid>,
|
||||
Json(body): Json<RespondToInvitationRequest>,
|
||||
) -> Result<axum::response::Response, AppError> {
|
||||
service.respond_to_invitation(invitation_id, auth.user_id, body.accept).await?;
|
||||
let msg = if body.accept { "Invitation accepted" } else { "Invitation declined" };
|
||||
Ok(ApiMessage::ok(msg).into_response())
|
||||
service
|
||||
.respond_to_invitation(invitation_id, auth.user_id, body.accept)
|
||||
.await?;
|
||||
let msg = if body.accept {
|
||||
"Invitation accepted"
|
||||
} else {
|
||||
"Invitation declined"
|
||||
};
|
||||
Ok(ApiMessage::ok(msg).into_response())
|
||||
}
|
||||
|
||||
pub async fn invite_team_member_handler(
|
||||
Extension(service): Extension<Arc<dyn InvitationService>>,
|
||||
Extension(auth): Extension<HackathonAuthUser>,
|
||||
Path(team_id): Path<Uuid>,
|
||||
Json(body): Json<CreateInvitationRequest>,
|
||||
Extension(service): Extension<Arc<dyn InvitationService>>,
|
||||
Extension(auth): Extension<HackathonAuthUser>,
|
||||
Path(team_id): Path<Uuid>,
|
||||
Json(body): Json<CreateInvitationRequest>,
|
||||
) -> Result<axum::response::Response, AppError> {
|
||||
let invitation = service.invite_member(team_id, auth.user_id, body.into()).await?;
|
||||
Ok(ApiSuccess(InvitationResponse::from(invitation)).into_response())
|
||||
let invitation = service
|
||||
.invite_member(team_id, auth.user_id, body.into())
|
||||
.await?;
|
||||
Ok(ApiSuccess(InvitationResponse::from(invitation)).into_response())
|
||||
}
|
||||
|
||||
@@ -1,21 +1,31 @@
|
||||
use axum::{middleware::from_fn, routing::{get, post}, Extension, Router};
|
||||
use sqlx::PgPool;
|
||||
use std::sync::Arc;
|
||||
use super::handlers::*;
|
||||
use crate::invitations::application::invitation_service::InvitationServiceImpl;
|
||||
use crate::invitations::domain::service::InvitationService;
|
||||
use crate::invitations::infrastructure::persistence::PostgresInvitationRepository;
|
||||
use crate::middleware::hackathon_auth::hackathon_auth_middleware;
|
||||
use super::handlers::*;
|
||||
use axum::{
|
||||
Extension, Router,
|
||||
middleware::from_fn,
|
||||
routing::{get, post},
|
||||
};
|
||||
use sqlx::PgPool;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn build_invitation_routes(pool: Arc<PgPool>) -> Router {
|
||||
let service: Arc<dyn InvitationService> = Arc::new(InvitationServiceImpl::new(
|
||||
Arc::new(PostgresInvitationRepository::new(pool.clone())),
|
||||
));
|
||||
Router::new()
|
||||
.route("/invitations/my", get(get_my_invitations_handler))
|
||||
.route("/invitations/:invitation_id/respond", post(respond_to_invitation_handler))
|
||||
.route("/invitations/teams/:team_id/invite", post(invite_team_member_handler))
|
||||
.layer(Extension(service))
|
||||
.layer(Extension(pool))
|
||||
.layer(from_fn(hackathon_auth_middleware))
|
||||
let service: Arc<dyn InvitationService> = Arc::new(InvitationServiceImpl::new(
|
||||
Arc::new(PostgresInvitationRepository::new(pool.clone())),
|
||||
));
|
||||
Router::new()
|
||||
.route("/invitations/my", get(get_my_invitations_handler))
|
||||
.route(
|
||||
"/invitations/:invitation_id/respond",
|
||||
post(respond_to_invitation_handler),
|
||||
)
|
||||
.route(
|
||||
"/invitations/teams/:team_id/invite",
|
||||
post(invite_team_member_handler),
|
||||
)
|
||||
.layer(Extension(service))
|
||||
.layer(Extension(pool))
|
||||
.layer(from_fn(hackathon_auth_middleware))
|
||||
}
|
||||
|
||||
+154
-102
@@ -1,159 +1,211 @@
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
use chrono::{DateTime, Utc};
|
||||
use async_trait::async_trait;
|
||||
use sqlx::{PgPool, FromRow};
|
||||
use imphnen_utils::errors::AppError;
|
||||
use crate::invitations::domain::entity::*;
|
||||
use crate::invitations::domain::repository::InvitationRepository;
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use imphnen_utils::errors::AppError;
|
||||
use sqlx::{FromRow, PgPool};
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(FromRow)]
|
||||
struct InvitationRow {
|
||||
id: Uuid,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
invitee_email: String,
|
||||
status: String,
|
||||
created_at: Option<DateTime<Utc>>,
|
||||
id: Uuid,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
invitee_email: String,
|
||||
status: String,
|
||||
created_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl From<InvitationRow> for InvitationEntity {
|
||||
fn from(r: InvitationRow) -> Self {
|
||||
Self {
|
||||
id: r.id,
|
||||
team_id: r.team_id,
|
||||
inviter_id: r.inviter_id,
|
||||
invitee_email: r.invitee_email,
|
||||
status: r.status,
|
||||
created_at: r.created_at,
|
||||
}
|
||||
}
|
||||
fn from(r: InvitationRow) -> Self {
|
||||
Self {
|
||||
id: r.id,
|
||||
team_id: r.team_id,
|
||||
inviter_id: r.inviter_id,
|
||||
invitee_email: r.invitee_email,
|
||||
status: r.status,
|
||||
created_at: r.created_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(FromRow)]
|
||||
struct InvitationDetailsRow {
|
||||
id: Uuid,
|
||||
team_id: Uuid,
|
||||
team_name: String,
|
||||
inviter_id: Uuid,
|
||||
inviter_fullname: String,
|
||||
invitee_email: String,
|
||||
status: String,
|
||||
created_at: Option<DateTime<Utc>>,
|
||||
id: Uuid,
|
||||
team_id: Uuid,
|
||||
team_name: String,
|
||||
inviter_id: Uuid,
|
||||
inviter_fullname: String,
|
||||
invitee_email: String,
|
||||
status: String,
|
||||
created_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl From<InvitationDetailsRow> for InvitationWithDetails {
|
||||
fn from(r: InvitationDetailsRow) -> Self {
|
||||
Self {
|
||||
id: r.id,
|
||||
team_id: r.team_id,
|
||||
team_name: r.team_name,
|
||||
inviter_id: r.inviter_id,
|
||||
inviter_fullname: r.inviter_fullname,
|
||||
invitee_email: r.invitee_email,
|
||||
status: r.status,
|
||||
created_at: r.created_at,
|
||||
}
|
||||
}
|
||||
fn from(r: InvitationDetailsRow) -> Self {
|
||||
Self {
|
||||
id: r.id,
|
||||
team_id: r.team_id,
|
||||
team_name: r.team_name,
|
||||
inviter_id: r.inviter_id,
|
||||
inviter_fullname: r.inviter_fullname,
|
||||
invitee_email: r.invitee_email,
|
||||
status: r.status,
|
||||
created_at: r.created_at,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PostgresInvitationRepository {
|
||||
pool: Arc<PgPool>,
|
||||
pool: Arc<PgPool>,
|
||||
}
|
||||
|
||||
impl PostgresInvitationRepository {
|
||||
pub fn new(pool: Arc<PgPool>) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
pub fn new(pool: Arc<PgPool>) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl InvitationRepository for PostgresInvitationRepository {
|
||||
async fn create(&self, invitation_id: Uuid, team_id: Uuid, inviter_id: Uuid, invitee_email: &str) -> Result<InvitationEntity, AppError> {
|
||||
let row: InvitationRow = sqlx::query_as(
|
||||
async fn create(
|
||||
&self,
|
||||
invitation_id: Uuid,
|
||||
team_id: Uuid,
|
||||
inviter_id: Uuid,
|
||||
invitee_email: &str,
|
||||
) -> Result<InvitationEntity, AppError> {
|
||||
let row: InvitationRow = sqlx::query_as(
|
||||
"INSERT INTO hackathon_team_invitations (id, team_id, inviter_id, invitee_email, status, created_at) VALUES ($1, $2, $3, $4, 'pending', NOW()) RETURNING id, team_id, inviter_id, invitee_email, status, created_at"
|
||||
)
|
||||
.bind(invitation_id).bind(team_id).bind(inviter_id).bind(invitee_email)
|
||||
.fetch_one(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||
Ok(row.into())
|
||||
}
|
||||
Ok(row.into())
|
||||
}
|
||||
|
||||
async fn find_by_id(&self, id: Uuid) -> Result<Option<InvitationEntity>, AppError> {
|
||||
let row: Option<InvitationRow> = sqlx::query_as(
|
||||
async fn find_by_id(
|
||||
&self,
|
||||
id: Uuid,
|
||||
) -> Result<Option<InvitationEntity>, AppError> {
|
||||
let row: Option<InvitationRow> = sqlx::query_as(
|
||||
"SELECT id, team_id, inviter_id, invitee_email, status, created_at FROM hackathon_team_invitations WHERE id = $1"
|
||||
)
|
||||
.bind(id).fetch_optional(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||
Ok(row.map(Into::into))
|
||||
}
|
||||
Ok(row.map(Into::into))
|
||||
}
|
||||
|
||||
async fn find_pending_by_email(&self, email: &str) -> Result<Vec<InvitationWithDetails>, AppError> {
|
||||
let rows: Vec<InvitationDetailsRow> = sqlx::query_as(
|
||||
async fn find_pending_by_email(
|
||||
&self,
|
||||
email: &str,
|
||||
) -> Result<Vec<InvitationWithDetails>, AppError> {
|
||||
let rows: Vec<InvitationDetailsRow> = sqlx::query_as(
|
||||
"SELECT i.id, i.team_id, t.name AS team_name, i.inviter_id, u.fullname AS inviter_fullname, i.invitee_email, i.status, i.created_at FROM hackathon_team_invitations i JOIN hackathon_teams t ON t.id = i.team_id JOIN hackathon_users u ON u.id = i.inviter_id WHERE i.invitee_email = $1 AND i.status = 'pending'"
|
||||
)
|
||||
.bind(email).fetch_all(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||
Ok(rows.into_iter().map(Into::into).collect())
|
||||
}
|
||||
Ok(rows.into_iter().map(Into::into).collect())
|
||||
}
|
||||
|
||||
async fn update_status(&self, id: Uuid, status: &str) -> Result<(), AppError> {
|
||||
sqlx::query("UPDATE hackathon_team_invitations SET status = $1 WHERE id = $2")
|
||||
.bind(status).bind(id)
|
||||
.execute(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
async fn update_status(&self, id: Uuid, status: &str) -> Result<(), AppError> {
|
||||
sqlx::query("UPDATE hackathon_team_invitations SET status = $1 WHERE id = $2")
|
||||
.bind(status)
|
||||
.bind(id)
|
||||
.execute(self.pool.as_ref())
|
||||
.await
|
||||
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn reject_pending_for_email_except(&self, email: &str, except_id: Uuid) -> Result<(), AppError> {
|
||||
sqlx::query("UPDATE hackathon_team_invitations SET status = 'rejected' WHERE invitee_email = $1 AND status = 'pending' AND id != $2")
|
||||
async fn reject_pending_for_email_except(
|
||||
&self,
|
||||
email: &str,
|
||||
except_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
sqlx::query("UPDATE hackathon_team_invitations SET status = 'rejected' WHERE invitee_email = $1 AND status = 'pending' AND id != $2")
|
||||
.bind(email).bind(except_id)
|
||||
.execute(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_team_member(&self, team_id: Uuid, user_id: Uuid) -> Result<(), AppError> {
|
||||
sqlx::query("INSERT INTO hackathon_team_members (id, team_id, user_id, role, status, joined_at) VALUES ($1, $2, $3, 'member', 'active', NOW())")
|
||||
async fn add_team_member(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
user_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
sqlx::query("INSERT INTO hackathon_team_members (id, team_id, user_id, role, status, joined_at) VALUES ($1, $2, $3, 'member', 'active', NOW())")
|
||||
.bind(Uuid::new_v4()).bind(team_id).bind(user_id)
|
||||
.execute(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn reject_pending_join_requests_for_user(&self, user_id: Uuid) -> Result<(), AppError> {
|
||||
sqlx::query("UPDATE hackathon_team_join_requests SET status = 'rejected' WHERE user_id = $1 AND status = 'pending'")
|
||||
async fn reject_pending_join_requests_for_user(
|
||||
&self,
|
||||
user_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
sqlx::query("UPDATE hackathon_team_join_requests SET status = 'rejected' WHERE user_id = $1 AND status = 'pending'")
|
||||
.bind(user_id)
|
||||
.execute(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_team_leader_id(&self, team_id: Uuid) -> Result<Option<Uuid>, AppError> {
|
||||
sqlx::query_scalar("SELECT leader_id FROM hackathon_teams WHERE id = $1")
|
||||
.bind(team_id).fetch_optional(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
async fn get_team_leader_id(
|
||||
&self,
|
||||
team_id: Uuid,
|
||||
) -> Result<Option<Uuid>, AppError> {
|
||||
sqlx::query_scalar("SELECT leader_id FROM hackathon_teams WHERE id = $1")
|
||||
.bind(team_id)
|
||||
.fetch_optional(self.pool.as_ref())
|
||||
.await
|
||||
.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
|
||||
async fn get_team_name(&self, team_id: Uuid) -> Result<Option<String>, AppError> {
|
||||
sqlx::query_scalar("SELECT name FROM hackathon_teams WHERE id = $1")
|
||||
.bind(team_id).fetch_optional(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
async fn get_team_name(&self, team_id: Uuid) -> Result<Option<String>, AppError> {
|
||||
sqlx::query_scalar("SELECT name FROM hackathon_teams WHERE id = $1")
|
||||
.bind(team_id)
|
||||
.fetch_optional(self.pool.as_ref())
|
||||
.await
|
||||
.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
|
||||
async fn get_user_email(&self, user_id: Uuid) -> Result<Option<String>, AppError> {
|
||||
sqlx::query_scalar("SELECT email FROM hackathon_users WHERE id = $1")
|
||||
.bind(user_id).fetch_optional(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
async fn get_user_email(&self, user_id: Uuid) -> Result<Option<String>, AppError> {
|
||||
sqlx::query_scalar("SELECT email FROM hackathon_users WHERE id = $1")
|
||||
.bind(user_id)
|
||||
.fetch_optional(self.pool.as_ref())
|
||||
.await
|
||||
.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
|
||||
async fn get_inviter_name(&self, user_id: Uuid) -> Result<Option<String>, AppError> {
|
||||
sqlx::query_scalar("SELECT fullname FROM hackathon_users WHERE id = $1")
|
||||
.bind(user_id).fetch_optional(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
async fn get_inviter_name(
|
||||
&self,
|
||||
user_id: Uuid,
|
||||
) -> Result<Option<String>, AppError> {
|
||||
sqlx::query_scalar("SELECT fullname FROM hackathon_users WHERE id = $1")
|
||||
.bind(user_id)
|
||||
.fetch_optional(self.pool.as_ref())
|
||||
.await
|
||||
.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
|
||||
async fn active_member_count(&self, team_id: Uuid) -> Result<i64, AppError> {
|
||||
sqlx::query_scalar("SELECT COUNT(*) FROM hackathon_team_members WHERE team_id = $1 AND status = 'active'")
|
||||
async fn active_member_count(&self, team_id: Uuid) -> Result<i64, AppError> {
|
||||
sqlx::query_scalar("SELECT COUNT(*) FROM hackathon_team_members WHERE team_id = $1 AND status = 'active'")
|
||||
.bind(team_id).fetch_one(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
async fn team_has_submission(&self, team_id: Uuid) -> Result<bool, AppError> {
|
||||
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM hackathon_project_submissions WHERE team_id = $1)")
|
||||
.bind(team_id).fetch_one(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
async fn team_has_submission(&self, team_id: Uuid) -> Result<bool, AppError> {
|
||||
sqlx::query_scalar(
|
||||
"SELECT EXISTS(SELECT 1 FROM hackathon_project_submissions WHERE team_id = $1)",
|
||||
)
|
||||
.bind(team_id)
|
||||
.fetch_one(self.pool.as_ref())
|
||||
.await
|
||||
.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
|
||||
async fn user_active_team_name(&self, user_id: Uuid) -> Result<Option<String>, AppError> {
|
||||
sqlx::query_scalar("SELECT t.name FROM hackathon_teams t JOIN hackathon_team_members m ON m.team_id = t.id WHERE m.user_id = $1 AND m.status = 'active' LIMIT 1")
|
||||
async fn user_active_team_name(
|
||||
&self,
|
||||
user_id: Uuid,
|
||||
) -> Result<Option<String>, AppError> {
|
||||
sqlx::query_scalar("SELECT t.name FROM hackathon_teams t JOIN hackathon_team_members m ON m.team_id = t.id WHERE m.user_id = $1 AND m.status = 'active' LIMIT 1")
|
||||
.bind(user_id).fetch_optional(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub mod domain;
|
||||
pub mod application;
|
||||
pub mod domain;
|
||||
pub mod infrastructure;
|
||||
|
||||
pub use infrastructure::http::routes::build_invitation_routes;
|
||||
|
||||
Reference in New Issue
Block a user