public override async Task <bool> HandleMessage(TimedEvent data) { // Update Status to Idle var success = await this.readModelService.UpdateStatus(data.DriverId, DriverStatus.Idle); //if (success == false) // driver not found // TODO : // TODO : Get stats for today from Package API // TODO : Remove Mocks var todaysStats = new StatsUpdateEvent { PODs = 0.85m, DNCs = 0.15m }; // TODO : Calculate overall stats from Package API // TODO : Remove Mocks var overAllStats = new StatsUpdateEvent { PODs = 0.864m, DNCs = 0.136m }; return(await this.readModelService.UpdateDriverWithStats(data.DriverId, todaysStats, overAllStats)); }
public async Task <bool> UpdateDriverWithStats(string driverId, StatsUpdateEvent todaysStats, StatsUpdateEvent overallStats) { // get driver from DB (to update) var query = Builders <Driver> .Filter.Eq(t => t.DriverId, driverId); var driver = await this.collection.Find(query).FirstAsync(); // Update the stats on the driver object driver.OverallStats = overallStats; driver.LatestStats.Add(todaysStats); // keep only the last 7 stats in the list if (driver.LatestStats.Count > 7) { driver.LatestStats.RemoveAt(0); } // replace the document // TODO : This should technically be an update statement for concurrency var result = await this.collection.ReplaceOneAsync(t => t.Id == driver.Id, driver, new UpdateOptions { IsUpsert = false }); return(result.ModifiedCount == 1); }