Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added API to delete a preset [SCS-219] #9

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/main/java/it/pagopa/swclient/mil/preset/ErrorCode.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public final class ErrorCode {
public static final String NOTICE_NUMBER_MUST_NOT_BE_NULL = MODULE_ID + "00000D";
public static final String NOTICE_NUMBER_MUST_MATCH_REGEXP = MODULE_ID + "00000E";


public static final String PRESET_ID_MUST_NOT_BE_NULL = MODULE_ID + "000017";
// business logic errors
public static final String SUBSCRIBER_NOT_FOUND = MODULE_ID + "00000F";
public static final String SUBSCRIBER_ALREADY_EXISTS = MODULE_ID + "000010";
Expand All @@ -39,6 +41,7 @@ public final class ErrorCode {

public static final String ERROR_UNAUTHORIZED = MODULE_ID + "000014";
public static final String ERROR_FORBIDDEN = MODULE_ID + "000015";
public static final String ERROR_DELETING_DATA_FROM_DB = MODULE_ID + "000016";


private ErrorCode() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package it.pagopa.swclient.mil.preset.bean;

import it.pagopa.swclient.mil.preset.ErrorCode;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.PathParam;

public class PresetPathParams {


/*
* ID assigned to subscribed terminal
*/
@PathParam(value = "presetId")
@NotNull(message = "[" + ErrorCode.PRESET_ID_MUST_NOT_BE_NULL + "] presetId must not be null")
private String presetId;

/**
* @return the presetId
*/
public String getPresetId() {
return presetId;
}

/**
* @param presetId the presetId to set
*/
public void setPresetId(String presetId) {
this.presetId = presetId;
}


@Override
public String toString() {
return "PresetPathParam [presetId=" +
presetId +
"]";
}
}
77 changes: 52 additions & 25 deletions src/main/java/it/pagopa/swclient/mil/preset/resource/PresetsResource.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package it.pagopa.swclient.mil.preset.resource;

import com.oracle.svm.core.annotate.Delete;
import io.quarkus.logging.Log;
import io.quarkus.panache.common.Parameters;
import io.quarkus.panache.common.Sort;
import io.quarkus.vertx.web.Param;
import io.smallrye.mutiny.Uni;
import it.pagopa.swclient.mil.bean.CommonHeader;
import it.pagopa.swclient.mil.bean.Errors;
import it.pagopa.swclient.mil.preset.ErrorCode;
import it.pagopa.swclient.mil.preset.OperationType;
import it.pagopa.swclient.mil.preset.PresetStatus;
import it.pagopa.swclient.mil.preset.bean.CreatePresetRequest;
import it.pagopa.swclient.mil.preset.bean.GetPresetsResponse;
import it.pagopa.swclient.mil.preset.bean.InstitutionPortalHeaders;
import it.pagopa.swclient.mil.preset.bean.PresetOperation;
import it.pagopa.swclient.mil.preset.bean.Subscriber;
import it.pagopa.swclient.mil.preset.bean.SubscriberPathParams;
import it.pagopa.swclient.mil.preset.bean.*;
import it.pagopa.swclient.mil.preset.dao.PresetEntity;
import it.pagopa.swclient.mil.preset.dao.PresetRepository;
import it.pagopa.swclient.mil.preset.dao.SubscriberEntity;
Expand All @@ -24,17 +21,12 @@
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.BeanParam;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.InternalServerErrorException;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import java.net.URI;
Expand All @@ -44,8 +36,8 @@
@Path("/presets")
public class PresetsResource {

public static final String SUBSCRIBER_FILTER = "subscriber.paTaxCode = :paTaxCode and subscriber.subscriberId = :subscriberId";
public static final String PRESET_FILTER = "presetOperation.paTaxCode = :paTaxCode and presetOperation.subscriberId = :subscriberId";
public static final String SUBSCRIBER_FILTER = "subscriber.paTaxCode = :paTaxCode and subscriber.subscriberId = :subscriberId";
public static final String PRESET_FILTER = "presetOperation.paTaxCode = :paTaxCode and presetOperation.subscriberId = :subscriberId";

public static final String LAST_PRESET_FILTER = PRESET_FILTER;
public static final String PA_TAX_CODE = "paTaxCode";
Expand All @@ -58,7 +50,7 @@ public class PresetsResource {
String presetLocationBaseURL;


@Inject
@Inject
PresetRepository presetRepository;

@Inject
Expand Down Expand Up @@ -107,6 +99,39 @@ public Uni<Response> createPreset(@Valid @BeanParam InstitutionPortalHeaders por
});
}

@DELETE
@RolesAllowed({ "InstitutionPortal" })
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path(value = "/{presetId}")
public Uni<Response> deletePreset(@Valid @BeanParam InstitutionPortalHeaders portalHeaders,
@Valid
@NotNull(message = "[" + ErrorCode.CREATE_PRESET_REQUEST_MUST_NOT_BE_EMPTY + "] request must not be empty")
PresetPathParams pathParams) {


return presetRepository.delete("presetOperation.presetId", pathParams.getPresetId()
)
.onFailure().transform(err -> {
Log.debugf("[%s] Error while deleting data from DB", ErrorCode.ERROR_WRITING_DATA_IN_DB);
return new InternalServerErrorException(Response
.status(Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(List.of(ErrorCode.ERROR_WRITING_DATA_IN_DB)))
.build());
})
.map(deleted -> {
Status status;
if (deleted > 0) {
status = Status.NO_CONTENT;
} else {
Log.warnf("No preset operation found for id %s", pathParams.getPresetId());
status = Status.NOT_FOUND;
}
Log.debugf("delete preset - Response %s", status);
return Response.status(status).build();
});
}

/**
* Returns the list of preset operations configured for a subscriber
*
Expand Down Expand Up @@ -147,12 +172,11 @@ public Uni<Response> getLastPresetsOperation(@Valid @BeanParam CommonHeader head
Log.debugf("getLastPresetsOperation - Input parameters: %s, %s", headers, pathParams);

return findLatestPresetOperation(pathParams.getPaTaxCode(), pathParams.getSubscriberId())
.chain(m -> {
.chain(m -> {
if (m == null) {
Log.debugf("getLastPresetsOperation - Response %s", Status.NOT_FOUND);
return Uni.createFrom().item(Response.status(Status.NOT_FOUND).build());
}
else {
} else {
Log.debugf("getLastPresetsOperation - Response %s", m);
return Uni.createFrom().item(Response.status(Status.OK).entity(m).build());
}
Expand Down Expand Up @@ -202,10 +226,10 @@ private Uni<PresetOperation> findLatestPresetOperation(String paTaxCode, String
.with(PA_TAX_CODE, paTaxCode)
.and(SUBSCRIBER_ID, subscriberId)
.map()
)
.firstResult()
)
.firstResult()
.onFailure().transform(PresetsResource::manageDbReadError)
.map(presetEntity -> {
.map(presetEntity -> {
if (presetEntity != null) {
if (StringUtils.equals(presetEntity.presetOperation.getStatus(), PresetStatus.TO_EXECUTE.name())) {
return presetEntity.presetOperation;
Expand Down Expand Up @@ -257,6 +281,9 @@ private Uni<PresetEntity> persistPreset(Subscriber subscriber,

}




/**
* Build the preset entity object to be persisted the {@link #persistPreset(Subscriber, CreatePresetRequest)} operation
*
Expand All @@ -281,9 +308,9 @@ private PresetEntity buildPresetEntity(Subscriber subscriber,
presetOperation.setStatusTimestamp(timestamp);
presetOperation.setSubscriberId(subscriber.getSubscriberId());

PresetEntity presetEntity = new PresetEntity();
presetEntity.id = presetId;
presetEntity.presetOperation = presetOperation;
PresetEntity presetEntity = new PresetEntity();
presetEntity.id = presetId;
presetEntity.presetOperation = presetOperation;

return presetEntity;
}
Expand Down
Loading