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

use crate::{
    auth::User,
    cornucopia::queries::size::get_size_id,
    entity_ref::{ExternalId, ExternalIdEntity, Id, Ref, RefTarget, Slug, SlugEntity},
    helpers::slugify,
    organizations::Organization,
    Color, ColorSummary, EntityRefPathParam, I18nString,
};

/// Size
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
pub struct Size {
    pub id: Id<Size>,
    pub color: ColorSummary,
    pub number: String,
    pub position: u8,
    pub name: I18nString,
    pub service_item: Option<bool>,
    pub ean_code: Option<String>,
    pub status: Option<String>,
    #[schemars(schema_with = "crate::jsonschema::rfc3339_date")]
    pub delivery_period: Option<time::Date>,
    pub slug: Slug<Size>,
    pub external_id: Option<ExternalId<Size>>,
    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,
}

/// Nested size (well, used by NestedColor, so `color` field isn't needed)
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
pub struct NestedSize {
    pub id: Id<Size>,
    pub number: String,
    pub position: u8,
    pub name: I18nString,
    pub slug: Slug<Size>,
    pub external_id: Option<ExternalId<Size>>,
    pub service_item: Option<bool>,
    pub ean_code: Option<String>,
    pub status: Option<String>,
    #[schemars(schema_with = "crate::jsonschema::rfc3339_date", default)]
    pub delivery_period: Option<time::Date>,
}

/// Nested size (well, used by NestedColor, so `color` field isn't needed)
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
pub struct NestedSizeSummary {
    pub id: Id<Size>,
    pub number: String,
    pub name: I18nString,
}

impl NestedSize {
    pub fn into_summary(self) -> NestedSizeSummary {
        NestedSizeSummary {
            id: self.id,
            number: self.number,
            name: self.name,
        }
    }
}

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

#[async_trait]
impl RefTarget for Size {
    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_size_id()
            .bind(
                client,
                &organization_id.into(),
                &id,
                &external_id.as_deref(),
                &slug.as_deref(),
            )
            .opt()
            .await?
            .map(Id::new))
    }
}

/// Size, for creation
#[derive(Debug, Deserialize)]
pub struct CreateSize {
    pub color: Ref<Color>,
    pub number: String,
    pub position: u8,
    pub name: I18nString,
    pub service_item: Option<bool>,
    pub ean_code: Option<String>,
    pub status: Option<String>,
    pub delivery_period: Option<time::Date>,
    pub slug: Option<Slug<Size>>,
    pub external_id: Option<ExternalId<Size>>,
}

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

    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 CreateSize {
    type RefTarget = Size;

    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);
    }
}

/// Size, for update
#[derive(Debug, Deserialize)]
pub struct UpdateSize {
    pub color: Option<Ref<Color>>,
    pub number: Option<String>,
    pub position: Option<u8>,
    pub name: Option<I18nString>,
    pub service_item: Option<bool>,
    pub delivery_period: Option<time::Date>,
    pub ean_code: Option<String>,
    pub status: Option<String>,
    pub slug: Option<Slug<Size>>,
    pub external_id: Option<ExternalId<Size>>,
}

impl From<CreateSize> for UpdateSize {
    fn from(size: CreateSize) -> Self {
        Self {
            color: Some(size.color),
            number: Some(size.number),
            position: Some(size.position),
            name: Some(size.name),
            service_item: size.service_item,
            delivery_period: size.delivery_period,
            ean_code: size.ean_code,
            status: size.status,
            slug: size.slug,
            external_id: size.external_id,
        }
    }
}

impl SlugEntity for UpdateSize {
    type RefTarget = Size;

    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 UpdateSize {
    type RefTarget = Size;

    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);
    }
}