doortts doortts 2018-03-13
project: Reduce impact of duplicatd project member
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
@d46526dd42eef0a7fe09a2e3ef1c4d11ba6eb4c0
app/models/ProjectUser.java
--- app/models/ProjectUser.java
+++ app/models/ProjectUser.java
@@ -61,7 +61,10 @@
     }
 
     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) {
@@ -87,8 +90,12 @@
     }
 
     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) {
Add a comment
List