示例#1
0
        public async Task <bool> Handle(NewNotificationAddedCommand request, CancellationToken cancellationToken)
        {
            var notificationModel = this._mapper.Map <Domain.Notification>(request);

            this._validator.ValidateAndThrowEx(request);

            // save to db
            this._unitOfWork.GetRepository <Domain.Notification>().Create(notificationModel);
            var saved = await this._unitOfWork.SaveChangesAsnyc();

            var userModel = await this._unitOfWork.GetRepository <User>().FindByIdAsync(notificationModel.UserId);

            var notificationServiceDto = new NotificationServiceDto
            {
                Body    = notificationModel.Message,
                Email   = userModel.Email,
                Subject = notificationModel.Title,
                PhoneNo = userModel.PhoneNo
            };
            var sent = await this._notificationService.Send(notificationModel.Type, notificationServiceDto);

            // change the notification status
            notificationModel.Status = sent ? Common.Enums.NotificationStatusEnum.Viewed : Common.Enums.NotificationStatusEnum.Error;
            await this._unitOfWork.SaveChangesAsnyc();

            return(saved && sent);
        }
        public async Task <bool> Send(NotificationTypeEnum notificationTypeEnum, NotificationServiceDto notificationServiceDto)
        {
            switch (notificationTypeEnum)
            {
            case NotificationTypeEnum.Email:
                return(await this._emailNotificationService.Send(notificationServiceDto));

            case NotificationTypeEnum.Text:
                throw new NotImplementedException("Text type not implemented");

            default:
                return(true);
            }
        }
        public async Task <bool> Send(NotificationServiceDto notificationServiceDto)
        {
            var client          = new SendGridClient(this._notificationService.ApiKey);
            var sendGridMessage = MailHelper.CreateSingleEmail(
                new EmailAddress(this._notificationService.From),
                new EmailAddress(notificationServiceDto.Email),
                notificationServiceDto.Subject,
                "",
                notificationServiceDto.Body
                );
            var response = await client.SendEmailAsync(sendGridMessage);

            return(response.IsSuccessStatusCode);
        }