monzo/endpoints/
accounts.rs

1//! Accounts API endpoint
2
3use chrono::{DateTime, Utc};
4use serde::Deserialize;
5
6/// A struct representing a Monzo Account
7#[derive(Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
8#[non_exhaustive]
9pub struct Account {
10    /// The unique ID of the account
11    pub id: String,
12
13    /// The account details including type and banking information
14    #[serde(flatten)]
15    pub account_type: Type,
16
17    /// Whether the account has been closed
18    pub closed: bool,
19
20    /// The `DateTime` that the account was created
21    pub created: DateTime<Utc>,
22
23    /// The account description
24    pub description: String,
25
26    /// This the a three-letter currency code
27    pub currency: String,
28
29    /// This is a country code for the country where the account is held
30    pub country_code: String,
31
32    /// A vector of account owners
33    pub owners: Vec<Owner>,
34
35    /// The business ID
36    ///
37    /// This is only set for business accounts
38    pub business_id: Option<String>,
39}
40
41/// Struct representating an owner of a Monzo account
42#[derive(Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
43pub struct Owner {
44    /// The user ID of the owner
45    pub user_id: String,
46
47    /// The preferred name of the owner
48    pub preferred_name: String,
49
50    /// The preferred first name of the owner
51    pub preferred_first_name: String,
52}
53
54/// Account details including type and banking information
55#[derive(Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
56#[serde(tag = "type", rename_all = "snake_case")]
57#[non_exhaustive]
58pub enum Type {
59    /// A standard monzo account
60    UkRetail(AccountDetails),
61
62    /// A monzo joint account
63    UkRetailJoint(AccountDetails),
64
65    /// A monzo business account
66    UkBusiness(AccountDetails),
67
68    /// A monzo rewards account
69    UkRewards,
70
71    /// A monzo flex account
72    UkMonzoFlex,
73}
74
75#[derive(Deserialize, Debug, PartialEq, Eq, Hash, Clone)]
76/// Banking information
77pub struct AccountDetails {
78    /// The account number
79    pub account_number: String,
80    /// The sort code
81    pub sort_code: String,
82}
83
84pub(crate) use list::Request as List;
85mod list {
86
87    use crate::endpoints::Endpoint;
88
89    /// An object representing a request to the Monzo API for a list of accounts
90    pub struct Request;
91
92    impl Endpoint for Request {
93        const METHOD: reqwest::Method = reqwest::Method::GET;
94
95        fn endpoint(&self) -> &'static str {
96            "/accounts"
97        }
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use test_case::test_case;
104
105    use super::Account;
106
107    #[test_case(
108        r#"
109            {
110      "id": "acc_00009",
111      "closed": false,
112      "created": "2021-06-12T00:00:00.000Z",
113      "description": "user_00009",
114      "type": "uk_retail",
115      "owner_type": "personal",
116      "is_flex": false,
117      "product_type": "standard",
118      "closed_account_app_access": false,
119      "currency": "GBP",
120      "legal_entity": "monzo_uk",
121      "country_code": "GB",
122      "country_code_alpha3": "GBR",
123      "owners": [
124        {
125          "user_id": "user_0000",
126          "preferred_name": "First Last",
127          "preferred_first_name": "First"
128        }
129      ],
130      "account_number": "12345678",
131      "sort_code": "040004",
132      "payment_details": {
133        "locale_uk": {
134          "account_number": "12345678",
135          "sort_code": "040004"
136        },
137        "iban": {
138          "unformatted": "GB90MONZXXXXXXXXXXX",
139          "formatted": "GB90MONZXXXXXXXXXXXXX",
140          "bic": "MONZGB2L",
141          "usage_description": "Receive international payments in over 40 currencies. We charge a currency conversion fee. [Learn more...](monzo://backend_screen?id=international-payment-views:bank-transfer-info&instance_params_id=acc_00009jmHyLkxAPVUSe8H45)",
142          "usage_description_web": "Receive international payments in over 40 currencies. We charge a currency conversion fee."
143        }
144      },
145      "monzo_branch_address_formatted": "Monzo Bank, Broadwalk House, 5 Appold St, London EC2A 2AG, United Kingdom",
146      "assets": {
147        "image_url": "https://public-images.monzo.com/card_styles/account_icon/personal@3x.png"
148      }
149    }
150        "#
151        ; "uk_retail"
152    )]
153    #[test_case(
154        r#"{
155            "id": "acc_ID",
156            "closed": false,
157            "created": "2024-01-20T00:00:00.000Z",
158            "description": "rewardsoptin_0000",
159            "type": "uk_rewards",
160            "owner_type": "personal",
161            "is_flex": false,
162            "product_type": "rewards",
163            "closed_account_app_access": false,
164            "currency": "GBP",
165            "legal_entity": "monzo_uk",
166            "country_code": "GB",
167            "country_code_alpha3": "GBR",
168            "owners": [
169                {
170                    "user_id": "user_0000",
171                    "preferred_name": "First Last",
172                    "preferred_first_name": "First"
173                }
174            ]
175        }"#
176        ; "uk_rewards"
177    )]
178    #[test_case(
179        r#"{
180            "id": "acc_0000",
181            "closed": false,
182            "created": "2024-01-01T00:00:00.000Z",
183            "description": "monzoflex_0000",
184            "type": "uk_monzo_flex",
185            "owner_type": "personal",
186            "is_flex": true,
187            "product_type": "flex",
188            "closed_account_app_access": false,
189            "currency": "GBP",
190            "legal_entity": "monzo_uk",
191            "country_code": "GB",
192            "country_code_alpha3": "GBR",
193            "owners": [
194                {
195                    "user_id": "user_0000",
196                    "preferred_name": "First Last",
197                    "preferred_first_name": "First"
198                }
199            ],
200            "assets": {
201                "image_url": "https://public-images.monzo.com/card_styles/account_icon/flex@3x.png"
202            }
203        }"#
204        ; "uk_monzo_flex"
205    )]
206    fn parse_account(json_data: &str) {
207        let _account: Account = serde_json::from_str(json_data).unwrap();
208    }
209}