public async Task DistributeGradeForCompany(string companyName, Grade grade, GradeDistribution distributionList) { // check that location stock is not exceeded. var current = await GetInventoryItem(grade, 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.Detached} = {dist.Available}"); } } if (current == null) { // initialise inventory current = new ServantInventoryItem { Company = companyName, Grade = grade }; } //Set new distribution current.Distribution = distributionList.Distribution; await UpsertInventoryItem(companyName, grade, current); distributionList.Distribution.ForEach(async dist => { await NewEventJournalEntry(new DistributionChanged(grade, companyName, dist.Location, dist)); }); }
public Task UpsertInventoryItem(string companyName, Grade grade, ServantInventoryItem current) { return(_context.ServantInventory.ReplaceOneAsync(item => item.Grade == grade && item.Company == companyName, current, new UpdateOptions { IsUpsert = true })); }