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::IntegrationConnector;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::ConnectorDirection;
use crate::domain::entity::ConnectorKind;
use crate::domain::entity::ConnectorStatus;
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateIntegrationConnectorDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub provider: String,
pub kind: ConnectorKind,
pub direction: ConnectorDirection,
pub status: ConnectorStatus,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateIntegrationConnectorDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub provider: String,
pub kind: ConnectorKind,
pub direction: ConnectorDirection,
pub status: ConnectorStatus,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchIntegrationConnectorDto {
#[cfg_attr(feature = "openapi", schema(example = "example"))]
#[serde(skip_serializing_if = "Option::is_none")]
pub provider: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub kind: Option<ConnectorKind>,
#[serde(skip_serializing_if = "Option::is_none")]
pub direction: Option<ConnectorDirection>,
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<ConnectorStatus>,
}
impl PatchIntegrationConnectorDto {
pub fn has_changes(&self) -> bool {
self.provider.is_some() || self.kind.is_some() || self.direction.is_some() || self.status.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct IntegrationConnectorResponseDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
pub id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = "example"))]
pub provider: String,
pub kind: ConnectorKind,
pub direction: ConnectorDirection,
pub status: ConnectorStatus,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct IntegrationConnectorListResponseDto {
pub items: Vec<IntegrationConnectorResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl IntegrationConnectorListResponseDto {
pub fn new(items: Vec<IntegrationConnectorResponseDto>, 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 IntegrationConnectorSummaryDto {
pub id: Uuid,
pub provider: String,
pub kind: ConnectorKind,
pub direction: ConnectorDirection,
pub created_at: Option<DateTime<Utc>>,
}
impl From<IntegrationConnector> for IntegrationConnectorResponseDto {
fn from(entity: IntegrationConnector) -> Self {
Self {
id: entity.id,
provider: entity.provider,
kind: entity.kind,
direction: entity.direction,
status: entity.status,
metadata: entity.metadata,
}
}
}
impl From<IntegrationConnector> for IntegrationConnectorSummaryDto {
fn from(entity: IntegrationConnector) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
provider: entity.provider,
kind: entity.kind,
direction: entity.direction,
created_at,
}
}
}
impl From<CreateIntegrationConnectorDto> for IntegrationConnector {
fn from(dto: CreateIntegrationConnectorDto) -> Self {
Self {
id: Uuid::new_v4(),
provider: dto.provider,
kind: dto.kind,
direction: dto.direction,
status: dto.status,
metadata: AuditMetadata::default(),
}
}
}
impl From<&IntegrationConnector> for IntegrationConnectorResponseDto {
fn from(entity: &IntegrationConnector) -> Self {
Self {
id: entity.id.clone(),
provider: entity.provider.clone(),
kind: entity.kind.clone(),
direction: entity.direction.clone(),
status: entity.status.clone(),
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreateIntegrationConnectorDto> for IntegrationConnector {
fn from_create_dto(dto: CreateIntegrationConnectorDto) -> backbone_core::ServiceResult<Self> {
Ok(IntegrationConnector::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdateIntegrationConnectorDto> for IntegrationConnector {
fn apply_update(mut self, dto: UpdateIntegrationConnectorDto) -> backbone_core::ServiceResult<Self> {
self.provider = dto.provider;
self.kind = dto.kind;
self.direction = dto.direction;
self.status = dto.status;
Ok(self)
}
}