Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed subscribedCompanies being initialized as null and doing the sam… #63

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/main/java/com/MeetMate/company/CompanyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> 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<Long> 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);
Expand All @@ -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<Long> a = company.getMemberIds();
a.remove(memberId);
update.set("memberIds", a);

mongoTemplate.updateFirst(query, update, Company.class);
}

private Company getCompanyWithToken(String token) throws IllegalArgumentException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/MeetMate/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, Object> generateMap() {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/MeetMate/user/UserService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +17,7 @@
import org.springframework.util.MultiValueMap;

import javax.naming.NameAlreadyBoundException;
import java.util.ArrayList;
import java.util.List;

@Service
Expand All @@ -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);
Expand Down Expand Up @@ -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()
);
}
}
Loading