Skip to content

Commit

Permalink
Merge pull request #2 from bb-io/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
vitalii-bezuhlyi authored Jul 2, 2024
2 parents 88ae792 + bf6339c commit b77b1d4
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 193 deletions.
17 changes: 5 additions & 12 deletions Apps.XtrfCustomerPortal/Actions/InvoiceActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,12 @@ public async Task<GetInvoicesResponse> SearchInvoices([ActionParameter] SearchIn
endpoint = endpoint.AddQueryParameter("search", request.Search);
}

if (request.InvoiceDateFrom.HasValue)
{
var invoiceDateFromMilliseconds = new DateTimeOffset(request.InvoiceDateFrom.Value).ToUnixTimeMilliseconds();
endpoint = endpoint.AddQueryParameter("invoiceDateFrom", invoiceDateFromMilliseconds.ToString());
}

if (request.InvoiceDateTo.HasValue)
{
var invoiceDateToMilliseconds = new DateTimeOffset(request.InvoiceDateTo.Value).ToUnixTimeMilliseconds();
endpoint = endpoint.AddQueryParameter("invoiceDateTo", invoiceDateToMilliseconds.ToString());
}

var invoices = await FetchInvoicesWithPagination(endpoint);
invoices.Invoices = invoices.Invoices
.Where(i => request.InvoiceDateFrom == null || i.ExpectedFullyPaidDate >= request.InvoiceDateFrom)
.Where(i => request.InvoiceDateTo == null || i.ExpectedFullyPaidDate <= request.InvoiceDateTo)
.ToList();

return invoices;
}

Expand Down
82 changes: 33 additions & 49 deletions Apps.XtrfCustomerPortal/Actions/ProjectActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,17 @@ public async Task<GetProjectsResponse> SearchProjects([ActionParameter] SearchPr
{
endpoint = endpoint.AddQueryParameter("customerProjectNumber", searchProjectsRequest.CustomerProjectNumber);
}

if (searchProjectsRequest.CreatedOnFrom.HasValue)
{
var createdOnFromMilliseconds =
new DateTimeOffset(searchProjectsRequest.CreatedOnFrom.Value).ToUnixTimeMilliseconds();
endpoint = endpoint.AddQueryParameter("startDateFrom", createdOnFromMilliseconds.ToString());
}

if (searchProjectsRequest.CreatedOnTo.HasValue)
{
var createdOnToMilliseconds =
new DateTimeOffset(searchProjectsRequest.CreatedOnTo.Value).ToUnixTimeMilliseconds();
endpoint = endpoint.AddQueryParameter("startDateTo", createdOnToMilliseconds.ToString());
}

if (searchProjectsRequest.ExpirationFrom.HasValue)
{
var expirationFromMilliseconds =
new DateTimeOffset(searchProjectsRequest.ExpirationFrom.Value).ToUnixTimeMilliseconds();
endpoint = endpoint.AddQueryParameter("deadlineFrom", expirationFromMilliseconds.ToString());
}

if (searchProjectsRequest.ExpirationTo.HasValue)
{
var expirationToMilliseconds =
new DateTimeOffset(searchProjectsRequest.ExpirationTo.Value).ToUnixTimeMilliseconds();
endpoint = endpoint.AddQueryParameter("deadlineTo", expirationToMilliseconds.ToString());
}


var projects = await FetchProjectsWithPagination(endpoint);
return new GetProjectsResponse(projects);
var response = new GetProjectsResponse(projects);
response.Projects = response.Projects
.Where(x => searchProjectsRequest.CreatedOnFrom == null || x.StartDate >= searchProjectsRequest.CreatedOnFrom)
.Where(x => searchProjectsRequest.CreatedOnTo == null || x.StartDate <= searchProjectsRequest.CreatedOnTo)
.Where(x => searchProjectsRequest.ExpirationFrom == null || x.Deadline >= searchProjectsRequest.ExpirationFrom)
.Where(x => searchProjectsRequest.ExpirationTo == null || x.Deadline <= searchProjectsRequest.ExpirationTo)
.ToList();

return response;
}

[Action("Get project", Description = "Get project based on project ID")]
Expand All @@ -84,7 +64,7 @@ public async Task<ProjectResponse> GetProject([ActionParameter] ProjectIdentifie
await Client.ExecuteRequestAsync<ProjectDto>($"/projects/{projectIdentifier.ProjectId}", Method.Get, null);
return new ProjectResponse(projectDto);
}

[Action("Create project", Description = "Create a new project")]
public async Task<ProjectResponse> CreateProject([ActionParameter] CreateProjectRequest request)
{
Expand All @@ -98,24 +78,24 @@ public async Task<ProjectResponse> CreateProject([ActionParameter] CreateProject
specializationId = int.Parse(request.SpecializationId),
deliveryDate = new
{
time = request.DeliveryDate.HasValue
? new DateTimeOffset(request.DeliveryDate.Value).ToUnixTimeMilliseconds()
time = request.DeliveryDate.HasValue
? new DateTimeOffset(request.DeliveryDate.Value).ToUnixTimeMilliseconds()
: DateTime.Now.AddDays(7).ToUnixTimeMilliseconds()
},
notes = request.Note ?? string.Empty,
notes = string.Empty,
priceProfileId = int.Parse(request.PriceProfileId),
personId = int.Parse(request.PersonId),
sendBackToId = request.SendBackToId == null ? int.Parse(request.PersonId) : int.Parse(request.SendBackToId),
additionalPersonIds = request.AdditionalPersonIds == null
? new List<int>()
additionalPersonIds = request.AdditionalPersonIds == null
? new List<int>()
: request.AdditionalPersonIds.Select(int.Parse).ToList(),
files = await UploadFilesAsync(request.Files, fileManagementClient),
referenceFiles = request.ReferenceFiles == null
? new List<FileUploadDto>()
referenceFiles = request.ReferenceFiles == null
? new List<FileUploadDto>()
: await UploadFilesAsync(request.ReferenceFiles, fileManagementClient),
customFields = new List<string>(),
officeId = request.OfficeId != null
? int.Parse(request.OfficeId)
officeId = request.OfficeId != null
? int.Parse(request.OfficeId)
: (await GetDefaultOffice()).Id,
budgetCode = request.BudgetCode ?? string.Empty,
catToolType = request.CatToolType ?? "TRADOS"
Expand All @@ -124,32 +104,36 @@ public async Task<ProjectResponse> CreateProject([ActionParameter] CreateProject
var projectDto = await Client.ExecuteRequestAsync<ProjectDto>("/v2/projects", Method.Post, obj);
return new ProjectResponse(projectDto);
}

[Action("Download project files", Description = "Download project translation files")]
public async Task<DownloadProjectFilesResponse> DownloadProjectFiles([ActionParameter] ProjectIdentifier projectIdentifier)

[Action("Download project translated files", Description = "Download project translation files")]
public async Task<DownloadProjectFilesResponse> DownloadProjectFiles(
[ActionParameter] ProjectIdentifier projectIdentifier)
{
var taskFilesDto = await Client.ExecuteRequestAsync<TaskFilesDto>($"/projects/{projectIdentifier.ProjectId}/files", Method.Get, null);
var taskFilesDto =
await Client.ExecuteRequestAsync<TaskFilesDto>($"/projects/{projectIdentifier.ProjectId}/files", Method.Get,
null);
var files = taskFilesDto.TasksFiles.SelectMany(x => x.Output?.Files ?? new List<TaskFilesDto.File>()).ToList();

var fileReferences = new List<FileReference>();
foreach (var file in files)
{
var invoicePdf = await Client.ExecuteRequestAsync($"/projects/files/{file.Id}", Method.Get, null, "application/octet-stream");
var invoicePdf = await Client.ExecuteRequestAsync($"/projects/files/{file.Id}", Method.Get, null,
"application/octet-stream");
var rawBytes = invoicePdf.RawBytes!;

var stream = new MemoryStream(rawBytes);
stream.Position = 0;

var fileReference = await fileManagementClient.UploadAsync(stream, "application/octet-stream", file.Name);
fileReferences.Add(fileReference);
}

return new DownloadProjectFilesResponse
{
Files = fileReferences
};
}

private async Task<List<ProjectDto>> FetchProjectsWithPagination(string endpoint)
{
var allProjects = new List<ProjectDto>();
Expand Down
32 changes: 8 additions & 24 deletions Apps.XtrfCustomerPortal/Actions/QuoteActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,15 @@ public async Task<GetQuotesResponse> SearchQuotes([ActionParameter] SearchQuotes
endpoint = endpoint.AddQueryParameter("search", searchQuotesRequest.Search);
}

if (searchQuotesRequest.CreatedOnFrom.HasValue)
{
var createdOnFromMilliseconds = new DateTimeOffset(searchQuotesRequest.CreatedOnFrom.Value).ToUnixTimeMilliseconds();
endpoint = endpoint.AddQueryParameter("createdOnFrom", createdOnFromMilliseconds.ToString());
}

if (searchQuotesRequest.CreatedOnTo.HasValue)
{
var createdOnToMilliseconds = new DateTimeOffset(searchQuotesRequest.CreatedOnTo.Value).ToUnixTimeMilliseconds();
endpoint = endpoint.AddQueryParameter("createdOnTo", createdOnToMilliseconds.ToString());
}

if (searchQuotesRequest.ExpirationFrom.HasValue)
{
var expirationFromMilliseconds = new DateTimeOffset(searchQuotesRequest.ExpirationFrom.Value).ToUnixTimeMilliseconds();
endpoint = endpoint.AddQueryParameter("expirationDateFrom", expirationFromMilliseconds.ToString());
}
var quotes = await FetchQuotesWithPagination(endpoint);

if (searchQuotesRequest.ExpirationTo.HasValue)
{
var expirationToMilliseconds = new DateTimeOffset(searchQuotesRequest.ExpirationTo.Value).ToUnixTimeMilliseconds();
endpoint = endpoint.AddQueryParameter("expirationDateTo", expirationToMilliseconds.ToString());
}
quotes.Quotes = quotes.Quotes
.Where(x => searchQuotesRequest.CreatedOnFrom == null || x.StartDate >= searchQuotesRequest.CreatedOnFrom)
.Where(x => searchQuotesRequest.CreatedOnTo == null || x.StartDate <= searchQuotesRequest.CreatedOnTo)
.Where(x => searchQuotesRequest.ExpirationFrom == null || x.Deadline >= searchQuotesRequest.ExpirationFrom)
.Where(x => searchQuotesRequest.ExpirationTo == null || x.Deadline <= searchQuotesRequest.ExpirationTo)
.ToList();

var quotes = await FetchQuotesWithPagination(endpoint);
return quotes;
}

Expand Down Expand Up @@ -84,7 +68,7 @@ public async Task<QuoteResponse> CreateQuote([ActionParameter] QuoteCreateReques
? new DateTimeOffset(request.DeliveryDate.Value).ToUnixTimeMilliseconds()
: DateTime.Now.AddDays(7).ToUnixTimeMilliseconds()
},
notes = request.Note ?? string.Empty,
notes = string.Empty,
priceProfileId = int.Parse(request.PriceProfileId),
personId = int.Parse(request.PersonId),
sendBackToId = request.SendBackToId == null ? int.Parse(request.PersonId) : int.Parse(request.SendBackToId),
Expand Down
28 changes: 25 additions & 3 deletions Apps.XtrfCustomerPortal/Api/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public async Task<T> ExecuteRequestAsync<T>(string endpoint, Method method, obje
request.WithJsonBody(bodyObj);
}

var response = await ExecuteWithErrorHandling<T>(request);
return response;
return await ExecuteWithErrorHandling<T>(request);
}

public async Task<RestResponse> ExecuteRequestAsync(string endpoint, Method method, object? bodyObj, string? acceptHeader = null)
Expand Down Expand Up @@ -95,6 +94,29 @@ private async Task<LoginDto> GetTokenAsync()

protected override Exception ConfigureErrorException(RestResponse response)
{
return new Exception($"Error message: {response.Content}; StatusCode: {response.StatusCode}");
try
{
var xmlSerializer = new XmlSerializer(typeof(XmlErrorDto));
using var xmlReader = new StringReader(response.Content!);

var xmlErrorDto = (XmlErrorDto)xmlSerializer.Deserialize(xmlReader)!;
return new Exception($"Error message: {xmlErrorDto.Body}; Status code: {response.StatusCode}");
}
catch (InvalidOperationException)
{
try
{
var jsonErrorDto = JsonConvert.DeserializeObject<JsonErrorDto>(response.Content!)!;
return new Exception($"Error message: {jsonErrorDto.ErrorMessage}; Status code: {response.StatusCode}");
}
catch (JsonException)
{
return new Exception($"Error message: {response.Content}; Status code: {response.StatusCode}");
}
}
catch (Exception ex)
{
return new Exception($"Unexpected error during error deserialization: {ex.Message}; Error body: {response.Content!} ; Status code: {response.StatusCode}");
}
}
}
2 changes: 1 addition & 1 deletion Apps.XtrfCustomerPortal/Apps.XtrfCustomerPortal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<Product>XTRF Customer portal</Product>
<Description>The XTRF Customer Portal is a private website restricted to authorized use for business purposes. It allows clients to access and manage translation project details. Within the portal, clients can view and edit information such as contact details, account manager, pricing, language combinations, and CAT tools used.</Description>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<PackageId>Apps.XtrfCustomerPortal</PackageId>
<AssemblyName>Apps.XtrfCustomerPortal</AssemblyName>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public Dictionary<string, string> GetData()
{ "OPENED", "Opened" },
{ "CLOSED", "Closed" },
{ "CLAIM", "Claim" },
{ "CANCELLED", "Cancelled" }
{ "CANCELLED", "Cancelled" },
{ "REQUESTED_PROJECT", "Requested"}
};
}
}
18 changes: 18 additions & 0 deletions Apps.XtrfCustomerPortal/DataSources/Static/ViewDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Blackbird.Applications.Sdk.Common.Dictionaries;

namespace Apps.XtrfCustomerPortal.DataSources.Static;

public class ViewDataSource : IStaticDataSourceHandler
{
public Dictionary<string, string> GetData()
{
return new()
{
{"UNPAID", "Unpaid"},
{"OVERDUE", "Overdue"},
{"EARLY_UNPAID", "Early unpaid"},
{"PAID", "Paid"},
{"ALL", "All"}
};
}
}
15 changes: 15 additions & 0 deletions Apps.XtrfCustomerPortal/Models/Dtos/JsonErrorDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Newtonsoft.Json;

namespace Apps.XtrfCustomerPortal.Models.Dtos;

public class JsonErrorDto
{
[JsonProperty("status")]
public string Status { get; set; }

[JsonProperty("errorMessage")]
public string ErrorMessage { get; set; }

[JsonProperty("detailedMessage")]
public string DetailedMessage { get; set; }
}
8 changes: 5 additions & 3 deletions Apps.XtrfCustomerPortal/Models/Dtos/TaskFilesDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ namespace Apps.XtrfCustomerPortal.Models.Dtos;
public class TaskFilesDto
{
public int Id { get; set; }

public string IdNumber { get; set; }
public List<TaskFile> TasksFiles { get; set; }

public bool OutputFilesAsZipDownloadable { get; set; }

public class TaskFile
{
public int Id { get; set; }
public string Id { get; set; }
public string IdNumber { get; set; }
public LanguageCombination LanguageCombination { get; set; }
public FileGroup InputWorkfiles { get; set; }
Expand All @@ -26,7 +28,7 @@ public class LanguageCombination

public class Language
{
public int Id { get; set; }
public string Id { get; set; }
public string Symbol { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
Expand All @@ -49,7 +51,7 @@ public class Directory
public class File
{
public string Name { get; set; }
public int Id { get; set; }
public string Id { get; set; }
public bool Downloadable { get; set; }
public string Category { get; set; }
public bool Zip { get; set; }
Expand Down
19 changes: 19 additions & 0 deletions Apps.XtrfCustomerPortal/Models/Dtos/XmlErrorDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Xml.Serialization;

namespace Apps.XtrfCustomerPortal.Models.Dtos;

[XmlRoot("html")]
public class XmlErrorDto
{
[XmlElement("head")]
public Head Head { get; set; }

[XmlElement("body")]
public string Body { get; set; }
}

public class Head
{
[XmlElement("title")]
public string Title { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ public class CreateProjectRequest
[Display("Delivery date", Description = "By default, the delivery date is set to the current date + 7 days.")]
public DateTime? DeliveryDate { get; set; }

[Display("Note")]
public string? Note { get; set; }

[Display("Price profile ID"), DataSource(typeof(PriceProfileDataSource))]
public string PriceProfileId { get; set; }

[Display("Person ID"), DataSource(typeof(PersonDataSource))]
[Display("Contact person ID"), DataSource(typeof(PersonDataSource))]
public string PersonId { get; set; }

[Display("Send back to ID"), DataSource(typeof(PersonDataSource))]
Expand All @@ -47,7 +44,7 @@ public class CreateProjectRequest
[Display("Additional person IDs"), DataSource(typeof(PersonDataSource))]
public IEnumerable<string>? AdditionalPersonIds { get; set; }

[Display("Additional email addresses")]
[Display("Reference files")]
public IEnumerable<FileReference>? ReferenceFiles { get; set; }

[Display("Office ID"), DataSource(typeof(OfficeDataSource))]
Expand All @@ -56,6 +53,6 @@ public class CreateProjectRequest
[Display("Budget code")]
public string? BudgetCode { get; set; }

[Display("Cat tool type", Description = "By default, the value is set to 'Trados'."), StaticDataSource(typeof(CatToolTypeDataSource))]
[Display("CAT tool type", Description = "By default, the value is set to 'Trados'."), StaticDataSource(typeof(CatToolTypeDataSource))]
public string? CatToolType { get; set; }
}
Loading

0 comments on commit b77b1d4

Please sign in to comment.