Skip to content

Commit

Permalink
fix: temp fix failure on notification at the end of op treatment
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien authored and fabiengo committed Jun 13, 2024
1 parent 4b934b6 commit 4645443
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,21 @@ public void forgotPassword(String email) {
PasswordRecoveryToken passwordRecoveryToken = passwordRecoveryTokenService.create(tenant);
mailService.sendEmailNewPassword(tenant, passwordRecoveryToken);
}

private List<TenantUserApi> groupingAllTenantUserApisInTheApartment(ApartmentSharing as) {
List<TenantUserApi> tenantUserApis = new ArrayList<>();
if (as.getTenants() != null && !as.getTenants().isEmpty()) {
as.getTenants().stream()
.filter(t -> t.getTenantsUserApi() != null && !t.getTenantsUserApi().isEmpty())
.forEach(t -> tenantUserApis.addAll(t.getTenantsUserApi()));
}
return tenantUserApis;
}
@Override
@Transactional
public void deleteAccount(Tenant tenant) {
List<WebhookDTO> webhookDTOList = new ArrayList<>();
ApartmentSharing apartmentSharing = tenant.getApartmentSharing();
apartmentSharing.groupingAllTenantUserApisInTheApartment().forEach((tenantUserApi) -> {
groupingAllTenantUserApisInTheApartment(apartmentSharing).forEach((tenantUserApi) -> {
UserApi userApi = tenantUserApi.getUserApi();
webhookDTOList.add(partnerCallBackService.getWebhookDTO(tenant, userApi, PartnerCallBackType.DELETED_ACCOUNT));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,18 +519,25 @@ private void changeTenantStatusToValidated(Tenant tenant, User operator, Process
operatorLogRepository.save(new OperatorLog(tenant, operator, tenant.getStatus(), ActionOperatorType.STOP_PROCESS, processedDocuments.count(), processedDocuments.timeSpent()));

TransactionalUtil.afterCommit(() -> {
if (tenant.getApartmentSharing().getApplicationType() == ApplicationType.GROUP) {
mailService.sendEmailToTenantAfterValidateAllDocumentsOfTenant(tenant);
} else {
if (tenant.getApartmentSharing().getTenants().stream()
.allMatch(t -> t.getStatus() == TenantFileStatus.VALIDATED)) {
tenant.getApartmentSharing().getTenants().stream()
.filter(t -> isNotBlank(t.getEmail()))
.forEach(t -> mailService.sendEmailToTenantAfterValidateAllDocuments(t));
try {
if (tenant.getApartmentSharing().getApplicationType() == ApplicationType.GROUP) {
mailService.sendEmailToTenantAfterValidateAllDocumentsOfTenant(tenant);
} else {
if (tenant.getApartmentSharing().getTenants().stream()
.allMatch(t -> t.getStatus() == TenantFileStatus.VALIDATED)) {
tenant.getApartmentSharing().getTenants().stream()
.filter(t -> isNotBlank(t.getEmail()))
.forEach(t -> mailService.sendEmailToTenantAfterValidateAllDocuments(t));
}
}
} catch (Exception e) {
log.error("CAUTION Unable to send notification to user ");
}
try {
partnerCallBackService.sendCallBack(tenant, PartnerCallBackType.VERIFIED_ACCOUNT);
} catch (Exception e) {
log.error("CAUTION Unable to send notification to partner");
}

partnerCallBackService.sendCallBack(tenant, PartnerCallBackType.VERIFIED_ACCOUNT);
});

}
Expand All @@ -544,6 +551,20 @@ private void changeTenantStatusToDeclined(Tenant tenant, User operator, Message
tenant, operator, tenant.getStatus(), ActionOperatorType.STOP_PROCESS, processedDocuments.count(), processedDocuments.timeSpent()
));

// TODO This part should be here to load tenant futur tenant data Please use DTO in AfterCommit INSTEAD !
// This part should treat sendMail and sendCallback
if (tenant.getApartmentSharing().getApplicationType() == ApplicationType.COUPLE) {
tenant.getApartmentSharing().getTenants().stream()
.filter(t -> isNotBlank(t.getEmail()))
.forEach(t -> {
if (t.isBelongToPartner())
t.getTenantsUserApi().get(0).getUserApi();
});
} else {
if (tenant.isBelongToPartner())
tenant.getTenantsUserApi().get(0).getUserApi();
}

TransactionalUtil.afterCommit(() -> {
if (tenant.getApartmentSharing().getApplicationType() == ApplicationType.COUPLE) {
tenant.getApartmentSharing().getTenants().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ public Integer totalSalary() {
return totalSalary;
}

public List<TenantUserApi> groupingAllTenantUserApisInTheApartment() {
List<TenantUserApi> tenantUserApis = new ArrayList<>();
if (tenants != null && !tenants.isEmpty()) {
tenants.stream()
.filter(t -> t.getTenantsUserApi() != null && !t.getTenantsUserApi().isEmpty())
.forEach(t -> tenantUserApis.addAll(t.getTenantsUserApi()));
}
return tenantUserApis;
}

public Integer totalGuarantorSalary() {
int totalSalary = 0;
for (Tenant tenant : tenants) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -66,19 +63,27 @@ public void registerTenant(String internalPartnerId, Tenant tenant, UserApi user
}
}
}

private List<TenantUserApi> groupingAllTenantUserApisInTheApartment(ApartmentSharing as) {
List<TenantUserApi> tenantUserApis = new ArrayList<>();
if (as.getTenants() != null && !as.getTenants().isEmpty()) {
as.getTenants().stream()
.filter(t -> t.getTenantsUserApi() != null && !t.getTenantsUserApi().isEmpty())
.forEach(t -> tenantUserApis.addAll(t.getTenantsUserApi()));
}
return tenantUserApis;
}
// TODO send callback should be transactionnal or have DTO
public void sendCallBack(Tenant tenant, PartnerCallBackType partnerCallBackType) {
Optional<ApartmentSharing> apartmentSharing = apartmentSharingRepository.findByTenant(tenant.getId());
if (apartmentSharing.isEmpty()) {
return;
}
apartmentSharing.get().groupingAllTenantUserApisInTheApartment().forEach(tenantUserApi -> {
groupingAllTenantUserApisInTheApartment(apartmentSharing.get()).forEach(tenantUserApi -> {
UserApi userApi = tenantUserApi.getUserApi();
WebhookDTO webhookDTO = getWebhookDTO(tenant, userApi, partnerCallBackType);
sendCallBack(tenant, webhookDTO);
});
}

@Override
public void sendCallBack(List<Tenant> tenantList, PartnerCallBackType partnerCallBackType) {
if (tenantList != null && !tenantList.isEmpty()) {
Expand Down

0 comments on commit 4645443

Please sign in to comment.