Skip to content
Extraits de code Groupes Projets
Valider 21181f75 rédigé par Mohamed Lemine BAILLAHI's avatar Mohamed Lemine BAILLAHI
Parcourir les fichiers

Merge branch 'feature/MLC-251' into 'develop'

MLC-251/update session & activity history path

Closes MLC-251

See merge request !8
parents 09ba5474 89814571
Branches
1 requête de fusion!8MLC-251/update session & activity history path
...@@ -23,4 +23,5 @@ ...@@ -23,4 +23,5 @@
- MLC-296/Updated AgentService to enrich AgentResponseDTO with agency name - MLC-296/Updated AgentService to enrich AgentResponseDTO with agency name
- MLC-296/Added agency name filtering support (case-insensitive, partial match) - MLC-296/Added agency name filtering support (case-insensitive, partial match)
- MLC-296/Fixed data type mismatch for agencyId (String → Long) - MLC-296/Fixed data type mismatch for agencyId (String → Long)
- MLC-251/Update Sessions & ActivityHistory Path
...@@ -33,12 +33,12 @@ public class Paths { ...@@ -33,12 +33,12 @@ public class Paths {
public static final String SESSION_URL = "/api/sessions"; public static final String SESSION_URL = BASE_URL+"/sessions";
public static final String SESSION_BY_USER_ID = "/user/{userId}"; public static final String SESSION_BY_USER_ID = "user/{userId}";
public static final String SESSION_CURRENT_BY_USER_ID = "/user/{userId}/current"; public static final String SESSION_CURRENT_BY_USER_ID = "/{userId}/current";
public static final String SESSION_INVALIDATE_BY_ID = "/{sessionId}/invalidate"; public static final String SESSION_INVALIDATE_BY_ID = "/{sessionId}/invalidate";
public static final String ACTIVITY_URL = "/api/activities"; public static final String ACTIVITY_URL = BASE_URL+"/activities";
public static final String ACTIVITY_BY_USER_ID = "/user/{userId}"; public static final String ACTIVITY_BY_USER_ID = "/user/{userId}";
public static final String RECENT_ACTIVITY = "recent"; public static final String RECENT_ACTIVITY = "recent";
......
...@@ -31,9 +31,9 @@ public class SessionController { ...@@ -31,9 +31,9 @@ public class SessionController {
} }
@GetMapping(Paths.SESSION_CURRENT_BY_USER_ID) @GetMapping(Paths.SESSION_CURRENT_BY_USER_ID)
public ResponseEntity<SessionDTO> getCurrentSession(@PathVariable Long userId) throws FunctionalException { public ResponseEntity<List<SessionDTO>> getCurrentSessions(@PathVariable Long userId) throws FunctionalException {
SessionDTO currentSession = sessionService.getCurrentSession(userId); List<SessionDTO> currentSessions = sessionService.getCurrentSessions(userId);
return ResponseEntity.ok(currentSession); return ResponseEntity.ok(currentSessions);
} }
@PostMapping(Paths.SESSION_INVALIDATE_BY_ID) @PostMapping(Paths.SESSION_INVALIDATE_BY_ID)
......
...@@ -9,5 +9,5 @@ import java.util.Optional; ...@@ -9,5 +9,5 @@ import java.util.Optional;
@Repository @Repository
public interface SessionRepository extends JpaRepository<Session, Long> { public interface SessionRepository extends JpaRepository<Session, Long> {
List<Session> findByUserId(Long userId); List<Session> findByUserId(Long userId);
Optional<Session> findByUserIdAndIsCurrentTrue(Long userId); List<Session> findByUserIdAndIsCurrentTrue(Long userId);
} }
\ No newline at end of file
...@@ -8,6 +8,6 @@ import java.util.List; ...@@ -8,6 +8,6 @@ import java.util.List;
public interface SessionService { public interface SessionService {
SessionDTO createSession(SessionDTO sessionDTO) throws FunctionalException; SessionDTO createSession(SessionDTO sessionDTO) throws FunctionalException;
List<SessionDTO> getSessionsByUserId(Long userId) throws FunctionalException; List<SessionDTO> getSessionsByUserId(Long userId) throws FunctionalException;
SessionDTO getCurrentSession(Long userId) throws FunctionalException; List<SessionDTO> getCurrentSessions(Long userId) throws FunctionalException;
void invalidateSession(Long sessionId) throws FunctionalException; void invalidateSession(Long sessionId) throws FunctionalException;
} }
\ No newline at end of file
...@@ -47,13 +47,6 @@ public class SessionServiceImpl implements SessionService { ...@@ -47,13 +47,6 @@ public class SessionServiceImpl implements SessionService {
session.setLastActive(LocalDateTime.now()); session.setLastActive(LocalDateTime.now());
session.setCurrent(true); session.setCurrent(true);
// Invalidate any existing current session for this user
sessionRepository.findByUserIdAndIsCurrentTrue(user.getId())
.ifPresent(existingSession -> {
existingSession.setCurrent(false);
sessionRepository.save(existingSession);
});
Session savedSession = sessionRepository.save(session); Session savedSession = sessionRepository.save(session);
return SessionMapper.toDTO(savedSession); return SessionMapper.toDTO(savedSession);
} }
...@@ -64,7 +57,6 @@ public class SessionServiceImpl implements SessionService { ...@@ -64,7 +57,6 @@ public class SessionServiceImpl implements SessionService {
throw new FunctionalException(Message.USER_ID_NULL); throw new FunctionalException(Message.USER_ID_NULL);
} }
// Check if user exists
userRepository.findById(userId) userRepository.findById(userId)
.orElseThrow(() -> new FunctionalException(Message.USER_NOT_FOUND + userId)); .orElseThrow(() -> new FunctionalException(Message.USER_NOT_FOUND + userId));
...@@ -75,19 +67,23 @@ public class SessionServiceImpl implements SessionService { ...@@ -75,19 +67,23 @@ public class SessionServiceImpl implements SessionService {
} }
@Override @Override
public SessionDTO getCurrentSession(Long userId) throws FunctionalException { public List<SessionDTO> getCurrentSessions(Long userId) throws FunctionalException {
if (userId == null) { if (userId == null) {
throw new FunctionalException(Message.USER_ID_NULL); throw new FunctionalException(Message.USER_ID_NULL);
} }
// Check if user exists
userRepository.findById(userId) userRepository.findById(userId)
.orElseThrow(() -> new FunctionalException(Message.USER_NOT_FOUND + userId)); .orElseThrow(() -> new FunctionalException(Message.USER_NOT_FOUND + userId));
Session currentSession = sessionRepository.findByUserIdAndIsCurrentTrue(userId) List<Session> currentSessions = sessionRepository.findByUserIdAndIsCurrentTrue(userId);
.orElseThrow(() -> new FunctionalException(Message.SESSION_NO_CURRENT_SESSION + userId));
if (currentSessions.isEmpty()) {
throw new FunctionalException(Message.SESSION_NO_CURRENT_SESSION + userId);
}
return SessionMapper.toDTO(currentSession); return currentSessions.stream()
.map(SessionMapper::toDTO)
.toList();
} }
@Override @Override
......
0% ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter