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
use async_trait::async_trait;
use cornucopia_async::{GenericClient, Params};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use url::Url;

use crate::{
    cornucopia::queries::group::get_group_id,
    cornucopia::queries::user::{get_org_user_id, GetOrgUserIdParams},
    entity_ref::{Id, Ref, RefTarget},
    helpers::slugify,
    organizations::Organization,
    Collection, CollectionSummary, EntityRefPathParam, Environment, Error, ExternalId,
    ExternalIdEntity, OrganizationSummary, PriceList, PriceListSummary, Slug, SlugEntity,
};

use super::rbac::Role;

#[derive(Debug, Clone, Deserialize, Serialize, derive_more::Display, JsonSchema)]
#[display(fmt = "{id}/{name}")]
pub struct User {
    pub id: Id<User>,
    pub name: String,
    pub email: String,
    #[serde(with = "time::serde::rfc3339::option")]
    #[schemars(schema_with = "crate::jsonschema::rfc3339_date_time")]
    pub last_sign_in: Option<time::OffsetDateTime>,
    #[serde(with = "time::serde::rfc3339")]
    #[schemars(schema_with = "crate::jsonschema::rfc3339_date_time")]
    pub created_at: time::OffsetDateTime,
    #[serde(with = "time::serde::rfc3339")]
    #[schemars(schema_with = "crate::jsonschema::rfc3339_date_time")]
    pub updated_at: time::OffsetDateTime,
    pub profile_image: Option<Url>,
    pub organizations: Vec<UserOrganization>,
    pub groups: Vec<GroupSummary>,
}

#[derive(Debug, Clone, Deserialize, Serialize, derive_more::Display, JsonSchema)]
#[display(fmt = "{id}/{name}")]
pub struct UserSummary {
    pub id: Id<User>,
    pub name: String,
    pub email: String,
    #[serde(with = "time::serde::rfc3339::option")]
    #[schemars(schema_with = "crate::jsonschema::rfc3339_date_time")]
    pub last_sign_in: Option<time::OffsetDateTime>,
    pub profile_image: Option<Url>,
}

#[async_trait]
impl RefTarget for User {
    async fn lookup_id(
        client: &impl GenericClient,
        organization_id: Id<Organization>,
        entity_ref: &crate::entity_ref::Ref<Self>,
    ) -> crate::Result<Option<Id<Self>>> {
        match entity_ref {
            Ref::Id(user_id) => Ok(get_org_user_id()
                .params(
                    client,
                    &GetOrgUserIdParams {
                        user_id: user_id.into(),
                        organization_id: organization_id.into(),
                    },
                )
                .opt()
                .await?
                .map(Id::new)),
            Ref::ExternalId(_) => Err(Error::ExternalIdReferenceUnsupported(
                Self::short_type_name(),
            )),
            Ref::Slug(_) => Err(Error::SlugReferenceUnsupported(Self::short_type_name())),
        }
    }
}

#[derive(
    Debug, Clone, Deserialize, Serialize, JsonSchema, derive_more::Display, derive_more::Constructor,
)]
#[display(fmt = "{organization} {roles:?}")]
pub struct UserOrganization {
    pub organization: OrganizationSummary,
    pub roles: Vec<Role>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct UserRoles {
    pub user: Id<User>,
    pub roles: Vec<Role>,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct Group {
    pub id: Id<Self>,
    pub slug: Slug<Self>,
    pub external_id: Option<ExternalId<Self>>,
    pub name: String,
    pub description: String,
    pub created_by: Option<Id<User>>,
    #[serde(with = "time::serde::rfc3339")]
    #[schemars(schema_with = "crate::jsonschema::rfc3339_date_time")]
    pub created_at: time::OffsetDateTime,
    #[serde(with = "time::serde::rfc3339")]
    #[schemars(schema_with = "crate::jsonschema::rfc3339_date_time")]
    pub updated_at: time::OffsetDateTime,
    pub users: Vec<UserSummary>,
    pub collections: Vec<CollectionSummary>,
    pub price_lists: Vec<PriceListSummary>,
}

impl EntityRefPathParam for Group {
    fn parameter_name() -> &'static str {
        "group_ref"
    }
}

#[async_trait]
impl RefTarget for Group {
    async fn lookup_id(
        client: &impl GenericClient,
        organization_id: Id<Organization>,
        entity_ref: &crate::entity_ref::Ref<Self>,
    ) -> crate::Result<Option<Id<Self>>> {
        let (id, external_id, slug) = entity_ref.to_owned().take_all_inner();
        Ok(get_group_id()
            .bind(
                client,
                &organization_id.into(),
                &id,
                &external_id.as_deref(),
                &slug.as_deref(),
            )
            .opt()
            .await?
            .map(Id::new))
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct GroupSummary {
    pub id: Id<Group>,
    pub slug: Slug<Group>,
    pub external_id: Option<ExternalId<Group>>,
    pub name: String,
    pub description: String,
    pub num_users: u32,
    pub num_collections: u32,
    pub num_price_lists: u32,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct CreateGroup {
    pub slug: Option<Slug<Group>>,
    pub external_id: Option<ExternalId<Group>>,
    pub name: String,
    #[serde(default)]
    pub description: String,
    #[serde(default)]
    pub users: Vec<Ref<User>>,
    #[serde(default)]
    pub collections: Vec<Ref<Collection>>,
    #[serde(default)]
    pub price_lists: Vec<Ref<PriceList>>,
}

impl SlugEntity for CreateGroup {
    type RefTarget = Group;
    fn generate_slug(&self, prefix: &str) -> Option<Slug<Self::RefTarget>> {
        Some(Slug::new(slugify(&[prefix, &self.name])))
    }

    fn slug(&self) -> Option<Slug<Self::RefTarget>> {
        self.slug.clone()
    }

    fn set_slug(&mut self, value: Slug<Self::RefTarget>) {
        self.slug = Some(value);
    }
}

impl ExternalIdEntity for CreateGroup {
    type RefTarget = Group;

    fn external_id(&self) -> Option<ExternalId<Self::RefTarget>> {
        self.external_id.clone()
    }

    fn set_external_id(&mut self, value: ExternalId<Self::RefTarget>) {
        self.external_id = Some(value);
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct UpdateGroup {
    pub slug: Option<Slug<Group>>,
    pub external_id: Option<ExternalId<Group>>,
    pub name: Option<String>,
    pub description: Option<String>,
    pub users: Option<Vec<Ref<User>>>,
    pub collections: Option<Vec<Ref<Collection>>>,
    pub price_lists: Option<Vec<Ref<PriceList>>>,
}

impl SlugEntity for UpdateGroup {
    type RefTarget = Group;

    fn slug(&self) -> Option<Slug<Self::RefTarget>> {
        self.slug.clone()
    }

    fn set_slug(&mut self, value: Slug<Self::RefTarget>) {
        self.slug = Some(value);
    }
}

impl ExternalIdEntity for UpdateGroup {
    type RefTarget = Group;

    fn external_id(&self) -> Option<ExternalId<Self::RefTarget>> {
        self.external_id.clone()
    }

    fn set_external_id(&mut self, value: ExternalId<Self::RefTarget>) {
        self.external_id = Some(value);
    }
}

impl From<CreateGroup> for UpdateGroup {
    fn from(data: CreateGroup) -> Self {
        Self {
            name: Some(data.name),
            description: Some(data.description),
            slug: data.slug,
            external_id: data.external_id,
            users: Some(data.users),
            collections: Some(data.collections),
            price_lists: Some(data.price_lists),
        }
    }
}

#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct CreateUser {
    pub email: String,
    pub password: Option<String>,
    pub name: String,
    pub profile_image: Option<Url>,
    pub roles: Option<Vec<Role>>,
    pub groups: Option<Vec<Ref<Group>>>,
}

#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct UpdateUser {
    pub email: Option<String>,
    pub password: Option<String>,
    pub name: Option<String>,
    pub profile_image: Option<Url>,
    pub roles: Option<Vec<Role>>,
    pub groups: Option<Vec<Ref<Group>>>,
}

impl From<CreateUser> for UpdateUser {
    fn from(user: CreateUser) -> Self {
        Self {
            email: Some(user.email),
            password: user.password,
            name: Some(user.name),
            profile_image: user.profile_image,
            roles: user.roles,
            groups: user.groups,
        }
    }
}

#[derive(Debug, Clone, Deserialize, JsonSchema)]
pub struct UpdateOwnUser {
    pub email: Option<String>,
    pub password: Option<String>,
    pub name: Option<String>,
    pub profile_image: Option<Url>,
}

impl From<UpdateOwnUser> for UpdateUser {
    fn from(user: UpdateOwnUser) -> Self {
        Self {
            email: user.email,
            password: user.password,
            name: user.name,
            profile_image: user.profile_image,
            roles: None,
            groups: None,
        }
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct UserLogin {
    pub email: String,
    pub password: String,
}

impl User {
    pub(crate) fn authenticated(
        self,
        environment: Environment,
        token: String,
    ) -> AuthenticatedUser {
        AuthenticatedUser {
            user: self,
            environment,
            token,
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, derive_more::Constructor)]
pub struct AuthenticatedUser {
    pub user: User,
    pub environment: Environment,
    pub token: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn user_id_stringifies_to_just_integer_value() {
        assert_eq!(Id::<User>::new(123).to_string(), "123");
    }
}