-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationDispatchQueryRepositoryAdapter.java
More file actions
103 lines (90 loc) · 3.68 KB
/
NotificationDispatchQueryRepositoryAdapter.java
File metadata and controls
103 lines (90 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package me.pinitnotification.infrastructure.persistence.notification;
import me.pinitnotification.application.notification.NotificationDispatchItem;
import me.pinitnotification.application.notification.NotificationDispatchQueryRepository;
import me.pinitnotification.domain.notification.UpcomingScheduleNotification;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.stereotype.Repository;
import java.time.Instant;
import java.util.*;
@Repository
public class NotificationDispatchQueryRepositoryAdapter implements NotificationDispatchQueryRepository {
private static final String FIND_DUE_WITH_TOKENS_SQL = """
SELECT n.public_id,
n.owner_id,
n.schedule_id,
n.schedule_title,
n.schedule_start_time,
n.idempotent_key,
ps.token
FROM upcoming_schedule_notification n
LEFT JOIN push_subscription ps ON ps.member_id = n.owner_id
WHERE n.schedule_start_time IS NOT NULL
AND n.schedule_start_time <= ?
""";
private final JdbcClient jdbcClient;
public NotificationDispatchQueryRepositoryAdapter(JdbcClient jdbcClient) {
this.jdbcClient = jdbcClient;
}
@Override
public List<NotificationDispatchItem> findAllDueNotificationsWithTokens(Instant now) {
List<DispatchRow> rows = jdbcClient.sql(FIND_DUE_WITH_TOKENS_SQL)
.param(now.toString())
.query((rs, rowNum) -> new DispatchRow(
UUID.fromString(rs.getString("public_id")),
rs.getLong("owner_id"),
rs.getLong("schedule_id"),
rs.getString("schedule_title"),
rs.getString("schedule_start_time"),
rs.getString("idempotent_key"),
rs.getString("token")
))
.list();
if (rows.isEmpty()) {
return List.of();
}
Map<UUID, DispatchAccumulator> aggregated = new LinkedHashMap<>();
for (DispatchRow row : rows) {
DispatchAccumulator accumulator = aggregated.computeIfAbsent(
row.notificationId,
id -> new DispatchAccumulator(
toDomain(row),
new ArrayList<>()
)
);
if (row.token != null) {
accumulator.tokens.add(row.token);
}
}
return aggregated.values().stream()
.map(accumulator -> new NotificationDispatchItem(accumulator.notification, List.copyOf(accumulator.tokens)))
.toList();
}
private UpcomingScheduleNotification toDomain(DispatchRow row) {
return new UpcomingScheduleNotification(
row.notificationId,
row.ownerId,
row.scheduleId,
row.scheduleTitle,
row.scheduleStartTime,
row.idempotentKey
);
}
private record DispatchRow(
UUID notificationId,
Long ownerId,
Long scheduleId,
String scheduleTitle,
String scheduleStartTime,
String idempotentKey,
String token
) {
}
private static class DispatchAccumulator {
private final UpcomingScheduleNotification notification;
private final List<String> tokens;
private DispatchAccumulator(UpcomingScheduleNotification notification, List<String> tokens) {
this.notification = notification;
this.tokens = tokens;
}
}
}