Skip to content

Commit

Permalink
Merge pull request #104 from Onlineberatung/OB-7136-fix-take-mail-sub…
Browse files Browse the repository at this point in the history
…jects-from-weblate

take mail subjects from weblate
  • Loading branch information
tkuzynow authored Dec 22, 2023
2 parents a8084a0 + 38b26f4 commit 1bdd4f9
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.caritas.cob.mailservice.api.mailtemplate;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
public class SubjectDescription {

String key;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,14 @@
public class TemplateDescription {

private Map<LanguageCode, String> htmlTemplateFilename;
private Map<LanguageCode, String> subject;
private SubjectDescription subject;
private List<String> templateDataFields;
private List<TemplateImage> templateImages;

private static LanguageCode defaultLanguage() {
return new MailDTO().getLanguage();
}

public String getSubjectOrFallback(LanguageCode language) {
return subject.containsKey(language)
? subject.get(language)
: subject.get(defaultLanguage());
}

public String getTemplateFilenameOrFallback(LanguageCode language) {
return htmlTemplateFilename.containsKey(language)
? htmlTemplateFilename.get(language)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Optional;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringSubstitutor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -22,6 +23,7 @@
**/
@Service
@RequiredArgsConstructor
@Slf4j
public class TemplateService {

@Value("${app.imprint.url}")
Expand Down Expand Up @@ -85,9 +87,28 @@ private Map<String, String> getTranslationMapAndDefaultToGermanIfNotFound(Langua
public String getRenderedSubject(TemplateDescription templateDescription,
Map<String, Object> templateData, LanguageCode languageCode) {
StringSubstitutor stringSubstitutor = new StringSubstitutor(templateData, "${", "}");
var subject = templateDescription.getSubjectOrFallback(languageCode);
var subjectKey = templateDescription.getSubject().getKey();
if (subjectKey == null) {
log.warn("Subject key is null for template {}", templateDescription.getHtmlTemplateFilename().get(LanguageCode.DE));
return tryRenderDefaultSubject(stringSubstitutor);
}
return getRenderedSubjectForSubjectKeyNotNull(languageCode, stringSubstitutor, subjectKey);
}

return stringSubstitutor.replace(subject);
private String tryRenderDefaultSubject(StringSubstitutor stringSubstitutor) {
return stringSubstitutor.replace("${subject}");
}

private String getRenderedSubjectForSubjectKeyNotNull(LanguageCode languageCode, StringSubstitutor stringSubstitutor,
String subjectKey) {
var translationKeys = getTranslationMapAndDefaultToGermanIfNotFound(languageCode);
String subject = translationKeys.get(subjectKey);
if (subject != null) {
return stringSubstitutor.replace(subject);
} else {
log.warn("Subject key {} not found in translations keys", subjectKey);
return tryRenderDefaultSubject(stringSubstitutor);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ private Map<String, String> fetchTranslationAsMap(String languageCode) throws Js
return mapper.readValue(translations, Map.class);
}

@Cacheable(value = "optionalTranslations")
public Optional<Map<String, String>> tryFetchTranslations(String languageCode) {

try {
var result = fetchTranslationAsMap(languageCode);
return Optional.of(result);
return result.isEmpty() ? Optional.empty() : Optional.of(result);
} catch (JsonProcessingException e) {
log.warn("Error while processing json file with translations. Returning empty translations", e);
return Optional.empty();
}

}

private String fetchTranslationsAsString(String languageCode) {
Expand All @@ -91,8 +91,6 @@ private String fetchTranslationsAsString(String languageCode) {
}
}



private String fetchDefaultTranslations(String translationComponentName, String languageCode) {
var inputStream = TranslationService.class.getResourceAsStream(
getTranslationFilename(translationComponentName + "." + languageCode));
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
http://www.ehcache.org/v3
http://www.ehcache.org/schema/ehcache-core-3.7.xsd">

<cache alias="optionalTranslations">
<expiry>
<ttl unit="seconds">60</ttl>
</expiry>

<resources>
<offheap unit="MB">10</offheap>
</resources>
</cache>

<cache alias="translations">
<expiry>
<ttl unit="seconds">60</ttl>
Expand Down
12 changes: 11 additions & 1 deletion src/main/resources/i18n/mailservice.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@
"mail.label.you.have.a.new.message.in.your.counselings" : "Sie haben eine neue Nachricht in Ihren Beratungen",
"mail.label.your.consultant.wants.to.assign.you.to.new.consultant": "Ihr(e) Berater(in) möchte Sie an eine(n) neue(n)\n Berater(in) übergeben. Dafür brauchen wir Ihre Zustimmung.",
"mail.label.imprint": "Impressum",
"mail.label.privacy": "Datenschutz"
"mail.label.privacy": "Datenschutz",
"mail.subject.assign.enquiry.notification": "Neue_r Ratsuchende_r",
"mail.subject.daily.enquiry.notification": "${subject}",
"mail.subject.enquiry.notification.consultant": "Neue Erstanfrage [PLZ: ${plz}]",
"mail.subject.feedback.message.notification": "Neue Feedback-Nachricht",
"mail.subject.free.text": "${subject}",
"mail.subject.message.notification.asker": "Antwort von ${consultantName}",
"mail.subject.message.notification.consultant": "Neue Nachricht [PLZ: ${plz}]",
"mail.subject.reassign.notification.consultant": "Neuzuweisung erfolgt",
"mail.subject.reassign.request.notification": "Anfrage Neuzuweisung",
"mail.subject.direct.enquiry.notification": "Neue_r Ratsuchende_r: Neue Erstanfrage über Kontaktlink [PLZ: ${plz}]"
}
12 changes: 11 additions & 1 deletion src/main/resources/i18n/mailservice.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@
"mail.label.you.have.a.new.message.in.your.counselings" : "You have a new message in your counselings",
"mail.label.your.consultant.wants.to.assign.you.to.new.consultant": "Your advisor would like to hand you over to a new advisor. We need your consent for this.",
"mail.label.imprint": "Imprint",
"mail.label.privacy": "Privacy"
"mail.label.privacy": "Privacy",
"mail.subject.assign.enquiry.notification": "New adviceseeker",
"mail.subject.daily.enquiry.notification": "${subject}",
"mail.subject.enquiry.notification.consultant": "New enquiry [Postcode: ${plz}]",
"mail.subject.feedback.message.notification": "New feedback message",
"mail.subject.free.text": "${subject}",
"mail.subject.message.notification.asker": "Answear from ${consultantName}",
"mail.subject.message.notification.consultant": "New message [Postcode: ${plz}]",
"mail.subject.reassign.notification.consultant": "Reassignment Done",
"mail.subject.reassign.request.notification": "Reassignment request",
"mail.subject.direct.enquiry.notification": "New adviceseeker: New enquiry via contact-link [PLZ: ${plz}]"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"de": "assign-enquiry-notification.html"
},
"subject": {
"de": "Neue_r Ratsuchende_r"
"key": "mail.subject.assign.enquiry.notification"
},
"templateImages" : [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"de": "daily-enquiry-notification.html"
},
"subject": {
"de": "${subject}"
"key": "mail.subject.daily.enquiry.notification"
},
"templateImages" : [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"de": "enquiry-notification-consultant.html"
},
"subject": {
"de": "Neue Erstanfrage [PLZ: ${plz}]"
"key": "mail.subject.enquiry.notification.consultant"
},
"templateImages" : [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"htmlTemplateFilename": {
"de": "feedback-message-notification.html"
},
"subject": {
"de": "Neue Feedback-Nachricht"
},
"subject": {
"key": "mail.subject.feedback.message.notification"
},
"templateImages" : [
{
"filename":"caritas-logo.jpg",
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/free-text.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"de": "free-text.html"
},
"subject": {
"de": "${subject}"
"key": "mail.subject.free.text"
},
"templateImages" : [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"de": "message-notification-asker.html"
},
"subject": {
"de": "Antwort von ${consultantName}"
"key": "mail.subject.message.notification.asker"
},
"templateImages" : [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"de": "message-notification-consultant.html"
},
"subject": {
"de": "Neue Nachricht [PLZ: ${plz}]"
"key": "mail.subject.message.notification.consultant"
},
"templateImages" : [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"en": "reassign-confirmation-notification.en.html"
},
"subject": {
"de": "Neuzuweisung erfolgt",
"en": "Reassignment Done"
"key": "mail.subject.reassign.notification.consultant"
},
"templateImages" : [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"de": "reassign-request-notification.html"
},
"subject": {
"de": "Anfrage Neuzuweisung"
"key": "mail.subject.reassign.request.notification"
},
"templateImages" : [
{
Expand Down
23 changes: 5 additions & 18 deletions src/test/java/de/caritas/cob/mailservice/api/ResourceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,18 @@ void descriptionShouldDeclareDistinctLanguages() throws IOException {
}

@Test
void descriptionShouldHaveDefaultSubject() throws IOException {
void descriptionShouldHaveSubjectKey() throws IOException {
var jsonFiles = resourceResolver.getResources(TEMPLATES_DIR + DESC_FILE_PATTERN);
var defaultLanguage = new MailDTO().getLanguage();
assertTrue(jsonFiles.length > 8);

for (Resource jsonFile : jsonFiles) {
var json = Files.readString(jsonFile.getFile().toPath());
var desc = objectMapper.readValue(json, TemplateDescription.class);
var subjects = desc.getSubject();
assertNotNull(subjects.get(defaultLanguage));
}
}

@Test
void descriptionSubjectsAndTemplateFilesShouldBeCorresponding() throws IOException {
var jsonFiles = resourceResolver.getResources(TEMPLATES_DIR + DESC_FILE_PATTERN);
assertTrue(jsonFiles.length > 8);

for (Resource jsonFile : jsonFiles) {
var json = Files.readString(jsonFile.getFile().toPath());
var desc = objectMapper.readValue(json, TemplateDescription.class);
var templateLanguages = desc.getHtmlTemplateFilename().keySet();
var subjectLanguages = desc.getSubject().keySet();

assertEquals(templateLanguages, subjectLanguages);
var subjectKey = desc.getSubject();
assertNotNull(subjectKey, String.format(
"Json file with resource description does not contain subject key: %s",
jsonFile.getFilename()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

import de.caritas.cob.mailservice.api.exception.TemplateServiceException;
import de.caritas.cob.mailservice.api.mailtemplate.SubjectDescription;
import de.caritas.cob.mailservice.api.mailtemplate.TemplateDescription;
import de.caritas.cob.mailservice.api.model.LanguageCode;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -32,7 +35,7 @@ public class TemplateServiceTest {
"Test " + PLACEHOLDER1_VALUE + " Test " + PLACEHOLDER2_VALUE;
private final TemplateDescription TEMPLATE_DESCRIPTION = new TemplateDescription(
Map.of(LanguageCode.DE, "test.html"),
Map.of(LanguageCode.DE, SUBJECT_WITH_PLACEHOLDERS),
new SubjectDescription("translationKey"),
FIELDS,
null
);
Expand Down Expand Up @@ -64,6 +67,8 @@ public void setup() {
@Test
public void getProcessedSubject_Should_ReturnSubjectWithReplacedPlaceholders() {

when(translationService.tryFetchTranslations(LanguageCode.DE.getValue()))
.thenReturn(Optional.of(Map.of("translationKey", SUBJECT_WITH_PLACEHOLDERS)));
var result = templateService.getRenderedSubject(
TEMPLATE_DESCRIPTION, TEMPLATE_DATA, LanguageCode.DE
);
Expand Down

0 comments on commit 1bdd4f9

Please sign in to comment.