Skip to content
Open
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
1 change: 1 addition & 0 deletions cms/models/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Item.schema.post("save", async function(doc, next) {
.model.findById(doc._id)
.populate("source")
.populate("evidenceType")
.populate("speciality")
.exec();
} catch (err) {
logger.error("An error occurred finding item: ", err.message);
Expand Down
2 changes: 2 additions & 0 deletions cms/routes/api/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports.single = function(req, res) {
.findById(req.params.itemId)
.populate("source")
.populate("evidenceType")
.populate("speciality")
.exec(function(err, item) {
if (err) return res.err(err);

Expand All @@ -25,6 +26,7 @@ exports.list = function(req, res) {
.find()
.populate("source")
.populate("evidenceType")
.populate("speciality")
.exec(function(err, items) {
if (err) return res.json({ err: err });

Expand Down
52 changes: 48 additions & 4 deletions lambda/MAS.Tests/Feeds/multiple-items.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@
"publicationDate": "2019-11-25T13:48:36.000Z",
"createdDate": "2019-10-22T15:05:05.927Z",
"speciality": [
"5daf1a5c8a34d485cb05b5ba"
{
"_id": "5e0e4331200a585f718e1ee5",
"key": "be1c2e2f-745e-4a82-b5aa-d4cef4d31a1b",
"title": "Anaesthesia and pain",
"__v": 0
},
{
_id: "5e0e4331200a58dead8e1ee8",
key: "53fc67a4-46d8-4171-9447-7fcf216c8749",
title: "Complementary and alternative therapies",
__v: 0
}
],
"evidenceType": {
"_id": "5df7abf383138898ee1f67ef",
Expand Down Expand Up @@ -44,7 +55,18 @@
"publicationDate": "2019-11-23T13:48:36.000Z",
"createdDate": "2019-10-23T10:38:43.000Z",
"speciality": [
"5daf1a5c8a34d478fd05b5d4"
{
"_id": "5e0e4331200a585f718e1ee5",
"key": "be1c2e2f-745e-4a82-b5aa-d4cef4d31a1b",
"title": "Anaesthesia and pain",
"__v": 0
},
{
_id: "5e0e4331200a58dead8e1ee8",
key: "53fc67a4-46d8-4171-9447-7fcf216c8749",
title: "Complementary and alternative therapies",
__v: 0
}
],
"evidenceType": {
"_id": "5df7abf383138898ee1f67ef",
Expand Down Expand Up @@ -72,7 +94,18 @@
"publicationDate": "2019-10-23T00:00:00.000Z",
"createdDate": "2019-10-23T10:56:40.000Z",
"speciality": [
"5daf1a5c8a34d4d6e505b5b9"
{
"_id": "5e0e4331200a585f718e1ee5",
"key": "be1c2e2f-745e-4a82-b5aa-d4cef4d31a1b",
"title": "Anaesthesia and pain",
"__v": 0
},
{
_id: "5e0e4331200a58dead8e1ee8",
key: "53fc67a4-46d8-4171-9447-7fcf216c8749",
title: "Complementary and alternative therapies",
__v: 0
}
],
"evidenceType": {
"_id": "5df7abf383138898ee1f67ef",
Expand Down Expand Up @@ -100,7 +133,18 @@
"publicationDate": "2019-10-28T00:00:00.000Z",
"createdDate": "2019-10-28T11:07:11.000Z",
"speciality": [
"5daf1a5c8a34d485cb05b5ba"
{
"_id": "5e0e4331200a585f718e1ee5",
"key": "be1c2e2f-745e-4a82-b5aa-d4cef4d31a1b",
"title": "Anaesthesia and pain",
"__v": 0
},
{
_id: "5e0e4331200a58dead8e1ee8",
key: "53fc67a4-46d8-4171-9447-7fcf216c8749",
title: "Complementary and alternative therapies",
__v: 0
}
],
"evidenceType": {
"_id": "5df7abf383138898ee1f67ef",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ Title: Some title
Short Summary: Wonder drug
Source: The Journal of Medicine
Evidence Type: Safety alerts
UKMI Comment: UKMI Comment
UKMI Comment: SPS Comment
Resource Links: <p><a title="Link 1" href="items/5de65fe432281d43fbfcd15a">Link 1</a></p>
<p><a title="sadada" href="items/5de65fe432281d43fbfcd15a">Link 2</a></p>
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task PutCMSItemSavesItemIntoS3()
Key = "mas_evidence_types:Safety%20alerts",
Title = "Safety alerts"
},
UKMiComment = "UKMI Comment",
Comment = "SPS Comment",
ResourceLinks = "<p><a title=\"Link 1\" href=\"items/5de65fe432281d43fbfcd15a\">Link 1</a></p>\r\n<p><a title=\"sadada\" href=\"items/5de65fe432281d43fbfcd15a\">Link 2</a></p>"
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div><div class='evidenceType'><strong>Some evidence type 2</strong><div class='item'>Some Title<br>Some source<br>Some speciality 2<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality | Another speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>
139 changes: 139 additions & 0 deletions lambda/MAS.Tests/UnitTests/DailyEmailTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using MAS.Models;
using MAS.Tests.Infrastructure;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace MAS.Tests.UnitTests
{
public class DailyEmailTests : TestBase
{
Item exampleItem = new Item()
{
Id = "123",
Title = "Some Title",
Slug = "abc",
ShortSummary = "Some short summary",
ResourceLinks = "",
Comment = "",
Speciality = new List<Speciality>
{
new Speciality()
{
Key = "1a",
Title = "Some speciality",

}
},
EvidenceType = new EvidenceType()
{
Key = "1b",
Title = "Some evidence type"
},
Source = new Source()
{
Id = "1c",
Title = "Some source"
}
};
Item exampleItem2 = new Item()
{
Id = "123",
Title = "Some Title",
Slug = "abc",
ShortSummary = "Some short summary",
ResourceLinks = "",
Comment = "",
Speciality = new List<Speciality>
{
new Speciality()
{
Key = "1a",
Title = "Some speciality 2",

}
},
EvidenceType = new EvidenceType()
{
Key = "1c",
Title = "Some evidence type 2"
},
Source = new Source()
{
Id = "1c",
Title = "Some source"
}
};

[Fact]
public void CanCreateSingleItemEmail()
{
var email = new DailyEmail()
{
Items = new List<Item>()
{
exampleItem
}
};

var expectedHtml = "<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use the extension method ShouldMatchApproved

Copy link
Author

@d-lan1 d-lan1 Jan 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good shout!


expectedHtml.ShouldMatchApproved(email.HTML);

}

[Fact]
public void CanCreateEmailWithTwoItemsSharingEvidenceType()
{
var email = new DailyEmail()
{
Items = new List<Item>()
{
exampleItem,
exampleItem
}
};

var expectedHtml = "<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>";
expectedHtml.ShouldMatchApproved(email.HTML);

}

[Fact]
public void CanCreateEmailWithTwoItemsDifferentEvidenceType()
{
var email = new DailyEmail()
{
Items = new List<Item>()
{
exampleItem,
exampleItem2
}
};

var expectedHtml = "<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div><div class='evidenceType'><strong>Some evidence type 2</strong><div class='item'>Some Title<br>Some source<br>Some speciality 2<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>";
expectedHtml.ShouldMatchApproved(email.HTML);

}

[Fact]
public void ItemsWithManySpecialitiesRenderCorrectly()
{
exampleItem.Speciality.Add(new Speciality() { Key = "abcd", Title = "Another speciality" });
var email = new DailyEmail()
{
Items = new List<Item>()
{
exampleItem
}
};

var expectedHtml = "<div class='evidenceType'><strong>Some evidence type</strong><div class='item'>Some Title<br>Some source<br>Some speciality | Another speciality<br>Some short summary<br><a href='https://www.medicinesresources.nhs.uk/abc'>SPS Comment</a></div></div>";
expectedHtml.ShouldMatchApproved(email.HTML);

}
}
}
2 changes: 1 addition & 1 deletion lambda/MAS/Controllers/MailController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task<IActionResult> PutMailAsync()
{
var items = await _contentService.GetItemsAsync();

var body = _mailService.CreateEmailBody(items);
var body = _mailService.CreateDailyEmailBody(items);
var subject = "MAS Email";
var previewText = "This MAS email was created " + DateTime.Now.ToShortDateString();

Expand Down
57 changes: 57 additions & 0 deletions lambda/MAS/Models/DailyEmail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MAS.Models
{
public class DailyEmail
{
public List<Item> Items { get; set; }

public List<IGrouping<string, Item>> GroupedItems
{
get
{
return Items.GroupBy(x => x.EvidenceType.Title).ToList();
}
}


public string HTML
Copy link
Contributor

@suegarner suegarner Jan 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this is a model

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to me to have a daily email model. I agree with you that HTML property may not belong in this class - but im unsure as I've seen business logic in models before and the HTML is after all just a string property returning a string interpretation of the rest of the class. The HTML properties logic could be moved into a separate service which perhaps returns a dynamic view. However, for this card I think what we currently have is ok. Perhaps in the branding card, we can look at this. What do you think?

{
get
{
var body = new StringBuilder();

foreach (var group in GroupedItems)
{
var evidenceType = group.Key;

body.Append("<div class='evidenceType'>");
body.Append("<strong>" + evidenceType + "</strong>");

foreach(var item in group)
{
body.Append("<div class='item'>");
body.Append(item.Title);
body.Append("<br>");
body.Append(item.Source.Title);
body.Append("<br>");
body.Append(String.Join(" | ", item.Speciality.Select(x => x.Title)));
body.Append("<br>");
body.Append(item.ShortSummary);
body.Append("<br>");
body.Append("<a href='https://www.medicinesresources.nhs.uk/" + @item.Slug + "'>SPS Comment</a>");
body.Append("</div>");
}

body.Append("</div>");
}

return body.ToString();
}
}
}
}
7 changes: 6 additions & 1 deletion lambda/MAS/Models/Item.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace MAS.Models
Expand All @@ -7,14 +8,18 @@ public class Item
{
[JsonProperty("_id"), Required, JsonRequired]
public string Id { get; set; }
[JsonProperty("slug")]
public string Slug { get; set; }
[Required, JsonRequired]
public string Title { get; set; }
[Required, JsonRequired]
public Source Source { get; set; }
[Required, JsonRequired]
public EvidenceType EvidenceType { get; set; }
[JsonProperty("speciality")]
public List<Speciality> Speciality { get; set; }
public string ShortSummary { get; set; }
public string UKMiComment { get; set; }
public string Comment { get; set; }
public string ResourceLinks { get; set; }
}
}
14 changes: 14 additions & 0 deletions lambda/MAS/Models/Speciality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MAS.Models
{
public class Speciality
{
public string Title { get; set; }

public string Key { get; set; }
}
}
Loading