Check the connection
MODIFY
src/integration/asset/schneider/AssetSchneiderIntegration.ts
src/integration/asset/schneider/AssetSchneiderIntegration.ts
+import axios from 'axios';
+import BackendError from '../../../exception/BackendError';
+import { ServerAction } from '../../../types/Server';
import { AssetConnectionSetting, AssetSetting } from '../../../types/Setting';
-
+import Constants from '../../../utils/Constants';
import AssetIntegration from '../AssetIntegration';
+
const MODULE_NAME = 'AssetSchneiderIntegration';
export default class AssetSchneiderIntegration extends AssetIntegration<AssetSetting> {
@@ -11,5 +15,34 @@ export default class AssetSchneiderIntegration extends AssetIntegration<AssetSet
public async checkConnection() {
// TODO: Check connection provided in 'this.connection' and throw an exception in case of failure
+ // Check if connection is initialized
+ this.isAssetConnectionInitialized();
+ // Set credentials params
+ const params = new URLSearchParams();
+ params.append('grant_type', 'password');
+ params.append('username', this.connection.connection.user);
+ params.append('password', this.connection.connection.password);
+ // Send credentials to get the token
+ await axios.post(`${this.connection.url}/GetToken`, params,
+ {
+ headers: this.buildFormHeaders()
+ });
+ }
+
+ private isAssetConnectionInitialized(): void {
+ if (!this.connection) {
+ throw new BackendError({
+ source: Constants.CENTRAL_SERVER,
+ module: MODULE_NAME, method: 'isAssetConnectionInitialized',
+ action: ServerAction.CHECK_CONNECTION,
+ message: 'No connection provided'
+ });
+ }
+ }
+
+ private buildFormHeaders(): object {
+ return {
+ 'Content-Type': 'application/x-www-form-urlencoded'
+ }
}
}

MODIFY
src/server/rest/service/AssetService.ts
src/server/rest/service/AssetService.ts
@@ -55,8 +55,12 @@ export default class AssetService {
try {
// Check connection
await assetImpl.checkConnection();
+ // Create success response
+ const response = {
+ isConnectionValid: true
+ }
// Success
- res.json(Object.assign({ connectionIsValid: true }, Constants.REST_RESPONSE_SUCCESS))
+ res.json(Object.assign(response, Constants.REST_RESPONSE_SUCCESS))
} catch (error) {
// KO
Logging.logError({
@@ -67,7 +71,12 @@ export default class AssetService {
action: action,
detailedMessages: { error: error.message, stack: error.stack }
});
- res.json(Object.assign({ connectionIsValid: false }, Constants.REST_RESPONSE_SUCCESS));
+ // Create fail response
+ const response = {
+ connectionIsValid: false,
+ statusErrorCode: error.response.status
+ }
+ res.json(Object.assign(response));
}
next();
}

Check the connection