rise-deploy 0.16.4

A simple and powerful CLI for deploying containerized applications
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use anyhow::{Context, Result};
use sqlx::PgPool;
use uuid::Uuid;

use crate::db::models::{Team, TeamMember, TeamRole, User};

/// List all teams
pub async fn list(pool: &PgPool) -> Result<Vec<Team>> {
    let teams = sqlx::query_as!(
        Team,
        r#"
        SELECT id, name, idp_managed, created_at, updated_at
        FROM teams
        ORDER BY created_at DESC
        "#
    )
    .fetch_all(pool)
    .await
    .context("Failed to list teams")?;

    Ok(teams)
}

/// List teams for a specific user
pub async fn list_for_user(pool: &PgPool, user_id: Uuid) -> Result<Vec<Team>> {
    let teams = sqlx::query_as!(
        Team,
        r#"
        SELECT t.id, t.name, t.idp_managed, t.created_at, t.updated_at
        FROM teams t
        INNER JOIN team_members tm ON t.id = tm.team_id
        WHERE tm.user_id = $1
        ORDER BY t.created_at DESC
        "#,
        user_id
    )
    .fetch_all(pool)
    .await
    .context("Failed to list teams for user")?;

    Ok(teams)
}

/// Find team by name (case-insensitive due to unique index)
pub async fn find_by_name<'a, E>(executor: E, name: &str) -> Result<Option<Team>>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    let team = sqlx::query_as!(
        Team,
        r#"
        SELECT id, name, idp_managed, created_at, updated_at
        FROM teams
        WHERE LOWER(name) = LOWER($1)
        "#,
        name
    )
    .fetch_optional(executor)
    .await
    .context("Failed to find team by name")?;

    Ok(team)
}

/// Find team by ID
pub async fn find_by_id(pool: &PgPool, id: Uuid) -> Result<Option<Team>> {
    let team = sqlx::query_as!(
        Team,
        r#"
        SELECT id, name, idp_managed, created_at, updated_at
        FROM teams
        WHERE id = $1
        "#,
        id
    )
    .fetch_optional(pool)
    .await
    .context("Failed to find team by ID")?;

    Ok(team)
}

/// Create a new team
pub async fn create<'a, E>(executor: E, name: &str) -> Result<Team>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    let team = sqlx::query_as!(
        Team,
        r#"
        INSERT INTO teams (name)
        VALUES ($1)
        RETURNING id, name, idp_managed, created_at, updated_at
        "#,
        name
    )
    .fetch_one(executor)
    .await
    .context("Failed to create team")?;

    Ok(team)
}

/// Delete team by ID
pub async fn delete(pool: &PgPool, id: Uuid) -> Result<()> {
    sqlx::query!("DELETE FROM teams WHERE id = $1", id)
        .execute(pool)
        .await
        .context("Failed to delete team")?;

    Ok(())
}

/// Get team members (users with member role only, not owners)
pub async fn get_members(pool: &PgPool, team_id: Uuid) -> Result<Vec<User>> {
    let members = sqlx::query_as!(
        User,
        r#"
        SELECT u.id, u.email, u.created_at, u.updated_at
        FROM users u
        INNER JOIN team_members tm ON u.id = tm.user_id
        WHERE tm.team_id = $1 AND tm.role = 'member'
        ORDER BY u.email
        "#,
        team_id
    )
    .fetch_all(pool)
    .await
    .context("Failed to get team members")?;

    Ok(members)
}

/// Get team owners
pub async fn get_owners(pool: &PgPool, team_id: Uuid) -> Result<Vec<User>> {
    let owners = sqlx::query_as!(
        User,
        r#"
        SELECT u.id, u.email, u.created_at, u.updated_at
        FROM users u
        INNER JOIN team_members tm ON u.id = tm.user_id
        WHERE tm.team_id = $1 AND tm.role = 'owner'
        ORDER BY u.email
        "#,
        team_id
    )
    .fetch_all(pool)
    .await
    .context("Failed to get team owners")?;

    Ok(owners)
}

/// Add member to team
pub async fn add_member<'a, E>(
    executor: E,
    team_id: Uuid,
    user_id: Uuid,
    role: TeamRole,
) -> Result<TeamMember>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    let role_str = role.to_string();

    let member = sqlx::query_as!(
        TeamMember,
        r#"
        INSERT INTO team_members (team_id, user_id, role)
        VALUES ($1, $2, $3)
        RETURNING team_id, user_id, role as "role: TeamRole", created_at
        "#,
        team_id,
        user_id,
        role_str
    )
    .fetch_one(executor)
    .await
    .context("Failed to add team member")?;

    Ok(member)
}

/// Remove member from team (specific role only)
pub async fn remove_member<'a, E>(
    executor: E,
    team_id: Uuid,
    user_id: Uuid,
    role: TeamRole,
) -> Result<()>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    let role_str = role.to_string();

    sqlx::query!(
        "DELETE FROM team_members WHERE team_id = $1 AND user_id = $2 AND role = $3",
        team_id,
        user_id,
        role_str
    )
    .execute(executor)
    .await
    .context("Failed to remove team member")?;

    Ok(())
}

/// Remove user from team (all roles)
pub async fn remove_all_user_roles<'a, E>(executor: E, team_id: Uuid, user_id: Uuid) -> Result<()>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    sqlx::query!(
        "DELETE FROM team_members WHERE team_id = $1 AND user_id = $2",
        team_id,
        user_id
    )
    .execute(executor)
    .await
    .context("Failed to remove user from team")?;

    Ok(())
}

/// Update member role
pub async fn update_member_role(
    pool: &PgPool,
    team_id: Uuid,
    user_id: Uuid,
    role: TeamRole,
) -> Result<TeamMember> {
    let role_str = role.to_string();

    let member = sqlx::query_as!(
        TeamMember,
        r#"
        UPDATE team_members
        SET role = $3
        WHERE team_id = $1 AND user_id = $2
        RETURNING team_id, user_id, role as "role: TeamRole", created_at
        "#,
        team_id,
        user_id,
        role_str
    )
    .fetch_one(pool)
    .await
    .context("Failed to update member role")?;

    Ok(member)
}

/// Check if user is team owner
pub async fn is_owner(pool: &PgPool, team_id: Uuid, user_id: Uuid) -> Result<bool> {
    let result = sqlx::query!(
        r#"
        SELECT EXISTS(
            SELECT 1 FROM team_members
            WHERE team_id = $1 AND user_id = $2 AND role = 'owner'
        ) as "exists!"
        "#,
        team_id,
        user_id
    )
    .fetch_one(pool)
    .await
    .context("Failed to check team ownership")?;

    Ok(result.exists)
}

/// Check if user is team member (owner or member)
pub async fn is_member<'a, E>(executor: E, team_id: Uuid, user_id: Uuid) -> Result<bool>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    let result = sqlx::query!(
        r#"
        SELECT EXISTS(
            SELECT 1 FROM team_members
            WHERE team_id = $1 AND user_id = $2
        ) as "exists!"
        "#,
        team_id,
        user_id
    )
    .fetch_one(executor)
    .await
    .context("Failed to check team membership")?;

    Ok(result.exists)
}

/// Batch fetch team names by IDs
pub async fn get_names_batch(
    pool: &PgPool,
    team_ids: &[Uuid],
) -> Result<std::collections::HashMap<Uuid, String>> {
    let records = sqlx::query!(
        r#"
        SELECT id, name
        FROM teams
        WHERE id = ANY($1)
        "#,
        team_ids
    )
    .fetch_all(pool)
    .await
    .context("Failed to batch fetch team names")?;

    Ok(records.into_iter().map(|r| (r.id, r.name)).collect())
}

/// Batch fetch full team details by IDs
pub async fn get_teams_batch(
    pool: &PgPool,
    team_ids: &[Uuid],
) -> Result<std::collections::HashMap<Uuid, Team>> {
    let teams = sqlx::query_as!(
        Team,
        r#"
        SELECT id, name, idp_managed, created_at, updated_at
        FROM teams
        WHERE id = ANY($1)
        "#,
        team_ids
    )
    .fetch_all(pool)
    .await
    .context("Failed to batch fetch teams")?;

    Ok(teams.into_iter().map(|t| (t.id, t)).collect())
}

// ============================================================================
// IdP Group Sync Functions
// ============================================================================

/// Update team name (for case correction from IdP)
pub async fn update_name<'a, E>(executor: E, team_id: Uuid, name: &str) -> Result<Team>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    let team = sqlx::query_as!(
        Team,
        r#"
        UPDATE teams
        SET name = $2, updated_at = NOW()
        WHERE id = $1
        RETURNING id, name, idp_managed, created_at, updated_at
        "#,
        team_id,
        name
    )
    .fetch_one(executor)
    .await
    .context("Failed to update team name")?;

    Ok(team)
}

/// Mark team as IdP-managed
pub async fn set_idp_managed<'a, E>(executor: E, team_id: Uuid, idp_managed: bool) -> Result<()>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    sqlx::query!(
        r#"
        UPDATE teams
        SET idp_managed = $2, updated_at = NOW()
        WHERE id = $1
        "#,
        team_id,
        idp_managed
    )
    .execute(executor)
    .await
    .context("Failed to set idp_managed flag")?;

    Ok(())
}

/// Remove all owners from a team (for IdP takeover)
pub async fn remove_all_owners<'a, E>(executor: E, team_id: Uuid) -> Result<()>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    sqlx::query!(
        r#"
        DELETE FROM team_members
        WHERE team_id = $1 AND role = 'owner'
        "#,
        team_id
    )
    .execute(executor)
    .await
    .context("Failed to remove all owners")?;

    Ok(())
}

/// Get all IdP-managed teams
pub async fn list_idp_managed<'a, E>(executor: E) -> Result<Vec<Team>>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    let teams = sqlx::query_as!(
        Team,
        r#"
        SELECT id, name, idp_managed, created_at, updated_at
        FROM teams
        WHERE idp_managed = TRUE
        ORDER BY name
        "#
    )
    .fetch_all(executor)
    .await
    .context("Failed to list IdP-managed teams")?;

    Ok(teams)
}

/// Get team names for all teams a user is a member of (for JWT groups claim)
pub async fn get_team_names_for_user(pool: &PgPool, user_id: Uuid) -> Result<Vec<String>> {
    let records = sqlx::query!(
        r#"
        SELECT t.name
        FROM teams t
        INNER JOIN team_members tm ON t.id = tm.team_id
        WHERE tm.user_id = $1
        ORDER BY t.name
        "#,
        user_id
    )
    .fetch_all(pool)
    .await
    .context("Failed to get team names for user")?;

    Ok(records.into_iter().map(|r| r.name).collect())
}

/// Remove all members from a team (all roles)
pub async fn remove_all_team_members<'a, E>(executor: E, team_id: Uuid) -> Result<u64>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    let result = sqlx::query!("DELETE FROM team_members WHERE team_id = $1", team_id)
        .execute(executor)
        .await
        .context("Failed to remove all team members")?;

    Ok(result.rows_affected())
}

/// Get all member user IDs for a team (any role)
pub async fn get_all_member_user_ids<'a, E>(executor: E, team_id: Uuid) -> Result<Vec<Uuid>>
where
    E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
    let records = sqlx::query!(
        r#"
        SELECT DISTINCT user_id
        FROM team_members
        WHERE team_id = $1
        "#,
        team_id
    )
    .fetch_all(executor)
    .await
    .context("Failed to get team member user IDs")?;

    Ok(records.into_iter().map(|r| r.user_id).collect())
}