예제 #1
0
        public async Task <AddReportSubscriberResponse> Handle(AddReportSubscriberRequest request,
                                                               CancellationToken cancellationToken)
        {
            var report =
                await reportValidationHub.ValidateAndReturnReport(request.ReportId, ReportPermission.AddComment)
                ?? throw new NoPermissionsException(ErrorMessages.NotAllowedMessage);

            var addedReportSubscriber = await reportSubscriberService.AddSubscriber(report, request.UserId) ??
                                        throw new CrudException("Adding report subscriber failed");

            if (ReportStatusTypeSmartEnum.FromValue(report.Status)
                .ShouldBeOpened(report, httpContextReader.CurrentUserId))
            {
                await reportManager.ChangeStatus(ReportStatusType.Opened, report);
            }

            var notification = await notifier.Push(
                $"You were added to subscribers in report: {report.Subject}",
                request.UserId);

            await hubManager.Invoke(SignalrActions.NOTIFICATION_RECEIVED, request.UserId,
                                    mapper.Map <NotificationDto>(notification));

            return((AddReportSubscriberResponse) new AddReportSubscriberResponse
            {
                ReportSubscriber = mapper.Map <ReportSubscriberDto>(addedReportSubscriber)
            }.LogInformation(
                       $"Assignee #{httpContextReader.CurrentUserId} has added subscriber #{request.UserId} in report #{report.Id}"));
        }
        public async Task <AddReportCommentResponse> Handle(AddReportCommentRequest request,
                                                            CancellationToken cancellationToken)
        {
            var report =
                await reportValidationHub.ValidateAndReturnReport(request.ReportId, ReportPermission.AddComment)
                ?? throw new NoPermissionsException(ErrorMessages.NotAllowedMessage);

            var addedReportComment = await reportCommentService.AddComment(request with {
                IsPrivate = report.Private
            })
                                     ?? throw new CrudException("Adding report comment failed");

            if (ReportStatusTypeSmartEnum.FromValue(report.Status)
                .ShouldBeOpened(report, addedReportComment.UserId))
            {
                await reportManager.ChangeStatus(ReportStatusType.Opened, report);
            }

            var membersGroup = ReportManagerUtils.GenerateReportMembersGroup(report, addedReportComment.UserId);

            var notifications = await notifier.PushToGroup(
                $"Someone responded to your report: {report.Subject}", membersGroup);

            foreach (var memberId in membersGroup)
            {
                await hubManager.Invoke(SignalrActions.NOTIFICATION_RECEIVED, memberId,
                                        mapper.Map <IEnumerable <NotificationDto> >(notifications));
            }

            return((AddReportCommentResponse) new AddReportCommentResponse
            {
                ReportComment = mapper.Map <ReportCommentDto>(addedReportComment)
            }
                   .LogInformation(
                       $"Member #{addedReportComment.UserId} of report #{report.Id} added comment #{addedReportComment.Id}"));
        }