Skip to content
Snippets Groups Projects
Commit 95cf97ea authored by OZGCloud's avatar OZGCloud
Browse files

prj-42 create nachricht draft and save it

parent 99569cb6
Branches
Tags
No related merge requests found
Showing
with 166 additions and 41 deletions
...@@ -39,6 +39,10 @@ ...@@ -39,6 +39,10 @@
<artifactId>kop-common-lib</artifactId> <artifactId>kop-common-lib</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency> <dependency>
<groupId>net.devh</groupId> <groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId> <artifactId>grpc-client-spring-boot-starter</artifactId>
......
...@@ -14,12 +14,15 @@ public class Bescheid { ...@@ -14,12 +14,15 @@ public class Bescheid {
private VorgangId vorgangId; private VorgangId vorgangId;
private String bescheidFileName; private boolean genehmigt;
private UserId createdBy;
private String bescheidFileName;
private File bescheidFile; private File bescheidFile;
@With @With
private FileId bescheidFileId; private FileId bescheidFileId;
private String contentType; private String contentType;
private long size; private long size;
} }
package de.ozgcloud.bescheid; package de.ozgcloud.bescheid;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.function.Predicate; import java.util.function.Predicate;
...@@ -58,6 +57,25 @@ class BescheidEventListener { ...@@ -58,6 +57,25 @@ class BescheidEventListener {
} }
} }
public void doCreateBescheidBiz(@NonNull Command command) {
var bescheid = service.createBescheid(createRequest(command));
bescheid = fileService.uploadBescheidFile(bescheid);
nachrichtService.createNachrichtDraft(bescheid);
}
private BescheidRequest createRequest(Command command) {
var eventBody = command.getBodyObject();
var builder = BescheidRequest.builder();
Optional.ofNullable(eventBody.get(VORGANG_ID_BODYKEY)).map(String.class::cast).map(VorgangId::from).ifPresent(builder::vorgangId);
Optional.ofNullable(eventBody.get(BESCHEID_VOM_BODYKEY)).map(LocalDate.class::cast).ifPresent(builder::bescheidVom);
Optional.ofNullable(eventBody.get(GENEHMIGT_BODYKEY)).map(Boolean.class::cast).ifPresent(builder::genehmigt);
builder.createFor(UserId.from(command.getCreatedBy()));
return builder.build();
}
private String buildErrorMessage(Exception e) { private String buildErrorMessage(Exception e) {
try { try {
StringBuilder sb = new StringBuilder(ERROR_MESSAGE); StringBuilder sb = new StringBuilder(ERROR_MESSAGE);
...@@ -76,22 +94,4 @@ class BescheidEventListener { ...@@ -76,22 +94,4 @@ class BescheidEventListener {
return ERROR_MESSAGE; return ERROR_MESSAGE;
} }
} }
public void doCreateBescheidBiz(@NonNull Command command) {
// TODO handle exception -> set command to error
var bescheid = service.createBescheid(createRequest(command.getBodyObject()));
bescheid = fileService.uploadBescheidFile(bescheid);
nachrichtService.createNachrichtDraft(bescheid);
}
private BescheidRequest createRequest(Map<String, Object> eventBody) {
var builder = BescheidRequest.builder();
Optional.ofNullable(eventBody.get(VORGANG_ID_BODYKEY)).map(String.class::cast).map(VorgangId::from).ifPresent(builder::vorgangId);
Optional.ofNullable(eventBody.get(BESCHEID_VOM_BODYKEY)).map(LocalDate.class::cast).ifPresent(builder::bescheidVom);
Optional.ofNullable(eventBody.get(GENEHMIGT_BODYKEY)).map(Boolean.class::cast).ifPresent(builder::genehmigt);
return builder.build();
}
} }
...@@ -14,4 +14,6 @@ public class BescheidRequest { ...@@ -14,4 +14,6 @@ public class BescheidRequest {
private LocalDate bescheidVom; private LocalDate bescheidVom;
private boolean genehmigt; private boolean genehmigt;
private UserId createFor;
} }
package de.ozgcloud.bescheid; package de.ozgcloud.bescheid;
import java.util.Objects;
import de.itvsh.kop.common.datatype.StringBasedValue; import de.itvsh.kop.common.datatype.StringBasedValue;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
...@@ -11,6 +13,9 @@ public class UserId extends StringBasedValue { ...@@ -11,6 +13,9 @@ public class UserId extends StringBasedValue {
} }
public static UserId from(String userId) { public static UserId from(String userId) {
if (Objects.nonNull(userId)) {
return new UserId(userId); return new UserId(userId);
} }
return null;
}
} }
...@@ -10,6 +10,8 @@ import java.util.concurrent.ExecutionException; ...@@ -10,6 +10,8 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import org.springframework.stereotype.Service;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import de.itvsh.kop.common.binaryfile.FileId; import de.itvsh.kop.common.binaryfile.FileId;
...@@ -25,6 +27,7 @@ import io.grpc.stub.CallStreamObserver; ...@@ -25,6 +27,7 @@ import io.grpc.stub.CallStreamObserver;
import io.grpc.stub.StreamObserver; import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.client.inject.GrpcClient; import net.devh.boot.grpc.client.inject.GrpcClient;
@Service
class BinaryFileRemoteService { class BinaryFileRemoteService {
private static final String CALL_CONTEXT_CLIENT = "bescheid-manager"; private static final String CALL_CONTEXT_CLIENT = "bescheid-manager";
...@@ -57,7 +60,7 @@ class BinaryFileRemoteService { ...@@ -57,7 +60,7 @@ class BinaryFileRemoteService {
private GrpcUploadBinaryFileRequest buildMetaDataRequest(Bescheid bescheid) { private GrpcUploadBinaryFileRequest buildMetaDataRequest(Bescheid bescheid) {
return GrpcUploadBinaryFileRequest.newBuilder() return GrpcUploadBinaryFileRequest.newBuilder()
.setMetadata(GrpcUploadBinaryFileMetaData.newBuilder() .setMetadata(GrpcUploadBinaryFileMetaData.newBuilder()
// TODO remove context - check wy needed! // TODO remove context - check why needed!
.setContext(GrpcCallContext.newBuilder().setClient(CALL_CONTEXT_CLIENT).build()) .setContext(GrpcCallContext.newBuilder().setClient(CALL_CONTEXT_CLIENT).build())
.setVorgangId(bescheid.getVorgangId().toString()) .setVorgangId(bescheid.getVorgangId().toString())
.setField(VORGANG_ATTACHMENT_FIELD) .setField(VORGANG_ATTACHMENT_FIELD)
......
package de.ozgcloud.bescheid.binaryfile; package de.ozgcloud.bescheid.binaryfile;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import de.ozgcloud.bescheid.Bescheid; import de.ozgcloud.bescheid.Bescheid;
@Service
public class BinaryFileService { public class BinaryFileService {
@Autowired @Autowired
......
...@@ -24,6 +24,7 @@ class DummyBescheidRemoteService implements BescheidRemoteService { ...@@ -24,6 +24,7 @@ class DummyBescheidRemoteService implements BescheidRemoteService {
.bescheidFile(file) .bescheidFile(file)
.contentType(DUMMY_BESCHEID_CONTENT_TYPE) .contentType(DUMMY_BESCHEID_CONTENT_TYPE)
.size(file.length()) .size(file.length())
.createdBy(request.getCreateFor())
.build(); .build();
} }
......
package de.ozgcloud.bescheid.nachricht; package de.ozgcloud.bescheid.nachricht;
import java.io.File; import de.itvsh.kop.common.binaryfile.FileId;
import de.ozgcloud.bescheid.UserId; import de.ozgcloud.bescheid.UserId;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
...@@ -18,8 +17,7 @@ public class Nachricht { ...@@ -18,8 +17,7 @@ public class Nachricht {
@NonNull @NonNull
private String mailBody; private String mailBody;
private File bescheid; private FileId bescheidFileId;
private String bescheidFileId;
@NonNull @NonNull
private UserId createdBy; private UserId createdBy;
......
...@@ -5,6 +5,7 @@ import org.mapstruct.Mapper; ...@@ -5,6 +5,7 @@ import org.mapstruct.Mapper;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.NullValueCheckStrategy; import org.mapstruct.NullValueCheckStrategy;
import de.itvsh.kop.common.binaryfile.FileId;
import de.itvsh.ozg.mail.postfach.GrpcPostfachNachricht; import de.itvsh.ozg.mail.postfach.GrpcPostfachNachricht;
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED, // @Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED, //
...@@ -27,4 +28,7 @@ public interface NachrichtMapper { ...@@ -27,4 +28,7 @@ public interface NachrichtMapper {
@Mapping(target = "replyOption", constant = "FORBIDDEN") @Mapping(target = "replyOption", constant = "FORBIDDEN")
GrpcPostfachNachricht mapToGrpc(Nachricht nachricht); GrpcPostfachNachricht mapToGrpc(Nachricht nachricht);
default String mapToSTring(FileId fileId) {
return fileId.toString();
}
} }
package de.ozgcloud.bescheid.nachricht; package de.ozgcloud.bescheid.nachricht;
import java.io.IOException;
import java.io.StringWriter;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import de.itvsh.kop.common.errorhandling.TechnicalException;
import de.ozgcloud.bescheid.Bescheid; import de.ozgcloud.bescheid.Bescheid;
import de.ozgcloud.bescheid.UserId; import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
@Service @Service
public class NachrichtService { public class NachrichtService {
...@@ -12,8 +18,13 @@ public class NachrichtService { ...@@ -12,8 +18,13 @@ public class NachrichtService {
@Autowired @Autowired
private NachrichtRemoteService remoteService; private NachrichtRemoteService remoteService;
@Autowired
private Configuration freemarkerCfg;
static final String SUBJECT = "Ihr Antrag"; static final String SUBJECT = "Ihr Antrag";
private static final String TEMPLATE_FILE = "bescheid.nachrichtTemplate.txt.ftlh";
public void createNachrichtDraft(Bescheid bescheid) { public void createNachrichtDraft(Bescheid bescheid) {
remoteService.saveDraft(buildNachricht(bescheid)); remoteService.saveDraft(buildNachricht(bescheid));
} }
...@@ -21,13 +32,33 @@ public class NachrichtService { ...@@ -21,13 +32,33 @@ public class NachrichtService {
Nachricht buildNachricht(Bescheid bescheid) { Nachricht buildNachricht(Bescheid bescheid) {
return Nachricht.builder() return Nachricht.builder()
.subject(SUBJECT) .subject(SUBJECT)
.mailBody(buildMessage()) .mailBody(buildMessage(bescheid))
.createdBy(UserId.from("TODO")) .createdBy(bescheid.getCreatedBy())
.bescheidFileId(bescheid.getBescheidFileId())
.build(); .build();
} }
String buildMessage() { String buildMessage(Bescheid bescheid) {
// TODO return fillTemplate(TEMPLATE_FILE, bescheid);
return "eine Nachricht"; }
private String fillTemplate(String templateName, Object dataModel) {
try {
Template template = getTemplate(templateName);
StringWriter stringWriter = new StringWriter();
template.process(dataModel, stringWriter);
return stringWriter.toString();
} catch (IOException | TemplateException e) {
throw new TechnicalException("Error filling template", e);
}
}
Template getTemplate(String templateName) {
try {
return freemarkerCfg.getTemplate(templateName);
} catch (IOException e) {
throw new TechnicalException("Error loading mail template", e);
}
} }
} }
Sehr geehrte/r Antragstellende/r,
ihr Antrag wurde <#if genehmigt>genehmigt<#else>abgelehnt</#if>.
Sie können innerhalb von vier Wochen Einspruch einlegen.
Mit freundlichen Grüßen,
Ihre Verwaltung
\ No newline at end of file
...@@ -24,7 +24,7 @@ class BescheidEventListenerITCase { ...@@ -24,7 +24,7 @@ class BescheidEventListenerITCase {
private BescheidService service; private BescheidService service;
@Nested @Nested
class TestCreateBesched { class TestCreateBescheid {
private Command command = CommandTestFactory.createBuilder().order(BescheidEventListener.ORDER).build(); private Command command = CommandTestFactory.createBuilder().order(BescheidEventListener.ORDER).build();
......
...@@ -2,6 +2,7 @@ package de.ozgcloud.bescheid; ...@@ -2,6 +2,7 @@ package de.ozgcloud.bescheid;
import java.time.LocalDate; import java.time.LocalDate;
import de.itvsh.ozg.pluto.command.CommandTestFactory;
import de.ozgcloud.bescheid.vorgang.VorgangId; import de.ozgcloud.bescheid.vorgang.VorgangId;
import de.ozgcloud.bescheid.vorgang.VorgangTestFactory; import de.ozgcloud.bescheid.vorgang.VorgangTestFactory;
...@@ -20,7 +21,8 @@ public class BescheidRequestTestFactory { ...@@ -20,7 +21,8 @@ public class BescheidRequestTestFactory {
return BescheidRequest.builder() return BescheidRequest.builder()
.vorgangId(VORGANG_ID) .vorgangId(VORGANG_ID)
.bescheidVom(BESCHEID_VOM) .bescheidVom(BESCHEID_VOM)
.genehmigt(GENEHMIGT); .genehmigt(GENEHMIGT)
.createFor(UserId.from(CommandTestFactory.CREATED_BY));
} }
} }
package de.ozgcloud.bescheid.nachricht;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import de.itvsh.kop.common.test.ITCase;
import de.ozgcloud.bescheid.BescheidTestFactory;
@ITCase
class NachrichtServiceITCase {
@Autowired
private NachrichtService service;
@Nested
class TestBuildMessage {
@Test
void shouldBuildMessage() {
var message = service.buildMessage(BescheidTestFactory.create());
assertThat(message).isNotBlank();
}
@Test
void shouldBeGenehmigt() {
var message = service.buildMessage(BescheidTestFactory.createBuilder().genehmigt(true).build());
assertThat(message).contains("genehmigt").doesNotContain("abgelehnt");
}
@Test
void shouldBeAbgelehnt() {
var message = service.buildMessage(BescheidTestFactory.createBuilder().genehmigt(false).build());
assertThat(message).contains("abgelehnt").doesNotContain("genehmigt");
}
}
}
...@@ -35,16 +35,11 @@ class NachrichtServiceTest { ...@@ -35,16 +35,11 @@ class NachrichtServiceTest {
verify(remoteService).saveDraft(nachricht); verify(remoteService).saveDraft(nachricht);
} }
@Test
void shouldSaveBescheid() {
}
@Nested @Nested
class BuildNachricht { class BuildNachricht {
@Test @Test
void shouldFillMailBody() { void shouldFillMailBody() {
doReturn(NachrichtTestFactory.MAIL_BODY).when(service).buildMessage(); doReturn(NachrichtTestFactory.MAIL_BODY).when(service).buildMessage(any());
var nachricht = service.buildNachricht(BescheidTestFactory.create()); var nachricht = service.buildNachricht(BescheidTestFactory.create());
...@@ -58,6 +53,22 @@ class NachrichtServiceTest { ...@@ -58,6 +53,22 @@ class NachrichtServiceTest {
assertThat(nachricht.getSubject()).isEqualTo(NachrichtService.SUBJECT); assertThat(nachricht.getSubject()).isEqualTo(NachrichtService.SUBJECT);
} }
@Test
void shouldSetUser() {
var nachricht = service.buildNachricht(BescheidTestFactory.create());
assertThat(nachricht.getCreatedBy()).isEqualTo(CallContextUserTestFactory.create());
}
}
@Nested
class BuildMessage {
@Test
void shouldBuildText() {
var message = service.buildMessage(BescheidTestFactory.create());
assertThat(message).isNotBlank();
}
} }
} }
......
logging:
level:
ROOT: WARN,
'[org.springframework]': ERROR
'[de.itvsh]': WARN
config: classpath:log4j2-local.xml
\ No newline at end of file
...@@ -8,6 +8,8 @@ public class CommandTestFactory { ...@@ -8,6 +8,8 @@ public class CommandTestFactory {
public static final String ID = UUID.randomUUID().toString(); public static final String ID = UUID.randomUUID().toString();
public static final String ORDER = "DO_TEST"; public static final String ORDER = "DO_TEST";
public static final String CREATED_BY = UUID.randomUUID().toString();
public static Command create() { public static Command create() {
return createBuilder().build(); return createBuilder().build();
} }
...@@ -16,6 +18,7 @@ public class CommandTestFactory { ...@@ -16,6 +18,7 @@ public class CommandTestFactory {
return TestCommand.builder() return TestCommand.builder()
.id(ID) .id(ID)
.body(Map.of()) .body(Map.of())
.bodyObject(Map.of()); .bodyObject(Map.of())
.createdBy(CREATED_BY);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment