Files
monorepo/LCU.API.Internal.ArchiverApi/LCU.Api.Internal.ArchiverApi/Services/ArchiveCompletionService.cs
T
2026-03-17 10:03:23 -05:00

95 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using Serilog;
using Newtonsoft.Json;
using LCU.Api.Internal.MaidBotLib.Models;
using LCU.Library.Internal.ApiResponse.Models;
using LCU.Library.Internal.ApiResponse.Services;
using LCU.Library.Internal.ArchiverApiModel.Models;
namespace LCU.Api.Internal.ArchiverApi.Services;
public class ArchiveCompletionService : IArchiveCompletionService
{
private readonly IFileOperationsService _fileOperationsService;
private readonly INotificationService _notificationService;
public ArchiveCompletionService(IFileOperationsService fileOperationsService,
INotificationService notificationService)
{
_fileOperationsService = fileOperationsService;
_notificationService = notificationService;
}
public void HandleReportArchiveCompletedRequest(Response response)
{
try
{
var payLoadString = response.ResponsePayload;
var archiveResponsePayload = JsonConvert.DeserializeObject<ArchiveResponsePayload>(payLoadString);
var archiveRequestResponse = archiveResponsePayload.ArchiveRequestResponse;
archiveRequestResponse.IsSuccessful = response.Successful;
archiveRequestResponse.MessageStack.AddRange(response.ResponseStack);
var requestResult = PopulateErrorDetailsAndRequestResponse(archiveResponsePayload, archiveRequestResponse);
QueueReturnResponse.QueueResponse(archiveResponsePayload.ArchiveRequest,
requestResult.archiveRequestResponse.IsSuccessful,
requestResult.archiveRequestResponse.MessageStack);
var errorsSent = _notificationService.SendErrorEmailAsync(requestResult.errorDetails).Result;
if (!errorsSent)
{
Log.ForContext(nameof(PropertyEnum.Group),
nameof(GroupEnum.ITDevelopment))
.Error($"Failure to send error details for {requestResult.errorDetails.Count} errors.");
throw new Exception($"Failure to send error details for {requestResult.errorDetails.Count} errors.");
}
}
catch (Exception exception)
{
Log.Error(exception, $"Error while archiving reports. " +
$"Message:{exception.Message}");
}
}
public (List<ErrorDetail> errorDetails, ArchiveRequestResponse archiveRequestResponse) PopulateErrorDetailsAndRequestResponse(ArchiveResponsePayload archiveResponsePayload,
ArchiveRequestResponse archiveRequestResponse)
{
var errorDetails = new List<ErrorDetail>();
foreach (var request in archiveResponsePayload.BulkOperationRequest.OperationRequests)
{
var errorMessage = "";
try
{
//If file still exist move did not occur.
var fileExists = _fileOperationsService.ValidateFileExists(request.Source).Result;
if (fileExists)
{
errorMessage = $"File {request.SourceFile} was not archived to the path of {request.Destination} ";
archiveRequestResponse.IsSuccessful = false;
archiveRequestResponse.MessageStack.Add(errorMessage);
}
}
catch (Exception exception)
{
var loggingMessage = $"Exception thrown while looking for the file at path {request.Source}.";
errorMessage = $"{loggingMessage}. Message: {exception.Message}";
Log.Error(exception, loggingMessage);
}
if (!string.IsNullOrEmpty(errorMessage))
{
var newErrorDetail = new ErrorDetail()
{
FileLocation = request.SourcePath,
Filename = request.FinalSourceFile,
Destination = request.Destination,
Description = errorMessage
};
errorDetails.Add(newErrorDetail);
}
}
return (errorDetails, archiveRequestResponse);
}
}