using System; using System.Collections.Generic; using System.Linq; using Hangfire.Server; using Kreta.BusinessLogic.Helpers.Models; using Kreta.Core.MessageBroker.Contract.MobileNotification; using Kreta.Core.MessageBroker.Contract.MobileNotification.Enum; using Kreta.Job.Tasks.Cache; using Kreta.Job.Tasks.Helpers.Utility; using Kreta.MessageBroker.Client.MobileNotification; using Kreta.Resources; using Serilog; using Serilog.Core.Enrichers; namespace Kreta.Job.Tasks.Helpers.Notification { internal static class OrarendValtozasNotificationHelper { internal static void SendOrarendValtozasNotification(PerformContext context) { var logger = Log.ForContext(new PropertyEnricher[] { new PropertyEnricher("LoggerId", Guid.NewGuid()) }); try { var userIds = new List(); foreach (var intezmenyAzonosito in KretaServer.KretaServer.Instance.GetOsszesIntezmeny()) { OrarendValtozasCache orarendValtozasCache = KretaServer.KretaServer.Instance.CacheManager.AquireCacheMobile(); if (orarendValtozasCache != null) { var orarendValtozasok = orarendValtozasCache.GetOrarendValtozasok(intezmenyAzonosito); var kikuldendoOrarendValtozasok = GetKikuldendoOrarendValtozasok(orarendValtozasok); var mobileNotificationMessages = new List(); foreach (var orarendValtozas in kikuldendoOrarendValtozasok) { mobileNotificationMessages.Add(MobileNotificationMessageHelper.CreateMessage( orarendValtozas.IntezmenyAzonosito, orarendValtozas.UserId, MobileNotificationMessageType.OrarendValtozas, orarendValtozas.OrarendiOraId, orarendValtozas.Uzenet, NotificationResource.OrarendValtozasTitle, orarendValtozas.Datum.ToIso8601UtcString())); userIds.Add(orarendValtozas.UserId); } if (mobileNotificationMessages.Any()) { orarendValtozasCache.RemoveOrarendValtozasok(intezmenyAzonosito); MobileNotificationMessageHelper.PostStudentNotification(mobileNotificationMessages.ToArray()); } } } context.SetJobParameter("UserIds", userIds); } catch (Exception ex) { logger.Fatal(ex, ex.GetType().FullName); } } private static List GetKikuldendoOrarendValtozasok(List orarendValtozasok) { var kikuldendoOrarendValtozasok = new List(); var userIdGroupedOrarendValtozasok = orarendValtozasok.GroupBy(x => x.UserId).Select(x => new { Key = x.Key, OrarendValtozasok = x.OrderBy(c => c.Datum) }); foreach (var orarendValtozas in userIdGroupedOrarendValtozasok) { var legkorabbi = orarendValtozas.OrarendValtozasok.First(); var napok = string.Concat(string.Join("i, ", orarendValtozas.OrarendValtozasok.Select(n => n.Nap).Distinct()), "i "); kikuldendoOrarendValtozasok.Add(new OrarendValtozasModel { IntezmenyAzonosito = legkorabbi.IntezmenyAzonosito, UserId = legkorabbi.UserId, IdpUniqueId = legkorabbi.IdpUniqueId, UserNev = legkorabbi.UserNev, OrarendiOraId = legkorabbi.OrarendiOraId, Uzenet = string.Format(NotificationResource.OrarendValtozasNotificationMessage, legkorabbi.UserNev, napok), Datum = legkorabbi.Datum }); } return kikuldendoOrarendValtozasok; } } }