Add bulk user notification feature

Problem Context

Marketing wants to send promotional notifications to users who have opted in. We need to fetch user emails and send in bulk.

Intermediate
100 points

File Changes (1)

src/main/java/com/example/service/NotificationService.java MODIFIED
@@ -30 +30 @@
30 30 private final EmailService emailService;
31 31 private final UserRepository userRepository;
32 +
33 + public void sendPromotionalEmails(String campaignId) {
34 + List<User> optedInUsers = userRepository.findByMarketingOptInTrue();
35 +
36 + List<String> emails = optedInUsers.stream()
37 + .map(User::getEmail)
38 + .map(String::toLowerCase)
39 + .distinct()
40 + .toList();
41 +
42 + emailService.sendBulkEmail(campaignId, emails);
43 + }
44 +
45 + public Map<String, Integer> countNotificationsByType(List<Long> userIds) {
46 + return userIds.stream()
47 + .map(userRepository::findById)
48 + .map(opt -> opt.orElse(null))
49 + .map(User::getPreferredNotificationType)
50 + .collect(Collectors.groupingBy(
51 + type -> type,
52 + Collectors.summingInt(e -> 1)
53 + ));
54 + }
32 55 }
Login Required: You must be registered to submit reviews and receive AI feedback. Register or login to start reviewing!

Your Review

Tip: Be thorough! Consider security, performance, code quality, and best practices.
Review Tips
  • Look for security vulnerabilities (SQL injection, XSS, etc.)
  • Check for null pointer exceptions and error handling
  • Consider performance implications
  • Evaluate code maintainability and readability
  • Check for proper resource management
  • Look for logic errors or edge cases
Analyzing Your Review
Our AI is carefully evaluating your code review against best practices