| | 1 | | using Syki.Back.Features.Academic.CreateNotification; |
| | 2 | |
|
| | 3 | | namespace Syki.Back.Features.Cross.LinkOldNotifications; |
| | 4 | |
|
| | 5 | | [CommandDescription("Vincular notificações ao novo usuário")] |
| 3046 | 6 | | public record LinkOldNotificationsCommand(Guid InstitutionId, Guid UserId) : ICommand; |
| | 7 | |
|
| | 8 | | public class LinkOldNotificationsCommandHandler(SykiDbContext ctx) : ICommandHandler<LinkOldNotificationsCommand> |
| | 9 | | { |
| | 10 | | public async Task Handle(CommandId commandId, LinkOldNotificationsCommand command) |
| | 11 | | { |
| | 12 | | var isStudent = await ctx.Students.AnyAsync(s => s.Id == command.UserId); |
| | 13 | | var group = isStudent ? UsersGroup.Students : UsersGroup.Teachers; |
| | 14 | |
|
| | 15 | | var userNotifications = await ctx.UserNotifications |
| | 16 | | .Where(x => x.UserId == command.UserId) |
| | 17 | | .Select(x => x.NotificationId) |
| | 18 | | .ToListAsync(); |
| | 19 | |
|
| | 20 | | var notifications = await ctx.Notifications |
| | 21 | | .Where(x => x.InstitutionId == command.InstitutionId && x.Timeless && (x.Target == group || x.Target == User |
| | 22 | | .Select(x => new { x.Id }) |
| | 23 | | .ToListAsync(); |
| | 24 | |
|
| | 25 | | foreach (var notification in notifications) |
| | 26 | | { |
| | 27 | | ctx.Add(new UserNotification(command.UserId, notification.Id)); |
| | 28 | | } |
| | 29 | | } |
| | 30 | | } |