public async Task Visit(DocumentUpdateMessage message) { var source = GetSource(message); string jsonMimeType = "application/json"; if (message.DocumentMimeType != jsonMimeType) { await _botClient.SendTextMessageAsync(message, MessageCode.ExpectedJsonFile); return; } try { var parser = new MetricsJsonParser(_botClient); var newMetrics = await parser.ParseAsync(message.DocumentFileId); _metricRepository.UpdateMetricData(source, newMetrics); await _botClient.SendTextMessageAsync(message, MessageCode.Done); } catch { await _botClient.SendTextMessageAsync(message, MessageCode.JsonError); throw; } // If there is no main metric, set any other var hasMainMetric = _metricRepository.SetMainMetricIfHasNo(source.Id); source.UpdateState(hasMainMetric ? StateType.HasMetric : StateType.NeedAddMetric); source.UpdateLastActionDate(); _sourceRepository.Update(source); }
public async Task ParseAsync_Success(int updateId) { IBotClient botClient = new FakeBotClient(new FakeBotResponse()); var parser = new MetricsJsonParser(botClient); var filename = FileName.GetJsonFilename(updateId.ToString()); var newMetrics = await parser.ParseAsync(filename); var testMetrics = new TestData().GetMetrics(updateId); Assert.NotNull(newMetrics); Assert.Equal(newMetrics.Count(), testMetrics.Count()); foreach (var newMetric in newMetrics) { var testMetric = testMetrics.FirstOrDefault(x => x.Name == newMetric.Name && x.Unit == newMetric.Unit); Assert.NotNull(testMetric); Assert.Equal(newMetric.Values.Count(), testMetric.Values.Count()); foreach (var value in newMetric.Values) { Assert.NotNull(testMetric.Values.FirstOrDefault(x => x.Content == value.Content && x.ValueDate == value.ValueDate)); } } }