< Summary - Estud

Information
Class: Estud.Back.Features.Notifications.CreateNotification.CreateNotificationService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Notifications/CreateNotification/CreateNotificationService.cs
Tag: 114_29044117136
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 58
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor()100%11100%
.cctor()100%11100%
Create()100%1212100%
CreateNotificationFor()100%11100%

File(s)

/home/runner/work/syki/syki/Back/Features/Notifications/CreateNotification/CreateNotificationService.cs

#LineLine coverage
 1using Estud.Back.Domain.Notifications;
 2
 3namespace Estud.Back.Features.Notifications.CreateNotification;
 4
 225public class CreateNotificationService(EstudDbContext ctx) : IEstudService
 6{
 7    private class Validator : AbstractValidator<CreateNotificationIn>
 8    {
 29        public Validator()
 10        {
 211            RuleFor(x => x.Title).NotEmpty().WithError(InvalidNotificationTitle.I);
 212            RuleFor(x => x.Description).NotEmpty().WithError(InvalidNotificationDescription.I);
 213        }
 14    }
 215    private static readonly Validator V = new();
 16
 17    public async Task<OneOf<CreateNotificationOut, EstudError>> Create(CreateNotificationIn data)
 18    {
 2619        if (V.Run(data, out var error)) return error;
 20
 1821        var institutionId = ctx.RequestUser.InstitutionId;
 22
 1823        var notification = new Notification(institutionId, NotificationType.Custom, data.Title, data.Description);
 24
 1825        if (data.TargetUsers is UsersGroup.All or UsersGroup.Teachers)
 26        {
 1627            await CreateNotificationFor(institutionId, notification, UserType.Teacher);
 28        }
 1829        if (data.TargetUsers is UsersGroup.All or UsersGroup.Students)
 30        {
 431            await CreateNotificationFor(institutionId, notification, UserType.Student);
 32        }
 33
 1834        await ctx.SaveChangesAsync(notification);
 35
 1836        return new CreateNotificationOut { Id = notification.Id };
 2237    }
 38
 39    private async Task CreateNotificationFor(int institutionId, Notification notification, UserType userType)
 40    {
 2041        FormattableString sql = $@"
 2042            SELECT
 2043                u.id
 2044            FROM
 2045                estud.users u
 2046            INNER JOIN
 2047                estud.roles r ON r.base_type = {userType}
 2048            INNER JOIN
 2049                estud.user_roles ur ON ur.user_id = u.id AND ur.role_id = r.id
 2050            WHERE
 2051                u.institution_id = {institutionId}
 2052        ";
 53
 2054        var usersIds = await ctx.Database.SqlQuery<int>(sql).ToListAsync();
 55
 4056        usersIds.ForEach(userId => ctx.UserNotifications.Add(new UserNotification(userId, notification)));
 2057    }
 58}