Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

[SELC-4968] feat: add delegation update and userInstitution update during institutionUpdate #502

Merged
merged 4 commits into from
Jun 3, 2024
Merged
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
6 changes: 6 additions & 0 deletions app/src/main/resources/swagger/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3233,6 +3233,9 @@
"items" : {
"type" : "string"
}
},
"parentDescription" : {
"type" : "string"
}
}
},
Expand Down Expand Up @@ -3511,6 +3514,9 @@
"ivassCode" : {
"type" : "string"
},
"parentDescription" : {
"type" : "string"
},
"paymentServiceProvider" : {
"$ref" : "#/components/schemas/PaymentServiceProvider"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import it.pagopa.selfcare.mscore.model.delegation.Delegation;
import it.pagopa.selfcare.mscore.model.delegation.DelegationWithPagination;
import it.pagopa.selfcare.mscore.model.delegation.GetDelegationParameters;
import it.pagopa.selfcare.mscore.model.institution.Institution;

import java.util.List;

Expand All @@ -17,4 +18,5 @@ public interface DelegationConnector {
Delegation findByIdAndModifyStatus(String delegationId, DelegationState status);
boolean checkIfDelegationsAreActive(String institutionId);
Delegation findAndActivate(String from, String to, String productId);
void updateDelegation(Institution update);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package it.pagopa.selfcare.mscore.api;


import it.pagopa.selfcare.mscore.model.institution.InstitutionUpdate;

import java.util.List;

public interface UserApiConnector {

List<String> getUserEmails(String institutionId, String productId);

void updateUserInstitution(String institutionId, InstitutionUpdate institutionUpdate);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum GenericError {
CREATE_DELEGATION_ERROR("001-0027", "Error while creating requested delegation"),
SEND_MAIL_FOR_DELEGATION_ERROR("002-0027", "Error during send mail for delegation"),
DELETE_DELEGATION_ERROR("0003-0027", "Error while deleting delegation"),
UPDATE_DELEGATION_ERROR("004-0027", "Error while updating delegation"),
ONBOARDING_VERIFICATION_ERROR("0015", "Error while verifying onboarding"),
GETTING_ONBOARDING_INFO_ERROR("0016", "Error while getting onboarding info"),
GET_PRODUCTS_ERROR("0031", "Error while getting products"),
Expand Down Expand Up @@ -59,6 +60,7 @@ public enum GenericError {
GET_INSTITUTIONS_REQUEST_ERROR("0054", "Invalid request parameters sent. Allowed filters combinations taxCode and subunit or origin and originId"),
VERIFY_USER_ERROR("0000", "Error while searching institutions related to given productId"),
GET_USER_ERROR("0000", "Error while searching user given UserID"),
UPDATE_USER_INSTITUTION_ERROR("0000", "Error while updating InstitutionUser for id %s"),
GENERIC_ERROR("0000", "Generic Error");
private final String code;
private final String detail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public class InstitutionUpdate {
private String ivassCode;
private boolean imported;
private AdditionalInformations additionalInformations;
private boolean delegation;
private Boolean delegation;
private String parentDescription;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import it.pagopa.selfcare.mscore.model.delegation.DelegationWithPagination;
import it.pagopa.selfcare.mscore.model.delegation.GetDelegationParameters;
import it.pagopa.selfcare.mscore.model.delegation.PageInfo;
import it.pagopa.selfcare.mscore.model.institution.Institution;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -153,4 +154,27 @@ public Delegation findAndActivate(String from, String to, String productId) {
FindAndModifyOptions findAndModifyOptions = FindAndModifyOptions.options().upsert(false).returnNew(true);
return delegationMapper.convertToDelegation(repository.findAndModify(query, update, findAndModifyOptions, DelegationEntity.class));
}

@Override
public void updateDelegation(Institution institutionUpdate) {

// If institution own some delegations, we also update "to" reference
// isDelegation is true if institution own some delegations
if (institutionUpdate.isDelegation()) {
manuraf marked this conversation as resolved.
Show resolved Hide resolved
Update updateFrom = new Update();
Query queryFrom = Query.query(Criteria.where(DelegationEntity.Fields.to.name()).is(institutionUpdate.getId()));
updateFrom.set(DelegationEntity.Fields.institutionToName.name(), institutionUpdate.getDescription());
repository.updateMulti(queryFrom, updateFrom, DelegationEntity.class);
}

Update updateTo = new Update();
Query queryTo = Query.query(Criteria.where(DelegationEntity.Fields.from.name()).is(institutionUpdate.getId()));
updateTo.set(DelegationEntity.Fields.institutionFromName.name(), institutionUpdate.getDescription());
if (Objects.nonNull(institutionUpdate.getParentDescription())) {
updateTo.set(DelegationEntity.Fields.institutionFromRootName.name(), institutionUpdate.getParentDescription());
}
repository.updateMulti(queryTo, updateTo, DelegationEntity.class);

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.pagopa.selfcare.mscore.connector.dao;

import com.mongodb.client.result.UpdateResult;
import it.pagopa.selfcare.mscore.model.aggregation.UserInstitutionFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -22,4 +23,6 @@ public interface MongoCustomConnector {
<O> O findAndModify(Query query, UpdateDefinition updateDefinition, FindAndModifyOptions findAndModifyOptions, Class<O> outputType);

<O> List<O> findUserInstitutionAggregation(UserInstitutionFilter filter, Class<O> outputType);

<O> UpdateResult updateMulti(Query query, UpdateDefinition updateDefinition, Class<O> outputType);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package it.pagopa.selfcare.mscore.connector.dao;

import com.mongodb.client.result.UpdateResult;
import it.pagopa.selfcare.mscore.model.aggregation.UserInstitutionFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.GraphLookupOperation;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.aggregation.UnwindOperation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
Expand Down Expand Up @@ -95,4 +99,9 @@ private MatchOperation checkIfInstitutionIdIsPresent(UserInstitutionFilter filte
private void checkIfExternalIdIsPresent(UserInstitutionFilter filter, GraphLookupOperation.GraphLookupOperationBuilder graphLookupOperation) {
graphLookupOperation.restrict(Criteria.where("externalId").is(filter.getExternalId()));
}

@Override
public <O> UpdateResult updateMulti(Query query, UpdateDefinition updateDefinition, Class<O> outputType){
return mongoOperations.updateMulti(query, updateDefinition, outputType);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package it.pagopa.selfcare.mscore.connector.dao.model.mapper;

import it.pagopa.selfcare.mscore.connector.dao.model.InstitutionEntity;
import it.pagopa.selfcare.mscore.connector.dao.model.inner.*;
import it.pagopa.selfcare.mscore.constant.Origin;
import it.pagopa.selfcare.mscore.model.institution.*;
import it.pagopa.selfcare.mscore.connector.dao.model.inner.GeoTaxonomyEntity;
import it.pagopa.selfcare.mscore.model.institution.DataProtectionOfficer;
import it.pagopa.selfcare.mscore.model.institution.InstitutionGeographicTaxonomies;
import it.pagopa.selfcare.mscore.model.institution.InstitutionUpdate;
import it.pagopa.selfcare.mscore.model.institution.PaymentServiceProvider;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.query.Update;

import java.time.OffsetDateTime;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@NoArgsConstructor(access = AccessLevel.NONE)
Expand All @@ -34,6 +38,7 @@ public static Map<String, Object> getNotNullField(InstitutionUpdate institutionU
response.put(InstitutionUpdate.Fields.institutionType.name(), institutionUpdate.getInstitutionType().name());
}
response.put(InstitutionUpdate.Fields.description.name(), institutionUpdate.getDescription());
response.put(InstitutionUpdate.Fields.parentDescription.name(), institutionUpdate.getParentDescription());
response.put(InstitutionUpdate.Fields.digitalAddress.name(), institutionUpdate.getDigitalAddress());
response.put(InstitutionUpdate.Fields.address.name(), institutionUpdate.getAddress());
response.put(InstitutionUpdate.Fields.taxCode.name(), institutionUpdate.getTaxCode());
Expand All @@ -44,7 +49,7 @@ public static Map<String, Object> getNotNullField(InstitutionUpdate institutionU
response.put(InstitutionUpdate.Fields.supportEmail.name(), institutionUpdate.getSupportEmail());
response.put(InstitutionUpdate.Fields.supportPhone.name(), institutionUpdate.getSupportPhone());
response.put(InstitutionUpdate.Fields.imported.name(), institutionUpdate.isImported());
response.put(InstitutionUpdate.Fields.delegation.name(), institutionUpdate.isDelegation());
response.put(InstitutionUpdate.Fields.delegation.name(), institutionUpdate.getDelegation());

if(institutionUpdate.getPaymentServiceProvider() != null) {
response.put(constructPaymentInnerField(PaymentServiceProvider.Fields.abiCode.name()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import it.pagopa.selfcare.mscore.exception.MsCoreException;
import it.pagopa.selfcare.mscore.model.delegation.*;
import it.pagopa.selfcare.mscore.model.institution.Institution;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.function.Executable;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
Expand Down Expand Up @@ -306,4 +308,43 @@ private GetDelegationParameters createDelegationParameters(String from, String t
.size(size)
.build();
}

@Test
void updateDelegation() {

String description = "description";
String rootName = "rootName";
String institutionId = "institutionId";

Institution institutionUpdate = new Institution();
institutionUpdate.setId(institutionId);
institutionUpdate.setDescription(description);
institutionUpdate.setParentDescription(rootName);
institutionUpdate.setDelegation(true);

//when
final Executable executable = () -> delegationConnectorImpl.updateDelegation(institutionUpdate);

Assertions.assertDoesNotThrow(executable);
verify(delegationRepository, times(2)).updateMulti(any(), any(), any());

}

@Test
void updateDelegation_noDelegation() {

String description = "description";
String institutionId = "institutionId";

Institution institutionUpdate = new Institution();
institutionUpdate.setId(institutionId);
institutionUpdate.setDescription(description);

//when
final Executable executable = () -> delegationConnectorImpl.updateDelegation(institutionUpdate);

Assertions.assertDoesNotThrow(executable);
verify(delegationRepository, times(1)).updateMulti(any(), any(), any());

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.pagopa.selfcare.mscore.connector.dao;

import com.mongodb.client.result.UpdateResult;
import it.pagopa.selfcare.mscore.model.aggregation.UserInstitutionAggregation;
import it.pagopa.selfcare.mscore.model.aggregation.UserInstitutionFilter;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -125,5 +126,14 @@ void findUserAndInstitutionWithExternalId() {

}

@Test
void updateMulti() {
UpdateResult updateResult = mock(UpdateResult.class);
when(mongoOperations.updateMulti(any(), any(), (Class<Object>) any())).thenReturn(updateResult);
Query query = new Query();
UpdateDefinition updateDefinition = new Update();
assertNotNull(mongoCustomConnector.updateMulti(query, updateDefinition, Object.class));
}

}

72 changes: 65 additions & 7 deletions connector/rest/docs/openapi/selfcare-user-docs.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"openapi" : "3.0.3",
"info" : {
"title" : "user-ms API",
"title" : "User API",
"version" : "1.0.0"
},
"servers" : [ {
Expand All @@ -12,16 +12,14 @@
"description" : "Auto generated value"
} ],
"paths" : {
"/authorize/{institutionId}" : {
"/authorize" : {
"get" : {
"tags" : [ "User Permission Controller" ],
"summary" : "Get permission for a user in an institution",
"parameters" : [ {
"name" : "institutionId",
"in" : "path",
"required" : true,
"in" : "query",
"schema" : {
"minLength" : 1,
"type" : "string"
}
}, {
Expand Down Expand Up @@ -61,6 +59,46 @@
} ]
}
},
"/institutions/{institutionId}" : {
"put" : {
"tags" : [ "Institution Controller" ],
"summary" : "The API updates the description in all occurrences of userInstitution, given a certain institutionId.",
"parameters" : [ {
"name" : "institutionId",
"in" : "path",
"required" : true,
"schema" : {
"type" : "string"
}
} ],
"requestBody" : {
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/UpdateDescriptionDto"
}
}
}
},
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"application/json" : { }
}
},
"403" : {
"description" : "Not Allowed"
},
"401" : {
"description" : "Not Authorized"
}
},
"security" : [ {
"SecurityScheme" : [ ]
} ]
}
},
"/institutions/{institutionId}/products/{productId}/createdAt" : {
"put" : {
"tags" : [ "Institution Controller" ],
Expand Down Expand Up @@ -660,6 +698,12 @@
"schema" : {
"type" : "string"
}
}, {
"name" : "productRole",
"in" : "query",
"schema" : {
"type" : "string"
}
}, {
"name" : "status",
"in" : "query",
Expand Down Expand Up @@ -1129,7 +1173,7 @@
"type" : "string"
},
"Product" : {
"required" : [ "productId", "role" ],
"required" : [ "productId", "role", "productRoles" ],
"type" : "object",
"properties" : {
"productId" : {
Expand All @@ -1151,7 +1195,7 @@
}
},
"Product1" : {
"required" : [ "productId", "role" ],
"required" : [ "productId", "role", "productRoles" ],
"type" : "object",
"properties" : {
"productId" : {
Expand Down Expand Up @@ -1185,7 +1229,21 @@
}
}
},
"UpdateDescriptionDto" : {
"required" : [ "institutionDescription" ],
"type" : "object",
"properties" : {
"institutionDescription" : {
"minLength" : 1,
"type" : "string"
},
"institutionRootName" : {
"type" : "string"
}
}
},
"UpdateUserRequest" : {
"required" : [ "email" ],
"type" : "object",
"properties" : {
"name" : {
Expand Down
Loading
Loading