public async Task GivenIHaveCreatedAndStoredAWebPushNotificationTemplate(Table table) { ITenantedNotificationTemplateStoreFactory storeFactory = this.serviceProvider.GetRequiredService <ITenantedNotificationTemplateStoreFactory>(); WebPushTemplate notificationTemplate = BuildWebPushNotificationTemplateFrom(table.Rows[0]); INotificationTemplateStore?store = await storeFactory.GetTemplateStoreForTenantAsync(this.featureContext.GetTransientTenant()).ConfigureAwait(false); await store.CreateOrUpdate(notificationTemplate.NotificationType !, CommunicationType.WebPush, notificationTemplate.ETag, notificationTemplate).ConfigureAwait(false); (WebPushTemplate, string?)webPushTemplate = await store.GetAsync <WebPushTemplate>(notificationTemplate.NotificationType !, CommunicationType.WebPush).ConfigureAwait(false); webPushTemplate.Item1.ETag = webPushTemplate.Item2; this.scenarioContext.Set(webPushTemplate.Item1); }
public async Task WhenISendTheUserNotificationTemplateAPIARequestToUpdateAnExistingWebPushNotificationTemplate(Table table) { IJsonSerializerSettingsProvider serializerSettingsProvider = this.serviceProvider.GetRequiredService <IJsonSerializerSettingsProvider>(); WebPushTemplate?savedWebPushTemplate = this.scenarioContext.Get <WebPushTemplate>(); WebPushTemplate notificationTemplate = DataSetupSteps.BuildWebPushNotificationTemplateFrom(table.Rows[0], savedWebPushTemplate.ETag); string?requestJson = JsonConvert.SerializeObject(notificationTemplate, serializerSettingsProvider.Instance); var requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json"); string transientTenantId = this.featureContext.GetTransientTenantId(); var uri = new Uri(FunctionsApiBindings.ManagementApiBaseUri, $"/{transientTenantId}/marain/usernotifications/templates"); await this.SendRequest(uri, HttpMethod.Put, requestContent, notificationTemplate.ETag).ConfigureAwait(false); }
/// <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)); }
private async Task SendWebPushNotificationAsync(string userId, string notificationType, string notificationId, ITenant tenant, WebPushTemplate webPushTemplate) { if (webPushTemplate is null) { throw new Exception($"There is no WebPushTemplate defined for tenant: {tenant.Id} and notification type: {notificationType}"); } // UserId will be a combination of tenantId and userId of that business. string airshipUserId = $"{tenant.Id}:{userId}"; // Fetch the shared airship config url from the tenant tenant.Properties.TryGet(Constants.TenantPropertyNames.AirshipKeyVaultUrl, out string airshipKeyVaultUrl); if (string.IsNullOrEmpty(airshipKeyVaultUrl)) { throw new Exception($"There is no SharedAirshipConfig defined for tenant: {tenant.Id} and notification type: {notificationType}"); } string?airshipSecretsString = await KeyVaultHelper.GetDeliveryChannelSecretAsync(this.configuration, airshipKeyVaultUrl).ConfigureAwait(false); if (string.IsNullOrEmpty(airshipSecretsString)) { throw new Exception("There is no airship delivery channel configuration setup in the keyvault"); } // Convert secret to airship secret model Airship airshipSecrets = JsonConvert.DeserializeObject <Airship>(airshipSecretsString); var airshipDeliveryChannelObject = new AirshipDeliveryChannel( webPushTemplate.Title !, webPushTemplate.Body !, webPushTemplate.ActionUrl !); try { AirshipWebPushResponse?airshipResponse = await this.SendAirshipNotificationAsync( airshipUserId, airshipDeliveryChannelObject, airshipSecrets).ConfigureAwait(false); await this.UpdateNotificationDeliveryStatusAsync( airshipDeliveryChannelObject.ContentType, notificationId, airshipResponse is null?UserNotificationDeliveryStatus.Unknown : UserNotificationDeliveryStatus.NotTracked, tenant).ConfigureAwait(false); } catch (Exception ex) { await this.UpdateNotificationDeliveryStatusAsync(airshipDeliveryChannelObject.ContentType, notificationId, UserNotificationDeliveryStatus.PermanentlyFailed, tenant, ex.Message).ConfigureAwait(false); } }