Skip to content

Commit

Permalink
Merge pull request #889 from dlcs/feature/auto_finish_empty_batch
Browse files Browse the repository at this point in the history
Auto complete 0 batches if legacy mode enabled
  • Loading branch information
JackLewis-digirati authored Aug 1, 2024
2 parents f6d9aeb + 04819af commit 18c5606
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ public async Task Post_CreateBatch_201_IfLegacyModeEnabled_MembersEmpty()
var model = await response.ReadAsHydraResponseAsync<DLCS.HydraModel.CustomerQueue>();
var dbBatch = dbContext.Batches.Single(a => a.Id == model.Id.GetLastPathElementAsInt());
dbBatch.Count.Should().Be(0);
dbBatch.Finished.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromMinutes(1));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public CreateEmptyBatchHandler(IBatchRepository batchRepository)

public async Task<ModifyEntityResult<Batch>> Handle(CreateEmptyBatch request, CancellationToken cancellationToken)
{
var batch = await batchRepository.CreateBatch(request.CustomerId, Array.Empty<Asset>(), cancellationToken);
var batch = await batchRepository.CreateBatch(request.CustomerId, Array.Empty<Asset>(), cancellationToken,
batch => batch.Finished = batch.Submitted);
return ModifyEntityResult<Batch>.Success(batch, WriteResult.Created);
}
}
7 changes: 5 additions & 2 deletions src/protagonist/DLCS.Model/Assets/IBatchRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -16,6 +17,8 @@ public interface IBatchRepository
/// <param name="customerId">Id of customer to create batch for.</param>
/// <param name="assets"></param>
/// <param name="cancellationToken">Current cancellation token</param>
/// <param name="postCreate">Optional action, will be called after Batch is created but before saving in db</param>
/// <returns>Created Batch object</returns>
Task<Batch> CreateBatch(int customerId, IReadOnlyList<Asset> assets, CancellationToken cancellationToken = default);
Task<Batch> CreateBatch(int customerId, IReadOnlyList<Asset> assets, CancellationToken cancellationToken,
Action<Batch>? postCreate = null);
}
4 changes: 3 additions & 1 deletion src/protagonist/DLCS.Repository/Assets/BatchRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public BatchRepository(DlcsContext dlcsContext)

/// <inheritdoc />
public async Task<Batch> CreateBatch(int customerId, IReadOnlyList<Asset> assets,
CancellationToken cancellationToken = default)
CancellationToken cancellationToken, Action<Batch>? postCreate = null)
{
var batch = new Batch
{
Expand All @@ -31,6 +31,8 @@ public async Task<Batch> CreateBatch(int customerId, IReadOnlyList<Asset> assets
Submitted = DateTime.UtcNow,
Superseded = false
};

postCreate?.Invoke(batch);
DlcsContext.Batches.Add(batch);
await DlcsContext.SaveChangesAsync(cancellationToken);

Expand Down

0 comments on commit 18c5606

Please sign in to comment.