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 | 8x 8x 8x 8x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 12x 4x 8x 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 { DateTime } from 'luxon';
const LAST_RUN = 'lastRun';
export default abstract class AFapiListBatch<IInput, IOutput> extends ABatchNode {
protected abstract getUrl(): string;
public async processAction(dto: BatchProcessDto<IInput>): Promise<BatchProcessDto<IOutput>> {
let url = `${this.getUrl()}?limit=${await this.getLimit(dto)}&offset=${await this.getOffset(dto)}`;
Iif (await this.useLastRun(dto)) {
const lastRun = await this.getLastRun(dto);
if (lastRun) {
url = `${url}&last_modified_after=${lastRun}`;
}
}
const requestDto = await this.getApplication().getRequestDto(
dto,
await this.getApplicationInstallFromProcess(dto),
HttpMethods.GET,
url,
);
const responseDto = await this.getSender().send<IResponse<IOutput>>(requestDto, [200]);
const response = responseDto.getJsonBody();
const items = response[this.getUrl().replace('-', '_')];
dto.setItemList(items);
Iif (items.length === await this.getLimit(dto)) {
dto.setBatchCursor((Number(dto.getBatchCursor('1')) + 1).toString());
} else {
await this.setLastRun(dto);
}
return dto as unknown as Promise<BatchProcessDto<IOutput>>;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
protected async getLimit(dto: BatchProcessDto<IInput>): Promise<number> {
return 1_000;
}
protected async getOffset(dto: BatchProcessDto<IInput>): Promise<number> {
return (Number(dto.getBatchCursor('1')) - 1) * await this.getLimit(dto);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
protected async getLastRunKey(dto: BatchProcessDto<IInput>): Promise<string> {
return this.getUrl();
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
protected async useLastRun(dto: BatchProcessDto<IInput>): Promise<boolean> {
return false;
}
protected async getLastRun(dto: BatchProcessDto<IInput>): Promise<string | undefined> {
if (!await this.useLastRun(dto)) {
return undefined;
}
const applicationInstall = await this.getApplicationInstallFromProcess(dto);
const lastRun = applicationInstall.getNonEncryptedSettings()[LAST_RUN]?.[await this.getLastRunKey(dto)];
if (!lastRun) {
return undefined;
}
return DateTime.fromISO(lastRun).toFormat('yyyy-MM-dd HH:mm:ss');
}
protected async setLastRun(dto: BatchProcessDto<IInput>): Promise<void> {
Eif (!await this.useLastRun(dto)) {
return;
}
const applicationInstall = await this.getApplicationInstallFromProcess(dto);
applicationInstall.addNonEncryptedSettings({
[LAST_RUN]: {
[await this.getLastRunKey(dto)]: new Date().toISOString(),
},
});
await this.getDbClient().getApplicationRepository().update(applicationInstall);
}
}
type IResponse<IOutput> = Record<string, IOutput[]>;
|