zammad-api 0.4.0

Async, idiomatic Rust client for the Zammad Service Platform
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
//! Strongly typed Zammad entities.
//!
//! Every record type carries an `extra` field that captures custom attributes
//! Zammad returns but that aren't covered by the typed schema. Use it to
//! read or write Object-Manager custom fields without having to redefine the
//! whole model.
//!
//! For tickets the crate exposes two shapes: the compact [`Ticket`] (default)
//! and [`TicketExpanded`], which carries human-readable name strings
//! Zammad adds when `expand=true` is sent. Select between them at call site
//! via [`crate::api::TicketsApi::list`] vs [`crate::api::TicketsApi::list_expanded`].

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

/// A Zammad ticket (compact form - IDs only).
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Ticket {
    /// Numeric ticket ID.
    pub id: u64,
    /// Customer-facing ticket number (e.g. `"100123"`).
    #[serde(default)]
    pub number: Option<String>,
    /// Ticket subject / title.
    #[serde(default)]
    pub title: Option<String>,
    /// ID of the assigned group (queue).
    #[serde(default)]
    pub group_id: Option<u64>,
    /// ID of the current state (`new`, `open`, `closed`, ...).
    #[serde(default)]
    pub state_id: Option<u64>,
    /// ID of the assigned priority.
    #[serde(default)]
    pub priority_id: Option<u64>,
    /// ID of the customer this ticket belongs to.
    #[serde(default)]
    pub customer_id: Option<u64>,
    /// ID of the agent currently owning the ticket.
    #[serde(default)]
    pub owner_id: Option<u64>,
    /// ID of the customer's organization, if any.
    #[serde(default)]
    pub organization_id: Option<u64>,
    /// Free-form internal note attached to the ticket.
    #[serde(default)]
    pub note: Option<String>,
    /// When the ticket was created.
    #[serde(default)]
    pub created_at: Option<DateTime<Utc>>,
    /// When the ticket was last updated.
    #[serde(default)]
    pub updated_at: Option<DateTime<Utc>>,
    /// Any attribute Zammad returned that isn't modelled above (typically
    /// Object-Manager custom fields).
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

/// A Zammad ticket returned with `expand=true`.
///
/// In addition to the numeric `*_id` fields carried by [`Ticket`], the
/// server adds human-readable name strings for group, state, priority,
/// customer, owner and organization.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TicketExpanded {
    /// Numeric ticket ID.
    pub id: u64,
    /// Customer-facing ticket number.
    #[serde(default)]
    pub number: Option<String>,
    /// Ticket subject / title.
    #[serde(default)]
    pub title: Option<String>,
    /// ID of the assigned group.
    #[serde(default)]
    pub group_id: Option<u64>,
    /// Name of the assigned group, e.g. `"Users"`.
    #[serde(default)]
    pub group: Option<String>,
    /// ID of the current state.
    #[serde(default)]
    pub state_id: Option<u64>,
    /// Name of the current state, e.g. `"open"`.
    #[serde(default)]
    pub state: Option<String>,
    /// ID of the assigned priority.
    #[serde(default)]
    pub priority_id: Option<u64>,
    /// Name of the assigned priority, e.g. `"2 normal"`.
    #[serde(default)]
    pub priority: Option<String>,
    /// ID of the customer.
    #[serde(default)]
    pub customer_id: Option<u64>,
    /// Login / email of the customer.
    #[serde(default)]
    pub customer: Option<String>,
    /// ID of the owning agent.
    #[serde(default)]
    pub owner_id: Option<u64>,
    /// Login / email of the owning agent.
    #[serde(default)]
    pub owner: Option<String>,
    /// ID of the customer's organization.
    #[serde(default)]
    pub organization_id: Option<u64>,
    /// Name of the customer's organization.
    #[serde(default)]
    pub organization: Option<String>,
    /// Free-form internal note.
    #[serde(default)]
    pub note: Option<String>,
    /// When the ticket was created.
    #[serde(default)]
    pub created_at: Option<DateTime<Utc>>,
    /// When the ticket was last updated.
    #[serde(default)]
    pub updated_at: Option<DateTime<Utc>>,
    /// Any attribute Zammad returned that isn't modelled above.
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

/// A Zammad user (agent or customer).
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct User {
    /// Numeric user ID.
    pub id: u64,
    /// Login (often the email).
    #[serde(default)]
    pub login: Option<String>,
    /// Given name.
    #[serde(default)]
    pub firstname: Option<String>,
    /// Family name.
    #[serde(default)]
    pub lastname: Option<String>,
    /// Primary email.
    #[serde(default)]
    pub email: Option<String>,
    /// Personal website URL.
    #[serde(default)]
    pub web: Option<String>,
    /// Landline phone.
    #[serde(default)]
    pub phone: Option<String>,
    /// Mobile phone.
    #[serde(default)]
    pub mobile: Option<String>,
    /// Fax number.
    #[serde(default)]
    pub fax: Option<String>,
    /// Free-form internal note.
    #[serde(default)]
    pub note: Option<String>,
    /// ID of the primary organization, if any.
    #[serde(default)]
    pub organization_id: Option<u64>,
    /// Whether the account is active.
    #[serde(default)]
    pub active: Option<bool>,
    /// Whether the email is verified.
    #[serde(default)]
    pub verified: Option<bool>,
    /// When the user was created.
    #[serde(default)]
    pub created_at: Option<DateTime<Utc>>,
    /// When the user was last updated.
    #[serde(default)]
    pub updated_at: Option<DateTime<Utc>>,
    /// Any attribute not modelled above (incl. custom fields).
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

/// Payload for `POST /api/v1/users`.
#[derive(Debug, Clone, Default, Serialize)]
pub struct NewUser {
    /// Given name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub firstname: Option<String>,
    /// Family name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lastname: Option<String>,
    /// Primary email.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    /// Login (defaults server-side to the email).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub login: Option<String>,
    /// Landline phone.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub phone: Option<String>,
    /// Mobile phone.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mobile: Option<String>,
    /// Organization name (Zammad resolves it to an ID).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub organization: Option<String>,
    /// Role names, e.g. `["Customer"]` or `["Agent"]`.
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub roles: Vec<String>,
    /// Additional fields (custom attributes, etc.).
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

impl NewUser {
    /// Convenience constructor seeded with the primary email.
    pub fn with_email(email: impl Into<String>) -> Self {
        Self {
            email: Some(email.into()),
            ..Self::default()
        }
    }

    /// Set the given name.
    pub fn firstname(mut self, s: impl Into<String>) -> Self {
        self.firstname = Some(s.into());
        self
    }
    /// Set the family name.
    pub fn lastname(mut self, s: impl Into<String>) -> Self {
        self.lastname = Some(s.into());
        self
    }
    /// Set the phone.
    pub fn phone(mut self, s: impl Into<String>) -> Self {
        self.phone = Some(s.into());
        self
    }
    /// Append a role.
    pub fn role(mut self, s: impl Into<String>) -> Self {
        self.roles.push(s.into());
        self
    }
    /// Set an arbitrary custom field on the new user.
    pub fn field(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
        self.extra.insert(key.into(), value.into());
        self
    }
}

/// A ticket article (note, email, phone log, ...).
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Article {
    /// Numeric article ID.
    pub id: u64,
    /// ID of the parent ticket.
    #[serde(default)]
    pub ticket_id: Option<u64>,
    /// Subject line.
    #[serde(default)]
    pub subject: Option<String>,
    /// Article body (plain text or HTML depending on `content_type`).
    #[serde(default)]
    pub body: Option<String>,
    /// Sender address (free-form string Zammad chose).
    #[serde(default)]
    pub from: Option<String>,
    /// Recipient address.
    #[serde(default)]
    pub to: Option<String>,
    /// CC recipients.
    #[serde(default)]
    pub cc: Option<String>,
    /// Reply-To header.
    #[serde(default)]
    pub reply_to: Option<String>,
    /// MIME content type (`text/plain`, `text/html`).
    #[serde(default)]
    pub content_type: Option<String>,
    /// Article type (`note`, `email`, `phone`, ...).
    #[serde(rename = "type", default)]
    pub article_type: Option<String>,
    /// Sender role (`Agent`, `Customer`, `System`).
    #[serde(default)]
    pub sender: Option<String>,
    /// Whether the article is hidden from customers.
    #[serde(default)]
    pub internal: Option<bool>,
    /// When the article was created.
    #[serde(default)]
    pub created_at: Option<DateTime<Utc>>,
    /// When the article was last updated.
    #[serde(default)]
    pub updated_at: Option<DateTime<Utc>>,
    /// File attachments on the article.
    #[serde(default)]
    pub attachments: Vec<Attachment>,
    /// Any attribute not modelled above.
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

/// Payload for `POST /api/v1/ticket_articles` and inline `article` on ticket creation.
#[derive(Debug, Clone, Default, Serialize)]
pub struct NewArticle {
    /// Subject line.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,
    /// Article body.
    pub body: String,
    /// Article type, e.g. `note`, `email`, `phone`, `web`, `sms`.
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub article_type: Option<String>,
    /// Whether the article should be hidden from customers.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub internal: Option<bool>,
    /// MIME content type - `text/plain` (default server-side) or `text/html`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_type: Option<String>,
    /// Sender role override (`Agent`, `Customer`, `System`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sender: Option<String>,
    /// From address (mail-type articles).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,
    /// To address (mail-type articles).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<String>,
    /// CC addresses.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cc: Option<String>,
    /// Inline attachments.
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub attachments: Vec<NewAttachment>,
    /// Parent ticket - filled in automatically by
    /// [`crate::api::ArticlesApi::create`], not needed when used inline.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ticket_id: Option<u64>,
}

impl NewArticle {
    /// Build with the given body. Without an explicit `article_type` the
    /// server defaults to a note.
    pub fn new(body: impl Into<String>) -> Self {
        Self {
            body: body.into(),
            ..Self::default()
        }
    }

    /// Set the subject line.
    pub fn subject(mut self, s: impl Into<String>) -> Self {
        self.subject = Some(s.into());
        self
    }
    /// Set the article type (`note`, `email`, `phone`, `web`, `sms`, ...).
    pub fn article_type(mut self, s: impl Into<String>) -> Self {
        self.article_type = Some(s.into());
        self
    }
    /// Set the MIME content type (`text/plain` or `text/html`).
    pub fn content_type(mut self, s: impl Into<String>) -> Self {
        self.content_type = Some(s.into());
        self
    }
    /// Mark the article as internal (hidden from customers).
    pub fn internal(mut self, b: bool) -> Self {
        self.internal = Some(b);
        self
    }
    /// Override the sender role.
    pub fn sender(mut self, s: impl Into<String>) -> Self {
        self.sender = Some(s.into());
        self
    }
    /// Set the From address.
    pub fn from(mut self, s: impl Into<String>) -> Self {
        self.from = Some(s.into());
        self
    }
    /// Set the To address.
    pub fn to(mut self, s: impl Into<String>) -> Self {
        self.to = Some(s.into());
        self
    }
    /// Set the CC addresses.
    pub fn cc(mut self, s: impl Into<String>) -> Self {
        self.cc = Some(s.into());
        self
    }
    /// Append an attachment.
    pub fn attach(mut self, a: NewAttachment) -> Self {
        self.attachments.push(a);
        self
    }
}

/// Attachment metadata returned by Zammad.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Attachment {
    /// Numeric attachment ID.
    pub id: u64,
    /// File name as uploaded.
    #[serde(default)]
    pub filename: Option<String>,
    /// File size - Zammad reports this as a stringified integer.
    #[serde(default)]
    pub size: Option<String>,
    /// Server-side preferences blob (content type, content disposition, ...).
    #[serde(default)]
    pub preferences: Option<Value>,
}

/// Attachment payload for outbound articles. `data` is base64-encoded.
#[derive(Debug, Clone, Serialize)]
pub struct NewAttachment {
    /// File name to show in Zammad.
    pub filename: String,
    /// Base64-encoded file content.
    pub data: String,
    /// MIME type, e.g. `text/plain`, `application/pdf`.
    #[serde(rename = "mime-type")]
    pub mime_type: String,
}

impl NewAttachment {
    /// Build from raw bytes - performs base64 encoding for you.
    pub fn from_bytes(
        filename: impl Into<String>,
        mime_type: impl Into<String>,
        bytes: &[u8],
    ) -> Self {
        use base64::Engine;
        Self {
            filename: filename.into(),
            mime_type: mime_type.into(),
            data: base64::engine::general_purpose::STANDARD.encode(bytes),
        }
    }
}

/// A Zammad organization.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Organization {
    /// Numeric organization ID.
    pub id: u64,
    /// Display name.
    #[serde(default)]
    pub name: Option<String>,
    /// Whether the organization is shared with all agents.
    #[serde(default)]
    pub shared: Option<bool>,
    /// Email domain used for automatic assignment.
    #[serde(default)]
    pub domain: Option<String>,
    /// Whether new users with `@domain` emails get auto-assigned.
    #[serde(default)]
    pub domain_assignment: Option<bool>,
    /// Whether the organization is active.
    #[serde(default)]
    pub active: Option<bool>,
    /// Free-form internal note.
    #[serde(default)]
    pub note: Option<String>,
    /// When the organization was created.
    #[serde(default)]
    pub created_at: Option<DateTime<Utc>>,
    /// When the organization was last updated.
    #[serde(default)]
    pub updated_at: Option<DateTime<Utc>>,
    /// Any attribute not modelled above (incl. custom fields).
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

/// Payload for `POST /api/v1/organizations`.
#[derive(Debug, Clone, Default, Serialize)]
pub struct NewOrganization {
    /// Display name (required).
    pub name: String,
    /// Email domain used for automatic assignment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub domain: Option<String>,
    /// Whether to share the organization with all agents.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shared: Option<bool>,
    /// Free-form internal note.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
    /// Additional fields (custom attributes, etc.).
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

impl NewOrganization {
    /// Build with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ..Self::default()
        }
    }
}

/// A Zammad group (queue).
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Group {
    /// Numeric group ID.
    pub id: u64,
    /// Display name.
    #[serde(default)]
    pub name: Option<String>,
    /// ID of the signature attached to outbound mail from this group.
    #[serde(default)]
    pub signature_id: Option<u64>,
    /// ID of the email-address record this group sends from.
    #[serde(default)]
    pub email_address_id: Option<u64>,
    /// Follow-up policy, e.g. `"yes"` / `"new_ticket"`.
    #[serde(default)]
    pub follow_up_possible: Option<String>,
    /// Whether follow-ups are reassigned to the previous owner.
    #[serde(default)]
    pub follow_up_assignment: Option<bool>,
    /// Whether the group is active.
    #[serde(default)]
    pub active: Option<bool>,
    /// Free-form note.
    #[serde(default)]
    pub note: Option<String>,
    /// Any attribute not modelled above.
    #[serde(flatten)]
    pub extra: Map<String, Value>,
}

/// A configurable ticket state.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TicketState {
    /// Numeric state ID.
    pub id: u64,
    /// Display name (`new`, `open`, `closed`, ...).
    #[serde(default)]
    pub name: Option<String>,
    /// ID of the underlying state type.
    #[serde(default)]
    pub state_type_id: Option<u64>,
    /// Whether tickets in this state ignore escalation timers.
    #[serde(default)]
    pub ignore_escalation: Option<bool>,
    /// Whether this is the default state on ticket creation.
    #[serde(default)]
    pub default_create: Option<bool>,
    /// Whether this is the default state on customer follow-up.
    #[serde(default)]
    pub default_follow_up: Option<bool>,
    /// Whether the state is active.
    #[serde(default)]
    pub active: Option<bool>,
    /// Free-form note.
    #[serde(default)]
    pub note: Option<String>,
}

/// A configurable ticket priority.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TicketPriority {
    /// Numeric priority ID.
    pub id: u64,
    /// Display name (`1 low`, `2 normal`, `3 high`, ...).
    #[serde(default)]
    pub name: Option<String>,
    /// Whether this is the default priority on ticket creation.
    #[serde(default)]
    pub default_create: Option<bool>,
    /// Icon class shown in the UI.
    #[serde(default)]
    pub ui_icon: Option<String>,
    /// Color used in the UI.
    #[serde(default)]
    pub ui_color: Option<String>,
    /// Whether the priority is active.
    #[serde(default)]
    pub active: Option<bool>,
    /// Free-form note.
    #[serde(default)]
    pub note: Option<String>,
}

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

    #[test]
    fn attachment_encodes_bytes_as_base64() {
        let a = NewAttachment::from_bytes("hi.txt", "text/plain", b"foobar");
        assert_eq!(a.data, "Zm9vYmFy");
    }
}