UNPKG

1.8 kBJavaScriptView Raw
1const AppConfig = require('@src/model/app-config');
2
3class BeforeSendProcessor {
4 constructor(commandName, paramsObject, modelIntrospector, profile) {
5 this.params = modelIntrospector.operations.get(commandName).params;
6 this.definitions = modelIntrospector.definitions;
7 this.paramsObject = paramsObject;
8 this.profile = profile;
9 }
10
11 processAll() {
12 this.appendVendorId();
13 this.mapTestersEmails();
14 }
15
16 appendVendorId() {
17 const vendorId = AppConfig.getInstance().getVendorId(this.profile);
18 const nonBodyParam = this.params.find(p => p.name === 'vendorId');
19 if (nonBodyParam) {
20 this.paramsObject.vendorId = vendorId;
21 return;
22 }
23
24 const bodyParam = this.params.find(p => p.in === 'body');
25 if (bodyParam && bodyParam.required && bodyParam.schema && bodyParam.schema.$ref) {
26 const key = bodyParam.schema.$ref.split('/').pop();
27 const definition = this.definitions.get(key);
28 if (!definition.properties) return;
29 if (Object.keys(definition.properties).includes('vendorId')) {
30 this.paramsObject[bodyParam.name] = this.paramsObject[bodyParam.name] || {};
31 this.paramsObject[bodyParam.name].vendorId = vendorId;
32 }
33 }
34 }
35
36 mapTestersEmails() {
37 const hasTestersParam = this.params.find(p => p.in === 'body' && p.name === 'TestersRequest');
38 if (hasTestersParam) {
39 const { testersEmails } = this.paramsObject;
40 this.paramsObject.testersRequest = {
41 testers: testersEmails.map(email => ({ emailId: email }))
42 };
43
44 delete this.paramsObject.testersEmails;
45 }
46 }
47}
48
49module.exports = BeforeSendProcessor;