public async Task SendNotifications(int notificationCodeId, string agencyCode, int agencyType, MainNotificationTemplateModel mainView, string UserRole, int committeeId = 0, int branchid = 0) { mainView.CommitteeId = committeeId; mainView.BranchId = branchid; var oldUserNotificationSettings = await _iNotificationQuerie.GetNotificationSettingByUserIdAndUserType(notificationCodeId, branchid, committeeId); if (oldUserNotificationSettings.Count == 0) { return; } UsersSearchCriteriaModel _usersSearchCriteriaModel = new UsersSearchCriteriaModel() { RoleName = UserRole, AgencyId = agencyCode, PageSize = 1000, AgencyType = agencyType }; var idmUsers = await _idmProxy.GetMonafasatUsersByAgencyTypeAndRoleName(_usersSearchCriteriaModel); if (idmUsers == null || !idmUsers.Items.Any()) { return; } var Users = _mapper.Map <QueryResult <EmployeeIntegrationModel> >(idmUsers); List <int> users = Users.Items.Select(x => x.userId).ToList(); var userNotificationSettings = await _iNotificationQuerie.GetNotificationSettingByRoleAndOperationCode(users, notificationCodeId, branchid, committeeId); await SendNotificationForUsers(idmUsers, userNotificationSettings, mainView); }
private async Task SendNotificationForUsers(int notificationCodeId, int branchId, int?committeeId, MainNotificationTemplateModel mainNotificationTemplateModel) { try { string output = JsonConvert.SerializeObject(mainNotificationTemplateModel); MainNotificationTemplateModel mainView = JsonConvert.DeserializeObject <MainNotificationTemplateModel>(output); var oldUserNotificationSettings = await _iNotificationQuerie.GetNotificationSettingByUserIdAndUserType(notificationCodeId, branchId, (committeeId ?? 0)); if (oldUserNotificationSettings.Count == 0) { return; } string UserRole = ((Enums.UserRole)oldUserNotificationSettings[0].UserRoleId).ToString(); var agencyDetails = new GovAgency(); if (branchId != 0) { agencyDetails = await _BranchQuery.GetAgencyCodeByBranchId(branchId); } else if (committeeId != null && committeeId != 0) { agencyDetails = await _CommitteeQuery.FindAgencyCodeByCommitteeId(committeeId); } var agencyCode = agencyDetails != null ? agencyDetails.AgencyCode : ""; var agencyType = agencyDetails != null ? agencyDetails.CategoryId : 0; UsersSearchCriteriaModel _usersSearchCriteriaModel = new UsersSearchCriteriaModel() { RoleName = UserRole, AgencyId = agencyCode, PageSize = 1000, AgencyType = agencyType ?? 0 }; var idmUsers = await _idmProxy.GetMonafasatUsersByAgencyTypeAndRoleName(_usersSearchCriteriaModel); var Users = _mapper.Map <QueryResult <EmployeeIntegrationModel> >(idmUsers); List <int> users = Users.Items.Select(x => x.userId).ToList(); var userNotificationSettings = await _iNotificationQuerie.GetNotificationSettingByRoleAndOperationCode(users, notificationCodeId, branchId, committeeId); var userNotificationsModel = userNotificationSettings.Select(x => new UserNotificationSettingModel { UserId = x.UserProfileId.Value, Email = idmUsers.Items.Where(i => i.userId == x.User.Id).FirstOrDefault().email, Mobile = idmUsers.Items.Where(i => i.userId == x.User.Id).FirstOrDefault().mobileNumber }).ToList(); var entityKey = TempletKey(mainView.EntityValue, mainView.EntityType); bool IsNullEmailFirstArrgs = string.IsNullOrEmpty(Convert.ToString(mainView.Args.BodyEmailArgs[0])); bool IsNullSmsmFirstArrgs = string.IsNullOrEmpty(Convert.ToString(mainView.Args.SMSArgs[0])); NotificationEmail email; NotificationSMS sms; NotificationPanel panel; foreach (var setting in userNotificationSettings) { if (IsNullEmailFirstArrgs) { mainView.Args.BodyEmailArgs[0] = Convert.ToString(setting.User.FullName); } if (IsNullSmsmFirstArrgs) { mainView.Args.SMSArgs[0] = Convert.ToString(setting.User.FullName); } NotificationDataModel template = await BuildNotificationTemplate(userNotificationSettings.FirstOrDefault().IsArabic, userNotificationSettings.FirstOrDefault().NotificationCodeId, mainView.Args); if (setting.Email) { email = new NotificationEmail(setting.UserProfileId.Value, userNotificationsModel.FirstOrDefault(u => u.UserId == setting.UserProfileId.Value).Email, template.Email.Title, template.Email.Body, setting.Id, mainView.Link, entityKey); await _notifayCommands.AddNotifayWithOutSave(email); } if (setting.Sms) { sms = new NotificationSMS(setting.UserProfileId.Value, userNotificationsModel.FirstOrDefault(u => u.UserId == setting.UserProfileId.Value).Mobile, template.SMS.Body, setting.Id, mainView.Link, entityKey); await _notifayCommands.AddNotifayWithOutSave(sms); } panel = new NotificationPanel(setting.UserProfileId.Value, template.PanelMessage, template.PanelMessage, setting.Id, mainView.Link, mainView.BranchId, mainView.CommitteeId, entityKey); await _notifayCommands.AddNotifayWithOutSave(panel); } if (userNotificationSettings.Count > 0) { await _notifayCommands.SaveChangesAsync(); } } catch (Exception ex) { _logger.LogError(ex.ToString()); } }