use bon::Builder;
use serde::{Deserialize, Serialize};
use super::LettaId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IdentityType {
Org,
User,
Other,
}
impl std::fmt::Display for IdentityType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Org => write!(f, "org"),
Self::User => write!(f, "user"),
Self::Other => write!(f, "other"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IdentityProperty {
pub key: String,
pub value: serde_json::Value,
#[serde(rename = "type")]
pub property_type: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Identity {
pub id: LettaId,
pub identifier_key: String,
pub name: String,
pub identity_type: IdentityType,
#[serde(skip_serializing_if = "Option::is_none")]
pub agent_ids: Option<Vec<LettaId>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub block_ids: Option<Vec<LettaId>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub project_id: Option<LettaId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<Vec<IdentityProperty>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Builder)]
pub struct CreateIdentityRequest {
pub identifier_key: String,
pub name: String,
pub identity_type: IdentityType,
#[serde(skip_serializing_if = "Option::is_none")]
pub project_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub agent_ids: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub block_ids: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<Vec<IdentityProperty>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UpdateIdentityRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub identifier_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub identity_type: Option<IdentityType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub agent_ids: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub block_ids: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<Vec<IdentityProperty>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ListIdentitiesParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub project_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub identifier_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub identity_type: Option<IdentityType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub before: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub after: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<i32>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_identity_type_serialization() {
let user_json = serde_json::to_string(&IdentityType::User).unwrap();
assert_eq!(user_json, "\"user\"");
let org_json = serde_json::to_string(&IdentityType::Org).unwrap();
assert_eq!(org_json, "\"org\"");
let other_json = serde_json::to_string(&IdentityType::Other).unwrap();
assert_eq!(other_json, "\"other\"");
let user: IdentityType = serde_json::from_str("\"user\"").unwrap();
assert_eq!(user, IdentityType::User);
let org: IdentityType = serde_json::from_str("\"org\"").unwrap();
assert_eq!(org, IdentityType::Org);
let other: IdentityType = serde_json::from_str("\"other\"").unwrap();
assert_eq!(other, IdentityType::Other);
}
}