private async Task SetIsBalancedPlanAync(int loggedUser, DateTime planUtcAt) { var user = await _userService.GetUserDevicesAsync(loggedUser); var userEats = await GetUserPlanPerDateAsync(loggedUser, planUtcAt); var imcKcals = await GetKCalImcAsync(loggedUser, planUtcAt); bool? oldPlanBalanced = null; DateTime?oldPlanCreatedAt = null; if (userEats.Count() > 0) { // Plan Exists var d = userEats.FirstOrDefault(); oldPlanBalanced = d.IsBalancedPlan; oldPlanCreatedAt = d.PlanCreatedAt; } var kcal = imcKcals.kcal; var imc = imcKcals.imc; IHealthyHelper healthyHelper = new HealthyHelper(imc, kcal, _accountService, _dishService); var result = healthyHelper.IsBalancedPlan(user, userEats); var nowUtc = DateTime.UtcNow; var userCurrentDateTime = nowUtc.AddHours(user.TimeZoneOffset); foreach (var eat in userEats) { eat.ModifiedAt = nowUtc; eat.IsBalanced = result.IsBalanced; if (IsValidDateForPlan(eat.CreatedAt, userCurrentDateTime)) { eat.PlanCreatedAt = planUtcAt; eat.IsBalancedPlan = result.IsBalanced; } else { eat.PlanCreatedAt = oldPlanCreatedAt; eat.IsBalancedPlan = oldPlanBalanced; } await _uow.EatRepository.UpdateAsync(eat, eat.Id); } await _uow.CommitAsync(); }
public async Task <IActionResult> IsValancedPlan([FromQuery] DateTime dateInUtc) { var loggedUser = User.GetUserIdFromToken(); var user = await _userService.GetUserDevicesAsync(loggedUser); var plan = await _eatService.GetUserPlanPerDateAsync(loggedUser, dateInUtc); var userImcKcal = await _eatService.GetKCalImcAsync(loggedUser, dateInUtc); IHealthyHelper healthyHelper = new HealthyHelper(userImcKcal.imc, userImcKcal.kcal, _accountService, _dishService); var result = healthyHelper.IsBalancedPlan(user, plan); return(Ok(new ApiOkResponse(result))); }
/// <summary> /// Return list of plan summary assuming that only one user's eats are provided /// </summary> /// <param name="eats">List of eats of an user</param> /// <returns></returns> public async Task <List <PlanSummaryResponse> > GetPlanSummaryAsync(IEnumerable <Eat> eats) { var result = new List <PlanSummaryResponse>(); var anyEat = eats.FirstOrDefault(); if (anyEat == null) { return(result); } var user = await _userService.GetUserDevicesAsync(anyEat.UserId); foreach (var eat in eats) { var planDate = eat.CreatedAt; if (!result.Any(r => r.PlanDateTime.Date == planDate.Date)) { var userEats = eats.Where(e => e.CreatedAt.Date == planDate.Date); IHealthyHelper healthyHelper = new HealthyHelper(eat.ImcAtThatMoment, eat.KCalAtThatMoment, _accountService, _dishService); var userHealthParameters = healthyHelper.GetUserEatHealtParameters(user); var isBalancedSummary = healthyHelper.IsBalancedPlan(user, userEats); var planSummary = new PlanSummaryResponse { PlanDateTime = planDate, UserEatHealtParameters = userHealthParameters, EatBalancedSummary = isBalancedSummary }; result.Add(planSummary); } } return(result); }