Repository: corona-warn-app/cwa-server
File: services/submission/src/main/java/app/coronawarn/server/services/submission/controller/SubmissionController.java (Java)

Code before:
      List<CheckIn> checkins = checkinsDataFilter.filter(submissionPayload.getCheckInsList());
      numberOfFilteredCheckins.set(submissionPayload.getCheckInsList().size() - checkins.size());
      numberOfSavedCheckins.set(traceTimeIntervalWarningSevice.saveCheckinsWithFakeData(checkins,
          submissionServiceConfig.getRandomCheckinsPaddingMultiplier(),
          submissionServiceConfig.getRandomCheckinsPaddingPepperAsByteArray()));
    } catch (final Exception e) {
      // Any check-in data processing related error must not interrupt the submission flow or interfere
      // with storing of the diagnosis keys
      logger.error("An error has occured while trying to store the event checkin data", e);
    }
    return new CheckinsStorageResult(numberOfFilteredCheckins.get(), numberOfSavedCheckins.get());
  }

  private void extractAndStoreDiagnosisKeys(SubmissionPayload submissionPayload) {
    List<DiagnosisKey> diagnosisKeys = extractValidDiagnosisKeysFromPayload(
        enhanceWithDefaultValuesIfMissing(submissionPayload));
    for (DiagnosisKey diagnosisKey : diagnosisKeys) {
      diagnosisKey.setTransmissionRiskLevel(getMappedTrasmissionRiskValue(diagnosisKey.getTransmissionRiskLevel()));
    }
    diagnosisKeyService.saveDiagnosisKeys(padDiagnosisKeys(diagnosisKeys));
  }

  private int getMappedTrasmissionRiskValue(int transmissionRiskLevel) {
    return trlDerivations.mapFromTrlSubmittedToTrlToStore(transmissionRiskLevel);
  }

  private List<DiagnosisKey> extractValidDiagnosisKeysFromPayload(SubmissionPayload submissionPayload) {
    List<TemporaryExposureKey> protoBufferKeys = submissionPayload.getKeysList();

    List<DiagnosisKey> diagnosisKeys = protoBufferKeys.stream()
        .map(protoBufferKey -> DiagnosisKey.builder()
            .fromTemporaryExposureKeyAndMetadata(
                protoBufferKey,
                submissionPayload.getVisitedCountriesList(),
                submissionPayload.getOrigin(),
                submissionPayload.getConsentToFederation())
            .withFieldNormalization(new SubmissionKeyNormalizer(submissionServiceConfig))
            .build()
        )
        .filter(diagnosisKey -> diagnosisKey.isYoungerThanRetentionThreshold(retentionDays))
        .collect(Collectors.toList());

    if (protoBufferKeys.size() > diagnosisKeys.size()) {
      logger.warn("Not persisting {} diagnosis key(s), as it is outdated beyond retention threshold.",
          protoBufferKeys.size() - diagnosisKeys.size());
    }
    return diagnosisKeys;
  }

  private SubmissionPayload enhanceWithDefaultValuesIfMissing(SubmissionPayload submissionPayload) {
    String originCountry = defaultIfEmptyOriginCountry(submissionPayload.getOrigin());

Review comment (suggestion):
```suggestion
    for (DiagnosisKey diagnosisKey : diagnosisKeys) {
      mapTrasmissionRiskValue(diagnosisKey);      
    }
    diagnosisKeyService.saveDiagnosisKeys(padDiagnosisKeys(diagnosisKeys));
  }

  private int mapTrasmissionRiskValue(DiagnosisKey diagnosisKey) { diagnosisKey.setTransmissionRiskLevel(trlDerivations.mapFromTrlSubmittedToTrlToStore(diagnosisKey.getTransmissionRiskLevel()));
  }

```

Code after:
      List<CheckIn> checkins = checkinsDataFilter.filter(submissionPayload.getCheckInsList());
      numberOfFilteredCheckins.set(submissionPayload.getCheckInsList().size() - checkins.size());
      numberOfSavedCheckins.set(traceTimeIntervalWarningSevice.saveCheckinsWithFakeData(checkins,
          submissionServiceConfig.getRandomCheckinsPaddingMultiplier(),
          submissionServiceConfig.getRandomCheckinsPaddingPepperAsByteArray()));
    } catch (final Exception e) {
      // Any check-in data processing related error must not interrupt the submission flow or interfere
      // with storing of the diagnosis keys
      logger.error("An error has occured while trying to store the event checkin data", e);
    }
    return new CheckinsStorageResult(numberOfFilteredCheckins.get(), numberOfSavedCheckins.get());
  }

  private void extractAndStoreDiagnosisKeys(SubmissionPayload submissionPayload) {
    List<DiagnosisKey> diagnosisKeys = extractValidDiagnosisKeysFromPayload(
        enhanceWithDefaultValuesIfMissing(submissionPayload));
    for (DiagnosisKey diagnosisKey : diagnosisKeys) {
      mapTrasmissionRiskValue(diagnosisKey);
    }
    diagnosisKeyService.saveDiagnosisKeys(padDiagnosisKeys(diagnosisKeys));
  }

  private void mapTrasmissionRiskValue(DiagnosisKey diagnosisKey) {
    diagnosisKey.setTransmissionRiskLevel(
        trlDerivations.mapFromTrlSubmittedToTrlToStore(diagnosisKey.getTransmissionRiskLevel()));
  }

  private List<DiagnosisKey> extractValidDiagnosisKeysFromPayload(SubmissionPayload submissionPayload) {
    List<TemporaryExposureKey> protoBufferKeys = submissionPayload.getKeysList();

    List<DiagnosisKey> diagnosisKeys = protoBufferKeys.stream()
        .map(protoBufferKey -> DiagnosisKey.builder()
            .fromTemporaryExposureKeyAndMetadata(
                protoBufferKey,
                submissionPayload.getVisitedCountriesList(),
                submissionPayload.getOrigin(),
                submissionPayload.getConsentToFederation())
            .withFieldNormalization(new SubmissionKeyNormalizer(submissionServiceConfig))
            .build()
        )
        .filter(diagnosisKey -> diagnosisKey.isYoungerThanRetentionThreshold(retentionDays))
        .collect(Collectors.toList());

    if (protoBufferKeys.size() > diagnosisKeys.size()) {
      logger.warn("Not persisting {} diagnosis key(s), as it is outdated beyond retention threshold.",
          protoBufferKeys.size() - diagnosisKeys.size());
    }
    return diagnosisKeys;
  }

  private SubmissionPayload enhanceWithDefaultValuesIfMissing(SubmissionPayload submissionPayload) {