backbone-mail 0.2.32

Odoo mail core port — message/notification/followers/activity/alias/sms queue (schema: messaging)
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
//! MailMessage DTOs (Data Transfer Objects)
//!
//! Generated by metaphor-schema. Do not edit manually.
//!
//! DTOs provide a clean separation between domain entities and API
//! representations, with validation and OpenAPI documentation support.

use serde::{Deserialize, Serialize};
use uuid::Uuid;
use chrono::{DateTime, Utc};

#[cfg(feature = "openapi")]
#[cfg(feature = "openapi")]
use utoipa::ToSchema;

#[cfg(feature = "validation")]
use validator::Validate;

use crate::domain::entity::MailMessage;
use crate::domain::entity::AuditMetadata;
use crate::domain::entity::MailMessageType;
use crate::domain::entity::MailModerationStatus;

// =============================================================================
// Create DTO
// =============================================================================

/// Request DTO for creating a new MailMessage
///
/// Used in POST requests to create new entities.
/// Excludes auto-generated fields (id, created_at, updated_at, deleted_at).
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct CreateMailMessageDto {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    pub date: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub body: String,
    #[serde(alias = "message_type")]
    pub message_type: MailMessageType,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "subtype_id")]
    pub subtype_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "is_internal")]
    pub is_internal: bool,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "author_id")]
    pub author_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "author_guest_id")]
    pub author_guest_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "email_from")]
    pub email_from: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "message_id")]
    pub message_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "reply_to")]
    pub reply_to: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "res_id")]
    pub res_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "record_name")]
    pub record_name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "moderation_status")]
    pub moderation_status: Option<MailModerationStatus>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub needaction: bool,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "has_error")]
    pub has_error: bool,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "failure_reason")]
    pub failure_reason: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "rating_value")]
    pub rating_value: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "pinned_at")]
    pub pinned_at: Option<DateTime<Utc>>,
}

// =============================================================================
// Update DTO
// =============================================================================

/// Request DTO for full update of a MailMessage
///
/// Used in PUT requests for full entity replacement.
/// All fields are required (except auto-generated ones).
#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct UpdateMailMessageDto {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    pub date: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub body: String,
    #[serde(alias = "message_type")]
    pub message_type: MailMessageType,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "subtype_id")]
    pub subtype_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "is_internal")]
    pub is_internal: bool,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "author_id")]
    pub author_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "author_guest_id")]
    pub author_guest_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "email_from")]
    pub email_from: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "message_id")]
    pub message_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "reply_to")]
    pub reply_to: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "res_id")]
    pub res_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "record_name")]
    pub record_name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "moderation_status")]
    pub moderation_status: Option<MailModerationStatus>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub needaction: bool,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(alias = "has_error")]
    pub has_error: bool,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "failure_reason")]
    pub failure_reason: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "rating_value")]
    pub rating_value: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "pinned_at")]
    pub pinned_at: Option<DateTime<Utc>>,
}

// =============================================================================
// Patch DTO
// =============================================================================

/// Request DTO for partial update of a MailMessage
///
/// Used in PATCH requests for partial updates.
/// All fields are optional - only provided fields will be updated.
#[derive(Debug, Clone, Default, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "validation", derive(Validate))]
#[serde(rename_all = "camelCase")]
pub struct PatchMailMessageDto {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub date: Option<DateTime<Utc>>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "message_type")]
    pub message_type: Option<MailMessageType>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "subtype_id")]
    pub subtype_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "is_internal")]
    pub is_internal: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "author_id")]
    pub author_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "author_guest_id")]
    pub author_guest_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "email_from")]
    pub email_from: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "message_id")]
    pub message_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "reply_to")]
    pub reply_to: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "res_id")]
    pub res_id: Option<Uuid>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "record_name")]
    pub record_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "moderation_status")]
    pub moderation_status: Option<MailModerationStatus>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub needaction: Option<bool>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    #[serde(skip_serializing_if = "Option::is_none", alias = "has_error")]
    pub has_error: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "failure_reason")]
    pub failure_reason: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "rating_value")]
    pub rating_value: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none", alias = "pinned_at")]
    pub pinned_at: Option<DateTime<Utc>>,
}

impl PatchMailMessageDto {
    /// Check if any field is set
    pub fn has_changes(&self) -> bool {
        self.subject.is_some() || self.date.is_some() || self.body.is_some() || self.message_type.is_some() || self.subtype_id.is_some() || self.is_internal.is_some() || self.author_id.is_some() || self.author_guest_id.is_some() || self.email_from.is_some() || self.message_id.is_some() || self.reply_to.is_some() || self.model.is_some() || self.res_id.is_some() || self.record_name.is_some() || self.moderation_status.is_some() || self.needaction.is_some() || self.has_error.is_some() || self.failure_reason.is_some() || self.rating_value.is_some() || self.pinned_at.is_some()
    }
}

// =============================================================================
// Response DTO
// =============================================================================

/// Response DTO for MailMessage entity
///
/// Used in API responses for single entity.
/// Includes all fields including metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailMessageResponseDto {
    #[cfg_attr(feature = "openapi", schema(example = "550e8400-e29b-41d4-a716-446655440000"))]
    pub id: Uuid,
    pub subject: Option<String>,
    #[cfg_attr(feature = "openapi", schema(example = "2024-01-01T00:00:00Z"))]
    pub date: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(example = "example"))]
    pub body: String,
    pub message_type: MailMessageType,
    pub subtype_id: Option<Uuid>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub is_internal: bool,
    pub author_id: Option<Uuid>,
    pub author_guest_id: Option<Uuid>,
    pub email_from: Option<String>,
    pub message_id: Option<String>,
    pub reply_to: Option<String>,
    pub model: Option<String>,
    pub res_id: Option<Uuid>,
    pub record_name: Option<String>,
    pub moderation_status: Option<MailModerationStatus>,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub needaction: bool,
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub has_error: bool,
    pub failure_reason: Option<String>,
    pub rating_value: Option<f64>,
    pub pinned_at: Option<DateTime<Utc>>,
    pub metadata: AuditMetadata,
}

// =============================================================================
// List Response DTO
// =============================================================================

/// Paginated list response for MailMessage entities
///
/// Used in API responses for list endpoints.
/// Includes pagination metadata.
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailMessageListResponseDto {
    /// List of items
    pub items: Vec<MailMessageResponseDto>,
    /// Total number of items (across all pages)
    pub total: u64,
    /// Current page number (1-indexed)
    pub page: u32,
    /// Number of items per page
    pub per_page: u32,
    /// Total number of pages
    pub total_pages: u32,
    /// Whether there is a next page
    pub has_next: bool,
    /// Whether there is a previous page
    pub has_prev: bool,
}

impl MailMessageListResponseDto {
    /// Create a new list response from items and pagination info
    pub fn new(items: Vec<MailMessageResponseDto>, total: u64, page: u32, per_page: u32) -> Self {
        let total_pages = if per_page > 0 {
            ((total as f64) / (per_page as f64)).ceil() as u32
        } else {
            0
        };
        Self {
            items,
            total,
            page,
            per_page,
            total_pages,
            has_next: page < total_pages,
            has_prev: page > 1,
        }
    }
}

/// Summary DTO for list views (compact version)
#[derive(Debug, Clone, Serialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct MailMessageSummaryDto {
    pub id: Uuid,
    pub subject: Option<String>,
    pub date: DateTime<Utc>,
    pub body: String,
    pub created_at: Option<DateTime<Utc>>,
}

// =============================================================================
// Conversions
// =============================================================================

impl From<MailMessage> for MailMessageResponseDto {
    fn from(entity: MailMessage) -> Self {
        Self {
            id: entity.id,
            subject: entity.subject,
            date: entity.date,
            body: entity.body,
            message_type: entity.message_type,
            subtype_id: entity.subtype_id,
            is_internal: entity.is_internal,
            author_id: entity.author_id,
            author_guest_id: entity.author_guest_id,
            email_from: entity.email_from,
            message_id: entity.message_id,
            reply_to: entity.reply_to,
            model: entity.model,
            res_id: entity.res_id,
            record_name: entity.record_name,
            moderation_status: entity.moderation_status,
            needaction: entity.needaction,
            has_error: entity.has_error,
            failure_reason: entity.failure_reason,
            rating_value: entity.rating_value,
            pinned_at: entity.pinned_at,
            metadata: entity.metadata,
        }
    }
}

impl From<MailMessage> for MailMessageSummaryDto {
    fn from(entity: MailMessage) -> Self {
        let created_at = backbone_core::PersistentEntity::created_at(&entity);
        Self {
            id: entity.id,
            subject: entity.subject,
            date: entity.date,
            body: entity.body,
            created_at,
        }
    }
}

impl From<CreateMailMessageDto> for MailMessage {
    fn from(dto: CreateMailMessageDto) -> Self {
        Self {
            id: Uuid::new_v4(),
            subject: dto.subject,
            date: dto.date,
            body: dto.body,
            message_type: dto.message_type,
            subtype_id: dto.subtype_id,
            is_internal: dto.is_internal,
            author_id: dto.author_id,
            author_guest_id: dto.author_guest_id,
            email_from: dto.email_from,
            message_id: dto.message_id,
            reply_to: dto.reply_to,
            model: dto.model,
            res_id: dto.res_id,
            record_name: dto.record_name,
            moderation_status: dto.moderation_status,
            needaction: dto.needaction,
            has_error: dto.has_error,
            failure_reason: dto.failure_reason,
            rating_value: dto.rating_value,
            pinned_at: dto.pinned_at,
            metadata: AuditMetadata::default(),
        }
    }
}

impl From<&MailMessage> for MailMessageResponseDto {
    fn from(entity: &MailMessage) -> Self {
        Self {
            id: entity.id.clone(),
            subject: entity.subject.clone(),
            date: entity.date.clone(),
            body: entity.body.clone(),
            message_type: entity.message_type.clone(),
            subtype_id: entity.subtype_id.clone(),
            is_internal: entity.is_internal.clone(),
            author_id: entity.author_id.clone(),
            author_guest_id: entity.author_guest_id.clone(),
            email_from: entity.email_from.clone(),
            message_id: entity.message_id.clone(),
            reply_to: entity.reply_to.clone(),
            model: entity.model.clone(),
            res_id: entity.res_id.clone(),
            record_name: entity.record_name.clone(),
            moderation_status: entity.moderation_status.clone(),
            needaction: entity.needaction.clone(),
            has_error: entity.has_error.clone(),
            failure_reason: entity.failure_reason.clone(),
            rating_value: entity.rating_value.clone(),
            pinned_at: entity.pinned_at.clone(),
            metadata: entity.metadata.clone(),
        }
    }
}

impl backbone_core::FromCreateDto<CreateMailMessageDto> for MailMessage {
    fn from_create_dto(dto: CreateMailMessageDto) -> backbone_core::ServiceResult<Self> {
        Ok(MailMessage::from(dto))
    }
}

impl backbone_core::ApplyUpdateDto<UpdateMailMessageDto> for MailMessage {
    fn apply_update(mut self, dto: UpdateMailMessageDto) -> backbone_core::ServiceResult<Self> {
        self.subject = dto.subject;
        self.date = dto.date;
        self.body = dto.body;
        self.message_type = dto.message_type;
        self.subtype_id = dto.subtype_id;
        self.is_internal = dto.is_internal;
        self.author_id = dto.author_id;
        self.author_guest_id = dto.author_guest_id;
        self.email_from = dto.email_from;
        self.message_id = dto.message_id;
        self.reply_to = dto.reply_to;
        self.model = dto.model;
        self.res_id = dto.res_id;
        self.record_name = dto.record_name;
        self.moderation_status = dto.moderation_status;
        self.needaction = dto.needaction;
        self.has_error = dto.has_error;
        self.failure_reason = dto.failure_reason;
        self.rating_value = dto.rating_value;
        self.pinned_at = dto.pinned_at;
        Ok(self)
    }
}

// =============================================================================
// Custom DTOs
// =============================================================================

// <<< CUSTOM DTOs
// Add custom DTOs specific to MailMessage here.
// This section will be preserved during regeneration.
// >>> END CUSTOM DTOs