Skip to content

Commit

Permalink
feat: Add details for document edition logs
Browse files Browse the repository at this point in the history
  • Loading branch information
juliette-derancourt committed Dec 1, 2023
1 parent b478b19 commit b3b7638
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import fr.dossierfacile.common.entity.Document;
import fr.dossierfacile.common.entity.DocumentPdfGenerationLog;
import fr.dossierfacile.common.entity.Tenant;
import fr.dossierfacile.common.enums.LogType;
import fr.dossierfacile.common.enums.PartnerCallBackType;
import fr.dossierfacile.common.enums.TenantFileStatus;
import fr.dossierfacile.common.repository.DocumentPdfGenerationLogRepository;
Expand Down Expand Up @@ -51,7 +50,7 @@ public TenantModel saveStep(Tenant tenant, T documentForm) {
}

Document document = saveDocument(tenant, documentForm);
logService.saveLog(LogType.ACCOUNT_EDITED, tenant.getId());
logService.saveDocumentEditedLog(document, tenant);

Long logId = documentPdfGenerationLogRepository.save(
DocumentPdfGenerationLog.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Document delete(Long id, Tenant tenant) {
document.getFiles().remove(file);
fileRepository.delete(file);

logService.saveLog(LogType.ACCOUNT_EDITED, tenant.getId());
logService.saveDocumentEditedLog(document, tenant);

if (document.getFiles().isEmpty()) {
documentService.delete(document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public class Log implements Serializable {
@Column(columnDefinition = "jsonb")
private Object jsonProfile;

@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private Object logDetails;

@Column
private Long messageId;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package fr.dossierfacile.common.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import fr.dossierfacile.common.entity.Document;
import fr.dossierfacile.common.entity.Guarantor;
import fr.dossierfacile.common.entity.Tenant;
import fr.dossierfacile.common.enums.DocumentCategory;
import fr.dossierfacile.common.enums.DocumentSubCategory;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Optional;

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

private DocumentCategory documentCategory;
private DocumentSubCategory documentSubCategory;
private Boolean noDocument;
private Long tenantId;
private Long guarantorId;

public static EditedDocumentModel from(Document document) {
return EditedDocumentModel.builder()
.documentCategory(document.getDocumentCategory())
.documentSubCategory(document.getDocumentSubCategory())
.noDocument(document.getNoDocument())
.tenantId(Optional.ofNullable(document.getTenant())
.map(Tenant::getId).orElse(null))
.guarantorId(Optional.ofNullable(document.getGuarantor())
.map(Guarantor::getId).orElse(null))
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package fr.dossierfacile.common.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import fr.dossierfacile.common.entity.Document;
import fr.dossierfacile.common.entity.Log;
import fr.dossierfacile.common.entity.Tenant;
import fr.dossierfacile.common.enums.LogType;
import fr.dossierfacile.common.mapper.DeletedTenantCommonMapper;
import fr.dossierfacile.common.model.EditedDocumentModel;
import fr.dossierfacile.common.repository.LogRepository;
import fr.dossierfacile.common.service.interfaces.LogService;
import lombok.AllArgsConstructor;
Expand All @@ -18,9 +21,9 @@
@Slf4j
@AllArgsConstructor
public class LogServiceImpl implements LogService {

private final LogRepository repository;
private final DeletedTenantCommonMapper deletedTenantCommonMapper;

private ObjectMapper objectMapper = new ObjectMapper();

{
Expand Down Expand Up @@ -56,4 +59,24 @@ public void saveLogWithTenantData(LogType logType, Tenant tenant) {
);
}

@Override
public void saveDocumentEditedLog(Document document, Tenant editor) {
Log log = Log.builder()
.logType(LogType.ACCOUNT_EDITED)
.tenantId(editor.getId())
.creationDateTime(LocalDateTime.now())
.logDetails(writeDocumentEditionDetails(document, editor.getId()))
.build();
saveLog(log);
}

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

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.dossierfacile.common.service.interfaces;

import fr.dossierfacile.common.entity.Document;
import fr.dossierfacile.common.entity.Tenant;
import fr.dossierfacile.common.enums.LogType;

Expand All @@ -8,4 +9,7 @@ public interface LogService {
void saveLog(LogType logType, Long tenantId);

void saveLogWithTenantData(LogType logType, Tenant tenant);

void saveDocumentEditedLog(Document document, Tenant editor);

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,6 @@
<include file="db/migration/202311080001-add-processed-documents-time-spent-column.xml" />
<include file="db/migration/202311090000-update-user-account-table.xml" />
<include file="db/migration/202311170000-update-tenant-table.xml" />
<include file="db/migration/202312010000-update-tenant-log-table.xml" />

</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd">
<changeSet id="202312010000-01" author="juliette">
<addColumn tableName="tenant_log">
<column name="log_details" type="jsonb"/>
</addColumn>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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;
import fr.dossierfacile.common.entity.Tenant;
import fr.dossierfacile.common.enums.DocumentCategory;
import fr.dossierfacile.common.enums.DocumentSubCategory;
import fr.dossierfacile.common.enums.LogType;
import fr.dossierfacile.common.repository.LogRepository;
import fr.dossierfacile.common.service.interfaces.LogService;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

class LogServiceImplTest {

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

@Test
void should_save_edition_log_for_tenant_document() {
Document document = Document.builder()
.documentCategory(DocumentCategory.IDENTIFICATION)
.documentSubCategory(DocumentSubCategory.FRENCH_IDENTITY_CARD)
.tenant(tenantWithId(2L))
.build();

logService.saveDocumentEditedLog(document, tenantWithId(1L));

Log savedLog = getSavedLog();
assertThat(savedLog.getLogType()).isEqualTo(LogType.ACCOUNT_EDITED);
assertThat(savedLog.getTenantId()).isEqualTo(1L);
assertThat(savedLog.getLogDetails()).isEqualTo("""
{"documentCategory":"IDENTIFICATION","documentSubCategory":"FRENCH_IDENTITY_CARD","tenantId":2}""");
}

@Test
void should_save_edition_log_for_guarantor_document() {
Document document = Document.builder()
.documentCategory(DocumentCategory.FINANCIAL)
.documentSubCategory(DocumentSubCategory.SALARY)
.noDocument(true)
.guarantor(Guarantor.builder().id(3L).build())
.build();

logService.saveDocumentEditedLog(document, tenantWithId(2L));

Log savedLog = getSavedLog();
assertThat(savedLog.getLogType()).isEqualTo(LogType.ACCOUNT_EDITED);
assertThat(savedLog.getTenantId()).isEqualTo(2L);
assertThat(savedLog.getLogDetails()).isEqualTo("""
{"documentCategory":"FINANCIAL","documentSubCategory":"SALARY","noDocument":true,"guarantorId":3}""");
}

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

private Log getSavedLog() {
ArgumentCaptor<Log> logCaptor = ArgumentCaptor.forClass(Log.class);
verify(logRepository, times(1)).save(logCaptor.capture());
return logCaptor.getAllValues().get(0);
}

}

0 comments on commit b3b7638

Please sign in to comment.