public IList <TemporaryExposureKey> FilterAndSaveKeys(IList <TemporaryExposureKeyGatewayDto> keys)
        {
            StringBuilder logErrorPerBatchSB = new StringBuilder();
            IList <TemporaryExposureKey> mappedKeys;

            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.");
                if (validationErrors?.Count > 0)
                {
                    InformationLogValidationErrors(validationErrors);
                }
                _logger.LogInformation("Saving...");
                acceptedKeys = _addTemporaryExposureKeyService.FilterDuplicateKeysAsync(acceptedKeys).Result;
                _tempKeyRepository.AddTemporaryExposureKeysAsync(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);

                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.AddTemporaryExposureKeysAsync(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));
            }
        }
コード例 #3
0
        private async Task CreateNewKeysInDatabase(TemporaryExposureKeyBatchDto parameters, IList <TemporaryExposureKey> newTemporaryExposureKeys, KeySource apiVersion)
        {
            var origin = _countryRepository.FindByIsoCode(parameters.regions[0]);

            foreach (var key in newTemporaryExposureKeys)
            {
                key.Origin     = origin;
                key.KeySource  = apiVersion;
                key.ReportType = ReportType.CONFIRMED_TEST;
            }

            var visitedCountries = parameters.visitedCountries.FindAll(countryCode => countryCode.ToLower() != origin.Code.ToLower());
            await _temporaryExposureKeyRepository.AddTemporaryExposureKeysAsync(newTemporaryExposureKeys);

            await CreateKeyCountryRelationships(visitedCountries, newTemporaryExposureKeys);
        }