All files / src/Batch SupplyDoGetSellingOrders.ts

92.85% Statements 26/28
70% Branches 7/10
100% Functions 5/5
92.85% Lines 26/28

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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 17218x 18x     18x 18x 18x   18x     18x       2x 2x 2x 2x 2x           2x       2x           2x 2x   2x     2x     2x     2x       4x       2x         4x 4x 2x     2x                                                                                                                                                                                                                    
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';
 
export const NAME = 'supply-do-get-selling-orders';
export const LAST_RUN_KEY = 'lastRunListSellingOrders';
export const LIMIT = 1000;
 
export default class SupplyDoGetSellingOrders extends ABatchNode {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: BatchProcessDto<IInput>): Promise<BatchProcessDto> {
        const appInstall = await this.getApplicationInstallFromProcess(dto);
        const lastRun = await appInstall.getNonEncryptedSettings()[this.getLastRunKey()];
        const page = Number(dto.getBatchCursor('0'));
        const ecommerce = dto.getUser();
        let url = 'items/selling_order?fields[]=*&fields[]=selling_order_history.*&fields[]=selling_order_product.*'
            + '&fields[]=selling_order_product.price.*&fields[]=selling_order_product.return_product.*&fields[]=selling_order_product.reclamation_product.*'
            + '&fields[]=customer.address.*&fields[]=selling_order_product.product_batch.*&fields[]=selling_order_product.product_batch.product.*'
            + `&fields[]=total_price.*&fields[]=transport.*&fields[]=customer.*${this.getStatusQueryParams()}&filter[ecommerce][_eq]=${ecommerce}`
            + `${this.addIdFilter(dto)}&limit=${LIMIT}&offset=${page * LIMIT}&meta=filter_count`;
 
        Iif (this.addIdFilter(dto) === '' && lastRun) {
            url += `&filter[date_updated][_gte]=${lastRun}`;
        }
 
        const req = await this.getApplication().getRequestDto(
            dto,
            await this.getApplicationInstallFromProcess(dto),
            HttpMethods.GET,
            url,
        );
        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({
                [this.getLastRunKey()]: new Date().toISOString(),
            });
            await this.getDbClient().getApplicationRepository().update(appInstall);
        }
 
        return dto.setItemList(resp.getJsonBody().data);
    }
 
    protected getLastRunKey(): string {
        return LAST_RUN_KEY;
    }
 
    protected getStatusQueryParams(): string {
        return '&filter[selling_order_history][_some][type][_eq]=processing'
            + '&filter[selling_order_history][_none][type][_in]=dispatched,ready_for_pickup,delivered,not_delivered,not_paid,wrong_address,wrong_phone_number,package_lost,canceled,rejected,missing_pickup_point,hold';
    }
 
    protected addIdFilter(dto: BatchProcessDto<IInput>): string {
        const { id } = dto.getJsonData();
        if (id) {
            return `&filter[id][_eq]=${id}`;
        }
 
        return '';
    }
 
}
 
export interface IInput {
    id?: string;
}
 
/* eslint-disable @typescript-eslint/naming-convention */
 
export interface ISellingOrder {
    customer: {
        email: string;
        phone: string;
        address: {
            city: string;
            country: string;
            name: string;
            street: string;
            street_number: string;
            zip_code: string;
        };
    };
    id: string;
    payment_type: string;
    date_created: string;
    date_updated: string;
    paid: boolean;
    transport: {
        carrier: number;
        id: number;
        tracking_number: string;
        packeta_pickup_point_id: string;
        ecommerce: number;
    };
    external_id: string;
    ecommerce: number;
    order_number: string;
    total_price: {
        amount: number;
        currency: string;
    }
    selling_order_history: {
        date: string;
        id: number;
        selling_order: string;
        type: string;
    }[];
    selling_order_product: {
        id: number;
        price: {
            amount: number;
            currency: string;
            id: number;
        };
        quantity: number;
        selling_order: string;
        product_batch: {
            product: {
                alt_ean: string;
                brand: number;
                color: string;
                country_of_origin: string;
                ean: string;
                eta_days: number;
                height_cm: string;
                id: number;
                length_cm: string;
                name: string;
                purchase_price: number;
                selling_price: number;
                supplier: number;
                weight_grams: number;
                width_cm: string;
                ecommerce: number;
                external_id: string;
                category: string;
                date_created: string;
            }
        };
        warehouse: number;
        reclamation_product: {
            id: number;
            quantity: number;
            reclamation: string;
            selling_order_product: number;
        }[];
        return_product: {
            id: number;
            quantity: number;
            return: string;
            selling_order_product: number;
        }[];
    }[];
}
 
export interface IResponse {
    data: ISellingOrder[];
    meta: {
        total_count: number;
        filter_count: number;
    };
}
 
/* eslint-enable @typescript-eslint/naming-convention */