Example #1
0
        public async Task <bool> SendReportMessage(ReportMessage reportMessage, int reportId, int currentUserId)
        {
            if (reportMessage == null)
            {
                throw new EntityNotFoundException();
            }

            var report = await database.SupportRepository.Get <Report>(reportId);

            bool isAdmin = await rolesService.IsPermitted(AdminRoles, currentUserId);

            if (report.IsClosed)
            {
                return(false);
            }

            if (currentUserId != report.UserId && !isAdmin)
            {
                return(false);
            }

            if (isAdmin)
            {
                reportMessage.SetReply(isReply: true);
            }

            if (!isAdmin && !report.ReportMessages.TakeLast(1).Any(rm => rm.IsReply))
            {
                return(false);
            }

            report.UpdateReport();
            reportMessage.SetDate();

            var user = await database.UserRepository.Get <User>(report.UserId);

            user.ReportMessages.Add(reportMessage);
            report.ReportMessages.Add(reportMessage);

            if (!await database.Complete())
            {
                return(false);
            }

            if (isAdmin)
            {
                await notificationSystem.PushNotification(report.UserId, StaticExpressions.ReplySent(reportId));
            }
            else
            {
                if (report.AssignedTo == null)
                {
                    await notificationSystem.PushNotificationToUsersByRoles(StaticExpressions.ReportMessageSent(reportId, user.UserName), AdminRoles);
                }
                else
                {
                    await notificationSystem.PushNotification(report.AssignedTo.GetValueOrDefault(), StaticExpressions.ReportMessageSent(reportId, user.UserName));
                }
            }

            return(true);
        }