public async Task DistributeMaterialForCompany(string companyName, string sapNr,
                                                       MaterialDistribution distributionList)
        {
            // check that location stock is not exceeded.
            var current = await GetInventoryItem(sapNr, companyName);

            foreach (var dist in distributionList.Distribution)
            {
                if (dist.Available < 0)
                {
                    throw new Exception(
                              $"Available is below Zero: Stock {dist.Stock} - Used {dist.Used} - Damage {dist.Damaged} = {dist.Available}");
                }
            }

            if (current == null)
            {
                // initialise inventory
                current = new MaterialInventoryItem {
                    Company = companyName, SapNr = sapNr
                };
            }
            //Set new distribution
            current.Distribution = distributionList.Distribution;

            await UpsertInventoryItem(companyName, sapNr, current);

            distributionList.Distribution.ForEach(async dist =>
            {
                await NewEventJournalEntry(new DistributionChanged(sapNr, companyName, dist.Location, dist));
            });
        }
 public Task UpsertInventoryItem(string companyName, string sapNr, MaterialInventoryItem current)
 {
     return(_context.MaterialInventory.ReplaceOneAsync(item => item.SapNr == sapNr &&
                                                       item.Company == companyName,
                                                       current, new UpdateOptions {
         IsUpsert = true
     }));
 }