Skip to content

Commit

Permalink
PP-11205 Add endpoint to expunge and archive historical data (#2233)
Browse files Browse the repository at this point in the history
* PP-11205 Add endpoint to expunge and archive historical data

- Added endpoint to expunge and archive historical data
  • Loading branch information
kbottla authored Nov 8, 2023
1 parent 3f62914 commit 173bd6f
Show file tree
Hide file tree
Showing 14 changed files with 368 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"filename": "openapi/adminusers_spec.yaml",
"hashed_secret": "3660e32cde5fccc8d1e4521d0c831c2012388720",
"is_verified": false,
"line_number": 1218
"line_number": 1231
}
],
"src/main/java/uk/gov/pay/adminusers/model/CreateUserRequest.java": [
Expand Down Expand Up @@ -466,5 +466,5 @@
}
]
},
"generated_at": "2023-10-26T11:32:22Z"
"generated_at": "2023-11-08T10:45:43Z"
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The [API Specification](/openapi/adminusers_spec.yaml) provides more detail on t
| `SUPPORT_URL` | The URL users can visit to get support. Defaults to `https://frontend.pymnt.localdomain/contact/`. |
| `EXPUNGE_AND_ARCHIVE_HISTORICAL_DATA_ENABLED` | Set to `true` to enable expunging (user related data) and archiving (services) historical data. Default is `false`. |
| `EXPUNGE_USER_DATA_AFTER_DAYS` | Number of days after which users (not attached to any service) not logged in or created but never logged in deleted. Default is 4 years. |
| `EXPUNGE_ARCHIVE_SERVICES_AFTER_DAYS` | Number of days after which services without transactions archived. Default is 7 years. |
-----------------------------------------------------------------------------------------------------------

## Maven profiles
Expand Down
13 changes: 13 additions & 0 deletions openapi/adminusers_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,19 @@ paths:
summary: Update user's role for a service
tags:
- Users
/v1/tasks/expunge-and-archive-historical-data:
post:
operationId: expungeAndArchiveHistoricalData
responses:
"200":
description: OK
summary: "Deletes and archives historical data based on `expungeAndArchiveDataConfig`.\
\ Currently, 1. deletes historical users not attached to any service 2.\
\ deletes historical invites 3. deletes forgotten passwords 4. archives services\
\ without transactions within the configured number of days and detaches users\
\ from service"
tags:
- Tasks
/v2/api/invites/otp/validate:
post:
operationId: validateOtpKey
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/uk/gov/pay/adminusers/app/AdminUsersApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import uk.gov.pay.adminusers.exception.ConflictExceptionMapper;
import uk.gov.pay.adminusers.exception.NotFoundExceptionMapper;
import uk.gov.pay.adminusers.exception.ValidationExceptionMapper;
import uk.gov.pay.adminusers.expungeandarchive.resource.ExpungeAndArchiveHistoricalDataResource;
import uk.gov.pay.adminusers.filters.LoggingMDCRequestFilter;
import uk.gov.pay.adminusers.filters.LoggingMDCResponseFilter;
import uk.gov.pay.adminusers.queue.managed.EventSubscriberQueueMessageReceiver;
Expand All @@ -42,7 +43,6 @@
import uk.gov.service.payments.logging.LogstashConsoleAppenderFactory;
import uk.gov.service.payments.logging.SentryAppenderFactory;

import java.net.URI;
import java.util.concurrent.TimeUnit;

import static java.util.EnumSet.of;
Expand Down Expand Up @@ -99,6 +99,7 @@ public void run(AdminUsersConfig configuration, Environment environment) {
environment.jersey().register(injector.getInstance(ResetPasswordResource.class));
environment.jersey().register(injector.getInstance(HealthCheckResource.class));
environment.jersey().register(injector.getInstance(EmailResource.class));
environment.jersey().register(injector.getInstance(ExpungeAndArchiveHistoricalDataResource.class));

// Register the custom ExceptionMapper(s)
environment.jersey().register(new ValidationExceptionMapper());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import uk.gov.service.payments.commons.api.json.ApiResponseDateTimeDeserializer;
import uk.gov.service.payments.commons.api.json.ApiResponseDateTimeSerializer;

import java.time.ZonedDateTime;

Expand All @@ -16,6 +18,7 @@ public class LedgerTransaction {
private String reference;

@JsonDeserialize(using = ApiResponseDateTimeDeserializer.class)
@JsonSerialize(using = ApiResponseDateTimeSerializer.class)
private ZonedDateTime createdDate;

public LedgerTransaction() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package uk.gov.pay.adminusers.expungeandarchive.resource;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.inject.Inject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.MDC;
import uk.gov.pay.adminusers.expungeandarchive.service.ExpungeAndArchiveHistoricalDataService;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import java.util.UUID;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static uk.gov.service.payments.logging.LoggingKeys.MDC_REQUEST_ID_KEY;

@Path("/v1/tasks")
@Tag(name = "Tasks")
public class ExpungeAndArchiveHistoricalDataResource {

private final ExpungeAndArchiveHistoricalDataService expungeAndArchiveHistoricalDataService;

@Inject
public ExpungeAndArchiveHistoricalDataResource(ExpungeAndArchiveHistoricalDataService expungeAndArchiveHistoricalDataService) {
this.expungeAndArchiveHistoricalDataService = expungeAndArchiveHistoricalDataService;
}

@POST
@Produces(APPLICATION_JSON)
@Operation(
summary = "Deletes and archives historical data based on `expungeAndArchiveDataConfig`. " +
"Currently, " +
" 1. deletes historical users not attached to any service" +
" 2. deletes historical invites" +
" 3. deletes forgotten passwords" +
" 4. archives services without transactions within the configured number of days and detaches users from service",
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
}
)
@Path("/expunge-and-archive-historical-data")
public Response expungeAndArchiveHistoricalData() {
String correlationId = MDC.get(MDC_REQUEST_ID_KEY) == null ? "ExpungeAndArchiveHistoricalDataResource-" + UUID.randomUUID() : MDC.get(MDC_REQUEST_ID_KEY);
MDC.put(MDC_REQUEST_ID_KEY, correlationId);

expungeAndArchiveHistoricalDataService.expungeAndArchiveHistoricalData();

MDC.remove(MDC_REQUEST_ID_KEY);
return Response.ok().build();
}
}
3 changes: 3 additions & 0 deletions src/main/java/uk/gov/pay/adminusers/model/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,7 @@ public ZonedDateTime getCreatedDate() {
public ZonedDateTime getWentLiveDate() {
return wentLiveDate;
}
public void setCreatedDate(ZonedDateTime createdDate) {
this.createdDate = createdDate;
}
}
1 change: 1 addition & 0 deletions src/main/resources/openapi-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ resourceClasses:
- uk.gov.pay.adminusers.resources.ServiceResource
- uk.gov.pay.adminusers.resources.UserResource
- uk.gov.pay.adminusers.resources.ToolboxEndpointResource
- uk.gov.pay.adminusers.expungeandarchive.resource.ExpungeAndArchiveHistoricalDataResource
readAllResources: true
sortOutput: true
prettyPrint: true
Expand Down
Loading

0 comments on commit 173bd6f

Please sign in to comment.