Skip to content

Commit

Permalink
Merge branch 'release/2024.04.19'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien committed Apr 19, 2024
2 parents 3ae2668 + 32a41e3 commit 251ed17
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.dossierfacile.api.front.register.form;

import fr.dossierfacile.common.enums.DocumentSubCategory;

public interface IDocumentFinancialForm {
DocumentSubCategory getTypeDocumentFinancial();

Integer getMonthlySum();
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package fr.dossierfacile.api.front.register.form.guarantor.natural_person;

import fr.dossierfacile.api.front.register.form.IDocumentFinancialForm;
import fr.dossierfacile.api.front.register.form.guarantor.DocumentGuarantorFormAbstract;
import fr.dossierfacile.api.front.validator.anotation.DocumentSubcategorySubset;
import fr.dossierfacile.api.front.validator.anotation.LengthOfText;
import fr.dossierfacile.api.front.validator.anotation.NumberOfPages;
import fr.dossierfacile.api.front.validator.anotation.guarantor.natural_person.financial.NoDocumentCustomTextFinancialGuarantorNaturalPerson;
import fr.dossierfacile.api.front.validator.anotation.guarantor.natural_person.financial.NumberOfDocumentFinancialGuarantorNaturalPerson;
import fr.dossierfacile.api.front.validator.anotation.tenant.financial.MonthlySumValue;
import fr.dossierfacile.common.enums.DocumentCategory;
import fr.dossierfacile.common.enums.DocumentSubCategory;
import fr.dossierfacile.common.enums.TypeGuarantor;
Expand All @@ -28,7 +30,8 @@
@NoDocumentCustomTextFinancialGuarantorNaturalPerson
@NumberOfDocumentFinancialGuarantorNaturalPerson
@NumberOfPages(category = DocumentCategory.FINANCIAL, max = 50)
public class DocumentFinancialGuarantorNaturalPersonForm extends DocumentGuarantorFormAbstract {
@MonthlySumValue
public class DocumentFinancialGuarantorNaturalPersonForm extends DocumentGuarantorFormAbstract implements IDocumentFinancialForm {

private Long documentId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fr.dossierfacile.api.front.register.form.tenant;

import fr.dossierfacile.api.front.register.form.DocumentForm;
import fr.dossierfacile.api.front.register.form.IDocumentFinancialForm;
import fr.dossierfacile.api.front.validator.anotation.DocumentSubcategorySubset;
import fr.dossierfacile.api.front.validator.anotation.LengthOfText;
import fr.dossierfacile.api.front.validator.anotation.NumberOfPages;
import fr.dossierfacile.api.front.validator.anotation.tenant.financial.MonthlySumValue;
import fr.dossierfacile.api.front.validator.anotation.tenant.financial.NoDocumentCustomTextFinancial;
import fr.dossierfacile.api.front.validator.anotation.tenant.financial.NumberOfDocumentFinancial;
import fr.dossierfacile.common.enums.DocumentCategory;
Expand All @@ -29,7 +31,8 @@
@NoDocumentCustomTextFinancial
@NumberOfDocumentFinancial
@NumberOfPages(category = DocumentCategory.FINANCIAL, max = 50)
public class DocumentFinancialForm extends DocumentForm {
@MonthlySumValue
public class DocumentFinancialForm extends DocumentForm implements IDocumentFinancialForm {

@Parameter(description = "Identifiant du document est nécessaire poour les mises à jour")
private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ protected Document saveDocument(Tenant tenant, DocumentFinancialGuarantorNatural
document.setDocumentDeniedReasons(null);
document.setDocumentSubCategory(documentSubCategory);
document.setMonthlySum(documentFinancialGuarantorNaturalPersonForm.getMonthlySum());
if (documentFinancialGuarantorNaturalPersonForm.getTypeDocumentFinancial() != DocumentSubCategory.NO_INCOME &&
(document.getMonthlySum() == null || document.getMonthlySum() < 0)) {
throw new IllegalStateException("Montant mensuel ne peut être nul ou négatif");
}

if (document.getNoDocument() != null && !document.getNoDocument() && documentFinancialGuarantorNaturalPersonForm.getNoDocument()) {
deleteFilesIfExistedBefore(document);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class NameGuarantorNaturalPerson implements SaveStep<NameGuarantorNatural
@Override
@Transactional
public TenantModel saveStep(Tenant tenant, NameGuarantorNaturalPersonForm nameGuarantorNaturalPersonForm) {
tenant = tenantRepository.findOneById(tenant.getId());
Guarantor guarantor = guarantorRepository.findByTenantAndTypeGuarantorAndId(tenant, TypeGuarantor.NATURAL_PERSON, nameGuarantorNaturalPersonForm.getGuarantorId())
.orElseThrow(() -> new GuarantorNotFoundException(nameGuarantorNaturalPersonForm.getGuarantorId()));
guarantor.setFirstName(nameGuarantorNaturalPersonForm.getFirstName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ protected Document saveDocument(Tenant tenant, DocumentFinancialForm documentFin
&& documentFinancialForm.getTypeDocumentFinancial() != DocumentSubCategory.NO_INCOME) {
document.setMonthlySum(documentFinancialForm.getMonthlySum());
}
if (documentFinancialForm.getTypeDocumentFinancial() != DocumentSubCategory.NO_INCOME &&
(document.getMonthlySum() == null || document.getMonthlySum() < 0)) {
throw new IllegalStateException("Montant mensuel ne peut être nul ou négatif");
}

if (document.getNoDocument() != null && !document.getNoDocument() && documentFinancialForm.getNoDocument()) {
deleteFilesIfExistedBefore(document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private static List<Tenant> getTenantOrPartners(Tenant tenant) {
@Override
@Transactional
public TenantModel saveStep(Tenant tenant, HonorDeclarationForm honorDeclarationForm) {
tenant = tenantRepository.findOneById(tenant.getId());
tenant.setClarification(honorDeclarationForm.getClarification());
for (Tenant t : getTenantOrPartners(tenant)) {
checkTenantValidity(t);
Expand All @@ -55,6 +56,7 @@ public TenantModel saveStep(Tenant tenant, HonorDeclarationForm honorDeclaration

apartmentSharingService.resetDossierPdfGenerated(tenant.getApartmentSharing());
mailService.sendEmailAccountCompleted(tenantSaved);
tenant.getApartmentSharing().getTenants().size();// load tenants in tx context
return tenantMapper.toTenantModel(tenantSaved);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

Expand Down Expand Up @@ -61,7 +62,7 @@ public <T> TenantModel saveStepRegister(Tenant tenant, T formStep, StepRegister
public void updateLastLoginDateAndResetWarnings(Tenant tenantToUpdate) {
LocalDateTime currentTime = LocalDateTime.now();
for (Tenant tenant : tenantToUpdate.getApartmentSharing().getTenants()) {
if (tenant.getId() == tenantToUpdate.getId() || StringUtils.isBlank(tenant.getEmail())) {
if (Objects.equals(tenant.getId(), tenantToUpdate.getId()) || StringUtils.isBlank(tenant.getEmail())) {
tenant.setLastLoginDate(currentTime);
tenant.setWarnings(0);
if (tenant.getStatus() == TenantFileStatus.ARCHIVED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.dossierfacile.api.front.service.interfaces.ApartmentSharingService;
import fr.dossierfacile.api.front.service.interfaces.TenantStatusService;
import fr.dossierfacile.common.entity.Guarantor;
import fr.dossierfacile.common.entity.Tenant;
import fr.dossierfacile.common.enums.PartnerCallBackType;
import fr.dossierfacile.common.enums.TenantFileStatus;
Expand All @@ -10,6 +11,9 @@
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
Expand All @@ -20,9 +24,14 @@ public class TenantStatusServiceImpl implements TenantStatusService {
private final TenantCommonRepository tenantRepository;

@Override
@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.READ_COMMITTED)
public Tenant updateTenantStatus(Tenant tenant) {
TenantFileStatus previousStatus = tenant.getStatus();
log.info("Updating status of tenant with ID [" + tenant.getId() + "] to [" + tenant.getStatus() + "]");
// load guarantor to the tx context before computeStatus() call
if (tenant.getGuarantors() != null) {
tenant.getGuarantors().forEach(Guarantor::getDocuments);
}
tenant.setStatus(tenant.computeStatus());
tenant = tenantRepository.save(tenant);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fr.dossierfacile.api.front.validator.anotation.tenant.financial;

import fr.dossierfacile.api.front.validator.tenant.financial.MonthlySumValueValidator;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)

@Constraint(
validatedBy = {MonthlySumValueValidator.class}
)

public @interface MonthlySumValue {
String message() default "Montant mensuel doit être strictement positif";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fr.dossierfacile.api.front.validator.tenant.financial;

import fr.dossierfacile.api.front.register.form.IDocumentFinancialForm;
import fr.dossierfacile.api.front.validator.anotation.tenant.financial.MonthlySumValue;
import fr.dossierfacile.common.enums.DocumentSubCategory;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class MonthlySumValueValidator implements ConstraintValidator<MonthlySumValue, IDocumentFinancialForm> {
@Override
public void initialize(MonthlySumValue constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}

// Check monthlySum is strictly positive if not NO_INCOME financialType
@Override
public boolean isValid(IDocumentFinancialForm documentFinancialForm, ConstraintValidatorContext constraintValidatorContext) {
return documentFinancialForm.getTypeDocumentFinancial() == DocumentSubCategory.NO_INCOME
|| (documentFinancialForm.getMonthlySum() != null && documentFinancialForm.getMonthlySum() > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigInteger;
import java.security.Principal;
import java.util.List;

Expand Down Expand Up @@ -47,7 +46,7 @@ public String myDashboard(Model model, Principal principal) {
public String boUserDashboard(Model model) {
List<Object[]> listTreatedCountByOperator = logService.listDailyTreatedFilesByOperator();
int dailyCount = listTreatedCountByOperator.stream()
.mapToInt(objects -> ((BigInteger) objects[1]).intValue())
.mapToInt(objects -> ((Number) objects[1]).intValue())
.sum();

model.addAttribute("listTreatedCountByOperator", listTreatedCountByOperator);
Expand Down
29 changes: 16 additions & 13 deletions dossierfacile-bo/src/main/java/fr/gouv/bo/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package fr.gouv.bo.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import fr.dossierfacile.common.entity.ApartmentSharing;
import fr.dossierfacile.common.entity.BOUser;
import fr.dossierfacile.common.entity.Log;
import fr.dossierfacile.common.entity.PropertyApartmentSharing;
import fr.dossierfacile.common.entity.Tenant;
import fr.dossierfacile.common.entity.UserRole;
import fr.dossierfacile.common.enums.ApplicationType;
import fr.dossierfacile.common.enums.LogType;
import fr.dossierfacile.common.enums.PartnerCallBackType;
import fr.dossierfacile.common.enums.Role;
import fr.dossierfacile.common.enums.TenantType;
import fr.dossierfacile.common.entity.*;
import fr.dossierfacile.common.enums.*;
import fr.dossierfacile.common.repository.TenantCommonRepository;
import fr.dossierfacile.common.service.interfaces.PartnerCallBackService;
import fr.gouv.bo.mapper.TenantMapper;
Expand Down Expand Up @@ -49,6 +42,7 @@ public class UserService {
private final ApartmentSharingService apartmentSharingService;
private final PartnerCallBackService partnerCallBackService;
private final LogService logService;
private final ObjectMapper objectMapper;

public List<BOUser> findAll() {
return userRepository.findAll(Sort.by("email"));
Expand Down Expand Up @@ -93,7 +87,7 @@ private void saveAndDeleteInfoByTenant(Tenant tenant, BOUser operator) {
.logType(LogType.ACCOUNT_DELETE)
.tenantId(tenant.getId())
.operatorId(operator.getId())
.jsonProfile(tenantMapper.toTenantModel(tenant))
.jsonProfile(writeValueAsString(tenantMapper.toTenantModel(tenant)))
.creationDateTime(LocalDateTime.now())
.build());
}
Expand Down Expand Up @@ -127,7 +121,7 @@ public void deleteApartmentSharing(Tenant tenant, BOUser operator) {
apartmentSharing.getTenants().stream()
.filter(t -> t.getKeycloakId() != null)
.collect(Collectors.toList()));
apartmentSharing.getTenants().forEach( t -> this.saveAndDeleteInfoByTenant(t, operator));
apartmentSharing.getTenants().forEach(t -> this.saveAndDeleteInfoByTenant(t, operator));
apartmentSharingRepository.delete(apartmentSharing);
}

Expand Down Expand Up @@ -187,5 +181,14 @@ public void createUserByEmail(String email, Role role) {
BOUser user = BOUser.builder().email(email).build();
addRoles(userRepository.save(user), Collections.singletonList(role));
}

private String writeValueAsString(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
log.error("FATAL: Something very bad happened " + object.getClass(), e);
}
return null;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public TenantFileStatus computeStatus() {
List<Document> allDocuments = (guarantors == null) ?
documents :
Stream.concat(documents.stream(),
guarantors.parallelStream()
.map(g -> g.getDocuments())
guarantors.stream()
.map(Guarantor::getDocuments)
.flatMap(List::stream))
.collect(Collectors.toList());
.toList();
// Check documents status
if (allDocuments != null && allDocuments.stream().anyMatch(d -> d.getDocumentStatus() == DECLINED)) {
return TenantFileStatus.DECLINED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import fr.dossierfacile.common.model.log.EditionType;
import fr.dossierfacile.common.repository.LogRepository;
import fr.dossierfacile.common.service.interfaces.LogService;
import fr.dossierfacile.common.utils.MapperUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -30,7 +29,7 @@ public class LogServiceImpl implements LogService {

private final LogRepository repository;
private final DeletedTenantCommonMapper deletedTenantCommonMapper;
private final ObjectMapper objectMapper = MapperUtil.newObjectMapper();
private final ObjectMapper objectMapper;

private void saveLog(Log log) {
repository.save(log);
Expand Down Expand Up @@ -83,7 +82,7 @@ public void savePartnerAccessRevocationLog(Tenant tenant, UserApi userApi) {
.logType(LogType.PARTNER_ACCESS_REVOKED)
.tenantId(tenant.getId())
.creationDateTime(LocalDateTime.now())
.userApis(new long[] { userApi.getId() })
.userApis(new long[]{userApi.getId()})
.build();
saveLog(log);
}
Expand All @@ -108,7 +107,7 @@ private String writeAsString(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
log.error("Cannot write log details as string");
log.error("FATAL: Cannot write log details as string", e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package fr.dossierfacile.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

@UtilityClass
@Slf4j
public class MapperUtil {
public static ObjectMapper newObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.dossierfacile.common.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import fr.dossierfacile.common.entity.Document;
import fr.dossierfacile.common.entity.Guarantor;
import fr.dossierfacile.common.entity.Log;
Expand All @@ -26,7 +27,7 @@
class LogServiceImplTest {

private final LogRepository logRepository = mock(LogRepository.class);
private final LogService logService = new LogServiceImpl(logRepository, null);
private final LogService logService = new LogServiceImpl(logRepository, null, new ObjectMapper());

@Test
void should_save_edition_log_for_tenant_document() {
Expand Down

0 comments on commit 251ed17

Please sign in to comment.