Skip to content
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
24 changes: 20 additions & 4 deletions jobConfig.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,37 @@ jobs:
request:
jobParams.subject:
type: string
- # Send an email. Requires email to be configured
- &email
# Send an email. Requires email to be configured
actionType: email
ignoreErrors: true
to: "{{{ job.contactEmail }}}"
subject: "[SciCat] {{ job.jobParams.subject }}"
bodyTemplateFile: demo_email.html
subject: |-
[SciCatLive] {{ default job.jobParams.subject "Job has been updated"}}
bodyTemplateFile: src/common/email-templates/job-template-simplified.html
update:
auth: "#jobOwnerUser"
actions: []
actions:
- # Only send emails for updates to status 'finished*'
Copy link
Member Author

Choose a reason for hiding this comment

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

Should this be added to jobConfig.recommended.yaml as well, since sending email is such a common task?

actionType: switch
phase: perform
property: job.statusCode
cases:
- regex: "/^finished/"
actions:
- *email
- actions:
- actionType: log
perform: "(Job {{job.id}} Skip email notification for status {{job.statusCode}}"


- jobType: url_demo
create:
auth: admin
actions:
- # Call a REST endpoint
actionType: url
ignoreErrors: true
url: http://localhost:3000/api/v3/health?jobid={{ job.id }}
method: GET
headers:
Expand Down
19 changes: 2 additions & 17 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ import { InstrumentsModule } from "./instruments/instruments.module";
import { MailerModule } from "@nestjs-modules/mailer";
import { join } from "path";
import { HandlebarsAdapter } from "@nestjs-modules/mailer/dist/adapters/handlebars.adapter";
import {
formatCamelCase,
unwrapJSON,
jsonify,
job_v3,
urlencode,
base64enc,
} from "./common/handlebars-helpers";
import { handlebarsHelpers } from "./common/handlebars-helpers";
import { CommonModule } from "./common/common.module";
import { RabbitMQModule } from "./common/rabbitmq/rabbitmq.module";
import { EventEmitterModule } from "@nestjs/event-emitter";
Expand Down Expand Up @@ -131,15 +124,7 @@ import { MaskSensitiveDataInterceptorModule } from "./common/interceptors/mask-s
},
template: {
dir: join(__dirname, "./common/email-templates"),
adapter: new HandlebarsAdapter({
unwrapJSON: unwrapJSON,
keyToWord: formatCamelCase,
eq: (a, b) => a === b,
jsonify: jsonify,
job_v3: job_v3,
urlencode: urlencode,
base64enc: base64enc,
}),
adapter: new HandlebarsAdapter(handlebarsHelpers),
Copy link
Member

Choose a reason for hiding this comment

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

for a later PR maybe, should we move the content of useFactory to a dedicated mailer factory class/file?

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed. It is getting rather long. It might make sense to wrap handlebars too. Currently you need to remember to register helpers if you call hb directly from outside of a module (eg during testing).

options: {
strict: true,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
{{!-- This is a minimal template used for testing email templating. --}}
Your {{job.type}} job with ID {{job.id}} has been completed successfully.
2 changes: 2 additions & 0 deletions src/common/handlebars-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export const handlebarsHelpers = {
unwrapJSON: unwrapJSON,
keyToWord: formatCamelCase,
eq: (a: unknown, b: unknown) => a === b,
matches: (query: string, regex: string) => RegExp(regex).test(query),
default: (query: unknown, def: unknown) => query || def,
jsonify: jsonify,
job_v3: job_v3,
urlencode: urlencode,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`Email Template job-template-simplified.html should render correctly 1`] = `
"<html>
<head>
<style type="text/css">
body {
font-family: Helvetica, sans-serif;
background: #f0f0f0;
margin: 1em;
}

.container {
margin: auto;
max-width: 50em;
background: white;
padding: 2em;
box-shadow: 0px 3px 1px -2px rgba(0, 0, 0, 0.2),
0px 2px 2px 0px rgba(0, 0, 0, 0.14),
0px 1px 5px 0px rgba(0, 0, 0, 0.12);
}
.job-id-container {
padding: 1em 0;
}
.footer {
font-size: 0.8em;
color: #666;
font-style: italic;
padding-top: 2em;
}
.link {
text-decoration: none;
color: #de9300;
}
.link:hover {
color: #fea901;
}

table {
border-collapse: collapse;
border-spacing: 10px;

tr {
border-top: 1px solid #eee;
}
td {
padding: 0.5em;
}

.key{
font-weight: bold;
color: #666;
vertical-align: top;
margin-top: 0;
}
}
</style>
</head>
<body>
<div class="container">
<div>
Your archive job has been submitted. It currently has status "jobStarted".
</div>
<div class="job-id-container">
<b>Job id:</b> jobId123
</div>
<p><b>The job will be perfomed on the following dataset(s):</b></p>
<table>
<tr style="background-color: lightblue;">
<td class="key">
testPid
</td>
</tr>
</table>

<div class="footer">
This email was automatically generated by
<a class="link" href="https://scicatproject.github.io/">SciCat</a>.
Please do not reply.
</div>
</div>
</body>
</html>"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface EmailJobActionOptions extends JobActionOptions {
from?: string;
subject: string;
bodyTemplateFile: string;
ignoreErrors?: boolean;
}

/**
Expand All @@ -26,6 +27,7 @@ export function isEmailJobActionOptions(
typeof opts.to === "string" &&
(opts.from === undefined || typeof opts.from === "string") &&
typeof opts.subject === "string" &&
typeof opts.bodyTemplateFile === "string"
typeof opts.bodyTemplateFile === "string" &&
(opts.ignoreErrors === undefined || typeof opts.ignoreErrors === "boolean")
);
}
Loading