Skip to content

Commit db8e5c0

Browse files
More examples
1 parent d235ee5 commit db8e5c0

File tree

14 files changed

+136
-20
lines changed

14 files changed

+136
-20
lines changed

sandbox/controllers/Controller.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ class Controller {
1111
* payload will be an object consisting of a code and a payload. If not customized
1212
* send 200 and the payload as received in this method.
1313
*/
14+
Object.entries(payload.headers).forEach(([name, value]) => response.setHeader(name, String(value)));
1415
response.status(payload.code || 200);
16+
1517
const responsePayload = payload.payload !== undefined ? payload.payload : payload;
1618
if (responsePayload instanceof Object) {
1719
response.json(responsePayload);
1820
} else {
1921
response.end(responsePayload);
2022
}
23+
console.log('response:', response);
2124
}
2225

2326
static sendError(response, error) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"data": {
3+
"attributes": {
4+
"cost": 10.25,
5+
"lineItem": "Letter",
6+
"quantity": 22,
7+
"specificationId": "2WL5eYSWGzCHlGmzNxuqVusPxDg",
8+
"stockRemaining": 2000,
9+
"timestamp": "yesterday"
10+
},
11+
"type": "ManagementInformation"
12+
}
13+
}

sandbox/data/examples/createMI/requests/createMI.json renamed to sandbox/data/examples/createMI/requests/createMI_NOTFOUND.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cost": 10.25,
55
"lineItem": "Letter",
66
"quantity": 22,
7-
"specificationId": "da0b1495-c7cb-468c-9d81-07dee089d728",
7+
"specificationId": "123456",
88
"stockRemaining": 2000,
99
"timestamp": "2023-11-17T14:27:51.413Z"
1010
},
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"data": {
3+
"attributes": {
4+
"cost": 10.25,
5+
"lineItem": "Letter",
6+
"quantity": 22,
7+
"specificationId": "2WL5eYSWGzCHlGmzNxuqVusPxDg",
8+
"stockRemaining": 2000,
9+
"timestamp": "2023-11-17T14:27:51.413Z"
10+
},
11+
"type": "ManagementInformation"
12+
}
13+
}

sandbox/data/examples/createMI/responses/createMI.json renamed to sandbox/data/examples/createMI/responses/createMI_SUCCESS.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cost": 10.25,
55
"lineItem": "Letter",
66
"quantity": 22,
7-
"specificationId": "da0b1495-c7cb-468c-9d81-07dee089d728",
7+
"specificationId": "2WL5eYSWGzCHlGmzNxuqVusPxDg",
88
"stockRemaining": 2000,
99
"timestamp": "2023-11-17T14:27:51.413Z"
1010
},

sandbox/services/DataService.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* eslint-disable no-unused-vars */
2+
const fs = require('fs/promises');
3+
24
const Service = require('./Service');
5+
const ResponseProvider = require('../utils/ResponseProvider');
6+
37

48
/**
59
* Fetch a data file
@@ -12,11 +16,19 @@ const Service = require('./Service');
1216
const getDataId = ({ id, xRequestID, xCorrelationID }) => new Promise(
1317
async (resolve, reject) => {
1418
try {
15-
resolve(Service.successResponse({
16-
id,
17-
xRequestID,
18-
xCorrelationID,
19-
}));
19+
const responseData = await ResponseProvider.getLetterDataResponse(id);
20+
if ((responseData.responseCode) >= 300 && (responseData.responseCode < 400))
21+
{
22+
resolve(Service.successResponse(null, responseData.responseCode, { Location: responseData.responsePath }));
23+
}else{
24+
const content = await fs.readFile(responseData.responsePath);
25+
const fileData = JSON.parse(content);
26+
resolve(Service.successResponse({
27+
xRequestID,
28+
xCorrelationID,
29+
data: fileData,
30+
}, responseData.responseCode));
31+
}
2032
} catch (e) {
2133
reject(Service.rejectResponse(
2234
e.message || 'Invalid input',
@@ -36,11 +48,13 @@ const getDataId = ({ id, xRequestID, xCorrelationID }) => new Promise(
3648
const headDataId = ({ id, xRequestID, xCorrelationID }) => new Promise(
3749
async (resolve, reject) => {
3850
try {
39-
resolve(Service.successResponse({
40-
id,
41-
xRequestID,
42-
xCorrelationID,
43-
}));
51+
responseData = await ResponseProvider.getLetterDataResponse(id);
52+
if ((responseData.responseCode >= 300) && (responseData.responseCode < 400))
53+
{
54+
resolve(Service.successResponse(null, 200));
55+
}else{
56+
resolve(Service.successResponse(null, 404));
57+
}
4458
} catch (e) {
4559
reject(Service.rejectResponse(
4660
e.message || 'Invalid input',

sandbox/services/MiService.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable no-unused-vars */
2+
const fs = require('fs/promises');
23
const Service = require('./Service');
4+
const ResponseProvider = require('../utils/ResponseProvider');
35

46
/**
57
* Create a new MI record
@@ -9,14 +11,18 @@ const Service = require('./Service');
911
* xCorrelationID String An optional ID which you can use to track transactions across multiple systems. It can take any value, but we recommend avoiding `.` characters. If not provided in the request, NHS Notify will default to a system generated ID in its place. The ID will be returned in a response header. (optional)
1012
* returns createMI_201_response
1113
* */
12-
const createMI = ({ xRequestID, createMIRequest, xCorrelationID }) => new Promise(
14+
const createMI = ({ xRequestID, body, xCorrelationID }) => new Promise(
1315
async (resolve, reject) => {
1416
try {
17+
const responseData = await ResponseProvider.postMIResponse(body);
18+
const content = await fs.readFile(responseData.responsePath);
19+
const fileData = JSON.parse(content);
20+
1521
resolve(Service.successResponse({
1622
xRequestID,
17-
createMIRequest,
1823
xCorrelationID,
19-
}));
24+
data: fileData,
25+
}, responseData.responseCode));
2026
} catch (e) {
2127
reject(Service.rejectResponse(
2228
e.message || 'Invalid input',

sandbox/services/Service.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ class Service {
33
return { error, code };
44
}
55

6-
static successResponse(payload, code = 200) {
7-
return { payload, code };
6+
static successResponse(payload, code = 200, headers = {}) {
7+
return { payload, code, headers };
88
}
99
}
1010

sandbox/utils/ResponseProvider.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,27 @@ async function patchLettersResponse(request) {
116116
return await mapExampleResponse(request, patchLettersFileMap);
117117
}
118118

119+
async function postMIResponse(request) {
120+
const postMIFileMap = {
121+
'data/examples/createMI/requests/createMI_SUCCESS.json': {responsePath: 'data/examples/createMI/responses/createMI_SUCCESS.json', responseCode: 200},
122+
'data/examples/createMI/requests/createMI_INVALID.json': {responsePath:'data/examples/errors/responses/badRequest.json',responseCode: 400},
123+
'data/examples/createMI/requests/createMI_NOTFOUND.json': {responsePath:'data/examples/errors/responses/resourceNotFound.json',responseCode: 404},
124+
};
125+
return await mapExampleResponse(request, postMIFileMap);
126+
}
127+
128+
async function getLetterDataResponse(id) {
129+
const getLetterDataFileMap = {
130+
'2AL5eYSWGzCHlGmzNxuqVusPxDg' : {responsePath: 'http://example.com', responseCode: 303},
131+
'2WL5eYSWGzCHlGmzNxuqVusPxDg' : {responsePath: 'data/examples/errors/responses/resourceNotFound.json', responseCode: 404},
132+
};
133+
return mapExampleGetResponse(id, getLetterDataFileMap);
134+
}
135+
119136
module.exports = {
120137
getLetterStatusResponse,
121138
getLettersResponse,
122139
patchLettersResponse,
140+
postMIResponse,
141+
getLetterDataResponse
123142
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"data": {
3+
"attributes": {
4+
"cost": 10.25,
5+
"lineItem": "Letter",
6+
"quantity": 22,
7+
"specificationId": "2WL5eYSWGzCHlGmzNxuqVusPxDg",
8+
"stockRemaining": 2000,
9+
"timestamp": "yesterday"
10+
},
11+
"type": "ManagementInformation"
12+
}
13+
}

0 commit comments

Comments
 (0)