Skip to content

Commit 124c582

Browse files
authored
[TOOLS-4855] Secondary backend code refactoring (#34)
* refactoring : common base-service added, etc. * fix : SSL cert logic modified for testing * fix : serve-web-manager.js moved into tools directory * fix : script cmd name changed
1 parent 4a45785 commit 124c582

33 files changed

+1014
-804
lines changed

apps/api-server/src/broker/broker.service.ts

Lines changed: 50 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CmsHttpsClientService } from '@cms-https-client/cms-https-client.service';
2-
import { HandleBrokerErrors, checkCmsTokenError } from '@common';
2+
import { BaseService, HandleBrokerErrors } from '@common';
33
import { BrokerError } from '@error/broker/broker-error';
44
import { HostService } from '@host';
55
import { Injectable } from '@nestjs/common';
@@ -23,26 +23,24 @@ import {
2323
* @since 1.0.0
2424
*/
2525
@Injectable()
26-
export class BrokerService {
26+
export class BrokerService extends BaseService {
2727
constructor(
28-
private readonly hostService: HostService,
29-
private readonly cmsClient: CmsHttpsClientService
30-
) {}
28+
protected readonly hostService: HostService,
29+
protected readonly cmsClient: CmsHttpsClientService
30+
) {
31+
super(hostService, cmsClient);
32+
}
3133

3234
@HandleBrokerErrors()
3335
async getBrokers(userId: string, hostUid: string) {
34-
const host = await this.hostService.findHostInternal(userId, hostUid);
35-
const url = `https://${host.address}:${host.port}/cm_api`;
36-
const body: BaseCmsRequest = {
36+
const cmsRequest: BaseCmsRequest = {
3737
task: 'getbrokersinfo',
38-
token: host.token ? host.token : '',
3938
};
40-
const response = await this.cmsClient.postAuthenticated<
41-
BaseCmsRequest,
42-
GetBrokersInfoCmsResponse
43-
>(url, body);
44-
45-
checkCmsTokenError(response);
39+
const response = await this.executeCmsRequest<BaseCmsRequest, GetBrokersInfoCmsResponse>(
40+
userId,
41+
hostUid,
42+
cmsRequest
43+
);
4644

4745
if (response.status !== 'success') {
4846
throw BrokerError.GetBrokersFailed();
@@ -52,20 +50,16 @@ export class BrokerService {
5250

5351
@HandleBrokerErrors()
5452
async stopBroker(userId: string, hostUid: string, bname: string): Promise<BaseCmsResponse> {
55-
const host = await this.hostService.findHostInternal(userId, hostUid);
56-
const url = `https://${host.address}:${host.port}/cm_api`;
57-
const body: HandleBrokerCmsRequest = {
53+
const cmsRequest: HandleBrokerCmsRequest = {
5854
task: 'broker_stop',
59-
token: host.token ? host.token : '',
6055
bname: bname,
6156
};
6257

63-
const response = await this.cmsClient.postAuthenticated<
64-
HandleBrokerCmsRequest,
65-
BaseCmsResponse
66-
>(url, body);
67-
68-
checkCmsTokenError(response);
58+
const response = await this.executeCmsRequest<HandleBrokerCmsRequest, BaseCmsResponse>(
59+
userId,
60+
hostUid,
61+
cmsRequest
62+
);
6963

7064
if (response.status !== 'success') {
7165
throw BrokerError.BrokerStopFailed();
@@ -75,20 +69,16 @@ export class BrokerService {
7569

7670
@HandleBrokerErrors()
7771
async startBroker(userId: string, hostUid: string, bname: string): Promise<BaseCmsResponse> {
78-
const host = await this.hostService.findHostInternal(userId, hostUid);
79-
const url = `https://${host.address}:${host.port}/cm_api`;
80-
const body: HandleBrokerCmsRequest = {
72+
const cmsRequest: HandleBrokerCmsRequest = {
8173
task: 'broker_start',
82-
token: host.token ? host.token : '',
8374
bname: bname,
8475
};
8576

86-
const response = await this.cmsClient.postAuthenticated<
87-
HandleBrokerCmsRequest,
88-
BaseCmsResponse
89-
>(url, body);
90-
91-
checkCmsTokenError(response);
77+
const response = await this.executeCmsRequest<HandleBrokerCmsRequest, BaseCmsResponse>(
78+
userId,
79+
hostUid,
80+
cmsRequest
81+
);
9282

9383
if (response.status !== 'success') {
9484
throw BrokerError.BrokerStartFailed();
@@ -98,30 +88,27 @@ export class BrokerService {
9888

9989
@HandleBrokerErrors()
10090
async restartBroker(userId: string, hostUid: string, bname: string): Promise<boolean> {
101-
const host = await this.hostService.findHostInternal(userId, hostUid);
102-
const url = `https://${host.address}:${host.port}/cm_api`;
10391
const stopRequest: HandleBrokerCmsRequest = {
10492
task: 'broker_stop',
105-
token: host.token ? host.token : '',
10693
bname: bname,
10794
};
10895

109-
const response = await this.cmsClient.postAuthenticated<
110-
HandleBrokerCmsRequest,
111-
BaseCmsResponse
112-
>(url, stopRequest);
113-
if (response.status === 'success') {
96+
const stopResponse = await this.executeCmsRequest<HandleBrokerCmsRequest, BaseCmsResponse>(
97+
userId,
98+
hostUid,
99+
stopRequest
100+
);
101+
if (stopResponse.status === 'success') {
114102
const startRequest: HandleBrokerCmsRequest = {
115103
task: 'broker_start',
116-
token: host.token ? host.token : '',
117104
bname: bname,
118105
};
119106

120-
const response = await this.cmsClient.postAuthenticated<
107+
const startResponse = await this.executeCmsRequest<
121108
HandleBrokerCmsRequest,
122109
BaseCmsResponse
123-
>(url, startRequest);
124-
if (response.status === 'success') {
110+
>(userId, hostUid, startRequest);
111+
if (startResponse.status === 'success') {
125112
return true;
126113
} else {
127114
throw BrokerError.BrokerStartFailed();
@@ -146,43 +133,33 @@ export class BrokerService {
146133
hostUid: string,
147134
bname: string
148135
): Promise<GetBrokerStatusClientResponse> {
149-
const host = await this.hostService.findHostInternal(userId, hostUid);
150-
const url = `https://${host.address}:${host.port}/cm_api`;
151-
const body: GetBrokerStatusCmsRequest = {
136+
const cmsRequest: GetBrokerStatusCmsRequest = {
152137
task: 'getbrokerstatus',
153-
token: host.token ? host.token : '',
154138
bname: bname,
155139
};
156140

157-
const response = await this.cmsClient.postAuthenticated<
141+
const response = await this.executeCmsRequest<
158142
GetBrokerStatusCmsRequest,
159143
GetBrokerStatusCmsResponse
160-
>(url, body);
161-
162-
checkCmsTokenError(response);
144+
>(userId, hostUid, cmsRequest);
163145

164146
if (response.status === 'success') {
165-
const { __EXEC_TIME, note, status, task, ...dataOnly } = response;
166-
return dataOnly;
147+
return this.extractDomainData(response);
167148
}
168149

169150
throw BrokerError.GetBrokersFailed({ response });
170151
}
171152

172153
@HandleBrokerErrors()
173154
async stopAllBrokers(userId: string, hostUid: string): Promise<boolean> {
174-
const host = await this.hostService.findHostInternal(userId, hostUid);
175-
const url = `https://${host.address}:${host.port}/cm_api`;
176-
const body = {
155+
const cmsRequest: BaseCmsRequest = {
177156
task: 'stopbroker',
178-
token: host.token ? host.token : '',
179157
};
180-
const response: BaseCmsResponse = await this.cmsClient.postAuthenticated<
181-
BaseCmsRequest,
182-
BaseCmsResponse
183-
>(url, body);
184-
185-
checkCmsTokenError(response);
158+
const response = await this.executeCmsRequest<BaseCmsRequest, BaseCmsResponse>(
159+
userId,
160+
hostUid,
161+
cmsRequest
162+
);
186163

187164
if (response.status === 'success') {
188165
return true;
@@ -193,18 +170,14 @@ export class BrokerService {
193170

194171
@HandleBrokerErrors()
195172
async startAllBrokers(userId: string, hostUid: string): Promise<boolean> {
196-
const host = await this.hostService.findHostInternal(userId, hostUid);
197-
const url = `https://${host.address}:${host.port}/cm_api`;
198-
const body = {
173+
const cmsRequest: BaseCmsRequest = {
199174
task: 'startbroker',
200-
token: host.token ? host.token : '',
201175
};
202-
const response: BaseCmsResponse = await this.cmsClient.postAuthenticated<
203-
BaseCmsRequest,
204-
BaseCmsResponse
205-
>(url, body);
206-
207-
checkCmsTokenError(response);
176+
const response = await this.executeCmsRequest<BaseCmsRequest, BaseCmsResponse>(
177+
userId,
178+
hostUid,
179+
cmsRequest
180+
);
208181

209182
if (response.status === 'success') {
210183
return true;

0 commit comments

Comments
 (0)