-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Apps.XTM/DataSourceHandlers/EnumHandlers/ProjectStatusHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Blackbird.Applications.Sdk.Common.Dictionaries; | ||
|
||
namespace Apps.XTM.DataSourceHandlers.EnumHandlers; | ||
|
||
public class ProjectStatusHandler : IStaticDataSourceHandler | ||
{ | ||
public Dictionary<string, string> GetData() | ||
{ | ||
return new() | ||
{ | ||
["NOT_STARTED"] = "Not started", | ||
["STARTED"] = "Started", | ||
["FINISHED"] = "Finished" | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Apps.XTM.Polling.Models.Memory; | ||
|
||
public class DateMemory | ||
{ | ||
public DateTime LastInteractionDate { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Apps.XTM.Polling.Models.Memory; | ||
|
||
public class ProjectStatusMemory | ||
{ | ||
public string Status { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
using System.Globalization; | ||
using Apps.XTM.Constants; | ||
using Apps.XTM.DataSourceHandlers.EnumHandlers; | ||
using Apps.XTM.Invocables; | ||
using Apps.XTM.Models.Request.Projects; | ||
using Apps.XTM.Models.Response.Projects; | ||
using Apps.XTM.Polling.Models.Memory; | ||
using Blackbird.Applications.Sdk.Common; | ||
using Blackbird.Applications.Sdk.Common.Dictionaries; | ||
using Blackbird.Applications.Sdk.Common.Invocation; | ||
using Blackbird.Applications.Sdk.Common.Polling; | ||
using RestSharp; | ||
|
||
namespace Apps.XTM.Polling; | ||
|
||
[PollingEventList] | ||
public class PollingList : XtmInvocable | ||
{ | ||
public PollingList(InvocationContext invocationContext) : base(invocationContext) | ||
{ | ||
} | ||
|
||
[PollingEvent("On projects created", "On any new projects created")] | ||
public Task<PollingEventResponse<DateMemory, ListProjectsResponse>> OnProjectsCreated( | ||
PollingEventRequest<DateMemory> request) => ProcessProjectsPolling(request, | ||
$"createdDateFrom={request.Memory?.LastInteractionDate.ToString("o", CultureInfo.InvariantCulture)}"); | ||
|
||
[PollingEvent("On projects updated", "On any projects are updated")] | ||
public Task<PollingEventResponse<DateMemory, ListProjectsResponse>> OnProjectsUpdated( | ||
PollingEventRequest<DateMemory> request) => ProcessProjectsPolling(request, | ||
$"modifiedDateFrom={request.Memory?.LastInteractionDate.ToString("o", CultureInfo.InvariantCulture)}"); | ||
|
||
[PollingEvent("On projects finished", "On any projects are finished")] | ||
public Task<PollingEventResponse<DateMemory, ListProjectsResponse>> OnProjectsFinished( | ||
PollingEventRequest<DateMemory> request) => ProcessProjectsPolling(request, | ||
$"finishedDateFrom={request.Memory?.LastInteractionDate.ToString("o", CultureInfo.InvariantCulture)}"); | ||
|
||
[PollingEvent("On project status changed", "On status of the specific project changed")] | ||
public async Task<PollingEventResponse<ProjectStatusMemory, SimpleProject>> OnProjectStatusChanged( | ||
PollingEventRequest<ProjectStatusMemory> request, | ||
[PollingEventParameter] ProjectRequest project, | ||
[PollingEventParameter] [Display("Project status")] [StaticDataSource(typeof(ProjectStatusHandler))] | ||
string? status) | ||
{ | ||
var projectEntity = (await Client.ExecuteXtmWithJson<List<SimpleProject>>( | ||
$"{ApiEndpoints.Projects}?ids={project.ProjectId}", | ||
Method.Get, | ||
null, | ||
Creds)).FirstOrDefault() ?? throw new InvalidOperationException("Not project found with the provided ID"); | ||
|
||
if (request.Memory is null) | ||
{ | ||
return new() | ||
{ | ||
FlyBird = false, | ||
Memory = new() | ||
{ | ||
Status = projectEntity.Status | ||
} | ||
}; | ||
} | ||
|
||
return new() | ||
{ | ||
FlyBird = request.Memory.Status != projectEntity.Status && | ||
(status == null || projectEntity.Status == status), | ||
Result = projectEntity, | ||
Memory = new() | ||
{ | ||
Status = projectEntity.Status | ||
} | ||
}; | ||
} | ||
|
||
private async Task<PollingEventResponse<DateMemory, ListProjectsResponse>> ProcessProjectsPolling( | ||
PollingEventRequest<DateMemory> request, | ||
string query) | ||
{ | ||
if (request.Memory is null) | ||
{ | ||
return new() | ||
{ | ||
FlyBird = false, | ||
Memory = new() | ||
{ | ||
LastInteractionDate = DateTime.UtcNow | ||
} | ||
}; | ||
} | ||
|
||
var result = new List<SimpleProject>(); | ||
var page = 1; | ||
|
||
List<SimpleProject> response; | ||
do | ||
{ | ||
response = await Client.ExecuteXtmWithJson<List<SimpleProject>>( | ||
$"{ApiEndpoints.Projects}?{query}&page={page}", | ||
Method.Get, | ||
null, | ||
Creds); | ||
result.AddRange(response); | ||
|
||
page++; | ||
} while (response.Any()); | ||
|
||
if (!result.Any()) | ||
{ | ||
return new() | ||
{ | ||
FlyBird = false, | ||
Memory = new() | ||
{ | ||
LastInteractionDate = DateTime.UtcNow | ||
} | ||
}; | ||
} | ||
|
||
return new() | ||
{ | ||
FlyBird = true, | ||
Result = new(result), | ||
Memory = new() | ||
{ | ||
LastInteractionDate = DateTime.UtcNow | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters