2021-06-11 19:30:11 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-06-27 13:51:12 +00:00
|
|
|
|
using System.Linq;
|
2021-06-09 21:03:07 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2021-07-04 17:34:10 +00:00
|
|
|
|
using Ardalis.GuardClauses;
|
2021-06-09 21:03:07 +00:00
|
|
|
|
using MongoDB.Driver;
|
2021-06-21 14:46:44 +00:00
|
|
|
|
using Retroactiune.Core.Entities;
|
|
|
|
|
using Retroactiune.Core.Interfaces;
|
2021-06-09 21:03:07 +00:00
|
|
|
|
|
2021-06-21 14:46:44 +00:00
|
|
|
|
namespace Retroactiune.Core.Services
|
2021-06-09 21:03:07 +00:00
|
|
|
|
{
|
2021-07-04 11:05:53 +00:00
|
|
|
|
public class TokensService : ITokensService
|
2021-06-09 21:03:07 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IMongoCollection<Token> _collection;
|
|
|
|
|
|
2021-07-04 11:05:53 +00:00
|
|
|
|
public TokensService(IMongoClient client, IDatabaseSettings settings)
|
2021-06-09 21:03:07 +00:00
|
|
|
|
{
|
|
|
|
|
var database = client.GetDatabase(settings.DatabaseName);
|
|
|
|
|
_collection = database.GetCollection<Token>(settings.TokensCollectionName);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 19:30:11 +00:00
|
|
|
|
public async Task GenerateTokensAsync(int numberOfTokens, string feedbackReceiverGuid,
|
|
|
|
|
DateTime? expiryTime = null)
|
2021-06-09 21:03:07 +00:00
|
|
|
|
{
|
2021-07-04 17:34:10 +00:00
|
|
|
|
Guard.Against.Negative(numberOfTokens, nameof(numberOfTokens));
|
2021-06-11 19:49:12 +00:00
|
|
|
|
|
2021-06-11 19:30:11 +00:00
|
|
|
|
var token = new List<Token>();
|
|
|
|
|
for (var i = 0; i < numberOfTokens; i++)
|
|
|
|
|
{
|
|
|
|
|
token.Add(new Token
|
|
|
|
|
{
|
|
|
|
|
CreatedAt = DateTime.UtcNow,
|
|
|
|
|
FeedbackReceiverId = feedbackReceiverGuid,
|
2021-06-15 20:08:15 +00:00
|
|
|
|
ExpiryTime = expiryTime,
|
2021-06-11 19:30:11 +00:00
|
|
|
|
TimeUsed = null
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _collection.InsertManyAsync(token);
|
2021-06-09 21:03:07 +00:00
|
|
|
|
}
|
2021-06-20 11:25:45 +00:00
|
|
|
|
|
2021-07-04 17:34:10 +00:00
|
|
|
|
public async Task DeleteTokensAsync(IEnumerable<string> tokenIds)
|
2021-06-20 11:25:45 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var filter = new FilterDefinitionBuilder<Token>();
|
|
|
|
|
await _collection.DeleteManyAsync(filter.In(i => i.Id, tokenIds));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
throw new GenericServiceException($"Operation failed: {e.Message} {e.StackTrace}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-27 12:14:34 +00:00
|
|
|
|
|
2021-07-04 17:34:10 +00:00
|
|
|
|
public async Task<IEnumerable<Token>> FindAsync(TokenListFilters filters)
|
2021-06-27 12:14:34 +00:00
|
|
|
|
{
|
2021-06-27 13:51:12 +00:00
|
|
|
|
var filterBuilder = new FilterDefinitionBuilder<Token>();
|
|
|
|
|
var activeFilters = new List<FilterDefinition<Token>>();
|
|
|
|
|
var tokensListFilter = FilterDefinition<Token>.Empty;
|
|
|
|
|
|
|
|
|
|
// Filter by token ids.
|
2021-06-30 20:07:39 +00:00
|
|
|
|
if (filters.Ids != null && filters.Ids.Any())
|
2021-06-27 13:51:12 +00:00
|
|
|
|
{
|
|
|
|
|
activeFilters.Add(filterBuilder.In(i => i.Id, filters.Ids));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filter tokens by their assigned feedback receiver id.
|
|
|
|
|
if (filters.FeedbackReceiverId != null)
|
|
|
|
|
{
|
|
|
|
|
activeFilters.Add(filterBuilder.Eq(i => i.FeedbackReceiverId, filters.FeedbackReceiverId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Datetime after
|
|
|
|
|
if (filters.CreatedAfter != null)
|
|
|
|
|
{
|
|
|
|
|
activeFilters.Add(filterBuilder.Gte(i => i.CreatedAt, filters.CreatedAfter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Datetime before
|
|
|
|
|
if (filters.CreatedBefore != null)
|
|
|
|
|
{
|
|
|
|
|
activeFilters.Add(filterBuilder.Lte(i => i.CreatedAt, filters.CreatedBefore));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Used time after
|
|
|
|
|
if (filters.UsedAfter != null)
|
|
|
|
|
{
|
|
|
|
|
activeFilters.Add(filterBuilder.Gte(i => i.TimeUsed, filters.UsedAfter));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Used time before
|
|
|
|
|
if (filters.UsedBefore != null)
|
|
|
|
|
{
|
|
|
|
|
activeFilters.Add(filterBuilder.Lte(i => i.TimeUsed, filters.UsedBefore));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Construct the final filter.
|
|
|
|
|
if (activeFilters.Any())
|
|
|
|
|
{
|
|
|
|
|
tokensListFilter = filterBuilder.And(activeFilters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var results = await _collection.FindAsync(tokensListFilter);
|
|
|
|
|
return await results.ToListAsync();
|
2021-06-27 12:14:34 +00:00
|
|
|
|
}
|
2021-07-04 11:05:53 +00:00
|
|
|
|
|
|
|
|
|
public async Task DeleteManyByFeedbackReceiverIdAsync(IEnumerable<string> feedbackReceiverIds)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var filter = new FilterDefinitionBuilder<Token>();
|
|
|
|
|
await _collection.DeleteManyAsync(filter.In(i => i.FeedbackReceiverId, feedbackReceiverIds));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
throw new GenericServiceException($"Operation failed: {e.Message} {e.StackTrace}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-04 17:34:10 +00:00
|
|
|
|
|
|
|
|
|
public async Task MarkTokenAsUsedAsync(Token token)
|
|
|
|
|
{
|
2021-07-11 13:26:01 +00:00
|
|
|
|
Guard.Against.Null(token, nameof(token));
|
2021-07-04 17:34:10 +00:00
|
|
|
|
var filterBuilder = new FilterDefinitionBuilder<Token>();
|
|
|
|
|
var updateBuilder = new UpdateDefinitionBuilder<Token>();
|
|
|
|
|
await _collection.UpdateOneAsync(filterBuilder.Eq(i => i.Id, token.Id),
|
|
|
|
|
updateBuilder.Set(i => i.TimeUsed, DateTime.UtcNow));
|
|
|
|
|
}
|
2021-06-09 21:03:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|