use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::IntegrationEvent;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::IntegrationStatus;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateIntegrationEventDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "connector_id")]
pub connector_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "event_type")]
pub event_type: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "external_id")]
pub external_id: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "business_key")]
pub business_key: String,
pub status: IntegrationStatus,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub payload: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "mapped_ref_type")]
pub mapped_ref_type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "mapped_ref_id")]
pub mapped_ref_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "error_detail")]
pub error_detail: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateIntegrationEventDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "connector_id")]
pub connector_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "event_type")]
pub event_type: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "external_id")]
pub external_id: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(alias = "business_key")]
pub business_key: String,
pub status: IntegrationStatus,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub payload: String,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "mapped_ref_type")]
pub mapped_ref_type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "mapped_ref_id")]
pub mapped_ref_id: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none", alias = "error_detail")]
pub error_detail: Option<String>,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchIntegrationEventDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "connector_id")]
pub connector_id: Option<Uuid>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "event_type")]
pub event_type: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "external_id")]
pub external_id: Option<String>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "business_key")]
pub business_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<IntegrationStatus>,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub payload: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "mapped_ref_type")]
pub mapped_ref_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", alias = "mapped_ref_id")]
pub mapped_ref_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none", alias = "error_detail")]
pub error_detail: Option<String>,
}
impl PatchIntegrationEventDto {
pub fn has_changes(&self) -> bool {
self.connector_id.is_some() || self.event_type.is_some() || self.external_id.is_some() || self.business_key.is_some() || self.status.is_some() || self.payload.is_some() || self.mapped_ref_type.is_some() || self.mapped_ref_id.is_some() || self.error_detail.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct IntegrationEventResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub connector_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub event_type: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub external_id: String,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub business_key: String,
pub status: IntegrationStatus,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub payload: String,
pub mapped_ref_type: Option<String>,
pub mapped_ref_id: Option<Uuid>,
pub error_detail: Option<String>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct IntegrationEventListResponseDto {
pub items: Vec<IntegrationEventResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl IntegrationEventListResponseDto {
pub fn new(items: Vec<IntegrationEventResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
let total_pages = if per_page > 0 {
((total as f64) / (per_page as f64)).ceil() as u32
} else {
0
};
Self {
items,
total,
page,
per_page,
total_pages,
has_next: page < total_pages,
has_prev: page > 1,
}
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct IntegrationEventSummaryDto {
pub id: Uuid,
pub connector_id: Uuid,
pub event_type: String,
pub external_id: String,
pub created_at: Option<DateTime<Utc>>,
}
impl From<IntegrationEvent> for IntegrationEventResponseDto {
fn from(entity: IntegrationEvent) -> Self {
Self {
id: entity.id,
connector_id: entity.connector_id,
event_type: entity.event_type,
external_id: entity.external_id,
business_key: entity.business_key,
status: entity.status,
payload: entity.payload,
mapped_ref_type: entity.mapped_ref_type,
mapped_ref_id: entity.mapped_ref_id,
error_detail: entity.error_detail,
metadata: entity.metadata,
}
}
}
impl From<IntegrationEvent> for IntegrationEventSummaryDto {
fn from(entity: IntegrationEvent) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
connector_id: entity.connector_id,
event_type: entity.event_type,
external_id: entity.external_id,
created_at,
}
}
}
impl From<CreateIntegrationEventDto> for IntegrationEvent {
fn from(dto: CreateIntegrationEventDto) -> Self {
Self {
id: Uuid::new_v4(),
connector_id: dto.connector_id,
event_type: dto.event_type,
external_id: dto.external_id,
business_key: dto.business_key,
status: dto.status,
payload: dto.payload,
mapped_ref_type: dto.mapped_ref_type,
mapped_ref_id: dto.mapped_ref_id,
error_detail: dto.error_detail,
metadata: AuditMetadata::default(),
}
}
}
impl From<&IntegrationEvent> for IntegrationEventResponseDto {
fn from(entity: &IntegrationEvent) -> Self {
Self {
id: entity.id.clone(),
connector_id: entity.connector_id.clone(),
event_type: entity.event_type.clone(),
external_id: entity.external_id.clone(),
business_key: entity.business_key.clone(),
status: entity.status.clone(),
payload: entity.payload.clone(),
mapped_ref_type: entity.mapped_ref_type.clone(),
mapped_ref_id: entity.mapped_ref_id.clone(),
error_detail: entity.error_detail.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateIntegrationEventDto> for IntegrationEvent {
fn from_create_dto(dto: CreateIntegrationEventDto) -> backbone_core::ServiceResult<Self> {
Ok(IntegrationEvent::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateIntegrationEventDto> for IntegrationEvent {
fn apply_update(mut self, dto: UpdateIntegrationEventDto) -> backbone_core::ServiceResult<Self> {
self.connector_id = dto.connector_id;
self.event_type = dto.event_type;
self.external_id = dto.external_id;
self.business_key = dto.business_key;
self.status = dto.status;
self.payload = dto.payload;
self.mapped_ref_type = dto.mapped_ref_type;
self.mapped_ref_id = dto.mapped_ref_id;
self.error_detail = dto.error_detail;
Ok(self)
}
}