Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.finboostplus.DTO;


public record SwitchAuthorityRequestDTO(String setAuthority, String authority) {
public record SwitchAuthorityRequestDTO(String setAuthority) {


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.util.List;

import com.finboostplus.enums.Status;
import com.finboostplus.projection.UserExpenseDivisionProjection;

public record UserExpenseDivisionDTO(Long expenseId, String title, String description, Long groupId, String groupName,
Status status, BigDecimal total, Long categoryId, String categoryName, Instant createdAt, List<UserExpenseDivisionProjection> memberList) {
Status status, BigDecimal total, Long categoryId, String categoryName, Instant createdAt, LocalDate deadlineDate, List<UserExpenseDivisionProjection> memberList) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public ResponseEntity<String> addGroupMember(@PathVariable Long groupId,
return new ResponseEntity<>("Membro adicionado com sucesso!", HttpStatus.OK);
}

@PostMapping("/{groupId}/members/{newAuthId}/transfer-ownership")
@PutMapping("/{groupId}/members/{newAuthId}/transfer-ownership")
public ResponseEntity<Object> switchAuthority(@PathVariable Long groupId,
@PathVariable Long newAuthId,
@RequestBody SwitchAuthorityRequestDTO authDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@
public interface GroupRepository extends JpaRepository<Group, Long> {
@Query(nativeQuery = true, value = """
SELECT
GROUP_MEMBERS.USER_ID,
GROUPS.ID,
GROUPS.NAME,
GROUPS.DESCRIPTION,
GROUP_MEMBERS.AUTH_LEVEL AS AUTHORITY,
GROUPS.ICON,
GROUPS.CREATED_AT,
COALESCE(SUM(EXPENSES.VALUE), 0) AS TOTAL_EXPENSES
G.ID,
G.NAME,
G.DESCRIPTION,
GM.AUTH_LEVEL AS AUTHORITY,
G.ICON,
G.CREATED_AT,
COALESCE(SUM(E.VALUE), 0) AS TOTAL_EXPENSES
FROM
GROUP_MEMBERS
INNER JOIN GROUPS ON GROUP_MEMBERS.GROUP_ID = GROUPS.ID
INNER JOIN EXPENSES ON EXPENSES.GROUP_ID = GROUPS.ID
GROUPS G
INNER JOIN GROUP_MEMBERS GM ON GM.GROUP_ID = G.ID
LEFT JOIN EXPENSES E ON E.GROUP_ID = G.ID
WHERE
GROUP_MEMBERS.USER_ID = :memberId
GM.USER_ID = :memberId
GROUP BY
GROUPS.ID,
GROUP_MEMBERS.USER_ID,
AUTH_LEVEL
""")
G.ID,
G.NAME,
G.DESCRIPTION,
GM.AUTH_LEVEL,
G.ICON,
G.CREATED_AT;
""")
Page<GroupProjection> listUserGroupsPaged(Long memberId, Pageable pageable);

@Modifying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public boolean createNewExpense(ExpenseCreateDTO expenseDTO, Long groupId) {

@Transactional(readOnly = true)
public UserExpenseDivisionDTO getExpenseInfoDetails(Long groupId, Long expenseId) {
User user = userRepository.findByEmailIgnoreCase(userService.authenticated())
userRepository.findByEmailIgnoreCase(userService.authenticated())
.orElseThrow(() -> new UserNotFoundException("Usuário não encontrado"));
Group group = groupRepository.findById(groupId)
.orElseThrow(() -> new GroupNotFoundException("Grupo não encontrado"));
Expand All @@ -157,6 +157,7 @@ public UserExpenseDivisionDTO getExpenseInfoDetails(Long groupId, Long expenseId
return new UserExpenseDivisionDTO(expense.getId(), expense.getTitle(),
expense.getDescription(), groupId, group.getName(), expense.getStatus(),
expense.getValue(), category.getId(), category.getName(), expense.getCreatedAt(),
expense.getDeadlineDate(),
memberList);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ public boolean insertGroupOwner(User user, Group group) {
return groupMemberRepository.save(owner) != null ? true : false;
}

@Transactional
public boolean switchAuthGroup(User user, Group group, int auth) {
GroupMember userAuth = new GroupMember();
userAuth.setUser(user);
userAuth.setGroup(group);
userAuth.setAuthorization(this.AUTHORITIES.get(auth));
userAuth.setEntryDate(Instant.now());
userAuth.setId(new GroupMemberId(user.getId(), group.getId()));
return groupMemberRepository.save(userAuth) != null ? true : false;
}
// @Transactional
// public boolean switchAuthGroup(User user, Group group, int auth) {
// GroupMember userAuth = new GroupMember();
// userAuth.setUser(user);
// userAuth.setGroup(group);
// userAuth.setAuthorization(this.AUTHORITIES.get(auth));
// userAuth.setEntryDate(Instant.now());
// userAuth.setId(new GroupMemberId(user.getId(), group.getId()));
// return groupMemberRepository.save(userAuth) != null ? true : false;
// }

@Transactional
public boolean getUsersOnGroupByAuthority(Long userId, Long groupId, List<String> authLevels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.finboostplus.exception.UserNotFoundException;
import com.finboostplus.exception.ValuesIncompatiblesException;
import com.finboostplus.model.Group;
import com.finboostplus.model.GroupMember;
import com.finboostplus.model.Role;
import com.finboostplus.model.User;
import com.finboostplus.model.ValidateUser;
Expand Down Expand Up @@ -233,32 +234,35 @@ public String validateUser(String uuid) {
@Transactional
public boolean switchAuthority(Long newOwnerId, Long groupId, SwitchAuthorityRequestDTO authDTO) {
List<String> authLevels = List.of("OWNER", "ADMIN", "USER");
String setAuthority = authDTO.setAuthority();
String authority = authDTO.authority();

if (!authLevels.contains(authority.toUpperCase().trim())
&& authLevels.contains(setAuthority.toUpperCase().trim())) {
throw new ValuesIncompatiblesException(
"Os valores recebidos não coincidem com os valores suportados");
}
String setAuthority = authDTO.setAuthority().toUpperCase().trim();
if (!authLevels.contains(setAuthority))
throw new ValuesIncompatiblesException("Valor inválido");
User user = userRepository.findByEmailIgnoreCase(authenticated())
.orElseThrow(() -> new UserNotFoundException("Usuário nao encontrado"));
User newUserAuth = userRepository.findById(newOwnerId)
GroupMember newUserAuth = groupMemberRepository.findGroupMemberByMemberId(newOwnerId, groupId)
.orElseThrow(() -> new UserNotFoundException("Usuário não encontrado"));
if (user.getId() == newUserAuth.getUser().getId())
throw new ForbiddenResourceException("Operação inválida");
Group group = groupService.getGroup(groupId);
if (group == null) {
if (group == null)
throw new GroupNotFoundException("Grupo não encontrado");
}
if (!groupMemberRepository.isUserGroupOwner(user.getId(),
group.getId())) {
throw new ForbiddenResourceException("Usuário não tem permissão para realizar essa operação");
} else if (!groupMemberService.isUserMemberOfGroup(newUserAuth.getId(), group.getId())) {
throw new UserNotFoundException("Usuário não pertence a este grupo");
} else if (groupMemberService.switchAuthGroup(newUserAuth, group, authLevels.indexOf(setAuthority))
&& groupMemberService.switchAuthGroup(user, group, authLevels.indexOf(authority))) {
if (setAuthority == "OWNER") {
if (!groupMemberRepository.isUserGroupOwner(user.getId(),
group.getId()))
throw new ForbiddenResourceException("Acesso negado");
GroupMember owner = groupMemberRepository.findGroupMemberByMemberId(user.getId(), groupId)
.orElseThrow(() -> new UserNotFoundException("Usuário não encontrado"));
owner.setAuthorization("ADMIN");
newUserAuth.setAuthorization(setAuthority);
groupMemberRepository.save(owner);
groupMemberRepository.save(newUserAuth);
return true;
} else {
return false;
}
if (!groupMemberRepository.doesUserHasAnyAuthority(user.getId(),
group.getId(), List.of("OWNER" ,"ADMIN")))
throw new ForbiddenResourceException("Acesso negado");
newUserAuth.setAuthorization(setAuthority);
groupMemberRepository.save(newUserAuth);
return true;
}
}
Loading