public async Task <long> CreateAlert(Exception exception, long?participantId = null, long?topicId = null)
        {
            using var log = BeginFunction(nameof(CommunicationMicroService), nameof(CreateAlert), exception, topicId);
            try
            {
                var utcNow = GetUtcNow();

                using var ctx = QuiltContextFactory.Create();

                var dbAlert = new Alert()
                {
                    AlertTypeCode     = AlertTypeCodes.OperationException,
                    CreateDateTimeUtc = utcNow,
                    Description       = "Operation Exception",
                    Exception         = exception.GetDetail(),
                    ParticipantId     = participantId,
                    TopicId           = topicId
                };
                _ = ctx.Alerts.Add(dbAlert);

                var dbAlertType = ctx.AlertTypes.Find(dbAlert.AlertTypeCode);

                var formatter = new AlertEmailFormatter(dbAlertType.Name);

                var dbEmailRequest = new EmailRequest()
                {
                    EmailRequestStatusCode = EmailRequestStatusCodes.Posted,
                    SenderEmail            = Constants.DoNotReplyEmail,
                    SenderEmailName        = Constants.DoNotReplyEmailName,
                    RecipientEmail         = Constants.AdminMailEmail,
                    RecipientEmailName     = Constants.AdminMailEmailName,
                    RecipientParticipantId = null,
                    Subject                       = formatter.GetSubject(),
                    BodyText                      = formatter.GetText(),
                    BodyHtml                      = formatter.GetHtml(),
                    BodyTypeCode                  = EmailBodyTypes.Alert,
                    CreateDateTimeUtc             = utcNow,
                    EmailRequestStatusDateTimeUtc = utcNow,
                };
                _ = ctx.EmailRequests.Add(dbEmailRequest);

                var dbAlertEmailRequest = new AlertEmailRequest()
                {
                    Alert        = dbAlert,
                    EmailRequest = dbEmailRequest,
                };
                _ = ctx.AlertEmailRequests.Add(dbAlertEmailRequest);

                _ = await ctx.SaveChangesAsync().ConfigureAwait(false);

                return(dbAlert.AlertId);
            }
            catch (Exception ex)
            {
                log.Exception(ex);
                throw;
            }
        }
Beispiel #2
0
        //protected void EndFunction()
        //{
        //    Logger.LogEndFunction();
        //}

        //protected void LogException(Exception ex)
        //{
        //    Logger.LogException(ex);
        //}

        //protected void LogResult(object result)
        //{
        //    Logger.LogResult(result);
        //}

        protected void OnAlertCreated(QuiltContext ctx, Alert dbAlert)
        {
            // Do not generate an email request if the alert involves an email request.
            //
            if (dbAlert.EmailRequestId != null)
            {
                return;
            }

            var dbAlertType = ctx.AlertTypes.Find(dbAlert.AlertTypeCode);

            var formatter = new AlertEmailFormatter(dbAlertType.Name);

            var dbEmailRequest = new EmailRequest()
            {
                EmailRequestStatusCode = EmailRequestStatusCodes.Posted,
                SenderEmail            = Constants.DoNotReplyEmail,
                SenderEmailName        = Constants.DoNotReplyEmailName,
                RecipientEmail         = Constants.AdminMailEmail,
                RecipientEmailName     = Constants.AdminMailEmailName,
                RecipientParticipantId = null,
                Subject                       = formatter.GetSubject(),
                BodyText                      = formatter.GetText(),
                BodyHtml                      = formatter.GetHtml(),
                BodyTypeCode                  = EmailBodyTypes.Alert,
                CreateDateTimeUtc             = Locale.GetUtcNow(),
                EmailRequestStatusDateTimeUtc = Locale.GetUtcNow(),
            };

            _ = ctx.EmailRequests.Add(dbEmailRequest);

            var dbAlertEmailRequest = new AlertEmailRequest()
            {
                Alert        = dbAlert,
                EmailRequest = dbEmailRequest,
            };

            _ = ctx.AlertEmailRequests.Add(dbAlertEmailRequest);
        }