Skip to content

Commit

Permalink
feat(tenant): Save log when application type changes
Browse files Browse the repository at this point in the history
  • Loading branch information
juliette-derancourt committed Dec 20, 2023
1 parent e9563b9 commit 87d5d07
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,14 @@ protected void linkEmailToTenants(Tenant tenantCreate, List<Pair<Tenant, String>
}
}

TenantModel saveStep(Tenant tenant, ApplicationType applicationType, List<Tenant> tenantToDelete, List<CoTenantForm> tenantToCreate) {
TenantModel saveStep(Tenant tenant, ApplicationType newApplicationType, List<Tenant> tenantToDelete, List<CoTenantForm> tenantToCreate) {
ApartmentSharing apartmentSharing = tenant.getApartmentSharing();
apartmentSharing.setApplicationType(applicationType);
ApplicationType currentApplicationType = apartmentSharing.getApplicationType();
apartmentSharing.setApplicationType(newApplicationType);
apartmentSharingService.resetDossierPdfGenerated(apartmentSharing);

deleteCoTenants(tenantToDelete);
logService.saveApplicationTypeChangedLog(apartmentSharing.getTenants(), currentApplicationType, newApplicationType);
createCoTenants(tenant, tenantToCreate, apartmentSharing);

LocalDateTime now = LocalDateTime.now();
Expand Down Expand Up @@ -226,7 +228,7 @@ private void createCoTenants(Tenant tenantCreate, List<CoTenantForm> tenants, Ap
}

private void deleteCoTenants(List<Tenant> tenantToDelete) {
tenantToDelete.stream().forEach(t -> userService.deleteAccount(t));
tenantToDelete.forEach(userService::deleteAccount);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum LogType {
ACCOUNT_CREATED,
ACCOUNT_CREATED_VIA_KC,
ACCOUNT_LINK, // first connection on DF
APPLICATION_TYPE_CHANGED,
ACCOUNT_COMPLETED,
ACCOUNT_DENIED,
ACCOUNT_EDITED,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.dossierfacile.common.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import fr.dossierfacile.common.enums.ApplicationType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApplicationTypeChangeModel {

private ApplicationType oldType;
private ApplicationType newType;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fr.dossierfacile.common.repository.ApartmentSharingRepository;
import fr.dossierfacile.common.service.interfaces.ApartmentSharingCommonService;
import fr.dossierfacile.common.service.interfaces.FileStorageService;
import fr.dossierfacile.common.service.interfaces.LogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -20,8 +21,10 @@
@AllArgsConstructor
@Slf4j
public class ApartmentSharingCommonServiceImpl implements ApartmentSharingCommonService {
ApartmentSharingRepository apartmentSharingRepository;
FileStorageService fileStorageService;

private final ApartmentSharingRepository apartmentSharingRepository;
private final FileStorageService fileStorageService;
private final LogService logService;

@Override
@Transactional(propagation = Propagation.SUPPORTS)
Expand All @@ -43,7 +46,11 @@ public void resetDossierPdfGenerated(ApartmentSharing apartmentSharing) {
@Transactional(propagation = Propagation.SUPPORTS)
public void removeTenant(ApartmentSharing apartmentSharing, Tenant tenant) {
apartmentSharing.getTenants().remove(tenant);
apartmentSharing.setApplicationType((apartmentSharing.getNumberOfTenants() >= 2) ? ApplicationType.GROUP : ApplicationType.ALONE);

ApplicationType newApplicationType = (apartmentSharing.getNumberOfTenants() >= 2) ? ApplicationType.GROUP : ApplicationType.ALONE;
logService.saveApplicationTypeChangedLog(apartmentSharing.getTenants(), apartmentSharing.getApplicationType(), newApplicationType);
apartmentSharing.setApplicationType(newApplicationType);

resetDossierPdfGenerated(apartmentSharing);
apartmentSharingRepository.save(apartmentSharing);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import fr.dossierfacile.common.entity.Log;
import fr.dossierfacile.common.entity.Tenant;
import fr.dossierfacile.common.entity.UserApi;
import fr.dossierfacile.common.enums.ApplicationType;
import fr.dossierfacile.common.enums.LogType;
import fr.dossierfacile.common.mapper.DeletedTenantCommonMapper;
import fr.dossierfacile.common.model.ApplicationTypeChangeModel;
import fr.dossierfacile.common.model.EditedDocumentModel;
import fr.dossierfacile.common.model.EditionType;
import fr.dossierfacile.common.repository.LogRepository;
Expand All @@ -18,6 +20,7 @@
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
Expand All @@ -43,20 +46,14 @@ public void saveLog(LogType logType, Long tenantId) {

@Override
public void saveLogWithTenantData(LogType logType, Tenant tenant) {
String content = null;
try {
content = objectMapper.writeValueAsString(deletedTenantCommonMapper.toDeletedTenantModel(tenant));
} catch (Exception e) {
log.error("Cannot correclty record tenant information in tenant_log");
}
this.saveLog(
Log.builder()
.logType(logType)
.tenantId(tenant.getId())
.creationDateTime(LocalDateTime.now())
.userApis(tenant.getTenantsUserApi().stream()
.mapToLong(tenantUserApi -> tenantUserApi.getUserApi().getId()).toArray())
.jsonProfile(content)
.jsonProfile(writeAsString(deletedTenantCommonMapper.toDeletedTenantModel(tenant)))
.build()
);
}
Expand All @@ -67,20 +64,11 @@ public void saveDocumentEditedLog(Document document, Tenant editor, EditionType
.logType(LogType.ACCOUNT_EDITED)
.tenantId(editor.getId())
.creationDateTime(LocalDateTime.now())
.logDetails(writeDocumentEditionDetails(document, editor.getId(), editionType))
.logDetails(writeAsString(EditedDocumentModel.from(document, editionType)))
.build();
saveLog(log);
}

private String writeDocumentEditionDetails(Document document, Long tenantId, EditionType editionType) {
try {
return objectMapper.writeValueAsString(EditedDocumentModel.from(document, editionType));
} catch (JsonProcessingException e) {
log.error("Cannot write details of document edition from tenant {}", tenantId);
}
return null;
}

@Override
public void savePartnerAccessRevocationLog(Tenant tenant, UserApi userApi) {
Log log = Log.builder()
Expand All @@ -92,4 +80,29 @@ public void savePartnerAccessRevocationLog(Tenant tenant, UserApi userApi) {
saveLog(log);
}

@Override
public void saveApplicationTypeChangedLog(List<Tenant> tenants, ApplicationType oldType, ApplicationType newType) {
if (oldType == newType) {
return;
}
for (Tenant tenant : tenants) {
Log log = Log.builder()
.logType(LogType.APPLICATION_TYPE_CHANGED)
.tenantId(tenant.getId())
.creationDateTime(LocalDateTime.now())
.logDetails(writeAsString(new ApplicationTypeChangeModel(oldType, newType)))
.build();
saveLog(log);
}
}

private String writeAsString(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
log.error("Cannot write log details as string");
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import fr.dossierfacile.common.entity.Document;
import fr.dossierfacile.common.entity.Tenant;
import fr.dossierfacile.common.entity.UserApi;
import fr.dossierfacile.common.enums.ApplicationType;
import fr.dossierfacile.common.enums.LogType;
import fr.dossierfacile.common.model.EditionType;

import java.util.List;

public interface LogService {

void saveLog(LogType logType, Long tenantId);
Expand All @@ -16,4 +19,6 @@ public interface LogService {

void savePartnerAccessRevocationLog(Tenant tenant, UserApi userApi);

void saveApplicationTypeChangedLog(List<Tenant> tenants, ApplicationType oldType, ApplicationType newType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.util.List;

import static fr.dossierfacile.common.enums.ApplicationType.ALONE;
import static fr.dossierfacile.common.enums.ApplicationType.GROUP;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -69,6 +73,17 @@ void should_save_revocation_log() {
assertThat(savedLog.getUserApis()).containsExactly(2L);
}

@Test
void should_application_type_change_log() {
logService.saveApplicationTypeChangedLog(List.of(tenantWithId(1L)), ALONE, GROUP);

Log savedLog = getSavedLog();
assertThat(savedLog.getLogType()).isEqualTo(LogType.APPLICATION_TYPE_CHANGED);
assertThat(savedLog.getTenantId()).isEqualTo(1L);
assertThat(savedLog.getLogDetails()).isEqualTo("""
{"oldType":"ALONE","newType":"GROUP"}""");
}

private static Tenant tenantWithId(long id) {
return Tenant.builder().id(id).build();
}
Expand Down

0 comments on commit 87d5d07

Please sign in to comment.