Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge with os #120

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/agencyadminservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,9 @@ components:
items:
type: string
enum: [ RELATIVE_COUNSELLING, SELF_COUNSELLING, PARENTAL_COUNSELLING]
dataProtection:
type: object
$ref: '#/components/schemas/DataProtectionDTO'

AgencyAdminFullResponseDTO:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ public class AgencyAdminService {
private final @NonNull AgencyTopicMergeService agencyTopicMergeService;
private final @NonNull AppointmentService appointmentService;

private final @NonNull DataProtectionConverter dataProtectionConverter;
@Autowired(required = false)
private AgencyTopicEnrichmentService agencyTopicEnrichmentService;

@Autowired(required = false)
private DemographicsConverter demographicsConverter;

@Autowired
private DataProtectionConverter dataProtectionConverter;

@Value("${feature.topics.enabled}")
private boolean featureTopicsEnabled;
Expand Down Expand Up @@ -147,7 +146,7 @@ private Agency fromAgencyDTO(AgencyDTO agencyDTO) {
if (featureDemographicsEnabled && agencyDTO.getDemographics() != null) {
demographicsConverter.convertToEntity(agencyDTO.getDemographics(), agencyBuilder);
}

dataProtectionConverter.convertToEntity(agencyDTO.getDataProtection(), agencyBuilder);
var agencyToCreate = agencyBuilder.build();

if (featureTopicsEnabled) {
Expand Down Expand Up @@ -210,9 +209,7 @@ private Agency mergeAgencies(Agency agency, UpdateAgencyDTO updateAgencyDTO) {
.counsellingRelations(agency.getCounsellingRelations())
.deleteDate(agency.getDeleteDate());

if (dataProtectionConverter != null) {
dataProtectionConverter.convertToEntity(updateAgencyDTO.getDataProtection(), agencyBuilder);
}
dataProtectionConverter.convertToEntity(updateAgencyDTO.getDataProtection(), agencyBuilder);

if (nonNull(updateAgencyDTO.getConsultingType())) {
agencyBuilder.consultingTypeId(updateAgencyDTO.getConsultingType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import de.caritas.cob.agencyservice.api.admin.validation.validators.annotation.CreateAgencyValidator;
import de.caritas.cob.agencyservice.api.admin.validation.validators.annotation.UpdateAgencyValidator;
import de.caritas.cob.agencyservice.api.admin.validation.validators.model.ValidateAgencyDTO;
import de.caritas.cob.agencyservice.api.exception.httpresponses.BadRequestException;
import de.caritas.cob.agencyservice.api.model.AgencyDTO;
import de.caritas.cob.agencyservice.api.model.UpdateAgencyDTO;
import de.caritas.cob.agencyservice.api.repository.agency.AgencyRepository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
Expand All @@ -20,6 +22,8 @@ public class AgencyValidator {

private final @NonNull ApplicationContext applicationContext;

private final @NonNull AgencyRepository agencyRepository;

/**
* Validates an {@link AgencyDTO}.
*
Expand Down Expand Up @@ -53,15 +57,19 @@ private ValidateAgencyDTO fromAgencyDto(AgencyDTO agencyDto) {
.postcode(agencyDto.getPostcode())
.consultingType(agencyDto.getConsultingType())
.demographicsDTO(agencyDto.getDemographics())
.tenantId(agencyDto.getTenantId())
.build();
}

private ValidateAgencyDTO fromUpdateAgencyDto(Long agencyId, UpdateAgencyDTO updateAgencyDTO) {
var existingAgency = agencyRepository.findById(agencyId).orElseThrow(() -> new BadRequestException("Agency with id " + agencyId + "not found!"));
return ValidateAgencyDTO.builder()
.id(agencyId)
.postcode(updateAgencyDTO.getPostcode())
.offline(updateAgencyDTO.getOffline())
.tenantId(existingAgency.getTenantId())
.demographicsDTO(updateAgencyDTO.getDemographics())
.dataProtectionDTO(updateAgencyDTO.getDataProtection())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package de.caritas.cob.agencyservice.api.admin.validation.validators;

import static de.caritas.cob.agencyservice.api.model.DataProtectionDTO.DataProtectionResponsibleEntityEnum.AGENCY_RESPONSIBLE;
import static de.caritas.cob.agencyservice.api.model.DataProtectionDTO.DataProtectionResponsibleEntityEnum.ALTERNATIVE_REPRESENTATIVE;
import static de.caritas.cob.agencyservice.api.model.DataProtectionDTO.DataProtectionResponsibleEntityEnum.DATA_PROTECTION_OFFICER;
import static org.apache.commons.lang3.StringUtils.isBlank;

import de.caritas.cob.agencyservice.api.admin.validation.validators.model.ValidateAgencyDTO;
import de.caritas.cob.agencyservice.api.exception.httpresponses.HttpStatusExceptionReason;
import de.caritas.cob.agencyservice.api.exception.httpresponses.InvalidOfflineStatusException;
import de.caritas.cob.agencyservice.api.model.DataProtectionContactDTO;
import de.caritas.cob.agencyservice.api.repository.agency.DataProtectionPlaceHolderType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AgencyDataProtectionValidationService {

public void validate(ValidateAgencyDTO validateAgencyDto) {
validateThatDataProtectionDtoExists(validateAgencyDto);
validateIfDataProtectionOfficer(validateAgencyDto);
validateIfAgencyResponsible(validateAgencyDto);
validateIfAlternativeRepresentative(validateAgencyDto);
}

private void validateThatDataProtectionDtoExists(ValidateAgencyDTO validateAgencyDto) {
if (validateAgencyDto.getDataProtectionDTO() == null) {
log.warn(
"Could not save agency with id {} status. Required fields for data protection officer is empty.",
validateAgencyDto.getId());
throw new InvalidOfflineStatusException(
HttpStatusExceptionReason.DATA_PROTECTION_DTO_IS_NULL);
}
}

private void validateIfDataProtectionOfficer(ValidateAgencyDTO validateAgencyDto) {
if (DATA_PROTECTION_OFFICER.equals(
validateAgencyDto.getDataProtectionDTO().getDataProtectionResponsibleEntity())
&& areFieldsEmpty(
validateAgencyDto.getDataProtectionDTO().getDataProtectionOfficerContact())) {
log.warn(
"Could not save agency with id {}. Required fields for data protection officer is empty.",
validateAgencyDto.getId());
throw new InvalidOfflineStatusException(
HttpStatusExceptionReason.DATA_PROTECTION_OFFICER_IS_EMPTY);
}
}

private void validateIfAgencyResponsible(ValidateAgencyDTO validateAgencyDto) {
if (AGENCY_RESPONSIBLE.equals(
validateAgencyDto.getDataProtectionDTO().getDataProtectionResponsibleEntity())
&& areFieldsEmpty(
validateAgencyDto.getDataProtectionDTO().getAgencyDataProtectionResponsibleContact())) {
log.warn(
"Could not save agency with id {} status. Required fields for agency responsible is empty.",
validateAgencyDto.getId());
throw new InvalidOfflineStatusException(
HttpStatusExceptionReason.DATA_PROTECTION_RESPONSIBLE_IS_EMPTY);
}
}

private void validateIfAlternativeRepresentative(ValidateAgencyDTO validateAgencyDto) {
if (ALTERNATIVE_REPRESENTATIVE.equals(
validateAgencyDto.getDataProtectionDTO().getDataProtectionResponsibleEntity())
&& areFieldsEmpty(validateAgencyDto.getDataProtectionDTO()
.getAlternativeDataProtectionRepresentativeContact())) {
log.warn(
"Could not save agency with id {} status. Required fields for alternative responsible is empty.",
validateAgencyDto.getId());
throw new InvalidOfflineStatusException(
HttpStatusExceptionReason.DATA_PROTECTION_ALTERNATIVE_RESPONSIBLE_IS_EMPTY);
}
}

private boolean areFieldsEmpty(DataProtectionContactDTO dataProtectionOfficerContact) {
return dataProtectionOfficerContact == null
|| isBlank(dataProtectionOfficerContact.getNameAndLegalForm())
|| isBlank(dataProtectionOfficerContact.getCity())
|| isBlank(dataProtectionOfficerContact.getPostcode())
|| isBlank(dataProtectionOfficerContact.getEmail());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.caritas.cob.agencyservice.api.admin.validation.validators;

import de.caritas.cob.agencyservice.api.admin.validation.validators.annotation.UpdateAgencyValidator;
import de.caritas.cob.agencyservice.api.admin.validation.validators.model.ValidateAgencyDTO;
import de.caritas.cob.agencyservice.api.service.ApplicationSettingsService;
import de.caritas.cob.agencyservice.api.service.TenantService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@UpdateAgencyValidator
@Slf4j
public class AgencyDataProtectionValidator implements ConcreteAgencyValidator {

private final @NonNull TenantService tenantService;

private final @NonNull ApplicationSettingsService applicationSettingsService;

private final @NonNull AgencyDataProtectionValidationService agencyDataProtectionValidationService;

@Value("${feature.multitenancy.with.single.domain.enabled}")
private boolean multitenancyWithSingleDomain;

@Override
public void validate(ValidateAgencyDTO validateAgencyDto) {

var tenant = tenantService.getRestrictedTenantDataByTenantId(validateAgencyDto.getTenantId());

if (Boolean.TRUE.equals(tenant.getSettings().getFeatureCentralDataProtectionTemplateEnabled())) {
log.info("Validating agency data protection for agency with id {}.", validateAgencyDto.getId());
agencyDataProtectionValidationService.validate(validateAgencyDto);
}

if (multitenancyWithSingleDomain) {
var mainTenantSubdomainForSingleDomainMultitenancy = applicationSettingsService.getApplicationSettings()
.getMainTenantSubdomainForSingleDomainMultitenancy();
de.caritas.cob.agencyservice.tenantservice.generated.web.model.RestrictedTenantDTO mainTenant = tenantService.getRestrictedTenantDataBySubdomain(
mainTenantSubdomainForSingleDomainMultitenancy.getValue());
if (Boolean.TRUE.equals(mainTenant.getSettings().getFeatureCentralDataProtectionTemplateEnabled())) {
agencyDataProtectionValidationService.validate(validateAgencyDto);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import de.caritas.cob.agencyservice.consultingtypeservice.generated.web.model.ExtendedConsultingTypeResponseDTO;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/*
Expand All @@ -24,6 +25,7 @@
@Component
@RequiredArgsConstructor
@UpdateAgencyValidator
@Slf4j
public class AgencyOfflineStatusValidator implements ConcreteAgencyValidator {

private final @NonNull AgencyRepository agencyRepository;
Expand All @@ -48,7 +50,6 @@ public void validate(ValidateAgencyDTO validateAgencyDto) {
if (hasNoConsultant(validateAgencyDto)) {
throw new InvalidOfflineStatusException(AGENCY_CONTAINS_NO_CONSULTANTS);
}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.caritas.cob.agencyservice.api.admin.validation.validators.model;

import de.caritas.cob.agencyservice.api.model.DataProtectionDTO;
import de.caritas.cob.agencyservice.api.model.DemographicsDTO;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -18,6 +19,9 @@ public class ValidateAgencyDTO {
private String postcode;
private Integer consultingType;
private Boolean offline;
private Long tenantId;
private DemographicsDTO demographicsDTO;

private DataProtectionDTO dataProtectionDTO;

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@ public enum HttpStatusExceptionReason {
AGENCY_CONTAINS_NO_CONSULTANTS,
AGENCY_IS_ALREADY_TEAM_AGENCY,
AGENCY_IS_ALREADY_DEFAULT_AGENCY,
DATA_PROTECTION_OFFICER_IS_EMPTY,
DATA_PROTECTION_RESPONSIBLE_IS_EMPTY,

DATA_PROTECTION_ALTERNATIVE_RESPONSIBLE_IS_EMPTY,

DATA_PROTECTION_DTO_IS_NULL,

AGENCY_ACCESS_DENIED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.caritas.cob.agencyservice.api.repository.agency;

import lombok.Getter;

@Getter
public enum DataProtectionPlaceHolderType {
DATA_PROTECTION_RESPONSIBLE("responsible"),
DATA_PROTECTION_OFFICER("dataProtectionOfficer");

private final String placeholderVariable;

DataProtectionPlaceHolderType(String placeholderVariable) {
this.placeholderVariable = placeholderVariable;
}

public static boolean anyExistsInText(String privacy) {
return DATA_PROTECTION_RESPONSIBLE.existsInText(privacy)
|| DATA_PROTECTION_OFFICER.existsInText(privacy);
}

public boolean existsInText(String text) {
return text.contains(getPlaceholder());
}

public String getPlaceholder() {
return "${" + placeholderVariable + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
import de.caritas.cob.agencyservice.api.model.AgencyAdminResponseDTO;
import de.caritas.cob.agencyservice.api.model.AgencyTypeRequestDTO;
import de.caritas.cob.agencyservice.api.model.DataProtectionContactDTO;
import de.caritas.cob.agencyservice.api.model.DataProtectionDTO;
import de.caritas.cob.agencyservice.api.model.DemographicsDTO;
import de.caritas.cob.agencyservice.api.model.UpdateAgencyDTO;
import de.caritas.cob.agencyservice.api.model.AgencyDTO;
import de.caritas.cob.agencyservice.api.repository.agency.Agency;
import de.caritas.cob.agencyservice.api.repository.agency.AgencyRepository;
import de.caritas.cob.agencyservice.api.repository.agency.AgencyTenantUnawareRepository;
import de.caritas.cob.agencyservice.api.repository.agency.DataProtectionResponsibleEntity;
import de.caritas.cob.agencyservice.api.service.AppointmentService;
import de.caritas.cob.agencyservice.api.service.LogService;
import de.caritas.cob.agencyservice.api.util.JsonConverter;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -108,18 +107,23 @@ void updateAgency_Should_ThrowNotFoundException_WhenAgencyIsNotFound() {

@Test
void createAgency_Should_CreateAgencyAndAddDefaultCounsellingRelations() {
// given
var agency = this.easyRandom.nextObject(Agency.class);
agency.setCounsellingRelations(null);
agency.setDataProtectionOfficerContactData(null);
clearDataProtection(agency);
var agencyDTO = this.easyRandom.nextObject(AgencyDTO.class);
agencyDTO.setCounsellingRelations(null);
agencyDTO.setConsultingType(1);
agencyDTO.setDataProtection(new DataProtectionDTO());

when(agencyRepository.save(any())).thenReturn(agency);
// when
agencyAdminService.createAgency(agencyDTO);
// then
verify(agencyRepository).save(agencyArgumentCaptor.capture());
assertThat(agencyArgumentCaptor.getValue().getCounsellingRelations(), is("RELATIVE_COUNSELLING,SELF_COUNSELLING,PARENTAL_COUNSELLING"));
verify(dataProtectionConverter).convertToEntity(Mockito.any(DataProtectionDTO.class), Mockito.any(Agency.AgencyBuilder.class));
}

@Test
Expand Down
Loading
Loading