Skip to content

Commit

Permalink
PS-148 notification email (#41)
Browse files Browse the repository at this point in the history
* first draft sub

* Fix notification

* Update core/Controllers/SubscriptionController.cs

Co-authored-by: Camille Brulotte <[email protected]>

* Update core/Controllers/SubscriptionController.cs

Co-authored-by: Camille Brulotte <[email protected]>

* Update core/Data/Entities/Notification.cs

Co-authored-by: Camille Brulotte <[email protected]>

* Update core/Misc/SchedulerSetup.cs

Co-authored-by: Camille Brulotte <[email protected]>

* Update core/Services/NotificationService.cs

Co-authored-by: Camille Brulotte <[email protected]>

* Update core/Services/NotificationService.cs

Co-authored-by: Camille Brulotte <[email protected]>

* Update core/Services/NotificationService.cs

* Update core/Services/SubscriptionService.cs

* Update core/Services/NotificationService.cs

* Update emails/Services/Abstractions/IEmailService.cs

* Update emails/Services/EmailService.cs

* Add loggers

* fix varying log

* change to use environement variable from conf

* Add tests

* Fix tests and add asserts

* fix security issue

---------

Co-authored-by: Camille Brulotte <[email protected]>
  • Loading branch information
MysticFragilist and camillebrulotte authored Apr 12, 2024
1 parent 36d68c2 commit 4ea095d
Show file tree
Hide file tree
Showing 35 changed files with 1,916 additions and 101 deletions.
4 changes: 4 additions & 0 deletions core/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion

#### Naming styles ####

# CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
dotnet_diagnostic.CA1862.severity = suggestion

[*.{cs,vb}]

# Naming rules
Expand Down
8 changes: 6 additions & 2 deletions core/Controllers/OrganizersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ namespace api.core.controllers;
[Authorize(Policy = AuthPolicies.IsModerator)]
[ApiController]
[Route("api/moderator/organizer")]
public class ModeratorUserController(IUserService userService, IAuthService authService, IEmailService emailService) : ControllerBase
public class ModeratorUserController(
IUserService userService,
IAuthService authService,
IEmailService emailService,
IConfiguration configuration) : ControllerBase
{
[AllowAnonymous]
[HttpGet("{organizerId}")]
Expand All @@ -41,7 +45,7 @@ public async Task<IActionResult> CreateOrganizer([FromBody] UserCreateDTO organi
var supabaseUser = authService.SignUp(organizer.Email, strongPassword);
Guid.TryParse(supabaseUser, out Guid userId);
var created = userService.AddOrganizer(userId, organizer);
var frontBaseUrl = Environment.GetEnvironmentVariable("FRONTEND_BASE_URL") ?? throw new Exception("FRONTEND_BASE_URL is not set");
var frontBaseUrl = configuration.GetValue<string>("FRONTEND_BASE_URL") ?? throw new Exception("FRONTEND_BASE_URL is not set");
await emailService.SendEmailAsync(
organizer.Email,
"Votre compte Hello!",
Expand Down
32 changes: 32 additions & 0 deletions core/Controllers/SubscriptionController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using api.core.Data;
using api.core.Data.requests;
using api.core.Services.Abstractions;

using Microsoft.AspNetCore.Mvc;

namespace api.core.Controllers;

[ApiController]
[Route("api/subscriptions")]
public class SubscriptionController(ISubscriptionService subscriptionService) : ControllerBase
{
[HttpPost]
public IActionResult Subscribe([FromBody] SubscribeRequestDTO request)
{
subscriptionService.Subscribe(request);
return Ok(new Response<object>
{
Data = "Subscribed successfully to this organizer posts."
});
}

[HttpDelete]
public IActionResult Unsubcribe([FromBody] UnsubscribeRequestDTO request)
{
subscriptionService.Unsubscribe(request);
return Ok(new Response<object>
{
Data = "Unsubscribed successfully from this organizer posts."
});
}
}
23 changes: 23 additions & 0 deletions core/Data/Entities/Notification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations.Schema;

using api.core.Data.Entities;

namespace api.core.data.entities;

[Table(nameof(Notification))]
public partial class Notification : BaseEntity
{
public Guid PublicationId { get; set; }

public Guid SubscriptionId { get; set; }

public bool IsSent { get; set; } = false;

[ForeignKey(nameof(PublicationId))]
[InverseProperty(nameof(Publication.Notifications))]
public virtual Publication Publication { get; set; } = null!;

[ForeignKey(nameof(SubscriptionId))]
[InverseProperty(nameof(Subscription.Notifications))]
public virtual Subscription Subscription { get; set; } = null!;
}
3 changes: 3 additions & 0 deletions core/Data/Entities/Organizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ public partial class Organizer : User
[ForeignKey("ActivityAreaId")]
[InverseProperty("Organizers")]
public virtual ActivityArea? ActivityArea { get; set; }

[InverseProperty(nameof(Subscription.Organizer))]
public virtual ICollection<Subscription> Subscriptions { get; set; } = new List<Subscription>();
}
3 changes: 3 additions & 0 deletions core/Data/Entities/Publication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ public partial class Publication
[ForeignKey("PublicationsId")]
[InverseProperty("Publications")]
public virtual ICollection<Tag> Tags { get; set; } = new List<Tag>();

[InverseProperty(nameof(Notification.Publication))]
public virtual ICollection<Notification> Notifications { get; set; } = new List<Notification>();
}
23 changes: 23 additions & 0 deletions core/Data/Entities/Subscription.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations.Schema;

using api.core.Data.Entities;


namespace api.core.data.entities;

[Table(nameof(Subscription))]
public partial class Subscription : BaseEntity
{
public string Email { get; set; } = null!;

public Guid OrganizerId { get; set; }

public string SubscriptionToken { get; set; } = null!;

[ForeignKey(nameof(OrganizerId))]
[InverseProperty(nameof(entities.Organizer.Subscriptions))]
public virtual Organizer Organizer { get; set; } = null!;

[InverseProperty(nameof(Notification.Subscription))]
public virtual ICollection<Notification> Notifications { get; set; } = new List<Notification>();
}
8 changes: 8 additions & 0 deletions core/Data/EventManagementContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public EventManagementContext(DbContextOptions<EventManagementContext> options)

public virtual DbSet<ActivityArea> ActivityAreas { get; set; }

public virtual DbSet<Subscription> Subscriptions { get; set; }

public virtual DbSet<Notification> Notifications { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Event>(entity =>
Expand Down Expand Up @@ -116,6 +120,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
});
});

modelBuilder.Entity<Subscription>()
.HasIndex(a => new { a.Email, a.OrganizerId })
.IsUnique();

OnModelCreatingPartial(modelBuilder);
}

Expand Down
15 changes: 15 additions & 0 deletions core/Data/Requests/SubscriptionRequestDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace api.core.Data.requests;

public class SubscribeRequestDTO
{
public required string Email { get; set; }

public required Guid OrganizerId { get; set; }
}


public class UnsubscribeRequestDTO
{
public required string SubscriptionToken { get; set; }

}
4 changes: 4 additions & 0 deletions core/Extensions/DependencyInjectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static IServiceCollection AddDependencyInjection(this IServiceCollection
services.AddTransient<IModeratorRepository, ModeratorRepository>();
services.AddTransient<IReportRepository, ReportRepository>();
services.AddTransient<IActivityAreaRepository, ActivityAreaRepository>();
services.AddTransient<ISubscriptionRepository, SubscriptionRepository>();
services.AddTransient<INotificationRepository, NotificationRepository>();

// Services
services.AddTransient<IUserService, UserService>();
Expand All @@ -40,6 +42,8 @@ public static IServiceCollection AddDependencyInjection(this IServiceCollection
services.AddTransient<IReportService, ReportService>();
services.AddTransient<IActivityAreaService, ActivityAreaService>();
services.AddTransient<IModeratorService, ModeratorService>();
services.AddTransient<ISubscriptionService, SubscriptionService>();
services.AddTransient<INotificationService, NotificationService>();

return services;
}
Expand Down
Loading

0 comments on commit 4ea095d

Please sign in to comment.