Skip to content

Commit 77663d1

Browse files
committed
UserTaskMatcher gets tasks with most overlapping skills to User (fixes #736)
1 parent 82d9f2e commit 77663d1

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

lib/code_corps/user_task_matcher.ex

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule CodeCorps.UserTaskMatcher do
2+
@moduledoc """
3+
Find the top tasks most matching a User's skills
4+
"""
5+
6+
alias CodeCorps.{Repo, Task, User, TaskSkill}
7+
import Ecto.Query
8+
9+
@spec match_user(User.t, number) :: [Task.t]
10+
def match_user(%CodeCorps.User{} = user, tasks_count) do
11+
query = from t in TaskSkill,
12+
join: skill in assoc(t, :skill),
13+
join: user_skill in assoc(skill, :user_skills),
14+
join: user in assoc(user_skill, :user),
15+
where: user.id == ^user.id,
16+
group_by: t.task_id,
17+
order_by: count(t.task_id),
18+
limit: ^tasks_count,
19+
select: t.task_id
20+
21+
matches = query |> Repo.all
22+
23+
Task |> where([t], t.id in ^matches) |> Repo.all
24+
end
25+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule CodeCorps.UserTaskMatcherTest do
2+
use CodeCorps.ModelCase
3+
4+
import CodeCorps.UserTaskMatcher
5+
6+
test "can find top x tasks for a user's skill" do
7+
coding = insert(:skill, title: "coding")
8+
design = insert(:skill, title: "design")
9+
10+
account_page = insert(:task)
11+
settings_page = insert(:task)
12+
photoshop = insert(:task)
13+
14+
insert(:task)
15+
16+
insert(:task_skill, task: account_page, skill: design)
17+
insert(:task_skill, task: account_page, skill: coding)
18+
insert(:task_skill, task: settings_page, skill: design)
19+
insert(:task_skill, task: settings_page, skill: coding)
20+
insert(:task_skill, task: photoshop, skill: coding)
21+
22+
user = insert(:user)
23+
24+
insert(:user_skill, user: user, skill: design)
25+
insert(:user_skill, user: user, skill: coding)
26+
27+
tasks = match_user(user, 2)
28+
29+
assert(length(tasks) == 2)
30+
assert(length(match_user(user, 3)) == 3)
31+
end
32+
end

web/models/skill.ex

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule CodeCorps.Skill do
1212
has_many :roles, through: [:role_skills, :role]
1313

1414
has_many :project_skills, CodeCorps.ProjectSkill
15+
has_many :user_skills, CodeCorps.UserSkill
1516
has_many :projects, through: [:project_skills, :project]
1617

1718
timestamps()

0 commit comments

Comments
 (0)