All files / src/Connector ABaseServantSoapConnector.ts

18.91% Statements 7/37
0% Branches 0/26
0% Functions 0/5
18.91% Lines 7/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1031x 1x 1x     1x 1x     1x                                                                       1x                                                                                                                  
import AConnector from '@orchesty/nodejs-sdk/dist/lib/Connector/AConnector';
import OnRepeatException from '@orchesty/nodejs-sdk/dist/lib/Exception/OnRepeatException';
import logger from '@orchesty/nodejs-sdk/dist/lib/Logger/Logger';
import BatchProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/BatchProcessDto';
import ProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/ProcessDto';
import ResultCode from '@orchesty/nodejs-sdk/dist/lib/Utils/ResultCode';
import * as soap from 'soap';
import ServantApplication from '../ServantApplication';
 
export function log<T>(
    dto: BatchProcessDto | ProcessDto,
    res: IResult & T,
    resultKey: string,
): T {
    const data = (res as never)[resultKey] as T;
    const { result } = res;
 
    let resultText: string | undefined = '';
    let success: boolean | undefined = true;
    if (resultKey === 'importRequestResult') {
        success = res?.importRequestResult?.success;
        resultText = `${res?.importRequestResult?.result}: ${res?.importRequestResult?.infoText}`;
    }
 
    if (result.resultCode === 1 || result.resultCode === 2) {
        logger.error(
            `Request process failed. Message: [${resultText ?? dto.getHeader(result.resultString) ?? ''}]`,
            dto,
        );
 
        dto.setStopProcess(ResultCode.STOP_AND_FAILED, result.resultString);
    } else {
        logger.info(
            `Request successfully processed. Message: [${resultText ?? JSON.stringify(data) ?? ''}]`,
            dto,
        );
 
        if (!success) {
            dto.setStopProcess(ResultCode.STOP_AND_FAILED, resultText ?? JSON.stringify(data) ?? '');
        }
    }
 
    return data;
}
 
export default abstract class ABaseServantSoapConnector extends AConnector {
 
    protected async callSOAP<T>(
        dto: ProcessDto,
        methodName: string,
        resultKey: string,
        args: object | null,
        forForm = false,
    ): Promise<ProcessDto<T>> {
        const app = this.getApplication<ServantApplication>();
        const appInstall = await this.getApplicationInstallFromProcess(dto, forForm ? null : true);
 
        const url = app.getBaseUrl();
 
        let resolve: CallableFunction;
        let reject: CallableFunction;
        const promise = new Promise((res, rej) => {
            resolve = res;
            reject = rej;
        });
 
        soap.createClient(url, (err, client) => {
            if (err) {
                reject(new OnRepeatException(60, 10, (err as Error).message));
            } else {
                client[methodName]({
                    ...app.prepareArgs(appInstall),
                    ...args,
                }, (er: unknown, res: IResult & T): void => {
                    logger.info(client.lastRequest ?? '', {}, false);
                    if (er) {
                        reject(new OnRepeatException(60, 10, (er as Error).message));
                    }
 
                    const data = log(dto, res, resultKey);
 
                    resolve(dto.setNewJsonData<T>(data));
                });
            }
        });
 
        return await promise as ProcessDto<T>;
    }
 
}
 
export interface IResult {
    result: {
        resultCode: number;
        resultString: string;
    };
    importRequestResult?: {
        success: boolean;
        result: string;
        infoText: string;
    };
}