Skip to content

Commit

Permalink
Removed files from action
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalii-bezuhlyi committed Jun 27, 2024
1 parent 2e1cb02 commit 1084ba0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
16 changes: 11 additions & 5 deletions Apps.XTRF/Classic/Actions/ClassicQuoteActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Blackbird.Applications.Sdk.Common.Invocation;
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces;
using Blackbird.Applications.Sdk.Utils.Extensions.Http;
using Newtonsoft.Json;
using RestSharp;

namespace Apps.XTRF.Classic.Actions;
Expand Down Expand Up @@ -57,7 +58,8 @@ public async Task<CustomerQuoteResponse> CreateQuote([ActionParameter] QuoteCrea
var customerActions = new CustomerActions(invocationContext);
var contactPerson = await customerActions.GetContactPerson(request);
var tokenResponse = await GetPersonAccessToken(contactPerson?.Contact?.Emails?.Primary ?? throw new Exception("Contact person has no primary email."));
var customerPortalClient = GetCustomerPortalClient(tokenResponse.Token);
var customerPortalClient = GetCustomerPortalClient(tokenResponse.Token);
var officeId = (await GetDefaultOffice(contactPerson.Contact.Emails.Primary)).Id;

var obj = new
{
Expand All @@ -80,19 +82,21 @@ public async Task<CustomerQuoteResponse> CreateQuote([ActionParameter] QuoteCrea
additionalPersonIds = request.AdditionalPersonIds == null
? new List<int>()
: request.AdditionalPersonIds.Select(int.Parse).ToList(),
files = await customerPortalClient.UploadFilesAsync(request.Files, fileManagementClient),
files = request.Files == null
? new List<FileUpload>()
: await customerPortalClient.UploadFilesAsync(request.Files, fileManagementClient),
referenceFiles = request.ReferenceFiles == null
? new List<FileUpload>()
: await customerPortalClient.UploadFilesAsync(request.ReferenceFiles, fileManagementClient),
customFields = new List<string>(),
officeId = request.OfficeId != null
? int.Parse(request.OfficeId)
: (await GetDefaultOffice(request, customerPortalClient)).Id,
: officeId,
budgetCode = request.BudgetCode ?? string.Empty,
catToolType = request.CatToolType ?? "TRADOS"
};

var quoteDto = await customerPortalClient.ExecuteRequestAsync<Apps.XTRF.Classic.Models.Entities.Quote>("/v2/quotes", Method.Post, obj);
var quoteDto = await customerPortalClient.ExecuteRequestAsync<Quote>("/v2/quotes", Method.Post, obj);
return new(quoteDto);
}

Expand Down Expand Up @@ -151,8 +155,10 @@ public async Task DeleteQuote([ActionParameter] QuoteIdentifier quoteIdentifier)

#region Utils

public async Task<FullOfficeDto> GetDefaultOffice(PersonIdentifier personIdentifier, XtrfCustomerPortalClient customerPortalClient)
public async Task<FullOfficeDto> GetDefaultOffice(string emailOrLogin)
{
var accessToken = await GetPersonAccessToken(emailOrLogin);
var customerPortalClient = GetCustomerPortalClient(accessToken.Token);
var officeDto = await customerPortalClient.ExecuteRequestAsync<FullOfficeDto>($"/offices/default", Method.Get, null);
return officeDto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ClassicPriceProfileDataSource(InvocationContext invocationContext,
FullOfficeDto officeDto;
if (string.IsNullOrEmpty(request.OfficeId))
{
officeDto = await quoteActions.GetDefaultOffice(request, customerPortalClient);
officeDto = await quoteActions.GetDefaultOffice(contactPerson.Contact.Emails.Primary);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class QuoteCreateRequest : PersonIdentifier
[Display("Customer project number")]
public string? CustomerProjectNumber { get; set; }

public IEnumerable<FileReference> Files { get; set; }
public IEnumerable<FileReference>? Files { get; set; }

[Display("Service ID"), DataSource(typeof(ClassicServiceDataSourceHandler))]
public string ServiceId { get; set; }
Expand Down
6 changes: 1 addition & 5 deletions Apps.XTRF/Shared/Api/XtrfCustomerPortalClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ namespace Apps.XTRF.Shared.Api;
public class XtrfCustomerPortalClient(List<AuthenticationCredentialsProvider> credentials, string token) : BlackBirdRestClient(new RestClientOptions(UrlHelper.BuildCustomerUrl(credentials.Get(CredsNames.Url).Value)))
{
private const string TokenKey = "XTRF-CP-Auth-Token";
private const string JsessionCookie = "JSESSIONID";


public async Task<T> ExecuteRequestAsync<T>(string endpoint, Method method, object? bodyObj)
{
var request = new RestRequest(endpoint, method)
.AddHeader(TokenKey, token);

request.Resource = UrlHelper.BuildCustomerRequestUrl(Options.BaseUrl!.ToString(), endpoint, token);
if (bodyObj is not null)
{
request.WithJsonBody(bodyObj);
Expand All @@ -38,8 +36,6 @@ public async Task<T> UploadFileAsync<T>(string endpoint, byte[] fileBytes, strin
.AddHeader(TokenKey, token)
.AddFile("file", fileBytes, fileName, "multipart/form-data");

request.Resource = UrlHelper.BuildCustomerRequestUrl(Options.BaseUrl!.ToString(), endpoint, token);

var response = await ExecuteWithErrorHandling<T>(request);
return response;
}
Expand Down
6 changes: 4 additions & 2 deletions Apps.XTRF/Shared/Invocables/XtrfInvocable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Blackbird.Applications.Sdk.Common.Authentication;
using Blackbird.Applications.Sdk.Common.Invocation;
using Blackbird.Applications.Sdk.Utils.Extensions.Http;
using Newtonsoft.Json;
using RestSharp;

namespace Apps.XTRF.Shared.Invocables;
Expand Down Expand Up @@ -71,7 +72,8 @@ protected async Task<PersonAccessToken> GetPersonAccessToken(string loginOrEmail
{
var request = new XtrfRequest("/customers/persons/accessToken", Method.Post, Creds)
.WithJsonBody(new { loginOrEmail });

return await Client.ExecuteWithErrorHandling<PersonAccessToken>(request);

var response = await Client.ExecuteWithErrorHandling(request);
return JsonConvert.DeserializeObject<PersonAccessToken>(response.Content!)!;
}
}

0 comments on commit 1084ba0

Please sign in to comment.