1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

use crate::{
    customers::{Billing, Shipping},
    MetaData,
};

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateCustomer {
    /// The email address for the customer
    email: String,
    /// Customer first name.    
    first_name: Option<String>,
    /// Customer last name.
    last_name: Option<String>,
    /// Customer login name.
    username: Option<String>,
    // Customer password.
    password: Option<String>,
    /// List of billing address data.    
    billing: Option<Billing>,
    /// List of shipping address data.
    shipping: Option<Shipping>,
    /// Meta data.
    meta_data: Option<Vec<MetaData>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct NoEmail;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct WithEmail(String);
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CreateCustomerBuilder<T> {
    /// The email address for the customer
    email: T,
    /// Customer first name.    
    first_name: Option<String>,
    /// Customer last name.
    last_name: Option<String>,
    /// Customer login name.
    username: Option<String>,
    // Customer password.
    password: Option<String>,
    /// List of billing address data.    
    billing: Option<Billing>,
    /// List of shipping address data.
    shipping: Option<Shipping>,
    /// Meta data.
    meta_data: Option<Vec<MetaData>>,
}

impl CreateCustomerBuilder<WithEmail> {
    pub fn build(self) -> CreateCustomer {
        CreateCustomer {
            email: self.email.0,
            first_name: self.first_name,
            last_name: self.last_name,
            username: self.username,
            password: self.password,
            billing: self.billing,
            shipping: self.shipping,
            meta_data: self.meta_data,
        }
    }
}
impl<T> CreateCustomerBuilder<T> {
    /// The email address for the customer
    pub fn email(self, email: impl Into<String>) -> CreateCustomerBuilder<WithEmail> {
        CreateCustomerBuilder {
            email: WithEmail(email.into()),
            first_name: self.first_name,
            last_name: self.last_name,
            username: self.username,
            password: self.password,
            billing: self.billing,
            shipping: self.shipping,
            meta_data: self.meta_data,
        }
    }
    /// Customer first name.    
    pub fn first_name(mut self, first_name: impl Into<String>) -> Self {
        let _ = self.first_name.insert(first_name.into());
        self
    }
    /// Customer last name.
    pub fn last_name(mut self, last_name: impl Into<String>) -> Self {
        let _ = self.last_name.insert(last_name.into());
        self
    }
    /// Customer login name.
    pub fn username(mut self, username: impl Into<String>) -> Self {
        let _ = self.username.insert(username.into());
        self
    }
    // Customer password.
    pub fn password(mut self, password: impl Into<String>) -> Self {
        let _ = self.password.insert(password.into());
        self
    }
    /// billing first name.
    pub fn billing_first_name(mut self, first_name: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).first_name = first_name.into();
        self
    }
    /// billing last name.
    pub fn billing_last_name(mut self, last_name: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).last_name = last_name.into();
        self
    }
    /// billing company name.
    pub fn billing_company(mut self, company: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).company = company.into();
        self
    }
    /// billing address line 1
    pub fn billing_address_1(mut self, address_1: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).address_1 = address_1.into();
        self
    }
    /// billing address line 2
    pub fn billing_address_2(mut self, address_2: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).address_2 = address_2.into();
        self
    }
    /// billing city name.
    pub fn billing_city(mut self, city: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).city = city.into();
        self
    }
    /// billing ISO code or name of the state, province or district.
    pub fn billing_state(mut self, state: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).state = state.into();
        self
    }
    /// billing postal code.
    pub fn billing_postcode(mut self, postcode: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).postcode = postcode.into();
        self
    }
    /// billing ISO code of the country.
    pub fn billing_country(mut self, country: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).country = country.into();
        self
    }
    /// billing email address.
    pub fn billing_email(mut self, email: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).email = email.into();
        self
    }
    /// billing phone number.
    pub fn billing_phone(mut self, phone: impl Into<String>) -> Self {
        self.billing.get_or_insert(Billing::default()).phone = phone.into();
        self
    }
    /// shipping first name.
    pub fn shipping_first_name(mut self, first_name: impl Into<String>) -> Self {
        self.shipping.get_or_insert(Shipping::default()).first_name = first_name.into();
        self
    }
    /// shipping last name.
    pub fn shipping_last_name(mut self, last_name: impl Into<String>) -> Self {
        self.shipping.get_or_insert(Shipping::default()).last_name = last_name.into();
        self
    }
    /// shipping company name.
    pub fn shipping_company(mut self, company: impl Into<String>) -> Self {
        self.shipping.get_or_insert(Shipping::default()).company = company.into();
        self
    }
    /// shipping address line 1
    pub fn shipping_address_1(mut self, address_1: impl Into<String>) -> Self {
        self.shipping.get_or_insert(Shipping::default()).address_1 = address_1.into();
        self
    }
    /// shipping address line 2
    pub fn shipping_address_2(mut self, address_2: impl Into<String>) -> Self {
        self.shipping.get_or_insert(Shipping::default()).address_2 = address_2.into();
        self
    }
    /// shipping city name.
    pub fn shipping_city(mut self, city: impl Into<String>) -> Self {
        self.shipping.get_or_insert(Shipping::default()).city = city.into();
        self
    }
    /// shipping ISO code or name of the state, province or district.
    pub fn shipping_state(mut self, state: impl Into<String>) -> Self {
        self.shipping.get_or_insert(Shipping::default()).state = state.into();
        self
    }
    /// shipping postal code.
    pub fn shipping_postcode(mut self, postcode: impl Into<String>) -> Self {
        self.shipping.get_or_insert(Shipping::default()).postcode = postcode.into();
        self
    }
    /// shipping ISO code of the country.
    pub fn shipping_country(mut self, country: impl Into<String>) -> Self {
        self.shipping.get_or_insert(Shipping::default()).country = country.into();
        self
    }
    /// Meta data.
    pub fn meta_data(mut self, key: impl Into<String>, value: impl serde::Serialize) -> Self {
        self.meta_data.get_or_insert(vec![]).push(MetaData {
            id: None,
            key: key.into(),
            value: serde_json::json!(value),
        });
        self
    }
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateCustomer {
    /// Unique identifier for the resource
    id: Option<i32>,
    /// The email address for the customer
    email: Option<String>,
    /// Customer first name.    
    first_name: Option<String>,
    /// Customer last name.
    last_name: Option<String>,
    /// Customer login name.
    username: Option<String>,
    // Customer password.
    password: Option<String>,
    /// List of billing address data.    
    billing: Option<Billing>,
    /// List of shipping address data.
    shipping: Option<Shipping>,
    /// Meta data.
    meta_data: Option<Vec<MetaData>>,
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct BillingUpdate {
    /// First name.
    first_name: Option<String>,
    /// Last name.
    last_name: Option<String>,
    /// Company name.
    company: Option<String>,
    /// Address line 1
    address_1: Option<String>,
    /// Address line 2
    address_2: Option<String>,
    /// City name.
    city: Option<String>,
    /// ISO code or name of the state, province or district.
    state: Option<String>,
    /// Postal code.
    postcode: Option<String>,
    /// ISO code of the country.
    country: Option<String>,
    /// Email address.
    email: Option<String>,
    /// Phone number.
    phone: Option<String>,
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ShippingUpdate {
    /// First name.
    first_name: Option<String>,
    /// Last name.
    last_name: Option<String>,
    /// Company name.
    company: Option<String>,
    /// Address line 1
    address_1: Option<String>,
    /// Address line 2
    address_2: Option<String>,
    /// City name.
    city: Option<String>,
    /// ISO code or name of the state, province or district.
    state: Option<String>,
    /// Postal code.
    postcode: Option<String>,
    /// ISO code of the country.
    country: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UpdateCustomerBuilder {
    /// Unique identifier for the resource
    id: Option<i32>,
    /// The email address for the customer
    email: Option<String>,
    /// Customer first name.    
    first_name: Option<String>,
    /// Customer last name.
    last_name: Option<String>,
    /// Customer login name.
    username: Option<String>,
    // Customer password.
    password: Option<String>,
    /// List of billing address data.    
    billing: Option<Billing>,
    /// List of shipping address data.
    shipping: Option<Shipping>,
    /// Meta data.
    meta_data: Option<Vec<MetaData>>,
}
impl UpdateCustomerBuilder {
    pub fn build(&mut self) -> UpdateCustomer {
        UpdateCustomer {
            id: self.id,
            email: self.email.clone(),
            first_name: self.first_name.clone(),
            last_name: self.last_name.clone(),
            username: self.username.clone(),
            password: self.password.clone(),
            billing: self.billing.clone(),
            shipping: self.shipping.clone(),
            meta_data: self.meta_data.clone(),
        }
    }
    /// Unique identifier for the resource
    pub fn id(&mut self, id: i32) -> &mut Self {
        let _ = self.id.insert(id);
        self
    }
    /// The email address for the customer
    pub fn email(&mut self, email: impl Into<String>) -> &mut Self {
        let _ = self.email.insert(email.into());
        self
    }
    /// Customer first name.    
    pub fn first_name(&mut self, first_name: impl Into<String>) -> &mut Self {
        let _ = self.first_name.insert(first_name.into());
        self
    }
    /// Customer last name.
    pub fn last_name(&mut self, last_name: impl Into<String>) -> &mut Self {
        let _ = self.last_name.insert(last_name.into());
        self
    }
    /// Customer login name.
    pub fn username(&mut self, username: impl Into<String>) -> &mut Self {
        let _ = self.username.insert(username.into());
        self
    }
    // Customer password.
    pub fn password(&mut self, password: impl Into<String>) -> &mut Self {
        let _ = self.password.insert(password.into());
        self
    }
    /// billing first name.
    pub fn billing_first_name(&mut self, first_name: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).first_name = first_name.into();
        self
    }
    /// billing last name.
    pub fn billing_last_name(&mut self, last_name: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).last_name = last_name.into();
        self
    }
    /// billing company name.
    pub fn billing_company(&mut self, company: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).company = company.into();
        self
    }
    /// billing address line 1
    pub fn billing_address_1(&mut self, address_1: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).address_1 = address_1.into();
        self
    }
    /// billing address line 2
    pub fn billing_address_2(&mut self, address_2: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).address_2 = address_2.into();
        self
    }
    /// billing city name.
    pub fn billing_city(&mut self, city: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).city = city.into();
        self
    }
    /// billing ISO code or name of the state, province or district.
    pub fn billing_state(&mut self, state: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).state = state.into();
        self
    }
    /// billing postal code.
    pub fn billing_postcode(&mut self, postcode: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).postcode = postcode.into();
        self
    }
    /// billing ISO code of the country.
    pub fn billing_country(&mut self, country: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).country = country.into();
        self
    }
    /// billing email address.
    pub fn billing_email(&mut self, email: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).email = email.into();
        self
    }
    /// billing phone number.
    pub fn billing_phone(&mut self, phone: impl Into<String>) -> &mut Self {
        self.billing.get_or_insert(Billing::default()).phone = phone.into();
        self
    }
    /// shipping first name.
    pub fn shipping_first_name(&mut self, first_name: impl Into<String>) -> &mut Self {
        self.shipping.get_or_insert(Shipping::default()).first_name = first_name.into();
        self
    }
    /// shipping last name.
    pub fn shipping_last_name(&mut self, last_name: impl Into<String>) -> &mut Self {
        self.shipping.get_or_insert(Shipping::default()).last_name = last_name.into();
        self
    }
    /// shipping company name.
    pub fn shipping_company(&mut self, company: impl Into<String>) -> &mut Self {
        self.shipping.get_or_insert(Shipping::default()).company = company.into();
        self
    }
    /// shipping address line 1
    pub fn shipping_address_1(&mut self, address_1: impl Into<String>) -> &mut Self {
        self.shipping.get_or_insert(Shipping::default()).address_1 = address_1.into();
        self
    }
    /// shipping address line 2
    pub fn shipping_address_2(&mut self, address_2: impl Into<String>) -> &mut Self {
        self.shipping.get_or_insert(Shipping::default()).address_2 = address_2.into();
        self
    }
    /// shipping city name.
    pub fn shipping_city(&mut self, city: impl Into<String>) -> &mut Self {
        self.shipping.get_or_insert(Shipping::default()).city = city.into();
        self
    }
    /// shipping ISO code or name of the state, province or district.
    pub fn shipping_state(&mut self, state: impl Into<String>) -> &mut Self {
        self.shipping.get_or_insert(Shipping::default()).state = state.into();
        self
    }
    /// shipping postal code.
    pub fn shipping_postcode(&mut self, postcode: impl Into<String>) -> &mut Self {
        self.shipping.get_or_insert(Shipping::default()).postcode = postcode.into();
        self
    }
    /// shipping ISO code of the country.
    pub fn shipping_country(&mut self, country: impl Into<String>) -> &mut Self {
        self.shipping.get_or_insert(Shipping::default()).country = country.into();
        self
    }
    /// Meta data.
    pub fn meta_data(&mut self, key: impl Into<String>, value: impl serde::Serialize) -> &mut Self {
        self.meta_data.get_or_insert(vec![]).push(MetaData {
            id: None,
            key: key.into(),
            value: serde_json::json!(value),
        });
        self
    }
}
#[cfg(test)]
mod tests {
    use crate::{customers::Customer, ApiClient, Entity};

    #[tokio::test]
    async fn test_list_all_customers() {
        let client = ApiClient::from_env().unwrap();
        let result: Vec<Customer> = client.list_all(Entity::Customer).await.unwrap();
        assert!(!result.is_empty());
    }
    #[tokio::test]
    async fn test_retrieve_customer() {
        let id = 4;
        let client = ApiClient::from_env().unwrap();
        let result: Customer = client.retrieve(Entity::Customer, id).await.unwrap();
        assert_eq!(id, result.id);
    }
    #[tokio::test]
    async fn test_search_customer() {
        let search_string = "Хасаншина";
        let client = ApiClient::from_env().unwrap();
        let result: Vec<Customer> = client
            .search(Entity::Customer, search_string)
            .await
            .unwrap();
        assert_eq!(result.first().unwrap().id, 4);
    }
    #[tokio::test]
    async fn test_create_delete_customer() {
        let customer_to_create = Customer::create()
            .email("info@google.com")
            .first_name("John")
            .last_name("Doe")
            .username("johny")
            .password("johnyspassword")
            .billing_first_name("John")
            .billing_last_name("Doe")
            .billing_company("Umbrella Corp")
            .billing_address_1("Leninsky street")
            .billing_address_2("дом 4")
            .billing_city("Moscow")
            .billing_state("Moscow")
            .billing_postcode("117000")
            .billing_country("Russia")
            .billing_email("info@google.com")
            .billing_phone("+79996669966")
            .shipping_first_name("John")
            .shipping_last_name("Doe")
            .shipping_company("Umbrella Corp")
            .shipping_address_1("Leninsky street")
            .shipping_address_2("дом 4")
            .shipping_city("Moscow")
            .shipping_state("Moscow")
            .shipping_postcode("117000")
            .shipping_country("Russia")
            .meta_data("test-meta", "test_value")
            .build();
        let client = ApiClient::from_env().unwrap();
        let created: Customer = client
            .create(Entity::Customer, customer_to_create)
            .await
            .unwrap();
        assert_eq!(created.email, "info@google.com");
        let id = created.id;
        let _deleted: Customer = client.delete(Entity::Customer, id).await.unwrap();
    }
    #[tokio::test]
    async fn test_update_customer() {
        let id = 4;
        let update = Customer::update().email("four@google.com").build();
        let client = ApiClient::from_env().unwrap();
        let updated: Customer = client.update(Entity::Customer, id, update).await.unwrap();
        assert_eq!(updated.id, id)
    }
}