-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BackgroundService For Link's Expiration Notificatioin #106
Comments
To implement a feature for notifying users about their link's expiration, you can follow these steps, leveraging the existing structure of the SwiftLink application: 1. Define a New Notification TypeFirst, define a new notification type for link expiration. This notification will be used to inform users when their links are nearing expiration or have expired. public record LinkExpirationNotification : INotification
{
public int LinkId { get; init; }
public string SubscriberEmail { get; init; }
public DateTime ExpirationDate { get; init; }
} 2. Create a Scheduled Task for Checking Link ExpirationsImplement a scheduled task that runs periodically to check for links that are nearing expiration. This task will query the database for links that are set to expire within a certain threshold (e.g., within the next 24 hours) and have not yet been notified. You can use a background service or a scheduled job (e.g., with Hangfire or Quartz.NET) for this purpose. public class LinkExpirationChecker : IHostedService
{
private readonly IApplicationDbContext _dbContext;
private readonly IMediator _mediator;
public LinkExpirationChecker(IApplicationDbContext dbContext, IMediator mediator)
{
_dbContext = dbContext;
_mediator = mediator;
}
// Implement the logic to start the background task
// and check for expiring links at scheduled intervals
} 3. Implement Notification LogicIn the scheduled task, for each link that is nearing expiration, publish a // Inside the scheduled task
var expiringLinks = // Query the database for links nearing expiration
foreach (var link in expiringLinks)
{
await _mediator.Publish(new LinkExpirationNotification
{
LinkId = link.Id,
SubscriberEmail = link.Subscriber.Email,
ExpirationDate = link.ExpirationDate
});
} 4. Handle the Expiration NotificationCreate a handler for public class LinkExpirationNotificationHandler : INotificationHandler<LinkExpirationNotification>
{
// Inject any services needed to send notifications (e.g., an email service)
public async Task Handle(LinkExpirationNotification notification, CancellationToken cancellationToken)
{
// Implement the logic to notify the user, e.g., send an email
}
} 5. Update the Link Entity (Optional)If you want to track whether a notification has been sent for an expiring link, you might need to add a new property to the public class Link
{
// Existing properties
public bool ExpirationNotificationSent { get; set; }
} ConclusionBy following these steps, you can implement a feature to notify users about their links' expiration. This involves defining a new notification type, creating a scheduled task to check for expiring links, implementing the logic to send notifications, and optionally updating the |
Hi @mohammadKarimi Thanks for the guidness🙌 |
for now, implement a decorator pattern to adapt your code with any type of channel. |
No description provided.
The text was updated successfully, but these errors were encountered: