Skip to content

Commit

Permalink
feature: add period on payfit data - delete content matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien authored and fabiengo committed Apr 16, 2024
1 parent e5b2364 commit 758a156
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import fr.dossierfacile.common.entity.BarCodeFileAnalysis;
import fr.dossierfacile.common.entity.File;
import fr.dossierfacile.common.utils.DateRange;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -96,12 +99,17 @@ public boolean is2DDoc() {
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
private static class PayfitAuthenticatedContent {

private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
private static final ObjectMapper objectMapper = new ObjectMapper();

static {
objectMapper.registerModule(new JavaTimeModule());
}

private String companyName;
private String employeeName;
private String netSalary;
private DateRange period;

static String format(Object object) {
try {
Expand All @@ -119,9 +127,9 @@ public String toString() {
map.put("Entreprise", companyName);
map.put("Employé", employeeName);
map.put("Salaire net", netSalary);
map.put("Période", dateFormatter.format(period.getStart()) + " - " + dateFormatter.format(period.getEnd()));
return formatToList(map);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,8 @@ <h4>Vérifications automatiques :</h4>
<span class="fa fa-times text-danger" th:if="${analysis.isInWrongCategory}"
> Mauvaise catégorie</span>
<br/>
<div class="small" th:if="${analysis.isNotAuthenticated && !analysis.is2DDoc()}">
<span class="bold">Devrait contenir : </span><br/>
<span th:utext="${analysis.authenticatedContent}"></span>
</div>
<div class="small" th:if="${analysis.is2DDoc()}">
<span class="bold">Informations extraites du 2D-Doc : </span><br/>
<div class="small" th:if="${analysis.authenticatedContent != null}">
<span class="bold">Informations extraites : </span><br/>
<span th:utext="${analysis.authenticatedContent}"></span>
</div>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import fr.dossierfacile.common.entity.BarCodeDocumentType;
import fr.dossierfacile.common.entity.BarCodeFileAnalysis;
import fr.dossierfacile.common.entity.BarCodeType;
Expand All @@ -13,6 +14,9 @@
class DisplayableBarCodeFileAnalysisTest {

private final ObjectMapper objectMapper = new ObjectMapper();
{
objectMapper.registerModule(new JavaTimeModule());
}

@Test
void should_format_payfit_content() throws JsonProcessingException {
Expand All @@ -22,7 +26,11 @@ void should_format_payfit_content() throws JsonProcessingException {
"companyName": "Some Company",
"grossSalary": "2 533,33 €",
"companySiret": "128759437",
"employeeName": "John Doe"
"employeeName": "John Doe",
"period": {
"end": "2024-01-31",
"start": "2024-01-01"
}
}
""");

Expand All @@ -33,6 +41,7 @@ void should_format_payfit_content() throws JsonProcessingException {
<ul>
<li>Entreprise : Some Company</li>
<li>Employé : John Doe</li>
<li>Période : 01/01/2024 - 31/01/2024</li>
<li>Salaire net : 1 895,39 €</li>
</ul>
""");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fr.dossierfacile.common.utils;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDate;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class DateRange {
private LocalDate start;
private LocalDate end;

public static DateRange of(LocalDate start, LocalDate end) {
return new DateRange(start, end);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Service
@AllArgsConstructor
public class QrCodeFileAuthenticator {

private final ObjectMapper objectMapper;
private final List<QrCodeDocumentIssuer<? extends AuthenticationRequest>> issuers;

public Optional<BarCodeFileAnalysis> analyze(InMemoryFile file) {
Expand All @@ -34,7 +34,7 @@ private BarCodeFileAnalysis buildAnalysis(AuthenticationResult result, QrCode qr
return BarCodeFileAnalysis.builder()
.documentType(result.getDocumentType())
.barCodeContent(qrCode.getContent())
.verifiedData(new ObjectMapper().convertValue(result.getApiResponse(), ObjectNode.class))
.verifiedData(objectMapper.convertValue(result.getApiResponse(), ObjectNode.class))
.authenticationStatus(result.getAuthenticationStatus())
.barCodeType(BarCodeType.QR_CODE)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@Service
@AllArgsConstructor
public class TwoDDocFileAuthenticator {

private final ObjectMapper objectMapper;
private final TwoDDocCertificationAuthorities certificationAuthorities;

public BarCodeFileAnalysis analyze(TwoDDocRawContent twoDDocContent) {
Expand All @@ -32,7 +32,7 @@ public BarCodeFileAnalysis analyze(TwoDDocRawContent twoDDocContent) {
return BarCodeFileAnalysis.builder()
.documentType(twoDDoc.getDocumentType())
.barCodeContent(twoDDocContent.rawContent())
.verifiedData(new ObjectMapper().convertValue(twoDDoc.data().withLabels(), ObjectNode.class))
.verifiedData(objectMapper.convertValue(twoDDoc.data().withLabels(), ObjectNode.class))
.authenticationStatus(status)
.barCodeType(BarCodeType.TWO_D_DOC)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package fr.dossierfacile.process.file.service.qrcodeanalysis.payfit;

import fr.dossierfacile.common.utils.DateRange;
import fr.dossierfacile.process.file.service.qrcodeanalysis.payfit.client.PayfitResponse;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Stream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Builder
@Getter
public class PaySlipVerifiedContent {
private static final Pattern PERIOD_PATTERN = Pattern.compile("Période du (\\d{2}/\\d{2}/\\d{4}) au (\\d{2}/\\d{2}/\\d{4})");
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy");

private final String companyName;
private final String companySiret;
private final String employeeName;
private final String netSalary;
private final String grossSalary;
private final DateRange period;

public static PaySlipVerifiedContent from(PayfitResponse payFitResponse) {
var companyInfo = payFitResponse.getContent().getCompanyInfo();
Expand All @@ -26,12 +33,17 @@ public static PaySlipVerifiedContent from(PayfitResponse payFitResponse) {
.employeeName(extractFrom(companyInfo, "Employé"))
.netSalary(extractAmountFrom(employeeInfo, "Net à payer avant impôt"))
.grossSalary(extractAmountFrom(employeeInfo, "Salaire brut"))
.period(extractPeriod(payFitResponse.getHeader()))
.build();
}

public boolean isMatchingWith(String fileContent) {
return Stream.of(companyName, companySiret, employeeName, netSalary, grossSalary)
.allMatch(fileContent::contains);
private static DateRange extractPeriod(String header) {
Matcher periodMatcher = PERIOD_PATTERN.matcher(header);
if (periodMatcher.matches()) {
return DateRange.of(LocalDate.parse(periodMatcher.group(1), DATE_TIME_FORMATTER),
LocalDate.parse(periodMatcher.group(2), DATE_TIME_FORMATTER));
}
return null;
}

private static String extractFrom(List<PayfitResponse.Info> list, String label) {
Expand All @@ -46,5 +58,4 @@ private static String extractAmountFrom(List<PayfitResponse.Info> list, String l
PayfitAmount amount = new PayfitAmount(extractFrom(list, label));
return amount.format();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ protected AuthenticationResult authenticate(InMemoryFile file, PayfitAuthenticat
}

private AuthenticationResult buildResult(InMemoryFile pdfFile, PayfitResponse response) {
String actualContent = pdfFile.getContentAsString();
PaySlipVerifiedContent verifiedContent = PaySlipVerifiedContent.from(response);
boolean isAuthentic = verifiedContent.isMatchingWith(actualContent);
return new AuthenticationResult(PAYFIT_PAYSLIP, verifiedContent, FileAuthenticationStatus.of(isAuthentic));
return new AuthenticationResult(PAYFIT_PAYSLIP, verifiedContent, FileAuthenticationStatus.VALID);
}

private AuthenticationResult error() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.dossierfacile.process.file.service.qrcodeanalysis;

import com.fasterxml.jackson.databind.ObjectMapper;
import fr.dossierfacile.common.entity.BarCodeDocumentType;
import fr.dossierfacile.common.entity.BarCodeFileAnalysis;
import fr.dossierfacile.process.file.TestFilesUtil;
Expand All @@ -17,7 +18,8 @@
class QrCodeFileAuthenticatorTest {

private final PayfitClient client = mock(PayfitClient.class);
private final QrCodeFileAuthenticator authenticator = new QrCodeFileAuthenticator(List.of(new PayfitDocumentIssuer(client)));
private final ObjectMapper objectMapper = new ObjectMapper();
private final QrCodeFileAuthenticator authenticator = new QrCodeFileAuthenticator(objectMapper, List.of(new PayfitDocumentIssuer(client)));

@Test
void should_process_pdf_file() throws IOException {
Expand Down

0 comments on commit 758a156

Please sign in to comment.