//-------------------------------------------------------------------------------------------------- /// <summary> /// Used to send emails (without Remainder) when we only have email address, especially for invitation /// to people outside of the system. /// Now use to send invitation to enroll to course. /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task <IHttpActionResult> GenerateAndSendEmails(WebApiModel.eLearning.NotificationModel model) { var template = db.NotificationTemplates.Where(t => t.NotificationType == model.NotificationType).FirstOrDefault(); string emailSubject = generateBodyMessage(template.TemplateSubject, model.NotificationType, model.ParameterListToSend); var receivers = new List <string>(); if (model.ReceiverType == WebApiModel.eLearning.ReceiverType.UserIds || model.ReceiverType == WebApiModel.eLearning.ReceiverType.Both) { foreach (var item in model.ReceiverId) { string receiverEmailAddress = db.User.Find(item).Email; receivers.Add(receiverEmailAddress); } } if (model.ReceiverType == WebApiModel.eLearning.ReceiverType.Emails || model.ReceiverType == WebApiModel.eLearning.ReceiverType.Both) { //send email ke setiap reciever if (!String.IsNullOrEmpty(model.Emails)) { receivers = model.Emails.Split(',').ToList(); } else { return(BadRequest()); } } int counter = 0; foreach (var receiver in receivers) { var cleanReceiver = receiver.Trim(); model.ParameterListToSend.ReceiverFullName = GetReceiverFullName(receiver); string emailBody = generateBodyMessage(template.TemplateMessage, model.NotificationType, model.ParameterListToSend); var response = await sendEmailUsingAPIAsync(DateTime.Now, (int)model.NotificationCategory, (int)model.NotificationType, cleanReceiver, emailSubject, emailBody, counter); if (response != null) { string EmailNotificationId = response.datID; //assumed returned Id // --> CALL insert BulkNotificationGroup API (NotificationMedium : Email, int [SLAReminderStatusId]) BulkNotificationModel objEmailNotification = new BulkNotificationModel { NotificationMedium = NotificationMedium.Email, NotificationId = EmailNotificationId }; var responseEmailNotificationGroup = RegisterBulkNotificationGroupFunc(objEmailNotification); } counter++; } ReminderResponse result = new ReminderResponse { Status = "Success", }; return(Ok(result)); }
public async Task <IHttpActionResult> GenerateAutoNotificationReminder(CreateAutoReminder reminder) { List <DateTime> ScheduleMessage = GetSLAReminder(reminder.NotificationType, reminder.StartNotificationDate); var template = db.NotificationTemplates.Where(t => t.NotificationType == reminder.NotificationType).FirstOrDefault(); int SLAReminderId = 0; // --> CALL StartNotification API (register SLAReminder) -> return [SLAReminderStatusId] CreateSLAReminderStatusModel objReminder = new CreateSLAReminderStatusModel { NotificationType = reminder.NotificationType, NotificationReminderStatusType = NotificationReminderStatusType.Open, StartDate = reminder.StartNotificationDate }; var responseStartNotification = StartNotificationFunc(objReminder); /*var responseStartNotification = await WepApiMethod.SendApiAsync<CreateSLAReminderStatusModel> * (HttpVerbs.Post, $"Reminder/SLA/StartNotification/", objReminder);*/ if (responseStartNotification != null) { SLAReminderId = responseStartNotification.Id; if (template.enableEmail) { string emailSubject = generateBodyMessage(template.TemplateSubject, reminder.NotificationType, reminder.ParameterListToSend); //send email ke setiap reciever foreach (var receiver in reminder.ReceiverId) { string receiverEmailAddress = db.User.Find(receiver).Email; reminder.ParameterListToSend.ReceiverFullName = GetReceiverFullName(receiverEmailAddress); string emailBody = generateBodyMessage(template.TemplateMessage, reminder.NotificationType, reminder.ParameterListToSend); int counter = 1; //send notification mengikut jadual foreach (var notifyDate in ScheduleMessage) { // --> CALL EMAIL API HERE--- // | send received notificationId here // \|/ var response = await sendEmailUsingAPIAsync(notifyDate, (int)reminder.NotificationCategory, (int)reminder.NotificationType, receiverEmailAddress, emailSubject, emailBody, counter); if (response != null) { string EmailNotificationId = response.datID; //assumed returned Id // --> CALL insert BulkNotificationGroup API (NotificationMedium : Email, int [SLAReminderStatusId]) BulkNotificationModel objEmailNotification = new BulkNotificationModel { SLAReminderStatusId = SLAReminderId, NotificationMedium = NotificationMedium.Email, NotificationId = EmailNotificationId }; var responseEmailNotificationGroup = RegisterBulkNotificationGroupFunc(objEmailNotification); } counter++; } } } if (template.enableSMSMessage) { //send sms ke setiap reciever foreach (var receiver in reminder.ReceiverId) { string receiverPhoneNo = db.User.Find(receiver).MobileNo; int counter = 1; foreach (var notifyDate in ScheduleMessage) { string SMSToSend = generateSMSMessage(template.SMSMessage, template.NotificationType, reminder.ParameterListToSend); // --> CALL SMS API HERE----- // | send received notificationId here // \|/ var response = await sendSMSUsingAPIAsync(notifyDate, (int)reminder.NotificationCategory, (int)reminder.NotificationType, receiverPhoneNo, null, SMSToSend, counter); if (response != null) { string SMSNotificationId = response.datID; //assumed returned Id // --> CALL insert BulkNotificationGroup API (NotificationMedium : SMS, [SLAReminderStatusId]) BulkNotificationModel objSMSNotification = new BulkNotificationModel { SLAReminderStatusId = SLAReminderId, NotificationMedium = NotificationMedium.SMS, NotificationId = SMSNotificationId }; var responseSMSNotificationGroup = RegisterBulkNotificationGroupFunc(objSMSNotification); } counter++; } } } if (template.enableWebMessage) { foreach (var receiver in reminder.ReceiverId) { int counter = 1; foreach (var notifyDate in ScheduleMessage) { string WebTextToSend = generateWEBMessage(template.WebMessage, template.NotificationType, reminder.ParameterListToSend); string WebLinkTextToSend = generateWEBLinkMessage(template.WebNotifyLink, template.NotificationType, reminder.ParameterListToSend); // --> CALL WEB API HERE----- // | send received notificationId here // \|/ CreateNotificationModel model = new CreateNotificationModel { UserId = receiver, NotificationType = reminder.NotificationType, Category = reminder.NotificationCategory, Message = WebTextToSend, Link = WebLinkTextToSend, SendDate = notifyDate }; var response = await sendWebNotifyAPI(model); if (response != -1) { string WEBNotificationId = response.ToString(); //assumed returned Id // --> CALL insert BulkNotificationGroup API (NotificationMedium : Web, [SLAReminderStatusId]) BulkNotificationModel objWEBNotification = new BulkNotificationModel { SLAReminderStatusId = SLAReminderId, NotificationMedium = NotificationMedium.Web, NotificationId = WEBNotificationId }; var responseWEBNotificationGroup = RegisterBulkNotificationGroup(objWEBNotification); } counter++; } } } ReminderResponse result = new ReminderResponse { Status = "Success", SLAReminderStatusId = SLAReminderId }; return(Ok(result)); } else { ReminderResponse result = new ReminderResponse { Status = "Failed", SLAReminderStatusId = SLAReminderId }; return(Ok(result)); } }