victorops 0.1.3

Async Rust client for VictorOps
Documentation
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Details about an HTTP request made to the VictorOps API.
#[derive(Debug, Clone)]
pub struct RequestDetails {
  /// The HTTP status code of the response.
  pub status_code: u16,
  /// The response body as a string.
  pub response_body: String,
  /// The request body that was sent.
  pub request_body: String,
}

/// A paged entity containing basic name and slug information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PagedEntity {
  /// The name of the entity.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub name: Option<String>,
  /// The slug identifier of the entity.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub slug: Option<String>,
}

/// A paged policy containing policy and team information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PagedPolicy {
  /// The policy entity information.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub policy: Option<PagedEntity>,
  /// The team entity information.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub team: Option<PagedEntity>,
}

/// Represents a state transition in an incident.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transition {
  /// The name of the transition.
  #[serde(skip_serializing_if = "Option::is_none", rename = "Name")]
  pub name: Option<String>,
  /// When the transition occurred.
  #[serde(skip_serializing_if = "Option::is_none", rename = "At")]
  pub at: Option<DateTime<Utc>>,
  /// Message associated with the transition.
  #[serde(skip_serializing_if = "Option::is_none", rename = "Message")]
  pub message: Option<String>,
  /// Who performed the transition.
  #[serde(skip_serializing_if = "Option::is_none", rename = "By")]
  pub by: Option<String>,
  /// Whether the transition was performed manually.
  #[serde(skip_serializing_if = "Option::is_none", rename = "Manually")]
  pub manually: Option<bool>,
  /// The ID of the alert that triggered this transition.
  #[serde(skip_serializing_if = "Option::is_none", rename = "alertId")]
  pub alert_id: Option<String>,
  /// The URL of the alert that triggered this transition.
  #[serde(skip_serializing_if = "Option::is_none", rename = "alertUrl")]
  pub alert_url: Option<String>,
}

/// Represents an incident in VictorOps.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Incident {
  /// The number of alerts in this incident.
  #[serde(skip_serializing_if = "Option::is_none", rename = "alertCount")]
  pub alert_count: Option<i32>,
  /// The current phase or state of the incident.
  #[serde(skip_serializing_if = "Option::is_none", rename = "currentPhase")]
  pub current_phase: Option<String>,
  /// The display name of the entity that triggered the incident.
  #[serde(skip_serializing_if = "Option::is_none", rename = "entityDisplayName")]
  pub entity_display_name: Option<String>,
  /// The unique identifier of the entity that triggered the incident.
  #[serde(skip_serializing_if = "Option::is_none", rename = "entityId")]
  pub entity_id: Option<String>,
  /// The state of the entity that triggered the incident.
  #[serde(skip_serializing_if = "Option::is_none", rename = "entityState")]
  pub entity_state: Option<String>,
  /// The type of entity that triggered the incident.
  #[serde(skip_serializing_if = "Option::is_none", rename = "entityType")]
  pub entity_type: Option<String>,
  /// The host associated with the incident.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub host: Option<String>,
  /// The incident number or identifier.
  #[serde(skip_serializing_if = "Option::is_none", rename = "incidentNumber")]
  pub incident_number: Option<String>,
  /// The ID of the last alert in this incident.
  #[serde(skip_serializing_if = "Option::is_none", rename = "lastAlertId")]
  pub last_alert_id: Option<String>,
  /// The timestamp of the last alert in this incident.
  #[serde(skip_serializing_if = "Option::is_none", rename = "lastAlertTime")]
  pub last_alert_time: Option<DateTime<Utc>>,
  /// The service associated with the incident.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub service: Option<String>,
  /// The timestamp when the incident started.
  #[serde(skip_serializing_if = "Option::is_none", rename = "startTime")]
  pub start_time: Option<DateTime<Utc>>,
  /// The list of teams that were paged for this incident.
  #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "pagedTeams")]
  pub paged_teams: Vec<String>,
  /// The list of users that were paged for this incident.
  #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "pagedUsers")]
  pub paged_users: Vec<String>,
  /// The list of escalation policies that were triggered for this incident.
  #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "pagedPolicies")]
  pub paged_policies: Vec<PagedPolicy>,
  /// The state transitions that occurred during this incident.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub transitions: Vec<Transition>,
}

/// Response containing a list of incidents.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncidentResponse {
  /// The list of incidents in the response.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub incidents: Vec<Incident>,
}

/// Represents a user in VictorOps.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
  /// The first name of the user.
  #[serde(skip_serializing_if = "Option::is_none", rename = "firstName")]
  pub first_name: Option<String>,
  /// The last name of the user.
  #[serde(skip_serializing_if = "Option::is_none", rename = "lastName")]
  pub last_name: Option<String>,
  /// The username of the user.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub username: Option<String>,
  /// The email address of the user.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub email: Option<String>,
  /// Whether the user has admin privileges.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub admin: Option<bool>,
  /// The number of hours until the user's session expires.
  #[serde(skip_serializing_if = "Option::is_none", rename = "expirationHours")]
  pub expiration_hours: Option<i32>,
  /// The timestamp when the user was created.
  #[serde(skip_serializing_if = "Option::is_none", rename = "createdAt")]
  pub created_at: Option<String>,
  /// The timestamp when the user's password was last updated.
  #[serde(
    skip_serializing_if = "Option::is_none",
    rename = "passwordLastUpdated"
  )]
  pub password_last_updated: Option<String>,
  /// Whether the user's account has been verified.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub verified: Option<bool>,
}

/// Response containing a list of users (v1 API format).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserList {
  /// The nested list of users in v1 API format.
  pub users: Vec<Vec<User>>,
}

/// Response containing a list of users (v2 API format).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserListV2 {
  /// The list of users in v2 API format.
  pub users: Vec<User>,
}

/// Represents a team in VictorOps.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Team {
  /// The name of the team.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub name: Option<String>,
  /// The unique slug identifier for the team.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub slug: Option<String>,
  /// The number of members in the team.
  #[serde(skip_serializing_if = "Option::is_none", rename = "memberCount")]
  pub member_count: Option<i32>,
  /// The version of the team configuration.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub version: Option<i32>,
  /// Whether this is the default team for the organization.
  #[serde(skip_serializing_if = "Option::is_none", rename = "isDefaultTeam")]
  pub is_default_team: Option<bool>,
}

/// Response containing team members.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TeamMembers {
  /// The list of team members.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub members: Vec<User>,
}

/// Represents an admin user.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Admin {
  /// The username of the admin.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub username: Option<String>,
  /// The first name of the admin.
  #[serde(skip_serializing_if = "Option::is_none", rename = "firstName")]
  pub first_name: Option<String>,
  /// The last name of the admin.
  #[serde(skip_serializing_if = "Option::is_none", rename = "lastName")]
  pub last_name: Option<String>,
  /// The URL to the admin's profile.
  #[serde(skip_serializing_if = "Option::is_none", rename = "_selfUrl")]
  pub self_url: Option<String>,
}

/// Response containing team administrators.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TeamAdmins {
  /// The list of team administrators.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub admin: Vec<Admin>,
}

/// Represents a contact method.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContactMethod {
  /// The unique identifier of the contact method.
  pub id: f64,
  /// The label or name of the contact method.
  pub label: String,
}

/// Response containing email contact methods.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmailsResponse {
  /// The list of email contact methods.
  #[serde(rename = "contactMethods")]
  pub contact_methods: Vec<serde_json::Value>,
}

/// Represents a team in API responses.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiTeam {
  /// The name of the team.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub name: Option<String>,
  /// The unique slug identifier for the team.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub slug: Option<String>,
}

/// Represents an escalation policy in API responses.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiEscalationPolicy {
  /// The name of the escalation policy.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub name: Option<String>,
  /// The unique slug identifier for the escalation policy.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub slug: Option<String>,
}

/// Represents a user in API responses.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiUser {
  /// The username of the user.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub username: Option<String>,
}

/// Represents an on-call override.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiOnCallOverride {
  /// The original user who was scheduled to be on-call.
  #[serde(skip_serializing_if = "Option::is_none", rename = "origOnCallUser")]
  pub orig_on_call_user: Option<ApiUser>,
  /// The user who is overriding the original on-call assignment.
  #[serde(skip_serializing_if = "Option::is_none", rename = "overrideOnCallUser")]
  pub override_on_call_user: Option<ApiUser>,
  /// The start time of the on-call override.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub start: Option<DateTime<Utc>>,
  /// The end time of the on-call override.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub end: Option<DateTime<Utc>>,
  /// The escalation policy associated with this override.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub policy: Option<ApiEscalationPolicy>,
}

/// Represents an on-call roll/rotation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiOnCallRoll {
  /// The start time of the on-call period.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub start: Option<DateTime<Utc>>,
  /// The end time of the on-call period.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub end: Option<DateTime<Utc>>,
  /// The user who is on-call during this period.
  #[serde(skip_serializing_if = "Option::is_none", rename = "onCallUser")]
  pub on_call_user: Option<ApiUser>,
  /// Whether this is a roll/rotation period.
  #[serde(skip_serializing_if = "Option::is_none", rename = "isRoll")]
  pub is_roll: Option<bool>,
}

/// Represents an on-call schedule entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiOnCallEntry {
  /// The user who is scheduled to be on-call.
  #[serde(skip_serializing_if = "Option::is_none", rename = "onCallUser")]
  pub on_call_user: Option<ApiUser>,
  /// The user who is overriding the scheduled on-call user.
  #[serde(skip_serializing_if = "Option::is_none", rename = "overrideOnCallUser")]
  pub override_on_call_user: Option<ApiUser>,
  /// The type of on-call assignment.
  #[serde(skip_serializing_if = "Option::is_none", rename = "onCallType")]
  pub on_call_type: Option<String>,
  /// The name of the rotation this entry belongs to.
  #[serde(skip_serializing_if = "Option::is_none", rename = "rotationName")]
  pub rotation_name: Option<String>,
  /// The name of the shift this entry belongs to.
  #[serde(skip_serializing_if = "Option::is_none", rename = "shiftName")]
  pub shift_name: Option<String>,
  /// The timestamp when the shift roll occurs.
  #[serde(skip_serializing_if = "Option::is_none", rename = "shiftRoll")]
  pub shift_roll: Option<DateTime<Utc>>,
  /// The list of rolls/rotations for this entry.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub rolls: Vec<ApiOnCallRoll>,
}

/// Represents an escalation policy schedule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiEscalationPolicySchedule {
  /// The escalation policy this schedule belongs to.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub policy: Option<ApiEscalationPolicy>,
  /// The schedule entries for this escalation policy.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub schedule: Vec<ApiOnCallEntry>,
  /// The on-call overrides for this escalation policy.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub overrides: Vec<ApiOnCallOverride>,
}

/// Represents a team's on-call schedule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiTeamSchedule {
  /// The team this schedule belongs to.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub team: Option<ApiTeam>,
  /// The escalation policy schedules for this team.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub schedules: Vec<ApiEscalationPolicySchedule>,
}

/// Represents a user's on-call schedule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiUserSchedule {
  /// The team schedules for this user.
  #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "teamSchedules")]
  pub schedules: Vec<ApiTeamSchedule>,
}

/// Request to take on-call duty.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TakeRequest {
  /// The user who is giving up on-call duty.
  #[serde(skip_serializing_if = "Option::is_none", rename = "fromUser")]
  pub from_user: Option<String>,
  /// The user who is taking on-call duty.
  #[serde(skip_serializing_if = "Option::is_none", rename = "toUser")]
  pub to_user: Option<String>,
}

/// Response from taking on-call duty.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TakeResponse {
  /// The result of the take request.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub result: Option<String>,
}

/// Represents an entry in an escalation policy step.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EscalationPolicyStepEntry {
  /// The type of execution for this escalation step.
  #[serde(skip_serializing_if = "Option::is_none", rename = "executionType")]
  pub execution_type: Option<String>,
  /// User information for user-based escalation targets.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub user: Option<std::collections::HashMap<String, String>>,
  /// Rotation group information for rotation-based escalation targets.
  #[serde(skip_serializing_if = "Option::is_none", rename = "rotationGroup")]
  pub rotation_group: Option<std::collections::HashMap<String, String>>,
  /// Webhook information for webhook-based escalation targets.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub webhook: Option<std::collections::HashMap<String, String>>,
  /// Email information for email-based escalation targets.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub email: Option<std::collections::HashMap<String, String>>,
  /// Target policy information for policy-based escalation targets.
  #[serde(skip_serializing_if = "Option::is_none", rename = "targetPolicy")]
  pub target_policy: Option<std::collections::HashMap<String, String>>,
}

/// Represents a step in an escalation policy.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EscalationPolicySteps {
  /// The timeout in seconds before escalating to the next step.
  pub timeout: i32,
  /// The list of entries/targets for this escalation step.
  pub entries: Vec<EscalationPolicyStepEntry>,
}

/// Represents an escalation policy.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EscalationPolicy {
  /// The name of the escalation policy.
  pub name: String,
  /// The team slug/ID that this escalation policy belongs to.
  #[serde(rename = "teamSlug")]
  pub team_id: String,
  /// Whether to ignore custom paging policies.
  #[serde(rename = "ignoreCustomPagingPolicies")]
  pub ignore_custom_paging_policies: bool,
  /// The escalation steps for this policy.
  pub steps: Vec<EscalationPolicySteps>,
  /// The unique slug/ID of the escalation policy.
  #[serde(rename = "slug")]
  pub id: String,
}

/// Represents escalation policy details in a list response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EscalationPolicyListDetail {
  /// The name of the escalation policy.
  pub name: String,
  /// The unique slug identifier of the escalation policy.
  pub slug: String,
}

/// Represents an element in an escalation policy list.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EscalationPolicyListElement {
  /// The escalation policy details.
  pub policy: EscalationPolicyListDetail,
  /// The team details associated with this escalation policy.
  pub team: EscalationPolicyListDetail,
}

/// Response containing a list of escalation policies.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EscalationPolicyList {
  /// The list of escalation policies.
  pub policies: Vec<EscalationPolicyListElement>,
}

/// Represents a routing key for directing alerts.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingKey {
  /// The routing key value used to route alerts.
  #[serde(skip_serializing_if = "Option::is_none", rename = "routingKey")]
  pub routing_key: Option<String>,
  /// The list of targets that this routing key routes to.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub targets: Vec<String>,
}

/// Represents targets in a routing key response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingKeyResponseTargets {
  /// The slug of the escalation policy this routing key targets.
  #[serde(skip_serializing_if = "Option::is_none", rename = "policySlug")]
  pub policy_slug: Option<String>,
}

/// Response containing routing key information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingKeyResponse {
  /// The routing key value.
  #[serde(skip_serializing_if = "Option::is_none", rename = "routingKey")]
  pub routing_key: Option<String>,
  /// The targets that this routing key routes to.
  #[serde(default, skip_serializing_if = "Vec::is_empty")]
  pub targets: Vec<RoutingKeyResponseTargets>,
}

/// Response containing a list of routing keys.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingKeyResponseList {
  /// The list of routing keys.
  #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "routingKeys")]
  pub routing_keys: Vec<RoutingKeyResponse>,
}

/// Types of contact methods available in VictorOps.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContactType {
  /// Phone or SMS contact method.
  Phone,
  /// Email contact method.
  Email,
  /// Mobile device push notification contact method.
  Device,
}

impl ContactType {
  /// Returns the endpoint noun for this contact type.
  pub fn endpoint_noun(&self) -> &'static str {
    match self {
      ContactType::Phone => "phones",
      ContactType::Email => "emails",
      ContactType::Device => "devices",
    }
  }

  /// Creates a ContactType from a notification type string.
  pub fn from_notification_type(notification_type: &str) -> Option<Self> {
    match notification_type {
      "push" => Some(ContactType::Device),
      "email" => Some(ContactType::Email),
      "phone" | "sms" => Some(ContactType::Phone),
      _ => None,
    }
  }
}

/// Represents a contact method for a user.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Contact {
  /// The phone number for phone-based contact methods.
  #[serde(skip_serializing_if = "Option::is_none", rename = "phone")]
  pub phone_number: Option<String>,
  /// The email address for email-based contact methods.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub email: Option<String>,
  /// The label or description of this contact method.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub label: Option<String>,
  /// The priority rank of this contact method.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub rank: Option<i32>,
  /// The external ID of this contact method.
  #[serde(skip_serializing_if = "Option::is_none", rename = "extId")]
  pub ext_id: Option<String>,
  /// The unique identifier of this contact method.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub id: Option<i32>,
  /// The value of this contact method.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub value: Option<String>,
  /// The verification status of this contact method.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub verified: Option<String>,
}

impl Contact {
  /// Determines the contact type based on the contact's fields.
  pub fn contact_type(&self) -> Option<ContactType> {
    if self.phone_number.is_some() {
      Some(ContactType::Phone)
    } else if self.email.is_some() {
      Some(ContactType::Email)
    } else {
      None
    }
  }
}

/// A group of contact methods.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContactGroup {
  /// The list of contact methods in this group.
  #[serde(rename = "contactMethods")]
  pub contact_methods: Vec<Contact>,
}

/// Response containing all contact methods for a user.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AllContactResponse {
  /// The phone contact methods for the user.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub phones: Option<ContactGroup>,
  /// The email contact methods for the user.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub emails: Option<ContactGroup>,
  /// The device contact methods for the user.
  #[serde(skip_serializing_if = "Option::is_none")]
  pub devices: Option<ContactGroup>,
}

/// Response for getting all contacts of a specific type.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetAllContactResponse {
  /// The list of contact methods of the requested type.
  #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "contactMethods")]
  pub contact_methods: Vec<Contact>,
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_contact_type_endpoint_noun() {
    assert_eq!(ContactType::Phone.endpoint_noun(), "phones");
    assert_eq!(ContactType::Email.endpoint_noun(), "emails");
    assert_eq!(ContactType::Device.endpoint_noun(), "devices");
  }

  #[test]
  fn test_contact_type_from_notification_type() {
    assert_eq!(
      ContactType::from_notification_type("push"),
      Some(ContactType::Device)
    );
    assert_eq!(
      ContactType::from_notification_type("email"),
      Some(ContactType::Email)
    );
    assert_eq!(
      ContactType::from_notification_type("phone"),
      Some(ContactType::Phone)
    );
    assert_eq!(
      ContactType::from_notification_type("sms"),
      Some(ContactType::Phone)
    );
    assert_eq!(ContactType::from_notification_type("unknown"), None);
  }

  #[test]
  fn test_contact_contact_type() {
    let phone_contact = Contact {
      phone_number: Some("555-1234".to_string()),
      email: None,
      label: Some("Primary".to_string()),
      rank: Some(1),
      ext_id: None,
      id: None,
      value: None,
      verified: None,
    };
    assert_eq!(phone_contact.contact_type(), Some(ContactType::Phone));

    let email_contact = Contact {
      phone_number: None,
      email: Some("test@example.com".to_string()),
      label: Some("Work".to_string()),
      rank: Some(1),
      ext_id: None,
      id: None,
      value: None,
      verified: None,
    };
    assert_eq!(email_contact.contact_type(), Some(ContactType::Email));

    let empty_contact = Contact {
      phone_number: None,
      email: None,
      label: None,
      rank: None,
      ext_id: None,
      id: None,
      value: None,
      verified: None,
    };
    assert_eq!(empty_contact.contact_type(), None);
  }

  #[test]
  fn test_contact_serialization() {
    let contact = Contact {
      phone_number: Some("555-1234".to_string()),
      email: Some("test@example.com".to_string()),
      label: Some("Primary".to_string()),
      rank: Some(1),
      ext_id: Some("ext123".to_string()),
      id: Some(42),
      value: Some("contact-value".to_string()),
      verified: Some("true".to_string()),
    };

    let json = serde_json::to_string(&contact).unwrap();
    let deserialized: Contact = serde_json::from_str(&json).unwrap();
    
    assert_eq!(deserialized.phone_number, contact.phone_number);
    assert_eq!(deserialized.email, contact.email);
    assert_eq!(deserialized.label, contact.label);
    assert_eq!(deserialized.rank, contact.rank);
  }

  #[test]
  fn test_contact_group_serialization() {
    let contact_group = ContactGroup {
      contact_methods: vec![
        Contact {
          phone_number: Some("555-1234".to_string()),
          email: None,
          label: Some("Primary".to_string()),
          rank: Some(1),
          ext_id: None,
          id: None,
          value: None,
          verified: None,
        }
      ],
    };

    let json = serde_json::to_string(&contact_group).unwrap();
    assert!(json.contains("contactMethods"));
    
    let deserialized: ContactGroup = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized.contact_methods.len(), 1);
  }
}