Browse Source

Don't delete notifications when recipient is deleted

pull/9353/head
ViacheslavKlimov 3 years ago
parent
commit
c0294e3878
  1. 1
      application/src/main/java/org/thingsboard/server/service/ttl/AlarmsCleanUpService.java
  2. 4
      application/src/main/java/org/thingsboard/server/service/ttl/NotificationsCleanUpService.java
  3. 28
      application/src/test/java/org/thingsboard/server/service/notification/NotificationApiTest.java
  4. 1
      dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java
  5. 2
      dao/src/main/java/org/thingsboard/server/dao/sqlts/insert/sql/SqlPartitioningRepository.java
  6. 3
      dao/src/main/java/org/thingsboard/server/dao/user/UserServiceImpl.java

1
application/src/main/java/org/thingsboard/server/service/ttl/AlarmsCleanUpService.java

@ -92,7 +92,6 @@ public class AlarmsCleanUpService {
while (true) {
PageData<AlarmId> toRemove = alarmDao.findAlarmsIdsByEndTsBeforeAndTenantId(expirationTime, tenantId, removalBatchRequest);
for (AlarmId alarmId : toRemove.getData()) {
relationService.deleteEntityRelations(tenantId, alarmId);
Alarm alarm = alarmService.delAlarm(tenantId, alarmId, false).getAlarm();
if (alarm != null) {
entityActionService.pushEntityActionToRuleEngine(alarm.getOriginator(), alarm, tenantId, null, ActionType.ALARM_DELETE, null);

4
application/src/main/java/org/thingsboard/server/service/ttl/NotificationsCleanUpService.java

@ -63,8 +63,8 @@ public class NotificationsCleanUpService extends AbstractCleanUpService {
if (lastRemovedNotificationTs > 0) {
long gap = TimeUnit.MINUTES.toMillis(10);
long requestExpTime = lastRemovedNotificationTs - TimeUnit.SECONDS.toMillis(NotificationRequestConfig.MAX_SENDING_DELAY) - gap;
// TODO: double-check this
notificationRequestDao.removeAllByCreatedTimeBefore(requestExpTime);
int removed = notificationRequestDao.removeAllByCreatedTimeBefore(requestExpTime);
log.info("Removed {} outdated notification requests older than {}", removed, requestExpTime);
}
}

28
application/src/test/java/org/thingsboard/server/service/notification/NotificationApiTest.java

@ -90,7 +90,6 @@ import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@DaoSqlTest
@Slf4j
@ -285,7 +284,7 @@ public class NotificationApiTest extends AbstractNotificationApiTest {
}
@Test
public void whenTenantIsDeleted_thenDeleteNotifications() throws Exception {
public void whenTenantIsDeleted_thenDeleteNotificationRequests() throws Exception {
createDifferentTenant();
NotificationTarget target = createNotificationTarget(savedDifferentTenantUser.getId());
int notificationsCount = 20;
@ -295,36 +294,11 @@ public class NotificationApiTest extends AbstractNotificationApiTest {
}
List<NotificationRequest> requests = notificationRequestService.findNotificationRequestsByTenantIdAndOriginatorType(differentTenantId, EntityType.USER, new PageLink(100)).getData();
assertThat(requests).size().isEqualTo(notificationsCount);
for (NotificationRequest request : requests) {
List<Notification> notifications = notificationDao.findByRequestId(differentTenantId, request.getId(), new PageLink(100)).getData();
assertThat(notifications).size().isNotZero();
}
deleteDifferentTenant();
assertThat(notificationRequestService.findNotificationRequestsByTenantIdAndOriginatorType(differentTenantId, EntityType.USER, new PageLink(1)).getTotalElements())
.isZero();
for (NotificationRequest request : requests) {
assertThat(notificationDao.findByRequestId(differentTenantId, request.getId(), new PageLink(100)).getTotalElements())
.isZero();
}
}
@Test
public void whenUserIsDeleted_thenDeleteNotifications() throws Exception {
NotificationTarget target = createNotificationTarget(customerUserId);
int notificationsCount = 20;
for (int i = 0; i < notificationsCount; i++) {
NotificationRequest request = submitNotificationRequest(target.getId(), "Test " + i, NotificationDeliveryMethod.WEB);
awaitNotificationRequest(request.getId());
}
List<Notification> notifications = notificationDao.findByRecipientIdAndPageLink(tenantId, customerUserId, new PageLink(100)).getData();
assertThat(notifications).size().isGreaterThanOrEqualTo(notificationsCount);
doDelete("/api/user/" + customerUserId).andExpect(status().isOk());
notifications = notificationDao.findByRecipientIdAndPageLink(tenantId, customerUserId, new PageLink(100)).getData();
assertThat(notifications).isEmpty();
}
@Test

1
dao/src/main/java/org/thingsboard/server/dao/notification/DefaultNotificationRequestService.java

@ -98,6 +98,7 @@ public class DefaultNotificationRequestService implements NotificationRequestSer
notificationRequestDao.updateById(tenantId, requestId, requestStatus, stats);
}
// notifications themselves are left in the database until removed by ttl
@Override
public void deleteNotificationRequestsByTenantId(TenantId tenantId) {
notificationRequestDao.removeByTenantId(tenantId);

2
dao/src/main/java/org/thingsboard/server/dao/sqlts/insert/sql/SqlPartitioningRepository.java

@ -140,7 +140,7 @@ public class SqlPartitioningRepository {
try {
partitions.add(Long.parseLong(partitionTsStr));
} catch (NumberFormatException nfe) {
log.warn("Failed to parse table name: {}", partitionTableName);
log.debug("Failed to parse table name: {}", partitionTableName);
}
}
return partitions;

3
dao/src/main/java/org/thingsboard/server/dao/user/UserServiceImpl.java

@ -49,7 +49,6 @@ import org.thingsboard.server.dao.eventsourcing.ActionEntityEvent;
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent;
import org.thingsboard.server.dao.exception.IncorrectParameterException;
import org.thingsboard.server.dao.notification.NotificationDao;
import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover;
@ -86,7 +85,6 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
private final UserDao userDao;
private final UserCredentialsDao userCredentialsDao;
private final UserAuthSettingsDao userAuthSettingsDao;
private final NotificationDao notificationDao;
private final DataValidator<User> userValidator;
private final DataValidator<UserCredentials> userCredentialsValidator;
private final ApplicationEventPublisher eventPublisher;
@ -257,7 +255,6 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
UserCredentials userCredentials = userCredentialsDao.findByUserId(tenantId, userId.getId());
userCredentialsDao.removeById(tenantId, userCredentials.getUuidId());
userAuthSettingsDao.removeByUserId(userId);
notificationDao.deleteByRecipientId(tenantId, userId);
deleteEntityRelations(tenantId, userId);
userDao.removeById(tenantId, userId.getId());
eventPublisher.publishEvent(new UserCredentialsInvalidationEvent(userId));

Loading…
Cancel
Save