All files / src/Batch UpgatesGetOrders.ts

96.77% Statements 30/31
91.66% Branches 11/12
100% Functions 2/2
96.77% Lines 30/31

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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 2158x 8x   8x 8x   8x   8x   8x     8x       4x 4x   4x 4x       4x 1x 3x 1x   2x   2x 2x 1x       4x 4x           4x   4x   4x 3x 3x 3x   4x   4x                                                                                                                                                                                                                                                                                                                            
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 { DateTime } from 'luxon';
import UpgatesApplication, { NAME as BASE_NAME } from '../UpgatesApplication';
 
export const NAME = `${BASE_NAME.toLowerCase()}-get-orders`;
 
const LIST_PAGE_ENDPOINT = 'api/v2/orders';
 
export default class UpgatesGetOrders extends ABatchNode {
 
    public getName(): string {
        return NAME;
    }
 
    public async processAction(dto: BatchProcessDto<IInput>): Promise<BatchProcessDto> {
        const app = this.getApplication<UpgatesApplication>();
        const appInstall = await this.getApplicationInstallFromProcess(dto);
 
        const { from, orderNumber, orderNumbers } = dto.getJsonData();
        const pageNumber = dto.getBatchCursor('1');
 
        let url: string;
 
        if (orderNumbers) {
            url = `${LIST_PAGE_ENDPOINT}?order_numbers=${orderNumbers}`;
        } else if (orderNumber) {
            url = `${LIST_PAGE_ENDPOINT}/${orderNumber}`;
        } else {
            url = `${LIST_PAGE_ENDPOINT}?page=${pageNumber}`;
 
            const lastRun = from ?? app.getIsoDateFromDate(appInstall.getNonEncryptedSettings().orderLastRun);
            if (lastRun) {
                url = `${url}&creation_time_from=${lastRun}`;
            }
        }
 
        const requestDto = app.getRequestDto(dto, appInstall, HttpMethods.GET, url);
        const res = await this.getSender().send<IResponseJson>(requestDto);
 
        const {
 
            number_of_pages,
            orders,
        } = res.getJsonBody();
 
        Iif (Number(pageNumber) < number_of_pages) {
            dto.setBatchCursor((Number(pageNumber) + 1).toString());
        } else if (!orderNumber) {
            appInstall.setNonEncryptedSettings({ orderLastRun: DateTime.now() });
            const repo = this.getDbClient().getApplicationRepository();
            await repo.update(appInstall);
        }
        dto.setItemList(orders);
 
        return dto;
    }
 
}
 
export interface IInput {
    from?: string;
    orderNumber?: string;
    orderNumbers?: string;
}
 
/* eslint-disable @typescript-eslint/naming-convention */
interface IResponseJson extends IOrderJson {
    number_of_pages: number;
}
 
interface IOrderJson {
    orders: IOrder[];
}
 
export interface IOrder {
    order_number: string;
    external_order_number: string;
    language_id: string;
    prices_with_vat_yn: string;
    status_id: string;
    currency_id: string;
    status: string;
    paid_date: string;
    tracking_code: string;
    resolved_yn: string;
    internal_note: string;
    last_update_time: string;
    creation_time: string;
    variable_symbol: string;
    total_weight: string;
    order_total: string;
    order_total_before_round: string;
    order_total_rest: string;
    invoice_number: string;
    origin: string;
    customer: {
        email: string;
        phone: string;
        firstname_invoice: string;
        surname_invoice: string;
        street_invoice: string;
        city_invoice: string;
        state_invoice: string;
        zip_invoice: string;
        country_id_invoice: string;
        postal_yn: string;
        firstname_postal: string;
        surname_postal: string;
        street_postal: string;
        city_postal: string;
        state_postal: string;
        zip_postal: string;
        country_id_postal: string;
        company_postal: string;
        company_yn: string;
        company: string;
        ico: string;
        dic: string;
        vat_payer_yn: string;
        customer_note: string;
        agreements: {
            name: string;
            valid_to: string;
            status: string;
        }[];
    };
    products: {
        product_id: string;
        option_set_id: string;
        code: string;
        code_supplier: string;
        ean: string;
        title: string;
        unit: string;
        quantity: string;
        price_per_unit: string;
        price: string;
        price_with_vat: string;
        price_without_vat: string;
        vat: string;
        buy_price: string;
        weight: string;
        invoice_info: string;
        parameters: {
            name: string;
            value: string;
        }[];
        configurations: {
            name: string;
            values: {
                value: string;
                operation: string;
                price: string;
            }[];
        }[];
    }[];
    discount_voucher: {
        code: string;
        type: string;
        amount: string;
        discounts: {
            vat: string;
            price: string;
        }[];
    };
    quantity_discount: {
        type: string;
        amount: string;
        discounts: {
            vat: string;
            price: string;
        }[];
    };
    loyalty_points: {
        one_point_for: string;
        amount: string;
        discounts: {
            vat: string;
            price: string;
        }[];
    };
    shipment: {
        id: number;
        code: string;
        name: string;
        price: string;
        vat: string;
        affiliate_id: string;
        affiliate_name: string;
    };
    payment: {
        id: number;
        code: string;
        name: string;
        price: string;
        vat: string;
        eet_yn: string;
    };
    metas: {
        key: string;
        type: string;
        value: string;
        values: {
            cs: {
                language: string;
                value: string;
            };
        };
    }[];
}
 
/* eslint-enable @typescript-eslint/naming-convention */