Skip to content

Commit

Permalink
feat: implemented fetch me project user (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobressan authored Oct 22, 2024
1 parent 1148af3 commit ce7c9af
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions src/domain/project/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,37 @@ pub async fn fetch_user(
Ok(project_users_aggregated)
}

pub async fn fetch_me_user(
cache: Arc<dyn ProjectDrivenCache>,
auth0: Arc<dyn Auth0Driven>,
cmd: FetchMeUserCmd,
) -> Result<ProjectUserAggregated> {
let user_id = assert_credential(&cmd.credential)?;
assert_permission(cache.clone(), &cmd.credential, &cmd.project_id, None).await?;

let Some(project_user) = cache
.find_user_permission(&user_id, &cmd.project_id)
.await?
else {
return Err(Error::CommandMalformed(
"invalid project and user id".into(),
));
};

let profile = auth0.find_info(&project_user.user_id).await?;

let project_user_aggregated = ProjectUserAggregated {
user_id: project_user.user_id.clone(),
project_id: project_user.project_id,
name: profile.name,
email: profile.email,
role: project_user.role,
created_at: project_user.created_at,
};

Ok(project_user_aggregated)
}

pub async fn delete_user(
cache: Arc<dyn ProjectDrivenCache>,
event: Arc<dyn EventDrivenBridge>,
Expand Down Expand Up @@ -731,6 +762,20 @@ impl FetchUserCmd {
}
}

#[derive(Debug, Clone)]
pub struct FetchMeUserCmd {
pub credential: Credential,
pub project_id: String,
}
impl FetchMeUserCmd {
pub fn new(credential: Credential, project_id: String) -> Result<Self> {
Ok(Self {
credential,
project_id,
})
}
}

#[derive(Debug, Clone)]
pub struct DeleteUserCmd {
pub credential: Credential,
Expand Down Expand Up @@ -992,6 +1037,14 @@ mod tests {
}
}
}
impl Default for FetchMeUserCmd {
fn default() -> Self {
Self {
credential: Credential::Auth0("user id".into()),
project_id: Uuid::new_v4().to_string(),
}
}
}
impl Default for DeleteUserCmd {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -1837,4 +1890,22 @@ mod tests {
let result = delete(Arc::new(cache), Arc::new(event), cmd).await;
assert!(result.is_err());
}

#[tokio::test]
async fn it_should_fetch_me_project_user() {
let mut cache = MockProjectDrivenCache::new();
cache
.expect_find_user_permission()
.returning(|_, _| Ok(Some(ProjectUser::default())));

let mut auth0 = MockAuth0Driven::new();
auth0
.expect_find_info()
.return_once(|_| Ok(Auth0Profile::default()));

let cmd = FetchMeUserCmd::default();

let result = fetch_me_user(Arc::new(cache), Arc::new(auth0), cmd).await;
assert!(result.is_ok());
}
}
23 changes: 23 additions & 0 deletions src/drivers/grpc/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@ impl proto::project_service_server::ProjectService for ProjectServiceImpl {

Ok(tonic::Response::new(message))
}
async fn fetch_me_project_user(
&self,
request: tonic::Request<proto::FetchMeProjectUserRequest>,
) -> Result<tonic::Response<proto::FetchMeProjectUserResponse>, tonic::Status> {
let credential = match request.extensions().get::<Credential>() {
Some(credential) => credential.clone(),
None => return Err(Status::unauthenticated("invalid credential")),
};

let req = request.into_inner();

let cmd = project::command::FetchMeUserCmd::new(credential, req.project_id)?;

let user =
project::command::fetch_me_user(self.cache.clone(), self.auth0.clone(), cmd.clone())
.await?;

let message = proto::FetchMeProjectUserResponse {
records: vec![user.into()],
};

Ok(tonic::Response::new(message))
}
async fn fetch_project_user_invites(
&self,
request: tonic::Request<proto::FetchProjectUserInvitesRequest>,
Expand Down

0 comments on commit ce7c9af

Please sign in to comment.