Skip to content
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

Open
mohammadKarimi opened this issue Feb 20, 2024 · 4 comments
Open

Add BackgroundService For Link's Expiration Notificatioin #106

mohammadKarimi opened this issue Feb 20, 2024 · 4 comments

Comments

@mohammadKarimi
Copy link
Owner

No description provided.

@mohammadKarimi
Copy link
Owner Author

@MohamadReza-khani

@mohammadKarimi
Copy link
Owner Author

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 Type

First, 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 Expirations

Implement 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 Logic

In the scheduled task, for each link that is nearing expiration, publish a LinkExpirationNotification using the IMediator instance. This will be handled by a notification handler designed to send the actual notification to the user.

// 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 Notification

Create a handler for LinkExpirationNotification that sends an email or another form of notification to the user, informing them of the impending expiration of their link.

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 Link entity, such as ExpirationNotificationSent. Update the logic in your scheduled task to mark the link as notified after sending the notification.

public class Link
{
    // Existing properties

    public bool ExpirationNotificationSent { get; set; }
}

Conclusion

By 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 Link entity to track notification status.

@MohamadReza-khani
Copy link
Contributor

Hi @mohammadKarimi Thanks for the guidness🙌
Could you please clarify how we plan to send notifications for reminders? Are we considering options like email, SMS, or any other channels?

@mohammadKarimi
Copy link
Owner Author

Hi @mohammadKarimi Thanks for the guidness🙌 Could you please clarify how we plan to send notifications for reminders? Are we considering options like email, SMS, or any other channels?

for now, implement a decorator pattern to adapt your code with any type of channel.
your code does not know who is responsible to send a notification, your decorator select one of available channel an a candidate. each strategies must be empty for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants