All files / src/Batch SupplyDoGetOrderHistory.ts

89.65% Statements 26/29
60% Branches 6/10
100% Functions 6/6
89.65% Lines 26/29

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 106 107 108  18x   18x     18x 18x 18x   18x     18x       1x 1x 1x   1x                 1x 1x   1x     1x     1x     1x       1x       1x       1x 1x 1x       1x       1x 1x       1x                                                                            
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';
 
export const NAME = 'supply-do-get-order-history';
export const LAST_RUN_KEY = 'lastRunListOrders';
export const LIMIT = 1000;
 
export default class SupplyDoGetOrderHistory extends ABatchNode {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: BatchProcessDto<IInput>): Promise<BatchProcessDto> {
        const appInstall = await this.getApplicationInstallFromProcess(dto);
        const ecommerce = dto.getUser();
        const page = Number(dto.getBatchCursor('0'));
 
        const req = await this.getApplication().getRequestDto(
            dto,
            await this.getApplicationInstallFromProcess(dto),
            HttpMethods.GET,
            'items/selling_order_history?fields[]=*&fields[]=*.*&fields[]=selling_order.transport.*'
            + `${this.addStatusFilter(dto)}&filter[selling_order][ecommerce][_eq]=${ecommerce}${this.addLastRunFilter(dto, appInstall)}${this.addOrderNumberFilter(dto)}&sort=-date`
            + `&limit=${LIMIT}&offset=${page * LIMIT}&meta=filter_count`,
        );
 
        const resp = await this.getSender().send<IResponse>(req, [200]);
        const { meta } = resp.getJsonBody();
 
        Iif (meta.filter_count && meta.filter_count > LIMIT * (page + 1)) {
            dto.setBatchCursor(String(page + 1));
        } else {
            appInstall.addNonEncryptedSettings({
                [LAST_RUN_KEY]: new Date().toISOString(),
            });
            await this.getDbClient().getApplicationRepository().update(appInstall);
        }
 
        return this.setItemList(dto, resp);
    }
 
    protected setItemList(dto: BatchProcessDto, resp: ResponseDto<IResponse>): BatchProcessDto {
        return dto.setItemList(resp.getJsonBody().data);
    }
 
    protected addStatusFilter(_dto: BatchProcessDto): string {
        return '&filter[type][_nin]=new,hold,canceled';
    }
 
    protected addLastRunFilter(_dto: BatchProcessDto<IInput>, appInstall: ApplicationInstall): string {
        const { orderNumber } = _dto.getJsonData();
        const lastRun = appInstall.getNonEncryptedSettings()[LAST_RUN_KEY] ?? new Date(0).toISOString();
        Iif (orderNumber) {
            return '';
        }
 
        return `&filter[_or][1][date_created][_gte]=${lastRun}&filter[_or][2][date_updated][_gte]=${lastRun}`;
    }
 
    protected addOrderNumberFilter(_dto: BatchProcessDto<IInput>): string {
        const { orderNumber } = _dto.getJsonData();
        Iif (orderNumber) {
            return `&filter[selling_order][order_number][_eq]=${orderNumber}`;
        }
 
        return '';
    }
 
}
 
export interface IInput {
    orderNumber?: string;
}
 
/* eslint-disable @typescript-eslint/naming-convention */
export interface IOutput {
    date: string;
    id: number;
    type: string;
    selling_order: {
        customer: number;
        id: string;
        payment_type: string;
        external_id: string;
        ecommerce: number;
        history: number[];
        products: number[];
        transport: {
            carrier: number;
            id: number;
            tracking_number: string;
            ecommerce: number;
        };
    };
}
 
export interface IResponse {
    data: IOutput[];
    meta: {
        filter_count?: number;
    };
}
/* eslint-enable @typescript-eslint/naming-convention */