From 8dc9faae2f85bd462c3b4dbc90ad126a0a6584c6 Mon Sep 17 00:00:00 2001
From: Yannis Barlas <yannisbarlas@gmail.com>
Date: Tue, 23 Mar 2021 19:49:01 +0200
Subject: [PATCH] feat(server): include send email component & update docs

---
 README.md    | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 package.json |  1 +
 src/index.js |  3 +++
 yarn.lock    | 12 +++++++++++
 4 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 3773a22..3463821 100644
--- a/README.md
+++ b/README.md
@@ -8,7 +8,7 @@ Install package and remove the dependencies it is meant to replace.
 
 ```sh
 ## if migrating from an existing project
-yarn remove pubsweet pubsweet-server @pubsweet/logger @pubsweet/base-model
+yarn remove pubsweet pubsweet-server @pubsweet/logger @pubsweet/base-model @pubsweet/component-send-email
 ##
 
 yarn add @coko/server
@@ -155,6 +155,46 @@ cron.schedule('* * * * * *', () => {
 
 The library that enables this is `node-cron`. Be sure to check its [documentation](https://github.com/node-cron/node-cron#node-cron) for further details.
 
+### Queue manager
+
+We use [`pg-boss`](https://github.com/timgit/pg-boss/blob/master/docs/usage.md#publish) to handle queues. This package exposes an instance of pg-boss, so you don't have to initiate it yourself. In other words, there's no need to use the `start` and `stop` functions. These are handled.
+
+The following SQL code needs to have run on your database, either via a database init script or as part of a migration:
+
+```sql
+CREATE EXTENSION IF NOT EXISTS pgcrypto;
+```
+
+Use the queue manager as follows:
+
+```js
+const { boss } = require('@coko/server')
+
+await boss.publish('my-job', { and: 'how' })
+```
+
+The above method is recommended, but if you already use pubsweet's `connectToJobQueue` and want a low-overhead way to switch to `@coko/server`, we also expose the same function for backwards compatibility.
+
+```js
+// Replace this
+const { connectToJobQueue } = require('pubsweet-server')
+
+// With this
+const { connectToJobQueue } = require('@coko/server')
+```
+
+You can also disable the queue manager altogether with the following config option:
+
+```js
+// config/default.js
+
+module.exports = {
+  'pubsweet-server': {
+    useJobQueue: false,
+  },
+}
+```
+
 ### Disable GraphQL
 
 There are cases where you might not want a graphql server at all. eg. If you are building a sevice with a single REST api endpoint with coko server.
@@ -213,6 +253,22 @@ Returns pubsweet's base model
 const { BaseModel } = require('@coko/server')
 ```
 
+##### sendEmail
+
+Reads your mailer config and sends an email.
+
+```js
+const { sendEmail } = require('@coko/server')
+
+sendEmail({
+  from: 'noreply@me.com',
+  html: `<p>Hello</p>`,
+  subject: `The hello message`,
+  text: 'Hello',
+  to: 'someone@email.com',
+})
+```
+
 ##### createJWT
 
 `createJWT` is an export of a function in `pubsweet-server` that does just that.  
diff --git a/package.json b/package.json
index 4d4c33a..f6bc72d 100644
--- a/package.json
+++ b/package.json
@@ -24,6 +24,7 @@
   },
   "dependencies": {
     "@pubsweet/base-model": "^4.0.8",
+    "@pubsweet/component-send-email": "^0.4.11",
     "@pubsweet/db-manager": "^3.1.19",
     "@pubsweet/errors": "^2.0.44",
     "@pubsweet/logger": "^0.2.54",
diff --git a/src/index.js b/src/index.js
index ede7d26..0475697 100644
--- a/src/index.js
+++ b/src/index.js
@@ -5,6 +5,7 @@ const authentication = require('pubsweet-server/src/authentication')
 const { pubsubManager, startServer } = require('pubsweet-server')
 const logger = require('@pubsweet/logger')
 const { db } = require('@pubsweet/db-manager')
+const { send: sendEmail } = require('@pubsweet/component-send-email')
 
 const app = require('./app')
 const { boss, connectToJobQueue } = require('./pgboss')
@@ -17,6 +18,8 @@ module.exports = {
   pubsubManager,
   startServer,
 
+  sendEmail,
+
   BaseModel,
   logger,
   db,
diff --git a/yarn.lock b/yarn.lock
index 6ec3f4f..ac7eae0 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -416,6 +416,13 @@
     objection "^2.1.6"
     uuid "^7.0.3"
 
+"@pubsweet/component-send-email@^0.4.11":
+  version "0.4.11"
+  resolved "https://registry.yarnpkg.com/@pubsweet/component-send-email/-/component-send-email-0.4.11.tgz#9870da674881b5d13f2933ce30ef1925a011d3d3"
+  integrity sha512-IhKTxELx+dG2K77lcnnwg6bmm3TzhIUXtrDTvu/h+J3CNu/wfpMYowttBijVJxg69RLkA9dhBWIZEIay8TQIaw==
+  dependencies:
+    nodemailer "^6.4.18"
+
 "@pubsweet/db-manager@^3.1.19":
   version "3.1.19"
   resolved "https://registry.yarnpkg.com/@pubsweet/db-manager/-/db-manager-3.1.19.tgz#0f54eea9640d66e2467d5211b8841f1b91dc107c"
@@ -6003,6 +6010,11 @@ node-notifier@^5.4.0:
     shellwords "^0.1.1"
     which "^1.3.0"
 
+nodemailer@^6.4.18:
+  version "6.5.0"
+  resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-6.5.0.tgz#d12c28d8d48778918e25f1999d97910231b175d9"
+  integrity sha512-Tm4RPrrIZbnqDKAvX+/4M+zovEReiKlEXWDzG4iwtpL9X34MJY+D5LnQPH/+eghe8DLlAVshHAJZAZWBGhkguw==
+
 normalize-package-data@^2.3.0, normalize-package-data@^2.3.2, normalize-package-data@^2.3.4, normalize-package-data@^2.3.5:
   version "2.5.0"
   resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8"
-- 
GitLab