Skip to content

Commit

Permalink
DLF154 - Ajout d'une routine qui génère une liste de producers
Browse files Browse the repository at this point in the history
  • Loading branch information
noelmugnier committed Jan 25, 2021
1 parent 1f234c0 commit 2a1835c
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;
using Sheaft.Core;

namespace Sheaft.Application.Commands
{
public class GenerateProducersFileCommand : Command<bool>
{
[JsonConstructor]
public GenerateProducersFileCommand(RequestUser requestUser) : base(requestUser)
{
}
}
}
58 changes: 58 additions & 0 deletions Sheaft.Application.Handlers/Commands/ProducerCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,51 @@
using Sheaft.Exceptions;
using Microsoft.EntityFrameworkCore;
using Sheaft.Application.Events;
using Newtonsoft.Json;
using System.Text;
using Newtonsoft.Json.Serialization;

namespace Sheaft.Application.Handlers
{
public class ProducerCommandsHandler : ResultsHandler,
IRequestHandler<GenerateProducersFileCommand, Result<bool>>,
IRequestHandler<RegisterProducerCommand, Result<Guid>>,
IRequestHandler<UpdateProducerCommand, Result<bool>>,
IRequestHandler<CheckProducerConfigurationCommand, Result<bool>>,
IRequestHandler<UpdateProducerTagsCommand, Result<bool>>,
IRequestHandler<SetProducerProductsWithNoVatCommand, Result<bool>>
{
private readonly RoleOptions _roleOptions;
private readonly IBlobService _blobService;

public ProducerCommandsHandler(
IAppDbContext context,
ISheaftMediatr mediatr,
IBlobService blobService,
ILogger<ProducerCommandsHandler> logger,
IOptionsSnapshot<RoleOptions> roleOptions)
: base(mediatr, context, logger)
{
_roleOptions = roleOptions.Value;
_blobService = blobService;
}

public async Task<Result<bool>> Handle(GenerateProducersFileCommand request, CancellationToken token)
{
return await ExecuteAsync(request, async () =>
{
var producers = await _context.GetAsync<Producer>(token);
var prods = producers.Select(p => new ProducerListItem(p));
var result = await _blobService.UploadProducersListAsync(
Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(prods,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() }})), token);
if(!result.Success)
return Failed<bool>(result.Exception);
return Ok(true);
});
}

public async Task<Result<Guid>> Handle(RegisterProducerCommand request, CancellationToken token)
Expand Down Expand Up @@ -233,6 +258,39 @@ public async Task<Result<bool>> Handle(SetProducerProductsWithNoVatCommand reque
return Ok(true);
});
}

internal class ProducerListItem
{
internal ProducerListItem(User user)
{
Address = new AddressItem(user.Address);
Id = user.Id.ToString("N");
Name = user.Name;
Picture = user.Picture;
}
public string Id { get; set; }
public string Name { get; set; }
public string Picture { get; set; }
public AddressItem Address { get; set; }
}

internal class AddressItem {
internal AddressItem(UserAddress address)
{
Line1 = address.Line1;
Line2 = address.Line2;
Zipcode = address.Zipcode;
City = address.City;
Latitude = address.Latitude;
Longitude = address.Longitude;
}
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
}
}
}
3 changes: 3 additions & 0 deletions Sheaft.Application.Interop/Persistence/IAppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ public interface IAppDbContext : IDisposable, IAsyncDisposable, IInfrastructure<
Task<IEnumerable<T>> GetByIdsAsync<T>(IEnumerable<Guid> ids, CancellationToken token, bool asNoTracking = false) where T : class, IIdEntity, ITrackRemove;
Task<IEnumerable<T>> FindByIdsAsync<T>(IEnumerable<Guid> ids, CancellationToken token, bool asNoTracking = false) where T : class, IIdEntity, ITrackRemove;
Task<IEnumerable<T>> GetAsync<T>(Expression<Func<T, bool>> where, CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove;
Task<IEnumerable<T>> GetAsync<T>(CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove;
Task<IEnumerable<T>> FindAsync<T>(Expression<Func<T, bool>> where, CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove;
Task<IEnumerable<T>> FindAsync<T>(CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove;
Task<bool> AnyAsync<T>(Expression<Func<T, bool>> where, CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove;
Task<bool> AnyAsync<T>(CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove;
Task EnsureNotExists<T>(Guid id, CancellationToken token, bool asNoTracking = false) where T : class, IIdEntity, ITrackRemove;
Task EnsureNotExists<T>(Expression<Func<T, bool>> where, CancellationToken token, bool asNoTracking = false) where T : class, IIdEntity, ITrackRemove;

Expand Down
1 change: 1 addition & 0 deletions Sheaft.Application.Interop/Services/IBlobsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public interface IBlobService
Task<Result<byte[]>> DownloadDocumentPageAsync(Guid documentId, Guid pageId, Guid userId, CancellationToken token);
Task<Result<bool>> UploadDocumentPageAsync(Guid documentId, Guid pageId, byte[] data, Guid userId, CancellationToken token);
Task<Result<bool>> DeleteDocumentPageAsync(Guid documentId, Guid pageId, Guid userId, CancellationToken token);
Task<Result<string>> UploadProducersListAsync(byte[] data, CancellationToken token);
}
}
45 changes: 39 additions & 6 deletions Sheaft.Infrastructure.Persistence/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ public async Task<IEnumerable<T>> FindByIdsAsync<T>(IEnumerable<Guid> ids, Cance

public async Task<IEnumerable<T>> GetAsync<T>(Expression<Func<T, bool>> where, CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove
{
var query = Set<T>().Where(c => !c.RemovedOn.HasValue).Where(where);
var query = Set<T>().Where(c => !c.RemovedOn.HasValue);
if(where != null)
query = query.Where(where);

if (asNoTracking)
query = query.AsNoTracking();

Expand All @@ -146,9 +149,17 @@ public async Task<IEnumerable<T>> GetAsync<T>(Expression<Func<T, bool>> where, C
return items;
}

public async Task<IEnumerable<T>> GetAsync<T>(CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove
{
return await GetAsync<T>(null, token, asNoTracking);
}

public async Task<IEnumerable<T>> FindAsync<T>(Expression<Func<T, bool>> where, CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove
{
var query = Set<T>().Where(c => !c.RemovedOn.HasValue).Where(where);
var query = Set<T>().Where(c => !c.RemovedOn.HasValue);
if(where != null)
query = query.Where(where);

if (asNoTracking)
query = query.AsNoTracking();

Expand All @@ -159,9 +170,17 @@ public async Task<IEnumerable<T>> FindAsync<T>(Expression<Func<T, bool>> where,
return items;
}

public async Task<IEnumerable<T>> FindAsync<T>(CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove
{
return await FindAsync<T>(null, token, asNoTracking);
}

public async Task<T> GetSingleAsync<T>(Expression<Func<T, bool>> where, CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove
{
var query = Set<T>().Where(c => !c.RemovedOn.HasValue).Where(where);
var query = Set<T>().Where(c => !c.RemovedOn.HasValue);
if(where != null)
query = query.Where(where);

if (asNoTracking)
query = query.AsNoTracking();

Expand All @@ -177,7 +196,10 @@ public async Task<T> GetSingleAsync<T>(Expression<Func<T, bool>> where, Cancella

public async Task<T> FindSingleAsync<T>(Expression<Func<T, bool>> where, CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove
{
var query = Set<T>().Where(c => !c.RemovedOn.HasValue).Where(where);
var query = Set<T>().Where(c => !c.RemovedOn.HasValue);
if(where != null)
query = query.Where(where);

if (asNoTracking)
query = query.AsNoTracking();

Expand All @@ -186,13 +208,21 @@ public async Task<T> FindSingleAsync<T>(Expression<Func<T, bool>> where, Cancell

public async Task<bool> AnyAsync<T>(Expression<Func<T, bool>> where, CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove
{
var query = Set<T>().Where(c => !c.RemovedOn.HasValue).Where(where);
var query = Set<T>().Where(c => !c.RemovedOn.HasValue);
if(where != null)
query = query.Where(where);

if (asNoTracking)
query = query.AsNoTracking();

return await query.AnyAsync(token);
}

public async Task<bool> AnyAsync<T>(CancellationToken token, bool asNoTracking = false) where T : class, ITrackRemove
{
return await AnyAsync<T>(null, token, asNoTracking);
}

public async Task EnsureNotExists<T>(Guid id, CancellationToken token, bool asNoTracking = false) where T : class, IIdEntity, ITrackRemove
{
var query = Set<T>().Where(c => !c.RemovedOn.HasValue && c.Id == id);
Expand All @@ -209,7 +239,10 @@ public async Task EnsureNotExists<T>(Guid id, CancellationToken token, bool asNo

public async Task EnsureNotExists<T>(Expression<Func<T, bool>> where, CancellationToken token, bool asNoTracking = false) where T : class, IIdEntity, ITrackRemove
{
var query = Set<T>().Where(c => !c.RemovedOn.HasValue).Where(where);
var query = Set<T>().Where(c => !c.RemovedOn.HasValue);
if(where != null)
query = query.Where(where);

if (asNoTracking)
query = query.AsNoTracking();

Expand Down
17 changes: 17 additions & 0 deletions Sheaft.Infrastructure.Services/BlobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,23 @@ public async Task<Result<string>> UploadDepartmentsProgressAsync(byte[] data, Ca
});
}

public async Task<Result<string>> UploadProducersListAsync(byte[] data, CancellationToken token)
{
return await ExecuteAsync(async () =>
{
var containerClient = new BlobContainerClient(_storageOptions.ConnectionString, _storageOptions.Containers.Producers);
await containerClient.CreateIfNotExistsAsync(cancellationToken: token);
var blobClient = containerClient.GetBlobClient("producers.json");
await blobClient.DeleteIfExistsAsync(cancellationToken: token);
using (var ms = new MemoryStream(data))
await blobClient.UploadAsync(ms, token);
return Ok(blobClient.Uri.ToString());
});
}

private string GetBlobSasUri(BlobClient blobClient, BlobSasBuilder sasBuilder, string container)
{
return new UriBuilder
Expand Down
1 change: 1 addition & 0 deletions Sheaft.Options/RoutineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class RoutineOptions
public string CheckPayinsCron { get; set; } = "*/30 * * * *";
public string CheckZonesProgressCron { get; set; } = "0 0 * * *";
public string CheckZonesFileCron { get; set; } = "30 0 * * *";
public string CheckProducersFileCron { get; set; } = "45 0 * * *";
public string CheckOrdersCron { get; set; } = "0 1 * * *";
public string CheckTransfersCron { get; set; } = "0 2 * * *";
public string CheckPayoutsCron { get; set; } = "0 3 * * *";
Expand Down
1 change: 1 addition & 0 deletions Sheaft.Options/StorageOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class StorageContainers
public string Rgpd { get; set; } = "rgpd";
public string PickingOrders { get; set; } = "pickingorders";
public string Progress { get; set; } = "progress";
public string Producers { get; set; } = "producers";
public string Documents { get; set; } = "documents";
}
public class StorageTables
Expand Down
22 changes: 13 additions & 9 deletions Sheaft.Web.Jobs/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,40 +332,44 @@ public static class RecuringJobs
public static void Register(RoutineOptions options)
{
RecurringJob.AddOrUpdate<SheaftHangfireBridge>("79d5e199b5ef41268fade4da1fa3f83b", mediatr =>
mediatr.Execute(nameof(CheckOrdersCommand), new CheckOrdersCommand(new RequestUser("79d5e199b5ef41268fade4da1fa3f83b", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
mediatr.Execute(nameof(CheckOrdersCommand), new CheckOrdersCommand(new RequestUser("037e7e93c73f4406a4e31994d8686b7c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
options.CheckOrdersCron);

RecurringJob.AddOrUpdate<SheaftHangfireBridge>("44d0d009c3d24cb6b05f113e49b60d35", mediatr =>
mediatr.Execute(nameof(CheckDonationsCommand), new CheckDonationsCommand(new RequestUser("44d0d009c3d24cb6b05f113e49b60d35", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
mediatr.Execute(nameof(CheckDonationsCommand), new CheckDonationsCommand(new RequestUser("037e7e93c73f4406a4e31994d8686b7c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
options.CheckDonationsCron);

RecurringJob.AddOrUpdate<SheaftHangfireBridge>("cd2bc132393f4a379f7ac44d56f84d9e", mediatr =>
mediatr.Execute(nameof(CheckPayinsCommand), new CheckPayinsCommand(new RequestUser("cd2bc132393f4a379f7ac44d56f84d9e", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
mediatr.Execute(nameof(CheckPayinsCommand), new CheckPayinsCommand(new RequestUser("037e7e93c73f4406a4e31994d8686b7c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
options.CheckPayinsCron);

RecurringJob.AddOrUpdate<SheaftHangfireBridge>("eaf648de5fe54fc1980c093fd78bb2f7", mediatr =>
mediatr.Execute(nameof(CheckPayinRefundsCommand), new CheckPayinRefundsCommand(new RequestUser("eaf648de5fe54fc1980c093fd78bb2f7", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
mediatr.Execute(nameof(CheckPayinRefundsCommand), new CheckPayinRefundsCommand(new RequestUser("037e7e93c73f4406a4e31994d8686b7c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
options.CheckPayinRefundsCron);

RecurringJob.AddOrUpdate<SheaftHangfireBridge>("dddd91a8fa494da3af1477d1b537fd95", mediatr =>
mediatr.Execute(nameof(CheckPayoutsCommand), new CheckPayoutsCommand(new RequestUser("dddd91a8fa494da3af1477d1b537fd95", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
mediatr.Execute(nameof(CheckPayoutsCommand), new CheckPayoutsCommand(new RequestUser("037e7e93c73f4406a4e31994d8686b7c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
options.CheckPayoutsCron);

RecurringJob.AddOrUpdate<SheaftHangfireBridge>("50a160aac50f480a872b04a509ef202c", mediatr =>
mediatr.Execute(nameof(CheckTransfersCommand), new CheckTransfersCommand(new RequestUser("50a160aac50f480a872b04a509ef202c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
mediatr.Execute(nameof(CheckTransfersCommand), new CheckTransfersCommand(new RequestUser("037e7e93c73f4406a4e31994d8686b7c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
options.CheckTransfersCron);

RecurringJob.AddOrUpdate<SheaftHangfireBridge>("ae81c9c623f940b386ac9d3144147557", mediatr =>
mediatr.Execute(nameof(CheckNewPayoutsCommand), new CheckNewPayoutsCommand(new RequestUser("ae81c9c623f940b386ac9d3144147557", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
mediatr.Execute(nameof(CheckNewPayoutsCommand), new CheckNewPayoutsCommand(new RequestUser("037e7e93c73f4406a4e31994d8686b7c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
options.CheckNewPayoutsCron);

RecurringJob.AddOrUpdate<SheaftHangfireBridge>("0b74e15ec9de4332981a8f933377fc0a", mediatr =>
mediatr.Execute(nameof(UpdateZonesProgressCommand), new UpdateZonesProgressCommand(new RequestUser("0b74e15ec9de4332981a8f933377fc0a", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
mediatr.Execute(nameof(UpdateZonesProgressCommand), new UpdateZonesProgressCommand(new RequestUser("037e7e93c73f4406a4e31994d8686b7c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
options.CheckZonesProgressCron);

RecurringJob.AddOrUpdate<SheaftHangfireBridge>("7236b37addc04f62ac2afef157903132", mediatr =>
mediatr.Execute(nameof(GenerateZonesFileCommand), new GenerateZonesFileCommand(new RequestUser("7236b37addc04f62ac2afef157903132", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
mediatr.Execute(nameof(GenerateZonesFileCommand), new GenerateZonesFileCommand(new RequestUser("037e7e93c73f4406a4e31994d8686b7c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
options.CheckZonesFileCron);

RecurringJob.AddOrUpdate<SheaftHangfireBridge>("4787cf27f6bd491292014902a84a11ae", mediatr =>
mediatr.Execute(nameof(GenerateProducersFileCommand), new GenerateProducersFileCommand(new RequestUser("037e7e93c73f4406a4e31994d8686b7c", Guid.NewGuid().ToString("N"), null)), CancellationToken.None),
options.CheckProducersFileCron);
}
}
}

0 comments on commit 2a1835c

Please sign in to comment.