Skip to content

Commit

Permalink
[PAGOPA-2181] fix: included domain ID in idempotency key (#125)
Browse files Browse the repository at this point in the history
Co-authored-by: AngeloCaporaso <[email protected]>
Co-authored-by: pagopa-github-bot <[email protected]>
  • Loading branch information
3 people authored Sep 30, 2024
1 parent 8ac8cef commit cb30604
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 154 deletions.
2 changes: 1 addition & 1 deletion helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: pagopa-wisp-converter
description: A service that permits to handle nodoInviaRPT and nodoInviaCarrelloRPT request from WISP, interfacing them with GPD system
type: application
version: 0.209.0
version: 0.210.0
appVersion: 0.3.6
dependencies:
- name: microservice-chart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public interface RTRepository extends CosmosRepository<RTEntity, String> {


@Query("SELECT * FROM c WHERE IS_NULL(c.rt) AND c.idDominio = @organizationId AND c._ts >= DateTimeToTimestamp(@dateFrom) / 1000 and c._ts <= DateTimeToTimestamp(@dateTo) / 1000")
@Query("SELECT * FROM c WHERE IS_NULL(c.rt) AND c.domainId = @organizationId AND c._ts >= DateTimeToTimestamp(@dateFrom) / 1000 and c._ts <= DateTimeToTimestamp(@dateTo) / 1000")
List<RTEntity> findByOrganizationId(@Param("organizationId") String organizationId,
@Param("dateFrom") String dateFrom,
@Param("dateTo") String dateTo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class IdempotencyKeyEntity {

private ReceiptTypeEnum receiptType;

private String sessionId;

private IdempotencyStatusEnum status;

private Instant lockedAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.azure.spring.data.cosmos.core.mapping.Container;
import com.azure.spring.data.cosmos.core.mapping.PartitionKey;
import it.gov.pagopa.wispconverter.repository.model.enumz.ReceiptStatusEnum;
import it.gov.pagopa.wispconverter.repository.model.enumz.ReceiptTypeEnum;
import lombok.*;
import org.springframework.data.annotation.Id;
Expand All @@ -16,17 +17,21 @@
public class RTEntity {

@Id
private String id;
private String id; // as domainId + iuv + ccp

@PartitionKey
private String partitionKey;

private String idDominio;
private String domainId;

private String iuv;

private String ccp;

private String sessionId;

private ReceiptStatusEnum receiptStatus;

private ReceiptTypeEnum receiptType;

// ricevuta-telematica-base-64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public class RTRequestEntity {
@PartitionKey
private String partitionKey;

private String domainId;

private String iuv;

private String ccp;

private String sessionId;

private String primitive;

private String payload;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package it.gov.pagopa.wispconverter.repository.model.enumz;

public enum ReceiptStatusEnum {
REDIRECT,
SENDING,
SCHEDULED,
SENT,
NOT_SENT
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import it.gov.pagopa.wispconverter.exception.AppException;
import it.gov.pagopa.wispconverter.repository.model.enumz.InternalStepStatus;
import it.gov.pagopa.wispconverter.repository.model.enumz.ReceiptStatusEnum;
import org.springframework.stereotype.Service;

import it.gov.pagopa.gen.wispconverter.client.checkout.model.CartRequestDto;
Expand Down Expand Up @@ -51,7 +52,7 @@ public String convert(String sessionId) throws URISyntaxException {
SessionDataDTO sessionData = this.rptExtractorService.extractSessionData(rptRequestEntity.getPrimitive(), rptRequestEntity.getPayload());

// prepare receipt-rt saving (nodoChiediCopiaRT)
rtReceiptCosmosService.saveRTEntity(sessionData);
rtReceiptCosmosService.saveRTEntity(sessionData, ReceiptStatusEnum.REDIRECT);

// calling GPD creation API in order to generate the debt position associated to RPTs
this.debtPositionService.createDebtPositions(sessionData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class IdempotencyService {
@Value("${wisp-converter.idempotency.lock-validity-in-minutes}")
private Integer lockValidityInMinutes;

public static String generateIdempotencyKeyId(String sessionId, String noticeNumber, String domainId) {
return String.format("%s_%s_%s", sessionId, noticeNumber, domainId);
}

public boolean isIdempotencyKeyProcessable(String idempotencyKey, ReceiptTypeEnum receiptType) {

boolean isProcessable = true;
Expand Down Expand Up @@ -68,6 +72,7 @@ public void lockIdempotencyKey(String idempotencyKey, ReceiptTypeEnum receiptTyp
IdempotencyKeyEntity idempotencyKeyEntity;

// try to retrieve idempotency key entity from the storage and check if exists
// In this case, no findBy with partition key is set because the search could refers to different days
Optional<IdempotencyKeyEntity> optIdempotencyKeyEntity = idempotencyKeyRepository.findById(idempotencyKey);
if (optIdempotencyKeyEntity.isPresent()) {

Expand All @@ -84,10 +89,18 @@ public void lockIdempotencyKey(String idempotencyKey, ReceiptTypeEnum receiptTyp

} else {

// extracting sessionId for entity
String sessionId = null;
String[] idempotencyKeyComponents = idempotencyKey.split("_");
if (idempotencyKeyComponents.length > 0) {
sessionId = idempotencyKeyComponents[0];
}

// it not exists, so it can be created a new idempotency key entity ex novo
idempotencyKeyEntity = IdempotencyKeyEntity.builder()
.id(idempotencyKey)
.receiptType(receiptTypeEnum)
.sessionId(sessionId)
.build();
}

Expand Down
Loading

0 comments on commit cb30604

Please sign in to comment.