-
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 #2 from COMP-1640-GRE/cuongphgch210011
Cuongphgch210011 - update job + fix bug -infinity
- Loading branch information
Showing
10 changed files
with
185 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace DotnetGRPC.Model | ||
{ | ||
[Table("contribution")] | ||
public class Contribution | ||
{ | ||
[Key] | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
[Column("id")] | ||
public long Id { get; set; } | ||
|
||
[Column("created_at")] | ||
public DateTime CreatedAt { get; set; } | ||
|
||
[Column("updated_at")] | ||
public DateTime UpdatedAt { get; set; } | ||
|
||
[Column("db_author_id")] | ||
public long DbAuthorId { get; set; } | ||
|
||
[Column("title")] | ||
public string Title { get; set; } | ||
|
||
[Column("description")] | ||
public string? Description { get; set; } | ||
|
||
[Column("evaluation")] | ||
public string? Evaluation { get; set; } | ||
|
||
[Column("view_count")] | ||
public long ViewCount { get; set; } | ||
|
||
[Column("semester_id")] | ||
public long SemesterId { get; set; } | ||
|
||
[Column("selected")] | ||
public bool? Selected { get; set; } | ||
|
||
[Column("is_anonymous")] | ||
public bool? IsAnonymous { get; set; } | ||
|
||
[Column("status")] | ||
public string Status { get; set; } | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using DotnetGRPC.Model; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class ContributionRepository | ||
{ | ||
private readonly AppDbContext _context; | ||
|
||
public ContributionRepository(AppDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task<List<Contribution>> FindPendingContributions14DaysAgo() | ||
{ | ||
var fourteenDaysAgo = DateTime.UtcNow.AddDays(-14); | ||
|
||
return await _context.Contribution | ||
.Where(c => c.UpdatedAt < fourteenDaysAgo && c.Status == "pending") | ||
.ToListAsync(); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using DotnetGRPC.Model; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public class FacultyRepository | ||
{ | ||
private readonly AppDbContext _context; | ||
|
||
public FacultyRepository(AppDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task<Faculty> FindByIdAsync(long id) | ||
{ | ||
return await _context.Faculty.FindAsync(id); | ||
Check warning on line 15 in DotnetGRPC/Repository/FacultyRepository.cs GitHub Actions / build
|
||
} | ||
} |
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 |
---|---|---|
|
@@ -14,30 +14,34 @@ public class NotificationService : Notification.NotificationBase | |
private readonly UserRepository _userRepository; | ||
private readonly TemplateRepository _templateRepository; | ||
private readonly NotificationRepository _notificationRepository; | ||
private readonly ContributionRepository _contributionRepository; | ||
private readonly FacultyRepository _facultyRepository; | ||
|
||
|
||
public NotificationService(AppDbContext dbContext, IOptions<EmailSettings> emailSettings, UserRepository userRepository, TemplateRepository templateRepository, NotificationRepository notificationRepository) | ||
public NotificationService(AppDbContext dbContext, IOptions<EmailSettings> emailSettings, UserRepository userRepository, TemplateRepository templateRepository, NotificationRepository notificationRepository, ContributionRepository contributionRepository, FacultyRepository facultyRepository) | ||
{ | ||
_dbContext = dbContext; | ||
_emailSettings = emailSettings.Value; | ||
_userRepository = userRepository; | ||
_templateRepository = templateRepository; | ||
_notificationRepository = notificationRepository; | ||
_contributionRepository = contributionRepository; | ||
_facultyRepository = facultyRepository; | ||
} | ||
|
||
public async Task SendEmailAsync(long studentUserId, string templateCode, string option) | ||
public async Task SendEmailAsync(long userId, string templateCode, string option) | ||
{ | ||
if (studentUserId == 0) | ||
if (userId == 0) | ||
{ | ||
throw new ArgumentException("Student is null"); | ||
} | ||
|
||
var student = await _userRepository.FindByIdAsync(studentUserId) ?? throw new ArgumentException("Student not found"); | ||
var student = await _userRepository.FindByIdAsync(userId) ?? 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("[email protected]"); | ||
mailMessage.From = new MailAddress(_emailSettings.Username); | ||
mailMessage.Subject = template.TemplateName; | ||
mailMessage.Body = template.TemplateContent | ||
.Replace("[Student Name]", $"{student.FirstName} {student.LastName}") | ||
|
@@ -106,6 +110,59 @@ public override async Task getListNotifications(ListNotificationRequest request, | |
await responseStream.WriteAsync(notificationResponse); | ||
} | ||
} | ||
|
||
public async Task SendNotifyPendingContribution() | ||
{ | ||
Console.WriteLine("Sending notification to faculty coordinators for pending contributions"); | ||
var contributions = await _contributionRepository.FindPendingContributions14DaysAgo(); | ||
if (contributions.Count == 0) | ||
{ | ||
Console.WriteLine("No pending contributions"); | ||
} | ||
foreach (var contribution in contributions) | ||
{ | ||
var templateNotification = await _templateRepository.FindByTemplateCodeAsync("fac01_noti"); | ||
var templateEmail = await _templateRepository.FindByTemplateCodeAsync("fac01_email"); | ||
var student = await _userRepository.FindByIdAsync(contribution.DbAuthorId); | ||
if (student.FacultyId == null) | ||
{ | ||
throw new Exception("Student has no faculty"); | ||
} | ||
var faculty = await _facultyRepository.FindByIdAsync(student.FacultyId.Value); | ||
var users = await _userRepository.FindFacultyCoordinators(student.FacultyId.Value); | ||
foreach (var user in users) | ||
{ | ||
var notification = new Model.Notification | ||
{ | ||
Content = templateNotification.TemplateContent, | ||
Seen = false, | ||
UserId = user.Id, | ||
WithEmail = true | ||
}; | ||
await _notificationRepository.SaveAsync(notification); | ||
|
||
using (var mailMessage = new MailMessage()) | ||
{ | ||
mailMessage.To.Add(new MailAddress(user.Email)); | ||
mailMessage.From = new MailAddress(_emailSettings.Username); | ||
mailMessage.Subject = templateEmail.TemplateName; | ||
mailMessage.Body = templateEmail.TemplateContent | ||
.Replace("[Student Name]", $"{student.FirstName} {student.LastName}") | ||
.Replace("[Title of Contribution]", contribution.Title) | ||
.Replace("[Date of Submission]", contribution.UpdatedAt.ToString("dd/MM/yyyy")) | ||
.Replace("[Faculty Name]", faculty.Name); | ||
mailMessage.IsBodyHtml = true; | ||
using (var smtpClient = new SmtpClient(_emailSettings.Host, _emailSettings.Port)) | ||
{ | ||
smtpClient.Credentials = new NetworkCredential(_emailSettings.Username, _emailSettings.Password); | ||
smtpClient.EnableSsl = _emailSettings.EnableSsl; | ||
await smtpClient.SendMailAsync(mailMessage); | ||
} | ||
} | ||
Console.WriteLine("Email sent to " + faculty.Name + " faculty coordinator"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
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