-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveExpiredJobOpportunities.cs
74 lines (62 loc) · 2.8 KB
/
RemoveExpiredJobOpportunities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Azure.Storage.Blobs;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using CareerMarketplace;
using System.Text;
using System.Text.Json;
namespace appsvc_function_dev_cm_sche_dotnet001
{
public class RemoveExpiredJobOpportunities
{
private readonly ILogger _logger;
public RemoveExpiredJobOpportunities(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<RemoveExpiredJobOpportunities>();
}
[Function("RemoveExpiredJobOpportunities")]
public async Task RunAsync([TimerTrigger("0 0 0 * * *")] TimerInfo myTimer)
{
_logger.LogInformation($"RemoveExpiredJobOpportunities function executed at: {DateTime.Now}");
var blobServiceClient = new BlobServiceClient(Globals.azureWebJobsStorage);
var graphClient = new GraphServiceClient(new ROPCConfidentialTokenCredential(_logger));
var response = await graphClient
.Sites[Globals.siteId]
.Lists[Globals.listId]
.Items
.GetAsync(requestConfiguration =>
{
requestConfiguration.QueryParameters.Expand = ["fields"];
});
var jobOpportunityListItems = response.Value;
var itemIds = new List<string>();
foreach (var item in jobOpportunityListItems)
{
if (item.Fields.AdditionalData.TryGetValue("ApplicationDeadlineDate", out var deadlineDateObj) && deadlineDateObj is DateTime deadlineDate)
{
if (deadlineDate < DateTime.Now)
itemIds.Add(item.Id);
}
else
_logger.LogWarning($"ListItemId: {item.Id} - The 'ApplicationDeadlineDate' field was not found or is not a valid date.");
}
if (itemIds.Any())
{
var data = new
{
ItemId = string.Join(",", itemIds)
};
var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
var httpClient = new HttpClient();
var responseContent = await httpClient.PostAsync(Globals.deleteFunctionUrl, content);
if (responseContent.IsSuccessStatusCode)
{
_logger.LogInformation($"Successfully deleted {itemIds.Count} expired job opportunities.");
}
else
_logger.LogError($"Somethign went wrong: {responseContent.StatusCode} - {responseContent.Content}");
}
_logger.LogInformation("RemoveExpiredJobOpportunities complete.");
}
}
}