Skip to content

Commit

Permalink
Merge pull request #423 from code4romania/hotfix/fix-data-export
Browse files Browse the repository at this point in the history
Update GetExcelDbCommandHandler.cs
  • Loading branch information
idormenco authored Oct 15, 2023
2 parents 1974aff + 8123cf3 commit 6437676
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<int> Handle(FillInAnswerCommand message, CancellationToken can
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
_logger.LogError(ex, "failed to save in db answers @{message}", message);
}

return await Task.FromResult(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private async Task<DataTable> GetPollingStationsObservations(CancellationToken c
{
object?[] rowValues =

Check warning on line 149 in src/api/VoteMonitor.Api.DataExport/Handlers/GetExcelDbCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
row.ObserverId,
row.ObserverId,
row.ObserverPhone,
row.ObserverName,
row.PollingStation,
Expand Down Expand Up @@ -222,18 +222,17 @@ private async Task<DataTable> GetForms(CancellationToken cancellationToken)

foreach (var question in questions)
{
object?[] rowValues = new List<object?>
var rowValues = new List<object?>

Check warning on line 225 in src/api/VoteMonitor.Api.DataExport/Handlers/GetExcelDbCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
question.FormCode,
question.FormSectionCode,
question.QuestionCode,
question.QuestionText,
GetFormattedQuestionType(question.QuestionType),
}
.Union(question.Options.Select(x => FormatOption(x.Text, x.IsFreeText, x.IsFlagged)))
.ToArray();
};

dataTable.Rows.Add(rowValues);
rowValues.AddRange(question.Options.Select(x => FormatOption(x.Text, x.IsFreeText, x.IsFlagged)));
dataTable.Rows.Add(rowValues.ToArray());
}

return dataTable;
Expand Down Expand Up @@ -282,19 +281,19 @@ private async Task<DataTable> GetNotesForExport(CancellationToken cancellationTo

foreach (var note in notes)
{
object?[] rowValues = new List<object?>
{
note.ObserverId,
note.ObserverPhone,
note.ObserverName,
GetNoteLocation(note.PollingStation, note.Question?.FormSection?.Form?.Code, note.Question?.Code),
note.LastModified.ToString("s"),
note.Text
}
.Union(note.Attachments.Select(x => _fileService.GetPreSignedUrl(x.FileName)))
.ToArray();
var rowValues = new List<object?>

Check warning on line 284 in src/api/VoteMonitor.Api.DataExport/Handlers/GetExcelDbCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
note.ObserverId,
note.ObserverPhone,
note.ObserverName,
GetNoteLocation(note.PollingStation, note.Question?.FormSection?.Form?.Code, note.Question?.Code),
note.LastModified.ToString("s"),
note.Text
};

dataTable.Rows.Add(rowValues);
rowValues.AddRange(note.Attachments.Select(x => _fileService.GetPreSignedUrl(x.FileName)));

dataTable.Rows.Add(rowValues.ToArray());
}

return dataTable;
Expand Down Expand Up @@ -359,7 +358,10 @@ private static string GetNoteLocation(string pollingStation, string formCode, st
(key, group) => new
{
FormCode = key,
Answers = group.OrderBy(x => x.ObserverId).ThenBy(x => x.LastModified).ToList()
Answers = group
.OrderBy(x => x.ObserverId)
.ThenBy(x => x.LastModified)
.ToList()
})
.ToDictionary(x => x.FormCode, y => y.Answers);

Expand Down Expand Up @@ -388,19 +390,18 @@ private static string GetNoteLocation(string pollingStation, string formCode, st

foreach (var row in filledInForm.Value)
{
object?[] rowValues = new List<object?>
var rowValues = new List<object?>

Check warning on line 393 in src/api/VoteMonitor.Api.DataExport/Handlers/GetExcelDbCommandHandler.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
row.ObserverId,
row.ObserverPhone,
row.ObserverName,
row.FormCode,
row.PollingStation,
row.LastModified
}
.Union(row.Answers.Select(x => x.SelectedValues))
.ToArray();
};

dataTable.Rows.Add(rowValues);
rowValues.AddRange(row.Answers.OrderBy(x => x.QuestionOrderNumber).Select(x => x.SelectedValues));
dataTable.Rows.Add(rowValues.ToArray());
}

result.Add((filledInForm.Key, dataTable));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task<int> Handle(RegisterPollingStationCommand message, Cancellatio
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
_logger.LogError(ex, "Error adding polling stations info {@request}", message);
}

return -1;
Expand Down
81 changes: 50 additions & 31 deletions src/api/VoteMonitor.Api.Note/Handlers/NoteQueriesHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using VoteMonitor.Api.Core.Services;
using VoteMonitor.Api.Note.Commands;
using VoteMonitor.Api.Note.Models;
Expand All @@ -17,11 +18,13 @@ public class NoteQueriesHandler :

private readonly VoteMonitorContext _context;
private readonly IFileService _fileService;
private readonly ILogger<NoteQueriesHandler> _logger;

public NoteQueriesHandler(VoteMonitorContext context, IFileService fileService)
public NoteQueriesHandler(VoteMonitorContext context, IFileService fileService, ILogger<NoteQueriesHandler> logger)
{
_context = context;
_fileService = fileService;
_logger = logger;
}
public async Task<List<NoteModel>> Handle(NoteQuery message, CancellationToken token)
{
Expand Down Expand Up @@ -67,21 +70,29 @@ public async Task<List<NoteModel>> Handle(NoteQuery message, CancellationToken t

public async Task<int> Handle(AddNoteCommandV2 request, CancellationToken cancellationToken)
{
var noteEntity = new Entities.Note
try
{
Text = request.Text,
IdPollingStation = request.PollingStationId,
// A note can be added to a polling station as well.
// In that case IdQuestion is either null or 0
IdQuestion = request.QuestionId == 0 ? null : request.QuestionId,
IdObserver = request.ObserverId,
LastModified = DateTime.UtcNow,
Attachments = request.Attachments.Select(a => new NotesAttachments { Path = a.Path, FileName = a.FileName }).ToList()
};

_context.Notes.Add(noteEntity);

return await _context.SaveChangesAsync(cancellationToken);
var noteEntity = new Entities.Note
{
Text = request.Text,
IdPollingStation = request.PollingStationId,
// A note can be added to a polling station as well.
// In that case IdQuestion is either null or 0
IdQuestion = request.QuestionId == 0 ? null : request.QuestionId,
IdObserver = request.ObserverId,
LastModified = DateTime.UtcNow,
Attachments = request.Attachments.Select(a => new NotesAttachments { Path = a.Path, FileName = a.FileName }).ToList()
};

_context.Notes.Add(noteEntity);

return await _context.SaveChangesAsync(cancellationToken);
}
catch (Exception e)
{
_logger.LogError(e, "Error when adding note {@request}", request);
throw;
}
}

public async Task<int> Handle(AddNoteCommand request, CancellationToken cancellationToken)

Check warning on line 98 in src/api/VoteMonitor.Api.Note/Handlers/NoteQueriesHandler.cs

View workflow job for this annotation

GitHub Actions / build

'AddNoteCommand' is obsolete: 'Will be removed when ui will use multiple files upload'
Expand Down Expand Up @@ -114,22 +125,30 @@ public async Task<int> Handle(AddNoteCommand request, CancellationToken cancella

public async Task<int> Handle(AddNoteToUnknownPollingStation request, CancellationToken cancellationToken)
{
var noteEntity = new Entities.NoteCorrupted()
try
{
Text = request.Text,
CountyCode = request.CountyCode,
MunicipalityCode = request.MunicipalityCode,
PollingStationNumber = request.PollingStationNumber,
// A note can be added to a polling station as well.
// In that case IdQuestion is either null or 0
IdQuestion = request.QuestionId == 0 ? null : request.QuestionId,
IdObserver = request.ObserverId,
LastModified = DateTime.UtcNow,
Attachments = request.Attachments.Select(a => new NotesAttachmentCorrupted() { Path = a.Path, FileName = a.FileName }).ToList()
};

_context.NotesCorrupted.Add(noteEntity);

return await _context.SaveChangesAsync(cancellationToken);
var noteEntity = new NoteCorrupted()
{
Text = request.Text,
CountyCode = request.CountyCode,
MunicipalityCode = request.MunicipalityCode,
PollingStationNumber = request.PollingStationNumber,
// A note can be added to a polling station as well.
// In that case IdQuestion is either null or 0
IdQuestion = request.QuestionId == 0 ? null : request.QuestionId,
IdObserver = request.ObserverId,
LastModified = DateTime.UtcNow,
Attachments = request.Attachments.Select(a => new NotesAttachmentCorrupted() { Path = a.Path, FileName = a.FileName }).ToList()
};

_context.NotesCorrupted.Add(noteEntity);

return await _context.SaveChangesAsync(cancellationToken);
}
catch (Exception e)
{
_logger.LogError(e, "Error when adding corrupted note {@request}", request);
throw;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task Handle(CreatePollingStationInfo request, CancellationToken can
}
catch (Exception ex)
{
_logger.LogError(ex, "Error creating polling station info: ");
_logger.LogError(ex, "Error creating polling station info: {@request}", request);
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task Handle(UpdatePollingStationInfo request, CancellationToken can
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating polling station info: ");
_logger.LogError(ex, "Error updating polling station info {@request}", request);
throw;
}
}
Expand Down
7 changes: 0 additions & 7 deletions src/api/VoteMonitor.Api/Extensions/ExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ public static IApplicationBuilder UseDefaultExceptionHandler(this IApplicationBu
var http = exHandlerFeature.Endpoint?.DisplayName?.Split(" => ")[0];
var type = exHandlerFeature.Error.GetType().Name;
var error = exHandlerFeature.Error.Message;
var msg =
$@"=================================
{http}
TYPE: {type}
REASON: {error}
---------------------------------
{exHandlerFeature.Error.StackTrace}";
Log.Error("{@http}{@type}{@reason}{@exception}", http, type, error, exHandlerFeature.Error);
Expand Down

0 comments on commit 6437676

Please sign in to comment.