public async Task <ActionResult <IPagedResult <SaleBasedStockOperation> > > BrowseSaleBasedStockOperations([FromQuery] BrowseStockOperations query) { if ((query.StockItemId is null || query.StockItemId.Length == 0) && !(await _authService.IsAdministrator(User))) { return(Unauthorized(new ErrorDto("unauthorized_resource_access", "Only administrators may view all sale-based stock operations."))); } await _stockItemAuthorizationLoader.AssertMultipleResourceAccessAsync(User, query.StockItemId, IsAuthorizedUserPolicy.Instance); return(Collection(await _stockOperationsService.BrowseSaleBasedStockOperations(query))); }
protected async Task EnsureRemovalOfSaleBasedOp(Guid saleId) { var sale = await _accountingService.GetSale(saleId); if (sale is null) { throw new BaristaException("sale_not_found", $"Error retrieving sale with ID {saleId} that supposedly changed state."); } var offer = await _offersService.GetOffer(sale.OfferId); if (offer is null) { _logger.LogInformation($"The sale {saleId} changed state to cancelled but its offer could not be found"); return; } if (!(offer.StockItemId is Guid stockItemId)) { return; } var autoStockOps = await _stockOperationsService.BrowseSaleBasedStockOperations(new BrowseSaleBasedStockOperations { StockItemId = new Guid[] { stockItemId }, SaleId = sale.Id }); if (autoStockOps.TotalResults == 0) { return; } else if (autoStockOps.TotalResults > 1) { throw new BaristaException("multiple_stock_ops_per_one_sale", $"Multiple stock operations yielded by query of stockItemId={stockItemId} and saleId={sale.Id}"); } var autoStockOp = autoStockOps.Items.Single(); var removalOpResult = await _busPublisher.SendRequest(new DeleteSaleBasedStockOperation(autoStockOp.Id)); if (!removalOpResult.Successful) { throw removalOpResult.ToException(); } }
protected override async Task <IEnumerable <Guid> > FindAsync(StockItemIdParameters args) => (await _stockOperationsService.BrowseSaleBasedStockOperations(new BrowseSaleBasedStockOperations() { StockItemId = new[] { args.StockItemId } })).Items.Select(i => i.Id);