api/routes/modules/assignments/tickets/ticket_messages/common.rs
1//! Ticket message utilities.
2//!
3//! This module provides helper types and conversions for ticket message endpoints.
4//!
5//! It includes:
6//! - `MessageResponse`: a serializable response type for ticket message API endpoints.
7//! - `UserResponse`: embedded user information for message responses.
8
9use db::models::ticket_messages::Model as TicketMessageModel;
10
11/// Represents a user in the context of a ticket message.
12#[derive(serde::Serialize)]
13pub struct UserResponse {
14 /// User ID
15 pub id: i64,
16 /// Username
17 pub username: String,
18}
19
20/// Represents a ticket message along with its author information.
21#[derive(serde::Serialize)]
22pub struct MessageResponse {
23 /// Message ID
24 pub id: i64,
25 /// Ticket ID this message belongs to
26 pub ticket_id: i64,
27 /// Content of the message
28 pub content: String,
29 /// Creation timestamp in RFC3339 format
30 pub created_at: String,
31 /// Last update timestamp in RFC3339 format
32 pub updated_at: String,
33 /// Optional user info for the author of the message
34 pub user: Option<UserResponse>,
35}
36
37impl From<(TicketMessageModel, String)> for MessageResponse {
38 fn from((message, username): (TicketMessageModel, String)) -> Self {
39 Self {
40 id: message.id,
41 ticket_id: message.ticket_id,
42 content: message.content,
43 created_at: message.created_at.to_rfc3339(),
44 updated_at: message.updated_at.to_rfc3339(),
45 user: Some(UserResponse {
46 id: message.user_id,
47 username,
48 }),
49 }
50 }
51}