mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-06 21:42:11 +00:00
fix(services): allow admins to reactivate services
This commit is contained in:
parent
dec8b15278
commit
b3e9a2eec3
3 changed files with 44 additions and 0 deletions
|
|
@ -260,6 +260,38 @@ public class ServicesRegistryServiceTests
|
||||||
updated.IsActive.Should().BeFalse("update preserves the persisted active state");
|
updated.IsActive.Should().BeFalse("update preserves the persisted active state");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Update_WhenAdminRequestsActiveState_ReactivatesService()
|
||||||
|
{
|
||||||
|
using var ctx = NewContext();
|
||||||
|
var service = NewService(ctx);
|
||||||
|
var created = await service.CreateAsync(Admin(), Input("Inactive service"), CancellationToken.None);
|
||||||
|
await service.DeactivateAsync(Admin(), created.Id, CancellationToken.None);
|
||||||
|
|
||||||
|
var input = Input("Inactive service");
|
||||||
|
input.IsActive = true;
|
||||||
|
var updated = await service.UpdateAsync(Admin(), created.Id, input, CancellationToken.None);
|
||||||
|
|
||||||
|
updated.IsActive.Should().BeTrue();
|
||||||
|
ctx.Services.Single().IsActive.Should().BeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Update_WhenSchedulerRequestsActiveState_ThrowsForbidden()
|
||||||
|
{
|
||||||
|
using var ctx = NewContext();
|
||||||
|
var service = NewService(ctx);
|
||||||
|
var created = await service.CreateAsync(Admin(), Input("Inactive service"), CancellationToken.None);
|
||||||
|
await service.DeactivateAsync(Admin(), created.Id, CancellationToken.None);
|
||||||
|
|
||||||
|
var input = Input("Inactive service");
|
||||||
|
input.IsActive = true;
|
||||||
|
var act = () => service.UpdateAsync(Scheduler(), created.Id, input, CancellationToken.None);
|
||||||
|
|
||||||
|
(await act.Should().ThrowAsync<ServicesRegistryValidationException>()).Which.Code.Should().Be("Forbidden");
|
||||||
|
ctx.Services.Single().IsActive.Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Update_WhenNotFound_ThrowsNotFound()
|
public async Task Update_WhenNotFound_ThrowsNotFound()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ namespace SeaHaven.Services.DTOs
|
||||||
public bool RequiresCompletionDocument { get; set; }
|
public bool RequiresCompletionDocument { get; set; }
|
||||||
public int? CompletionDocTemplateId { get; set; }
|
public int? CompletionDocTemplateId { get; set; }
|
||||||
public List<WorkOrderType>? SupportedWorkOrderTypes { get; set; }
|
public List<WorkOrderType>? SupportedWorkOrderTypes { get; set; }
|
||||||
|
public bool? IsActive { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ServicesRegistryValidationErrorDto
|
public class ServicesRegistryValidationErrorDto
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,11 @@ namespace SeaHaven.Services.Implementation
|
||||||
service.IconKey = iconKey;
|
service.IconKey = iconKey;
|
||||||
service.RequiresCompletionDocument = input.RequiresCompletionDocument;
|
service.RequiresCompletionDocument = input.RequiresCompletionDocument;
|
||||||
service.CompletionDocTemplateId = template?.Id;
|
service.CompletionDocTemplateId = template?.Id;
|
||||||
|
if (input.IsActive.HasValue)
|
||||||
|
{
|
||||||
|
EnsureCanChangeActiveState(user);
|
||||||
|
service.IsActive = input.IsActive.Value;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -138,6 +143,12 @@ namespace SeaHaven.Services.Implementation
|
||||||
throw new ServicesRegistryValidationException("Forbidden", "Scheduler or Admin role is required.");
|
throw new ServicesRegistryValidationException("Forbidden", "Scheduler or Admin role is required.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void EnsureCanChangeActiveState(ClaimsPrincipal user)
|
||||||
|
{
|
||||||
|
if (user is null || !user.IsInRole(AdminRole))
|
||||||
|
throw new ServicesRegistryValidationException("Forbidden", "Only administrators can change service active state.");
|
||||||
|
}
|
||||||
|
|
||||||
private static ServicesRegistryValidationException DuplicateName() =>
|
private static ServicesRegistryValidationException DuplicateName() =>
|
||||||
new("DuplicateName", "A service with this name already exists.");
|
new("DuplicateName", "A service with this name already exists.");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue