@commercelayer/cli-plugin-checkout
Advanced tools
Comparing version 1.2.3 to 2.0.0-beta.0
@@ -11,4 +11,4 @@ import { CommerceLayerClient } from '@commercelayer/sdk'; | ||
init(): Promise<any>; | ||
catch(error: any): Promise<void>; | ||
protected handleError(error: any, flags?: any): void; | ||
catch(error: any): Promise<any>; | ||
protected handleError(error: any, flags?: any): Promise<any>; | ||
protected commercelayerInit(flags: any): CommerceLayerClient; | ||
@@ -15,0 +15,0 @@ protected checkApplication(accessToken: string, kind: string): boolean; |
@@ -18,3 +18,3 @@ "use strict"; | ||
async catch(error) { | ||
this.handleError(error); | ||
return this.handleError(error); | ||
} | ||
@@ -31,3 +31,3 @@ handleError(error, flags) { | ||
else | ||
throw error; | ||
return super.catch(error); | ||
} | ||
@@ -34,0 +34,0 @@ commercelayerInit(flags) { |
@@ -8,2 +8,3 @@ import Command, { flags } from '../../base'; | ||
sku: flags.IOptionFlag<string[]>; | ||
bundle: flags.IOptionFlag<string[]>; | ||
market: flags.IOptionFlag<string | undefined>; | ||
@@ -18,4 +19,4 @@ coupon: flags.IOptionFlag<string | undefined>; | ||
run(): Promise<any>; | ||
private parseSKUs; | ||
private parseItems; | ||
private buildLineItems; | ||
} |
@@ -22,15 +22,27 @@ "use strict"; | ||
} | ||
if (!flags.sku) | ||
this.error(`One of the options ${chalk_1.default.cyanBright('--order (-O)')} or ${chalk_1.default.cyanBright('--sku (-S)')} is required`); | ||
if (!flags.sku && !flags.bundle) | ||
this.error(`One of the options ${chalk_1.default.cyanBright('--order (-O)')}, ${chalk_1.default.cyanBright('--sku (-S)')} or ${chalk_1.default.cyanBright('--bundle (-B)')} is required`); | ||
this.checkApplication(accessToken, 'sales_channel'); | ||
// Parse SKU options | ||
const skus = this.parseSKUs(flags); | ||
// Parse SKU and Bundle options | ||
const skus = this.parseItems(flags.sku); | ||
const bundles = this.parseItems(flags.bundle); | ||
// Build line items | ||
const lineItems = this.buildLineItems(skus); | ||
const lineItems = this.buildLineItems(skus, bundles); | ||
/* | ||
this.log(printObject(lineItems)) | ||
this.exit() | ||
*/ | ||
const cl = this.commercelayerInit(flags); | ||
const clSkus = await cl.skus.list({ filters: { code_matches_any: lineItems.map(li => li.sku_code).join(',') } }); | ||
// Check SKUs existence | ||
const clSkus = await cl.skus.list({ filters: { code_matches_any: lineItems.filter(li => li.item_type === 'sku').map(li => li.sku_code).join(',') } }); | ||
lineItems.forEach(li => { | ||
if (!clSkus.some(cls => cls.code === li.sku_code)) | ||
this.error(`Inexistent SKU: ${li.sku_code}`); | ||
this.error(`Inexistent SKU: ${chalk_1.default.redBright(String(li.sku_code))}`); | ||
}); | ||
// Check bundles existence | ||
const clBundles = await cl.bundles.list({ filters: { code_matches_any: lineItems.filter(li => li.item_type === 'bundle').map(li => li.bundle_code).join(',') } }); | ||
lineItems.forEach(li => { | ||
if (!clBundles.some(clb => clb.code === li.bundle_code)) | ||
this.error(`Inexistent bundle: ${chalk_1.default.redBright(String(li.bundle_code))}`); | ||
}); | ||
// Create order | ||
@@ -50,3 +62,7 @@ const market = flags.market; | ||
li.order = cl.orders.relationship(order); | ||
const lineItem = cl.line_items.create(li).then(li => this.log(`Created line item ${chalk_1.default.bold(li.id)} for SKU ${chalk_1.default.italic(li.sku_code || '')} and associated to order ${chalk_1.default.bold(order.id)}`)); | ||
const lineItem = cl.line_items.create(li).then(li => { | ||
const liName = (li.item_type === 'sku') ? 'SKU' : 'bundle'; | ||
const liCode = (li.item_type === 'sku') ? li.sku_code : li.bundle_code; | ||
this.log(`Created line item ${chalk_1.default.bold(li.id)} for ${liName} ${chalk_1.default.italic(liCode || '')} and associated to order ${chalk_1.default.bold(order.id)}`); | ||
}); | ||
if (lineItem) | ||
@@ -63,22 +79,55 @@ lis.push(lineItem); | ||
} | ||
parseSKUs(flags) { | ||
const skus = []; | ||
flags.sku.forEach((s) => skus.push(...s.split(','))); | ||
return skus; | ||
parseItems(itemsFlags) { | ||
const items = []; | ||
itemsFlags.forEach((i) => items.push(...i.split(','))); | ||
return items; | ||
} | ||
buildLineItems(skus) { | ||
/* | ||
private checkItem(item: string, type: string): { code: string; quantity: number; } { | ||
const def = item.split(':') | ||
if (def.length > 2) this.error('Invalid SKU option: ' + chalk.redBright(item)) | ||
const sku_code = def[0] | ||
const quantity = Number((def.length > 1) ? def[1] : 1) | ||
if (Number.isNaN(quantity) || (quantity < 0)) this.error('Invalid SKU definition: ' + chalk.redBright(s)) | ||
} | ||
*/ | ||
buildLineItems(skus, bundles) { | ||
const lineItems = []; | ||
skus.forEach(s => { | ||
const sd = s.split(':'); | ||
if (sd.length > 2) | ||
this.error('Invalid SKU option: ' + chalk_1.default.redBright(s)); | ||
const quantity = Number((sd.length > 1) ? sd[1] : 1); | ||
if (Number.isNaN(quantity) || (quantity < 0)) | ||
this.error('Invalid SKU definition: ' + chalk_1.default.redBright(s)); | ||
lineItems.push({ | ||
sku_code: sd[0], | ||
quantity, | ||
_update_quantity: true, | ||
// SKUs | ||
if (skus && (skus.length > 0)) | ||
skus.forEach(s => { | ||
const sd = s.split(':'); | ||
if (sd.length > 2) | ||
this.error('Invalid SKU option: ' + chalk_1.default.redBright(s)); | ||
const sku_code = sd[0]; | ||
const quantity = Number((sd.length > 1) ? sd[1] : 1); | ||
if (Number.isNaN(quantity) || (quantity < 0)) | ||
this.error('Invalid SKU definition: ' + chalk_1.default.redBright(s)); | ||
lineItems.push({ | ||
item_type: 'sku', | ||
sku_code, | ||
quantity, | ||
_update_quantity: true, | ||
}); | ||
}); | ||
}); | ||
// Bundles | ||
if (bundles && (bundles.length > 0)) | ||
bundles.forEach(b => { | ||
const bd = b.split(':'); | ||
if (bd.length > 2) | ||
this.error('Invalid Bundle option: ' + chalk_1.default.redBright(b)); | ||
const bundle_code = bd[0]; | ||
const quantity = Number((bd.length > 1) ? bd[1] : 1); | ||
if (Number.isNaN(quantity) || (quantity < 0)) | ||
this.error('Invalid Bundle definition: ' + chalk_1.default.redBright(b)); | ||
lineItems.push({ | ||
item_type: 'bundle', | ||
bundle_code, | ||
quantity, | ||
_update_quantity: true, | ||
}); | ||
}); | ||
return lineItems; | ||
@@ -97,3 +146,3 @@ } | ||
description: 'an order id', | ||
exclusive: ['sku'], | ||
exclusive: ['sku', 'bundle'], | ||
multiple: false, | ||
@@ -105,14 +154,19 @@ }), sku: base_1.flags.string({ | ||
multiple: true, | ||
}), bundle: base_1.flags.string({ | ||
char: 'B', | ||
description: 'a bundle code', | ||
exclusive: ['order'], | ||
multiple: true, | ||
}), market: base_1.flags.string({ | ||
char: 'm', | ||
description: 'a market number', | ||
dependsOn: ['sku'], | ||
exclusive: ['order'], | ||
}), coupon: base_1.flags.string({ | ||
char: 'c', | ||
description: 'a promo code', | ||
dependsOn: ['sku'], | ||
exclusive: ['order'], | ||
}), email: base_1.flags.string({ | ||
char: 'e', | ||
description: 'a customer email', | ||
dependsOn: ['sku'], | ||
exclusive: ['order'], | ||
}) }); |
@@ -1,1 +0,1 @@ | ||
{"version":"1.2.3","commands":{"checkout":{"id":"checkout","description":"create checkout URLs","pluginName":"@commercelayer/cli-plugin-checkout","pluginType":"core","aliases":[],"examples":["$ commercelayer checkout -O <order-id>","$ cl checkout -S <sku-code> -m <market-id> -c <coupon-code> -e <email-address>","$ cl checkout -S <sku-code-1> -S <sku-code-2> -m <market-id>"],"flags":{"organization":{"name":"organization","type":"option","char":"o","description":"the slug of your organization","required":true},"domain":{"name":"domain","type":"option","char":"d","hidden":true,"required":false},"accessToken":{"name":"accessToken","type":"option","char":"a","hidden":false,"required":true},"open":{"name":"open","type":"boolean","description":"open checkout URL in default browser","allowNo":false},"order":{"name":"order","type":"option","char":"O","description":"an order id"},"sku":{"name":"sku","type":"option","char":"S","description":"an SKU code"},"market":{"name":"market","type":"option","char":"m","description":"a market number"},"coupon":{"name":"coupon","type":"option","char":"c","description":"a promo code"},"email":{"name":"email","type":"option","char":"e","description":"a customer email"}},"args":[]},"checkout:noc":{"id":"checkout:noc","pluginName":"@commercelayer/cli-plugin-checkout","pluginType":"core","hidden":true,"aliases":[],"flags":{},"args":[]},"checkout:order":{"id":"checkout:order","description":"create checkout URLs starting from an existing order","pluginName":"@commercelayer/cli-plugin-checkout","pluginType":"core","aliases":[],"flags":{"organization":{"name":"organization","type":"option","char":"o","description":"the slug of your organization","required":true},"domain":{"name":"domain","type":"option","char":"d","hidden":true,"required":false},"accessToken":{"name":"accessToken","type":"option","char":"a","hidden":false,"required":true},"open":{"name":"open","type":"boolean","description":"open checkout URL in default browser","allowNo":false}},"args":[{"name":"id","description":"unique id of the order","required":true}]}}} | ||
{"version":"2.0.0-beta.0","commands":{"checkout":{"id":"checkout","description":"create checkout URLs","pluginName":"@commercelayer/cli-plugin-checkout","pluginType":"core","aliases":[],"examples":["$ commercelayer checkout -O <order-id>","$ cl checkout -S <sku-code> -m <market-id> -c <coupon-code> -e <email-address>","$ cl checkout -S <sku-code-1> -S <sku-code-2> -m <market-id>"],"flags":{"organization":{"name":"organization","type":"option","char":"o","description":"the slug of your organization","required":true},"domain":{"name":"domain","type":"option","char":"d","hidden":true,"required":false},"accessToken":{"name":"accessToken","type":"option","char":"a","hidden":false,"required":true},"open":{"name":"open","type":"boolean","description":"open checkout URL in default browser","allowNo":false},"order":{"name":"order","type":"option","char":"O","description":"an order id"},"sku":{"name":"sku","type":"option","char":"S","description":"an SKU code"},"bundle":{"name":"bundle","type":"option","char":"B","description":"a bundle code"},"market":{"name":"market","type":"option","char":"m","description":"a market number"},"coupon":{"name":"coupon","type":"option","char":"c","description":"a promo code"},"email":{"name":"email","type":"option","char":"e","description":"a customer email"}},"args":[]},"checkout:noc":{"id":"checkout:noc","pluginName":"@commercelayer/cli-plugin-checkout","pluginType":"core","hidden":true,"aliases":[],"flags":{},"args":[]},"checkout:order":{"id":"checkout:order","description":"create checkout URLs starting from an existing order","pluginName":"@commercelayer/cli-plugin-checkout","pluginType":"core","aliases":[],"flags":{"organization":{"name":"organization","type":"option","char":"o","description":"the slug of your organization","required":true},"domain":{"name":"domain","type":"option","char":"d","hidden":true,"required":false},"accessToken":{"name":"accessToken","type":"option","char":"a","hidden":false,"required":true},"open":{"name":"open","type":"boolean","description":"open checkout URL in default browser","allowNo":false}},"args":[{"name":"id","description":"unique id of the order","required":true}]}}} |
{ | ||
"name": "@commercelayer/cli-plugin-checkout", | ||
"description": "Commerce Layer CLI Checkout plugin", | ||
"version": "1.2.3", | ||
"version": "2.0.0-beta.0", | ||
"author": "Pierluigi Viti <pierluigi@commercelayer.io>", | ||
@@ -17,3 +17,3 @@ "bin": { | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^10.17.60", | ||
"@types/node": "^16.11.0", | ||
"chai": "^4.3.4", | ||
@@ -33,7 +33,7 @@ "eslint": "^5.16.0", | ||
"nyc": "^14.1.1", | ||
"ts-node": "^8.10.2", | ||
"typescript": "^4.4.0" | ||
"ts-node": "^10.4.0", | ||
"typescript": "^4.4.4" | ||
}, | ||
"engines": { | ||
"node": ">=8.0.0" | ||
"node": ">=12.20" | ||
}, | ||
@@ -40,0 +40,0 @@ "files": [ |
@@ -35,2 +35,3 @@ @commercelayer/cli-plugin-checkout | ||
OPTIONS | ||
-B, --bundle=bundle a bundle code | ||
-O, --order=order an order id | ||
@@ -37,0 +38,0 @@ -S, --sku=sku an SKU code |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23929
375
72
1