/// <inheritdoc />
        public async Task <ApiResponse> SetNotificationTemplate(
            string tenantId,
            ICommunicationTemplate communicationTemplate,
            CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrEmpty(tenantId))
            {
                throw new ArgumentNullException(nameof(tenantId));
            }

            if (communicationTemplate == null)
            {
                throw new ArgumentNullException(nameof(communicationTemplate));
            }

            Uri requestUri = this.ConstructUri($"/{tenantId}/marain/usernotifications/templates");

            HttpRequestMessage request = this.BuildRequest(HttpMethod.Put, requestUri, communicationTemplate);

            request = this.AddETagToNotificationTemplateHeaders(communicationTemplate, request);

            HttpResponseMessage response = await this.SendRequestAndThrowOnFailure(request, cancellationToken).ConfigureAwait(false);

            return(new ApiResponse(response.StatusCode));
        }
        public async Task <OpenApiResult> CreateTemplateAsync(
            IOpenApiContext context,
            ICommunicationTemplate body,
            [OpenApiParameter("If-None-Match")]
            string etag)
        {
            if (string.IsNullOrWhiteSpace(body.NotificationType))
            {
                throw new OpenApiNotFoundException("The NotificationType was not found in the object");
            }

            if (string.IsNullOrWhiteSpace(body.ContentType))
            {
                throw new OpenApiNotFoundException("The ContentType was not found in the object");
            }

            // We can guarantee tenant Id is available because it's part of the Uri.
            ITenant tenant = await this.marainServicesTenancy.GetRequestingTenantAsync(context.CurrentTenantId !).ConfigureAwait(false);

            // Gets the AzureBlobTemplateStore
            INotificationTemplateStore store = await this.tenantedTemplateStoreFactory.GetTemplateStoreForTenantAsync(tenant).ConfigureAwait(false);

            try
            {
                if (body is EmailTemplate emailTemplate)
                {
                    emailTemplate.ETag = etag;
                    await store.CreateOrUpdate(body.NotificationType, CommunicationType.Email, emailTemplate.ETag, emailTemplate).ConfigureAwait(false);
                }
                else if (body is SmsTemplate smsTemplate)
                {
                    smsTemplate.ETag = etag;
                    await store.CreateOrUpdate(body.NotificationType, CommunicationType.Sms, smsTemplate.ETag, smsTemplate).ConfigureAwait(false);
                }
                else if (body is WebPushTemplate webPushTemplate)
                {
                    webPushTemplate.ETag = etag;
                    await store.CreateOrUpdate(body.NotificationType, CommunicationType.WebPush, webPushTemplate.ETag, webPushTemplate).ConfigureAwait(false);
                }
                else
                {
                    // this should be removed in future updates
                    throw new OpenApiNotFoundException($"The template for ContentType: {body.ContentType} is not a valid content type");
                }
            }
            catch (StorageException e)
            {
                if (e?.RequestInformation?.HttpStatusCode == (int)System.Net.HttpStatusCode.PreconditionFailed)
                {
                    throw new OpenApiBadRequestException("Precondition failure. Blob's ETag does not match ETag provided.");
                }

                throw;
            }

            return(this.OkResult());
        }
        private HttpRequestMessage AddETagToNotificationTemplateHeaders(ICommunicationTemplate communicationTemplate, HttpRequestMessage httpRequestMessage)
        {
            if (communicationTemplate is EmailTemplate emailTemplate)
            {
                return(this.AddETagToHeader(httpRequestMessage, emailTemplate.ETag));
            }
            else if (communicationTemplate is SmsTemplate smsTemplate)
            {
                return(this.AddETagToHeader(httpRequestMessage, smsTemplate.ETag));
            }
            else if (communicationTemplate is WebPushTemplate webPushTemplate)
            {
                return(this.AddETagToHeader(httpRequestMessage, webPushTemplate.ETag));
            }

            return(null);
        }
Example #4
0
 public CommunicationTemplateController(ICommunicationTemplate communicationTemp)
 {
     _communicationTemp = communicationTemp;
 }