-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UserTaskMatcher gets tasks with most overlapping skills to User (fixes …
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule CodeCorps.UserTaskMatcher do | ||
@moduledoc """ | ||
Find the top tasks most matching a User's skills | ||
""" | ||
|
||
alias CodeCorps.{Repo, Task, User, TaskSkill} | ||
import Ecto.Query | ||
|
||
@spec match_user(User.t, number) :: [Task.t] | ||
def match_user(%CodeCorps.User{} = user, tasks_count) do | ||
query = from t in TaskSkill, | ||
join: skill in assoc(t, :skill), | ||
join: user_skill in assoc(skill, :user_skills), | ||
join: user in assoc(user_skill, :user), | ||
where: user.id == ^user.id, | ||
group_by: t.task_id, | ||
order_by: count(t.task_id), | ||
limit: ^tasks_count, | ||
select: t.task_id | ||
|
||
matches = query |> Repo.all | ||
|
||
Task |> where([t], t.id in ^matches) |> Repo.all | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule CodeCorps.UserTaskMatcherTest do | ||
use CodeCorps.ModelCase | ||
|
||
import CodeCorps.UserTaskMatcher | ||
|
||
test "can find top x tasks for a user's skill" do | ||
coding = insert(:skill, title: "coding") | ||
design = insert(:skill, title: "design") | ||
|
||
account_page = insert(:task) | ||
settings_page = insert(:task) | ||
photoshop = insert(:task) | ||
|
||
insert(:task) | ||
|
||
insert(:task_skill, task: account_page, skill: design) | ||
insert(:task_skill, task: account_page, skill: coding) | ||
insert(:task_skill, task: settings_page, skill: design) | ||
insert(:task_skill, task: settings_page, skill: coding) | ||
insert(:task_skill, task: photoshop, skill: coding) | ||
|
||
user = insert(:user) | ||
|
||
insert(:user_skill, user: user, skill: design) | ||
insert(:user_skill, user: user, skill: coding) | ||
|
||
tasks = match_user(user, 2) | ||
|
||
assert(length(tasks) == 2) | ||
assert(length(match_user(user, 3)) == 3) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters