use uuid::Uuid;
use backbone_core::GenericCrudService;
use crate::domain::entity::Registration;
use crate::infrastructure::persistence::seat_repository::{
RegisterCommand, RegistrationRow, SeatRepository,
};
use crate::presentation::dto::{CreateRegistrationDto, UpdateRegistrationDto};
pub type RegistrationService = GenericCrudService<
Registration,
CreateRegistrationDto,
UpdateRegistrationDto,
RegistrationRepositoryAlias,
>;
use super::event_error::EventResult;
type RegistrationRepositoryAlias = crate::infrastructure::persistence::RegistrationRepository;
pub struct RegistrationCommandService {
seats: SeatRepository,
}
impl RegistrationCommandService {
pub fn new(seats: SeatRepository) -> Self {
Self { seats }
}
pub async fn register(&self, cmd: RegisterCommand) -> EventResult<RegistrationRow> {
match self.seats.register(&cmd).await {
Ok(row) => Ok(row),
Err(refusal) => {
crate::infrastructure::persistence::seat_repository::record_audit(
&self.seats.rpool(),
"registration_refused",
cmd.actor,
"event",
cmd.event_id,
serde_json::json!({ "email": cmd.email, "code": refusal.code() }),
)
.await;
Err(refusal)
}
}
}
pub async fn confirm(
&self,
registration_id: Uuid,
actor: Option<Uuid>,
) -> EventResult<RegistrationRow> {
self.transition(registration_id, "open", actor).await
}
pub async fn set_draft(
&self,
registration_id: Uuid,
actor: Option<Uuid>,
) -> EventResult<RegistrationRow> {
self.transition(registration_id, "draft", actor).await
}
pub async fn set_done(
&self,
registration_id: Uuid,
actor: Option<Uuid>,
) -> EventResult<RegistrationRow> {
self.transition(registration_id, "done", actor).await
}
pub async fn cancel(
&self,
registration_id: Uuid,
actor: Option<Uuid>,
) -> EventResult<RegistrationRow> {
self.transition(registration_id, "cancel", actor).await
}
async fn transition(
&self,
registration_id: Uuid,
to: &str,
actor: Option<Uuid>,
) -> EventResult<RegistrationRow> {
self.seats.transition(registration_id, to, actor).await?;
self.seats.find_registration(registration_id).await
}
pub async fn sync_from_partner(
&self,
registration_id: Uuid,
partner_id: Uuid,
name: Option<&str>,
phone: Option<&str>,
company_name: Option<&str>,
actor: Option<Uuid>,
) -> EventResult<RegistrationRow> {
self.seats
.sync_from_partner(registration_id, partner_id, name, phone, company_name, actor)
.await
}
pub async fn get(&self, registration_id: Uuid) -> EventResult<RegistrationRow> {
self.seats.find_registration(registration_id).await
}
pub async fn list(
&self,
event_id: Uuid,
limit: i64,
after: Option<Uuid>,
) -> EventResult<Vec<RegistrationRow>> {
self.seats.list_registrations(event_id, limit, after).await
}
}