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 | 16x 16x 16x 16x 16x 16x 16x 16x 16x 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 { HttpMethods } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import BatchProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/BatchProcessDto';
import ABaseShopify, { API_VERSION } from '../ABaseShopify';
export const NAME = 'shopify-get-products-list';
const LIMIT = 100;
const LIST_PRODUCTS_ENDPOINT = `admin/api/${API_VERSION}/products.json?limit=${LIMIT}`;
const LAST_RUN_KEY = 'lastRunListProductsChanges';
export default class ShopifyGetProductsList extends ABatchNode {
public getName(): string {
return NAME;
}
public async processAction(dto: BatchProcessDto<IInputJson>): Promise<BatchProcessDto> {
const { from } = dto.getJsonData();
const appInstall = await this.getApplicationInstallFromProcess(dto);
const creationTimeFrom = from || appInstall.getNonEncryptedSettings()[LAST_RUN_KEY];
let url = dto.getBatchCursor(LIST_PRODUCTS_ENDPOINT);
Iif (creationTimeFrom) {
const separatorChar = url.includes('?') ? '&' : '?';
url = `${url}${separatorChar}created_at_min=${creationTimeFrom}`;
}
const app = this.getApplication<ABaseShopify>();
const requestDto = app.getRequestDto(dto, appInstall, HttpMethods.GET, url);
const res = await this.getSender().send<IResponseJson>(requestDto);
const nextPageLink = app.getNextPageFromHeaders(res.getHeaders());
Iif (nextPageLink) {
dto.setBatchCursor(nextPageLink);
} else {
await this.writeLastTimeRun(appInstall);
}
const { products } = res.getJsonBody();
dto.setItemList(products);
return dto;
}
private async writeLastTimeRun(appInstall: ApplicationInstall): Promise<void> {
appInstall.addNonEncryptedSettings({ [LAST_RUN_KEY]: new Date() });
await this.getDbClient().getApplicationRepository().update(appInstall);
}
}
interface IInputJson {
from: string;
}
interface IResponseJson {
products: {
id: string;
}[];
}
/* eslint-disable @typescript-eslint/naming-convention */
export interface IOutput {
id: number;
title: string;
body_html: string;
vendor: string;
product_type: string;
created_at: string;
handle: string;
updated_at: string;
published_at: string;
published_scope: string;
tags: string;
admin_graphql_api_id: string;
variants: {
id: number;
product_id: number;
title: string;
price: string;
sku: string;
position: number;
inventory_policy: string;
compare_at_price?: unknown;
fulfillment_service: string;
inventory_management: string;
option1: string;
option2?: unknown;
option3?: unknown;
created_at: string;
updated_at: string;
taxable: boolean;
barcode: string;
grams: number;
image_id: number;
weight: number;
weight_unit: string;
inventory_item_id: number;
inventory_quantity: number;
old_inventory_quantity: number;
presentment_prices: {
price: {
amount: string;
currency_code: string;
};
compare_at_price?: unknown;
}[];
requires_shipping: boolean;
admin_graphql_api_id: string;
}[];
options: {
id: number;
product_id: number;
name: string;
position: number;
values: string[];
}[];
images: {
id: number;
product_id: number;
position: number;
created_at: string;
updated_at: string;
alt?: unknown;
width: number;
height: number;
src: string;
variant_ids: unknown[];
admin_graphql_api_id: string;
}[];
image: {
id: number;
product_id: number;
position: number;
created_at: string;
updated_at: string;
alt?: unknown;
width: number;
height: number;
src: string;
variant_ids: unknown[];
admin_graphql_api_id: string;
};
template_suffix?: unknown;
}
/* eslint-enable @typescript-eslint/naming-convention */
|