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
Expand Up @@ -2,6 +2,6 @@

import java.math.BigDecimal;

public record GroupDetailsDTO(Long id, String name, String description, String icon, String authorization,
public record GroupDetailsDTO(Long id, String name, String description, String icon, String authorization, BigDecimal partial_total,
BigDecimal total) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.finboostplus.DTO;

public record GroupMemberAuthorityDTO(String authority){}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import com.finboostplus.DTO.CategoryDTO;
import com.finboostplus.DTO.GroupCreateDTO;
import com.finboostplus.DTO.GroupDetailsDTO;
import com.finboostplus.DTO.GroupMemberAuthorityDTO;
import com.finboostplus.DTO.GroupMemberDTO;
import com.finboostplus.DTO.GroupMemberResponseDTO;
import com.finboostplus.DTO.GroupUpdateDTO;
import com.finboostplus.DTO.SwitchAuthorityRequestDTO;
import com.finboostplus.model.Group;
import com.finboostplus.projection.GroupProjection;
import com.finboostplus.repository.GroupRepository;
import com.finboostplus.repository.UserRepository;
import com.finboostplus.service.CategoryService;
import com.finboostplus.service.ExpenseService;
Expand Down Expand Up @@ -91,6 +93,12 @@ public ResponseEntity<GroupDetailsDTO> getGroupDetails(@PathVariable Long groupI
return new ResponseEntity<>(dto, HttpStatus.OK);
}

@GetMapping("/{groupId}/members/{memberId}/authority")
public ResponseEntity<GroupMemberAuthorityDTO> getMemberAuthority(@PathVariable Long groupId, @PathVariable Long memberId) {
GroupMemberAuthorityDTO authority = groupService.getMemberAuthority(groupId, memberId);
return ResponseEntity.ok(authority);
}

@PostMapping
public ResponseEntity<String> createGroup(@Valid @RequestBody GroupCreateDTO dto) {
boolean groupIsCreated = groupService.createNewGroup(dto);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.finboostplus.projection;

import java.math.BigDecimal;
import java.time.Instant;

public interface GroupProjection {
Long getId();
Long getGroupId();

String getName();

Expand All @@ -15,5 +16,7 @@ public interface GroupProjection {

Instant getCreatedAt();

Double getTotalExpenses();
BigDecimal getPartial_total();

BigDecimal getTotal();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,48 @@
import org.springframework.stereotype.Repository;

import com.finboostplus.DTO.GroupDetailsDTO;
import com.finboostplus.DTO.GroupMemberAuthorityDTO;
import com.finboostplus.model.Group;
import com.finboostplus.projection.GroupProjection;

@Repository
public interface GroupRepository extends JpaRepository<Group, Long> {
@Query(nativeQuery = true, value = """
SELECT
G.ID,
G.ID AS GROUP_ID,
G.NAME,
G.DESCRIPTION,
GM.AUTH_LEVEL AS AUTHORITY,
G.ICON,
G.CREATED_AT,
COALESCE(SUM(E.VALUE), 0) AS TOTAL_EXPENSES
COALESCE(SUM(UED.PARTIAL_VALUE), 0) AS PARTIAL_TOTAL,
COALESCE(SUM(E.VALUE), 0) AS TOTAL
FROM
GROUPS G
INNER JOIN GROUP_MEMBERS GM ON GM.GROUP_ID = G.ID
LEFT JOIN EXPENSES E ON E.GROUP_ID = G.ID
WHERE
GM.USER_ID = :memberId
AND E.STATUS != 'PAID' -- aplica filtro no join!
LEFT JOIN USER_EXPENSE_DIVISIONS UED ON UED.EXPENSE_ID = E.ID
AND UED.USER_ID = :memberId
AND UED.STATUS != 'PAID'
GROUP BY
G.ID,
G.NAME,
G.DESCRIPTION,
GM.AUTH_LEVEL,
G.ICON,
G.CREATED_AT;
G.CREATED_AT
""")
Page<GroupProjection> listUserGroupsPaged(Long memberId, Pageable pageable);

@Query(nativeQuery = true, value = """
SELECT GM.AUTH_LEVEL AS AUTHORITY
FROM GROUP_MEMBERS AS GM
WHERE GM.GROUP_ID = :groupId
AND GM.USER_ID = :memberId
""")
GroupMemberAuthorityDTO getMemberAuthority(Long groupId, Long memberId);

@Modifying
@Query(nativeQuery = true, value = """
DELETE FROM groups
Expand All @@ -56,12 +68,20 @@ public interface GroupRepository extends JpaRepository<Group, Long> {
(
SELECT
COALESCE(SUM(UED.PARTIAL_VALUE), 0)
FROM
USER_EXPENSE_DIVISIONS UED
WHERE
UED.USER_ID = :userId
AND UED.STATUS != 'PAID'
) AS PARTIAL_TOTAL,
(
SELECT
COALESCE(SUM(E.VALUE), 0)
FROM
EXPENSES E
INNER JOIN USER_EXPENSE_DIVISIONS UED ON E.ID = UED.EXPENSE_ID
WHERE
E.GROUP_ID = :groupId
AND UED.USER_ID = :userId
AND E.STATUS != 'PAID'
) AS TOTAL
FROM
GROUPS AS G
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.finboostplus.DTO.GroupCreateDTO;
import com.finboostplus.DTO.GroupDetailsDTO;
import com.finboostplus.DTO.GroupMemberAuthorityDTO;
import com.finboostplus.DTO.GroupMemberResponseDTO;
import com.finboostplus.DTO.GroupUpdateDTO;
import com.finboostplus.exception.ForbiddenResourceException;
Expand Down Expand Up @@ -115,6 +116,19 @@ public Group getGroup(Long idGroup) {
return group;
}

@Transactional(readOnly = true)
public GroupMemberAuthorityDTO getMemberAuthority(Long groupId, Long memberId) {
User user = userRepository
.findByEmailIgnoreCase(userService.authenticated())
.orElseThrow(() -> new UsernameNotFoundException("Usuário não encontrado!"));
boolean isLoggedUserMemberOfGroup = groupMemberRepository.isUserMemberOfGroup(user.getId(), groupId);
boolean isUserMemberOfGroup = groupMemberRepository.isUserMemberOfGroup(memberId, groupId);
if (isLoggedUserMemberOfGroup && isUserMemberOfGroup) {
return groupRepository.getMemberAuthority(groupId, memberId);
}
throw new ForbiddenResourceException("Acesso negado");
}

@Transactional(readOnly = true)
public GroupDetailsDTO getGroupDetails(Long groupId) {
User user = userRepository.findByEmailIgnoreCase(userService.authenticated())
Expand Down
Loading