Skip to content

Commit

Permalink
[NO-ISSUE] feat: add multiple freebusy to Rust sdk + test (#31)
Browse files Browse the repository at this point in the history
* refactor: adapt/update js client

* chore: update justfile

* ci: adapt JS client lib's CI

* test: start adding integration tests for our use cases

* ci: fix actions/setup-node usage for pnpm

* ci: update actions + install pnpm

* ci: remove cache on actions/setup-node as it's pretty fast

* test: add more tests to integration tests

* refactor: rename file + adapt requirements' tests

* feat(users): allow to provide external userId (uuid)

* chore: fix formatting

* test(requirements): provide userUuid

* feat(js lib): add format + lint biome + fixes

* refactor: convert unix timestamps to datetimes

* fix: format & lint

* fix: missing imports

* fix: format

* fix: make client lib return Dates

* chore: add js docs to client lib

* chore: add more JSDocs

* feat: allow to query multiple calendars + allow to query on name + allow query metadata

* chore: remove debug logs

* feat: add query freebusy on multiple users + fix metadata

* refactor: extract convertEventDates & convertInstanceDates

* chore: remove comment

* chore: add more comments

* refactor: rework result of "freebusy of multiple users"

* fix: client lib multiple freebusy

* chore: remove duplicate requirements

* feat: allow to query on multiple calendars of same user

* fix: properly parse IDs + add test on TS client

* feat: add multiple freebusy to Rust sdk + test

* chore: remove println

* fix: adapt called URL in freebusy test

* chore: remove unused struct

* chore: add comment
  • Loading branch information
GuillaumeDecMeetsMore authored Aug 30, 2024
1 parent 1d61bd0 commit f95161e
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 5 deletions.
8 changes: 4 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export RUST_BACKTRACE := "1"

# Install minimal tools
install_tools:
cargo install sqlx-cli --no-default-features --features postgres
cargo install cargo-nextest
cargo install sqlx-cli
cargo install cargo-pretty-test

# Install all tools
install_all_tools: install_tools
Expand Down Expand Up @@ -33,8 +33,8 @@ prepare_sqlx:
cd scheduler && cargo sqlx prepare --workspace

# Run the tests on a temporary DB container
test:
bash ./scripts/run_tests.sh
test test_name="":
bash ./scripts/run_tests.sh {{test_name}}

# Lint
lint: _setup_db
Expand Down
2 changes: 2 additions & 0 deletions scheduler/clients/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ use user::UserClient;
pub use user::{
CreateUserInput,
GetUserFreeBusyInput,
MultipleFreeBusyAPIResponse,
MultipleFreeBusyRequestBody,
OAuthInput,
RemoveUserIntegrationInput,
UpdateUserInput,
Expand Down
17 changes: 17 additions & 0 deletions scheduler/clients/rust/src/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::sync::Arc;

use chrono::{DateTime, Utc};
// Re-export API structs
pub use multiple_freebusy::{
APIResponse as MultipleFreeBusyAPIResponse,
RequestBody as MultipleFreeBusyRequestBody,
};
use nettu_scheduler_api_structs::*;
use nettu_scheduler_domain::{IntegrationProvider, Metadata};
use reqwest::StatusCode;
Expand Down Expand Up @@ -101,6 +106,18 @@ impl UserClient {
.await
}

/// Get free/busy information for multiple users between a time range
///
/// Response will contain a hashmap of user's UUID with their free/busy information
pub async fn multiple_users_free_busy(
&self,
body: MultipleFreeBusyRequestBody,
) -> APIResponse<MultipleFreeBusyAPIResponse> {
self.base
.post(body, "user/freebusy".to_string(), StatusCode::OK)
.await
}

pub async fn oauth(&self, input: OAuthInput) -> APIResponse<oauth_integration::APIResponse> {
let user_id = input.user_id.clone();
let body = oauth_integration::RequestBody {
Expand Down
126 changes: 126 additions & 0 deletions scheduler/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use nettu_scheduler_sdk::{
KVMetadata,
Metadata,
MetadataFindInput,
MultipleFreeBusyRequestBody,
NettuSDK,
RemoveServiceUserInput,
UpdateCalendarInput,
Expand Down Expand Up @@ -650,3 +651,128 @@ async fn test_crud_service() {
// Get now returns 404
assert!(admin_client.service.get(service.id.clone()).await.is_err());
}

#[actix_web::main]
#[test]
async fn test_freebusy_multiple() {
let (app, sdk, address) = spawn_app().await;
let res = sdk
.account
.create(&app.config.create_account_secret_code)
.await
.expect("Expected to create account");
let admin_client = NettuSDK::new(address, res.secret_api_key);
let user1 = admin_client
.user
.create(CreateUserInput {
metadata: None,
user_id: None,
})
.await
.unwrap()
.user;
let user2 = admin_client
.user
.create(CreateUserInput {
metadata: None,
user_id: None,
})
.await
.unwrap()
.user;

let calendar1 = admin_client
.calendar
.create(CreateCalendarInput {
user_id: user1.id.clone(),
timezone: chrono_tz::UTC,
week_start: Weekday::Mon,
metadata: None,
})
.await
.unwrap()
.calendar;
let calendar2 = admin_client
.calendar
.create(CreateCalendarInput {
user_id: user2.id.clone(),
timezone: chrono_tz::UTC,
week_start: Weekday::Mon,
metadata: None,
})
.await
.unwrap()
.calendar;

let _event1 = admin_client
.event
.create(CreateEventInput {
user_id: user1.id.clone(),
calendar_id: calendar1.id.clone(),
duration: 1000 * 60 * 60,
reminders: Vec::new(),
busy: None,
recurrence: None,
service_id: None,
start_time: DateTime::from_timestamp_millis(0).unwrap(),
metadata: None,
})
.await
.unwrap()
.event;
let _event2 = admin_client
.event
.create(CreateEventInput {
user_id: user2.id.clone(),
calendar_id: calendar2.id.clone(),
duration: 1000 * 60 * 60,
reminders: Vec::new(),
busy: None,
recurrence: None,
service_id: None,
start_time: DateTime::from_timestamp_millis(1000 * 60 * 60).unwrap(),
metadata: None,
})
.await
.unwrap()
.event;

let multiple_free_busy_req = MultipleFreeBusyRequestBody {
start_time: DateTime::from_timestamp_millis(0).unwrap(),
end_time: DateTime::from_timestamp_millis(1000 * 60 * 60 * 24).unwrap(),
user_ids: vec![user1.id.clone(), user2.id.clone()],
};

let multiple_free_busy_res = admin_client
.user
.multiple_users_free_busy(multiple_free_busy_req)
.await
.unwrap();

let user_ids = multiple_free_busy_res.0.keys();
assert_eq!(user_ids.len(), 2);
// Check that user1 and user2 are in the response
assert!(user_ids.clone().any(|id| id == &user1.id));
assert!(user_ids.clone().any(|id| id == &user2.id));

let user1_free_busy = multiple_free_busy_res.0.get(&user1.id).unwrap();
let user2_free_busy = multiple_free_busy_res.0.get(&user2.id).unwrap();

assert_eq!(
user1_free_busy.front().unwrap().start_time,
DateTime::from_timestamp_millis(0).unwrap(),
);
assert_eq!(
user1_free_busy.front().unwrap().end_time,
DateTime::from_timestamp_millis(1000 * 60 * 60).unwrap(),
);

assert_eq!(
user2_free_busy.front().unwrap().start_time,
DateTime::from_timestamp_millis(1000 * 60 * 60).unwrap(),
);
assert_eq!(
user2_free_busy.front().unwrap().end_time,
DateTime::from_timestamp_millis(1000 * 60 * 60 * 2).unwrap(),
);
}
11 changes: 10 additions & 1 deletion scripts/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ sleep 1
cd crates/infra && sqlx migrate run && cd ../..

# Run the tests
cargo test --workspace && cd ..
# Argument
TEST_NAME=$1

# Add `-- --nocapture` if DEBUG is set
if [ -n "$DEBUG" ]; then
cargo test --workspace $1 -- --nocapture && cd ..
else
# If not in debug mode, run the tests with `cargo-pretty-test`
cargo-pretty-test --workspace $1 && cd ..
fi

# The cleanup function will be called automatically

0 comments on commit f95161e

Please sign in to comment.