Esempio n. 1
0
        public async Task HandleStaleGroups()
        {
            var allGroups = await _stockportApiRepository.GetResponse <List <Group> >();

            if (allGroups == null || !allGroups.Any())
            {
                throw new GroupsServiceException("No groups were returned from content api");
            }

            _logger.LogInformation($"Returned {allGroups.Count} groups");

            var emailPeriods = _configuration.GetArchiveEmailPeriods();

            if (emailPeriods == null || !emailPeriods.Any())
            {
                throw new GroupsServiceException("No periods returned from the service");
            }

            var fromAddress = _configuration.GetEmailEmailFrom("stockportgov");

            foreach (var period in emailPeriods)
            {
                var stagedGroups = FilterGroupsByStage(allGroups, period.NumOfDays);

                if (period.NumOfDays == emailPeriods.Select(p => p.NumOfDays).Max())
                {
                    var loopExceptions = new List <Exception>();
                    foreach (var group in stagedGroups)
                    {
                        try
                        {
                            ArchiveGroup(group);
                        }
                        catch (Exception e)
                        {
                            loopExceptions.Add(e);
                        }
                    }

                    foreach (var exception in loopExceptions)
                    {
                        _logger.LogError(exception.Message);
                    }
                }

                SendEmailToGroups(stagedGroups, period.Template, period.Subject, fromAddress.ToString());
            }
        }