Skip to content

Commit

Permalink
Merge branch 'master' into feat/AdjustReportNotifications
Browse files Browse the repository at this point in the history
  • Loading branch information
aleklundeq committed Dec 11, 2023
2 parents be6dd2c + 0aa6cf3 commit e953b7d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Manager
}
public class LineOrgPerson
{
public string? DepartmentSapId { get; set; }
[JsonProperty("azureUniqueId")] public string AzureUniqueId { get; set; }

[JsonProperty("managerId")] public string ManagerId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Fusion.Resources.Functions.ApiClients;

public interface ILineOrgApiClient
{
Task<IEnumerable<string>> GetOrgUnitDepartmentsAsync();
Task<List<LineOrgPerson>> GetResourceOwnersFromFullDepartment(List<string> fullDepartments);
Task<IEnumerable<LineOrgApiClient.OrgUnits>> GetOrgUnitDepartmentsAsync();
Task<List<LineOrgPerson>> GetResourceOwnersFromFullDepartment(List<LineOrgApiClient.OrgUnits> fullDepartments);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,36 @@ public LineOrgApiClient(IHttpClientFactory httpClientFactory)
lineOrgClient.Timeout = TimeSpan.FromMinutes(5);
}

public async Task<IEnumerable<string>> GetOrgUnitDepartmentsAsync()
public async Task<IEnumerable<OrgUnits>> GetOrgUnitDepartmentsAsync()
{
var data =
await lineOrgClient.GetAsJsonAsync<InternalCollection<DepartmentRef>>($"/org-units?$top={int.MaxValue}");
await lineOrgClient.GetAsJsonAsync<InternalCollection<OrgUnits>>($"/org-units?$top={int.MaxValue}");

return data.Value
.Where(x => !string.IsNullOrEmpty(x.FullDepartment))
.Select(x => x.FullDepartment!).ToList();
.ToList();
}

public async Task<List<LineOrgPerson>> GetResourceOwnersFromFullDepartment(List<string> fullDepartments)
public async Task<List<LineOrgPerson>> GetResourceOwnersFromFullDepartment(List<OrgUnits> fullDepartments)
{
var list = fullDepartments
.Select(l => $"'{l}'")
.Select(l => $"'{l.FullDepartment?.Replace("&", "%26")}'")
.ToList()
.Aggregate((a, b) => $"{a}, {b}");
var queryString = $"/lineorg/persons?$filter=fullDepartment in " +
$"({list}) " +
$"and isResourceOwner eq 'true'";
var resourceOwners = await lineOrgClient.GetAsJsonAsync<LineOrgPersonsResponse>(queryString);
foreach (var r in resourceOwners.Value)
r.DepartmentSapId = fullDepartments.FirstOrDefault(x => x.FullDepartment == r.FullDepartment)?.SapId;

return resourceOwners.Value;
}

internal class DepartmentRef
public class OrgUnits
{
public string? FullDepartment { get; set; }
public string? SapId { get; set; }
}

internal class InternalCollection<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task ReassignResourceAllocationRequestsWithInvalidDepartment([Timer
log.LogTrace($"Next occurrences: {timer.FormatNextOccurrences(3)}");

var activeProjects = await resourcesClient.GetProjectsAsync();
var activeDepartments = (await lineOrgClient.GetOrgUnitDepartmentsAsync()).ToList();
var activeDepartments = (await lineOrgClient.GetOrgUnitDepartmentsAsync()).Select(o => o.FullDepartment).ToList();

foreach (var project in activeProjects)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class ScheduledNotificationQueueDto
{
public string AzureUniqueId { get; set; }
public string FullDepartment { get; set; }
public string DepartmentSapId { get; set; }
public NotificationRoleType Role { get; set; }
}
public enum NotificationRoleType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
using Fusion.Resources.Functions.ApiClients;
using Fusion.Resources.Functions.ApiClients.ApiModels;
using Fusion.Resources.Functions.Functions.Notifications.Models.DTOs;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Configuration;
Expand All @@ -31,7 +32,7 @@ public ScheduledReportTimerTriggerFunction(ILineOrgApiClient lineOrgApiClient,

[FunctionName("scheduled-report-timer-trigger-function")]
public async Task RunAsync(
[TimerTrigger("0 0 0 * * MON", RunOnStartup = false)]
[TimerTrigger("0 0 8 * * MON", RunOnStartup = false)]
TimerInfo scheduledReportTimer)
{
_logger.LogInformation(
Expand Down Expand Up @@ -60,23 +61,19 @@ private async Task SendResourceOwnersToQueue(ServiceBusSender sender)
{
try
{
var departments = await _lineOrgClient.GetOrgUnitDepartmentsAsync();
if (departments == null || !departments.Any())
throw new Exception("No departments found.");

// TODO: These resource-owners are handpicked to limit the scope of the project.
var resourceOwners =
await _lineOrgClient.GetResourceOwnersFromFullDepartment(
new List<string>
{
"PDP PRD PMC PCA PCA1",
"PDP PRD PMC PCA PCA2",
"PDP PRD PMC PCA PCA3",
"PDP PRD PMC PCA PCA4",
"PDP PRD PMC PCA PCA5",
"PDP PRD PMC PCA PCA6",
"CFO FCOE PO CPC DA SOL"
});
var selectedDepartments = departments
.Where(d => d.FullDepartment != null && d.FullDepartment.Contains("PRD")).Distinct().ToList();
var resourceOwners = await GetLineOrgPersonsFromDepartmetnsChunked(selectedDepartments);

if (resourceOwners == null || !resourceOwners.Any())
throw new Exception("No resource-owners found.");

foreach (var resourceOwner in resourceOwners)
foreach (var resourceOwner in resourceOwners.DistinctBy(ro => ro.AzureUniqueId))
{
try
{
Expand Down Expand Up @@ -106,6 +103,21 @@ await _lineOrgClient.GetResourceOwnersFromFullDepartment(
}
}

private async Task<List<LineOrgPerson>> GetLineOrgPersonsFromDepartmetnsChunked(List<LineOrgApiClient.OrgUnits> selectedDepartments)
{
var resourceOwners = new List<LineOrgPerson>();
const int chuckSize = 10;
for (var i = 0; i < selectedDepartments.Count; i += chuckSize)
{
var chunk = selectedDepartments.Skip(i).Take(chuckSize).ToList();
var chunkedResourceOwners =
await _lineOrgClient.GetResourceOwnersFromFullDepartment(chunk);
resourceOwners.AddRange(chunkedResourceOwners);
}

return resourceOwners;
}

private async Task SendDtoToQueue(ServiceBusSender sender, ScheduledNotificationQueueDto dto)
{
var serializedDto = JsonConvert.SerializeObject(dto);
Expand Down

0 comments on commit e953b7d

Please sign in to comment.