Skip to content

Commit

Permalink
Merge pull request #768 from bcgov/develop
Browse files Browse the repository at this point in the history
Promote
  • Loading branch information
leewrigh authored Feb 6, 2025
2 parents 365ddbc + d9a3470 commit 2cb8a5b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
8 changes: 8 additions & 0 deletions backend/CommonModels/Models/EDT/CaseStatusModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace CommonModels.Models.EDT;

using Common.Models;

public class CaseStatusModel : KeyIdPair
{
public string Status { get; set; } = string.Empty;
}
3 changes: 2 additions & 1 deletion backend/edt.disclosure/EdtDisclosureServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class EdtDisclosureServiceConfiguration
public ConnectionStringConfiguration ConnectionStrings { get; set; } = new();
public KafkaClusterConfiguration KafkaCluster { get; set; } = new();
public EdtDisclosureClientConfiguration EdtClient { get; set; } = new();

public int CaseCheckRetries { get; set; } = 10;
public int CaseCheckTimeoutSeconds { get; set; } = 10;
public KeycloakConfiguration Keycloak { get; set; } = new();
public List<CustomDisplayField> CaseDisplayCustomFields { get; set; } = new();

Expand Down
1 change: 0 additions & 1 deletion backend/edt.disclosure/HttpClients/Services/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ private async Task<IDomainResult<T>> SendCoreInternalAsync<T>(HttpMethod method,
{
Content = content
};
Logger.LogInformation($"Client timeout: {this.client.Timeout}");
using var response = await this.client.SendAsync(request, cancellationToken);

if (!response.IsSuccessStatusCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace edt.disclosure.HttpClients.Services.EdtDisclosure;
using Common.Constants.Auth;
using Common.Models;
using Common.Models.EDT;
using CommonModels.Models.EDT;
using DIAM.Common.Models;
using edt.disclosure.Exceptions;
using edt.disclosure.Infrastructure.Telemetry;
Expand Down Expand Up @@ -567,10 +568,17 @@ private async Task<IEnumerable<OrgUnitModel>> GetUserCaseAccess(string userId)
}
}

/// <summary>
/// Add a user to a case - attempt some retries if user not found on case imme
/// </summary>
/// <param name="userKey"></param>
/// <param name="caseId"></param>
/// <returns></returns>
/// <exception cref="EdtDisclosureServiceException"></exception>
public async Task<bool> AddUserToCase(string userKey, int caseId)
{
var caseListing = await this.GetUserCases(userKey);

var caseCheckRetries = this.configuration.CaseCheckRetries;
var userOnCase = caseListing.FirstOrDefault(caze => caze.Id == caseId);

if (userOnCase != null)
Expand All @@ -584,10 +592,38 @@ public async Task<bool> AddUserToCase(string userKey, int caseId)
var result = await this.PostAsync($"api/v1/cases/{caseId}/users/{userKey}");
if (result.IsSuccess)
{
this.logger.LogInformation($"Successfully added user {userKey} to case {caseId}");

var userCases = await this.GetUserCases(userKey);
this.logger.LogInformation($"User {userKey} now has access to {userCases.Count()} cases");
return true;

var addedOk = userCases.Any(caze => caze.Id == caseId);
// retry 5 times to get case for user
if (!addedOk)
{
while (caseCheckRetries > 0 && !addedOk)
{
logger.LogInformation($"Case {caseId} not found for user {userKey} - retrying - remaining attempts {caseCheckRetries}");

userCases = await this.GetUserCases(userKey);
addedOk = userCases.Any(caze => caze.Id == caseId);
if (addedOk)
{
break;
}
caseCheckRetries--;
await Task.Delay(this.configuration.CaseCheckTimeoutSeconds * 1000);
}
}
if (addedOk)
{
this.logger.LogInformation($"Successfully added user {userKey} to case {caseId} - user has access to {userCases.Count()} cases");

return true;
}
else
{
this.logger.LogError($"Failed to add user {userKey} to case {caseId} - case not found after retries");
throw new EdtDisclosureServiceException($"Failed to add user {userKey} to case {caseId} - case not found after retries");
}
}
else
{
Expand Down Expand Up @@ -655,7 +691,7 @@ public async Task<bool> AddUserToCase(string userKey, int caseId, string caseGro
private async Task<IEnumerable<UserCaseGroup>> GetUserCaseGroups(string userKey, int caseId)
{
var result = await this.GetAsync<IEnumerable<UserCaseGroup>>($"api/v1/cases/{caseId}/users/{userKey}/groups");
this.logger.LogInformation($"Got {result.Value.Count()} user cases for user {userKey}");
this.logger.LogInformation($"Got {result.Value.Count()} user case groups for user {userKey}");

if (result.IsSuccess)
{
Expand Down Expand Up @@ -814,7 +850,7 @@ public async Task<int> CreateCase(EdtCaseDto caseInfo)
var errorString = string.Join(",", response.Errors);
if (errorString.Contains("request timed out"))
{
this.logger.LogError("Request to create case timeed-out - checking status");
this.logger.LogError("Request to create case timed-out - checking status");
while (retryCount > 0)
{
this.logger.LogInformation($"Attempting retry {retryCount} on case lookup {caseInfo.Key}");
Expand Down Expand Up @@ -857,13 +893,13 @@ public async Task<int> CreateCase(EdtCaseDto caseInfo)
/// <param name="userIdOrKey"></param>
/// <returns></returns>
/// <exception cref="EdtDisclosureServiceException"></exception>
public async Task<IEnumerable<KeyIdPair>> GetUserCases(string userIdOrKey)
public async Task<IEnumerable<CaseStatusModel>> GetUserCases(string userIdOrKey)
{
this.logger.LogInformation($"Getting cases for user {userIdOrKey} org-unit {this.EdtOrgUnitId}");

var apiCall = $"api/v1/org-units/{this.EdtOrgUnitId}/users/{userIdOrKey}/cases";
this.logger.LogInformation($"Calling API {apiCall}");
var response = await this.GetAsync<IEnumerable<KeyIdPair>>(apiCall);
var response = await this.GetAsync<IEnumerable<CaseStatusModel>>(apiCall);

if (response.IsSuccess)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace edt.disclosure.HttpClients.Services.EdtDisclosure;
using System.Collections.Generic;
using Common.Models;
using Common.Models.EDT;
using CommonModels.Models.EDT;
using DIAM.Common.Models;
using edt.disclosure.ServiceEvents.CourtLocation.Models;

Expand Down Expand Up @@ -44,7 +45,7 @@ public interface IEdtDisclosureClient
/// <param name="caseCreation"></param>
/// <returns>Id of the newly created case</returns>
Task<int> CreateCase(EdtCaseDto caseCreation);
Task<IEnumerable<KeyIdPair>> GetUserCases(string? userID);
Task<IEnumerable<CaseStatusModel>> GetUserCases(string? userID);
Task<bool> AddUserToCase(string? userID, int caseID);
Task<bool> AddUserToCase(string? userID, int caseID, string caseGroupName);
Task<bool> AddUserToCase(string? userID, int caseID, int caseGroupID);
Expand Down

0 comments on commit 2cb8a5b

Please sign in to comment.