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 @@ -22,7 +22,7 @@
@Table(
name = "notification_device_token",
uniqueConstraints = {
@UniqueConstraint(name = "uq_notification_device_token_token", columnNames = "token")
@UniqueConstraint(name = "uq_notification_device_token_user_id", columnNames = "user_id")
}
)
@NoArgsConstructor(access = PROTECTED)
Expand All @@ -37,7 +37,7 @@ public class NotificationDeviceToken extends BaseEntity {
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Column(name = "token", length = 255, nullable = false, unique = true)
@Column(name = "token", length = 255, nullable = false)
private String token;

private NotificationDeviceToken(Integer id, User user, String token) {
Expand All @@ -50,7 +50,7 @@ public static NotificationDeviceToken of(User user, String token) {
return new NotificationDeviceToken(null, user, token);
}

public void updateUser(User user) {
this.user = user;
public void updateToken(String token) {
this.token = token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public interface NotificationDeviceTokenRepository extends Repository<NotificationDeviceToken, Integer> {

Optional<NotificationDeviceToken> findByToken(String token);
Optional<NotificationDeviceToken> findByUserId(Integer userId);

@Query("""
SELECT ndt
Expand All @@ -31,13 +31,6 @@ Optional<NotificationDeviceToken> findByUserIdAndToken(
""")
List<String> findTokensByUserId(@Param("userId") Integer userId);

@Query("""
SELECT ndt.user.id
FROM NotificationDeviceToken ndt
WHERE ndt.token = :token
""")
Optional<Integer> findUserIdByToken(@Param("token") String token);

void save(NotificationDeviceToken notificationDeviceToken);

void delete(NotificationDeviceToken notificationDeviceToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package gg.agit.konect.domain.notification.service;

import static gg.agit.konect.global.code.ApiResponseCode.DUPLICATE_NOTIFICATION_TOKEN;
import static gg.agit.konect.global.code.ApiResponseCode.INVALID_NOTIFICATION_TOKEN;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -68,32 +66,11 @@ public void registerToken(Integer userId, NotificationTokenRegisterRequest reque
String token = request.token();
validateExpoToken(token);

Integer existingOwnerId = notificationDeviceTokenRepository.findUserIdByToken(token)
.orElse(null);
if (existingOwnerId != null) {
if (!existingOwnerId.equals(userId)) {
throw CustomException.of(DUPLICATE_NOTIFICATION_TOKEN);
}
return;
}

try {
notificationDeviceTokenRepository.save(NotificationDeviceToken.of(user, token));
} catch (DataIntegrityViolationException e) {
Integer ownerId = notificationDeviceTokenRepository.findUserIdByToken(token)
.orElse(null);
if (ownerId == null) {
log.warn(
"Token uniqueness violation without owner: userId={}, token={}",
userId,
token
);
throw e;
}
if (!ownerId.equals(userId)) {
throw CustomException.of(DUPLICATE_NOTIFICATION_TOKEN);
}
}
notificationDeviceTokenRepository.findByUserId(userId)
.ifPresentOrElse(
notificationDeviceToken -> notificationDeviceToken.updateToken(token),
() -> notificationDeviceTokenRepository.save(NotificationDeviceToken.of(user, token))
);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DELETE ndt1
FROM notification_device_token ndt1
INNER JOIN notification_device_token ndt2
ON ndt1.user_id = ndt2.user_id
AND (
ndt1.updated_at < ndt2.updated_at
OR (ndt1.updated_at = ndt2.updated_at AND ndt1.id < ndt2.id)
);

ALTER TABLE notification_device_token
DROP INDEX uq_notification_device_token_token,
ADD CONSTRAINT uq_notification_device_token_user_id UNIQUE (user_id);