-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
105 lines (88 loc) · 2.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const util = require('util');
const url = require('url');
const request = require('request-promise-native');
const logger = require('@hmcts/nodejs-logging').Logger.getLogger(__filename);
const createAosCase = (params, proxy) => {
params.eventId = 'testAosAwaiting';
return _createCase(params, proxy);
};
const createDnCase = (params, proxy) => {
params.eventId = 'testAwaitingDecreeNisi';
return _createCase(params, proxy);
};
const createDaCase = (params, proxy) => {
params.eventId = 'testAwaitingDecreeAbsolute';
return _createCase(params, proxy);
};
const createDaCaseInDaRequested = (params, proxy) => {
params.eventId = 'testRequestDa';
return _createCase(params, proxy)
};
function _createCase(params, proxy) {
return _createCaseForUser(params, proxy)
.then(createCaseResponse => {
logger.info(`Created case ${createCaseResponse.id}`);
return _updateCase(params, createCaseResponse.id, params.eventId, proxy);
})
.catch(error => {
logger.info(`Error creating case: ${util.inspect(error)}`);
throw error;
});
}
function _createCaseForUser(params, proxy) {
const uri = `${params.baseUrl}/casemaintenance/version/1/submit`;
const headers = {
Authorization: `Bearer ${params.authToken}`,
'Content-Type': 'application/json'
};
const options = {
uri,
headers,
body: params.caseData,
json: true
};
if (proxy) {
Object.assign(options, _setupProxy(proxy));
}
return request.post(options);
};
const _updateCase = (params, caseId, eventId, proxy) => {
logger.info(`Issuing event ${eventId} against case ${caseId}.`);
const baseUrl = params.baseUrl;
const uri = `${baseUrl}/casemaintenance/version/1/updateCase/${caseId}/${eventId}`;
const headers = {
Authorization: `Bearer ${params.authToken}`,
'Content-Type': 'application/json'
};
const options = {
uri,
headers,
body: {},
json: true
};
if (proxy) {
Object.assign(options, _setupProxy(proxy));
}
return request.post(options);
};
const _setupProxy = proxy => {
const proxyUrl = url.parse(proxy);
let proxyOptions = {};
if (proxyUrl.protocol.indexOf('socks') >= 0) {
proxyOptions = {
strictSSL: false,
agentClass: socksAgent,
socksHost: proxyUrl.hostname || 'localhost',
socksPort: proxyUrl.port || 9000
};
} else {
proxyOptions = { proxy: proxyUrl.href };
}
return proxyOptions;
};
module.exports = {
createAosCase,
createDnCase,
createDaCase,
createDaCaseInDaRequested
};