use serde::{Deserialize, Serialize};
use crate::common::tmf_error::TMFError;
use crate::{
common::{contact::ContactMedium, related_party::RelatedParty},
tmf632::Characteristic,
tmf651::agreement::AgreementRef,
tmf666::{AccountRef, PaymentMethodRef},
DateTime, HasId, HasName, HasRelatedParty, HasValidity, TimePeriod, Uri,
};
use tmflib_derive::{HasId, HasName, HasRelatedParty, HasValidity};
use super::MOD_PATH;
const CLASS_PATH: &str = "partyRole";
#[derive(Clone, Debug, Default, Deserialize, HasValidity, Serialize)]
pub struct CreditProfile {
pub credit_profile_date: Option<DateTime>,
pub credit_risk_rating: Option<u32>,
pub credit_score: Option<u32>,
pub valid_for: Option<TimePeriod>,
}
#[derive(
Clone, Debug, Default, Deserialize, HasId, HasName, HasRelatedParty, HasValidity, Serialize,
)]
#[serde(rename_all = "camelCase")]
pub struct PartyRole {
pub id: Option<String>,
pub href: Option<Uri>,
pub name: Option<String>,
pub status: Option<String>,
pub status_reason: Option<String>,
pub valid_for: Option<TimePeriod>,
pub engaged_party: Option<RelatedParty>,
pub related_party: Option<Vec<RelatedParty>>,
pub credit_profile: Option<Vec<CreditProfile>>,
pub agreement: Option<Vec<AgreementRef>>,
pub account: Option<Vec<AccountRef>>,
pub payment_method: Option<Vec<PaymentMethodRef>>,
pub contact_medium: Option<Vec<ContactMedium>>,
pub characteristic: Option<Vec<Characteristic>>,
}
impl PartyRole {
pub fn new(name: impl Into<String>, party: RelatedParty) -> PartyRole {
PartyRole {
name: Some(name.into()),
engaged_party: Some(party),
..PartyRole::create()
}
}
pub fn engaged_party(mut self, related_party: RelatedParty) -> PartyRole {
self.engaged_party = Some(related_party);
self
}
pub fn add_profile(&mut self, profile: CreditProfile) {
match self.credit_profile.as_mut() {
Some(cp) => cp.push(profile),
None => self.credit_profile = Some(vec![profile]),
}
}
pub fn get_profile(&self, idx: usize) -> Option<&CreditProfile> {
match self.credit_profile.as_ref() {
Some(cp) => cp.get(idx),
None => None,
}
}
pub fn add_payment(&mut self, payment: PaymentMethodRef) {
match self.payment_method.as_mut() {
Some(pm) => pm.push(payment),
None => self.payment_method = Some(vec![payment]),
}
}
}
#[cfg(test)]
mod test {
use crate::common::related_party::RelatedParty;
#[cfg(all(feature = "tmf632", feature = "build-V4"))]
use crate::tmf632::individual_v4::Individual;
#[cfg(all(feature = "tmf632", feature = "build-V5"))]
use crate::tmf632::individual_v5::Individual;
use super::*;
const ROLE_NAME: &str = "APartyRole";
const IND1: &str = "Individual1";
const IND2: &str = "Individual2";
const PARTYROLE_JSON: &str = "{
\"name\" : \"PartyRoleName\"
}";
#[test]
fn test_party_role_new_name() {
let ind = Individual::new(IND1);
let role = PartyRole::new(ROLE_NAME, RelatedParty::from(&ind));
assert_eq!(role.name.is_some(), true);
assert_eq!(role.get_name().as_str(), ROLE_NAME);
assert_eq!(role.engaged_party.is_some(), true);
assert_eq!(
role.engaged_party.as_ref().unwrap().name,
Some(ind.get_name())
);
}
#[test]
fn test_partyrole_deserialize() {
let partyrole: PartyRole = serde_json::from_str(PARTYROLE_JSON).unwrap();
assert_eq!(partyrole.get_name().as_str(), "PartyRoleName");
}
#[test]
fn test_partyrole_engagedparty() {
let ind1 = Individual::new(IND1);
let ind2 = Individual::new(IND2);
let party2 = RelatedParty::from(&ind2);
let party1 = RelatedParty::from(&ind1);
let partyrole = PartyRole::new(ROLE_NAME, party1).engaged_party(party2);
assert_eq!(partyrole.engaged_party.is_some(), true);
assert_eq!(partyrole.engaged_party.unwrap().name.is_some(), true);
}
}