public async Task <Result> Handle(NotifyExpenseRemovedCommand request, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Message path: {path}", _operationContext.GetDescription());

        var @event    = request.ExpensesRemovedEvent;
        var groupName = $"{TenantGroupPrefix}-{_identityContext.TenantId}";

        var notification = new GeneralNotification
        {
            Type        = NotificationType.Success,
            Message     = $"Expense was successfully removed from {@event.CampaignName} campaign.",
            MessageType = NotificationMessageTypes.ExpensesRemoved,
            Data        = new
            {
                Id           = @event.ExpenseId,
                Total        = @event.Total,
                AddedBy      = @event.AddedBy,
                AddedAt      = @event.AddedAt,
                CampaignName = @event.CampaignName,
            }
        };

        _logger.LogInformationIfEnabled("Sending notification to {GroupName}", groupName);

        await _notificationService.SendMessageToGroupAsync(
            notification : notification,
            @group : groupName,
            cancellationToken : cancellationToken);

        _logger.LogInformationIfEnabled("Notification to {GroupName} has been successfully sent", groupName);

        return(Success.Empty);
    }
예제 #2
0
    public async Task Handle_Should_Send_Notification_With_CorrectData()
    {
        // Arrange
        var tenantId = _fixture.Fixture.Create <long>();

        Mock.Get(_fixture.IdentityContext)
        .Setup(s => s.TenantId)
        .Returns(tenantId);

        GeneralNotification cachedNotification = null !;
        var cachedGroup = string.Empty;

        Mock.Get(_fixture.NotificationService)
        .Setup(s => s.SendMessageToGroupAsync(
                   It.IsAny <INotificationMessage>(),
                   It.IsAny <string>(),
                   CancellationToken.None))
        .Callback <INotificationMessage, string, CancellationToken>(((message, group, _) =>
        {
            cachedGroup = group;
            cachedNotification = (GeneralNotification)message;
        }))
        .Returns(Task.CompletedTask);

        var expectedGroupName = $"{TenantGroupPrefix}-{tenantId}";
        var expenseAddedEvent = _fixture.Fixture.Create <ExpensesAddedEvent>();

        // Act
        await _fixture.Handler.Handle(
            new NotifyExpenseAddedCommand(expenseAddedEvent),
            CancellationToken.None);

        // Assert
        cachedGroup.Should().Be(expectedGroupName);

        cachedNotification
        .Type
        .Should()
        .Be(NotificationType.Success);

        cachedNotification
        .MessageType
        .Should()
        .Be(NotificationMessageTypes.ExpensesAdded);
    }
    public async Task <Result <bool> > Handle(NotifyExpenseAddedCommand request, CancellationToken cancellationToken)
    {
        var @event = request.ExpensesAddedEvent;

        // await _eventsPublisher.Publish(
        //     new ExpensesRemovedEvent(
        //         @event.TenantId,
        //         @event.ExpenseId,
        //         @event.Total,
        //         @event.AddedBy,
        //         @event.AddedAt,
        //         @event.CampaignName),
        //     cancellationToken);
        //
        // return new Success<bool>(true);

        var groupName = $"{TenantGroupPrefix}-{_identityContext.TenantId}";

        var notification = new GeneralNotification
        {
            Type        = NotificationType.Success,
            Message     = $"Expense was successfully added to {@event.CampaignName} campaign.",
            MessageType = NotificationMessageTypes.ExpensesAdded,
            Data        = new
            {
                Id           = @event.ExpenseId,
                Total        = @event.Total,
                AddedBy      = @event.AddedBy,
                AddedAt      = @event.AddedAt,
                CampaignName = @event.CampaignName,
            }
        };

        _logger.LogInformationIfEnabled("Sending notification to {GroupName}", groupName);

        await _notificationService.SendMessageToGroupAsync(
            notification : notification,
            @group : groupName,
            cancellationToken : cancellationToken);

        _logger.LogInformationIfEnabled("Notification to {GroupName} has been successfully sent", groupName);

        return(new Result <bool>());
    }