/// <inheritdoc/> public async Task <NotificationTemplate> GenerateTemplateAsync( INotificationTemplateStore templateStore, IPropertyBag body, List <CommunicationType> registeredCommunicationChannels, string notificationType) { EmailTemplate? emailTemplate = null; SmsTemplate? smsTemplate = null; WebPushTemplate?webPushTemplate = null; IReadOnlyDictionary <string, object> propertiesDictionary = body.AsDictionaryRecursive(); var propertiesHash = Hash.FromReadOnlyDictionary(propertiesDictionary); foreach (CommunicationType channel in registeredCommunicationChannels) { switch (channel) { case CommunicationType.Email: try { (EmailTemplate, string?)emailRawTemplate = await templateStore.GetAsync <EmailTemplate>(notificationType, CommunicationType.Email).ConfigureAwait(false); string?emailBody = await this.GenerateTemplateForFieldAsync(emailRawTemplate.Item1.Body, propertiesHash).ConfigureAwait(false); string?emailSubject = await this.GenerateTemplateForFieldAsync(emailRawTemplate.Item1.Subject, propertiesHash).ConfigureAwait(false); if (string.IsNullOrEmpty(emailSubject)) { this.logger.LogError($"The template for the communication type Email doesn't have subject which is necessary to trigger a {notificationType} notification."); break; } if (string.IsNullOrEmpty(emailBody)) { this.logger.LogError($"The template for the communication type Email doesn't have body which is necessary to trigger a {notificationType} notification."); break; } emailTemplate = new EmailTemplate( notificationType, emailSubject, emailBody); } catch (Exception) { this.logger.LogError("The template for the communication type Email doesn't exist"); } break; case CommunicationType.Sms: try { (SmsTemplate, string?)smsRawTemplate = await templateStore.GetAsync <SmsTemplate>(notificationType, CommunicationType.Sms).ConfigureAwait(false); string?smsBody = await this.GenerateTemplateForFieldAsync(smsRawTemplate.Item1.Body !, propertiesHash).ConfigureAwait(false); if (string.IsNullOrEmpty(smsBody)) { this.logger.LogError($"The template for the communication type Sms doesn't have body which is necessary to trigger a {notificationType} notification."); break; } smsTemplate = new SmsTemplate(notificationType, smsBody); } catch (Exception) { this.logger.LogError("The template for the communication type Sms doesn't exist"); } break; case CommunicationType.WebPush: try { (WebPushTemplate, string?)webPushRawTemplate = await templateStore.GetAsync <WebPushTemplate>(notificationType, CommunicationType.WebPush).ConfigureAwait(false); string?webPushTitle = await this.GenerateTemplateForFieldAsync(webPushRawTemplate.Item1.Title, propertiesHash).ConfigureAwait(false); string?webPushBody = await this.GenerateTemplateForFieldAsync(webPushRawTemplate.Item1.Body, propertiesHash).ConfigureAwait(false); string?webPushImage = await this.GenerateTemplateForFieldAsync(webPushRawTemplate.Item1.Image, propertiesHash).ConfigureAwait(false); string?actionUrl = await this.GenerateTemplateForFieldAsync(webPushRawTemplate.Item1.ActionUrl, propertiesHash).ConfigureAwait(false); if (string.IsNullOrEmpty(webPushTitle)) { this.logger.LogError($"The template for the communication type WebPush doesn't have title which is necessary to trigger a {notificationType} notification."); break; } if (string.IsNullOrEmpty(webPushBody)) { this.logger.LogError($"The template for the communication type WebPush doesn't have body which is necessary to trigger a {notificationType} notification."); break; } webPushTemplate = new WebPushTemplate( notificationType, webPushTitle, webPushBody, webPushImage, actionUrl); } catch (Exception) { this.logger.LogError("The template for the communication type WebPush doesn't exist"); } break; } } return(new NotificationTemplate( notificationType, smsTemplate, emailTemplate, webPushTemplate)); }
public async Task <OpenApiResult> GetTemplateAsync( IOpenApiContext context, string notificationType, CommunicationType communicationType) { // We can guarantee tenant Id is available because it's part of the Uri. ITenant tenant = await this.marainServicesTenancy.GetRequestingTenantAsync(context.CurrentTenantId !).ConfigureAwait(false); INotificationTemplateStore store = await this.tenantedTemplateStoreFactory.GetTemplateStoreForTenantAsync(tenant).ConfigureAwait(false); HalDocument?response = null; string? eTag = null; // Gets the template by notificationType switch (communicationType) { case CommunicationType.Email: (EmailTemplate, string?)emailTemplateWrapper = await store.GetAsync <EmailTemplate>(notificationType, communicationType).ConfigureAwait(false); if (emailTemplateWrapper.Item1 is null) { throw new OpenApiNotFoundException($"The notification template for notificationType {notificationType} and communicationType {communicationType} was not found."); } // Add etag to the EmailTemplate eTag = emailTemplateWrapper.Item2; emailTemplateWrapper.Item1.ETag = emailTemplateWrapper.Item2; response = await this.emailTemplateMapper.MapAsync(emailTemplateWrapper.Item1, context).ConfigureAwait(false); break; case CommunicationType.Sms: (SmsTemplate, string?)smsTemplateWrapper = await store.GetAsync <SmsTemplate>(notificationType, communicationType).ConfigureAwait(false); if (smsTemplateWrapper.Item1 is null) { throw new OpenApiNotFoundException($"The notification template for notificationType {notificationType} and communicationType {communicationType} was not found."); } // Add etag to the SmsTemplate eTag = smsTemplateWrapper.Item2; smsTemplateWrapper.Item1.ETag = smsTemplateWrapper.Item2; response = await this.smsTemplateMapper.MapAsync(smsTemplateWrapper.Item1, context).ConfigureAwait(false); break; case CommunicationType.WebPush: (WebPushTemplate, string?)webpushWrapper = await store.GetAsync <WebPushTemplate>(notificationType, communicationType).ConfigureAwait(false); if (webpushWrapper.Item1 is null) { throw new OpenApiNotFoundException($"The notification template for notificationType {notificationType} and communicationType {communicationType} was not found."); } // Add etag to the WebPushTemplate eTag = webpushWrapper.Item2; webpushWrapper.Item1.ETag = webpushWrapper.Item2; response = await this.webPushTemplateMapper.MapAsync(webpushWrapper.Item1, context).ConfigureAwait(false); break; } if (response is null) { throw new OpenApiNotFoundException($"The notification template for notificationType {notificationType} and communicationType {communicationType} was not found."); } OpenApiResult okResult = this.OkResult(response, "application/json"); if (!string.IsNullOrEmpty(eTag)) { okResult.Results.Add("ETag", eTag); } return(okResult); }