notifications-node-client 7.0.6 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,7 +9,7 @@
9
9
  - [x] I’ve used the pull request template
10
10
  - [ ] I’ve written unit tests for these changes
11
11
  - [ ] I’ve updated the documentation in
12
- - `DOCUMENTATION.md`
12
+ - [notifications-tech-docs repository](https://github.com/alphagov/notifications-tech-docs/blob/main/source/documentation/client_docs/_node.md)
13
13
  - `CHANGELOG.md`
14
14
  - [ ] I’ve bumped the version number in
15
15
  - `package.json`
package/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## 8.1.0 - 2024-05-09
2
+
3
+ * The `sendEmail` function can now be passed `oneClickUnsubscribeURL` as an optional argument.
4
+
5
+ ## 8.0.0 - 2023-12-27
6
+
7
+ * Remove the default `is_csv` boolean parameter from `prepareUpload`. This method now accepts a file and an options map with the following options. For more specific information please read the documentation.
8
+ * `filename` (string) - specify the document's filename upon download
9
+ * `confirm_email_before_download` (boolean) - require the user to enter their email address before the file can be downloaded.
10
+ * `retention_period` (string) - how long Notify should make the file available to the user for.
11
+
1
12
  ## 7.0.6 - 2023-11-13
2
13
 
3
14
  * Bump axios from 1.2.6 to 1.6.1
@@ -23,10 +23,11 @@ function NotifyClient() {
23
23
  * @param {Object} personalisation
24
24
  * @param {String} reference
25
25
  * @param {String} replyToId
26
+ * @param {String} oneClickUnsubscribeURL
26
27
  *
27
28
  * @returns {Object}
28
29
  */
29
- function createNotificationPayload(type, templateId, to, personalisation, reference, replyToId) {
30
+ function createNotificationPayload(type, templateId, to, personalisation, reference, replyToId, oneClickUnsubscribeURL) {
30
31
 
31
32
  var payload = {
32
33
  template_id: templateId
@@ -53,6 +54,10 @@ function createNotificationPayload(type, templateId, to, personalisation, refere
53
54
  payload.sms_sender_id = replyToId;
54
55
  }
55
56
 
57
+ if (oneClickUnsubscribeURL && type == 'email') {
58
+ payload.one_click_unsubscribe_url = oneClickUnsubscribeURL;
59
+ }
60
+
56
61
  return payload;
57
62
  }
58
63
 
@@ -144,16 +149,17 @@ Object.assign(NotifyClient.prototype, {
144
149
  */
145
150
  sendEmail: function (templateId, emailAddress, options) {
146
151
  options = options || {};
147
- var err = checkOptionsKeys(['personalisation', 'reference', 'emailReplyToId'], options)
152
+ var err = checkOptionsKeys(['personalisation', 'reference', 'emailReplyToId', 'oneClickUnsubscribeURL'], options)
148
153
  if (err) {
149
154
  return Promise.reject(err);
150
155
  }
151
156
  var personalisation = options.personalisation || undefined,
152
157
  reference = options.reference || undefined,
153
- emailReplyToId = options.emailReplyToId || undefined;
158
+ emailReplyToId = options.emailReplyToId || undefined,
159
+ oneClickUnsubscribeURL = options.oneClickUnsubscribeURL || undefined;
154
160
 
155
161
  return this.apiClient.post('/v2/notifications/email',
156
- createNotificationPayload('email', templateId, emailAddress, personalisation, reference, emailReplyToId));
162
+ createNotificationPayload('email', templateId, emailAddress, personalisation, reference, emailReplyToId, oneClickUnsubscribeURL));
157
163
  },
158
164
 
159
165
  /**
@@ -356,26 +362,18 @@ Object.assign(NotifyClient.prototype, {
356
362
  prepareUpload: function(fileData, options) {
357
363
  let data = {
358
364
  file: _check_and_encode_file(fileData, 2),
359
- is_csv: false,
365
+ filename: null,
360
366
  confirm_email_before_download: null,
361
367
  retention_period: null,
362
368
  }
363
369
 
364
370
  if (options !== undefined) {
365
- if (typeof(options) === 'boolean') {
366
- data.is_csv = options
367
- }
368
- else {
369
- if (options.isCsv !== undefined) {
370
- data.is_csv = options.isCsv;
371
- }
372
-
373
- data.confirm_email_before_download = options.confirmEmailBeforeDownload !== undefined ? options.confirmEmailBeforeDownload : null;
374
- data.retention_period = options.retentionPeriod || null
375
- }
371
+ data.filename = options.filename || null;
372
+ data.confirm_email_before_download = options.confirmEmailBeforeDownload !== undefined ? options.confirmEmailBeforeDownload : null;
373
+ data.retention_period = options.retentionPeriod || null;
376
374
  }
377
375
 
378
- return data
376
+ return data;
379
377
  },
380
378
 
381
379
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notifications-node-client",
3
- "version": "7.0.6",
3
+ "version": "8.1.0",
4
4
  "homepage": "https://docs.notifications.service.gov.uk/node.html",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,4 +36,3 @@
36
36
  "standard-markdown": "5.0.1"
37
37
  }
38
38
  }
39
-
@@ -25,7 +25,8 @@
25
25
  "properties": {
26
26
  "body": {"type": "string"},
27
27
  "from_email": {"type": "string", "format": "email_address"},
28
- "subject": {"type": "string"}
28
+ "subject": {"type": "string"},
29
+ "one_click_unsubscribe_url": {"type": ["string", "null"], "format": "uri"}
29
30
  },
30
31
  "required": ["body", "from_email", "subject"]
31
32
  },
@@ -36,6 +36,7 @@ describer('notification api with a live service', function () {
36
36
  let letterNotificationId;
37
37
  const personalisation = { name: 'Foo' };
38
38
  const clientRef = 'client-ref';
39
+ const oneClickUnsubscribeURL = 'https://www.example.com';
39
40
  const email = process.env.FUNCTIONAL_TEST_EMAIL;
40
41
  const phoneNumber = process.env.FUNCTIONAL_TEST_NUMBER;
41
42
  const letterContact = {
@@ -88,6 +89,22 @@ describer('notification api with a live service', function () {
88
89
  expect(response.data).to.be.jsonSchema(postEmailNotificationResponseJson);
89
90
  response.data.content.body.should.equal('Hello Foo\r\n\r\nFunctional test help make our world a better place');
90
91
  response.data.content.subject.should.equal('Functional Tests are good');
92
+ should.equal(response.data.content.one_click_unsubscribe_url, null);
93
+ response.data.reference.should.equal(clientRef);
94
+ emailNotificationId = response.data.id;
95
+ })
96
+ });
97
+
98
+ it('send email notification with unsubscribe link', () => {
99
+ var postEmailNotificationResponseJson = require('./schemas/v2/POST_notification_email_response.json'),
100
+ options = {personalisation: personalisation, reference: clientRef, oneClickUnsubscribeURL: oneClickUnsubscribeURL};
101
+
102
+ return notifyClient.sendEmail(emailTemplateId, email, options).then((response) => {
103
+ response.status.should.equal(201);
104
+ expect(response.data).to.be.jsonSchema(postEmailNotificationResponseJson);
105
+ response.data.content.body.should.equal('Hello Foo\r\n\r\nFunctional test help make our world a better place');
106
+ response.data.content.subject.should.equal('Functional Tests are good');
107
+ response.data.content.one_click_unsubscribe_url.should.equal(oneClickUnsubscribeURL);
91
108
  response.data.reference.should.equal(clientRef);
92
109
  emailNotificationId = response.data.id;
93
110
  })
@@ -92,18 +92,19 @@ describe('notification api', () => {
92
92
  });
93
93
  });
94
94
 
95
- it('should send an email with document upload', () => {
96
- let email = 'dom@example.com',
95
+ it('should send an email with a one click unsubscribe URL', () => {
96
+
97
+ let email = 'me@example.com',
97
98
  templateId = '123',
98
99
  options = {
99
- personalisation: {documents:
100
- notifyClient.prepareUpload(Buffer.from("%PDF-1.5 testpdf"))
101
- },
100
+ personalisation: {foo: 'bar'},
101
+ oneClickUnsubscribeURL: '456',
102
102
  },
103
103
  data = {
104
104
  template_id: templateId,
105
105
  email_address: email,
106
106
  personalisation: options.personalisation,
107
+ one_click_unsubscribe_url: options.oneClickUnsubscribeURL
107
108
  };
108
109
 
109
110
  notifyAuthNock
@@ -113,16 +114,15 @@ describe('notification api', () => {
113
114
  return notifyClient.sendEmail(templateId, email, options)
114
115
  .then((response) => {
115
116
  expect(response.status).to.equal(200);
116
- expect(response.config.data).to.include('"is_csv":false');
117
117
  });
118
118
  });
119
119
 
120
- it('should send an email with CSV document upload', () => {
120
+ it('should send an email with document upload', () => {
121
121
  let email = 'dom@example.com',
122
122
  templateId = '123',
123
123
  options = {
124
124
  personalisation: {documents:
125
- notifyClient.prepareUpload(Buffer.from("a,b"), true)
125
+ notifyClient.prepareUpload(Buffer.from("%PDF-1.5 testpdf"))
126
126
  },
127
127
  },
128
128
  data = {
@@ -138,16 +138,15 @@ describe('notification api', () => {
138
138
  return notifyClient.sendEmail(templateId, email, options)
139
139
  .then((response) => {
140
140
  expect(response.status).to.equal(200);
141
- expect(response.config.data).to.include('"is_csv":true');
142
141
  });
143
142
  });
144
143
 
145
- it('should send an email with non CSV document upload', () => {
144
+ it('should send an email with a custom filename', () => {
146
145
  let email = 'dom@example.com',
147
146
  templateId = '123',
148
147
  options = {
149
148
  personalisation: {documents:
150
- notifyClient.prepareUpload(Buffer.from("%PDF-1.5 testpdf"), false)
149
+ notifyClient.prepareUpload(Buffer.from("a,b"), {filename: 'report.csv'})
151
150
  },
152
151
  },
153
152
  data = {
@@ -163,7 +162,7 @@ describe('notification api', () => {
163
162
  return notifyClient.sendEmail(templateId, email, options)
164
163
  .then((response) => {
165
164
  expect(response.status).to.equal(200);
166
- expect(response.config.data).to.include('"is_csv":false');
165
+ expect(response.config.data).to.include('"filename":"report.csv"');
167
166
  });
168
167
  });
169
168
 
@@ -194,8 +193,8 @@ describe('notification api', () => {
194
193
  expect(typeof(file)).to.equal('object')
195
194
  expect(Buffer.isBuffer(file)).to.equal(true);
196
195
  expect(
197
- notifyClient.prepareUpload(file, true)
198
- ).contains({file: 'MSwyLDMKYSxiLGMK', is_csv: true})
196
+ notifyClient.prepareUpload(file)
197
+ ).contains({file: 'MSwyLDMKYSxiLGMK'})
199
198
  });
200
199
  it('should accept files as strings (from fs.readFile with an encoding)', () => {
201
200
  let fs = require('fs');
@@ -203,50 +202,36 @@ describe('notification api', () => {
203
202
  expect(typeof(file)).to.equal('string')
204
203
  expect(Buffer.isBuffer(file)).to.equal(false);
205
204
  expect(
206
- notifyClient.prepareUpload(file, true)
207
- ).contains({file: 'MSwyLDMKYSxiLGMK', is_csv: true})
208
- });
209
-
210
- it('should allow isCsv to be set with the old method (directly into options)', () => {
211
- let file = Buffer.alloc(2*1024*1024)
212
- expect(
213
- notifyClient.prepareUpload(file, true)
214
- ).contains({is_csv: true, confirm_email_before_download: null, retention_period: null})
215
- });
216
-
217
- it('should allow isCsv to be set as part of the options object', () => {
218
- let file = Buffer.alloc(2*1024*1024)
219
- expect(
220
- notifyClient.prepareUpload(file, {isCsv: true})
221
- ).contains({is_csv: true, confirm_email_before_download: null, retention_period: null})
222
- });
223
-
224
- it('should imply isCsv=false from empty options', () => {
225
- let file = Buffer.alloc(2*1024*1024)
226
- expect(
227
- notifyClient.prepareUpload(file, {})
228
- ).contains({is_csv: false, confirm_email_before_download: null, retention_period: null})
205
+ notifyClient.prepareUpload(file)
206
+ ).contains({file: 'MSwyLDMKYSxiLGMK'})
229
207
  });
230
208
 
231
209
  it('should allow send a file email confirmation to be disabled', () => {
232
210
  let file = Buffer.alloc(2*1024*1024)
233
211
  expect(
234
212
  notifyClient.prepareUpload(file, {confirmEmailBeforeDownload: false})
235
- ).contains({is_csv: false, confirm_email_before_download: false, retention_period: null})
213
+ ).contains({confirm_email_before_download: false, retention_period: null})
236
214
  });
237
215
 
238
216
  it('should allow send a file email confirmation to be set', () => {
239
217
  let file = Buffer.alloc(2*1024*1024)
240
218
  expect(
241
219
  notifyClient.prepareUpload(file, {confirmEmailBeforeDownload: true})
242
- ).contains({is_csv: false, confirm_email_before_download: true, retention_period: null})
220
+ ).contains({confirm_email_before_download: true, retention_period: null})
243
221
  });
244
222
 
245
223
  it('should allow custom retention periods to be set', () => {
246
224
  let file = Buffer.alloc(2*1024*1024)
247
225
  expect(
248
226
  notifyClient.prepareUpload(file, {retentionPeriod: "52 weeks"})
249
- ).contains({is_csv: false, confirm_email_before_download: null, retention_period: '52 weeks'})
227
+ ).contains({confirm_email_before_download: null, retention_period: '52 weeks'})
228
+ });
229
+
230
+ it('should allow custom filenames to be set', () => {
231
+ let file = Buffer.alloc(2*1024*1024)
232
+ expect(
233
+ notifyClient.prepareUpload(file, {filename: "report.csv"})
234
+ ).contains({confirm_email_before_download: null, filename: "report.csv"})
250
235
  });
251
236
  });
252
237