All files / src/Batch ABaseServantSoapBatch.ts

18.75% Statements 6/32
0% Branches 0/20
0% Functions 0/5
18.75% Lines 6/32

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 881x 1x 1x   1x 1x     1x                                                                                                                                                              
import ABatchNode from '@orchesty/nodejs-sdk/dist/lib/Batch/ABatchNode';
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 * as soap from 'soap';
import { log } from '../Connector/ABaseServantSoapConnector';
import ServantApplication from '../ServantApplication';
 
export default abstract class ABaseSoapBatch extends ABatchNode {
 
    // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
    protected async callSOAP<T extends object[]>(
        dto: BatchProcessDto,
        methodName: string,
        resultKey: string,
        args: object | null,
        lastRunKey: string | null = null,
        lastRunDayOffset: number|null = null,
    ): Promise<BatchProcessDto> {
        const app = this.getApplication<ServantApplication>();
        const appInstall = await this.getApplicationInstallFromProcess(dto);
        let body = args;
 
        if (lastRunKey) {
            const lastRun = await appInstall.getNonEncryptedSettings()[lastRunKey] ?? new Date(0).toISOString();
            const lastRunDate = new Date(lastRun);
            lastRunDate.setDate(lastRunDate.getDate() - (lastRunDayOffset ?? 0));
            body = {
                ...body,
                interval: {
                    from: lastRunDate.toISOString(),
                    to: new Date().toISOString(),
                },
            };
        }
 
        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),
                    ...body,
                }, async (er: unknown, res: IResult & T): Promise<void> => {
                    logger.info(client.lastRequest ?? '', {}, false);
                    if (er) {
                        reject(new OnRepeatException(60, 10, (er as Error).message));
                    }
 
                    const data = this.processDataAfterRequest(log(dto, res, resultKey));
 
                    if (lastRunKey) {
                        appInstall.addNonEncryptedSettings({
                            [lastRunKey]: new Date().toISOString(),
                        });
                        await this.getDbClient().getApplicationRepository().update(appInstall);
                    }
 
                    resolve(dto.setItemList(data ?? []));
                });
            }
        });
 
        return await promise as BatchProcessDto<T>;
    }
 
    protected processDataAfterRequest(data: object[]|undefined): object[] {
        return data ?? [];
    }
 
}
 
export interface IResult {
    result: {
        resultCode: number;
        resultString: string;
    };
}