-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from COMP-1640-GRE/cuongphgch210011
Cuongphgch210011
- Loading branch information
Showing
7 changed files
with
50 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
using System.Net.Mail; | ||
using DotnetGRPC.Model.DTO; | ||
using DotnetGRPC.Model; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Options; | ||
using Grpc.Core; | ||
using System.Net; | ||
|
@@ -27,26 +25,24 @@ public NotificationService(AppDbContext dbContext, IOptions<EmailSettings> email | |
_notificationRepository = notificationRepository; | ||
} | ||
|
||
public async Task SendEmailAsync(long studentUserId, string templateCode, long staffUserId, string option) | ||
public async Task SendEmailAsync(long studentUserId, string templateCode, string option) | ||
{ | ||
if (studentUserId == 0) | ||
{ | ||
throw new ArgumentException("Student is null"); | ||
} | ||
|
||
var student = await _userRepository.FindByIdAsync(studentUserId); | ||
var staff = await _userRepository.FindByIdAsync(staffUserId); | ||
var template = await _templateRepository.FindByTemplateCodeAsync(templateCode); | ||
var student = await _userRepository.FindByIdAsync(studentUserId) ?? throw new ArgumentException("Student not found"); | ||
var template = await _templateRepository.FindByTemplateCodeAsync(templateCode) ?? throw new ArgumentException("Template not found"); | ||
using (var mailMessage = new MailMessage()) | ||
{ | ||
mailMessage.To.Add(new MailAddress(student.Email)); | ||
mailMessage.From = new MailAddress(staff.Email); | ||
mailMessage.From = new MailAddress("[email protected]"); | ||
mailMessage.Subject = template.TemplateName; | ||
mailMessage.Body = template.TemplateContent | ||
.Replace("[Student Name]", $"{student.FirstName} {student.LastName}") | ||
.Replace("[Name]", $"{staff.FirstName} {staff.LastName}") | ||
.Replace("[Role]", staff.Role.ToString()) | ||
.Replace("[choose appropriate option]", option); | ||
.Replace("[choose appropriate option]", option) | ||
.Replace("[6-Letter Code]", option); | ||
mailMessage.IsBodyHtml = true; | ||
|
||
using (var smtpClient = new SmtpClient(_emailSettings.Host, _emailSettings.Port)) | ||
|
@@ -60,47 +56,56 @@ public async Task SendEmailAsync(long studentUserId, string templateCode, long s | |
|
||
public override async Task<NotificationResponse> sendNotification(NotificationRequest request, ServerCallContext context) | ||
{ | ||
if (request.WithEmail) | ||
{ | ||
await SendEmailAsync(request.StudentUserid, request.TemplateCode.Replace("noti", "email"), request.StaffUserid, request.Option); | ||
} | ||
var content = string.Empty; | ||
|
||
var template = await _templateRepository.FindByTemplateCodeAsync(request.TemplateCode); | ||
var content = template.TemplateContent.Replace("[choose appropriate option]", request.Option); | ||
|
||
var notification = new Model.Notification | ||
if (request.TemplateCode != "reset_pw_email") | ||
{ | ||
Content = content, | ||
Seen = false, | ||
UserId = request.StudentUserid, | ||
WithEmail = request.WithEmail | ||
}; | ||
if (request.WithEmail) | ||
{ | ||
await SendEmailAsync(request.UserId, request.TemplateCode.Replace("noti", "email"), request.Option); | ||
} | ||
var template = await _templateRepository.FindByTemplateCodeAsync(request.TemplateCode); | ||
content = template.TemplateContent.Replace("[choose appropriate option]", request.Option); | ||
|
||
await _notificationRepository.SaveAsync(notification); | ||
var notification = new Model.Notification | ||
{ | ||
Content = content, | ||
Seen = false, | ||
UserId = request.UserId, | ||
WithEmail = request.WithEmail | ||
}; | ||
|
||
await _notificationRepository.SaveAsync(notification); | ||
} | ||
else | ||
{ | ||
await SendEmailAsync(request.UserId, request.TemplateCode, request.Option); | ||
} | ||
var notificationResponse = new NotificationResponse | ||
{ | ||
Content = content, | ||
StudentUserid = notification.UserId, | ||
UserId = request.UserId, | ||
WithEmail = request.WithEmail | ||
}; | ||
|
||
return notificationResponse; | ||
} | ||
|
||
public override async Task getListNotifications(ListNotificationRequest request, IServerStreamWriter<NotificationResponse> responseStream, ServerCallContext context) | ||
{ | ||
var notifications = await _notificationRepository.GetNotifications(request.StudentUserid, request.Seen); | ||
var notifications = await _notificationRepository.GetNotifications(request.UserId, request.Seen); | ||
|
||
foreach (var notification in notifications) | ||
{ | ||
var notificationResponse = new NotificationResponse | ||
{ | ||
Content = notification.Content, | ||
StudentUserid = notification.UserId, | ||
UserId = notification.UserId, | ||
}; | ||
|
||
await responseStream.WriteAsync(notificationResponse); | ||
} | ||
} | ||
} | ||
} | ||
|