All files / src/Batch FlexiBeeSimpleIterator.ts

96.29% Statements 26/27
75% Branches 6/8
100% Functions 2/2
96.29% Lines 26/27

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 7114x 14x       14x       98x   98x   98x     7x 7x     7x 7x 7x 1x     7x 7x 7x 2x     7x             7x 7x   7x 7x   7x 7x 7x     7x     7x                                  
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 FlexiBeeApplication from '../FexiBeeApplication';
 
export abstract class FlexiBeeSimpleIterator<T> extends ABatchNode {
 
    protected abstract endpoint: string;
 
    protected iterateOnly = false;
 
    protected pageSize = 100;
 
    protected relations: string[] = [];
 
    public async processAction(dto: BatchProcessDto): Promise<BatchProcessDto> {
        const appInstall = await this.getApplicationInstallFromProcess(dto);
        const app = this.getApplication<FlexiBeeApplication>();
 
        // eslint-disable-next-line
        const { lastUpdate } = dto.getJsonData() as any;
        let url = this.endpoint;
        if (lastUpdate) {
            url = `${url}/${encodeURI(`(lastUpdate > ${lastUpdate})`)}`;
        }
 
        const page = Number(dto.getBatchCursor('0'));
        url = `${url}.json?detail=full&add-row-count=true&start=${page * this.pageSize}&limit=${this.pageSize}`;
        if (this.relations.length) {
            url = `${url}&relations=${this.relations.join(',')}`;
        }
 
        const request = await app.getRequestDto(
            dto,
            appInstall,
            HttpMethods.GET,
            app.getUrl(appInstall, url),
        );
 
        const response = await this.getSender().send<Response>(request);
        const data = response.getJsonBody().winstrom;
        // eslint-disable-next-line
        const items = ((data as any)[this.endpoint] || []) as T[];
        await this.processItems(dto, items);
 
        const rows = Number(data['@rowCount']);
        const pages = Math.ceil(rows / this.pageSize);
        Iif (pages > (page + 1)) {
            dto.setBatchCursor(String(page + 1), this.iterateOnly);
        } else {
            dto.removeBatchCursor();
        }
 
        return dto.setItemList(items, this.iterateOnly);
    }
 
    protected async processItems(_dto: BatchProcessDto, _items: T[]): Promise<void> {
    }
 
}
 
/* eslint-disable @typescript-eslint/naming-convention */
 
interface Response {
    winstrom: {
        '@rowCount': string;
    };
}
 
/* eslint-enable @typescript-eslint/naming-convention */