All files / src/Batch AShoptetList.ts

94.59% Statements 35/37
77.77% Branches 14/18
75% Functions 3/4
94.59% Lines 35/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 103 104 105  12x   12x   12x 12x   12x               24x         8x 8x 8x 8x   8x 8x       8x   8x 8x 4x 4x 2x 2x 2x         8x 2x     8x 8x     8x 8x     8x             8x       8x   8x 4x   4x     8x             2x 2x                                          
import { ApplicationInstall } from '@orchesty/nodejs-sdk/dist/lib/Application/Database/ApplicationInstall';
import ABatchNode from '@orchesty/nodejs-sdk/dist/lib/Batch/ABatchNode';
import ResponseDto from '@orchesty/nodejs-sdk/dist/lib/Transport/Curl/ResponseDto';
import { HttpMethods } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import BatchProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/BatchProcessDto';
import { DateTime } from 'luxon';
import ShoptetPremiumApplication from '../ShoptetPremiumApplication';
 
export default abstract class AShoptetList<ResponseData> extends ABatchNode {
 
    protected abstract endpoint: string;
 
    protected abstract fromParamKey: string;
 
    protected abstract lastRunKey: string;
 
    protected lastRunOffset = 60; // seconds
 
    protected abstract processResult(responseDto: ResponseDto<ResponseData>, batchProcessDto: BatchProcessDto): IPaging;
 
    public async processAction(dto: BatchProcessDto<{ from: string }>): Promise<BatchProcessDto> {
        const { from } = dto.getJsonData();
        const appInstall = await this.getApplicationInstallFromProcess(dto);
        await this.saveInProgress(appInstall);
        const { page, dateFrom } = JSON.parse(dto.getBatchCursor('{ "page": 1, "dateFrom": null }')) as ICursor;
 
        let querySeparator = '?';
        Iif (this.endpoint.includes('?')) {
            querySeparator = '&';
        }
 
        let url = `${this.endpoint}${querySeparator}itemsPerPage=1000`;
 
        let creationTimeFrom = dateFrom ?? from;
        if (!creationTimeFrom) {
            const lastRun = appInstall.getNonEncryptedSettings()[this.lastRunKey];
            if (lastRun) {
                const lastRunWithOffset = DateTime.fromISO(lastRun).minus({ second: this.lastRunOffset }).toISO();
                Eif (lastRunWithOffset) {
                    creationTimeFrom = ShoptetPremiumApplication.shoptetDateISO(lastRunWithOffset);
                }
            }
        }
 
        if (!creationTimeFrom) {
            creationTimeFrom = this.getDefaultLastRun();
        }
 
        Eif (page) {
            url = `${url}&page=${page}`;
        }
 
        Eif (creationTimeFrom) {
            url = `${url}&${this.fromParamKey}=${creationTimeFrom}`;
        }
 
        const requestDto = await this.getApplication().getRequestDto(
            dto,
            appInstall,
            HttpMethods.GET,
            url,
        );
 
        const res = await this.getSender().send<ResponseData>(
            requestDto,
            { success: 200, stopAndFail: 422 },
        );
        const paginator = this.processResult(res, dto);
 
        if (paginator.pageCount > page) {
            dto.setBatchCursor(JSON.stringify({ page: page + 1, dateFrom: creationTimeFrom }));
        } else {
            await this.saveLastRunKey(appInstall);
        }
 
        return dto;
    }
 
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    protected async saveInProgress(appInstall: ApplicationInstall): Promise<void> {}
 
    protected async saveLastRunKey(appInstall: ApplicationInstall): Promise<void> {
        appInstall.addNonEncryptedSettings({ [this.lastRunKey]: new Date() });
        await this.getDbClient().getApplicationRepository().update(appInstall);
    }
 
    protected getDefaultLastRun(): string {
        return '';
    }
 
}
 
export interface IPaging {
    totalCount: number;
    page: number;
    pageCount: number;
    itemsOnPage: number;
    itemsPerPage: number;
}
 
export interface ICursor {
    page: number;
    dateFrom?: string;
}