1use chrono::{DateTime, Utc};
4use instant_xml::{FromXml, ToXml};
5
6use super::{ContactAuthInfo, Fax, PostalInfo, Voice, XMLNS};
7use crate::common::{NoExtension, EPP_XMLNS};
8use crate::request::{Command, Transaction};
9
10impl Transaction<NoExtension> for ContactCreate<'_> {}
11
12impl Command for ContactCreate<'_> {
13 type Response = CreateData;
14 const COMMAND: &'static str = "create";
15}
16
17#[derive(Debug, ToXml)]
21#[xml(rename = "create", ns(XMLNS))]
22pub struct ContactCreateRequest<'a> {
23 id: &'a str,
25 postal_info: PostalInfo<'a>,
27 voice: Option<Voice<'a>>,
29 fax: Option<Fax<'a>>,
31 email: &'a str,
33 auth_info: ContactAuthInfo<'a>,
35}
36
37#[derive(Debug, ToXml)]
39#[xml(rename = "create", ns(EPP_XMLNS))]
40pub struct ContactCreate<'a> {
41 pub contact: ContactCreateRequest<'a>,
43}
44
45impl<'a> ContactCreate<'a> {
46 pub fn new(
47 id: &'a str,
48 email: &'a str,
49 postal_info: PostalInfo<'a>,
50 voice: Option<Voice<'a>>,
51 auth_password: &'a str,
52 ) -> Self {
53 Self {
54 contact: ContactCreateRequest {
55 id,
56 postal_info,
57 voice,
58 fax: None,
59 email,
60 auth_info: ContactAuthInfo::new(auth_password),
61 },
62 }
63 }
64
65 pub fn set_fax(&mut self, fax: Fax<'a>) {
67 self.contact.fax = Some(fax);
68 }
69}
70
71#[derive(Debug, FromXml)]
75#[xml(rename = "creData", ns(XMLNS))]
76pub struct CreateData {
77 pub id: String,
79 #[xml(rename = "crDate")]
80 pub created_at: DateTime<Utc>,
82}
83
84#[cfg(test)]
85mod tests {
86 use chrono::{TimeZone, Utc};
87
88 use super::{ContactCreate, Fax, PostalInfo, Voice};
89 use crate::contact::{Address, InfoType};
90 use crate::response::ResultCode;
91 use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
92
93 #[test]
94 fn command() {
95 let street = &["58", "Orchid Road"];
96 let address = Address::new(
97 street,
98 "Paris",
99 Some("Paris"),
100 Some("392374"),
101 "FR".parse().unwrap(),
102 );
103 let postal_info = PostalInfo::new(
104 InfoType::International,
105 "John Doe",
106 Some("Acme Widgets"),
107 address,
108 );
109 let mut voice = Voice::new("+33.47237942");
110 voice.set_extension("123");
111 let mut fax = Fax::new("+33.86698799");
112 fax.set_extension("677");
113
114 let mut object = ContactCreate::new(
115 "eppdev-contact-3",
116 "contact@eppdev.net",
117 postal_info,
118 Some(voice),
119 "eppdev-387323",
120 );
121 object.set_fax(fax);
122
123 assert_serialized("request/contact/create.xml", &object);
124 }
125
126 #[test]
127 fn command_minimal() {
128 let address = Address::new(&[], "Paris", None, None, "FR".parse().unwrap());
129 let postal_info = PostalInfo::new(InfoType::International, "John Doe", None, address);
130 let object = ContactCreate::new(
131 "eppdev-contact-3",
132 "contact@eppdev.net",
133 postal_info,
134 None,
135 "eppdev-387323",
136 );
137
138 assert_serialized("request/contact/create_minimal.xml", &object);
139 }
140
141 #[test]
142 fn response() {
143 let object = response_from_file::<ContactCreate>("response/contact/create.xml");
144 let results = object.res_data().unwrap();
145
146 assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
147 assert_eq!(object.result.message, SUCCESS_MSG);
148 assert_eq!(results.id, "eppdev-contact-4");
149 assert_eq!(
150 results.created_at,
151 Utc.with_ymd_and_hms(2021, 7, 25, 16, 5, 32).unwrap(),
152 );
153 assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
154 assert_eq!(object.tr_ids.server_tr_id, SVTRID);
155 }
156}