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: [sc-16358] Passing parameters into a custom message template #539

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,160 @@
"required": true
}
},
{
"type": "Container",
"color": "accent",
"spacing": "none",
"items": [
{
"type": "TextBlock",
"text": "Formatting",
"weight": "bolder",
"size": "medium",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "To put a title in your template, use a hash:",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "# This is a title",
"weight": "lighter",
"size": "small",
"spacing": "none"
},
{
"type": "TextBlock",
"text": ""
},
{
"type": "TextBlock",
"text": "To make bullet points, use asterisks:",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "* point 1",
"weight": "lighter",
"size": "small",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "* point 2",
"weight": "lighter",
"size": "small",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "* point 3",
"weight": "lighter",
"size": "small",
"spacing": "none"
},
{
"type": "TextBlock",
"text": ""
},
{
"type": "TextBlock",
"text": "To add inset text, use a caret:",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "^ You must tell us if your circumstances change",
"weight": "lighter",
"size": "small",
"spacing": "none"
},
{
"type": "TextBlock",
"text": ""
},
{
"type": "TextBlock",
"text": "To add a horizontal line, use three dashes:",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "First paragraph",
"weight": "lighter",
"size": "small",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "---",
"weight": "lighter",
"size": "small",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "Second paragraph",
"weight": "lighter",
"size": "small",
"spacing": "none"
}
]
},
{
"type": "Container",
"color": "accent",
"spacing": "none",
"showWhen": "Array.isArray(data.parameters) && data.parameters.length > 0",
"items": [
{
"type": "TextBlock",
"text": "Personalisation",
"weight": "bolder",
"size": "medium",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "Use double brackets around the parameters you want to use to personalise your message:",
"spacing": "none"
},
{
"type": "TextBlock",
"text": "Hello ((firstName)) ((lastName))",
"weight": "lighter",
"size": "small",
"spacing": "none"
},
{
"type": "TextBlock",
"text": ""
},
{
"type": "TextBlock",
"text": "These are the parameters you can use in your message:",
"spacing": "none"
},
{
"type": "MarkupTable",
"arrayPath": "data.parameters",
"color": "accent",
"separator": "none",
"columns": [
{
"title": "Name",
"value": "{{ item.name }}"
},
{
"title": "Description",
"value": "{{ item.description }}"
}
]
}
]
},
{
"showWhen": "data.messageType === 'mail'",
"id": "subject",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
{
"title": "Message Content",
"value": "{{ data.message }}"
"value": "<span style=\"word-wrap: break-word; white-space: pre-wrap;\">{{ data.message }}</span>"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
"Finding": {
"Type": "Task",
"InputPath": "$.customTemplateId",
"Resource": "module:findingById",
"ResourceConfig": {
"modelId": "tymly_govUkCustomTemplates"
},
"Resource": "module:findCustomMessageTemplate",
"ResultPath": "$",
"Next": "AwaitingHumanInput"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"recipient",
"statusCode",
"modified"
]
],
"orderBy": ["-modified"]
}
},
"ResultPath": "$.sendReceipts",
Expand Down
33 changes: 29 additions & 4 deletions lib/components/services/notify/Message-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MessageTemplate {
this.blueprint = definition.blueprintName
this.namespace = definition.namespace
this.isCustomTemplate = definition.custom === true
this.parameters = definition.parameters || {}
this.availableEvents = new Set()

if (!this.templateId) throw new Error('Message template missing templateId')
Expand Down Expand Up @@ -71,10 +72,22 @@ class MessageTemplate {
_input.message = customTemplate.message || ''

if (_input.parameters) {
for (const [k, v] of Object.entries(_input.parameters)) {
const regex = new RegExp(`\\(\\(\\s*${k}\\s*\\)\\)`, 'g')
_input.subject = _input.subject.replace(regex, v)
_input.message = _input.message.replace(regex, v)
for (const [parameterName, value] of Object.entries(_input.parameters)) {
const parameterConfig = this.parameters[parameterName]

_input.subject = injectMessageParameters({
string: _input.subject,
parameterName,
parameterConfig,
value
})

_input.message = injectMessageParameters({
string: _input.message,
parameterName,
parameterConfig,
value
})
}
}
})
Expand Down Expand Up @@ -152,4 +165,16 @@ class MessageTemplate {
}
}

function injectMessageParameters ({ string, parameterName, parameterConfig, value }) {
// Use jsonschema to validate value against parameterConfig?
// const validator = require('jsonschema').validate

if (Array.isArray(value)) {
value = value.join('\n')
}

const regex = new RegExp(`\\(\\(\\s*${parameterName}\\s*\\)\\)`, 'g')
return string.replace(regex, value)
}

module.exports = MessageTemplate
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ class FindCustomMessageTemplate {

const template = this.notify.templates[customTemplate.templateName]

const parameters = Object.entries(template.parameters).map(([name, { description }]) => {
return { name, description }
})

const availableEvents = [...template.availableEvents]
.map(eventName => this.notify.events[eventName])
.filter(event => event)

return context.sendTaskSuccess({ ...customTemplate, availableEvents })
return context.sendTaskSuccess({
...customTemplate,
parameters,
availableEvents
})
}
}

Expand Down