< Summary

Information
Class: Syki.Back.Features.Academic.CreateNotification.CreateNotificationService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Academic/CreateNotification/CreateNotificationService.cs
Tag: 22_11348620282
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 43
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
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%
Create()100%1010100%
CreateNotificationFor()100%11100%

File(s)

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

#LineLine coverage
 1namespace Syki.Back.Features.Academic.CreateNotification;
 2
 123public class CreateNotificationService(SykiDbContext ctx) : IAcademicService
 4{
 5    public async Task<NotificationOut> Create(Guid institutionId, CreateNotificationIn data)
 6    {
 127        var notification = new Notification(institutionId, data.Title, data.Description, data.TargetUsers, data.Timeless
 8
 129        if (data.TargetUsers is UsersGroup.All or UsersGroup.Teachers)
 10        {
 711            await CreateNotificationFor(institutionId, notification.Id, UserRole.Teacher);
 12        }
 1213        if (data.TargetUsers is UsersGroup.All or UsersGroup.Students)
 14        {
 815            await CreateNotificationFor(institutionId, notification.Id, UserRole.Student);
 16        }
 17
 1218        ctx.Add(notification);
 1219        await ctx.SaveChangesAsync();
 20
 1221        return notification.ToOut();
 1222    }
 23
 24    private async Task CreateNotificationFor(Guid institutionId, Guid notificationId, UserRole userRole)
 25    {
 1526        FormattableString sql = $@"
 1527            SELECT
 1528                u.id
 1529            FROM
 1530                syki.users u
 1531            INNER JOIN
 1532                syki.roles r ON r.name = {userRole.ToString()}
 1533            INNER JOIN
 1534                syki.user_roles ur ON ur.user_id = u.id AND ur.role_id = r.id
 1535            WHERE
 1536                u.institution_id = {institutionId}
 1537        ";
 38
 1539        var usersIds = await ctx.Database.SqlQuery<Guid>(sql).ToListAsync();
 40
 2441        usersIds.ForEach(userId => ctx.UserNotifications.Add(new UserNotification(userId, notificationId)));
 1542    }
 43}