1use serde::{Deserialize, Serialize};
10use std::convert::From;
11
12use crate::tmf629::customer::Customer;
13#[cfg(all(feature = "tmf632", feature = "build-V4"))]
14use crate::tmf632::individual_v4::Individual;
15#[cfg(all(feature = "tmf632", feature = "build-V5"))]
16use crate::tmf632::individual_v5::Individual;
17#[cfg(all(feature = "tmf632", feature = "build-V4"))]
18use crate::tmf632::organization_v4::{Organization, OrganizationRef};
19#[cfg(all(feature = "tmf632", feature = "build-V5"))]
20use crate::tmf632::organization_v5::{Organization, OrganizationRef};
21#[cfg(feature = "tmf669")]
22use crate::tmf669::party_role::PartyRole;
23use crate::{HasId, HasName, Uri};
24
25#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
27#[serde(rename_all = "camelCase")]
28pub struct RelatedParty {
29    pub id: String,
31    pub href: String,
33    #[serde(skip_serializing_if = "Option::is_none")]
35    pub name: Option<String>,
36    #[serde(skip_serializing_if = "Option::is_none")]
38    pub role: Option<String>,
39
40    #[serde(skip_serializing_if = "Option::is_none")]
43    #[serde(rename = "@baseType")]
44    pub base_type: Option<String>,
45    #[serde(skip_serializing_if = "Option::is_none")]
47    #[serde(rename = "@schemaLocation")]
48    pub schema_location: Option<Uri>,
49    #[serde(skip_serializing_if = "Option::is_none")]
51    #[serde(rename = "@type")]
52    pub r#type: Option<String>,
53    #[serde(skip_serializing_if = "Option::is_none")]
55    #[serde(rename = "@referredType")]
56    pub referred_type: Option<String>,
57}
58
59impl From<&Customer> for RelatedParty {
60    fn from(cust: &Customer) -> Self {
61        RelatedParty {
62            id: cust.get_id(),
63            href: cust.get_href(),
64            name: cust.name.clone(),
65            role: Some(Customer::get_class()),
66            base_type: Some(Customer::get_class()),
67            referred_type: Some(Customer::get_class()),
68            r#type: Some(Customer::get_class()),
69            schema_location: None,
70        }
71    }
72}
73
74impl From<Organization> for RelatedParty {
75    fn from(org: Organization) -> Self {
76        RelatedParty {
77            id: org.get_id(),
78            href: org.get_href(),
79            name: Some(org.get_name()),
80            role: Some(Organization::get_class()),
81            referred_type: Some(Organization::get_class()),
82            base_type: Some(Organization::get_class()),
83            r#type: Some(Organization::get_class()),
84            schema_location: None,
85        }
86    }
87}
88
89impl From<&Organization> for RelatedParty {
90    fn from(org: &Organization) -> Self {
91        RelatedParty {
92            id: org.get_id(),
93            href: org.get_href(),
94            name: Some(org.get_name()),
95            role: Some(Organization::get_class()),
96            referred_type: Some(Organization::get_class()),
97            base_type: Some(Organization::get_class()),
98            r#type: Some(Organization::get_class()),
99            schema_location: None,
100        }
101    }
102}
103
104impl From<OrganizationRef> for RelatedParty {
105    fn from(value: OrganizationRef) -> Self {
106        RelatedParty {
107            id: value.id.clone(),
108            href: value.href.clone(),
109            name: Some(value.name.clone()),
110            role: Some(Organization::get_class()),
111            referred_type: Some(Organization::get_class()),
112            base_type: Some(Organization::get_class()),
113            r#type: Some(Organization::get_class()),
114            schema_location: None,
115        }
116    }
117}
118
119impl From<&Individual> for RelatedParty {
120    fn from(value: &Individual) -> Self {
121        RelatedParty {
122            id: value.get_id(),
123            href: value.get_href(),
124            name: Some(value.get_name()),
125            role: Some(Individual::get_class()),
126            referred_type: Some(Individual::get_class()),
127            base_type: Some(Individual::get_class()),
128            r#type: Some(Individual::get_class()),
129            schema_location: None,
130        }
131    }
132}
133
134impl From<&PartyRole> for RelatedParty {
137    fn from(value: &PartyRole) -> Self {
138        RelatedParty {
139            id: value.get_id(),
140            href: value.get_href(),
141            name: None,
142            role: value.name.clone(),
143            referred_type: Some(PartyRole::get_class()),
144            base_type: Some(PartyRole::get_class()),
145            r#type: Some(PartyRole::get_class()),
146            schema_location: None,
147        }
148    }
149}
150
151#[cfg(test)]
152mod test {
153    use super::RelatedParty;
154    use crate::tmf629::customer::Customer;
155    #[cfg(all(feature = "tmf632", feature = "build-V4"))]
156    use crate::tmf632::organization_v4::Organization;
157    #[cfg(all(feature = "tmf632", feature = "build-V4"))]
158    use crate::tmf632::organization_v4::OrganizationRef;
159    #[cfg(all(feature = "tmf632", feature = "build-V5"))]
160    use crate::tmf632::organization_v5::Organization;
161    #[cfg(all(feature = "tmf632", feature = "build-V5"))]
162    use crate::tmf632::organization_v5::OrganizationRef;
163    use crate::tmf669::party_role::PartyRole;
164    use crate::{HasId, HasName};
165
166    const ORG_NAME: &str = "An Organisation";
167    const ROLE_NAME: &str = "A Role";
168
169    #[test]
170    fn test_related_party_from_customer_id() {
171        let org = Organization::new(ORG_NAME);
172        let cust = Customer::new(org);
173        let party = RelatedParty::from(&cust);
174        assert_eq!(cust.id.unwrap(), party.id);
175    }
176    #[test]
177    fn test_related_party_from_customer_href() {
178        let org = Organization::new(ORG_NAME);
179        let cust = Customer::new(org);
180        let party = RelatedParty::from(&cust);
181        assert_eq!(cust.href.unwrap(), party.href);
182    }
183    #[test]
184    fn test_related_party_from_customer_name() {
185        let org = Organization::new(ORG_NAME);
186        let cust = Customer::new(org);
187        let party = RelatedParty::from(&cust);
188        assert_eq!(cust.name, party.name);
189    }
190    #[test]
191    fn test_related_party_from_customer_role() {
192        let org = Organization::new(ORG_NAME);
193        let cust = Customer::new(org);
194        let party = RelatedParty::from(&cust);
195        assert_eq!(party.role.unwrap(), Customer::get_class());
196    }
197    #[test]
198    fn test_related_party_from_customer_referred() {
199        let org = Organization::new(ORG_NAME);
200        let cust = Customer::new(org);
201        let party = RelatedParty::from(&cust);
202
203        assert_eq!(party.referred_type.unwrap(), Customer::get_class());
204    }
205
206    #[test]
207    fn test_related_party_from_organization() {
208        let org = Organization::new(ORG_NAME);
209
210        let party = RelatedParty::from(&org);
211
212        assert_eq!(org.name, party.name);
213        assert_eq!(org.id.unwrap(), party.id);
214        assert_eq!(org.href.unwrap(), party.href);
215    }
216
217    #[test]
218    fn test_related_party_from_organization_ref() {
219        let org = Organization::new(ORG_NAME);
220        let org_ref = OrganizationRef::from(org);
221
222        let party = RelatedParty::from(org_ref.clone());
223
224        assert_eq!(org_ref.name, party.name.unwrap());
225        assert_eq!(org_ref.id, party.id);
226        assert_eq!(org_ref.href, party.href);
227    }
228
229    #[test]
230    fn test_relatedparty_from_partyrole() {
231        let party = Organization::new(ORG_NAME);
232        let role = PartyRole::new(ROLE_NAME, RelatedParty::from(party.clone()));
233
234        let new_party = RelatedParty::from(&role);
235
236        assert_eq!(new_party.id, role.get_id());
237        assert_eq!(new_party.role.is_some(), true);
238        assert_eq!(new_party.role.unwrap(), role.get_name());
239        assert_eq!(new_party.referred_type.is_some(), true);
240        assert_eq!(new_party.referred_type.unwrap(), PartyRole::get_class());
241    }
242}