All files / src/Batch ShoptetGetProductChangesList.ts

95.83% Statements 23/24
50% Branches 1/2
100% Functions 6/6
95.83% Lines 23/24

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      12x 12x 12x 12x   12x   12x   12x   12x   12x     12x       4x   4x 4x         4x       4x       1x       4x 4x       2x     2x 2x   2x 2x                                  
import { ApplicationInstall } from '@orchesty/nodejs-sdk/dist/lib/Application/Database/ApplicationInstall';
import ResponseDto from '@orchesty/nodejs-sdk/dist/lib/Transport/Curl/ResponseDto';
import BatchProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/BatchProcessDto';
import ResultCode from '@orchesty/nodejs-sdk/dist/lib/Utils/ResultCode';
import { PRODUCTS_IN_PROGRESS_KEY } from '../ABaseShoptet';
import ShoptetPremiumApplication from '../ShoptetPremiumApplication';
import AShoptetList, { IPaging } from './AShoptetList';
 
export const NAME = 'shoptet-get-product-changes-list';
 
export default class ShoptetGetProductChangesList extends AShoptetList<IResponseJson> {
 
    public endpoint = 'api/products/changes';
 
    public lastRunKey = 'lastRunListProductChanges';
 
    public fromParamKey = 'from';
 
    public getName(): string {
        return NAME;
    }
 
    protected processResult(responseDto: ResponseDto<IResponseJson>, batchProcessDto: BatchProcessDto): IPaging {
        const body = responseDto.getJsonBody().data;
 
        if (body.changes.length) {
            this.setItemsListToDto(batchProcessDto, body.changes);
        } else E{
            batchProcessDto.setStopProcess(ResultCode.DO_NOT_CONTINUE, 'No changes since last import.');
        }
 
        return body.paginator;
    }
 
    protected setItemsListToDto(dto: BatchProcessDto, responseBody: IOutputJson[]): void {
        dto.setItemList(responseBody);
    }
 
    protected getDefaultLastRun(): string {
        return ShoptetPremiumApplication.shoptetDateISO(new Date(), -1 * 30 * 24);
    }
 
    protected async saveInProgress(appInstall: ApplicationInstall): Promise<void> {
        appInstall.addNonEncryptedSettings({ [PRODUCTS_IN_PROGRESS_KEY]: Date.now() });
        await this.getDbClient().getApplicationRepository().update(appInstall);
    }
 
    protected async saveLastRunKey(appInstall: ApplicationInstall): Promise<void> {
        const encryptedSettings = appInstall.getNonEncryptedSettings();
 
        // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
        delete encryptedSettings[PRODUCTS_IN_PROGRESS_KEY];
        encryptedSettings[this.lastRunKey] = new Date();
 
        appInstall.setNonEncryptedSettings(encryptedSettings);
        await this.getDbClient().getApplicationRepository().update(appInstall);
    }
 
}
 
export interface IResponseJson {
    data: {
        changes: IOutputJson[];
        paginator: IPaging;
    };
}
 
export interface IOutputJson {
    changeType: string;
    changeTime: string;
    guid: string;
}