diff --git a/OFM.Infrastructure.WebAPI/Models/Fundings/Fundings.cs b/OFM.Infrastructure.WebAPI/Models/Fundings/Fundings.cs index a3d10bf..8e00267 100644 --- a/OFM.Infrastructure.WebAPI/Models/Fundings/Fundings.cs +++ b/OFM.Infrastructure.WebAPI/Models/Fundings/Fundings.cs @@ -159,6 +159,8 @@ public class SupplementaryApplication : ofm_allowance public Guid? _ofm_summary_submittedby_value { get; set; } [property: JsonPropertyName("app.ofm_funding_number_base")] public string? ofm_funding_number_base { get; set; } + [property: JsonPropertyName("funding.statuscode")] + public int? fundingstatuscode { get; set; } } public class BusinessClosure diff --git a/OFM.Infrastructure.WebAPI/Services/Processes/Emails/P210CreateFundingNotificationProvider.cs b/OFM.Infrastructure.WebAPI/Services/Processes/Emails/P210CreateFundingNotificationProvider.cs index d614f71..3700f74 100644 --- a/OFM.Infrastructure.WebAPI/Services/Processes/Emails/P210CreateFundingNotificationProvider.cs +++ b/OFM.Infrastructure.WebAPI/Services/Processes/Emails/P210CreateFundingNotificationProvider.cs @@ -31,6 +31,7 @@ public class P210CreateFundingNotificationProvider : ID365ProcessProvider private string? _informationCommunicationType; private string _contactId; + public P210CreateFundingNotificationProvider(IOptionsSnapshot notificationSettings, ID365AppUserService appUserService, ID365WebApiService d365WebApiService, ILoggerFactory loggerFactory, TimeProvider timeProvider, IFundingRepository fundingRepository, IEmailRepository emailRepository) { _notificationSettings = notificationSettings.Value; @@ -70,6 +71,62 @@ public string RequestUri return requestUri; } } + public string AllowanceRequestUri + { + get + { + var fetchXml = $""" + + + + + + + + + + + + + + + + + + + + + + 2 + 6 + + + + + + + + + + + + + + + + + + + + """; + + var requestUri = $""" + ofm_allowances?fetchXml={WebUtility.UrlEncode(fetchXml)} + """; + + return requestUri; + } + } #endregion public async Task GetDataAsync() @@ -102,6 +159,36 @@ public async Task GetDataAsync() return await Task.FromResult(new ProcessData(d365Result)); } + public async Task GetDataAsyncAllowance() + { + _logger.LogDebug(CustomLogEvent.Process, "GetAllowanceDataAsync"); + + var response = await _d365webapiservice.SendRetrieveRequestAsync(_appUserService.AZSystemAppUser, AllowanceRequestUri); + + if (!response.IsSuccessStatusCode) + { + var responseBody = await response.Content.ReadAsStringAsync(); + _logger.LogError(CustomLogEvent.Process, "Failed to query Allowance records with the server error {responseBody}", responseBody.CleanLog()); + + return await Task.FromResult(new ProcessData(string.Empty)); + } + + var jsonObject = await response.Content.ReadFromJsonAsync(); + + JsonNode d365Result = string.Empty; + if (jsonObject?.TryGetPropertyValue("value", out var currentValue) == true) + { + if (currentValue?.AsArray().Count == 0) + { + _logger.LogInformation(CustomLogEvent.Process, "No Allowance records found with query {requestUri}", AllowanceRequestUri.CleanLog()); + } + d365Result = currentValue!; + } + + _logger.LogDebug(CustomLogEvent.Process, "Query Result {queryResult}", d365Result.ToString().CleanLog()); + + return await Task.FromResult(new ProcessData(d365Result)); + } public async Task RunProcessAsync(ID365AppUserService appUserService, ID365WebApiService d365WebApiService, ProcessParameter processParams) { @@ -115,11 +202,11 @@ public async Task RunProcessAsync(ID365AppUserService appUserService Funding? _funding = await _fundingRepository?.GetFundingByIdAsync(new Guid(processParams.Funding!.FundingId!)); var expenseOfficer = _funding.ofm_application?._ofm_expense_authority_value; var primaryContact = _funding.ofm_application?._ofm_contact_value; - // Provider FA Approver + // Provider FA Approver int statusReason = (int)_funding!.statuscode; // funding status _logger.LogInformation("Got the Status", statusReason); - + var startTime = _timeProvider.GetTimestamp(); #region Create the funding email notifications @@ -130,13 +217,13 @@ public async Task RunProcessAsync(ID365AppUserService appUserService _logger.LogInformation("Entered if FASignaturePending", statusReason); // Get template details to create emails. var localDataTemplate = await _emailRepository.GetTemplateDataAsync(_notificationSettings.EmailTemplates.First(t => t.TemplateNumber == 210).TemplateNumber); - + var serializedDataTemplate = JsonSerializer.Deserialize>(localDataTemplate.Data.ToString()); _logger.LogInformation("Got the Template", serializedDataTemplate.Count); var hyperlink = _notificationSettings.FundingUrl + _funding.Id; var hyperlinkFATab = _notificationSettings.FundingTabUrl; - _logger.LogInformation("Got the hyperlink", hyperlink +hyperlinkFATab); + _logger.LogInformation("Got the hyperlink", hyperlink + hyperlinkFATab); var templateobj = serializedDataTemplate?.FirstOrDefault(); string? subject = templateobj?.title; string? emaildescription = templateobj?.safehtml; @@ -191,10 +278,30 @@ public async Task RunProcessAsync(ID365AppUserService appUserService recipientsList.Add((Guid)providerApprover); await _emailRepository.CreateAndUpdateEmail(subject, emaildescription, recipientsList, _processParams.Notification.SenderId, _informationCommunicationType, appUserService, d365WebApiService, 210); } + var localDataAllowance = await GetDataAsyncAllowance(); + var deserializedDataAllowance = JsonSerializer.Deserialize>(localDataAllowance.Data.ToString()); + if (deserializedDataAllowance == null || deserializedDataAllowance.Count == 0) + { + _logger.LogInformation("No records returned from FetchXml", deserializedDataAllowance.Count); + return ProcessResult.Completed(ProcessId).SimpleProcessResult; + } + foreach (var allowance in deserializedDataAllowance) + { + bool emailCreated = await _emailRepository.CreateAllowanceEmail(allowance, _processParams.Notification.SenderId, _informationCommunicationType, ProcessId, d365WebApiService); + } + #endregion Create the Supp email notifications + + + + + + } + return ProcessResult.Failure(ProcessId, new String[] { "Upsert action failed" }, 0, 0).SimpleProcessResult; - return ProcessResult.Completed(ProcessId).SimpleProcessResult; - #endregion } -} \ No newline at end of file +} + + + diff --git a/OFM.Infrastructure.WebAPI/Services/Processes/Emails/P240AllowanceApprovalDenialNotificationProvider.cs b/OFM.Infrastructure.WebAPI/Services/Processes/Emails/P240AllowanceApprovalDenialNotificationProvider.cs index 1872214..ba6b3f1 100644 --- a/OFM.Infrastructure.WebAPI/Services/Processes/Emails/P240AllowanceApprovalDenialNotificationProvider.cs +++ b/OFM.Infrastructure.WebAPI/Services/Processes/Emails/P240AllowanceApprovalDenialNotificationProvider.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Options; +using ECC.Core.DataContext; +using Microsoft.Extensions.Options; using OFM.Infrastructure.WebAPI.Extensions; using OFM.Infrastructure.WebAPI.Models; using OFM.Infrastructure.WebAPI.Models.Fundings; @@ -74,6 +75,12 @@ public string RequestUri + + + + + + @@ -136,6 +143,7 @@ public async Task RunProcessAsync(ID365AppUserService appUserService } try { + if(deserializedData.First().fundingstatuscode == (int)ofm_funding_StatusCode.Active) await _emailRepository.CreateAllowanceEmail(deserializedData.First(), _processParams.Notification.SenderId, _informationCommunicationType, ProcessId, d365WebApiService); } catch (Exception ex)