Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Summary): Filter departments to PRD and also send report to delegated responsibles #675

Merged
merged 9 commits into from
Aug 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,17 @@ public Task PutWeeklySummaryReportAsync(string departmentSapId, ApiWeeklySummary

public class ApiResourceOwnerDepartment
{
public ApiResourceOwnerDepartment(string departmentSapId, string fullDepartmentName,
Guid resourceOwnerAzureUniqueId)
{
DepartmentSapId = departmentSapId;
FullDepartmentName = fullDepartmentName;
ResourceOwnerAzureUniqueId = resourceOwnerAzureUniqueId;
}

public ApiResourceOwnerDepartment()
{
}

public string DepartmentSapId { get; init; } = string.Empty;
public string DepartmentSapId { get; init; } = null!;
public string FullDepartmentName { get; init; } = null!;

public Guid[] ResourceOwnersAzureUniqueId { get; init; } = null!;

public string FullDepartmentName { get; init; } = string.Empty;
public Guid[] DelegateResourceOwnersAzureUniqueId { get; init; } = null!;

public Guid ResourceOwnerAzureUniqueId { get; init; }
}

public record ApiCollection<T>(ICollection<T> Items);
Expand Down
6 changes: 6 additions & 0 deletions src/Fusion.Summary.Api/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ protected Task<TResult> DispatchAsync<TResult>(IRequest<TResult> command)
var profileResolver = HttpContext.RequestServices.GetRequiredService<IFusionProfileResolver>();
return await profileResolver.ResolvePersonBasicProfileAsync(personId);
}

protected async Task<IEnumerable<ResolvedPersonProfile>> ResolvePersonsAsync(IEnumerable<PersonIdentifier> personIdentifiers)
{
var profileResolver = HttpContext.RequestServices.GetRequiredService<IFusionProfileResolver>();
return await profileResolver.ResolvePersonsAsync(personIdentifiers);
}
}
10 changes: 7 additions & 3 deletions src/Fusion.Summary.Api/Controllers/ApiModels/ApiDepartment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ namespace Fusion.Summary.Api.Controllers.ApiModels;
public class ApiDepartment
{
public string DepartmentSapId { get; set; } = string.Empty;
public Guid ResourceOwnerAzureUniqueId { get; set; }
public string FullDepartmentName { get; set; } = string.Empty;

public Guid[] ResourceOwnersAzureUniqueId { get; init; } = null!;

public Guid[] DelegateResourceOwnersAzureUniqueId { get; init; } = null!;

public static ApiDepartment FromQueryDepartment(QueryDepartment queryDepartment)
{
return new ApiDepartment
{
DepartmentSapId = queryDepartment.SapDepartmentId,
ResourceOwnerAzureUniqueId = queryDepartment.ResourceOwnerAzureUniqueId,
FullDepartmentName = queryDepartment.FullDepartmentName
FullDepartmentName = queryDepartment.FullDepartmentName,
ResourceOwnersAzureUniqueId = queryDepartment.ResourceOwnersAzureUniqueId.ToArray(),
DelegateResourceOwnersAzureUniqueId = queryDepartment.DelegateResourceOwnersAzureUniqueId.ToArray()
};
}
}
30 changes: 22 additions & 8 deletions src/Fusion.Summary.Api/Controllers/DepartmentsController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Fusion.AspNetCore.FluentAuthorization;
using Fusion.Authorization;
using Fusion.Integration.Profile;
using Fusion.Summary.Api.Authorization.Extensions;
using Fusion.Summary.Api.Controllers.ApiModels;
using Fusion.Summary.Api.Controllers.Requests;
Expand Down Expand Up @@ -121,8 +122,17 @@ public async Task<IActionResult> PutV1(string sapDepartmentId, [FromBody] PutDep
if (string.IsNullOrWhiteSpace(sapDepartmentId))
return BadRequest("SapDepartmentId route parameter is required");

if (await ResolvePersonAsync(request.ResourceOwnerAzureUniqueId) is null)
return BadRequest("Resource owner not found in azure ad");
var personIdentifiers = request.ResourceOwnersAzureUniqueId
.Concat(request.DelegateResourceOwnersAzureUniqueId)
.Select(p => new PersonIdentifier(p));

var unresolvedProfiles = (await ResolvePersonsAsync(personIdentifiers))
.Where(r => !r.Success)
.ToList();

if (unresolvedProfiles.Count != 0)
return BadRequest($"Profiles: {string.Join(',', unresolvedProfiles)} could not be resolved");


var department = await DispatchAsync(new GetDepartment(sapDepartmentId));

Expand All @@ -132,19 +142,23 @@ public async Task<IActionResult> PutV1(string sapDepartmentId, [FromBody] PutDep
await DispatchAsync(
new CreateDepartment(
sapDepartmentId,
request.ResourceOwnerAzureUniqueId,
request.FullDepartmentName));
request.FullDepartmentName,
request.ResourceOwnersAzureUniqueId,
request.DelegateResourceOwnersAzureUniqueId));

return Created();
}
// Check if department owner has changed
else if (department.ResourceOwnerAzureUniqueId != request.ResourceOwnerAzureUniqueId)

// Check if department owners has changed
if (!department.ResourceOwnersAzureUniqueId.SequenceEqual(request.ResourceOwnersAzureUniqueId) ||
!department.DelegateResourceOwnersAzureUniqueId.SequenceEqual(request.DelegateResourceOwnersAzureUniqueId))
{
await DispatchAsync(
new UpdateDepartment(
sapDepartmentId,
request.ResourceOwnerAzureUniqueId,
request.FullDepartmentName));
request.FullDepartmentName,
request.ResourceOwnersAzureUniqueId,
request.DelegateResourceOwnersAzureUniqueId));

return Ok();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Fusion.Summary.Api.Controllers.Requests;

public record PutDepartmentRequest(string FullDepartmentName, Guid ResourceOwnerAzureUniqueId)
public record PutDepartmentRequest(string FullDepartmentName, Guid[] ResourceOwnersAzureUniqueId, Guid[] DelegateResourceOwnersAzureUniqueId)
{
public class Validator : AbstractValidator<PutDepartmentRequest>
{
public Validator()
{
RuleFor(x => x.FullDepartmentName).NotEmpty();
RuleFor(x => x.ResourceOwnerAzureUniqueId).NotEmpty();
RuleFor(x => x.ResourceOwnersAzureUniqueId.Concat(x.DelegateResourceOwnersAzureUniqueId))
.NotEmpty()
.WithMessage($"Either {nameof(ResourceOwnersAzureUniqueId)} or {nameof(DelegateResourceOwnersAzureUniqueId)} must contain at least one element.");
}
}
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ protected override void Up(MigrationBuilder migrationBuilder)
columns: table => new
{
DepartmentSapId = table.Column<string>(type: "nvarchar(450)", nullable: false),
ResourceOwnerAzureUniqueId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
FullDepartmentName = table.Column<string>(type: "nvarchar(max)", nullable: false)
FullDepartmentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
ResourceOwnersAzureUniqueId = table.Column<string>(type: "nvarchar(max)", nullable: false),
DelegateResourceOwnersAzureUniqueId = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<string>("DepartmentSapId")
.HasColumnType("nvarchar(450)");

b.Property<string>("DelegateResourceOwnersAzureUniqueId")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<string>("FullDepartmentName")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<Guid>("ResourceOwnerAzureUniqueId")
.HasColumnType("uniqueidentifier");
b.Property<string>("ResourceOwnersAzureUniqueId")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.HasKey("DepartmentSapId");

Expand Down
10 changes: 7 additions & 3 deletions src/Fusion.Summary.Api/Database/Models/DbDepartment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ namespace Fusion.Summary.Api.Database.Models;
public class DbDepartment
{
public string DepartmentSapId { get; set; } = string.Empty;
public Guid ResourceOwnerAzureUniqueId { get; set; }
public string FullDepartmentName { get; set; } = string.Empty;

public List<Guid> ResourceOwnersAzureUniqueId { get; set; } = [];

public List<Guid> DelegateResourceOwnersAzureUniqueId { get; set; } = [];

public static DbDepartment FromQueryDepartment(QueryDepartment queryDepartment)
{
return new DbDepartment
{
DepartmentSapId = queryDepartment.SapDepartmentId,
ResourceOwnerAzureUniqueId = queryDepartment.ResourceOwnerAzureUniqueId,
FullDepartmentName = queryDepartment.FullDepartmentName
FullDepartmentName = queryDepartment.FullDepartmentName,
ResourceOwnersAzureUniqueId = queryDepartment.ResourceOwnersAzureUniqueId.ToList(),
DelegateResourceOwnersAzureUniqueId = queryDepartment.DelegateResourceOwnersAzureUniqueId.ToList()
};
}

Expand Down
10 changes: 6 additions & 4 deletions src/Fusion.Summary.Api/Domain/Commands/CreateDepartment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ public class CreateDepartment : IRequest
{
private QueryDepartment _queryDepartment;

public CreateDepartment(string SapDepartmentId, Guid ResourceOwnerAzureUniqueId, string FullDepartmentName)
public CreateDepartment(string sapDepartmentId, string fullDepartmentName,
IEnumerable<Guid> resourceOwnersAzureUniqueId, IEnumerable<Guid> delegateResourceOwnersAzureUniqueId)
{
_queryDepartment = new QueryDepartment
{
SapDepartmentId = SapDepartmentId,
ResourceOwnerAzureUniqueId = ResourceOwnerAzureUniqueId,
FullDepartmentName = FullDepartmentName
SapDepartmentId = sapDepartmentId,
FullDepartmentName = fullDepartmentName,
ResourceOwnersAzureUniqueId = resourceOwnersAzureUniqueId.ToList(),
DelegateResourceOwnersAzureUniqueId = delegateResourceOwnersAzureUniqueId.ToList()
};
}

Expand Down
18 changes: 9 additions & 9 deletions src/Fusion.Summary.Api/Domain/Commands/UpdateDepartment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ public class UpdateDepartment : IRequest
{
private QueryDepartment _queryDepartment;

public UpdateDepartment(string SapDepartmentId, Guid ResourceOwnerAzureUniqueId, string FullDepartmentName)
public UpdateDepartment(string sapDepartmentId, string fullDepartmentName,
IEnumerable<Guid> resourceOwnersAzureUniqueId, IEnumerable<Guid> delegateResourceOwnersAzureUniqueId)
{
_queryDepartment = new QueryDepartment
{
SapDepartmentId = SapDepartmentId,
ResourceOwnerAzureUniqueId = ResourceOwnerAzureUniqueId,
FullDepartmentName = FullDepartmentName
SapDepartmentId = sapDepartmentId,
FullDepartmentName = fullDepartmentName,
ResourceOwnersAzureUniqueId = resourceOwnersAzureUniqueId.ToList(),
DelegateResourceOwnersAzureUniqueId = delegateResourceOwnersAzureUniqueId.ToList()
};
}

Expand All @@ -35,12 +37,10 @@ public async Task Handle(UpdateDepartment request, CancellationToken cancellatio

if (existingDepartment != null)
{
if (existingDepartment.ResourceOwnerAzureUniqueId != request._queryDepartment.ResourceOwnerAzureUniqueId)
{
existingDepartment.ResourceOwnerAzureUniqueId = request._queryDepartment.ResourceOwnerAzureUniqueId;
existingDepartment.ResourceOwnersAzureUniqueId = request._queryDepartment.ResourceOwnersAzureUniqueId.ToList();
existingDepartment.DelegateResourceOwnersAzureUniqueId = request._queryDepartment.DelegateResourceOwnersAzureUniqueId.ToList();

await _context.SaveChangesAsync();
}
await _context.SaveChangesAsync();
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/Fusion.Summary.Api/Domain/Models/QueryDepartment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ namespace Fusion.Summary.Api.Domain.Models;
public class QueryDepartment
{
public string SapDepartmentId { get; set; } = string.Empty;
public Guid ResourceOwnerAzureUniqueId { get; set; }
public string FullDepartmentName { get; set; } = string.Empty;
public List<Guid> ResourceOwnersAzureUniqueId { get; set; } = null!;

public List<Guid> DelegateResourceOwnersAzureUniqueId { get; set; } = null!;

public static QueryDepartment FromDbDepartment(DbDepartment dbDepartment)
{
return new QueryDepartment
{
SapDepartmentId = dbDepartment.DepartmentSapId,
ResourceOwnerAzureUniqueId = dbDepartment.ResourceOwnerAzureUniqueId,
FullDepartmentName = dbDepartment.FullDepartmentName
FullDepartmentName = dbDepartment.FullDepartmentName,
ResourceOwnersAzureUniqueId = dbDepartment.ResourceOwnersAzureUniqueId.ToList(),
DelegateResourceOwnersAzureUniqueId = dbDepartment.DelegateResourceOwnersAzureUniqueId.ToList()
};
}

Expand All @@ -24,7 +27,9 @@ public static QueryDepartment FromApiDepartment(ApiDepartment apiDepartment)
return new QueryDepartment
{
SapDepartmentId = apiDepartment.DepartmentSapId,
FullDepartmentName = apiDepartment.FullDepartmentName
FullDepartmentName = apiDepartment.FullDepartmentName,
ResourceOwnersAzureUniqueId = apiDepartment.ResourceOwnersAzureUniqueId.ToList(),
DelegateResourceOwnersAzureUniqueId = apiDepartment.DelegateResourceOwnersAzureUniqueId.ToList()
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
"environment": "pr",
"disabledFunctions": [
"department-resource-owner-sync",
"weekly-report-sender",
"weekly-department-recipients-sync",
"weekly-department-summary-sender",
"weekly-department-summary-worker"
]
}
Expand Down
Loading
Loading