1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const async = require('async');
|
17 |
|
18 | const BaseService = require('./BaseService');
|
19 | const { CollectionHook, EndpointFieldNames, HTTPMethod, OperationType } = require('./Constants');
|
20 | const { Endpoints, getObjectByOmitting, isNullOrUndefined, readFile } = require('./Utils');
|
21 |
|
22 | const endpointRequestsTimeout = 60 * 1000;
|
23 |
|
24 | class BusinessLogicService extends BaseService {
|
25 | getCommonCode(envId, name, fields, done) {
|
26 | let endpoint = Endpoints.commonCode(this.cliManager.config.defaultSchemaVersion, envId, name);
|
27 | if (fields) endpoint += '?fields=' + encodeURIComponent(fields);
|
28 | this.cliManager.sendRequest({ endpoint, method: HTTPMethod.GET }, done);
|
29 | }
|
30 |
|
31 | createCommonCode(source = {}, envId, done) {
|
32 | const data = getObjectByOmitting(source, ['codeFile']);
|
33 |
|
34 | async.series([
|
35 | (next) => {
|
36 | BusinessLogicService._getCodeContent(source, source.name, (err, code) => {
|
37 | if (err) {
|
38 | return next(err);
|
39 | }
|
40 |
|
41 | data.code = code;
|
42 | next();
|
43 | });
|
44 | },
|
45 | (next) => {
|
46 | const endpoint = Endpoints.commonCode(this.cliManager.config.defaultSchemaVersion, envId);
|
47 | this.cliManager.sendRequest({ endpoint, data, method: HTTPMethod.POST }, next);
|
48 | }
|
49 | ], (err, results) => {
|
50 | if (err) {
|
51 | return done(err);
|
52 | }
|
53 |
|
54 | done(null, results.pop());
|
55 | });
|
56 | }
|
57 |
|
58 | updateCommonCode(name, source, envId, done) {
|
59 | const data = getObjectByOmitting(source, ['codeFile']);
|
60 |
|
61 | async.series([
|
62 | (next) => {
|
63 | BusinessLogicService._getCodeContent(source, name, (err, code) => {
|
64 | if (err) {
|
65 | return next(err);
|
66 | }
|
67 |
|
68 | data.code = code;
|
69 | next();
|
70 | });
|
71 | },
|
72 | (next) => {
|
73 | const endpoint = Endpoints.commonCode(this.cliManager.config.defaultSchemaVersion, envId, name);
|
74 | this.cliManager.sendRequest({ endpoint, data, method: HTTPMethod.PUT }, next);
|
75 | }
|
76 | ], (err, results) => {
|
77 | if (err) {
|
78 | return done(err);
|
79 | }
|
80 |
|
81 | done(null, results.pop());
|
82 | });
|
83 | }
|
84 |
|
85 | getCollectionHooks(envId, done) {
|
86 | let endpoint = Endpoints.collectionHooks(this.cliManager.config.defaultSchemaVersion, envId);
|
87 | endpoint += '?fields=["sdkHandlerName","host", "code"]';
|
88 | this.cliManager.sendRequest({ endpoint }, done);
|
89 | }
|
90 |
|
91 | createOrUpdateHook(source = {}, envId, collName, hookName, done) {
|
92 | const data = getObjectByOmitting(source, ['codeFile', 'type', 'service', 'serviceEnvironment', 'handlerName']);
|
93 |
|
94 | async.series([
|
95 | (next) => {
|
96 | BusinessLogicService._getCodeContent(source, hookName, (err, code) => {
|
97 | if (err) {
|
98 | return next(err);
|
99 | }
|
100 |
|
101 | if (isNullOrUndefined(code)) {
|
102 | const userFriendlyHookName = Object.keys(CollectionHook).find(key => CollectionHook[key] === hookName);
|
103 | code = `function ${userFriendlyHookName}(request, response, modules) {\n response.continue();\n}`;
|
104 | }
|
105 |
|
106 | data.code = code;
|
107 | next();
|
108 | });
|
109 | },
|
110 | (next) => {
|
111 | const endpoint = Endpoints.hooks(this.cliManager.config.defaultSchemaVersion, envId, collName, hookName);
|
112 | this.cliManager.sendRequest({ endpoint, data, method: HTTPMethod.PUT }, next);
|
113 | }
|
114 | ], (err, results) => {
|
115 | if (err) {
|
116 | return done(err);
|
117 | }
|
118 |
|
119 | done(null, results.pop());
|
120 | });
|
121 | }
|
122 |
|
123 | createEndpoint(source = {}, envId, done) {
|
124 | this._createOrUpdateEndpoint(source, envId, OperationType.CREATE, null, done);
|
125 | }
|
126 |
|
127 | updateEndpoint(name, source = {}, envId, done) {
|
128 | this._createOrUpdateEndpoint(source, envId, OperationType.UPDATE, name, done);
|
129 | }
|
130 |
|
131 | _createOrUpdateEndpoint(source = {}, envId, operationType, name, done) {
|
132 | const data = getObjectByOmitting(source, ['codeFile', 'type', 'service', 'serviceEnvironment', 'handlerName']);
|
133 |
|
134 | async.series([
|
135 | (next) => {
|
136 | BusinessLogicService._getCodeContent(source, source.name, (err, code) => {
|
137 | if (err) {
|
138 | return next(err);
|
139 | }
|
140 |
|
141 | if (isNullOrUndefined(code)) {
|
142 | code = 'function onRequest(request, response, modules) {\n response.continue();\n}';
|
143 | }
|
144 |
|
145 | data.code = code;
|
146 | next();
|
147 | });
|
148 | },
|
149 | (next) => {
|
150 | const endpoint = Endpoints.endpoints(this.cliManager.config.defaultSchemaVersion, envId, name);
|
151 | const method = operationType === OperationType.CREATE ? HTTPMethod.POST : HTTPMethod.PUT;
|
152 | this.cliManager.sendRequest({ endpoint, data, method, timeout: endpointRequestsTimeout }, next);
|
153 | }
|
154 | ], (err, results) => {
|
155 | if (err) {
|
156 | return done(err);
|
157 | }
|
158 |
|
159 | done(null, results.pop());
|
160 | });
|
161 | }
|
162 |
|
163 | getEndpoints(envId, name, done) {
|
164 | const endpoint = Endpoints.endpoints(this.cliManager.config.defaultSchemaVersion, envId, name);
|
165 | const fields = EndpointFieldNames.reduce((accumulator, currentField, index, arr) => {
|
166 | let currentValue = `"${currentField}"`;
|
167 | if (index < arr.length - 1) {
|
168 | currentValue += ',';
|
169 | }
|
170 |
|
171 | accumulator += currentValue;
|
172 | return accumulator;
|
173 | }, '');
|
174 |
|
175 | const query = isNullOrUndefined(name) ? { fields: `[${fields}]` } : null;
|
176 | this.cliManager.sendRequest({ endpoint, query, method: HTTPMethod.GET, timeout: endpointRequestsTimeout }, done);
|
177 | }
|
178 |
|
179 | static _getCodeContent(source, name, done) {
|
180 | const inlineCodeIsSet = isNullOrUndefined(source.code) === false;
|
181 | const codeFileIsSet = isNullOrUndefined(source.codeFile) === false;
|
182 | if (!inlineCodeIsSet && !codeFileIsSet) {
|
183 | return setImmediate(done);
|
184 | }
|
185 |
|
186 | if (inlineCodeIsSet) {
|
187 | return setImmediate(() => { done(null, source.code); });
|
188 | }
|
189 |
|
190 | readFile(source.codeFile, (err, content) => {
|
191 | if (err) {
|
192 | return done(err);
|
193 | }
|
194 |
|
195 | done(null, content.toString());
|
196 | });
|
197 | }
|
198 | }
|
199 |
|
200 | module.exports = BusinessLogicService;
|