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

Added polling events #17

Merged
merged 11 commits into from
Jun 27, 2024
5 changes: 2 additions & 3 deletions Apps.XTM/Apps.XTM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Product>XTM</Product>
<Version>3.1.5</Version>
<Version>3.2.0</Version>
<Description>Translation management system that centralizes localization assets and enables fast, accurate translation and deployment of content tailored to any audience in any territory</Description>
<AssemblyName>Apps.XTM</AssemblyName>
<LangVersion>12</LangVersion>
Expand All @@ -18,8 +18,7 @@
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.10.*"/>
<PackageReference Include="System.ServiceModel.Primitives" Version="8.0.0"/>
<PackageReference Include="System.ServiceModel.Security" Version="4.10.*"/>
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.6.0"/>
<PackageReference Include="Blackbird.Applications.Sdk.Utils" Version="1.0.17"/>
<PackageReference Include="Blackbird.Applications.Sdk.Common" Version="2.8.0"/>
</ItemGroup>
<ItemGroup>
<EmbeddedResource CopyToOutputDirectory="Always" Include="image\icon.png"/>
Expand Down
16 changes: 16 additions & 0 deletions Apps.XTM/DataSourceHandlers/EnumHandlers/ProjectStatusHandler.cs
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"
};
}
}
2 changes: 2 additions & 0 deletions Apps.XTM/Models/Response/Projects/FullProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public class FullProject
[Display("Target languages")] public IEnumerable<string> TargetLanguages { get; set; }

[Display("Template ID")] public string TemplateId { get; set; }

public string Status { get; set; }
}
6 changes: 6 additions & 0 deletions Apps.XTM/Polling/Models/Memory/DateMemory.cs
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; }
}
6 changes: 6 additions & 0 deletions Apps.XTM/Polling/Models/Memory/ProjectStatusMemory.cs
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; }
}
129 changes: 129 additions & 0 deletions Apps.XTM/Polling/PollingList.cs
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 (polling)", "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 (polling)", "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 (polling)", "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 (polling)", "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
}
};
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ Before you can connect you need to make sure that:
- **On project accepted**
- **On project finished**
- **On invoice status changed**
- **On projects created (polling)**
- **On projects updated (polling)**
- **On projects finished (polling)**
- **On project status changed (polling)**

## Missing features

Expand Down