Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/Application/Common/Services/ConversionCreditService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Application.Common.Exceptions;
using Domain.Entities;
using Microsoft.Extensions.Options;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Application.Common.Services
{
internal class ConversionCreditService : IConversionCreditService
{
private readonly GraphServiceClient _graphServiceClient;
private readonly AzureB2CSettings _azureB2CSettings;
private readonly string _conversionCreditsAttributeName;

public ConversionCreditService(GraphServiceClient graphServiceClient, IOptions<AzureB2CSettings> azureB2CSettings)
{
_graphServiceClient = graphServiceClient;
_azureB2CSettings = azureB2CSettings.Value;
_conversionCreditsAttributeName = AdditionalParameterRetriever.GetConversionCreditsAttributeName(_azureB2CSettings.B2cExtensionAppClientId);
}

public async Task<int> EditConversionCredits(string userId, int quantity)
{
User? user = await GetUser(userId);

if (user == null)
{
throw new NotFoundException($"The user {userId} was not found.", userId);
}

decimal credits = 0;
if (user.AdditionalData.ContainsKey(_conversionCreditsAttributeName))
{
credits = (decimal)user.AdditionalData[_conversionCreditsAttributeName];
}

credits = credits + quantity;

user.AdditionalData[_conversionCreditsAttributeName] = credits;

var requestBody = new User
{
AdditionalData = new Dictionary<string, object>
{
{
_conversionCreditsAttributeName , credits.ToString()
}
},
};

User? updatedUser = await _graphServiceClient.Users[user.Id].PatchAsync(requestBody);

if (updatedUser == null)
{
updatedUser = await GetUser(userId);
}

return decimal.ToInt16((decimal)updatedUser.AdditionalData[_conversionCreditsAttributeName]);
}

public async Task<int> GetConversionCredits(string userId)
{
User? user = await GetUser(userId);

if (user == null)
{
throw new NotFoundException($"The user {userId} was not found.", userId);
}

decimal credits = 0;
if (user.AdditionalData.ContainsKey(_conversionCreditsAttributeName))
{
credits = (decimal)user.AdditionalData[_conversionCreditsAttributeName];
}

return decimal.ToInt16(credits);
}

public async Task<User?> GetUser(string userId)
{
string selects = $"displayName,id,mail,mobilePhone,{_conversionCreditsAttributeName}";

User? user = await _graphServiceClient.Users[userId].GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Select = new string[] { "Id", "displayName", _conversionCreditsAttributeName };
});

return user;
}
}
}
15 changes: 15 additions & 0 deletions src/Application/Common/Services/IConversionCreditService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.Common.Services
{
public interface IConversionCreditService
{
Task<Microsoft.Graph.Models.User?> GetUser(string userId);
Task<int> GetConversionCredits(string userId);
Task<int> EditConversionCredits(string userId, int quantity);
}
}
3 changes: 3 additions & 0 deletions src/Application/Common/Services/ISavedWorkItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ namespace Application.Common.Services
{
public interface ISavedWorkItemService
{
Task<WorkItemStatus> GetSavedWorkItemStatus(string workItemId);
Task CreateSavedWorkItemStatus(WorkItemStatus workItemStatus, string userId, string revitVersion, string objectKey, string fileName);
List<SavedWorkItem> GetSavedWorkItems(string userId);
Task UpdateSavedWorkItemStatus(WorkItemStatus workItemStatus);
Task<SavedWorkItem> GetSavedWorkItem(string workItemId);
Task MarkSavedWorkItemAsCredited(string workItemId);
}
}
53 changes: 50 additions & 3 deletions src/Application/Common/Services/SavedWorkItemService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Autodesk.Forge.DesignAutomation.Model;
using Application.WorkItems.Commands.UpdateWorkItemStatus;
using Autodesk.Forge.DesignAutomation.Model;
using Azure;
using Azure.Data.Tables;
using Domain.Entities;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -55,7 +57,7 @@ public async Task UpdateSavedWorkItemStatus(WorkItemStatus workItemStatus)
savedWorkItem.UpdateStatus(workItemStatus);
savedWorkItem.LastModified = DateTime.UtcNow;

Azure.Response response = await tableClient.UpdateEntityAsync(savedWorkItem, Azure.ETag.All );
Azure.Response response = await tableClient.UpdateEntityAsync(savedWorkItem, Azure.ETag.All);

if (response.IsError)
{
Expand All @@ -68,7 +70,7 @@ public List<SavedWorkItem> GetSavedWorkItems(string userId)
TableClient tableClient = _tableServiceClient.GetTableClient(_partitionKey);
tableClient.CreateIfNotExists();

Pageable<SavedWorkItem> queryResults = tableClient.Query<SavedWorkItem>(ent =>
Pageable<SavedWorkItem> queryResults = tableClient.Query<SavedWorkItem>(ent =>
ent.UserId == userId && ent.Created >= DateTime.Now.AddDays(-30));

List<SavedWorkItem> savedWorkItems = new List<SavedWorkItem>();
Expand All @@ -80,5 +82,50 @@ public List<SavedWorkItem> GetSavedWorkItems(string userId)

return savedWorkItems;
}

public async Task<SavedWorkItem> GetSavedWorkItem(string workItemId)
{
TableClient tableClient = _tableServiceClient.GetTableClient(_partitionKey);
tableClient.CreateIfNotExists();

SavedWorkItem savedWorkItem = await tableClient.GetEntityAsync<SavedWorkItem>(_partitionKey, workItemId);

return savedWorkItem;
}

public async Task MarkSavedWorkItemAsCredited(string workItemId)
{
TableClient tableClient = _tableServiceClient.GetTableClient(_partitionKey);
tableClient.CreateIfNotExists();

SavedWorkItem savedWorkItem = await tableClient.GetEntityAsync<SavedWorkItem>(_partitionKey, workItemId);
savedWorkItem.Credited = true;

Azure.Response response = await tableClient.UpdateEntityAsync(savedWorkItem, Azure.ETag.All);

if (response.IsError)
{
throw new Exception(response.ReasonPhrase);
}
}

public async Task<WorkItemStatus> GetSavedWorkItemStatus(string workItemId)
{
TableClient tableClient = _tableServiceClient.GetTableClient(_partitionKey);
tableClient.CreateIfNotExists();

SavedWorkItem savedWorkItem = await tableClient.GetEntityAsync<SavedWorkItem>(_partitionKey, workItemId);

string statusName = System.Text.Json.JsonNamingPolicy.CamelCase.ConvertName(savedWorkItem.Status);
WorkItemStatus status = new WorkItemStatus()
{
Status = WorkItemStatusDTO.ToEnum<Status>(statusName),
Progress = savedWorkItem.Progress,
ReportUrl = savedWorkItem.ReportUrl,
Id = savedWorkItem.WorkItemId,
};

return status;
}
}
}
1 change: 1 addition & 0 deletions src/Application/ConfigureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection
services.AddOss(configuration);
services.AddTransient<OssClient>();
services.AddSingleton<ISavedWorkItemService, SavedWorkItemService>();
services.AddSingleton<IConversionCreditService, ConversionCreditService>();

services.Configure<ForgeConfiguration>(configuration.GetSection("Forge"));
services.Configure<StripeSettings>(configuration.GetSection("Stripe"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.Users.Commands.EditConversionCredits
{
public class EditConversionCreditsCommand : IRequest<int>
{
public EditConversionCreditsCommand(string userId, int quantity)
{
UserId = userId;
Quantity = quantity;
}

public readonly string UserId;
public readonly int Quantity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Application.ForgeApplications.Commands.CreateForgeApplication;
using Autodesk.Forge.DesignAutomation.Model;
using Autodesk.Forge.DesignAutomation;
using MediatR;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Entities;
using Application.Common.Services;

namespace Application.Users.Commands.EditConversionCredits
{
public class EditConversionCreditsCommandHandler : IRequestHandler<EditConversionCreditsCommand, int>
{
private readonly IConversionCreditService _conversionCreditService;

public EditConversionCreditsCommandHandler(IConversionCreditService conversionCreditService)
{
_conversionCreditService = conversionCreditService;
}

public async Task<int> Handle(EditConversionCreditsCommand request, CancellationToken cancellationToken)
{
int updated = await _conversionCreditService.EditConversionCredits(request.UserId, request.Quantity);

return updated;
}
}

}
36 changes: 8 additions & 28 deletions src/Application/Users/Queries/GetUser/GetUserQueryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,26 @@ namespace Application.Users.Queries.GetUser
{
public class GetUserQueryHandler : IRequestHandler<GetUserQuery, UserDTO>
{
private readonly GraphServiceClient _graphServiceClient;
private readonly AzureB2CSettings _azureB2CSettings;
private readonly IConversionCreditService _conversionCreditService;

public GetUserQueryHandler(GraphServiceClient graphServiceClient, IOptions<AzureB2CSettings> azureB2CSettings)
public GetUserQueryHandler(IConversionCreditService conversionCreditService)
{
_graphServiceClient = graphServiceClient;
_azureB2CSettings = azureB2CSettings.Value;
_conversionCreditService = conversionCreditService;
}

public async Task<UserDTO> Handle(GetUserQuery request, CancellationToken cancellationToken)
{
string conversionCreditsAttributeName = AdditionalParameterRetriever.GetConversionCreditsAttributeName(_azureB2CSettings.B2cExtensionAppClientId);

string selects = $"displayName,id,mail,mobilePhone,{conversionCreditsAttributeName}";

Microsoft.Graph.Models.User? user = await _graphServiceClient.Users[request.UserId].GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Select = new string[] {"Id", "displayName",conversionCreditsAttributeName };
});

if (user == null)
{
throw new NotFoundException($"The user {request.UserId} was not found.", request.UserId);
}

decimal credis = 0;
if (user.AdditionalData.ContainsKey(conversionCreditsAttributeName))
{
credis = (decimal)user.AdditionalData[conversionCreditsAttributeName];
}
Microsoft.Graph.Models.User? user = await _conversionCreditService.GetUser(request.UserId);
int credits = await _conversionCreditService.GetConversionCredits(request.UserId);

UserDTO userDTO = new UserDTO()
{
Id = user.Id,
Name = user.DisplayName,
ConversionCredits = decimal.ToInt16(credis)
};
ConversionCredits = decimal.ToInt16(credits)
};

return userDTO;
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Graph.Models;
using System;
using System.Runtime.Serialization;
using System.Text.Json;

Expand All @@ -21,24 +22,38 @@ public class CreateWorkItemCommandHandler : IRequestHandler<CreateWorkItemComman
{
private readonly DesignAutomationClient _designAutomationClient;
private readonly ISavedWorkItemService _savedWorkItemService;
private readonly IConversionCreditService _conversionCreditService;
private readonly OssClient _ossClient;
private readonly ILogger<GetUploadUrlQueryHandler> _logger;
private readonly AuthenticationClient _authenticationClient;
private readonly ForgeConfiguration _forgeConfiguration;

public CreateWorkItemCommandHandler(DesignAutomationClient designAutomationClient,ILogger<GetUploadUrlQueryHandler> logger, AuthenticationClient authenticationClient,
IOptions<ForgeConfiguration> forgeConfiguration, OssClient ossClient, ISavedWorkItemService savedWorkItemService)
IOptions<ForgeConfiguration> forgeConfiguration, OssClient ossClient,
ISavedWorkItemService savedWorkItemService, IConversionCreditService conversionCreditService)
{
_ossClient = ossClient;
_logger = logger;
_authenticationClient = authenticationClient;
_forgeConfiguration = forgeConfiguration.Value;
_designAutomationClient = designAutomationClient;
_savedWorkItemService = savedWorkItemService;
_conversionCreditService = conversionCreditService;
}

public async Task<WorkItemStatus> Handle(CreateWorkItemCommand request, CancellationToken cancellationToken)
{
int credits = await _conversionCreditService.GetConversionCredits(request.UserId);

if (credits == 0)
{
return new WorkItemStatus()
{
Status = Autodesk.Forge.DesignAutomation.Model.Status.FailedInstructions,
Progress = "You don't have enought credits to convert this file."
};
}

TwoLeggedToken twoLeggedToken = await _authenticationClient.GetTwoLeggedTokenAsync(_forgeConfiguration.ClientId, _forgeConfiguration.ClientSecret, new List<Scopes> { Scopes.DataWrite, Scopes.DataRead });

string inputBucketKey = _forgeConfiguration.InputBucketKey;
Expand Down
Loading
Loading