feat: migrate imphnen-backend-hackathon into workspace as imphnen-hackathon crate

Consolidates the standalone hackathon backend (16 crates) into a single
imphnen-hackathon crate following the existing clean architecture patterns.
All endpoints are exposed under /v1/hackathon/ via the gateway.

Features migrated:
- Auth: Supabase-based signup/login/GitHub OAuth/password reset (own JWT)
- Users: profile management with team listing
- Teams: CRUD with city validation, deadline enforcement, invite system
- Invitations: team member invitations with accept/reject flow
- Join Requests: team join request workflow
- Chat: team messaging with author/leader delete permissions
- Submissions: project submission lifecycle (draft→pending→submitted)
- Storage: Supabase Storage file upload endpoints
- Certificates: public user certificate data endpoint
- Winners: public winners listing
- Admin: admin-only CRUD for all entities

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
maulanasdqn
2026-04-02 15:15:00 +07:00
co-authored by Claude Sonnet 4.6
parent 05a5b39195
commit 11442c6285
119 changed files with 4385 additions and 11 deletions
@@ -0,0 +1,43 @@
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use uuid::Uuid;
use chrono::{DateTime, Utc};
use crate::chat::domain::entity::*;
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct MessageResponse {
pub id: Uuid,
pub team_id: Uuid,
pub user_id: Uuid,
pub user_fullname: String,
pub user_avatar: Option<String>,
pub message: String,
pub created_at: Option<DateTime<Utc>>,
pub updated_at: Option<DateTime<Utc>>,
}
impl From<MessageWithUser> for MessageResponse {
fn from(e: MessageWithUser) -> Self {
Self {
id: e.id,
team_id: e.team_id,
user_id: e.user_id,
user_fullname: e.user_fullname,
user_avatar: e.user_avatar,
message: e.message,
created_at: e.created_at,
updated_at: e.updated_at,
}
}
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct SendMessageRequest {
pub message: String,
}
impl From<SendMessageRequest> for SendMessageInput {
fn from(r: SendMessageRequest) -> Self {
Self { message: r.message }
}
}
@@ -0,0 +1,36 @@
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use std::sync::Arc;
use uuid::Uuid;
use imphnen_utils::{errors::AppError, response_format::{ApiSuccess, ApiMessage}};
use crate::middleware::hackathon_auth::HackathonAuthUser;
use crate::chat::domain::service::ChatService;
use super::dto::*;
pub async fn get_team_messages_handler(
Extension(service): Extension<Arc<dyn ChatService>>,
Extension(auth): Extension<HackathonAuthUser>,
Path(team_id): Path<Uuid>,
) -> Result<axum::response::Response, AppError> {
let messages = service.get_team_messages(team_id, auth.user_id).await?;
let response: Vec<MessageResponse> = messages.into_iter().map(MessageResponse::from).collect();
Ok(ApiSuccess(response).into_response())
}
pub async fn send_message_handler(
Extension(service): Extension<Arc<dyn ChatService>>,
Extension(auth): Extension<HackathonAuthUser>,
Path(team_id): Path<Uuid>,
Json(body): Json<SendMessageRequest>,
) -> Result<axum::response::Response, AppError> {
let message = service.send_message(team_id, auth.user_id, body.into()).await?;
Ok(ApiSuccess(MessageResponse::from(message)).into_response())
}
pub async fn delete_message_handler(
Extension(service): Extension<Arc<dyn ChatService>>,
Extension(auth): Extension<HackathonAuthUser>,
Path(message_id): Path<Uuid>,
) -> Result<axum::response::Response, AppError> {
service.delete_message(message_id, auth.user_id).await?;
Ok(ApiMessage::ok("Message deleted").into_response())
}
@@ -0,0 +1,3 @@
pub mod dto;
pub mod handlers;
pub mod routes;
@@ -0,0 +1,22 @@
use axum::{middleware::from_fn, routing::{delete, get, post}, Extension, Router};
use sqlx::PgPool;
use std::sync::Arc;
use crate::chat::application::chat_service::ChatServiceImpl;
use crate::chat::domain::service::ChatService;
use crate::chat::infrastructure::persistence::PostgresChatRepository;
use crate::common::hackathon_jwt::HackathonJwtService;
use crate::middleware::hackathon_auth::hackathon_auth_middleware;
use super::handlers::*;
pub fn build_chat_routes(pool: Arc<PgPool>, jwt: Arc<HackathonJwtService>) -> Router {
let service: Arc<dyn ChatService> = Arc::new(ChatServiceImpl::new(
Arc::new(PostgresChatRepository::new(pool.clone())),
));
Router::new()
.route("/chat/teams/:team_id", get(get_team_messages_handler).post(send_message_handler))
.route("/chat/messages/:message_id", delete(delete_message_handler))
.layer(Extension(service))
.layer(Extension(jwt.clone()))
.layer(Extension(pool))
.layer(from_fn(hackathon_auth_middleware))
}
@@ -0,0 +1,2 @@
pub mod http;
pub mod persistence;
@@ -0,0 +1,2 @@
pub mod postgres_chat_repository;
pub use postgres_chat_repository::PostgresChatRepository;
@@ -0,0 +1,127 @@
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::chat::domain::entity::*;
use crate::chat::domain::repository::ChatRepository;
#[derive(FromRow)]
struct MessageRow {
id: Uuid,
team_id: Uuid,
user_id: Uuid,
message: String,
created_at: Option<DateTime<Utc>>,
updated_at: Option<DateTime<Utc>>,
}
impl From<MessageRow> for MessageEntity {
fn from(r: MessageRow) -> Self {
Self {
id: r.id,
team_id: r.team_id,
user_id: r.user_id,
message: r.message,
created_at: r.created_at,
updated_at: r.updated_at,
}
}
}
#[derive(FromRow)]
struct MessageWithUserRow {
id: Uuid,
team_id: Uuid,
user_id: Uuid,
user_fullname: String,
user_avatar: Option<String>,
message: String,
created_at: Option<DateTime<Utc>>,
updated_at: Option<DateTime<Utc>>,
}
impl From<MessageWithUserRow> for MessageWithUser {
fn from(r: MessageWithUserRow) -> Self {
Self {
id: r.id,
team_id: r.team_id,
user_id: r.user_id,
user_fullname: r.user_fullname,
user_avatar: r.user_avatar,
message: r.message,
created_at: r.created_at,
updated_at: r.updated_at,
}
}
}
#[derive(FromRow)]
struct UserInfoRow {
fullname: String,
avatar: Option<String>,
}
pub struct PostgresChatRepository {
pool: Arc<PgPool>,
}
impl PostgresChatRepository {
pub fn new(pool: Arc<PgPool>) -> Self {
Self { pool }
}
}
#[async_trait]
impl ChatRepository for PostgresChatRepository {
async fn find_team_messages(&self, team_id: Uuid) -> Result<Vec<MessageWithUser>, AppError> {
let rows: Vec<MessageWithUserRow> = sqlx::query_as(
"SELECT m.id, m.team_id, m.user_id, u.fullname AS user_fullname, u.avatar AS user_avatar, m.message, m.created_at, m.updated_at FROM hackathon_team_messages m JOIN hackathon_users u ON u.id = m.user_id WHERE m.team_id = $1 ORDER BY m.created_at ASC"
)
.bind(team_id).fetch_all(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
Ok(rows.into_iter().map(Into::into).collect())
}
async fn create_message(&self, id: Uuid, team_id: Uuid, user_id: Uuid, message: &str) -> Result<MessageEntity, AppError> {
let now = Utc::now();
let row: MessageRow = sqlx::query_as(
"INSERT INTO hackathon_team_messages (id, team_id, user_id, message, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, team_id, user_id, message, created_at, updated_at"
)
.bind(id).bind(team_id).bind(user_id).bind(message).bind(now).bind(now)
.fetch_one(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
Ok(row.into())
}
async fn find_message_by_id(&self, id: Uuid) -> Result<Option<MessageEntity>, AppError> {
let row: Option<MessageRow> = sqlx::query_as(
"SELECT id, team_id, user_id, message, created_at, updated_at FROM hackathon_team_messages 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))
}
async fn delete_message(&self, id: Uuid) -> Result<bool, AppError> {
let result = sqlx::query("DELETE FROM hackathon_team_messages WHERE id = $1")
.bind(id).execute(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
Ok(result.rows_affected() > 0)
}
async fn get_user_info(&self, user_id: Uuid) -> Result<Option<(String, Option<String>)>, AppError> {
let row: Option<UserInfoRow> = sqlx::query_as(
"SELECT fullname, avatar FROM hackathon_users WHERE id = $1"
)
.bind(user_id).fetch_optional(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))?;
Ok(row.map(|r| (r.fullname, r.avatar)))
}
async fn is_team_member(&self, team_id: Uuid, user_id: Uuid) -> Result<bool, AppError> {
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM hackathon_team_members WHERE team_id = $1 AND user_id = $2 AND status = 'active')")
.bind(team_id).bind(user_id).fetch_one(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))
}
async fn is_team_leader(&self, team_id: Uuid, user_id: Uuid) -> Result<bool, AppError> {
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM hackathon_teams WHERE id = $1 AND leader_id = $2)")
.bind(team_id).bind(user_id).fetch_one(self.pool.as_ref()).await.map_err(|e| AppError::InternalServerError(e.to_string()))
}
}