示例#1
0
        protected void OnNotificationCreated(QuiltContext ctx, Notification dbNotification)
        {
            var dbNotificationType = ctx.NotificationTypes.Find(dbNotification.NotificationTypeCode);

            var formatter = new NotificationEmailFormatter(dbNotificationType.Subject);

            var dbParticipant     = ctx.Participants.Where(r => r.ParticipantId == dbNotification.ParticipantId).Single();
            var participantUserId = ParseUserId.FromParticipantReference(dbParticipant.ParticipantReference);
            var dbAspNetUser      = ctx.AspNetUsers.Where(r => r.Id == participantUserId).Single();

            var dbEmailRequest = new EmailRequest()
            {
                EmailRequestStatusCode = EmailRequestStatusCodes.Posted,
                SenderEmail            = Constants.DoNotReplyEmail,
                SenderEmailName        = Constants.DoNotReplyEmailName,
                RecipientEmail         = dbAspNetUser.Email,
                RecipientEmailName     = dbAspNetUser.EmailName(),
                RecipientParticipant   = dbNotification.Participant,
                Subject                       = formatter.GetSubject(),
                BodyText                      = formatter.GetText(),
                BodyHtml                      = formatter.GetHtml(),
                BodyTypeCode                  = EmailBodyTypes.Notification,
                CreateDateTimeUtc             = Locale.GetUtcNow(),
                EmailRequestStatusDateTimeUtc = Locale.GetUtcNow(),
            };

            _ = ctx.EmailRequests.Add(dbEmailRequest);

            var dbNotificationEmailRequest = new NotificationEmailRequest()
            {
                Notification = dbNotification,
                EmailRequest = dbEmailRequest
            };

            _ = ctx.NotificationEmailRequests.Add(dbNotificationEmailRequest);
        }
        public async Task SendNotification(long participantId, string notificationTypeCode, long?topicId)
        {
            using var log = BeginFunction(nameof(CommunicationMicroService), nameof(SendNotification), participantId, notificationTypeCode, topicId);
            try
            {
                using var ctx = QuiltContextFactory.Create();

                var dbNotification = new Notification()
                {
                    ParticipantId        = participantId,
                    NotificationTypeCode = notificationTypeCode,
                    TopicId           = topicId,
                    CreateDateTimeUtc = GetUtcNow()
                };
                _ = ctx.Notifications.Add(dbNotification);

                var dbNotificationType = ctx.NotificationTypes.Find(dbNotification.NotificationTypeCode);

                var formatter = new NotificationEmailFormatter(dbNotificationType.Subject);

                var dbParticipant = ctx.Participants.Where(r => r.ParticipantId == dbNotification.ParticipantId).Single();

                string email;
                if (TryParseUserId.FromParticipantReference(dbParticipant.ParticipantReference, out string userId))
                {
                    var dbAspNetUser = await ctx.AspNetUsers.Where(r => r.Id == userId).SingleAsync().ConfigureAwait(false);

                    email = dbAspNetUser.Email;

                    var dbEmailRequest = new EmailRequest()
                    {
                        EmailRequestStatusCode = EmailRequestStatusCodes.Posted,
                        SenderEmail            = Constants.DoNotReplyEmail,
                        SenderEmailName        = Constants.DoNotReplyEmailName,
                        RecipientEmail         = email,
                        RecipientEmailName     = email,
                        RecipientParticipant   = dbNotification.Participant,
                        Subject                       = formatter.GetSubject(),
                        BodyText                      = formatter.GetText(),
                        BodyHtml                      = formatter.GetHtml(),
                        BodyTypeCode                  = EmailBodyTypes.Notification,
                        CreateDateTimeUtc             = GetUtcNow(),
                        EmailRequestStatusDateTimeUtc = GetUtcNow(),
                    };
                    _ = ctx.EmailRequests.Add(dbEmailRequest);

                    var dbNotificationEmailRequest = new NotificationEmailRequest()
                    {
                        Notification = dbNotification,
                        EmailRequest = dbEmailRequest
                    };
                    _ = ctx.NotificationEmailRequests.Add(dbNotificationEmailRequest);
                }

                _ = await ctx.SaveChangesAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                log.Exception(ex);
                throw;
            }
        }