public async Task AddUpstreamAppointmentsAsync(List <AppointmentExtraInfo> appList, int?year, string calendarName, CalendarDisplay display, string upstreamCustomTokenInput) { try { if (!year.HasValue) { return; } var upstreamApi = _configuration[ConfigurationValues.UpstreamApiUrl]; var httpClient = _httpClientFactory.CreateClient(); if (_configuration.GetValue <bool>(ConfigurationValues.UpstreamApiCustomTokenInput)) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", upstreamCustomTokenInput); } if (!string.IsNullOrWhiteSpace(_configuration[ConfigurationValues.UpstreamApiKey])) { httpClient.DefaultRequestHeaders.Add(UpstreamApiKeyHeaderName, _configuration[ConfigurationValues.UpstreamApiKey]); } var res = await httpClient.GetFromJsonAsync <IEnumerable <UpstreamAppointment> >(upstreamApi + $"?year={year}&calendarName={calendarName}&display={display}"); var customers = await _context.Customers.ToListAsync(); var colorCombination = new Dictionary <string, (string color, string textColor)>(); var countColor = 0; foreach (var ele in res) { var colorHex = ""; var textColorHex = ""; var matchedCustomer = customers.Where(x => x.ShortDescription.Equals(ele.CustomerShortDescription.Split(new[] { '\r', '\n' }).FirstOrDefault(), StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); if (matchedCustomer == null) { matchedCustomer = customers.Where(x => x.Name.Equals(ele.CustomerShortDescription.Split(new[] { '\r', '\n' }).FirstOrDefault(), StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); } if (colorCombination.ContainsKey(ele.CustomerShortDescription.ToLower().Trim())) { var currentCombinationToUse = colorCombination[ele.CustomerShortDescription.ToLower().Trim()]; colorHex = currentCombinationToUse.color; textColorHex = currentCombinationToUse.textColor; } else { colorHex = Colors[countColor]; textColorHex = TextColors[countColor]; colorCombination.Add(ele.CustomerShortDescription.ToLower().Trim(), (colorHex, textColorHex)); countColor++; if (countColor >= Colors.Length) { countColor = 0; } } appList.Add(new AppointmentExtraInfo { ID = ele.ID, StartDate = ele.StartDate.Date, EndDate = ele.EndDate.Date, Customer = matchedCustomer ?? new Customer { ShortDescription = ele.CustomerShortDescription, Color = colorHex, TextColor = textColorHex }, Note = ele.Description, IsFromUpstream = true, TypeID = 99, Type = new AppointmentType { ID = 99, Billable = ele.Billable, RequireCustomer = true }, Confirmed = true, CalendarName = ele.SourceCalendar, RequireTravel = ele.HasWarning }); } } catch (Exception ex) { _logger.LogError(ex, ex.Message); } }
public async Task <ActionResult <IEnumerable <AppointmentExtraInfo> > > GetAppointments(int?year, string calendarName, CalendarDisplay display, [FromHeader] string upstreamCustomTokenInput) { var appointment = _context.Appointments.Include(x => x.Customer).Include(x => x.Type).AsQueryable(); if (year.HasValue) { appointment = appointment.Where(x => x.StartDate.Year == year); } if (display == CalendarDisplay.Event || (display == CalendarDisplay.Calendar && !string.IsNullOrWhiteSpace(calendarName))) { if (!string.IsNullOrWhiteSpace(calendarName)) { appointment = appointment.Where(x => x.CalendarName == calendarName); } else { appointment = appointment.Where(x => x.CalendarName == "" || x.CalendarName == null); } } var appList = await appointment.OrderBy(x => x.StartDate).ToListAsync(); var appointmentList = _mapper.Map <List <AppointmentExtraInfo> >(appList); _warningChecker.PerformCheck(appointmentList); await _upstreamAppointments.AddUpstreamAppointmentsAsync(appointmentList, year, calendarName, display, upstreamCustomTokenInput); SetProjectColor(appointmentList); return(appointmentList); }