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

feat(functions): App departments within PRD is now eligible for weekly report #607

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
}
public class LineOrgPerson
{
public string? DepartmentSapId { get; set; }

Check warning on line 33 in src/backend/function/Fusion.Resources.Functions/ApiClients/ApiModels/LineOrg.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
[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
Loading