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
use actix_web::{HttpResponse, ResponseError};
use chrono::prelude::*;
use derive_more::Display;
use serde::{Deserialize, Serialize};
use validator::Validate;

#[derive(Serialize, Deserialize, Debug, Clone, Validate)]
pub struct UpdateMyAccountBody {
    pub id: String,
    #[validate(email)]
    pub email: Option<String>,
    pub informations: Option<UserInformationsBody>,
    pub contact: Option<UserContactBody>,
    pub account_settings: Option<AccountSettingsBody>,
    pub company: Option<UpdateCompanyBody>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserInformationsBody {
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub birthday: Option<DateTime<Utc>>,
    pub gender: Option<String>,
    pub civil_status: Option<String>,
    pub picture: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserContactBody {
    pub address: Option<UpdateMyAccountAddressBody>,
    pub phone: Option<UserPhoneBody>,
    pub socials: Option<Vec<UserSocialBody>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateMyAccountAddressBody {
    pub id: Option<String>,
    pub road_names: Option<Vec<UpdateAddressRoadNameBody>>,
    pub postal_code: Option<i32>,
    pub country: Option<String>,
    pub city: Option<String>,
    pub location: Option<AddressLocationBody>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserPhoneBody {
    pub country_code: Option<i32>,
    pub number: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UserSocialBody {
    pub kind: Option<String>,
    pub value: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AccountSettingsBody {
    pub language: Option<String>,
    pub currency: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateCompanyBody {
    pub id: Option<String>,
    pub short_names: Option<Vec<UpdateCompanyShortNameBody>>,
    pub long_names: Option<Vec<UpdateCompanyLongNameBody>>,
    pub descriptions: Option<Vec<UpdateCompanyDescriptionBody>>,
    pub activities: Option<Vec<CompanyActivityBody>>,
    pub fiscal: Option<UpdateCompanyFiscalBody>,
    pub logo: Option<String>,
    pub address: Option<UpdateCompanyAddressBody>,
    pub website: Option<String>,
    pub socials: Option<Vec<CompanySocialBody>>,
    pub contacts: Option<Vec<CompanyContactBody>>,
    pub characteristics: Option<Vec<CompanyCharacteristicBody>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateCompanyShortNameBody {
    pub id: Option<String>,
    pub language_code: Option<String>,
    pub value: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateCompanyLongNameBody {
    pub id: Option<String>,
    pub language_code: Option<String>,
    pub value: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateCompanyDescriptionBody {
    pub id: Option<String>,
    pub language_code: Option<String>,
    pub value: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CompanyActivityBody {
    pub activity: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateCompanyFiscalBody {
    pub corporate_names: Option<Vec<UpdateCompanyCorporateNameBody>>,
    pub legal_form: Option<String>,
    pub matriculation: Option<String>,
    pub tax_code: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateCompanyCorporateNameBody {
    pub id: Option<String>,
    pub language_code: Option<String>,
    pub value: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateCompanyAddressBody {
    pub id: Option<String>,
    pub road_names: Option<Vec<UpdateAddressRoadNameBody>>,
    pub postal_code: Option<i32>,
    pub country: Option<String>,
    pub city: Option<String>,
    pub location: Option<AddressLocationBody>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateAddressRoadNameBody {
    pub id: Option<String>,
    pub language_code: Option<String>,
    pub value: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AddressLocationBody {
    pub latitude: f64,
    pub longitude: f64,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CompanySocialBody {
    pub kind: Option<String>,
    pub value: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CompanyContactBody {
    pub level: Option<i32>,
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub phone: Option<CompanyPhoneBody>,
    pub email: Option<String>,
    pub position: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CompanyPhoneBody {
    pub country_code: Option<i32>,
    pub number: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CompanyCharacteristicBody {
    pub kind: Option<String>,
    pub value: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateMyAccountResult {
    pub success: bool,
}

#[derive(Debug, Display)]
pub enum UpdateMyAccountError {
    #[display(fmt = "invalid_object_id")]
    InvalidObjectId,
    #[display(fmt = "user_not_found")]
    UserNotFound,
    #[display(fmt = "email_already_exists")]
    EmailExists,
    #[display(fmt = "domain_is_not_alphabectic")]
    DomainIsNotAplhabetic,
    #[display(fmt = "domain_contains_number")]
    DomainContainsNumber,
    #[display(fmt = "domain_is_reserved")]
    DomainIsReserved,
    #[display(fmt = "domain_is_too_short")]
    DomainIsTooShort,
    #[display(fmt = "domain_already_exists")]
    DomainExists,
    Default(String),
}

impl ResponseError for UpdateMyAccountError {
    fn error_response(&self) -> HttpResponse {
        match self {
            UpdateMyAccountError::InvalidObjectId => {
                HttpResponse::NotAcceptable().body("invalid_object_id")
            }
            UpdateMyAccountError::UserNotFound => HttpResponse::Conflict().body("user_not_found"),
            UpdateMyAccountError::EmailExists => HttpResponse::Conflict().body("email_already_exists"),
            UpdateMyAccountError::DomainIsNotAplhabetic => {
                HttpResponse::NotAcceptable().body("domain_is_not_alphabectic")
            }
            UpdateMyAccountError::DomainContainsNumber => {
                HttpResponse::NotAcceptable().body("domain_contains_number")
            }
            UpdateMyAccountError::DomainIsReserved => {
                HttpResponse::NotAcceptable().body("domain_is_reserved")
            }
            UpdateMyAccountError::DomainIsTooShort => {
                HttpResponse::NotAcceptable().body("domain_is_too_short")
            }
            UpdateMyAccountError::DomainExists => {
                HttpResponse::UnprocessableEntity().body("domain_already_exists")
            }
            UpdateMyAccountError::Default(error) => HttpResponse::BadRequest().body(error),
        }
    }
}