diff --git a/src/main/java/com/MeetMate/company/CompanyService.java b/src/main/java/com/MeetMate/company/CompanyService.java index 45dad12..a10399c 100644 --- a/src/main/java/com/MeetMate/company/CompanyService.java +++ b/src/main/java/com/MeetMate/company/CompanyService.java @@ -118,10 +118,16 @@ public void addMember(String token, String memberEmail, String memberName, Strin Query query = new Query(Criteria.where("ownerEmail").is(company.getOwnerEmail())); Update update = new Update(); - ArrayList a = company.getMemberIds(); - a.add(userRepository.findUserByEmail(memberEmail) + + Long memberId = userRepository.findUserByEmail(memberEmail) .orElseThrow(() -> new IllegalStateException("Member could not be created correctly!")) - .getId()); + .getId(); + + ArrayList a = company.getMemberIds(); + if (a.contains(memberId)) + throw new IllegalStateException("Member already exists"); + + a.add(memberId); update.set("memberIds", a); mongoTemplate.updateFirst(query, update, Company.class); @@ -134,7 +140,18 @@ public void deleteMember(String token, long memberId) throws IllegalAccessExcept if (!isCompanyMember(company, memberId)) throw new IllegalAccessException("Not a member of the company"); - userRepository.deleteById(memberId); + User member = userRepository.findUserById(memberId) + .orElseThrow(() -> new EntityNotFoundException("Member not found")); + + userRepository.delete(member); + + Query query = new Query(Criteria.where("ownerEmail").is(company.getOwnerEmail())); + Update update = new Update(); + ArrayList a = company.getMemberIds(); + a.remove(memberId); + update.set("memberIds", a); + + mongoTemplate.updateFirst(query, update, Company.class); } private Company getCompanyWithToken(String token) throws IllegalArgumentException { diff --git a/src/main/java/com/MeetMate/user/User.java b/src/main/java/com/MeetMate/user/User.java index f583b4d..00ffd32 100644 --- a/src/main/java/com/MeetMate/user/User.java +++ b/src/main/java/com/MeetMate/user/User.java @@ -40,7 +40,6 @@ public User(Long id, String name, String email, String password) { this.email = email; this.password = password; this.createdAt = LocalDate.now(); - subscribedCompanies = new ArrayList<>(); } @Experimentational @@ -55,6 +54,7 @@ public User(String name, String email, String password, UserRole role) { this.password = password; this.createdAt = LocalDate.now(); this.role = role; + subscribedCompanies = new ArrayList<>(); } public Map generateMap() { diff --git a/src/main/java/com/MeetMate/user/UserService.java b/src/main/java/com/MeetMate/user/UserService.java index 3eb1d84..9a5c8bc 100644 --- a/src/main/java/com/MeetMate/user/UserService.java +++ b/src/main/java/com/MeetMate/user/UserService.java @@ -1,5 +1,6 @@ package com.MeetMate.user; +import com.MeetMate.company.CompanyRepository; import com.MeetMate.enums.UserRole; import com.MeetMate.response.AuthenticationResponse; import com.MeetMate.response.GetResponse; @@ -16,6 +17,7 @@ import org.springframework.util.MultiValueMap; import javax.naming.NameAlreadyBoundException; +import java.util.ArrayList; import java.util.List; @Service @@ -26,6 +28,7 @@ public class UserService { private final JwtService jwtService; private final PasswordEncoder passwordEncoder; private final AuthenticationManager authenticationManager; + private final CompanyRepository companyRepository; public GetResponse getUser(String token) { String email = jwtService.extractUserEmail(token); @@ -176,6 +179,13 @@ public void subscribeToCompany(String token, long companyId) { if (user.getRole() != UserRole.CLIENT) throw new IllegalArgumentException("Only CLIENT users can subscribe to companies"); - user.getSubscribedCompanies().add(companyId); + if(user.getSubscribedCompanies().contains(companyId)) + throw new IllegalArgumentException("User is already subscribed to this company!"); + + user.getSubscribedCompanies() + .add(companyRepository.findCompanyById(companyId) + .orElseThrow(() -> new EntityNotFoundException("Company could not be found!")) + .getId() + ); } }