Skip to content

Commit

Permalink
Update status of existing registrations in case of a deleted activation
Browse files Browse the repository at this point in the history
  • Loading branch information
romanstrobl committed Dec 12, 2024
1 parent 4d0ee02 commit ea41ab2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import com.wultra.security.powerauth.client.model.enumeration.ActivationStatus;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;
import lombok.*;

/**
* Class representing request object responsible for updating activation status.
Expand All @@ -30,6 +29,9 @@
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class UpdateDeviceStatusRequest {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void createOrUpdateDevice(final CreateDeviceRequest requestObject, final
logger.info("Creating new device registration: app ID: {}, activation ID: {}, platform: {}", requestObject.getAppId(), requestObject.getActivationId(), platform);
device = initDeviceRegistrationEntity(appCredentials, pushToken);
} else {
// An existing row was found by one of the lookup methods, update this row. This means that either:
// An existing row was found by one of the lookup methods. This means that either:
// 1. A row with same activation ID and push token is updated, in this case only the last registration timestamp changes.
// 2. A row with same activation ID but different push token is updated. A new push token has been issued by Google or Apple for an activation.
// 3. A row with different activation ID but same push token is created. The user removed an activation and created a new one, the push token remains the same,
Expand All @@ -89,6 +89,10 @@ public void createOrUpdateDevice(final CreateDeviceRequest requestObject, final
logger.info("Creating new device registration for a new activation: app ID: {}, activation ID: {}, platform: {}", requestObject.getAppId(), requestObject.getActivationId(), platform);
// Create a new registration record for a new activation ID
device = initDeviceRegistrationEntity(appCredentials, pushToken);
// Make sure that status of existing device registrations is updated in case of a deleted activation
for (PushDeviceRegistrationEntity deviceRegistration: devices) {
updateStatus(deviceRegistration.getActivationId(), null);
}
}
}
device.setTimestampLastRegistered(new Date());
Expand Down Expand Up @@ -152,12 +156,10 @@ public void createOrUpdateDevices(final CreateDeviceForActivationsRequest reques
}

@Transactional
public void updateStatus(final UpdateDeviceStatusRequest request) throws PushServerException {
final String activationId = request.getActivationId();

public void updateStatus(final String activationId, final ActivationStatus status) throws PushServerException {
final List<PushDeviceRegistrationEntity> device = pushDeviceRepository.findByActivationId(activationId);

final ActivationStatus activationStatus = request.getActivationStatus() == null ? fetchActivationStatus(activationId) : request.getActivationStatus();
final ActivationStatus activationStatus = status == null ? fetchActivationStatus(activationId) : status;

for (PushDeviceRegistrationEntity registration : device) {
registration.setActive(activationStatus == ActivationStatus.ACTIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Response updateDeviceStatus(final UpdateDeviceStatusRequest request) thro
throw new PushServerException(errorMessage);
}

deviceRegistrationService.updateStatus(request);
deviceRegistrationService.updateStatus(request.getActivationId(), request.getActivationStatus());
logger.info("The updateDeviceStatus request succeeded, activation ID: {}", request.getActivationId());
return new Response();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void testUpdateStatus_statusInRequest() throws Exception {
request.setActivationId("a1");
request.setActivationStatus(ActivationStatus.ACTIVE);

tested.updateStatus(request);
tested.updateStatus(request.getActivationId(), request.getActivationStatus());

final List<PushDeviceRegistrationEntity> entities2 = deviceRepository.findByActivationId("a1");
assertEquals(1, entities2.size());
Expand Down Expand Up @@ -243,7 +243,7 @@ void testUpdateStatus_missingStatusInRequest() throws Exception {
final UpdateDeviceStatusRequest request = new UpdateDeviceStatusRequest();
request.setActivationId("a1");

tested.updateStatus(request);
tested.updateStatus(request.getActivationId(), request.getActivationStatus());
verify(powerAuthClient).getActivationStatus("a1");

final List<PushDeviceRegistrationEntity> entities2 = deviceRepository.findByActivationId("a1");
Expand Down

0 comments on commit ea41ab2

Please sign in to comment.