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 | 16x 16x 16x 16x 16x 64x 64x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 3x 3x 4x 4x 4x 4x | import ABatchNode from '@orchesty/nodejs-sdk/dist/lib/Batch/ABatchNode';
import { HttpMethods } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import BatchProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/BatchProcessDto';
import FormData from 'form-data';
import { SUCCESS } from '../Connector/ABaseConnector';
export abstract class ABaseBatch<T> extends ABatchNode {
public constructor(private readonly useInForm = false) {
super();
}
protected abstract getMethod(): string;
protected abstract processOutputData(dto: BatchProcessDto, body: object): Promise<BatchProcessDto>;
protected abstract hasNextPage(jsonBody: object): boolean;
public async processAction(dto: BatchProcessDto<T>): Promise<BatchProcessDto> {
let page = Number(dto.getBatchCursor('1'));
const appInstall = await this.getApplicationInstallFromProcess(dto, this.useInForm ? null : true);
const lastRun = appInstall.getNonEncryptedSettings()[this.getLastRunKey()] as string | undefined;
const req = await this.getApplication().getRequestDto(
dto,
appInstall,
HttpMethods.POST,
undefined,
this.prepareBody(this.getMethod(), await this.getParameters(dto, page, this.prepareLastRun(lastRun))),
);
const resp = await this.getSender().send<IResponse>(req, [200]);
const { status, ...jsonBody } = resp.getJsonBody();
Iif (status !== SUCCESS) {
throw new Error(`Request failed. Reason: ${resp.getBody()}`);
}
await this.processOutputData(dto, jsonBody);
const hasNextPage = this.hasNextPage(jsonBody);
Iif (hasNextPage) {
page++;
dto.setBatchCursor(String(page));
} else {
dto.removeBatchCursor();
appInstall.addNonEncryptedSettings({ [this.getLastRunKey()]: this.getNewLastRun(jsonBody, lastRun) });
const repo = this.getDbClient().getApplicationRepository();
await repo.update(appInstall);
}
return dto;
}
protected getLastRunKey(): string {
return this.getMethod();
}
protected prepareLastRun(lastRun: string|null|undefined): Date|number|undefined {
return lastRun ? new Date(lastRun) : undefined;
}
protected getNewLastRun(_jsonBody: object, _lastRun?: string): string {
return new Date().toISOString();
}
// eslint-disable-next-line @typescript-eslint/require-await
protected async getParameters(_dto: BatchProcessDto<T>, _page: number, _lastRun?: Date|number): Promise<object> {
return [];
}
protected prepareBody(method: string, parameters: object): FormData {
const body = new FormData();
body.append('method', method);
body.append('parameters', JSON.stringify(parameters));
return body;
}
}
export interface IResponse {
status: string;
}
|