Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scope coaching_relationships query by user_id #93

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions entity_api/src/coaching_relationship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub async fn find_by_organization(
pub async fn find_by_organization_with_user_names(
db: &DatabaseConnection,
organization_id: Id,
user_id: Id,
) -> Result<Vec<CoachingRelationshipWithUserNames>, Error> {
let coaches = Alias::new("coaches");
let coachees = Alias::new("coachees");
Expand All @@ -79,6 +80,11 @@ pub async fn find_by_organization_with_user_names(
coachees::Relation::CoachingRelationships.def().rev(),
coachees.clone(),
)
.filter(
Condition::any()
.add(coaching_relationships::Column::CoachId.eq(user_id))
.add(coaching_relationships::Column::CoacheeId.eq(user_id)),
)
.select_only()
.column(coaching_relationships::Column::Id)
.column(coaching_relationships::Column::OrganizationId)
Expand Down Expand Up @@ -271,14 +277,19 @@ mod tests {
let db = MockDatabase::new(DatabaseBackend::Postgres).into_connection();

let organization_id = Id::new_v4();
let _ = find_by_organization_with_user_names(&db, organization_id).await;
let user_id = Id::new_v4();
let _ = find_by_organization_with_user_names(&db, organization_id, user_id).await;

assert_eq!(
db.into_transaction_log(),
[Transaction::from_sql_and_values(
DatabaseBackend::Postgres,
r#"SELECT "coaching_relationships"."id", "coaching_relationships"."organization_id", "coaching_relationships"."coach_id", "coaching_relationships"."coachee_id", "coaching_relationships"."created_at", "coaching_relationships"."updated_at", coaches.first_name AS "coach_first_name", coaches.last_name AS "coach_last_name", coachees.first_name AS "coachee_first_name", coachees.last_name AS "coachee_last_name" FROM "refactor_platform"."coaching_relationships" JOIN "refactor_platform"."users" AS "coaches" ON "coaching_relationships"."coach_id" = "coaches"."id" JOIN "refactor_platform"."users" AS "coachees" ON "coaching_relationships"."coachee_id" = "coachees"."id" WHERE "coaching_relationships"."organization_id" IN (SELECT "organizations"."id" FROM "refactor_platform"."organizations" WHERE "organizations"."id" = $1)"#,
[organization_id.clone().into()]
r#"SELECT "coaching_relationships"."id", "coaching_relationships"."organization_id", "coaching_relationships"."coach_id", "coaching_relationships"."coachee_id", "coaching_relationships"."created_at", "coaching_relationships"."updated_at", coaches.first_name AS "coach_first_name", coaches.last_name AS "coach_last_name", coachees.first_name AS "coachee_first_name", coachees.last_name AS "coachee_last_name" FROM "refactor_platform"."coaching_relationships" JOIN "refactor_platform"."users" AS "coaches" ON "coaching_relationships"."coach_id" = "coaches"."id" JOIN "refactor_platform"."users" AS "coachees" ON "coaching_relationships"."coachee_id" = "coachees"."id" WHERE "coaching_relationships"."organization_id" IN (SELECT "organizations"."id" FROM "refactor_platform"."organizations" WHERE "organizations"."id" = $1) AND ("coaching_relationships"."coach_id" = $2 OR "coaching_relationships"."coachee_id" = $3)"#,
[
organization_id.clone().into(),
user_id.clone().into(),
user_id.clone().into()
]
)]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub async fn read(
)]
pub async fn index(
CompareApiVersion(_v): CompareApiVersion,
AuthenticatedUser(_user): AuthenticatedUser,
AuthenticatedUser(user): AuthenticatedUser,
// TODO: create a new Extractor to authorize the user to access
// the data requested
State(app_state): State<AppState>,
Expand All @@ -123,6 +123,7 @@ pub async fn index(
let coaching_relationships = CoachingRelationshipApi::find_by_organization_with_user_names(
app_state.db_conn_ref(),
organization_id,
user.id,
)
.await?;

Expand Down