Skip to content

Commit

Permalink
Added managerName and managerSurname
Browse files Browse the repository at this point in the history
  • Loading branch information
giampieroferrara committed Dec 13, 2024
1 parent 156a475 commit 9ed1cd4
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import it.pagopa.selfcare.onboarding.entity.Onboarding;
import it.pagopa.selfcare.onboarding.entity.OnboardingAttachment;
import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow;
import org.openapi.quarkus.user_registry_json.model.UserResource;

import java.io.File;
import java.util.List;
import java.util.Optional;
import org.openapi.quarkus.user_registry_json.model.UserResource;

public interface ContractService {
File createContractPDF(
Expand All @@ -19,7 +18,7 @@ File createContractPDF(
String pdfFormatFilename);

File createAttachmentPDF(
String templatePath, Onboarding onboarding, String productName, String pdfFormatFilename);
String templatePath, Onboarding onboarding, String productName, String pdfFormatFilename, UserResource userResource);

File loadContractPDF(String contractTemplatePath, String onboardingId, String productName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public File createAttachmentPDF(
String attachmentTemplatePath,
Onboarding onboarding,
String productName,
String attachmentName) {
String attachmentName, UserResource userResource) {

log.info("START - createAttachmentPDF for template: {}", attachmentTemplatePath);

Expand All @@ -235,7 +235,7 @@ public File createAttachmentPDF(
File attachmentPdfFile =
"pdf".equals(fileType)
? azureBlobClient.getFileAsPdf(attachmentTemplatePath)
: createPdfFileAttachment(attachmentTemplatePath, onboarding);
: createPdfFileAttachment(attachmentTemplatePath, onboarding, userResource);

// Define the filename and path for storage.
final String filename =
Expand Down Expand Up @@ -304,7 +304,7 @@ private File createPdfFileContract(
return temporaryPdfFile.toFile();
}

private File createPdfFileAttachment(String attachmentTemplatePath, Onboarding onboarding)
private File createPdfFileAttachment(String attachmentTemplatePath, Onboarding onboarding, UserResource userResource)
throws IOException {
final String builder =
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
Expand All @@ -317,7 +317,7 @@ private File createPdfFileAttachment(String attachmentTemplatePath, Onboarding o
// Create a temporary PDF file to store the contract.
Path attachmentPdfFile = Files.createTempFile(builder, ".pdf");
// Prepare common data for the contract document.
Map<String, Object> data = setUpAttachmentData(onboarding);
Map<String, Object> data = setUpAttachmentData(onboarding, userResource);

log.debug("data Map for PDF: {}", data);
fillPDFAsFile(attachmentPdfFile, attachmentTemplateText, data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.pagopa.selfcare.onboarding.service;

import static it.pagopa.selfcare.onboarding.utils.Utils.*;
import static it.pagopa.selfcare.onboarding.utils.Utils.CONTRACT_FILENAME_FUNC;
import static it.pagopa.selfcare.onboarding.utils.Utils.NOT_ALLOWED_WORKFLOWS_FOR_INSTITUTION_NOTIFICATIONS;

import com.microsoft.azure.functions.ExecutionContext;
Expand All @@ -14,7 +14,11 @@
import it.pagopa.selfcare.onboarding.config.MailTemplatePlaceholdersConfig;
import it.pagopa.selfcare.onboarding.dto.NotificationCountResult;
import it.pagopa.selfcare.onboarding.dto.ResendNotificationsFilters;
import it.pagopa.selfcare.onboarding.entity.*;
import it.pagopa.selfcare.onboarding.entity.Onboarding;
import it.pagopa.selfcare.onboarding.entity.OnboardingAttachment;
import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow;
import it.pagopa.selfcare.onboarding.entity.Token;
import it.pagopa.selfcare.onboarding.entity.User;
import it.pagopa.selfcare.onboarding.exception.GenericOnboardingException;
import it.pagopa.selfcare.onboarding.repository.OnboardingRepository;
import it.pagopa.selfcare.onboarding.repository.TokenRepository;
Expand All @@ -29,7 +33,11 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.bson.Document;
Expand Down Expand Up @@ -84,8 +92,6 @@ public Optional<Onboarding> getOnboarding(String onboardingId) {

public void createContract(OnboardingWorkflow onboardingWorkflow) {
Onboarding onboarding = onboardingWorkflow.getOnboarding();
String validManagerId = getValidManagerId(onboarding.getUsers());
UserResource manager = userRegistryApi.findByIdUsingGET(USERS_WORKS_FIELD_LIST, validManagerId);

List<UserResource> delegates =
onboarding.getUsers().stream()
Expand All @@ -99,19 +105,24 @@ public void createContract(OnboardingWorkflow onboardingWorkflow) {
contractService.createContractPDF(
onboardingWorkflow.getContractTemplatePath(product),
onboarding,
manager,
getUserResource(onboarding),
delegates,
product.getTitle(),
onboardingWorkflow.getPdfFormatFilename());
}

private UserResource getUserResource(Onboarding onboarding) {
String validManagerId = getValidManagerId(onboarding.getUsers());
return userRegistryApi.findByIdUsingGET(USERS_WORKS_FIELD_LIST, validManagerId);
}

public void createAttachment(OnboardingAttachment onboardingAttachment) {
Onboarding onboarding = onboardingAttachment.getOnboarding();
Product product = productService.getProductIsValid(onboarding.getProductId());
AttachmentTemplate attachment = onboardingAttachment.getAttachment();

contractService.createAttachmentPDF(
attachment.getTemplatePath(), onboarding, product.getTitle(), attachment.getName());
attachment.getTemplatePath(), onboarding, product.getTitle(), attachment.getName(), getUserResource(onboarding));
}

public void loadContract(Onboarding onboarding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ public static Map<String, Object> setUpCommonData(
return map;
}

public static Map<String, Object> setUpAttachmentData(Onboarding onboarding) {
public static Map<String, Object> setUpAttachmentData(Onboarding onboarding, UserResource userResource) {
Map<String, Object> map = new HashMap<>();

map.put(INSTITUTION_NAME, onboarding.getInstitution().getDescription());
map.put("institutionTaxCode", Optional.ofNullable(onboarding.getInstitution().getTaxCode()).orElse(UNDERSCORE));
map.put("institutionMail", onboarding.getInstitution().getDigitalAddress());
map.put("managerName", getStringValue(userResource.getName()));
map.put("managerSurname", getStringValue(userResource.getFamilyName()));
if (Objects.nonNull(onboarding.getInstitution().getGpuData())) {
map.put(
"businessRegisterNumber",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package it.pagopa.selfcare.onboarding.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import it.pagopa.selfcare.azurestorage.AzureBlobClient;
Expand All @@ -8,8 +17,23 @@
import it.pagopa.selfcare.onboarding.config.MailTemplatePlaceholdersConfig;
import it.pagopa.selfcare.onboarding.config.PagoPaSignatureConfig;
import it.pagopa.selfcare.onboarding.crypto.PadesSignService;
import it.pagopa.selfcare.onboarding.entity.*;
import it.pagopa.selfcare.onboarding.entity.AggregateInstitution;
import it.pagopa.selfcare.onboarding.entity.GPUData;
import it.pagopa.selfcare.onboarding.entity.Institution;
import it.pagopa.selfcare.onboarding.entity.Onboarding;
import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow;
import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflowAggregator;
import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflowInstitution;
import it.pagopa.selfcare.onboarding.entity.PaymentServiceProvider;
import it.pagopa.selfcare.onboarding.entity.User;
import jakarta.inject.Inject;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -21,13 +45,6 @@
import org.openapi.quarkus.user_registry_json.model.UserResource;
import org.openapi.quarkus.user_registry_json.model.WorkContactResource;

import java.io.File;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@QuarkusTest
class ContractServiceDefaultTest {

Expand Down Expand Up @@ -237,12 +254,13 @@ void createAttachmentPDF() {
Onboarding onboarding = createOnboarding();

Mockito.when(azureBlobClient.getFileAsText(contractFilepath)).thenReturn(contractHtml);

Mockito.when(azureBlobClient.uploadFile(any(), any(), any())).thenReturn(contractHtml);

UserResource userResource = createUserResource();

File attachmentPDF =
contractService.createAttachmentPDF(
contractFilepath, onboarding, productNameAccent, pdfFormatFile);
contractFilepath, onboarding, productNameAccent, pdfFormatFile, userResource);

assertNotNull(attachmentPDF);

Expand Down Expand Up @@ -500,4 +518,20 @@ void createContractPRV() {
Mockito.verify(azureBlobClient, Mockito.times(1)).uploadFile(any(), any(), any());
Mockito.verifyNoMoreInteractions(azureBlobClient);
}

private UserResource createUserResource() {
UserResource userResource = new UserResource();
userResource.setId(UUID.randomUUID());

CertifiableFieldResourceOfstring resourceOfName = new CertifiableFieldResourceOfstring();
resourceOfName.setCertification(CertifiableFieldResourceOfstring.CertificationEnum.NONE);
resourceOfName.setValue("name");
userResource.setName(resourceOfName);

CertifiableFieldResourceOfstring resourceOfSurname = new CertifiableFieldResourceOfstring();
resourceOfSurname.setCertification(CertifiableFieldResourceOfstring.CertificationEnum.NONE);
resourceOfSurname.setValue("surname");
userResource.setFamilyName(resourceOfSurname);
return userResource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

import static it.pagopa.selfcare.onboarding.service.OnboardingService.USERS_FIELD_LIST;
import static it.pagopa.selfcare.onboarding.service.OnboardingService.USERS_WORKS_FIELD_LIST;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import com.microsoft.azure.functions.ExecutionContext;
import eu.europa.esig.dss.enumerations.DigestAlgorithm;
Expand All @@ -19,7 +28,13 @@
import it.pagopa.selfcare.onboarding.common.WorkflowType;
import it.pagopa.selfcare.onboarding.dto.NotificationCountResult;
import it.pagopa.selfcare.onboarding.dto.ResendNotificationsFilters;
import it.pagopa.selfcare.onboarding.entity.*;
import it.pagopa.selfcare.onboarding.entity.Institution;
import it.pagopa.selfcare.onboarding.entity.Onboarding;
import it.pagopa.selfcare.onboarding.entity.OnboardingAttachment;
import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflow;
import it.pagopa.selfcare.onboarding.entity.OnboardingWorkflowInstitution;
import it.pagopa.selfcare.onboarding.entity.Token;
import it.pagopa.selfcare.onboarding.entity.User;
import it.pagopa.selfcare.onboarding.exception.GenericOnboardingException;
import it.pagopa.selfcare.onboarding.repository.OnboardingRepository;
import it.pagopa.selfcare.onboarding.repository.TokenRepository;
Expand All @@ -29,7 +44,13 @@
import it.pagopa.selfcare.product.service.ProductService;
import jakarta.inject.Inject;
import java.io.File;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Test;
Expand All @@ -38,6 +59,7 @@
import org.openapi.quarkus.user_registry_json.api.UserApi;
import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring;
import org.openapi.quarkus.user_registry_json.model.UserResource;
import org.openapi.quarkus.user_registry_json.model.WorkContactResource;

@QuarkusTest
class OnboardingServiceTest {
Expand Down Expand Up @@ -202,6 +224,14 @@ void createAttachments() {

when(productService.getProductIsValid(onboarding.getProductId())).thenReturn(product);

UserResource userResource = new UserResource();
userResource.setId(UUID.randomUUID());
Map<String, WorkContactResource> map = new HashMap<>();
userResource.setWorkContacts(map);

when(userRegistryApi.findByIdUsingGET(anyString(), anyString()))
.thenReturn(userResource);

// Act
onboardingService.createAttachment(onboardingAttachment);

Expand All @@ -211,7 +241,7 @@ void createAttachments() {
// Capture the path of the template used for the PDF
ArgumentCaptor<String> captorTemplatePath = ArgumentCaptor.forClass(String.class);
Mockito.verify(contractService, Mockito.times(1))
.createAttachmentPDF(captorTemplatePath.capture(), any(), any(), any());
.createAttachmentPDF(captorTemplatePath.capture(), any(), any(), any(), any());

// Check that the correct template was used
assertEquals(
Expand Down

0 comments on commit 9ed1cd4

Please sign in to comment.