111 lines
6.2 KiB
TypeScript
111 lines
6.2 KiB
TypeScript
import type { Command } from "commander";
|
|
import * as core from '../../../data/core/client'
|
|
import { getAllAssetsInFolder, isValidASIN, uploadDownloadedImageToSquidexAsAsset, upsertAssetFolder } from "../common/catalog-helpers";
|
|
import { getMarketplacesUsingJsonQuery, getProductListingsUsingJsonQuery, getProductsUsingJsonQuery } from "../../../data/api-client";
|
|
import { SCHEMAS } from "../../../data/models/schemas";
|
|
import { logForCommand } from "../common/console";
|
|
|
|
export const COMMAND_NAME = 'append-images';
|
|
|
|
const log = logForCommand(COMMAND_NAME);
|
|
const error = logForCommand(COMMAND_NAME, console.error);
|
|
|
|
export const amazonAppendImagesCommand = (amazonCommand: Command) =>
|
|
amazonCommand.command(COMMAND_NAME).alias('append-image')
|
|
.description('Download images from URL .')
|
|
.argument('<ASIN>', 'Amazon Standard Identification Numbers')
|
|
.argument('<URLs...>', 'Image URLs to transmit to the database.')
|
|
.action(async (asin: string, urls: string[]) => {
|
|
if (!isValidASIN(asin)) {
|
|
error(`error: ${asin} is not a valid ASIN. Amazon Standard Identification Numbers are 10-digits long with letters A-Z and numbers 0-9.`);
|
|
return;
|
|
}
|
|
let productsDto = await getProductsUsingJsonQuery(JSON.stringify({ filter: {
|
|
op: 'eq',
|
|
path: 'data.marketplaceConnections.iv.connection.asin',
|
|
value: asin,
|
|
}}));
|
|
if (productsDto.items.length === 0) {
|
|
error(`error: ${asin} was not found in the database. Please procure or enter the product first.`);
|
|
return;
|
|
}
|
|
let marketplacesDto = await getMarketplacesUsingJsonQuery(JSON.stringify({ filter: {
|
|
op: 'eq',
|
|
path: 'data.marketplaceName.en-US',
|
|
value: 'Amazon',
|
|
}}));
|
|
if (marketplacesDto.items.length === 0) {
|
|
error(`error: Amazon marketplace not found in database. Please set up Amazon marketplace in database.`);
|
|
return;
|
|
}
|
|
log(`Upserting Asset Folder products`);
|
|
let productsAssetFolder = await upsertAssetFolder('products');
|
|
log(`Matching Asset Folder: ${productsAssetFolder.folderName} with Asset Folder ID: ${productsAssetFolder.id}`);
|
|
|
|
log(`Upserting Asset Folder ${productsAssetFolder.folderName}/amazon`);
|
|
let productsAmazonAssetFolder = await upsertAssetFolder('amazon', productsAssetFolder.id);
|
|
log(`Matching Asset Folder ${productsAssetFolder.folderName}/${productsAmazonAssetFolder.folderName} with Asset Folder ID: ${productsAmazonAssetFolder.id}`);
|
|
|
|
log(`Upserting Asset Folder ${productsAssetFolder.folderName}/${productsAmazonAssetFolder.folderName}/${asin}`);
|
|
let productsASINFolder = await upsertAssetFolder(asin, productsAmazonAssetFolder.id);
|
|
log(`Matching Asset Folder ${productsAssetFolder.folderName}/${productsAmazonAssetFolder.folderName}/${productsASINFolder.folderName} with Asset Folder ID: ${productsASINFolder.id}`);
|
|
let amazonMarketplaceDto = marketplacesDto.items[0];
|
|
for (let productDto of productsDto.items) {
|
|
let listingsDto = await getProductListingsUsingJsonQuery(JSON.stringify({ filter: {
|
|
'and': [
|
|
{
|
|
op: 'eq',
|
|
path: 'data.product.iv',
|
|
value: productDto.id,
|
|
},
|
|
{
|
|
op: 'eq',
|
|
path: 'data.marketplace.iv',
|
|
value: amazonMarketplaceDto.id,
|
|
}
|
|
]
|
|
}}));
|
|
if (listingsDto.items.length === 0) {
|
|
error(`error: Product listing for ${asin} on Amazon marketplace was not found in the database. Please procure or enter the product listing first.`);
|
|
return;
|
|
}
|
|
for (let listingDto of listingsDto.items) {
|
|
let listing = listingDto.data!;
|
|
let didUpdate = false;
|
|
|
|
let amazonAssetsDto = await getAllAssetsInFolder(productsASINFolder.id);
|
|
|
|
for (let i = 0; i < urls.length; i++) {
|
|
log(urls[i]);
|
|
let foundUploaded = amazonAssetsDto.filter((asset) => (asset.metadata['amazon-url'] as string||'') === urls[i]);
|
|
if (!foundUploaded.length) { // is not found
|
|
log(`Transmitting Product Image ${urls[i]} to Squidex Assets`);
|
|
let assetDto = await uploadDownloadedImageToSquidexAsAsset(urls[i]!, productsASINFolder.id);
|
|
log(`Saved Asset Id: ${assetDto.id} to Asset Folder Id: ${assetDto.parentId}`);
|
|
if (!listing.marketplaceImages) {
|
|
listing.marketplaceImages = { iv: [] };
|
|
}
|
|
if (!listing.marketplaceImages.iv) {
|
|
listing.marketplaceImages.iv = [];
|
|
}
|
|
listing.marketplaceImages.iv.push(assetDto.id);
|
|
didUpdate = true;
|
|
}
|
|
else { // is found
|
|
log(`Matched Asset Id: ${foundUploaded[0].id}, Amazon Product Image: ${foundUploaded[0].metadata['amazon-url'] as string||''}.`);
|
|
}
|
|
}
|
|
if (didUpdate) {
|
|
log(`Listing did update, updating product listing with appended images.`);
|
|
let updatedDto = await core.client.contents.putContent({
|
|
schema: SCHEMAS.LISTINGS,
|
|
id: listingDto.id,
|
|
unpublished: false,
|
|
requestBody: listing as any,
|
|
});
|
|
log(`Listing version ${updatedDto.version} stored.`);
|
|
}
|
|
}
|
|
}
|
|
})
|
|
.configureHelp(); |