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
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderNotesCreate {
    note: String,
    customer_note: bool,
    added_by_user: Option<bool>,
}
#[derive(Default)]
pub struct NoNote;
pub struct WithNote(String);
#[derive(Default)]
pub struct OrderNotesCreateBuilder<T> {
    note: T,
    customer_note: Option<bool>,
    added_by_user: Option<bool>,
}
impl<T> OrderNotesCreateBuilder<T> {
    pub fn new() -> OrderNotesCreateBuilder<NoNote> {
        OrderNotesCreateBuilder::default()
    }
    /// Order note content.
    pub fn note(self, note: impl Into<String>) -> OrderNotesCreateBuilder<WithNote> {
        OrderNotesCreateBuilder {
            note: WithNote(note.into()),
            customer_note: self.customer_note,
            added_by_user: self.customer_note,
        }
    }
    /// If true, the note will be shown to customers and they will be notified. If false, the note will be for admin reference only. Default is false.    
    pub fn customer_note(mut self) -> Self {
        let _ = self.customer_note.insert(true);
        self
    }
    /// If true, this note will be attributed to the current user. If false, the note will be attributed to the system. Default is false.    
    pub fn added_by_user(mut self) -> Self {
        let _ = self.added_by_user.insert(true);
        self
    }
}
impl OrderNotesCreateBuilder<WithNote> {
    pub fn build(self) -> OrderNotesCreate {
        OrderNotesCreate {
            note: self.note.0,
            customer_note: self.customer_note.unwrap_or_default(),
            added_by_user: self.added_by_user,
        }
    }
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct OrderNotesUpdate {
    id: i32,
    note: Option<String>,
    customer_note: Option<bool>,
    added_by_user: Option<bool>,
}
#[derive(Default)]
pub struct NoId;
pub struct WithId(i32);
#[derive(Default)]
pub struct OrderNotesUpdateBuilder<I> {
    id: I,
    note: Option<String>,
    customer_note: Option<bool>,
    added_by_user: Option<bool>,
}
impl<I> OrderNotesUpdateBuilder<I> {
    pub fn new() -> OrderNotesUpdateBuilder<NoId> {
        OrderNotesUpdateBuilder::default()
    }
    /// Unique identifier for the resource.
    pub fn id(self, id: i32) -> OrderNotesUpdateBuilder<WithId> {
        OrderNotesUpdateBuilder {
            id: WithId(id),
            note: self.note,
            customer_note: self.customer_note,
            added_by_user: self.added_by_user,
        }
    }
    /// Order note content.
    pub fn note(mut self, note: impl Into<String>) -> Self {
        let _ = self.note.insert(note.into());
        self
    }
    /// If true, the note will be shown to customers and they will be notified. If false, the note will be for admin reference only. Default is false.    
    pub fn customer_note(mut self) -> Self {
        let _ = self.customer_note.insert(true);
        self
    }
    /// If true, this note will be attributed to the current user. If false, the note will be attributed to the system. Default is false.    
    pub fn added_by_user(mut self) -> Self {
        let _ = self.added_by_user.insert(true);
        self
    }
}
impl OrderNotesUpdateBuilder<WithId> {
    pub fn build(self) -> OrderNotesUpdate {
        OrderNotesUpdate {
            id: self.id.0,
            note: self.note,
            customer_note: self.customer_note,
            added_by_user: self.added_by_user,
        }
    }
}
#[cfg(test)]
mod tests {
    use crate::{
        controllers::orders::ORDER_ID, order_notes::OrderNotes, ApiClient, Entity, SubEntity,
    };

    #[tokio::test]
    async fn test_list_all_order_notes() {
        let client = ApiClient::from_env().unwrap();
        let order_notes: Vec<OrderNotes> = client
            .list_all_subentities(Entity::Order, ORDER_ID, SubEntity::OrderNote)
            .await
            .unwrap();
        assert!(!order_notes.is_empty());
    }
    #[tokio::test]
    async fn test_retrieve_order_note() {
        let client = ApiClient::from_env().unwrap();
        let order_notes: Vec<OrderNotes> = client
            .list_all_subentities(Entity::Order, ORDER_ID, SubEntity::OrderNote)
            .await
            .unwrap();
        let id = order_notes.last().unwrap().id;
        let order_note: OrderNotes = client
            .retrieve_subentity(Entity::Order, ORDER_ID, SubEntity::OrderNote, id)
            .await
            .unwrap();
        assert_eq!(id, order_note.id);
    }
    #[tokio::test]
    async fn test_create_order_note() {
        let client = ApiClient::from_env().unwrap();
        let note = OrderNotes::create()
            .note("Testing note")
            .customer_note()
            .added_by_user()
            .build();
        let created_note: OrderNotes = client
            .create_subentity(Entity::Order, ORDER_ID, SubEntity::OrderNote, note)
            .await
            .unwrap();
        assert_eq!(created_note.customer_note, true);
        let _deleted: OrderNotes = client
            .delete_subentity(
                Entity::Order,
                ORDER_ID,
                SubEntity::OrderNote,
                created_note.id,
            )
            .await
            .unwrap();
    }
    #[tokio::test]
    async fn test_update_order_note() {
        let client = ApiClient::from_env().unwrap();
        let order_notes: Vec<OrderNotes> = client
            .list_all_subentities(Entity::Order, ORDER_ID, SubEntity::OrderNote)
            .await
            .unwrap();
        let id = order_notes.first().unwrap().id;
        let update = OrderNotes::update().id(id).note("some test note").build();
        let updated_note: Result<OrderNotes, _> = client
            .update_subentity(Entity::Order, ORDER_ID, SubEntity::OrderNote, id, update)
            .await;
        assert!(updated_note.is_err())
    }
}