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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::str::FromStr;

use async_graphql::InputObject;
use async_trait::async_trait;

use derive_builder::Builder;
use poem_openapi::Object;
use serde::Serialize;
use sqlx::Row;
use uuid::Uuid;

use crate::{
    backend::engine::SDKEngine,
    common::commons::{SortOrder, UpdateListInput},
    errors::sdk::SDKError,
};

use super::team::{Team, TeamVisibility};

#[async_trait]
pub trait TeamCrudOperations {
    async fn create_team(&self, input: CreateTeamInput) -> Result<Team, SDKError>;
    async fn get_team(&self, id: Uuid) -> Result<Team, SDKError>;
    async fn get_teams(&self, input: GetTeamsInput) -> Result<Vec<Team>, SDKError>;
    async fn update_team(&self, id: Uuid, input: UpdateTeamInput) -> Result<Team, SDKError>;
    async fn delete_team(&self, id: Uuid) -> Result<Team, SDKError>;
}

#[derive(Clone, Default, Object, Builder, InputObject, Serialize)]
#[builder(pattern = "owned")]
pub struct CreateTeamInput {
    pub name: String,

    #[graphql(skip)]
    pub owner_id: Uuid,
    pub visibility: TeamVisibility,

    #[builder(setter(strip_option), default)]
    pub prefix: Option<String>,

    #[builder(setter(strip_option), default)]
    pub members: Option<Vec<Uuid>>,
    #[builder(setter(strip_option), default)]
    pub projects: Option<Vec<Uuid>>,
}

#[derive(Clone, Default, Object, Builder, InputObject, Serialize)]
#[builder(pattern = "owned")]
pub struct UpdateTeamInput {
    #[builder(setter(strip_option), default)]
    pub name: Option<String>,
    #[builder(setter(strip_option), default)]
    pub owner_id: Option<Uuid>,
    #[builder(setter(strip_option), default)]
    pub visibility: Option<TeamVisibility>,
    #[builder(setter(strip_option), default)]
    pub prefix: Option<String>,

    #[builder(setter(strip_option), default)]
    pub members: Option<UpdateListInput>,
    #[builder(setter(strip_option), default)]
    pub teams: Option<UpdateListInput>,
}

#[derive(Default, Object, Builder, InputObject)]
#[builder(pattern = "owned")]
pub struct GetTeamsInput {
    #[builder(setter(strip_option), default)]
    pub filter: Option<GetTeamsWhere>,

    #[builder(setter(strip_option), default)]
    pub sort_by: Option<String>,
    #[builder(setter(strip_option), default)]
    pub sort_order: Option<SortOrder>,

    #[builder(setter(into, strip_option), default = "Some(100)")]
    pub limit: Option<i32>,
    #[builder(setter(into, strip_option), default = "Some(0)")]
    pub offset: Option<i32>,
}

#[derive(Default, Object, Builder, InputObject)]
#[builder(pattern = "owned")]
pub struct GetTeamsWhere {
    #[builder(setter(strip_option), default)]
    pub name: Option<String>,
    #[builder(setter(strip_option), default)]
    pub owner_id: Option<Uuid>,
    #[builder(setter(strip_option), default)]
    pub visibility: Option<TeamVisibility>,
    #[builder(setter(strip_option), default)]
    pub prefix: Option<String>,

    #[oai(skip)]
    #[builder(setter(strip_option), default)]
    pub _and: Option<Vec<GetTeamsWhere>>,
    #[oai(skip)]
    #[builder(setter(strip_option), default)]
    pub _or: Option<Vec<GetTeamsWhere>>,
}

impl GetTeamsWhere {
    pub fn compile_sql(&self) -> String {
        let mut query = String::new();

        if let Some(name) = &self.name {
            query.push_str(format!("name = '{}' ", name).as_str());
        }

        if let Some(owner_id) = &self.owner_id {
            query.push_str(format!("owner_id = '{}' ", owner_id).as_str());
        }

        if let Some(visibility) = &self.visibility {
            query.push_str(format!("visibility = '{}' ", visibility).as_str());
        }

        if let Some(prefix) = &self.prefix {
            query.push_str(format!("prefix = '{}' ", prefix).as_str());
        }

        if let Some(_and) = &self._and {
            query.push_str("AND (");

            for (index, where_clause) in _and.iter().enumerate() {
                query.push_str(where_clause.compile_sql().as_str());

                if index != _and.len() - 1 {
                    query.push_str("AND ");
                }
            }

            query.push_str(") ");
        }

        if let Some(_or) = &self._or {
            query.push_str("OR (");

            for (index, where_clause) in _or.iter().enumerate() {
                query.push_str(where_clause.compile_sql().as_str());

                if index != _or.len() - 1 {
                    query.push_str("OR ");
                }
            }

            query.push_str(") ");
        }

        query
    }
}

#[async_trait]
impl TeamCrudOperations for SDKEngine {
    async fn create_team(&self, input: CreateTeamInput) -> Result<Team, SDKError> {
        let mut tx = self.db_pool.begin().await?;

        let team_final_info = sqlx::query!(
            r#"
            INSERT INTO teams (name, owner_id, visibility, prefix)
            VALUES ($1, $2, $3, $4)
            RETURNING *
            "#,
            input.name,
            input.owner_id,
            input.visibility.to_string(),
            input.prefix
        )
        .fetch_one(&mut *tx)
        .await?;

        if let Some(members) = input.members {
            for member_id in members {
                sqlx::query!(
                    r#"
                    INSERT INTO members_by_teams (team_id, member_id)
                    VALUES ($1, $2)
                    "#,
                    team_final_info.id,
                    member_id
                )
                .execute(&mut *tx)
                .await?;
            }
        }

        if let Some(projects) = input.projects {
            for project_id in projects {
                sqlx::query!(
                    r#"
                    INSERT INTO teams_by_projects (team_id, project_id)
                    VALUES ($1, $2)
                    "#,
                    team_final_info.id,
                    project_id
                )
                .execute(&mut *tx)
                .await?;
            }
        }

        tx.commit().await?;

        let team = Team {
            id: team_final_info.id,
            created_at: team_final_info.created_at,
            updated_at: team_final_info.updated_at,
            name: team_final_info.name,
            owner_id: team_final_info.owner_id,
            visibility: team_final_info
                .visibility
                .and_then(|a| TeamVisibility::from_str(&a).ok())
                .unwrap_or_default(),
            prefix: team_final_info.prefix,
        };

        Ok(team)
    }

    async fn get_team(&self, id: Uuid) -> Result<Team, SDKError> {
        let team_info = sqlx::query!(
            r#"
            SELECT id, created_at, updated_at, name, owner_id, visibility, prefix
            FROM teams
            WHERE id = $1
            "#,
            id
        )
        .fetch_one(self.db_pool.as_ref())
        .await?;

        let team = Team {
            id: team_info.id,
            created_at: team_info.created_at,
            updated_at: team_info.updated_at,
            name: team_info.name,
            owner_id: team_info.owner_id,
            visibility: team_info
                .visibility
                .and_then(|a| TeamVisibility::from_str(&a).ok())
                .unwrap_or_default(),
            prefix: team_info.prefix,
        };

        Ok(team)
    }

    async fn get_teams(&self, input: GetTeamsInput) -> Result<Vec<Team>, SDKError> {
        let mut query = "SELECT * FROM teams ".to_string();

        if let Some(filter) = input.filter {
            query.push_str(format!("WHERE {} ", filter.compile_sql()).as_str());
        }

        if let Some(sort_by) = input.sort_by {
            query.push_str(format!("ORDER BY {} ", sort_by).as_str());
        }

        if let Some(sort_order) = input.sort_order {
            query.push_str(format!("{} ", sort_order).as_str());
        }

        if let Some(limit) = input.limit {
            query.push_str(format!("LIMIT {} ", limit).as_str());
        }

        if let Some(offset) = input.offset {
            query.push_str(format!("OFFSET {} ", offset).as_str());
        }

        let teams_info = sqlx::query(query.as_str()).fetch_all(self.db_pool.as_ref()).await?;

        let teams = teams_info
            .iter()
            .map(|x| Team {
                id: x.get("id"),
                created_at: x.get("created_at"),
                updated_at: x.get("updated_at"),
                name: x.get("name"),
                owner_id: x.get("owner_id"),
                visibility: x
                    .get::<'_, Option<String>, _>("visibility")
                    .and_then(|a| TeamVisibility::from_str(&a).ok())
                    .unwrap_or_default(),
                prefix: x.get("prefix"),
            })
            .collect::<Vec<Team>>();

        Ok(teams)
    }

    async fn update_team(&self, id: Uuid, input: UpdateTeamInput) -> Result<Team, SDKError> {
        let mut tx = self.db_pool.begin().await?;

        let team_final_info = sqlx::query!(
            r#"
            UPDATE teams
            SET
                name = COALESCE($1, name),
                owner_id = COALESCE($2, owner_id),
                visibility = COALESCE($3, visibility),
                prefix = COALESCE($4, prefix),
                updated_at = now()
            WHERE id = $5
            RETURNING *
            "#,
            input.name,
            input.owner_id,
            input.visibility.map(|visibility| visibility.to_string()),
            input.prefix,
            id,
        )
        .fetch_one(&mut *tx)
        .await?;

        if let Some(members) = input.members {
            for member_id in members.add {
                sqlx::query!(
                    r#"
                    INSERT INTO members_by_teams (team_id, member_id)
                    VALUES ($1, $2)
                    "#,
                    id,
                    member_id
                )
                .execute(&mut *tx)
                .await?;
            }

            for member_id in members.remove {
                sqlx::query!(
                    r#"
                    DELETE FROM members_by_teams
                    WHERE team_id = $1 AND member_id = $2
                    "#,
                    id,
                    member_id
                )
                .execute(&mut *tx)
                .await?;
            }
        }

        if let Some(projects) = input.teams {
            for project_id in projects.add {
                sqlx::query!(
                    r#"
                    INSERT INTO teams_by_projects (team_id, project_id)
                    VALUES ($1, $2)
                    "#,
                    id,
                    project_id
                )
                .execute(&mut *tx)
                .await?;
            }

            for project_id in projects.remove {
                sqlx::query!(
                    r#"
                    DELETE FROM teams_by_projects
                    WHERE team_id = $1 AND project_id = $2
                    "#,
                    id,
                    project_id
                )
                .execute(&mut *tx)
                .await?;
            }
        }

        let team = Team {
            id: team_final_info.id,
            created_at: team_final_info.created_at,
            updated_at: team_final_info.updated_at,
            name: team_final_info.name,
            owner_id: team_final_info.owner_id,
            visibility: team_final_info
                .visibility
                .and_then(|a| TeamVisibility::from_str(&a).ok())
                .unwrap_or_default(),
            prefix: team_final_info.prefix,
        };

        tx.commit().await?;

        Ok(team)
    }

    async fn delete_team(&self, id: Uuid) -> Result<Team, SDKError> {
        let team_info = sqlx::query!(
            r#"
            DELETE FROM teams
            WHERE id = $1
            RETURNING *
            "#,
            id
        )
        .fetch_one(self.db_pool.as_ref())
        .await?;

        let team = Team {
            id: team_info.id,
            created_at: team_info.created_at,
            updated_at: team_info.updated_at,
            name: team_info.name,
            owner_id: team_info.owner_id,
            visibility: team_info
                .visibility
                .and_then(|a| TeamVisibility::from_str(&a).ok())
                .unwrap_or_default(),
            prefix: team_info.prefix,
        };

        Ok(team)
    }
}