feat: Add seed_test_data script to populate initial test data for events, testimonials, hackathons, and mentors

This commit is contained in:
MythEclipse
2025-10-08 23:03:00 +07:00
parent 8c2527d61e
commit 466ba3391a
14 changed files with 395 additions and 54 deletions
Generated
+1
View File
@@ -2038,6 +2038,7 @@ dependencies = [
"imphnen-cms",
"imphnen-dimentorin",
"imphnen-entities",
"imphnen-gacha",
"imphnen-gateway",
"imphnen-hackathon",
"imphnen-iam",
+5
View File
@@ -47,6 +47,10 @@ path = "src/bin/seed_teams.rs"
name = "seed_hackathons"
path = "src/bin/seed_hackathons.rs"
[[bin]]
name = "seed_test_data"
path = "src/bin/seed_test_data.rs"
[dependencies]
imphnen-libs.workspace = true
imphnen-utils.workspace = true
@@ -54,6 +58,7 @@ imphnen-gateway.workspace = true
imphnen-entities.workspace = true
imphnen-iam.workspace = true
imphnen-cms.workspace = true
imphnen-gacha.workspace = true
imphnen-dimentorin.workspace = true
imphnen-hackathon.workspace = true
axum.workspace = true
+4 -5
View File
@@ -18,13 +18,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
.await?;
db.query("DELETE type::thing('app_gacha_items', $id)")
.bind(("id", "gacha_item_test_id"))
.bind(("id", "1"))
.await?;
db.query("DELETE type::thing('app_gacha_rolls', $id)")
.bind(("id", "gacha_roll_test_id"))
.bind(("id", "test-gacha-roll-001"))
.await?;
let gacha_item_id = "gacha_item_test_id";
let gacha_item_id = "1";
db.query("CREATE type::thing('app_gacha_items', $id) SET name = $name, image_url = $image_url, is_deleted = $is_deleted, created_at = $created_at, updated_at = $updated_at")
.bind(("id", gacha_item_id))
.bind(("name", "Test Gacha Item"))
@@ -35,7 +34,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.await?;
println!("Gacha Item seeded successfully!");
let gacha_roll_id = "gacha_roll_test_id";
let gacha_roll_id = "test-gacha-roll-001";
db.query("CREATE type::thing('app_gacha_rolls', $id) SET item = $item, quantity = $quantity, weight = $weight, is_deleted = $is_deleted, created_at = $created_at, updated_at = $updated_at")
.bind(("id", gacha_roll_id))
.bind(("item", Thing::from(("app_gacha_items", gacha_item_id))))
+175
View File
@@ -0,0 +1,175 @@
use imphnen_cms::v1::landing::events::events_schema::EventsSchema;
use imphnen_cms::v1::landing::testimonials::testimonials_schema::TestimonialsSchema;
use imphnen_dimentorin::v1::mentors::mentors_schema::MentorSchema;
use imphnen_dimentorin::v1::mentors::mentors_dto::MentoringRate;
use imphnen_hackathon::v1::hackathon::hackathon_schema::{
HackathonSchema, HackathonEventsSchema, HackathonTimelineSchema,
HackathonStatus, HackathonEventType, HackathonPhase
};
use imphnen_utils::get_iso_date;
use std::error::Error;
use surrealdb::{opt::auth::Root, sql::Thing};
use chrono::Utc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let env = &imphnen_libs::environment::ENV;
use surrealdb::engine::any;
let db = any::connect(&env.surrealdb_url).await?;
db.signin(Root {
username: &env.surrealdb_username,
password: &env.surrealdb_password,
})
.await?;
db.use_ns(env.surrealdb_namespace.clone())
.use_db(env.surrealdb_dbname.clone())
.await?;
// Seed Events - handle existing data
let event = EventsSchema {
id: Thing::from(("app_events", "1")),
name: "Test Event".to_string(),
description: "Test event description".to_string(),
detail_link: "https://example.com/event".to_string(),
price: 50.0,
is_online: true,
start_date: get_iso_date(),
end_date: get_iso_date(),
location: None,
is_deleted: false,
created_at: get_iso_date(),
updated_at: get_iso_date(),
};
match db.create::<Option<EventsSchema>>(("app_events", "1"))
.content(event)
.await {
Ok(_) => println!("✅ Inserted test event"),
Err(_) => println!("⚠️ Test event already exists, skipping"),
};
// Seed Testimonials - handle existing data
let testimonial = TestimonialsSchema {
id: Thing::from(("app_testimonials", "1")),
user: Thing::from(("app_users", "c3b1d6a8-8d4f-4b36-b789-2e532ec7a7b2")),
role: "Student".to_string(),
content: "This is a great platform!".to_string(),
is_deleted: false,
created_at: get_iso_date(),
updated_at: get_iso_date(),
};
match db.create::<Option<TestimonialsSchema>>(("app_testimonials", "1"))
.content(testimonial)
.await {
Ok(_) => println!("✅ Inserted test testimonial"),
Err(_) => println!("⚠️ Test testimonial already exists, skipping"),
};
// Seed Hackathon - handle existing data
let hackathon = HackathonSchema {
id: Thing::from(("app_hackathons", "1")),
name: "Test Hackathon".to_string(),
description: "Test hackathon description".to_string(),
start_date: Utc::now() + chrono::Duration::days(30),
end_date: Utc::now() + chrono::Duration::days(37),
registration_deadline: Utc::now() + chrono::Duration::days(25),
max_participants: Some(100),
status: HackathonStatus::Draft,
theme: Some("Technology".to_string()),
rules: Some("Follow the rules".to_string()),
prizes: Some(vec![]),
previous_winners: Some(vec![]),
organizers: vec!["c3b1d6a8-8d4f-4b36-b789-2e532ec7a7b2".to_string()],
is_deleted: false,
created_at: Some(get_iso_date()),
updated_at: Some(get_iso_date()),
};
match db.create::<Option<HackathonSchema>>(("app_hackathons", "1"))
.content(hackathon)
.await {
Ok(_) => println!("✅ Inserted test hackathon"),
Err(_) => println!("⚠️ Test hackathon already exists, skipping"),
};
// Seed Hackathon Event
let hackathon_event = HackathonEventsSchema {
id: Thing::from(("app_hackathon_events", "test-event-001")),
hackathon_id: Thing::from(("app_hackathons", "1")),
title: "Test Event".to_string(),
description: Some("Test hackathon event description".to_string()),
event_type: HackathonEventType::Workshop,
start_time: Utc::now() + chrono::Duration::days(30),
end_time: Utc::now() + chrono::Duration::days(30) + chrono::Duration::hours(6),
location: Some("Online".to_string()),
virtual_link: None,
max_attendees: Some(50),
is_mandatory: false,
is_deleted: false,
created_at: Some(get_iso_date()),
updated_at: Some(get_iso_date()),
};
// Try to create hackathon event, skip if already exists
match db.create::<Option<HackathonEventsSchema>>(("app_hackathon_events", "test-event-001"))
.content(hackathon_event)
.await {
Ok(_) => println!("✅ Inserted test hackathon event"),
Err(_) => println!("⚠️ Test hackathon event already exists, skipping"),
};
// Seed Hackathon Timeline
let hackathon_timeline = HackathonTimelineSchema {
id: Thing::from(("app_hackathon_timeline", "test-timeline-001")),
hackathon_id: Thing::from(("app_hackathons", "1")),
phase: HackathonPhase::Registration,
title: "Test Timeline".to_string(),
description: Some("Test hackathon timeline description".to_string()),
start_date: Utc::now(),
end_date: Utc::now() + chrono::Duration::days(7),
is_active: true,
order: 1,
is_deleted: false,
created_at: Some(get_iso_date()),
updated_at: Some(get_iso_date()),
};
// Try to create hackathon timeline, skip if already exists
match db.create::<Option<HackathonTimelineSchema>>(("app_hackathon_timeline", "test-timeline-001"))
.content(hackathon_timeline)
.await {
Ok(_) => println!("✅ Inserted test hackathon timeline"),
Err(_) => println!("⚠️ Test hackathon timeline already exists, skipping"),
};
// Seed Mentor - handle existing data
let mentor = MentorSchema {
id: Thing::from(("app_mentors", "e6f78d23-83bf-5c2b-bcd4-001345678901")),
user_id: Some(Thing::from(("app_users", "e6f78d23-83bf-5c2b-bcd4-001345678901"))),
industries: vec!["Technology".to_string(), "Education".to_string()],
expertise: vec!["Software Development".to_string()],
languages: vec!["English".to_string(), "Indonesian".to_string()],
current_company: "Tech Corp".to_string(),
current_role: "Senior Engineer".to_string(),
years_of_experience: 5,
topics_of_interest: vec!["Rust".to_string(), "Web Development".to_string()],
preferred_mentee_level: vec!["Beginner".to_string()],
preferred_mentoring_formats: vec!["1:1".to_string(), "Group".to_string()],
availability_commitment: "Weekly".to_string(),
mentoring_rate: MentoringRate {
amount: 100,
currency: "IDR".to_string(),
per_duration: "hour".to_string(),
},
status: "active".to_string(),
is_deleted: false,
created_at: get_iso_date(),
updated_at: get_iso_date(),
};
match db.create::<Option<MentorSchema>>(("app_mentors", "e6f78d23-83bf-5c2b-bcd4-001345678901"))
.content(mentor)
.await {
Ok(_) => println!("✅ Inserted test mentor"),
Err(_) => println!("⚠️ Test mentor already exists, skipping"),
};
println!("✅ All test data seeded successfully");
Ok(())
}
+18
View File
@@ -36,6 +36,24 @@ async fn main() -> Result<(), Box<dyn Error>> {
"User",
"5713cb37-dc02-4e87-8048-d7a41d352059",
),
(
"testuser1-id",
"testuser1@example.com",
"Test User 1",
"5713cb37-dc02-4e87-8048-d7a41d352059",
),
(
"testuser2-id",
"testuser2@example.com",
"Test User 2",
"5713cb37-dc02-4e87-8048-d7a41d352059",
),
(
"testuser3-id",
"testuser3@example.com",
"Test User 3",
"5713cb37-dc02-4e87-8048-d7a41d352059",
),
];
for (id, email, fullname, role_id) in users {
+1
View File
@@ -23,6 +23,7 @@ fn main() -> Result<(), Box<dyn Error>> {
run_seed("seed_hackathons")?;
run_seed("seed_gacha_rolls")?;
run_seed("seed_mentor_user")?;
run_seed("seed_test_data")?;
println!("\n✅ All seeding completed successfully.");
Ok(())
}
@@ -123,8 +123,18 @@ impl<'a> MentorsRepository<'a> {
) -> Result<MentorDetailWithUserDto> {
let now = Instant::now();
let db = &self.state.surrealdb_ws;
let mut builder = DetailQueryBuilder::new(ResourceEnum::Mentors.to_string())
.with_id(get_id(id)?.1)
// Validate ID format first
let mentor_id = match get_id(id) {
Ok((_, id_str)) => id_str,
Err(_) => bail!("Invalid mentor ID format"),
};
let mentors_table = ResourceEnum::Mentors.to_string();
// Build query with proper ID binding
let mut builder = DetailQueryBuilder::new(mentors_table.clone())
.with_id(mentor_id) // Use the extracted ID string
.with_select_fields(vec![
"id",
"user_id",
@@ -152,20 +162,23 @@ impl<'a> MentorsRepository<'a> {
let sql = builder.build();
info!(query = %sql, "Executing SurrealDB query in query_mentor_by_id");
let mentor_opt: Option<MentorDetailWithUserDto> =
builder.apply_bindings(db.query(sql)).await?.take(0)?;
let mentor_opt: Option<MentorDetailWithUserDto> = builder
.apply_bindings(db.query(sql))
.await?
.take(0)?;
let elapsed = now.elapsed();
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
== "development"
{
println!("Query 'query_mentor_by_id' took: {elapsed:.2?}");
}
let Some(mentor) = mentor_opt else {
bail!("Mentor not found in database");
bail!("Mentor not found");
};
if mentor.is_deleted && !include_deleted {
bail!("Mentor has been deleted");
}
Ok(mentor)
}
@@ -59,7 +59,12 @@ impl GachaClaimSchema {
&Uuid::new_v4().to_string(),
),
user: user_id,
item: roll.item.id.clone(),
// roll.item is optional at the DTO level; assume caller ensured a valid item exists
item: roll
.item
.as_ref()
.map(|i| i.id.clone())
.unwrap_or_else(|| make_thing(&ResourceEnum::GachaItems.to_string(), &Uuid::new_v4().to_string())),
..Default::default()
}
}
@@ -29,7 +29,17 @@ impl GachaRollItemDto {
pub fn from(dto: &GachaRollQueryDto) -> Self {
Self {
id: dto.id.id.to_raw(),
item: GachaItemDto::from(dto.item.clone()),
// Handle case where item might be missing
item: match &dto.item {
Some(item) => GachaItemDto::from(item.clone()),
None => GachaItemDto {
id: "".to_string(),
name: "Unknown".to_string(),
is_deleted: false,
created_at: None,
updated_at: None,
}
},
weight: dto.weight,
quantity: dto.quantity,
is_deleted: dto.is_deleted,
@@ -42,7 +52,8 @@ impl GachaRollItemDto {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GachaRollQueryDto {
pub id: Thing,
pub item: GachaItemSchema,
// item can be missing in the DB (during partial queries); make optional to allow graceful handling
pub item: Option<GachaItemSchema>,
pub weight: f32,
pub quantity: i32,
pub is_deleted: bool,
@@ -9,7 +9,6 @@ use anyhow::{Result, bail};
use rand::prelude::*;
use imphnen_utils::get_iso_date;
use rand_distr::weighted::WeightedIndex;
use serde_json::{Map, Value};
use std::time::Instant;
use tracing::instrument;
@@ -81,36 +80,68 @@ impl<'a> GachaRollRepository<'a> {
let now = Instant::now();
let db = &self.state.surrealdb_ws;
let table_name = ResourceEnum::GachaRolls.to_string();
let sql =
format!("SELECT * FROM {table_name} WHERE is_deleted = false FETCH item");
info!(query = %sql, "Executing SurrealDB query");
let result: Vec<GachaRollQueryDto> = db.query(sql).await?.take(0)?;
// Use DetailQueryBuilder to properly fetch related item data
let builder = DetailQueryBuilder::new(table_name)
.with_condition("is_deleted = false AND quantity > 0")
.with_select_fields(vec!["*"])
.with_fetch("item");
let sql = builder.build();
info!(query = %sql, "Executing SurrealDB query for active rolls");
let mut result = builder.apply_bindings(db.query(sql)).await?;
let results = match result.take(0) {
Ok(v) => v,
Err(_) => return Ok(Vec::new()),
};
let elapsed = now.elapsed();
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())
== "development"
{
println!("Query 'query_all_active_rolls' took: {elapsed:.2?}");
}
Ok(result)
Ok(results)
}
#[instrument]
pub fn roll_once(rolls: &[GachaRollQueryDto]) -> Option<GachaRollQueryDto> {
let filtered: Vec<_> = rolls
let filtered: Vec<GachaRollQueryDto> = rolls
.iter()
.filter(|r| !r.is_deleted && r.quantity > 0)
.cloned()
.collect();
let weights: Vec<f32> = filtered
.iter()
.map(|r| r.weight * r.quantity as f32)
.collect();
if weights.iter().all(|&w| w <= 0.0) {
if filtered.is_empty() {
return None;
}
let dist = WeightedIndex::new(&weights).ok()?;
// Simple random selection based on quantity weights
let total_weight: f32 = filtered.iter()
.map(|r| r.weight * r.quantity as f32)
.sum();
if total_weight <= 0.0 {
// Fallback to equal probability if weights are invalid
let mut rng = rand::rngs::ThreadRng::default();
let index = dist.sample(&mut rng);
Some(filtered[index].clone())
let index = rng.random_range(0..filtered.len());
return Some(filtered[index].clone());
}
// Weighted random selection
let mut rng = rand::rngs::ThreadRng::default();
let random_value = rng.random_range(0.0..total_weight);
let mut cumulative_weight = 0.0;
for roll in &filtered {
cumulative_weight += roll.weight * roll.quantity as f32;
if random_value <= cumulative_weight {
return Some(roll.clone());
}
}
// This should rarely happen but provides a fallback
Some(filtered[0].clone())
}
#[instrument(skip(self, id), err)]
@@ -27,6 +27,17 @@ impl<'a> HackathonRepository<'a> {
pub fn new(state: &'a AppState) -> Self {
Self { state }
}
// Normalize an incoming id so callers can pass either the full thing string
// (e.g. "app_hackathons:1") or the raw id ("1"). If the id starts with
// the table prefix ("{table}:") the prefix is stripped.
fn normalize_id(&self, table: &str, id: &str) -> String {
if id.starts_with(&format!("{}:", table)) {
id.splitn(2, ':').nth(1).unwrap_or(id).to_string()
} else {
id.to_string()
}
}
}
// Hackathon CRUD operations
@@ -94,10 +105,12 @@ impl<'a> HackathonRepository<'a> {
let table = ResourceEnum::Hackathons.to_string();
info!(query = %format!("SELECT * FROM {} WHERE id = '{}'", table, id), "Executing SurrealDB query");
let normalized_id = self.normalize_id(&table, &id);
let record: Option<HackathonSchema> = self
.state
.surrealdb_ws
.select((table, id))
.select((table, normalized_id.clone()))
.await?;
match record {
@@ -210,9 +223,11 @@ impl<'a> HackathonRepository<'a> {
]);
info!(query = %format!("UPDATE {} SET is_deleted = true WHERE id = '{}'", table, id), "Executing SurrealDB query");
let normalized_id = self.normalize_id(&table, &id);
info!(query = %format!("UPDATE {} SET is_deleted = true WHERE id = '{}'", table, normalized_id), "Executing SurrealDB query");
let record: Option<HackathonSchema> = self
.state.surrealdb_ws
.update((table, id))
.update((table, normalized_id.clone()))
.merge(serde_json::to_value(updates)?)
.await?;
@@ -230,9 +245,11 @@ impl<'a> HackathonRepository<'a> {
let table = ResourceEnum::HackathonEvents.to_string();
let id = surrealdb::Uuid::new_v4().to_string();
let normalized_hackathon_id = self.normalize_id("app_hackathons", &hackathon_id);
let schema = HackathonEventsSchema {
id: Thing::from((table.clone(), id.clone())),
hackathon_id: Thing::from(("app_hackathons".to_string(), hackathon_id)),
hackathon_id: Thing::from(("app_hackathons".to_string(), normalized_hackathon_id)),
title: event.title,
description: event.description,
event_type: event.event_type,
@@ -264,9 +281,11 @@ impl<'a> HackathonRepository<'a> {
pub async fn list_hackathon_events(&self, meta: imphnen_libs::MetaRequestDto, hackathon_id: String) -> Result<imphnen_libs::ResponseListSuccessDto<Vec<HackathonEventsSchema>>> {
let table = ResourceEnum::HackathonEvents.to_string();
let normalized_hackathon_id = self.normalize_id("app_hackathons", &hackathon_id);
let builder = QueryListBuilder::new(&self.state.surrealdb_ws, &table, &meta)
.with_condition("is_deleted = false")
.with_condition(&format!("hackathon_id = type::thing('app_hackathons', '{}')", hackathon_id))
.with_condition(&format!("hackathon_id = type::thing('app_hackathons', '{}')", normalized_hackathon_id))
.search_field("title")
.select_fields(vec!["*"]);
@@ -360,9 +379,11 @@ impl<'a> HackathonRepository<'a> {
let table = ResourceEnum::HackathonTimeline.to_string();
let id = surrealdb::Uuid::new_v4().to_string();
let normalized_hackathon_id = self.normalize_id("app_hackathons", &hackathon_id);
let schema = HackathonTimelineSchema {
id: Thing::from((table.clone(), id.clone())),
hackathon_id: Thing::from(("app_hackathons".to_string(), hackathon_id)),
hackathon_id: Thing::from(("app_hackathons".to_string(), normalized_hackathon_id)),
phase: timeline.phase,
title: timeline.title,
description: timeline.description,
@@ -392,9 +413,11 @@ impl<'a> HackathonRepository<'a> {
pub async fn list_hackathon_timeline(&self, meta: imphnen_libs::MetaRequestDto, hackathon_id: String) -> Result<imphnen_libs::ResponseListSuccessDto<Vec<HackathonTimelineSchema>>> {
let table = ResourceEnum::HackathonTimeline.to_string();
let normalized_hackathon_id = self.normalize_id("app_hackathons", &hackathon_id);
let builder = QueryListBuilder::new(&self.state.surrealdb_ws, &table, &meta)
.with_condition("is_deleted = false")
.with_condition(&format!("hackathon_id = type::thing('app_hackathons', '{}')", hackathon_id))
.with_condition(&format!("hackathon_id = type::thing('app_hackathons', '{}')", normalized_hackathon_id))
.search_field("title")
.select_fields(vec!["*"]);
@@ -481,10 +504,13 @@ impl<'a> HackathonRepository<'a> {
let table = ResourceEnum::HackathonSubmissions.to_string();
let id = surrealdb::Uuid::new_v4().to_string();
let normalized_hackathon_id = self.normalize_id("app_hackathons", &hackathon_id);
let normalized_team_id = self.normalize_id("app_teams", &team_id);
let schema = HackathonSubmissionsSchema {
id: Thing::from((table.clone(), id.clone())),
hackathon_id: Thing::from(("app_hackathons".to_string(), hackathon_id)),
team_id: Thing::from(("app_teams".to_string(), team_id)),
hackathon_id: Thing::from(("app_hackathons".to_string(), normalized_hackathon_id)),
team_id: Thing::from(("app_teams".to_string(), normalized_team_id)),
project_name: submission.project_name,
description: submission.description,
repository_url: submission.repository_url,
@@ -515,9 +541,11 @@ impl<'a> HackathonRepository<'a> {
pub async fn list_hackathon_submissions(&self, meta: imphnen_libs::MetaRequestDto, hackathon_id: String) -> Result<imphnen_libs::ResponseListSuccessDto<Vec<HackathonSubmissionsSchema>>> {
let table = ResourceEnum::HackathonSubmissions.to_string();
let normalized_hackathon_id = self.normalize_id("app_hackathons", &hackathon_id);
let builder = QueryListBuilder::new(&self.state.surrealdb_ws, &table, &meta)
.with_condition("is_deleted = false")
.with_condition(&format!("hackathon_id = type::thing('app_hackathons', '{}')", hackathon_id))
.with_condition(&format!("hackathon_id = type::thing('app_hackathons', '{}')", normalized_hackathon_id))
.search_field("project_name")
.select_fields(vec!["*"]);
@@ -1,5 +1,7 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, Deserializer};
use std::str::FromStr;
use serde::de;
use surrealdb::sql::Thing;
use imphnen_utils::make_thing;
@@ -104,7 +106,38 @@ pub enum HackathonStatus {
Cancelled,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, utoipa::ToSchema, strum::Display)]
#[derive(Clone, Debug, Serialize, PartialEq, utoipa::ToSchema, strum::Display)]
pub enum HackathonPhase {
Registration,
Ideation,
Development,
Submission,
Judging,
Awards,
}
// Manual Deserialize implementation for case-insensitive support
impl<'de> Deserialize<'de> for HackathonPhase {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let normalized = s.to_lowercase();
match normalized.as_str() {
"registration" => Ok(HackathonPhase::Registration),
"ideation" => Ok(HackathonPhase::Ideation),
"development" => Ok(HackathonPhase::Development),
"submission" => Ok(HackathonPhase::Submission),
"judging" => Ok(HackathonPhase::Judging),
"awards" => Ok(HackathonPhase::Awards),
_ => Err(serde::de::Error::custom(format!("Invalid HackathonPhase: {}", s)))
}
}
}
#[derive(Clone, Debug, Serialize, PartialEq, utoipa::ToSchema, strum::Display)]
pub enum HackathonEventType {
Workshop,
Keynote,
@@ -114,14 +147,31 @@ pub enum HackathonEventType {
Other,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, utoipa::ToSchema, strum::Display)]
pub enum HackathonPhase {
Registration,
Ideation,
Development,
Submission,
Judging,
Awards,
// Implement case-insensitive string parsing for HackathonEventType
impl FromStr for HackathonEventType {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"workshop" => Ok(Self::Workshop),
"keynote" => Ok(Self::Keynote),
"networking" => Ok(Self::Networking),
"judging" => Ok(Self::Judging),
"ceremony" => Ok(Self::Ceremony),
"other" => Ok(Self::Other),
_ => Err(format!("Invalid HackathonEventType: {}", s)),
}
}
}
// Manual Deserialize implementation for case-insensitive support
impl<'de> Deserialize<'de> for HackathonEventType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Self::from_str(&s).map_err(de::Error::custom)
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, utoipa::ToSchema, strum::Display)]
+2 -2
View File
@@ -219,8 +219,8 @@ impl<'a> TeamsRepository<'a> {
let now = Instant::now();
let db = &self.state.surrealdb_ws;
let sql = format!(
"SELECT team.* FROM {} AS membership
INNER JOIN {} AS team ON membership.team_id = team.id
"SELECT team.* FROM {} membership
INNER JOIN {} team ON membership.team_id = team.id
WHERE membership.user_id = $user_id
AND membership.is_active = true
AND team.is_deleted = false
+6 -2
View File
@@ -87,8 +87,12 @@ impl<'a> UsersRepository<'a> {
.with_fetch("role")
.with_fetch("role.permissions");
let sql = builder.build();
let user_opt: Option<UsersDetailQueryDto> =
builder.apply_bindings(db.query(sql)).await?.take(0)?;
// Some SurrealDB queries may return multiple rows (e.g., duplicates).
// Safely take all rows and pick the first valid user (not deleted and with a valid role).
let rows: Vec<UsersDetailQueryDto> = builder.apply_bindings(db.query(sql)).await?.take(0)?;
let user_opt: Option<UsersDetailQueryDto> = rows
.into_iter()
.find(|u| !u.is_deleted && !u.role.is_deleted && u.role.updated_at.is_some());
let elapsed = now.elapsed();
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string())