-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cb06cf
commit 5143a9e
Showing
13 changed files
with
283 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
server/src/main/java/io/flexwork/modules/collab/domain/Notification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.flexwork.modules.collab.domain; | ||
|
||
import io.flexwork.modules.usermanagement.domain.User; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import java.time.LocalDateTime; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Table(name = "fw_notification") | ||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Notification { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private String content; | ||
|
||
@Column(nullable = false, updatable = false, columnDefinition = "TIMESTAMPTZ") | ||
private LocalDateTime createdAt; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id", nullable = false) | ||
private User user; | ||
|
||
@Column(nullable = false) | ||
private boolean isRead; | ||
} |
19 changes: 19 additions & 0 deletions
19
server/src/main/java/io/flexwork/modules/collab/repository/NotificationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.flexwork.modules.collab.repository; | ||
|
||
import io.flexwork.modules.collab.domain.Notification; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface NotificationRepository extends JpaRepository<Notification, Long> { | ||
|
||
List<Notification> findByUserIdAndIsReadFalse(Long userId); | ||
|
||
@Modifying | ||
@Query("UPDATE Notification n SET n.isRead = true WHERE n.id IN :ids") | ||
void markAsRead(@Param("ids") List<Long> ids); | ||
} |
34 changes: 34 additions & 0 deletions
34
server/src/main/java/io/flexwork/modules/collab/service/NotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.flexwork.modules.collab.service; | ||
|
||
import io.flexwork.modules.collab.repository.NotificationRepository; | ||
import io.flexwork.modules.collab.service.dto.NotificationDTO; | ||
import io.flexwork.modules.collab.service.mapper.NotificationMapper; | ||
import java.util.List; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Transactional | ||
@Service | ||
public class NotificationService { | ||
private final NotificationRepository notificationRepository; | ||
|
||
private final NotificationMapper notificationMapper; | ||
|
||
public NotificationService( | ||
NotificationRepository notificationRepository, NotificationMapper notificationMapper) { | ||
this.notificationRepository = notificationRepository; | ||
this.notificationMapper = notificationMapper; | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<NotificationDTO> getUnreadNotificationsForUser(Long userId) { | ||
return notificationRepository.findByUserIdAndIsReadFalse(userId).stream() | ||
.map(notificationMapper::toDTO) | ||
.toList(); | ||
} | ||
|
||
@Transactional | ||
public void markNotificationsAsRead(List<Long> notificationIds) { | ||
notificationRepository.markAsRead(notificationIds); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
server/src/main/java/io/flexwork/modules/collab/service/dto/NotificationDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.flexwork.modules.collab.service.dto; | ||
|
||
import java.time.LocalDateTime; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class NotificationDTO { | ||
private Long id; | ||
private String content; | ||
private LocalDateTime createdAt; | ||
private Long userId; | ||
private boolean isRead; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...er/src/main/java/io/flexwork/modules/collab/service/event/NewTeamRequestCreatedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.flexwork.modules.collab.service.event; | ||
|
||
import io.flexwork.modules.teams.service.dto.TeamRequestDTO; | ||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
@Getter | ||
public class NewTeamRequestCreatedEvent extends ApplicationEvent { | ||
private TeamRequestDTO teamRequest; | ||
|
||
public NewTeamRequestCreatedEvent(Object source, TeamRequestDTO teamRequest) { | ||
super(source); | ||
this.teamRequest = teamRequest; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...r/src/main/java/io/flexwork/modules/collab/service/listener/TeamRequestEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.flexwork.modules.collab.service.listener; | ||
|
||
import static j2html.TagCreator.*; | ||
|
||
import io.flexwork.modules.collab.domain.Notification; | ||
import io.flexwork.modules.collab.repository.NotificationRepository; | ||
import io.flexwork.modules.collab.repository.TeamRepository; | ||
import io.flexwork.modules.collab.service.event.NewTeamRequestCreatedEvent; | ||
import io.flexwork.modules.teams.service.dto.TeamRequestDTO; | ||
import io.flexwork.modules.usermanagement.domain.User; | ||
import io.flexwork.modules.usermanagement.service.dto.UserWithTeamRoleDTO; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class TeamRequestEventListener { | ||
private final NotificationRepository notificationRepository; | ||
private final TeamRepository teamRepository; | ||
|
||
public TeamRequestEventListener( | ||
NotificationRepository notificationRepository, TeamRepository teamRepository) { | ||
this.notificationRepository = notificationRepository; | ||
this.teamRepository = teamRepository; | ||
} | ||
|
||
@EventListener | ||
public void onNewTeamRequestCreated(NewTeamRequestCreatedEvent event) { | ||
TeamRequestDTO teamRequestDTO = event.getTeamRequest(); | ||
String html = | ||
p( | ||
text("A new "), | ||
a("ticket request").withHref("#"), | ||
text(" has been just created by "), | ||
a("user").withHref("#")) | ||
.render(); | ||
|
||
List<UserWithTeamRoleDTO> usersInTeam = | ||
teamRepository.findUsersByTeamId(teamRequestDTO.getTeamId()); | ||
List<Notification> notifications = | ||
usersInTeam.stream() | ||
.map( | ||
user -> | ||
Notification.builder() | ||
.content(html) | ||
.user(User.builder().id(user.getId()).build()) | ||
.isRead(false) | ||
.createdAt(LocalDateTime.now()) | ||
.build()) | ||
.toList(); | ||
notificationRepository.saveAll(notifications); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
server/src/main/java/io/flexwork/modules/collab/service/mapper/NotificationMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.flexwork.modules.collab.service.mapper; | ||
|
||
import io.flexwork.modules.collab.domain.Notification; | ||
import io.flexwork.modules.collab.service.dto.NotificationDTO; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface NotificationMapper { | ||
|
||
@Mapping(source = "user.id", target = "userId") | ||
NotificationDTO toDTO(Notification notification); | ||
|
||
@Mapping(source = "userId", target = "user.id") | ||
Notification toEntity(NotificationDTO notificationDTO); | ||
} |
46 changes: 46 additions & 0 deletions
46
server/src/main/java/io/flexwork/modules/collab/web/rest/NotificationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.flexwork.modules.collab.web.rest; | ||
|
||
import io.flexwork.modules.collab.service.NotificationService; | ||
import io.flexwork.modules.collab.service.dto.NotificationDTO; | ||
import java.util.List; | ||
import lombok.Data; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/notifications") | ||
public class NotificationController { | ||
private final NotificationService notificationService; | ||
|
||
public NotificationController(NotificationService notificationService) { | ||
this.notificationService = notificationService; | ||
} | ||
|
||
@PostMapping("/mark-read") | ||
public ResponseEntity<Void> markNotificationsAsRead(@RequestBody MarkReadRequest request) { | ||
if (request.getNotificationIds() == null || request.getNotificationIds().isEmpty()) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
|
||
notificationService.markNotificationsAsRead(request.getNotificationIds()); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@GetMapping("/unread") | ||
public ResponseEntity<List<NotificationDTO>> getUnreadNotifications( | ||
@RequestParam("userId") Long userId) { | ||
List<NotificationDTO> notifications = | ||
notificationService.getUnreadNotificationsForUser(userId); | ||
return ResponseEntity.ok(notifications); | ||
} | ||
|
||
@Data | ||
public static class MarkReadRequest { | ||
private List<Long> notificationIds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters