Skip to content

Commit

Permalink
feat: Revision create terminal and initialized solutions (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoGiuliani authored Jul 30, 2024
1 parent 3e1b01d commit 75fb5d5
Show file tree
Hide file tree
Showing 20 changed files with 657 additions and 79 deletions.
15 changes: 15 additions & 0 deletions src/main/java/it/pagopa/swclient/mil/papos/dao/SolutionEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package it.pagopa.swclient.mil.papos.dao;

import io.quarkus.mongodb.panache.PanacheMongoEntity;
import io.quarkus.mongodb.panache.common.MongoEntity;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@MongoEntity(database = "mil", collection = "solutions")
public class SolutionEntity extends PanacheMongoEntity {

private String pspId;
private String locationCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package it.pagopa.swclient.mil.papos.dao;

import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoRepositoryBase;
import jakarta.enterprise.context.ApplicationScoped;
import org.bson.types.ObjectId;

@ApplicationScoped
public class SolutionRepository implements ReactivePanacheMongoRepositoryBase<SolutionEntity, ObjectId> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
@MongoEntity(database = "mil", collection = "terminals")
public class TerminalEntity extends PanacheMongoEntity {

private String pspId;
private String terminalUuid;
private String solutionId;
private String terminalId;
private Boolean enabled;
private String payeeCode;
private String terminalUuid;
private List<String> workstations;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
@MongoEntity(database = "mil", collection = "transactions")
public class TransactionEntity extends PanacheMongoEntity {

private String transactionId;
private String pspId;
private String terminalId;
private String terminalUuid;
private String noticeNumber;
private String payeeCode;
private Date creationTimestamp;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/it/pagopa/swclient/mil/papos/model/SolutionDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package it.pagopa.swclient.mil.papos.model;

import it.pagopa.swclient.mil.papos.util.ErrorCodes;
import it.pagopa.swclient.mil.papos.util.RegexPatterns;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;

public record SolutionDto(@NotNull(message = ErrorCodes.ERROR_PSPID_MUST_NOT_BE_NULL_MSG)
@Pattern(regexp = RegexPatterns.ASCII_PRINTABLE_1_TO_64_PATTERN)
String pspId,

@NotNull(message = ErrorCodes.ERROR_PAYEECODE_MUST_NOT_BE_NULL_MSG)
@Pattern(regexp = RegexPatterns.EXACT_ELEVEN_NUM_PATTERN)
String locationCode) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import java.util.List;

public record TerminalDto(@NotNull(message = ErrorCodes.ERROR_PSPID_MUST_NOT_BE_NULL_MSG)
@Pattern(regexp = RegexPatterns.ASCII_PRINTABLE_1_TO_64_PATTERN)
String pspId,
public record TerminalDto(@NotNull(message = ErrorCodes.ERROR_SOLUTIONID_MUST_NOT_BE_NULL_MSG)
@Pattern(regexp = RegexPatterns.MONGO_OBJECT_ID_PATTERN)
String solutionId,

@NotNull(message = ErrorCodes.ERROR_TERMINALID_MUST_NOT_BE_NULL_MSG)
@Pattern(regexp = RegexPatterns.FOUR_TO_TWELVE_DIGITS_PATTERN)
Expand All @@ -18,9 +18,5 @@ public record TerminalDto(@NotNull(message = ErrorCodes.ERROR_PSPID_MUST_NOT_BE_
@NotNull(message = ErrorCodes.ERROR_ENABLED_MUST_NOT_BE_NULL_MSG)
Boolean enabled,

@NotNull(message = ErrorCodes.ERROR_PAYEECODE_MUST_NOT_BE_NULL_MSG)
@Pattern(regexp = RegexPatterns.EXACT_ELEVEN_NUM_PATTERN)
String payeeCode,

List<String> workstations) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package it.pagopa.swclient.mil.papos.resource;

import io.quarkus.logging.Log;
import io.smallrye.mutiny.Uni;
import it.pagopa.swclient.mil.papos.model.SolutionDto;
import it.pagopa.swclient.mil.papos.service.SolutionService;
import it.pagopa.swclient.mil.papos.util.ErrorCodes;
import it.pagopa.swclient.mil.papos.util.Errors;
import it.pagopa.swclient.mil.papos.util.RegexPatterns;
import jakarta.annotation.security.RolesAllowed;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

@Path("/solutions")
public class SolutionResource {
private final SolutionService solutionService;

public SolutionResource(SolutionService solutionService) {
this.solutionService = solutionService;
}

@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({"mil_papos_admin"})
public Uni<Response> createSolution(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
@Pattern(regexp = RegexPatterns.REQUEST_ID_PATTERN) String requestId,
@Valid @NotNull(message = ErrorCodes.ERROR_DTO_MUST_NOT_BE_NULL_MSG) SolutionDto solution) {

Log.debugf("SolutionResource -> createSolution - Input requestId, solutionDto: %s, %s", requestId, solution);

return solutionService.createSolution(solution)
.onFailure()
.transform(err -> {
Log.errorf(err, "SolutionResource -> createSolution: unexpected error during persist for solution [%s]", solution);

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB, ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
.onItem()
.transform(solutionSaved -> {
Log.debugf("SolutionResource -> createSolution: solution saved correctly on DB [%s]", solutionSaved);

return Response
.status(Response.Status.CREATED)
.entity(solutionSaved)
.build();
});
}

@GET
@Path("/{solutionId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({"mil_papos_admin"})
public Uni<Response> findSolution(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
@Pattern(regexp = RegexPatterns.REQUEST_ID_PATTERN) String requestId,
@PathParam(value = "solutionId") String solutionId) {

Log.debugf("SolutionResource -> findSolution: Input requestId, solutionId: %s, %s", requestId, solutionId);

return solutionService.findById(solutionId)
.onFailure()
.transform(err -> {
Log.errorf(err, "SolutionResource -> findSolution: error during search solution with solutionId: [%s]", solutionId);

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB, ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
.onItem()
.transformToUni(solution -> {
if (solution == null) {
Log.errorf("SolutionResource -> findSolution: error 404 during searching solution with solutionId: [%s, %s]", solutionId);

return Uni.createFrom().failure(new NotFoundException(Response
.status(Response.Status.NOT_FOUND)
.entity(new Errors(ErrorCodes.ERROR_SOLUTION_NOT_FOUND, ErrorCodes.ERROR_SOLUTION_NOT_FOUND_MSG))
.build()));
}

return Uni.createFrom().item(Response
.status(Response.Status.OK)
.entity(solution)
.build());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import it.pagopa.swclient.mil.papos.model.TerminalDto;
import it.pagopa.swclient.mil.papos.model.TerminalPageResponse;
import it.pagopa.swclient.mil.papos.model.WorkstationsDto;
import it.pagopa.swclient.mil.papos.service.SolutionService;
import it.pagopa.swclient.mil.papos.service.TerminalService;
import it.pagopa.swclient.mil.papos.util.ErrorCodes;
import it.pagopa.swclient.mil.papos.util.Errors;
Expand All @@ -29,13 +30,15 @@
@Path("/terminals")
public class TerminalResource {
private final TerminalService terminalService;
private final SolutionService solutionService;

private final JsonWebToken jwt;

private final ObjectMapper objectMapper;

public TerminalResource(TerminalService terminalService, JsonWebToken jwt, ObjectMapper objectMapper) {
public TerminalResource(TerminalService terminalService, SolutionService solutionService, JsonWebToken jwt, ObjectMapper objectMapper) {
this.terminalService = terminalService;
this.solutionService = solutionService;
this.jwt = jwt;
this.objectMapper = objectMapper;
}
Expand All @@ -53,35 +56,44 @@ public Uni<Response> createTerminal(

Log.debugf("TerminalResource -> createTerminal - Input requestId, createTerminal: %s, %s", requestId, terminal);

checkToken(terminal.pspId());
return solutionService.findById(terminal.solutionId())
.onItem()
.transformToUni(solution -> {
if (solution == null) {
Log.errorf("TerminalResource -> createTerminal: error 404 during searching solution with solutionId: %s", terminal.solutionId());

return terminalService.createTerminal(terminal)
.onFailure()
.transform(err -> {
Log.errorf(err,
"TerminalResource -> createTerminal: unexpected error during persist for terminal [%s]",
terminal);
return Uni.createFrom().failure(new NotFoundException(Response
.status(Response.Status.NOT_FOUND)
.entity(new Errors(ErrorCodes.ERROR_SOLUTION_NOT_FOUND, ErrorCodes.ERROR_SOLUTION_NOT_FOUND_MSG))
.build()));
}

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB,
ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
.onItem()
.transform(terminalSaved -> {
Log.debugf("TerminalResource -> createTerminal: terminal saved correctly on DB [%s]",
terminalSaved);
checkToken(solution.getPspId());

return Response.status(Response.Status.CREATED).build();
return terminalService.createTerminal(terminal)
.onFailure()
.transform(err -> {
Log.errorf(err, "TerminalResource -> createTerminal: unexpected error during persist for terminal [%s]", terminal);

return new InternalServerErrorException(Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(new Errors(ErrorCodes.ERROR_GENERIC_FROM_DB, ErrorCodes.ERROR_GENERIC_FROM_DB_MSG))
.build());
})
.onItem()
.transform(terminalSaved -> {
Log.debugf("TerminalResource -> createTerminal: terminal saved correctly on DB [%s]", terminalSaved);

return Response.status(Response.Status.CREATED).build();
});
});
}

@POST
@Path("/bulkload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ "pos_service_provider" })
@RolesAllowed({"pos_service_provider"})
public Uni<Response> bulkLoadTerminals(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
Expand Down Expand Up @@ -115,9 +127,9 @@ public Uni<Response> bulkLoadTerminals(

try {
List<TerminalDto> terminalRequests = objectMapper.readValue(file, objectMapper.getTypeFactory().constructCollectionType(List.class, TerminalDto.class));
for (TerminalDto terminal : terminalRequests) {
checkToken(terminal.pspId());
}
// TODO: CHECK THIS for (TerminalDto terminal : terminalRequests) {
// checkToken(terminal.pspId());
// }

return terminalService.processBulkLoad(terminalRequests)
.onFailure()
Expand Down Expand Up @@ -154,7 +166,7 @@ public Uni<Response> bulkLoadTerminals(
@Path("/bulkload/{bulkLoadingId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ "pos_service_provider" })
@RolesAllowed({"pos_service_provider"})
public Uni<Response> getBulkLoadingStatusFile(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
Expand Down Expand Up @@ -199,7 +211,7 @@ public Uni<Response> getBulkLoadingStatusFile(
@Path("/findByPayeeCode")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ "public_administration" })
@RolesAllowed({"public_administration"})
public Uni<Response> findByPayeeCode(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
Expand All @@ -217,7 +229,7 @@ public Uni<Response> findByPayeeCode(
@Path("/findByPspId")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ "pos_service_provider" })
@RolesAllowed({"pos_service_provider"})
public Uni<Response> findByPspId(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
Expand All @@ -235,7 +247,7 @@ public Uni<Response> findByPspId(
@Path("/findByWorkstation")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ "public_administration" })
@RolesAllowed({"public_administration"})
public Uni<Response> findByWorkstation(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
Expand All @@ -251,7 +263,7 @@ public Uni<Response> findByWorkstation(
@Path("/{terminalUuid}/updateWorkstations")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ "public_administration" })
@RolesAllowed({"public_administration"})
public Uni<Response> updateWorkstations(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
Expand Down Expand Up @@ -313,7 +325,7 @@ public Uni<Response> updateWorkstations(
@Path("/{terminalUuid}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ "pos_service_provider" })
@RolesAllowed({"pos_service_provider"})
public Uni<Response> updateTerminal(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
Expand All @@ -323,7 +335,7 @@ public Uni<Response> updateTerminal(

Log.debugf("TerminalResource -> updateTerminal - Input requestId, updateTerminal: %s, %s", requestId, terminal);

checkToken(terminal.pspId());
// TODO: CHECHK THIS checkToken(terminal.pspId());

return terminalService.findTerminal(terminalUuid)
.onFailure()
Expand Down Expand Up @@ -377,7 +389,7 @@ public Uni<Response> updateTerminal(
@Path("/{terminalUuid}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ "pos_service_provider" })
@RolesAllowed({"pos_service_provider"})
public Uni<Response> deleteTerminal(
@HeaderParam("RequestId")
@NotNull(message = ErrorCodes.ERROR_REQUESTID_MUST_NOT_BE_NULL_MSG)
Expand Down
Loading

0 comments on commit 75fb5d5

Please sign in to comment.