public IList <TemporaryExposureKey> FilterAndSaveKeys(IList <TemporaryExposureKeyGatewayDto> keys) { StringBuilder logErrorPerBatchSB = new StringBuilder(); IList <TemporaryExposureKey> mappedKeys = null; IList <string> validationErrors = new List <string>(); try { mappedKeys = _keyFilter.MapKeys(keys); } catch (Exception e) { _logger.LogError("|SmitteStop| StoreService: unable to parse and map response" + e); throw; } _logger.LogInformation($"Mapped {mappedKeys?.Count} keys from {keys?.Count}"); IList <TemporaryExposureKey> acceptedKeys = null; int acceptedKeysCount = 0; if (mappedKeys.Any()) { foreach (var key in mappedKeys) { CalulateDaysAndRisk(key); } _logger.LogInformation($"Starting key validation."); acceptedKeys = _keyFilter.ValidateKeys(mappedKeys, out validationErrors); _logger.LogInformation($"Keys validated with {validationErrors?.Count} error."); _logger.LogInformation($"RemoveKeyDuplicates-start with {acceptedKeys?.Count} keys."); acceptedKeys = _keyFilter.RemoveKeyDuplicatesAsync(acceptedKeys).Result; _logger.LogInformation($"RemoveKeyDuplicates-ended with {acceptedKeys?.Count} keys after filtering."); _logger.LogInformation($"Saving..."); _tempKeyRepository.AddTemporaryExposureKeys(acceptedKeys).Wait(); _logger.LogInformation($"{acceptedKeys?.Count} keys saved."); acceptedKeysCount = acceptedKeys.Count; } _logger.LogInformation($"Received {keys.Count} keys. Accepted and saved { acceptedKeysCount } of them."); foreach (string error in validationErrors) { logErrorPerBatchSB.Append(error + Environment.NewLine); } var errors = logErrorPerBatchSB.ToString(); if (!string.IsNullOrEmpty(errors)) { _logger.LogWarning($"Received {keys.Count} keys. Accepted and saved { acceptedKeysCount } of them. Validation Errors:{Environment.NewLine}{ errors }"); } return(acceptedKeys ?? new List <TemporaryExposureKey>()); }
public async Task <IActionResult> UploadDiagnosisKeys() { var requestBody = String.Empty; try { _logger.LogInformation("UploadDiagnosisKeys endpoint called"); using (var reader = new StreamReader(HttpContext.Request.Body)) { requestBody = await reader.ReadToEndAsync(); } var parameter = JsonSerializer.Deserialize <TemporaryExposureKeyBatchDto>(requestBody); _exposureKeyValidator.ValidateParameterAndThrowIfIncorrect(parameter, _keyValidationRulesConfig, _logger); var newTemporaryExposureKeys = await _addTemporaryExposureKeyService.GetFilteredKeysEntitiesFromDTO(parameter); if (newTemporaryExposureKeys.Any()) { var origin = _countryRepository.FindByIsoCode(parameter.regions[0]); foreach (var key in newTemporaryExposureKeys) { key.Origin = origin; key.KeySource = KeySource.SmitteStopApiVersion1; key.ReportType = ReportType.CONFIRMED_TEST; } await _temporaryExposureKeyRepository.AddTemporaryExposureKeys(newTemporaryExposureKeys); } _logger.LogInformation("Keys uploaded successfully"); return(Ok()); } catch (JsonException je) { _logger.LogError($"Incorrect JSON format: { je} [Deserialized request]: {requestBody}"); return(BadRequest($"Incorrect JSON format: {je.Message}")); } catch (ArgumentException ae) { _logger.LogError("Incorrect input format: " + ae); return(BadRequest("Incorrect input format: " + ae.Message)); } catch (SqlException e) { _logger.LogError("Error occurred when uploading keys to the database." + e); return(StatusCode(500, "Error occurred when uploading keys to the database.")); } catch (Exception e) { _logger.LogError("Error uploading keys:" + e); return(StatusCode(500)); } }
private async Task CreateNewKeysInDatabase(TemporaryExposureKeyBatchDto parameters, IList <TemporaryExposureKey> newTemporaryExposureKeys) { var origin = _countryRepository.FindByIsoCode(parameters.regions[0]); foreach (var key in newTemporaryExposureKeys) { key.Origin = origin; key.KeySource = KeySource.SmitteStopApiVersion2; key.ReportType = ReportType.CONFIRMED_TEST; } var visitedCountries = parameters.visitedCountries.FindAll(countryCode => countryCode.ToLower() != origin.Code.ToLower()); await _temporaryExposureKeyRepository.AddTemporaryExposureKeys(newTemporaryExposureKeys); await CreateKeyCountryRelationships(visitedCountries, newTemporaryExposureKeys); }