use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
#[cfg(feature = "validation")]
use validator::Validate;
use crate::domain::entity::PosManagerPin;
use crate::domain::entity::AuditMetadata;
const NEVER_VERIFIES: &str = "(set via the guarded set-pin verb)";
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreatePosManagerPinDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "employee_party_id")]
pub employee_party_id: Uuid,
}
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdatePosManagerPinDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(alias = "employee_party_id")]
pub employee_party_id: Uuid,
}
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchPosManagerPinDto {
#[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
#[serde(skip_serializing_if = "Option::is_none", alias = "employee_party_id")]
pub employee_party_id: Option<Uuid>,
}
impl PatchPosManagerPinDto {
pub fn has_changes(&self) -> bool {
self.employee_party_id.is_some()
}
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PosManagerPinResponseDto {
#[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 employee_party_id: Uuid,
#[cfg_attr(feature = "openapi", schema(example = 42))]
pub failed_attempts: i32,
pub locked_until: Option<DateTime<Utc>>,
pub last_attempt_at: Option<DateTime<Utc>>,
pub metadata: AuditMetadata,
}
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct PosManagerPinListResponseDto {
pub items: Vec<PosManagerPinResponseDto>,
pub total: u64,
pub page: u32,
pub per_page: u32,
pub total_pages: u32,
pub has_next: bool,
pub has_prev: bool,
}
impl PosManagerPinListResponseDto {
pub fn new(items: Vec<PosManagerPinResponseDto>, 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 PosManagerPinSummaryDto {
pub id: Uuid,
pub employee_party_id: Uuid,
pub created_at: Option<DateTime<Utc>>,
}
impl From<PosManagerPin> for PosManagerPinResponseDto {
fn from(entity: PosManagerPin) -> Self {
Self {
id: entity.id,
employee_party_id: entity.employee_party_id,
failed_attempts: entity.failed_attempts,
locked_until: entity.locked_until,
last_attempt_at: entity.last_attempt_at,
metadata: entity.metadata,
}
}
}
impl From<PosManagerPin> for PosManagerPinSummaryDto {
fn from(entity: PosManagerPin) -> Self {
let created_at = backbone_core::PersistentEntity::created_at(&entity);
Self {
id: entity.id,
employee_party_id: entity.employee_party_id,
created_at,
}
}
}
impl From<CreatePosManagerPinDto> for PosManagerPin {
fn from(dto: CreatePosManagerPinDto) -> Self {
Self {
id: Uuid::new_v4(),
employee_party_id: dto.employee_party_id,
pin_hash: NEVER_VERIFIES.to_string(),
failed_attempts: 0,
locked_until: None,
last_attempt_at: None,
last_attempt_ip: None,
metadata: AuditMetadata::default(),
}
}
}
impl From<&PosManagerPin> for PosManagerPinResponseDto {
fn from(entity: &PosManagerPin) -> Self {
Self {
id: entity.id,
employee_party_id: entity.employee_party_id,
failed_attempts: entity.failed_attempts,
locked_until: entity.locked_until,
last_attempt_at: entity.last_attempt_at,
metadata: entity.metadata.clone(),
}
}
}
impl backbone_core::FromCreateDto<CreatePosManagerPinDto> for PosManagerPin {
fn from_create_dto(dto: CreatePosManagerPinDto) -> backbone_core::ServiceResult<Self> {
Ok(PosManagerPin::from(dto))
}
}
impl backbone_core::ApplyUpdateDto<UpdatePosManagerPinDto> for PosManagerPin {
fn apply_update(mut self, dto: UpdatePosManagerPinDto) -> backbone_core::ServiceResult<Self> {
self.employee_party_id = dto.employee_party_id;
Ok(self)
}
}