use crate::core::pluralize::Pluralizer;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LinkEntity {
pub id: Uuid,
#[serde(rename = "type")]
pub entity_type: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub deleted_at: Option<DateTime<Utc>>,
pub status: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub tenant_id: Option<Uuid>,
pub link_type: String,
pub source_id: Uuid,
pub target_id: Uuid,
pub metadata: Option<serde_json::Value>,
}
impl LinkEntity {
pub fn new(
link_type: impl Into<String>,
source_id: Uuid,
target_id: Uuid,
metadata: Option<serde_json::Value>,
) -> Self {
let now = Utc::now();
Self {
id: Uuid::new_v4(),
entity_type: "link".to_string(),
created_at: now,
updated_at: now,
deleted_at: None,
status: "active".to_string(),
tenant_id: None,
link_type: link_type.into(),
source_id,
target_id,
metadata,
}
}
pub fn new_with_tenant(
tenant_id: Uuid,
link_type: impl Into<String>,
source_id: Uuid,
target_id: Uuid,
metadata: Option<serde_json::Value>,
) -> Self {
let now = Utc::now();
Self {
id: Uuid::new_v4(),
entity_type: "link".to_string(),
created_at: now,
updated_at: now,
deleted_at: None,
status: "active".to_string(),
tenant_id: Some(tenant_id),
link_type: link_type.into(),
source_id,
target_id,
metadata,
}
}
pub fn soft_delete(&mut self) {
self.deleted_at = Some(Utc::now());
self.updated_at = Utc::now();
}
pub fn restore(&mut self) {
self.deleted_at = None;
self.updated_at = Utc::now();
}
pub fn touch(&mut self) {
self.updated_at = Utc::now();
}
pub fn is_deleted(&self) -> bool {
self.deleted_at.is_some()
}
pub fn is_active(&self) -> bool {
self.status == "active" && !self.is_deleted()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LinkAuthConfig {
#[serde(default = "default_link_auth_policy")]
pub list: String,
#[serde(default = "default_link_auth_policy")]
pub get: String,
#[serde(default = "default_link_auth_policy")]
pub create: String,
#[serde(default = "default_link_auth_policy")]
pub update: String,
#[serde(default = "default_link_auth_policy")]
pub delete: String,
}
fn default_link_auth_policy() -> String {
"authenticated".to_string()
}
impl Default for LinkAuthConfig {
fn default() -> Self {
Self {
list: default_link_auth_policy(),
get: default_link_auth_policy(),
create: default_link_auth_policy(),
update: default_link_auth_policy(),
delete: default_link_auth_policy(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LinkDefinition {
pub link_type: String,
pub source_type: String,
pub target_type: String,
pub forward_route_name: String,
pub reverse_route_name: String,
pub description: Option<String>,
pub required_fields: Option<Vec<String>>,
#[serde(default)]
pub auth: Option<LinkAuthConfig>,
}
impl LinkDefinition {
pub fn default_forward_route_name(target_type: &str, link_type: &str) -> String {
format!(
"{}-{}",
Pluralizer::pluralize(target_type),
Pluralizer::pluralize(link_type)
)
}
pub fn default_reverse_route_name(source_type: &str, link_type: &str) -> String {
format!(
"{}-{}",
Pluralizer::pluralize(source_type),
Pluralizer::pluralize(link_type)
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_link_creation() {
let user_id = Uuid::new_v4();
let car_id = Uuid::new_v4();
let link = LinkEntity::new("owner", user_id, car_id, None);
assert_eq!(link.link_type, "owner");
assert_eq!(link.source_id, user_id);
assert_eq!(link.target_id, car_id);
assert!(link.metadata.is_none());
assert!(link.tenant_id.is_none());
assert_eq!(link.status, "active");
assert!(!link.is_deleted());
assert!(link.is_active());
}
#[test]
fn test_link_creation_without_tenant() {
let user_id = Uuid::new_v4();
let car_id = Uuid::new_v4();
let link = LinkEntity::new("owner", user_id, car_id, None);
assert!(link.tenant_id.is_none());
}
#[test]
fn test_link_creation_with_tenant() {
let tenant_id = Uuid::new_v4();
let user_id = Uuid::new_v4();
let car_id = Uuid::new_v4();
let link = LinkEntity::new_with_tenant(tenant_id, "owner", user_id, car_id, None);
assert_eq!(link.link_type, "owner");
assert_eq!(link.source_id, user_id);
assert_eq!(link.target_id, car_id);
assert_eq!(link.tenant_id, Some(tenant_id));
assert_eq!(link.status, "active");
}
#[test]
fn test_link_with_tenant_and_metadata() {
let tenant_id = Uuid::new_v4();
let user_id = Uuid::new_v4();
let company_id = Uuid::new_v4();
let metadata = serde_json::json!({
"role": "Senior Developer",
"start_date": "2024-01-01"
});
let link = LinkEntity::new_with_tenant(
tenant_id,
"worker",
user_id,
company_id,
Some(metadata.clone()),
);
assert_eq!(link.tenant_id, Some(tenant_id));
assert_eq!(link.metadata, Some(metadata));
}
#[test]
fn test_link_serialization_without_tenant() {
let link = LinkEntity::new("owner", Uuid::new_v4(), Uuid::new_v4(), None);
let json = serde_json::to_value(&link).unwrap();
assert!(json.get("tenant_id").is_none());
}
#[test]
fn test_link_serialization_with_tenant() {
let tenant_id = Uuid::new_v4();
let link =
LinkEntity::new_with_tenant(tenant_id, "owner", Uuid::new_v4(), Uuid::new_v4(), None);
let json = serde_json::to_value(&link).unwrap();
assert_eq!(
json.get("tenant_id").and_then(|v| v.as_str()),
Some(tenant_id.to_string().as_str())
);
}
#[test]
fn test_link_with_metadata() {
let user_id = Uuid::new_v4();
let company_id = Uuid::new_v4();
let metadata = serde_json::json!({
"role": "Senior Developer",
"start_date": "2024-01-01"
});
let link = LinkEntity::new("worker", user_id, company_id, Some(metadata.clone()));
assert_eq!(link.metadata, Some(metadata));
}
#[test]
fn test_link_soft_delete() {
let mut link = LinkEntity::new("owner", Uuid::new_v4(), Uuid::new_v4(), None);
assert!(!link.is_deleted());
assert!(link.is_active());
link.soft_delete();
assert!(link.is_deleted());
assert!(!link.is_active());
}
#[test]
fn test_link_restore() {
let mut link = LinkEntity::new("owner", Uuid::new_v4(), Uuid::new_v4(), None);
link.soft_delete();
assert!(link.is_deleted());
link.restore();
assert!(!link.is_deleted());
assert!(link.is_active());
}
#[test]
fn test_default_route_names() {
let forward = LinkDefinition::default_forward_route_name("car", "owner");
assert_eq!(forward, "cars-owners");
let reverse = LinkDefinition::default_reverse_route_name("user", "owner");
assert_eq!(reverse, "users-owners");
}
#[test]
fn test_route_names_with_irregular_plurals() {
let forward = LinkDefinition::default_forward_route_name("company", "owner");
assert_eq!(forward, "companies-owners");
let reverse = LinkDefinition::default_reverse_route_name("company", "worker");
assert_eq!(reverse, "companies-workers");
}
#[test]
fn test_link_auth_config_default() {
let auth = LinkAuthConfig::default();
assert_eq!(auth.list, "authenticated");
assert_eq!(auth.get, "authenticated");
assert_eq!(auth.create, "authenticated");
assert_eq!(auth.update, "authenticated");
assert_eq!(auth.delete, "authenticated");
}
#[test]
fn test_link_definition_with_auth() {
let yaml = r#"
link_type: has_invoice
source_type: order
target_type: invoice
forward_route_name: invoices
reverse_route_name: order
auth:
list: authenticated
get: owner
create: service_only
update: owner
delete: admin_only
"#;
let def: LinkDefinition = serde_yaml::from_str(yaml).unwrap();
assert_eq!(def.link_type, "has_invoice");
assert_eq!(def.source_type, "order");
assert_eq!(def.target_type, "invoice");
let auth = def.auth.unwrap();
assert_eq!(auth.list, "authenticated");
assert_eq!(auth.get, "owner");
assert_eq!(auth.create, "service_only");
assert_eq!(auth.update, "owner");
assert_eq!(auth.delete, "admin_only");
}
#[test]
fn test_touch_updates_updated_at() {
let mut link = LinkEntity::new("owner", Uuid::new_v4(), Uuid::new_v4(), None);
let original_updated_at = link.updated_at;
std::thread::sleep(std::time::Duration::from_millis(10));
link.touch();
assert!(
link.updated_at > original_updated_at,
"touch() should advance updated_at"
);
}
#[test]
fn test_link_auth_config_partial_yaml_deserialization() {
let yaml = r#"
list: public
delete: admin_only
"#;
let auth: LinkAuthConfig =
serde_yaml::from_str(yaml).expect("partial LinkAuthConfig should deserialize");
assert_eq!(auth.list, "public");
assert_eq!(auth.delete, "admin_only");
assert_eq!(auth.get, "authenticated");
assert_eq!(auth.create, "authenticated");
assert_eq!(auth.update, "authenticated");
}
#[test]
fn test_link_entity_with_metadata() {
let metadata = serde_json::json!({"role": "CTO", "department": "Engineering"});
let link = LinkEntity::new(
"worker",
Uuid::new_v4(),
Uuid::new_v4(),
Some(metadata.clone()),
);
assert_eq!(link.metadata, Some(metadata));
assert_eq!(link.entity_type, "link");
}
#[test]
fn test_link_entity_without_metadata() {
let link = LinkEntity::new("driver", Uuid::new_v4(), Uuid::new_v4(), None);
assert!(link.metadata.is_none());
assert_eq!(link.link_type, "driver");
assert_eq!(link.entity_type, "link");
assert!(link.deleted_at.is_none());
}
}