Skip to content

Commit

Permalink
Merge pull request #6257 from bcgov/dev
Browse files Browse the repository at this point in the history
Hotfix to resolve remove dependent issue on mobile app
  • Loading branch information
MikeLyttle authored Jul 11, 2024
2 parents a5a075c + 4a06aca commit 187a9e8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 62 deletions.
13 changes: 3 additions & 10 deletions Apps/GatewayApi/src/Controllers/DependentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ public async Task<RequestResult<DependentModel>> AddDependent([FromBody] AddDepe
/// <summary>
/// Deletes a dependent from the database.
/// </summary>
/// <returns>The http status.</returns>
/// <returns>An empty dependent model wrapped in a RequestResult.</returns>
/// <param name="hdid">The Delegate hdid.</param>
/// <param name="dependentHdid">The Dependent hdid.</param>
/// <param name="dependent">The dependent model object to be deleted.</param>
/// <param name="ct"><see cref="CancellationToken"/> to manage the async request.</param>
/// <response code="200">The Dependent record was deleted.</response>
/// <response code="400">The request is invalid.</response>
Expand All @@ -130,15 +129,9 @@ public async Task<RequestResult<DependentModel>> AddDependent([FromBody] AddDepe
[HttpDelete]
[Authorize(Policy = UserProfilePolicy.Write)]
[Route("{hdid}/[controller]/{dependentHdid}")]
public async Task<ActionResult<RequestResult<DependentModel>>> Delete(string hdid, string dependentHdid, [FromBody] DependentModel dependent, CancellationToken ct)
public async Task<ActionResult<RequestResult<DependentModel>>> Delete(string hdid, string dependentHdid, CancellationToken ct)
{
if (dependent.OwnerId != dependentHdid || dependent.DelegateId != hdid)
{
this.logger.LogError("Parameters do not match body of delete");
return new BadRequestResult();
}

return await this.dependentService.RemoveAsync(dependent, ct);
return await this.dependentService.RemoveAsync(hdid, dependentHdid, ct);
}
}
}
10 changes: 5 additions & 5 deletions Apps/GatewayApi/src/Services/DependentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ public async Task<RequestResult<IEnumerable<GetDependentResponse>>> GetDependent
}

/// <inheritdoc/>
public async Task<RequestResult<DependentModel>> RemoveAsync(DependentModel dependent, CancellationToken ct = default)
public async Task<RequestResult<DependentModel>> RemoveAsync(string delegateHdid, string dependentHdid, CancellationToken ct = default)
{
ResourceDelegate? resourceDelegate = (await this.resourceDelegateDelegate.GetAsync(dependent.DelegateId, 0, 500, ct)).FirstOrDefault(d => d.ResourceOwnerHdid == dependent.OwnerId);
ResourceDelegate? resourceDelegate = (await this.resourceDelegateDelegate.GetAsync(delegateHdid, 0, 500, ct)).FirstOrDefault(d => d.ResourceOwnerHdid == dependentHdid);
if (resourceDelegate == null)
{
throw new NotFoundException($"Dependent {dependent.OwnerId} not found for delegate {dependent.DelegateId}");
throw new NotFoundException($"Dependent {dependentHdid} not found for delegate {delegateHdid}");
}

// commit to the database if change feed is disabled; if change feed enabled, commit will happen when message sender is called
Expand All @@ -255,11 +255,11 @@ public async Task<RequestResult<DependentModel>> RemoveAsync(DependentModel depe
throw new DatabaseException(dbDependent.Message);
}

await this.UpdateNotificationSettingsAsync(dependent.OwnerId, dependent.DelegateId, true, ct);
await this.UpdateNotificationSettingsAsync(dependentHdid, delegateHdid, true, ct);

if (this.dependentsChangeFeedEnabled)
{
await this.messageSender.SendAsync(new[] { new MessageEnvelope(new DependentRemovedEvent(dependent.DelegateId, dependent.OwnerId), dependent.DelegateId) }, ct);
await this.messageSender.SendAsync([new MessageEnvelope(new DependentRemovedEvent(delegateHdid, dependentHdid), delegateHdid)], ct);
}

return RequestResultFactory.Success(new DependentModel());
Expand Down
9 changes: 5 additions & 4 deletions Apps/GatewayApi/src/Services/IDependentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ public interface IDependentService
Task<RequestResult<DependentModel>> AddDependentAsync(string delegateHdid, AddDependentRequest addDependentRequest, CancellationToken ct = default);

/// <summary>
/// Removes a dependent delegate relation.
/// Removes a dependent-delegate relation.
/// </summary>
/// <param name="dependent">The dependent model to be deleted.</param>
/// <param name="delegateHdid">The HDID of the delegate.</param>
/// <param name="dependentHdid">The HDID of the dependent.</param>
/// <param name="ct"><see cref="CancellationToken"/> to manage the async request.</param>
/// <returns>A dependent model wrapped in a RequestResult.</returns>
Task<RequestResult<DependentModel>> RemoveAsync(DependentModel dependent, CancellationToken ct = default);
/// <returns>An empty dependent model wrapped in a RequestResult.</returns>
Task<RequestResult<DependentModel>> RemoveAsync(string delegateHdid, string dependentHdid, CancellationToken ct = default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,59 +122,26 @@ public async Task ShouldDeleteDependent()
{
string delegateId = this.hdid;
string dependentId = "123";
DependentModel dependentModel = new() { DelegateId = delegateId, OwnerId = dependentId };

RequestResult<DependentModel> expectedResult = new()
{
ResourcePayload = dependentModel,
ResourcePayload = new(),
ResultStatus = ResultType.Success,
};

Mock<IDependentService> dependentServiceMock = new();
dependentServiceMock.Setup(s => s.RemoveAsync(dependentModel, CancellationToken.None)).ReturnsAsync(expectedResult);
dependentServiceMock.Setup(s => s.RemoveAsync(delegateId, dependentId, CancellationToken.None)).ReturnsAsync(expectedResult);

Mock<IHttpContextAccessor> httpContextAccessorMock = CreateValidHttpContext(this.token, this.userId, this.hdid);

DependentController dependentController = new(
new Mock<ILogger<DependentController>>().Object,
dependentServiceMock.Object,
httpContextAccessorMock.Object);
ActionResult<RequestResult<DependentModel>> actualResult = await dependentController.Delete(delegateId, dependentId, dependentModel, CancellationToken.None);
ActionResult<RequestResult<DependentModel>> actualResult = await dependentController.Delete(delegateId, dependentId, CancellationToken.None);
actualResult.Value.ShouldDeepEqual(expectedResult);
}

/// <summary>
/// DeleteDependent - BadRequest path scenario.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task ShouldFailDeleteDependent()
{
string delegateId = this.hdid;
string dependentId = "123";
DependentModel dependentModel = new() { DelegateId = delegateId, OwnerId = dependentId };

RequestResult<DependentModel> expectedResult = new()
{
ResourcePayload = dependentModel,
ResultStatus = ResultType.Success,
};

Mock<IDependentService> dependentServiceMock = new();
dependentServiceMock.Setup(s => s.RemoveAsync(dependentModel, CancellationToken.None)).ReturnsAsync(expectedResult);

Mock<IHttpContextAccessor> httpContextAccessorMock = CreateValidHttpContext(this.token, this.userId, delegateId);

DependentController dependentController = new(
new Mock<ILogger<DependentController>>().Object,
dependentServiceMock.Object,
httpContextAccessorMock.Object);

ActionResult<RequestResult<DependentModel>> actualResult = await dependentController.Delete("anotherId", "wrongId", dependentModel, CancellationToken.None);

Assert.IsType<BadRequestResult>(actualResult.Result);
}

private static IEnumerable<DependentModel> GetMockDependents()
{
List<DependentModel> dependentModels = [];
Expand Down
11 changes: 4 additions & 7 deletions Apps/GatewayApi/test/unit/Services.Test/DependentServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,28 +410,26 @@ public async Task ValidateAddProtectedDependentNotAllowed()
[Fact]
public async Task ValidateRemove()
{
DependentModel delegateModel = new() { OwnerId = this.mockHdId, DelegateId = this.mockParentHdid };
IList<ResourceDelegate> resourceDelegates = new[] { new ResourceDelegate { ProfileHdid = this.mockParentHdid, ResourceOwnerHdid = this.mockHdId } };
DbResult<ResourceDelegate> dbResult = new()
{
Status = DbStatusCode.Deleted,
};
IDependentService service = this.SetupMockForRemoveDependent(resourceDelegates, dbResult);

RequestResult<DependentModel> actualResult = await service.RemoveAsync(delegateModel);
RequestResult<DependentModel> actualResult = await service.RemoveAsync(this.mockParentHdid, this.mockHdId);

Assert.Equal(ResultType.Success, actualResult.ResultStatus);
}

[Fact]
public async Task ValidateRemoveNotFound()
{
DependentModel delegateModel = new() { OwnerId = this.mockHdId, DelegateId = this.mockParentHdid };
IDependentService service = this.SetupMockForRemoveDependent([], new());

async Task Actual()
{
await service.RemoveAsync(delegateModel);
await service.RemoveAsync(this.mockParentHdid, this.mockHdId);
}

await Assert.ThrowsAsync<NotFoundException>(Actual);
Expand All @@ -440,8 +438,7 @@ async Task Actual()
[Fact]
public async Task ValidateRemoveDatabaseException()
{
DependentModel delegateModel = new() { OwnerId = this.mockHdId, DelegateId = this.mockParentHdid };
IList<ResourceDelegate> resourceDelegates = new[] { new ResourceDelegate { ProfileHdid = this.mockParentHdid, ResourceOwnerHdid = this.mockHdId } };
IList<ResourceDelegate> resourceDelegates = [new ResourceDelegate { ProfileHdid = this.mockParentHdid, ResourceOwnerHdid = this.mockHdId }];
DbResult<ResourceDelegate> dbResult = new()
{
Status = DbStatusCode.Error,
Expand All @@ -450,7 +447,7 @@ public async Task ValidateRemoveDatabaseException()

async Task Actual()
{
await service.RemoveAsync(delegateModel);
await service.RemoveAsync(this.mockParentHdid, this.mockHdId);
}

await Assert.ThrowsAsync<DatabaseException>(Actual);
Expand Down

0 comments on commit 187a9e8

Please sign in to comment.