Notify matching users when a new shift/job is posted (in-app notifications)
- Notification entity + NotificationService: on publish, notify users whose saved prefs match the listing (role/city/+shift type); users with no preference aren't spammed - Wired into PostShift, PostJob, and Admin Review publish - 🔔 bell with unread count in the header (@inject) + /Me/Notifications page (mark-all-read on open) - Reliable in-app delivery (works in Iran without FCM); Web Push can ride the same records later - Verified: employee pref → employer posts matching shift → employee bell=۱ + 'شیفت جدید: پزشک عمومی' Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using JobsMedical.Web.Data;
|
||||
using JobsMedical.Web.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace JobsMedical.Web.Services;
|
||||
|
||||
/// <summary>
|
||||
/// In-app notifications. When a new shift/job is published, notifies users whose saved
|
||||
/// preferences match it (role / city / — for shifts — shift type). Users with no preference set
|
||||
/// are NOT notified (avoids spamming everyone). One notification per matching user.
|
||||
/// </summary>
|
||||
public class NotificationService
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly ILogger<NotificationService> _log;
|
||||
|
||||
public NotificationService(AppDbContext db, ILogger<NotificationService> log)
|
||||
{
|
||||
_db = db;
|
||||
_log = log;
|
||||
}
|
||||
|
||||
public Task<int> UnreadCountAsync(int userId) =>
|
||||
_db.Notifications.CountAsync(n => n.UserId == userId && !n.IsRead);
|
||||
|
||||
public Task<List<Notification>> ListAsync(int userId, int take = 50) =>
|
||||
_db.Notifications.Where(n => n.UserId == userId)
|
||||
.OrderByDescending(n => n.CreatedAt).Take(take).ToListAsync();
|
||||
|
||||
public async Task MarkAllReadAsync(int userId) =>
|
||||
await _db.Notifications.Where(n => n.UserId == userId && !n.IsRead)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(n => n.IsRead, true));
|
||||
|
||||
public async Task NotifyNewShiftAsync(int shiftId)
|
||||
{
|
||||
var s = await _db.Shifts.Include(x => x.Facility).Include(x => x.Role)
|
||||
.FirstOrDefaultAsync(x => x.Id == shiftId);
|
||||
if (s is null) return;
|
||||
|
||||
var users = await MatchingUserIdsAsync(s.RoleId, s.Facility.CityId, s.ShiftType);
|
||||
var title = $"شیفت جدید: {s.Role.Name}";
|
||||
var body = $"{s.Facility.Name} — {JalaliDate.WeekDayName(s.Date)} {JalaliDate.Time(s.StartTime)}";
|
||||
await AddAsync(users, title, body, $"/Shifts/Details/{s.Id}");
|
||||
}
|
||||
|
||||
public async Task NotifyNewJobAsync(int jobId)
|
||||
{
|
||||
var j = await _db.JobOpenings.Include(x => x.Facility).Include(x => x.Role)
|
||||
.FirstOrDefaultAsync(x => x.Id == jobId);
|
||||
if (j is null) return;
|
||||
|
||||
var users = await MatchingUserIdsAsync(j.RoleId, j.Facility.CityId, null);
|
||||
await AddAsync(users, $"استخدام جدید: {j.Title}", j.Facility.Name, $"/Jobs/Details/{j.Id}");
|
||||
}
|
||||
|
||||
/// <summary>Users with a non-empty preference that matches the listing (via their visitor link).</summary>
|
||||
private async Task<List<int>> MatchingUserIdsAsync(int roleId, int cityId, ShiftType? shiftType)
|
||||
{
|
||||
var q = from up in _db.UserPreferences
|
||||
join v in _db.Visitors on up.VisitorId equals v.Id
|
||||
where v.UserId != null
|
||||
&& (up.RoleId != null || up.CityId != null) // must have a real preference
|
||||
&& (up.RoleId == null || up.RoleId == roleId)
|
||||
&& (up.CityId == null || up.CityId == cityId)
|
||||
&& (shiftType == null || up.PreferredShiftType == null || up.PreferredShiftType == shiftType)
|
||||
select v.UserId!.Value;
|
||||
return await q.Distinct().ToListAsync();
|
||||
}
|
||||
|
||||
private async Task AddAsync(List<int> userIds, string title, string? body, string url)
|
||||
{
|
||||
if (userIds.Count == 0) return;
|
||||
foreach (var uid in userIds)
|
||||
_db.Notifications.Add(new Notification { UserId = uid, Title = title, Body = body, Url = url });
|
||||
await _db.SaveChangesAsync();
|
||||
_log.LogInformation("Notified {Count} users: {Title}", userIds.Count, title);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user