ubeac / ubeac-api

Source of uBeac .NET Core packages
3 stars 2 forks source link

Implement Sending Notification and Template Rendering #157

Open ilhesam opened 1 year ago

ilhesam commented 1 year ago

One of the needs of any software is sending notifications like email to the users. We want to make implementation of notification services easier for developers.

Use cases:

We have to handle two purposes in this phase:

uBeac.Core.Notification.Abstractions

We should have a class for the notifications:

public class Notification
{
    public NotificationReceptor Receptor { get; set; } 
    public string Subject { get; set; } // "Welcome to uBeac!"
    public string Body { get; set; } // "Hello Hesam, Your account has been created successfully!"
}

public class NotificationReceptor
{
    public string Receptor { get; set; } // "ilhesam@outlook.com"
}

public class EmailReceptor : NotificationReceptor
{
    public string Cc { get; set; }
    public string Bcc { get; set; }
}

And the notification provider interfaces:

public interface INotificationProvider
{
    Task SendAsync(Notification notification, CancellationToken cancellationToken = default);
}

We don't implement any providers currently. The end developer should implement the notification providers.

uBeac.Core.Notification.Templates.Abstractions

public class NotificationTemplate
{
    public string UniqueKey { get; set; } // "sign-up-email"
    public string Subject { get; set; } // "Welcome to {{SiteName}}"
    public string Body { get; set; } // "Hello {{Name}}, Your account has been created successfully!"
}

Also, we should create a repository and service with the CRUD methods for notification templates + template renderer interface:

public interface INotificationTemplateRepository // inherits from base repository
{
    // CRUD methods
}

public interface INotificationTemplateService // inherits from base repository
{
    // CRUD methods
}

public interface INotificationTemplateRenderer
{
    string Render(string template, object model);
}

And, we should implement the interfaces in the another packages:

uBeac.Core.Notification

Finally, We should have a notification service like this:

public interface INotificationService
{
    // Just sends the notification (base methods)
    Task SendAsync(Notification notification, CancellationToken cancellationToken = default);
    Task SendAsync(INotificationProvider provider, Notification notification, CancellationToken cancellationToken = default);

    // First renders the template and Then sends the notification (easy-to-use methods)
    Task SendAsync(NotificationTemplate template, object model, NotificationReceptor receptor, CancellationToken cancellationToken = default);
    Task SendAsync(string templateUid, object model, NotificationReceptor receptor, CancellationToken cancellationToken = default);
    Task SendAsync(INotificationProvider provider, NotificationTemplate template, object model, NotificationReceptor receptor, CancellationToken cancellationToken = default);
    Task SendAsync(INotificationProvider provider, string templateUid, object model, NotificationReceptor receptor, CancellationToken cancellationToken = default);
}

Registration

The developers should can register it easily! I think the following structure is good:

services.AddNotification(options =>
{
    options.AddProvider<SmtpEmailProvider>(); 
    options.AddProvider<MessageBirdProvider>();

    options.AddMustacheTemplateRenderer();
});
ilhesam commented 1 year ago

Update: I talked with @pournasserian and We changed the plan. First, we implement Template Engine without any dependent to Sending Notifications. Because, Template Rendering is a general discussion and does not lead only to Sending Notifications. Follow the issue: https://github.com/ubeac/ubeac-api/issues/158