ZUGFeRDRESTService: Improve Logging

This commit is contained in:
Jonathan Jenne 2020-03-23 16:24:39 +01:00
parent b5feec77f7
commit c9fdf80936
2 changed files with 74 additions and 35 deletions

View File

@ -18,6 +18,7 @@ Public Class ZUGFeRDInterface
NoValidFile NoValidFile
NoZugferd NoZugferd
NoValidZugferd NoValidZugferd
MissingProperties
End Enum End Enum
Public ReadOnly Property FileGroup As FileGroups Public ReadOnly Property FileGroup As FileGroups
@ -130,7 +131,7 @@ Public Class ZUGFeRDInterface
Dim oFoundResult As PDFAttachments.AttachmentResult = Nothing Dim oFoundResult As PDFAttachments.AttachmentResult = Nothing
For Each oResult In oResults For Each oResult In oResults
If oResult.FileName = PDFAttachments.ZUGFERD_XML_FILENAME Then If oResult.FileName.ToUpper() = PDFAttachments.ZUGFERD_XML_FILENAME.ToUpper() Then
oFound = True oFound = True
oFoundResult = oResult oFoundResult = oResult
End If End If

View File

@ -8,6 +8,8 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using DigitalData.Modules.Interfaces; using DigitalData.Modules.Interfaces;
using DigitalData.Modules.Logging; using DigitalData.Modules.Logging;
using static DigitalData.Modules.Interfaces.Exceptions;
using static DigitalData.Modules.Interfaces.ZUGFeRDInterface;
namespace ZUGFeRDRESTService.Controllers namespace ZUGFeRDRESTService.Controllers
{ {
@ -39,6 +41,20 @@ namespace ZUGFeRDRESTService.Controllers
message = String.Empty; message = String.Empty;
errors = new List<string>(); errors = new List<string>();
} }
public ValidationResponse(string Status, string Message)
{
status = Status;
message = Message;
errors = new List<string>();
}
public ValidationResponse(string Status, string Message, List<String> Errors)
{
status = Status;
message = Message;
errors = Errors;
}
} }
public ValidationController(ILogging logging, IDatabase database) public ValidationController(ILogging logging, IDatabase database)
@ -61,66 +77,88 @@ namespace ZUGFeRDRESTService.Controllers
} }
/// <summary> /// <summary>
/// POST: api/Validation /// POST: /api/Validation
/// </summary> /// </summary>
/// <param name="files">This parameter's name needs to correspond to the html form's file-input name</param> /// <param name="files">This parameter's name needs to correspond to the html form's file-input name</param>
[HttpPost] [HttpPost]
public async Task<ValidationResponse> Post(IFormFile file, string user_id) public async Task<ValidationResponse> Post(IFormFile file, string user_id)
{ {
string oFilePath; _logger.Debug("Start processing request to ValidationController");
string oFileName;
oFilePath = Path.Combine(Path.GetTempPath(), file.FileName); string oFilePath = Path.Combine(Path.GetTempPath(), file.FileName);
oFileName = file.FileName;
_logger.Debug("Saving file to temp-path [{0}]", oFilePath);
using FileStream oStream = new FileStream(oFilePath, FileMode.Create); using FileStream oStream = new FileStream(oFilePath, FileMode.Create);
await file.CopyToAsync(oStream); await file.CopyToAsync(oStream);
// TODO: Delete temp file / Do we even need the temp file?
_logger.Debug("File successfully saved!");
CrossIndustryDocumentType oDocument; CrossIndustryDocumentType oDocument;
PropertyValues.CheckPropertyValuesResult oResult = new PropertyValues.CheckPropertyValuesResult();
try try
{ {
_logger.Debug("Extracting ZUGFeRD Data from file [{0}]", oFilePath);
oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oFilePath); oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oFilePath);
}
catch (Exception ex)
{
_logger.Error(ex);
return new ValidationResponse()
{
status = RESPONSE_ERROR,
message = "The uploaded file is not a valid ZUGFeRD Invoice!"
};
}
try _logger.Debug("Checking ZUGFeRD Data against the database");
{
PropertyValues.CheckPropertyValuesResult oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID");
if (oResult.MissingProperties.Count > 0) oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID");
{
return new ValidationResponse() _logger.Debug("Result of checking against the database: {0} valid properties, {1} missing properties", oResult.ValidProperties, oResult.MissingProperties);
{
status = RESPONSE_ERROR, if (oResult.MissingProperties.Count > 0) {
message = "The uploaded file is a valid ZUGFeRD Invoice but missing some properties!", throw new ZUGFeRDExecption(ZUGFeRDInterface.ErrorType.MissingProperties,
errors = oResult.MissingProperties "Die hochgeladene Datei ist eine gültige ZUGFeRD-Rechnung, allerdings fehlen benötigte Daten.");
};
}
}
catch (Exception ex)
{
_logger.Error(ex);
return new ValidationResponse()
{
status = RESPONSE_ERROR,
message = "The uploaded file is not a valid ZUGFeRD Invoice!"
};
} }
return new ValidationResponse() return new ValidationResponse()
{ {
status = RESPONSE_OK, status = RESPONSE_OK,
message = "The uploaded files is a valid ZUGFeRD Invoice!" message = "Die hochgeladene Datei ist eine gültige-ZUGFeRD Rechnung"
};
}
catch (ZUGFeRDExecption ex)
{
_logger.Error(ex);
// Determine which message should be sent in the response
string oMessage = ex.ErrorType switch
{
ErrorType.NoValidFile => "Die hochgeladene Datei ist keine gültige Datei.",
ErrorType.NoZugferd => "Die hochgeladene Datei ist keine ZUGFeRD-Rechnung.",
ErrorType.NoValidZugferd => "Die hochgeladene Datei ist keine gültige ZUGFeRD-Rechnung.",
ErrorType.MissingProperties => "Die hochgeladene Datei ist eine gültige ZUGFeRD-Rechnung, jedoch fehlen einige Metadaten",
_ => "Die hochgeladene Datei kann nicht validiert werden.",
};
// Determine if any errors should be sent in the response
List<string> oErrors = ex.ErrorType switch
{
// Errors contains the list of missing fields
ErrorType.MissingProperties => oResult.MissingProperties,
_ => new List<string>()
};
return new ValidationResponse()
{
status = RESPONSE_ERROR,
message = oMessage
};
}
catch (Exception ex)
{
_logger.Error(ex);
return new ValidationResponse()
{
status = RESPONSE_ERROR,
message = "Die hochgeladene Datei kann nicht validiert werden!"
}; };
} }
} }
}
} }