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
use actix_http::StatusCode;
use uuid::Uuid;

use crate::{
    config::data::{SharedHttpClient, SharedKeycloakApi, SharedPool},
    error::ServiceError,
    model::{
        dto::{DeleteMapCollaboratorDto, MapCollaboratorDto, NewMapCollaboratorDto},
        entity::{Map, MapCollaborator},
    },
    service::users,
};

/// The maximum number of collaborators a map can have.
const MAX_COLLABORATORS: usize = 30;

/// Get all collaborators for a map.
///
/// # Errors
/// * If the connection to the database could not be established.
/// * If the connection to the Keycloak API could not be established.
pub async fn get_all(
    map_id: i32,
    pool: &SharedPool,
    keycloak_api: &SharedKeycloakApi,
    http_client: &SharedHttpClient,
) -> Result<Vec<MapCollaboratorDto>, ServiceError> {
    let mut conn = pool.get().await?;
    // Check if map exists
    let _ = Map::find_by_id(map_id, &mut conn).await?;

    let collaborators = MapCollaborator::find_by_map_id(map_id, &mut conn).await?;

    let collaborator_ids = collaborators.iter().map(|c| c.user_id).collect::<Vec<_>>();
    let users = users::find_by_ids(collaborator_ids, keycloak_api, http_client).await?;

    let dtos = collaborators
        .into_iter()
        .zip(users.into_iter())
        .map(|(c, u)| MapCollaboratorDto::from((c, u)))
        .collect::<Vec<MapCollaboratorDto>>();

    Ok(dtos)
}

/// Create a new collaborator for a map.
///
/// # Errors
/// * If the user tries to add themselves as a collaborator.
/// * If the user is not the creator of the map.
/// * If the map already has 30 collaborators.
/// * If the connection to the database could not be established.
/// * If the connection to the Keycloak API could not be established.
pub async fn create(
    map_and_collaborator: (i32, NewMapCollaboratorDto),
    user_id: Uuid,
    pool: &SharedPool,
    keycloak_api: &SharedKeycloakApi,
    http_client: &SharedHttpClient,
) -> Result<MapCollaboratorDto, ServiceError> {
    let (map_id, ref new_map_collaborator) = map_and_collaborator;

    if new_map_collaborator.user_id == user_id {
        return Err(ServiceError::new(
            StatusCode::BAD_REQUEST,
            "You cannot add yourself as a collaborator.",
        ));
    }

    let mut conn = pool.get().await?;

    let map = Map::find_by_id(map_id, &mut conn).await?;

    if map.created_by != user_id {
        return Err(ServiceError::new(
            StatusCode::FORBIDDEN,
            "You are not the creator of this map.",
        ));
    }

    let current_collaborators = MapCollaborator::find_by_map_id(map_id, &mut conn).await?;

    if current_collaborators.len() >= MAX_COLLABORATORS {
        return Err(ServiceError::new(
            StatusCode::BAD_REQUEST,
            &format!("A map can have at most {MAX_COLLABORATORS} collaborators."),
        ));
    }

    let collaborator_user =
        users::find_by_id(new_map_collaborator.user_id, keycloak_api, http_client).await?;

    let collaborator = MapCollaborator::create(map_and_collaborator, &mut conn).await?;

    Ok(MapCollaboratorDto::from((collaborator, collaborator_user)))
}

/// Remove a collaborator from a map.
///
/// # Errors
/// * If the user is not the creator of the map.
/// * If the connection to the database could not be established.
/// * If the connection to the Keycloak API could not be established.
pub async fn delete(
    map_and_dto: (i32, DeleteMapCollaboratorDto),
    user_id: Uuid,
    pool: &SharedPool,
) -> Result<(), ServiceError> {
    let (map_id, _) = map_and_dto;

    let mut conn = pool.get().await?;

    let map = Map::find_by_id(map_id, &mut conn).await?;

    if map.created_by != user_id {
        return Err(ServiceError::new(
            StatusCode::FORBIDDEN,
            "You are not the creator of this map.",
        ));
    }

    MapCollaborator::delete(map_and_dto, &mut conn).await?;

    Ok(())
}