Skip to content

Commit

Permalink
project: Reduce impact of duplicatd project member
Browse files Browse the repository at this point in the history
It is possible to be duplicated when to add a user as a project member.

The best solution is searching and removing all duplicated members.
And after that, add unique constraints into project_user table.

```
delete p1 from project_user p1, project_user p2
 where p1.id > p2.id
 and p1.user_id = p2.user_id
 and p1.project_id = p2.project_id
 and p1.role_id = p2.role_id;

alter table project_user
add constraint uq_project_user_1 unique (user_id, project_id, role_id);
```

But more easy way to control this bug is just making it removable from project member.
This commit implemented second one.

See: Yona Github issue #364
  • Loading branch information
doortts authored and mjpark03 committed Mar 22, 2018
1 parent 9867b3b commit 34d278d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions app/models/ProjectUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public static void create(Long userId, Long projectId, Long roleId) {
}

public static void delete(Long userId, Long projectId) {
ProjectUser.findByIds(userId, projectId).delete();
ProjectUser projectUser = ProjectUser.findByIds(userId, projectId);
if (projectUser != null) {
projectUser.delete();
}
}

public static void assignRole(Long userId, Long projectId, Long roleId) {
Expand All @@ -87,8 +90,12 @@ public static void assignRole(Long userId, Long projectId, RoleType roleType) {
}

public static ProjectUser findByIds(Long userId, Long projectId) {
return find.where().eq("user.id", userId).eq("project.id", projectId)
.ne("role.id", RoleType.SITEMANAGER.roleType()).findUnique();
List<ProjectUser> projectUsers = find.where().eq("user.id", userId).eq("project.id", projectId)
.ne("role.id", RoleType.SITEMANAGER.roleType()).findList();
if(projectUsers.size() > 0) {
return projectUsers.get(0);
}
return null;
}

public static List<ProjectUser> findMemberListByProject(Long projectId) {
Expand Down

0 comments on commit 34d278d

Please sign in to comment.