Skip to content

Commit

Permalink
[SELC-4705] Fix: fixed update and create userInfo on userCDC (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminiaScarciofolo authored Apr 12, 2024
1 parent 188b32f commit b43f683
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package it.pagopa.selfcare.user.event.entity;

import io.quarkus.mongodb.panache.common.MongoEntity;
import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntity;
import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntityBase;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldNameConstants;
import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.codecs.pojo.annotations.BsonProperty;

import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
Expand All @@ -16,7 +19,9 @@
public class UserInfo extends ReactivePanacheMongoEntityBase {

@BsonId
@BsonProperty("_id")
private String userId;
private List<UserInstitutionRole> institutions;

private List<UserInstitutionRole> institutions = new ArrayList<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.experimental.FieldNameConstants;
import org.bson.types.ObjectId;

import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
Expand All @@ -19,6 +20,8 @@ public class UserInstitution extends ReactivePanacheMongoEntity {
private String userId;
private String institutionId;
private String institutionDescription;
private List<OnboardedProduct> products;
private String institutionRootName;
private List<OnboardedProduct> products = new ArrayList<>();
private String userMailUuid;

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class UserInstitutionRole {

private String institutionId;
private String institutionName;
private String institutionRootName;
private String userMailUuid;
private PartyRole role;
private OnboardedProductState state;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,16 @@

import it.pagopa.selfcare.onboarding.common.PartyRole;
import it.pagopa.selfcare.user.event.constant.OnboardedProductState;
import it.pagopa.selfcare.user.event.entity.UserInfo;
import it.pagopa.selfcare.user.event.entity.UserInstitution;
import it.pagopa.selfcare.user.event.entity.UserInstitutionRole;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.List;

@Mapper(componentModel = "cdi")
public interface UserMapper {

@Mapping(target = "institutions", expression = "java(productToUserInstitutionRole(userInstitution, role, state))")
UserInfo toNewUserInfo(UserInstitution userInstitution, PartyRole role, OnboardedProductState state);

@Mapping(target = "institutionName", source = "userInstitution.institutionDescription")
@Mapping(target = "role", source = "role")
@Mapping(target = "state", source = "state")
UserInstitutionRole toUserInstitutionRole(UserInstitution userInstitution, PartyRole role, OnboardedProductState state);

@Named("productToUserInstitutionRole")
default List<UserInstitutionRole> productToUserInstitutionRole(UserInstitution userInstitution, PartyRole role, OnboardedProductState state) {
UserInstitutionRole userInstitutionRole = new UserInstitutionRole();
userInstitutionRole.setInstitutionId(userInstitution.getInstitutionId());
userInstitutionRole.setInstitutionName(userInstitution.getInstitutionDescription());
userInstitutionRole.setRole(role);
userInstitutionRole.setState(state);
return List.of(userInstitutionRole);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,22 @@ public Uni<Void> updateUser(UserInstitution userInstitution) {
return deleteInstitutionOrAllUserInfo(opt.get(), userInstitution);
}
})
.orElse(Uni.createFrom().voidItem()));
.orElse(createNewUserInfo(userInstitution)));
}

private Uni<Void> createNewUserInfo(UserInstitution userInstitution) {
UserInfo userInfo = new UserInfo();
userInfo.setUserId(userInstitution.getUserId());
userInfo.setInstitutions(new ArrayList<>());
if(!CollectionUtils.isEmpty(userInstitution.getProducts())){
userInstitution.getProducts().forEach(product -> {
if (VALID_PRODUCT_STATE.contains(product.getStatus())) {
PartyRole role = product.getRole();
userInfo.getInstitutions().add(userMapper.toUserInstitutionRole(userInstitution, role, product.getStatus()));
}
});
}
return UserInfo.persistOrUpdate(userInfo).replaceWith(Uni.createFrom().voidItem());
}

private Uni<Void> deleteInstitutionOrAllUserInfo(ReactivePanacheMongoEntityBase entityBase, UserInstitution userInstitution) {
Expand All @@ -45,13 +60,9 @@ private Uni<Void> deleteInstitutionOrAllUserInfo(ReactivePanacheMongoEntityBase
if (userInfo.getInstitutions().stream()
.anyMatch(userInstitutionRole -> userInstitutionRole.getInstitutionId().equalsIgnoreCase(userInstitution.getInstitutionId()))) {

List<UserInstitutionRole> roleList = userInfo.getInstitutions().stream()
.filter(userInstitutionRole -> userInstitutionRole.getInstitutionId()
.equalsIgnoreCase(userInstitution.getInstitutionId())).toList();

userInfo.setInstitutions(roleList);
userInfo.getInstitutions().removeIf(userInstitutionRole -> userInstitutionRole.getInstitutionId().equalsIgnoreCase(userInstitution.getInstitutionId()));

if (roleList.isEmpty()) {
if (CollectionUtils.isEmpty(userInfo.getInstitutions())) {
return UserInfo.deleteById(userInstitution.getUserId()).replaceWith(Uni.createFrom().voidItem());
} else {
return UserInfo.persistOrUpdate(userInfo);
Expand Down
2 changes: 1 addition & 1 deletion apps/user-cdc/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ quarkus.mongodb.connection-string = ${MONGODB-CONNECTION-STRING}
quarkus.mongodb.database = selcUser

user-cdc.mongodb.watch.enabled=${USER-CDC-MONGODB-WATCH-ENABLED:false}
user-cdc.appinsights.connection-string=${APPLICATIONINSIGHTS_CONNECTION_STRING}
user-cdc.appinsights.connection-string=${APPLICATIONINSIGHTS_CONNECTION_STRING:InstrumentationKey=00000000-0000-0000-0000-000000000000}

user-cdc.retry.min-backoff=${USER-CDC-RETRY-MIN-BACKOFF:10}
user-cdc.retry.max-backoff=${USER-CDC-RETRY-MAX-BACKOFF:12}
Expand Down

0 comments on commit b43f683

Please sign in to comment.