public void Execute() { //is plugin installed? var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName("Widgets.mobSocial"); if (pluginDescriptor == null) { return; } //plugin var plugin = pluginDescriptor.Instance() as MobSocialWebApiPlugin; if (plugin == null) { return; } //the task will send reminders to customers based on the timezone they are in, we don't want to send ill-timed emails //first get all the battles int notUsedTotalPages; var allBattles = _videoBattleService.GetAll(null, null, null, null, null, null, string.Empty, null, null, out notUsedTotalPages, 1, int.MaxValue); #region to participants who have not uploaded video, that means only battles with pending status and within settings' threshold var pendingBattles = allBattles.Where(x => x.VideoBattleStatus == VideoBattleStatus.Pending && x.VotingStartDate.Subtract(DateTime.UtcNow).TotalDays <= _mobSocialSettings.VideoUploadReminderEmailThresholdDays); //TODO: Find a way to improve performance of this feature foreach (var battle in pendingBattles) { //participantVideos for this battle var participantVideos = _videoBattleVideoService.GetBattleVideos(battle.Id); //the ids of participants who have uploaded videos var participantIds = participantVideos.Select(x => x.ParticipantId); var battleParticipants = _videoBattleParticipantService.GetVideoBattleParticipants(battle.Id, VideoBattleParticipantStatus.ChallengeAccepted); //the ids of participants who need to be reminded var requireReminderParticipantIds = battleParticipants.Where(x => !participantIds.Contains(x.ParticipantId)).Select(x => x.ParticipantId).ToArray(); //send reminders to them var participantCustomers = _customerService.GetCustomersByIds(requireReminderParticipantIds); foreach (var customer in participantCustomers) { //get the current time in customer's timezone var customerDateTime = _dateTimeHelper.ConvertToUserTime(DateTime.UtcNow, TimeZoneInfo.Utc, _dateTimeHelper.GetCustomerTimeZone(customer)); //make sure it's good time to send email, between 7:00 AM - 5:00 PM should be a good time right? if (customerDateTime.Hour >= 7 && customerDateTime.Hour <= 17) { _mobSocialMessageService.SendXDaysToBattleStartNotificationToParticipant(customer, battle, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id); } } } #endregion #region to the followers who are following the battle but haven't yet voted var openBattles = allBattles.Where(x => x.VideoBattleStatus == VideoBattleStatus.Open && x.VotingStartDate.Subtract(DateTime.UtcNow).TotalDays <= _mobSocialSettings.VideoUploadReminderEmailThresholdDays); //TODO: Find a way to improve performance of this feature foreach (var battle in openBattles) { //followers of this battle var followers = _customerFollowService.GetFollowers <VideoBattle>(battle.Id); //get the votes casted so far var votes = _videoBattleVoteService.GetVideoBattleVotes(battle.Id, null); //their customer ids var votesCustomerIds = votes.Select(x => x.ParticipantId); //and now the followers who haven't voted, need to be reminded//should not be a battle owner either var followersToRemindIds = followers.Where(x => !votesCustomerIds.Contains(x.CustomerId) && x.CustomerId != battle.ChallengerId).Select(x => x.CustomerId).ToArray(); //send reminders to them var followerCustomers = _customerService.GetCustomersByIds(followersToRemindIds); foreach (var customer in followerCustomers) { //get the current time in customer's timezone var customerDateTime = _dateTimeHelper.ConvertToUserTime(DateTime.UtcNow, TimeZoneInfo.Utc, _dateTimeHelper.GetCustomerTimeZone(customer)); //make sure it's good time to send email, between 7:00 AM - 5:00 PM should be a good time right? if (customerDateTime.Hour >= 7 && customerDateTime.Hour <= 17) { _mobSocialMessageService.SendXDaysToBattleEndNotificationToFollower(customer, battle, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id); } } } #endregion //TODO: Send a consolidated email which contains all the battles (to participate and to vote) }