Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add layout to step message templates # 69 #71

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Novu.Tests/IntegrationTests/MessageTemplateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ public static DigestMessageTemplate DigestMessageTemplate()
return new DigestMessageTemplate();
}

public static EmailMessageTemplate EmailMessageTemplate()
public static EmailMessageTemplate EmailMessageTemplate(string layoutId = null)
{
return new EmailMessageTemplate
{
// Type = StepTypeEnum.Email,
ContentType = MessageTemplateContentType.Editor,
SenderName = "Hello",
Subject = "{{mail.subject}}",
LayoutId = layoutId,
Content = new List<EmailBlock>
{
new()
Expand Down
4 changes: 2 additions & 2 deletions src/Novu.Tests/IntegrationTests/StepFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public static Step InApp(bool active = false)
};
}

public static Step Email(bool active = false)
public static Step Email(bool active = false, string layoutId = null)
{
return new Step
{
Name = $"Email ({StringGenerator.LoremIpsum(2)})",
Template = MessageTemplateFactory.EmailMessageTemplate(),
Template = MessageTemplateFactory.EmailMessageTemplate(layoutId),
Active = active,
};
}
Expand Down
18 changes: 18 additions & 0 deletions src/Novu.Tests/IntegrationTests/WorkflowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Novu.DTO;
using Novu.DTO.Layouts;
using Novu.DTO.Workflows;
using Novu.Extensions;
using Novu.Models.Workflows;
Expand Down Expand Up @@ -83,6 +85,22 @@ public async Task Should_Create_Steps(string test, Step[] steps, int stepsCount)
workflow.Triggers.Should().HaveCount(1);
}


[Fact]
public async Task Should_Create_Step_With_LayoutSet()
{
// we know there is always a default layout
var layout = (await Layout.Get())
.Data
.Single(x => x.IsDefault);

var steps = new[] { StepFactory.Email(layoutId: layout.Id) };
var workflow = await Make<Workflow>(steps: steps);
workflow.Should().NotBeNull();
workflow.Triggers.Should().HaveCount(1);
workflow.Steps.First().Template.LayoutId.Should().Be(layout.Id);
}

[Fact]
public async Task Should_Get_Workflows()
{
Expand Down
14 changes: 14 additions & 0 deletions src/Novu/Models/Workflows/Step/Message/BaseMessageTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,18 @@ public abstract class BaseMessageTemplate
[JsonProperty("deleted")] public bool Deleted { get; set; }
[JsonProperty("variables")] public TemplateVariable[] Variables { get; set; }
[JsonProperty("actor")] public Actor Actor { get; set; }

/// <summary>
/// All messages CAN have a layout although the general approach is to only provide layouts for
/// an email as a wrapper around the content of the message
/// </summary>
[JsonProperty("layoutId")]
public string? LayoutId
{
get => _layoutId;
set => _layoutId = value;
}

// ReSharper disable once InconsistentNaming
[JsonProperty("_layoutId")] public string? _layoutId { get; set; }
}
28 changes: 19 additions & 9 deletions src/Novu/Models/Workflows/Step/Message/IMessageTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Novu.Models.Workflows.Step.Message;

public interface IMessageTemplate
{
[JsonProperty("type")]
[JsonConverter(typeof(StringEnumConverter))]
StepTypeEnum Type { get; set; }

/// <summary>
/// Polymorphic between string (code) and typed EmailBlock[]
/// </summary>
object Content { get; set; }

[JsonProperty("contentType")]
[JsonConverter(typeof(StringEnumConverter))]
MessageTemplateContentType ContentType { get; set; }

[JsonProperty("deleted")] bool Deleted { get; set; }
bool Deleted { get; set; }

TemplateVariable[] Variables { get; set; }

/// <summary>
/// Layout has three lifecycles (create, update and read) and the create and update use the non-underscore
/// whereas the read uses underscore.
///
/// The problems lies that these are nested objects that the API doesn't create as sub-resources to manage.
///
/// Therefore the implementation must deal with READ/WRITE in situ.
/// </summary>
/// <remarks>
/// The same problem for <see cref="FeedId"/>
/// </remarks>
public string? LayoutId { get; set; }
// ReSharper disable once InconsistentNaming
public string? _layoutId { get; set; }

[JsonProperty("variables")] TemplateVariable[] Variables { get; set; }
public Actor Actor { get; set; }
}
Loading