Skip to content

Commit 3c8b1c9

Browse files
authored
Merge pull request #352 from bcgov/applicationscore-poc
Multiple Calculator check ignore more than 1 calculator logic
2 parents 62e63ba + 9ddadcf commit 3c8b1c9

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

OFM.Infrastructure.WebAPI/Models/ApplicationScore/AppicationScore.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ public static class DataverseQueries
292292
public static string AllScoreCategoryQuery = "ofm_application_score_categories?$filter=_ofm_application_score_calculator_value eq '{0}'&$select=ofm_name,ofm_maximum_score,ofm_description,ofm_category_display_name,ofm_application_score_group";
293293
public static string AllACCBDataQuery = @$"ofm_accbs?$select=ofm_accbid,ofm_postal_code,ofm_income_indicator,ofm_name&$filter=(_ofm_application_score_calculator_value eq {{0}} and statecode eq 0)";
294294
public static string AllPopulationCentreQuery = $@"ofm_population_centres?$select=ofm_population_centreid,ofm_city,ofm_projected_population,ofm_name,ofm_projected_population&$filter=(_ofm_application_score_calculator_value eq {{0}} and statecode eq 0)";
295+
296+
public const string ApplicationScoresQuery = "ofm_application_scores?$filter=_ofm_application_value eq {0}&$select=ofm_application_scoreid,_ofm_application_score_calculator_value,_ofm_application_value";
297+
295298
}
296299
/// <summary>
297300
/// Represents a OFM Application entity.
@@ -627,4 +630,17 @@ public JsonObject Clone(Guid calculatorId)
627630
//region.ccof_region_period_start
628631
public string? ProgramYear => _data.ContainsKey("region.ccof_region_period_start") ? _data.GetFormattedValue("region.ccof_region_period_start") : null;
629632
}
633+
634+
//application score
635+
public class ApplicationScoreValue
636+
{
637+
protected readonly JsonObject _data;
638+
public ApplicationScoreValue(JsonObject data)
639+
{
640+
_data = data ?? throw new ArgumentNullException(nameof(data));
641+
}
642+
public Guid? ApplicationScoreId => _data.GetPropertyValue<Guid>("ofm_application_scoreid");
643+
public Guid? ApplicationScoreCalculatorId => _data.GetPropertyValue<Guid>("_ofm_application_score_calculator_value");
644+
645+
}
630646
}

OFM.Infrastructure.WebAPI/Services/Processes/ApplicationScore/DataverseRepository.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public interface IDataverseRepository
5656
/// <param name="calculatorId">GUID of the application score calculator</param>
5757
/// <returns></returns>
5858
Task<ACCBIncomeIndicator> GetIncomeDataAsync(string postalCode, Guid calculatorId);
59+
60+
61+
Task<IEnumerable<ApplicationScoreValue>> GetApplicationScores(Guid ApplicationId);
62+
5963
/// <summary>
6064
/// Get Approved Parent Fees of the facility
6165
/// </summary>
@@ -138,7 +142,19 @@ public async Task<OFMApplication> GetFundingApplicationAsync(Guid applicationId)
138142
var json = await response.Content.ReadFromJsonAsync<JsonObject>();
139143
return new OFMApplication(json);
140144
}
141-
145+
public async Task<IEnumerable<ApplicationScoreValue>> GetApplicationScores(Guid applicationId)
146+
{
147+
148+
var response = await d365WebApiService.SendRetrieveRequestAsync(appUserService.AZSystemAppUser, $"{string.Format(DataverseQueries.ApplicationScoresQuery, applicationId)}", formatted: true, pageSize: 5000);
149+
if (!response.IsSuccessStatusCode)
150+
{
151+
var responseBody = await response.Content.ReadAsStringAsync();
152+
throw new Exception($"GetApplicationScores({applicationId}): HTTP Failure: {responseBody}");
153+
}
154+
var json = await response.Content.ReadFromJsonAsync<JsonObject>();
155+
var values = json["value"]?.AsArray() ?? new JsonArray();
156+
return values.Select(v => new ApplicationScoreValue(v.AsObject()));
157+
}
142158
public async Task<IEnumerable<OFMApplication>> GetUnprocessedApplicationsAsync(DateTime lastTime)
143159
{
144160
var formattedTime = lastTime.ToString("yyyy-MM-ddTHH:mm:ssZ");

OFM.Infrastructure.WebAPI/Services/Processes/ApplicationScore/P800ScoreCalculatorProvider.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ private string RetrieveApplicationCalculator
3636
</filter>
3737
<link-entity name=""ofm_intake"" from=""ofm_application_score_calculator"" to=""ofm_application_score_calculatorid"" link-type=""inner"" alias=""intake"">
3838
<attribute name=""ofm_start_date"" />
39-
<attribute name=""ofm_end_date"" />
39+
<attribute name=""ofm_end_date"" />
40+
<filter>
41+
<condition attribute=""statecode"" operator=""eq"" value=""0"" />
42+
</filter>
4043
</link-entity>
4144
</entity>
4245
</fetch>";
@@ -177,17 +180,24 @@ private IComparisonHandler comparisonChain
177180
{
178181
scores.Add(scoreData);
179182
}
180-
}
181-
182-
//Upsert Scores
183-
foreach (var score in scores)
183+
}
184+
185+
186+
187+
var existingScores = await dataverseRepository.GetApplicationScores(applicationId);
188+
189+
190+
if ((existingScores == null || !existingScores.Any()) || (existingScores != null && existingScores.Any() && existingScores.Where(x => x.ApplicationScoreCalculatorId.Equals(calculatorId)).Any()))
184191
{
185-
_logger.LogInformation(CustomLogEvent.Process, "Upsert Batch started for calculated scores for application {applicationId}", applicationId);
186-
await dataverseRepository.UpsertApplicationScoreAsync($"_ofm_application_value={applicationId},_ofm_application_score_category_value={score["ofm_application_score_category@odata.bind"]?.ToString().Replace("ofm_application_score_categories", "").Replace("(", "").Replace(")", "")}", score);
187-
_logger.LogInformation(CustomLogEvent.Process, "Upsert Batch completed for calculated scores for application {applicationId}", applicationId);
192+
//Upsert Scores
193+
foreach (var score in scores)
194+
{
195+
_logger.LogInformation(CustomLogEvent.Process, "Upsert Batch started for calculated scores for application {applicationId}", applicationId);
196+
await dataverseRepository.UpsertApplicationScoreAsync($"_ofm_application_value={applicationId},_ofm_application_score_category_value={score["ofm_application_score_category@odata.bind"]?.ToString().Replace("ofm_application_score_categories", "").Replace("(", "").Replace(")", "")}", score);
197+
_logger.LogInformation(CustomLogEvent.Process, "Upsert Batch completed for calculated scores for application {applicationId}", applicationId);
188198

199+
}
189200
}
190-
191201

192202

193203
//commented due to timeout while processing batch

0 commit comments

Comments
 (0)