public void Can_Create()
        {
            //Arrange
            var relatedEntityType = A <RelatedEntityType>();
            var relatedEntityId   = A <int>();
            var now          = DateTime.Now;
            var notification = new UserNotification(A <string>(), A <string>(), NotificationType.Advice, A <int>(), A <int>(), now);
            var notificationWithRelatedEntity = ExpectRelatedEntity(relatedEntityType, relatedEntityId, notification);

            var transaction = new Mock <IDatabaseTransaction>();

            _transactionManager.Setup(x => x.Begin(IsolationLevel.ReadCommitted)).Returns(transaction.Object);
            _userNotificationRepository.Setup(x => x.Add(It.IsAny <UserNotification>())).Returns(notificationWithRelatedEntity);
            _operationClock.Setup(x => x.Now).Returns(now);

            //Act
            var result = _sut.AddUserNotification(notificationWithRelatedEntity.OrganizationId, notificationWithRelatedEntity.NotificationRecipientId, notificationWithRelatedEntity.Name, notificationWithRelatedEntity.NotificationMessage, relatedEntityId, relatedEntityType, notification.NotificationType);

            //Assert
            Assert.True(result.Ok);
            transaction.Verify(x => x.Commit());
        }
Beispiel #2
0
        private bool DispatchEmails(Advice advice)
        {
            var message = new MailMessage
            {
                Body         = advice.Body,
                Subject      = (advice.Subject).Replace('\r', ' ').Replace('\n', ' '),
                BodyEncoding = Encoding.UTF8,
                IsBodyHtml   = true
            };

            foreach (var r in advice.Reciepients)
            {
                switch (r.RecieverType)
                {
                case RecieverType.RECIEVER:
                    switch (r.RecpientType)
                    {
                    case RecieverType.USER:
                        AddRecipientByName(r, message.To);
                        break;

                    case RecieverType.ROLE:
                        AddRecipientByRole(advice, r, message.To);
                        break;
                    }
                    break;

                case RecieverType.CC:
                    switch (r.RecpientType)
                    {
                    case RecieverType.USER:
                        AddRecipientByName(r, message.CC);
                        break;

                    case RecieverType.ROLE:
                        AddRecipientByRole(advice, r, message.CC);
                        break;
                    }
                    break;
                }
            }

            if (message.To.Any() || message.CC.Any())
            {
                MailClient.Send(message);
                advice.SentDate = OperationClock.Now;
                return(true);
            }
            else
            {
                var organizationIdOfRelatedEntityId = GetRelatedEntityOrganizationId(advice);
                if (organizationIdOfRelatedEntityId.IsNone)
                {
                    Logger?.Error($"Advis doesn't have valid/correct related entity (RelationId and Type mismatch). Advice Id: {advice.Id}, Advice RelationId: {advice.RelationId}, Advice RelatedEntityType: {advice.Type}");
                }
                else
                {
                    if (advice.HasInvalidState())
                    {
                        Logger?.Error($"Advis is missing critical function information. Advice Id: {advice.Id}, Advice RelationId: {advice.RelationId}, Advice RelatedEntityType: {advice.Type}, Advice ownerId: {advice.ObjectOwnerId}");
                    }
                    else
                    {
                        var nameForNotification = advice.Name ?? "Ikke navngivet";
                        UserNotificationService.AddUserNotification(organizationIdOfRelatedEntityId.Value, advice.ObjectOwnerId.Value, nameForNotification, "Advis kunne ikke sendes da der ikke blev fundet nogen gyldig modtager. Dette kan skyldes at der ikke er nogen bruger tilknyttet den/de valgte rolle(r).", advice.RelationId.Value, advice.Type.Value, NotificationType.Advice);
                    }
                }
                return(false);
            }
        }