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

Ipk 168 user to company member integration #62

Merged
merged 8 commits into from
Oct 16, 2024
Merged
8 changes: 6 additions & 2 deletions src/main/java/com/MeetMate/company/Company.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.ArrayList;

@Document(collection = "companies")
@Data
@NoArgsConstructor
Expand All @@ -14,15 +16,17 @@ public class Company {
private String name;
private String description;
private BusinessType businessType;
private String[] memberEmails;
private ArrayList<Long> memberIds;
@Indexed(unique = true)
private String ownerEmail;

public Company(long id,String name, String ownerEmail) {
public Company(long id,String name, String ownerEmail, long ownerId) {
this.id = id;
this.name = name;
this.ownerEmail = ownerEmail;
this.description = "";
this.businessType = null;
this.memberIds = new ArrayList<>();
this.memberIds.add(ownerId);
}
}
91 changes: 89 additions & 2 deletions src/main/java/com/MeetMate/company/CompanyController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.MeetMate.company;

import com.MeetMate.enums.UserRole;
import com.MeetMate.response.GetResponse;
import com.MeetMate.user.User;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.graphql.data.method.annotation.Argument;
Expand All @@ -12,6 +15,7 @@
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.InaccessibleObjectException;
import java.util.ArrayList;

@Controller
@RequestMapping(path = "api/company")
Expand All @@ -27,7 +31,7 @@ public Company getCompany(@Argument long id) {

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
return new Company(-1, "error", "error");
return new Company(-1, "error", "error", -1);

// if (tc == EntityNotFoundException.class)
// return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());
Expand Down Expand Up @@ -75,7 +79,7 @@ public ResponseEntity<?> editCompany(
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());

if (tc == IllegalArgumentException.class)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("message: " + t.getMessage());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage());
}
Expand All @@ -100,4 +104,87 @@ public ResponseEntity<?> deleteCompany(@ContextValue String token) {
}
}

////////////////MEMBER MANAGEMENT////////////////

@QueryMapping
public GetResponse getMember(
@ContextValue String token,
@Argument long memberId
) {
token = token.substring(7);
try {
GetResponse g = companyService.getMember(token, memberId);
System.out.println(g.toString());
return g;
} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
return null;
}
}

@QueryMapping
public ArrayList<GetResponse> getAllMembers(
@ContextValue String token
) {
token = token.substring(7);
try {
return companyService.getAllMembers(token);

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
return null;
}
}

@MutationMapping
public ResponseEntity<?> addMember(
@ContextValue String token,
@Argument String memberEmail,
@Argument String memberName,
@Argument String memberPassword) {
token = token.substring(7);
try {
companyService.addMember(token, memberEmail, memberName, memberPassword);
return ResponseEntity.ok().build();

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();

if (tc == EntityNotFoundException.class)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());

if (tc == IllegalArgumentException.class)
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("message: " + t.getMessage());

if (tc == IllegalStateException.class)
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage());
}
}

@MutationMapping
public ResponseEntity<?> deleteMember(
@ContextValue String token,
@Argument long memberId
) {
token = token.substring(7);
try {
companyService.deleteMember(token, memberId);
return ResponseEntity.ok().build();

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
if (tc == EntityNotFoundException.class)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());

if (tc == IllegalArgumentException.class)
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("message: " + t.getMessage());

if (tc == IllegalAccessException.class)
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("message: " + t.getMessage());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage());
}
}
}
84 changes: 83 additions & 1 deletion src/main/java/com/MeetMate/company/CompanyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.MeetMate.company.sequence.CompanySequenceService;
import com.MeetMate.enums.BusinessType;
import com.MeetMate.enums.UserRole;
import com.MeetMate.response.GetResponse;
import com.MeetMate.security.JwtService;
import com.MeetMate.user.User;
import com.MeetMate.user.UserController;
import com.MeetMate.user.UserRepository;
import jakarta.persistence.EntityNotFoundException;
Expand All @@ -17,6 +19,8 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;

@Service
@RequiredArgsConstructor
public class CompanyService {
Expand Down Expand Up @@ -45,8 +49,11 @@ public void createCompany(String companyName, String ownerEmail, String ownerNam
ownerData.add("associatedCompany", String.valueOf(companyId));

userController.registerNewUser(ownerData);
long ownerId = userRepository.findUserByEmail(ownerEmail)
.orElseThrow(() -> new IllegalStateException("Owner could not be created correctly!"))
.getId();

companyRepository.save(new Company(companyId, companyName, ownerEmail));
companyRepository.save(new Company(companyId, companyName, ownerEmail, ownerId));
companySequenceService.incrementId();
}

Expand All @@ -73,6 +80,63 @@ public void deleteCompany(String token) {
companyRepository.delete(company);
}

public GetResponse getMember(String token, long memberId) {
Company company = getCompanyWithToken(token);

GetResponse member = getMemberById(memberId);

if (!company.getMemberIds().contains(member.getId()))
throw new EntityNotFoundException("Member not found");

return member;
}

public ArrayList<GetResponse> getAllMembers(String token) {
Company company = getCompanyWithToken(token);
System.out.println(company.toString());
ArrayList<GetResponse> members = new ArrayList<>();
for (Long memberId : company.getMemberIds()) {
GetResponse member = getMemberById(memberId);
members.add(member);
}
System.out.println(members.toString());
return members;
}

@Transactional
public void addMember(String token, String memberEmail, String memberName, String memberPassword) {
Company company = getCompanyWithToken(token);

MultiValueMap<String, String> memberData = new LinkedMultiValueMap<>();
memberData.add("email", memberEmail);
memberData.add("name", memberName);
memberData.add("password", memberPassword);
memberData.add("role", UserRole.COMPANY_MEMBER.toString());
memberData.add("associatedCompany", String.valueOf(company.getId()));

userController.registerNewUser(memberData);

Query query = new Query(Criteria.where("ownerEmail").is(company.getOwnerEmail()));
Update update = new Update();
ArrayList<Long> a = company.getMemberIds();
a.add(userRepository.findUserByEmail(memberEmail)
.orElseThrow(() -> new IllegalStateException("Member could not be created correctly!"))
.getId());
update.set("memberIds", a);

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

@Transactional
public void deleteMember(String token, long memberId) throws IllegalAccessException {
Company company = getCompanyWithToken(token);

if (!isCompanyMember(company, memberId))
throw new IllegalAccessException("Not a member of the company");

userRepository.deleteById(memberId);
}

private Company getCompanyWithToken(String token) throws IllegalArgumentException {
String ownerEmail = jwtService.extractUserEmail(token);
if (userRepository.findUserByEmail(ownerEmail)
Expand All @@ -83,4 +147,22 @@ private Company getCompanyWithToken(String token) throws IllegalArgumentExceptio
return companyRepository.findCompanyByOwnerEmail(ownerEmail)
.orElseThrow(() -> new EntityNotFoundException("Company not found"));
}

private boolean isCompanyMember(Company company, long memberId) {
return company.getMemberIds().contains(memberId);
}

private GetResponse getMemberById(long id) {
User user = userRepository.findUserById(id)
.orElseThrow(() -> new EntityNotFoundException("User does not exist"));

return GetResponse.builder()
.id(user.getId())
.name(user.getName())
.created_at(user.getCreatedAt())
.email(user.getEmail())
.role(user.getRole())
.associatedCompany(user.getAssociatedCompany())
.build();
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/MeetMate/response/GetResponse.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.MeetMate.response;

import com.MeetMate.enums.UserRole;

import java.time.LocalDate;
import java.util.ArrayList;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -16,4 +19,6 @@ public class GetResponse {
LocalDate created_at;
String email;
UserRole role;
long associatedCompany;
ArrayList<Long> subscribedCompanies;
}
19 changes: 9 additions & 10 deletions src/main/java/com/MeetMate/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDate;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@Entity
@Table(name = "users")
Expand All @@ -32,7 +29,7 @@ public class User implements UserDetails {
private String refreshToken;
// bool verified
private long associatedCompany;
private long[] subscribedCompanies;
private ArrayList<Long> subscribedCompanies;

public User() {}

Expand All @@ -43,6 +40,13 @@ public User(Long id, String name, String email, String password) {
this.email = email;
this.password = password;
this.createdAt = LocalDate.now();
subscribedCompanies = new ArrayList<>();
}

@Experimentational
public User(String email, String password) {
this.email = email;
this.password = password;
}

public User(String name, String email, String password, UserRole role) {
Expand All @@ -52,11 +56,6 @@ public User(String name, String email, String password, UserRole role) {
this.createdAt = LocalDate.now();
this.role = role;
}
@Experimentational
public User(String email, String password) {
this.email = email;
this.password = password;
}

public Map<String, Object> generateMap() {
HashMap<String, Object> map = new HashMap<>();
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/com/MeetMate/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class UserController {
public ResponseEntity<?> getUser(@RequestHeader(name = "Authorization") String token) {
token = token.substring(7);
try {
return ResponseEntity.ok(userService.getUserByEmail(token));
return ResponseEntity.ok(userService.getUser(token));

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
Expand Down Expand Up @@ -152,4 +152,29 @@ public ResponseEntity<?> deleteUser(@RequestHeader(name = "Authorization") Strin
.body("type: " + tc + "\nmessage: " + t.getMessage());
}
}

@PutMapping(path = "subscribe")
@ResponseBody
public ResponseEntity<?> subscribeToCompany(
@RequestHeader(name = "Authorization") String token,
@RequestParam long companyId
) {
token = token.substring(7);
try {
userService.subscribeToCompany(token, companyId);
return ResponseEntity.ok().build();

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();

if (tc == EntityNotFoundException.class)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage());

if (tc == IllegalArgumentException.class)
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body("message: " + t.getMessage());

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("message: " + t.getMessage());
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/MeetMate/user/UserRepository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.MeetMate.user;

import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -15,4 +16,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findUserById(Long id);

void deleteByEmail(String email);

void deleteById(@NotNull Long id);
}
Loading
Loading