Beispiel #1
0
        public Tuple <bool, string> Process(ShiftNotifierQueueItem item)
        {
            bool   success = true;
            string result  = "";

            if (item != null && item.Shift != null)
            {
                var    text             = _shiftsService.GenerateShiftNotificationText(item.Shift);
                string departmentNumber = _departmentSettingsService.GetTextToCallNumberForDepartment(item.Shift.DepartmentId);

                if (item.Shift.Personnel != null)
                {
                    foreach (var person in item.Shift.Personnel)
                    {
                        UserProfile profile = item.Profiles.FirstOrDefault(x => x.UserId == person.UserId);
                        _communicationService.SendNotification(person.UserId, item.Shift.DepartmentId, text, departmentNumber,
                                                               item.Shift.Name, profile);
                    }
                }

                if (item.Signups != null)
                {
                    foreach (var signup in item.Signups)
                    {
                        if (signup.Trade != null && signup.Trade.IsTradeComplete())
                        {
                            if (!String.IsNullOrWhiteSpace(signup.Trade.UserId))
                            {
                                UserProfile profile = item.Profiles.FirstOrDefault(x => x.UserId == signup.Trade.UserId);
                                _communicationService.SendNotification(signup.Trade.UserId, item.Shift.DepartmentId, text, departmentNumber,
                                                                       item.Shift.Name, profile);
                            }
                            else if (signup.GetTradeType() == ShiftTradeTypes.Source)
                            {
                                UserProfile profile = item.Profiles.FirstOrDefault(x => x.UserId == signup.Trade.TargetShiftSignup.UserId);
                                _communicationService.SendNotification(signup.Trade.TargetShiftSignup.UserId, item.Shift.DepartmentId, text, departmentNumber,
                                                                       item.Shift.Name, profile);
                            }
                            else if (signup.GetTradeType() == ShiftTradeTypes.Target)
                            {
                                UserProfile profile = item.Profiles.FirstOrDefault(x => x.UserId == signup.Trade.SourceShiftSignup.UserId);
                                _communicationService.SendNotification(signup.Trade.SourceShiftSignup.UserId, item.Shift.DepartmentId, text, departmentNumber,
                                                                       item.Shift.Name, profile);
                            }
                        }
                        else
                        {
                            UserProfile profile = item.Profiles.FirstOrDefault(x => x.UserId == signup.UserId);
                            _communicationService.SendNotification(signup.UserId, item.Shift.DepartmentId, text, departmentNumber,
                                                                   item.Shift.Name, profile);
                        }
                    }
                }
            }

            return(new Tuple <bool, string>(success, result));
        }
Beispiel #2
0
        public async Task ProcessAsync(ShiftNotiferCommand command, IQuidjiboProgress progress, CancellationToken cancellationToken)
        {
            try
            {
                progress.Report(1, $"Starting the {Name} Task");

                //await Task.Run(async () =>
                //{
                IUserProfileService _userProfileService = null;
                ILogService         _logsService        = null;
                var _shiftsService = Bootstrapper.GetKernel().Resolve <IShiftsService>();

                var logic = new ShiftNotifierLogic();

                var shifts = await _shiftsService.GetShiftsStartingNextDayAsync(DateTime.UtcNow);

                if (shifts != null && shifts.Any())
                {
                    _logger.LogInformation("ShiftNotifer::Shifts to Notify: " + shifts.Count());

                    _userProfileService = Bootstrapper.GetKernel().Resolve <IUserProfileService>();
                    _logsService        = Bootstrapper.GetKernel().Resolve <ILogService>();

                    foreach (var shift in shifts)
                    {
                        var qi = new ShiftNotifierQueueItem();

                        var processLog = await _logsService.GetProcessLogForTypeTimeAsync(ProcessLogTypes.ShiftNotifier, shift.ShiftId, shift.StartDay);

                        if (processLog != null)
                        {
                            await _logsService.SetProcessLogAsync(ProcessLogTypes.ShiftNotifier, shift.ShiftId, shift.StartDay);

                            if (shift.Personnel != null && shift.Personnel.Any())
                            {
                                qi.Profiles = await _userProfileService.GetSelectedUserProfilesAsync(shift.Personnel.Select(x => x.UserId).ToList());
                            }

                            qi.Day = shift.GetShiftDayforDateTime(DateTime.UtcNow.AddDays(1));
                            if (qi.Day != null)
                            {
                                if (qi.Profiles == null)
                                {
                                    qi.Profiles = new List <UserProfile>();
                                }

                                qi.Signups = await _shiftsService.GetShiftSignpsForShiftDayAsync(qi.Day.ShiftDayId);

                                if (qi.Signups != null && qi.Signups.Any())
                                {
                                    qi.Profiles.AddRange(await _userProfileService.GetSelectedUserProfilesAsync(qi.Signups.Select(x => x.UserId).ToList()));

                                    var users = new List <string>();
                                    foreach (var signup in qi.Signups)
                                    {
                                        if (signup.Trade != null)
                                        {
                                            if (!String.IsNullOrWhiteSpace(signup.Trade.UserId))
                                            {
                                                users.Add(signup.Trade.UserId);
                                            }
                                            else if (signup.Trade.TargetShiftSignup != null)
                                            {
                                                users.Add(signup.Trade.TargetShiftSignup.UserId);
                                            }
                                        }
                                    }

                                    if (users.Any())
                                    {
                                        qi.Profiles.AddRange(await _userProfileService.GetSelectedUserProfilesAsync(users));
                                    }
                                }
                            }

                            qi.Shift = shift;

                            _logger.LogInformation("ShiftNotifer::Processing Shift Notification: " + qi.Shift.ShiftId);

                            var result = await logic.Process(qi);

                            if (result.Item1)
                            {
                                _logger.LogInformation($"ShiftNotifer::Processed Shift Notification {qi.Shift.ShiftId} successfully.");
                            }
                            else
                            {
                                _logger.LogInformation($"ShiftNotifer::Failed to Process shift notification {qi.Shift.ShiftId} error {result.Item2}");
                            }
                        }
                    }
                }
                //}, cancellationToken);

                progress.Report(100, $"Finishing the {Name} Task");
            }
            catch (Exception ex)
            {
                Resgrid.Framework.Logging.LogException(ex);
                _logger.LogError(ex.ToString());
            }
        }