Skip to content

Commit

Permalink
Merge pull request #36 from raktar-project/user-creation-cleanup
Browse files Browse the repository at this point in the history
Update user details when they are out of sync with Cognito
  • Loading branch information
davidsteiner authored Jul 9, 2023
2 parents bf47ea5 + f119db5 commit 9f5c982
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
10 changes: 10 additions & 0 deletions application/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ impl CognitoUserData {
}
}
}

impl From<User> for CognitoUserData {
fn from(user: User) -> Self {
Self {
login: user.login,
given_name: user.given_name,
family_name: user.family_name,
}
}
}
25 changes: 21 additions & 4 deletions application/repository/dynamodb/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,23 @@ pub async fn get_users(db_client: &Client, table_name: &str) -> AppResult<Vec<Us
}
}

pub async fn put_new_user(db_client: &Client, table_name: &str, user: User) -> AppResult<User> {
pub async fn put_user(
db_client: &Client,
table_name: &str,
user: User,
is_new: bool,
) -> AppResult<User> {
let condition = if is_new {
Some("attribute_not_exists(pk) AND attribute_not_exists(sk)".to_string())
} else {
None
};
let put = Put::builder()
.table_name(table_name)
.set_item(Some(to_item(user.clone())?))
.item("pk", AttributeValue::S("USERS".to_string()))
.item("sk", AttributeValue::S(format!("LOGIN#{}", user.login)))
.condition_expression("attribute_not_exists(pk) AND attribute_not_exists(sk)")
.set_condition_expression(condition)
.build();
let put_login_mapping_item = TransactWriteItem::builder().put(put).build();

Expand Down Expand Up @@ -110,7 +120,7 @@ pub async fn create_next_user(

let user = user_data.into_user(next_id);

put_new_user(db_client, table_name, user).await
put_user(db_client, table_name, user, true).await
}

pub async fn update_or_create_user(
Expand All @@ -136,7 +146,14 @@ pub async fn update_or_create_user(
}
Some(item) => {
let user: User = from_item(item)?;
// TODO: if the details of the user in the database are stale, we should update it

// if the existing user data is out of sync, update it
let existing_user_data: CognitoUserData = user.clone().into();
if existing_user_data != user_data {
let new_user = user_data.into_user(user.id);
put_user(db_client, table_name, new_user, false).await?;
}

Ok(user)
}
}
Expand Down
26 changes: 22 additions & 4 deletions tests/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing_test::traced_test;

use crate::common::setup::create_db_client;
use common::setup::build_repository;
use raktar::repository::dynamodb::user::put_new_user;
use raktar::repository::dynamodb::user::put_user;

#[tokio::test]
#[traced_test]
Expand Down Expand Up @@ -51,7 +51,7 @@ async fn test_user_ids_are_incremented() {
}

#[tokio::test]
async fn test_cant_put_the_same_user_twice() {
async fn test_cant_put_the_same_new_user_twice() {
let (db_client, table_name) = create_db_client().await;

let user = User {
Expand All @@ -61,9 +61,27 @@ async fn test_cant_put_the_same_user_twice() {
family_name: "Wayne".to_string(),
};

let result = put_new_user(&db_client, &table_name, user.clone()).await;
let result = put_user(&db_client, &table_name, user.clone(), true).await;
assert!(result.is_ok());

let result = put_new_user(&db_client, &table_name, user.clone()).await;
let result = put_user(&db_client, &table_name, user.clone(), true).await;
assert!(result.is_err());
}

#[tokio::test]
async fn test_updating_user_works() {
let (db_client, table_name) = create_db_client().await;

let user = User {
id: 33,
login: "user_x@raktar.io".to_string(),
given_name: "Bruce".to_string(),
family_name: "Wayne".to_string(),
};

let result = put_user(&db_client, &table_name, user.clone(), false).await;
assert!(result.is_ok());

let result = put_user(&db_client, &table_name, user.clone(), false).await;
assert!(result.is_ok());
}

0 comments on commit 9f5c982

Please sign in to comment.