Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Chronos.MainApi.Schedule.Messaging;
using Chronos.MainApi.Schedule.Services;
using Chronos.MainApi.Shared.Controllers.Utils;
using Chronos.MainApi.Shared.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -333,7 +334,8 @@ public async Task<IActionResult> TriggerBatchScheduling(Guid schedulingPeriodId)
var request = new SchedulePeriodRequest(
SchedulingPeriodId: schedulingPeriodId,
OrganizationId: organizationId,
Mode: SchedulingMode.Batch
Mode: SchedulingMode.Batch,
InitiatedByUserId: User.GetUserId()
);

// Publish to RabbitMQ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Chronos.Domain.Schedule;
using Chronos.Domain.Schedule.Messages;
using Chronos.MainApi.Schedule.Messaging;
using Chronos.MainApi.Shared.Extensions;
using Chronos.MainApi.Shared.ExternalMangement;
using Chronos.Shared.Exceptions;

Expand All @@ -16,9 +17,27 @@ public class ActivityConstraintService(
IManagementExternalService validationService,
IMessagePublisher messagePublisher,
IActivityRepository activityRepository,
ISubjectRepository subjectRepository
ISubjectRepository subjectRepository,
IHttpContextAccessor httpContextAccessor
) : IActivityConstraintService
{
private Guid? GetInitiatingUserId()
{
var user = httpContextAccessor.HttpContext?.User;
if (user?.Identity?.IsAuthenticated != true)
{
return null;
}

try
{
return user.GetUserId();
}
catch
{
return null;
}
}


public async Task<Guid> CreateActivityConstraintAsync(Guid organizationId, Guid activityId, string key, string value, int? weekNum = null)
Expand Down Expand Up @@ -53,7 +72,8 @@ await messagePublisher.PublishAsync(
Operation: ConstraintChangeOperation.Created,
ActivityId: constraint.ActivityId,
UserId: null,
Mode: SchedulingMode.Online
Mode: SchedulingMode.Online,
InitiatedByUserId: GetInitiatingUserId()
),
"request.online"
);
Expand Down Expand Up @@ -111,7 +131,8 @@ await messagePublisher.PublishAsync(
Operation: ConstraintChangeOperation.Updated,
ActivityId: constraint.ActivityId,
UserId: null,
Mode: SchedulingMode.Online
Mode: SchedulingMode.Online,
InitiatedByUserId: GetInitiatingUserId()
),
"request.online"
);
Expand All @@ -137,7 +158,8 @@ await messagePublisher.PublishAsync(
Operation: ConstraintChangeOperation.Deleted,
ActivityId: constraint.ActivityId,
UserId: null,
Mode: SchedulingMode.Online
Mode: SchedulingMode.Online,
InitiatedByUserId: GetInitiatingUserId()
),
"request.online"
);
Expand Down
30 changes: 26 additions & 4 deletions src/Chronos.MainApi/Schedule/Services/UserConstraintService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Chronos.Domain.Schedule;
using Chronos.Domain.Schedule.Messages;
using Chronos.MainApi.Schedule.Messaging;
using Chronos.MainApi.Shared.Extensions;
using Chronos.MainApi.Shared.ExternalMangement;
using Chronos.Shared.Exceptions;

Expand All @@ -11,8 +12,26 @@ public class UserConstraintService(
IManagementExternalService validationService,
ISchedulingPeriodService schedulingPeriodService,
ILogger<UserConstraintService> logger,
IMessagePublisher messagePublisher) : IUserConstraintService
IMessagePublisher messagePublisher,
IHttpContextAccessor httpContextAccessor) : IUserConstraintService
{
private Guid? GetInitiatingUserId()
{
var user = httpContextAccessor.HttpContext?.User;
if (user?.Identity?.IsAuthenticated != true)
{
return null;
}

try
{
return user.GetUserId();
}
catch
{
return null;
}
}
public async Task<Guid> CreateUserConstraintAsync(Guid organizationId, Guid userId, Guid schedulingPeriodId, string key, string value, int? weekNum = null)
{
await validationService.ValidateOrganizationAsync(organizationId);
Expand All @@ -38,7 +57,8 @@ await messagePublisher.PublishAsync(
Operation: ConstraintChangeOperation.Created,
ActivityId: null,
UserId: userId,
Mode: SchedulingMode.Online
Mode: SchedulingMode.Online,
InitiatedByUserId: GetInitiatingUserId()
),
"request.online"
);
Expand Down Expand Up @@ -113,7 +133,8 @@ await messagePublisher.PublishAsync(
Operation: ConstraintChangeOperation.Updated,
ActivityId: null,
UserId: constraint.UserId,
Mode: SchedulingMode.Online
Mode: SchedulingMode.Online,
InitiatedByUserId: GetInitiatingUserId()
),
"request.online"
);
Expand All @@ -136,7 +157,8 @@ await messagePublisher.PublishAsync(
Operation: ConstraintChangeOperation.Deleted,
ActivityId: null,
UserId: constraint.UserId,
Mode: SchedulingMode.Online
Mode: SchedulingMode.Online,
InitiatedByUserId: GetInitiatingUserId()
),
"request.online"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Chronos.MainApi.Schedule.Services;
using Chronos.MainApi.Shared.ExternalMangement;
using Chronos.Shared.Exceptions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
Expand Down Expand Up @@ -34,13 +35,17 @@ public void SetUp()
_messagePublisher = Substitute.For<IMessagePublisher>();
_logger = Substitute.For<ILogger<ActivityConstraintService>>();

var httpContextAccessor = Substitute.For<IHttpContextAccessor>();
httpContextAccessor.HttpContext.Returns((HttpContext?)null);

_service = new ActivityConstraintService(
_activityConstraintRepository,
_logger,
_validationService,
_messagePublisher,
_activityRepository,
_subjectRepository);
_subjectRepository,
httpContextAccessor);
}

#region CreateActivityConstraintAsync Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Chronos.MainApi.Schedule.Services;
using Chronos.MainApi.Shared.ExternalMangement;
using Chronos.Shared.Exceptions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
Expand All @@ -30,12 +31,16 @@ public void SetUp()
_messagePublisher = Substitute.For<IMessagePublisher>();
_logger = Substitute.For<ILogger<UserConstraintService>>();

var httpContextAccessor = Substitute.For<IHttpContextAccessor>();
httpContextAccessor.HttpContext.Returns((HttpContext?)null);

_service = new UserConstraintService(
_userConstraintRepository,
_validationService,
_schedulingPeriodService,
_logger,
_messagePublisher);
_messagePublisher,
httpContextAccessor);
}

#region CreateUserConstraintAsync Tests
Expand Down
Loading