Skip to content

Commit

Permalink
feat(discount code import): add new package (#209)
Browse files Browse the repository at this point in the history
Add new package [discount-code-importer]. For more details, see here: https://commercetools.github.io/nodejs/cli/discount-code-importer.html

affects: @commercetools/discount-code-importer

resolves #143 

BREAKING CHANGE
  • Loading branch information
wizzy25 committed Jul 3, 2017
1 parent b650c07 commit 2c3f677
Show file tree
Hide file tree
Showing 16 changed files with 1,380 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ https://commercetools.github.io/nodejs/
| [`csv-parser-orders`](/packages/csv-parser-orders) | [![csv-parser-orders Version][csv-parser-orders-icon]][csv-parser-orders-version] | [![csv-parser-orders Dependencies Status][csv-parser-orders-dependencies-icon]][csv-parser-orders-dependencies] |
| [`csv-parser-price`](/packages/csv-parser-price) | [![csv-parser-price Version][csv-parser-price-icon]][csv-parser-price-version] | [![csv-parser-price Dependencies Status][csv-parser-price-dependencies-icon]][csv-parser-price-dependencies] |
| [`discount-code-generator`](/packages/discount-code-generator) | [![discount-code-generator Version][discount-code-generator-icon]][discount-code-generator-version] | [![discount-code-generator Dependencies Status][discount-code-generator-dependencies-icon]][discount-code-generator-dependencies] |
[`discount-code-importer`](/packages/discount-code-importer) | [![discount-code-importer Version][discount-code-importer-icon]][discount-code-importer-version] | [![discount-code-importer Dependencies Status][discount-code-importer-dependencies-icon]][discount-code-importer-dependencies] |

### Other
| Package | Version | Dependencies |
Expand Down Expand Up @@ -94,6 +95,10 @@ https://commercetools.github.io/nodejs/
[discount-code-generator-icon]: https://img.shields.io/npm/v/@commercetools/discount-code-generator.svg?style=flat-square
[discount-code-generator-dependencies]: https://david-dm.org/commercetools/nodejs?path=packages/discount-code-generator
[discount-code-generator-dependencies-icon]: https://img.shields.io/david/commercetools/nodejs.svg?path=packages/discount-code-generator&style=flat-square
[discount-code-importer-version]: https://www.npmjs.com/package/@commercetools/discount-code-importer
[discount-code-importer-icon]: https://img.shields.io/npm/v/@commercetools/discount-code-importer.svg?style=flat-square
[discount-code-importer-dependencies]: https://david-dm.org/commercetools/nodejs?path=packages/discount-code-importer
[discount-code-importer-dependencies-icon]: https://img.shields.io/david/commercetools/nodejs.svg?path=packages/discount-code-importer&style=flat-square
[sync-actions-version]: https://www.npmjs.com/package/@commercetools/sync-actions
[sync-actions-icon]: https://img.shields.io/npm/v/@commercetools/sync-actions.svg?style=flat-square
[sync-actions-dependencies]: https://david-dm.org/commercetools/nodejs?path=packages/sync-actions
Expand Down
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
* [CSV Price Parser](/cli/csv-parser-price.md)
* [CSV Order Parser](/cli/csv-parser-orders.md)
* [Discount Code Generator](/cli/discount-code-generator.md)
* [Discount Code Importer](/cli/discount-code-importer.md)
1 change: 1 addition & 0 deletions docs/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Our CLI tools perform various functions from importing and exporting to syncing
* [CSV Price Parser](/cli/csv-parser-price.md)
* [CSV Order Parser](/cli/csv-parser-orders.md)
* [Discount Code Generator](/cli/discount-code-generator.md)
* [Discount Code Importer](/cli/discount-code-importer.md)
194 changes: 194 additions & 0 deletions docs/cli/discount-code-importer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
# Discount Code Importer

A package that helps with importing [commercetools discount codes](http://dev.commercetools.com/http-api-projects-discountCodes.html) in JSON format to the [commercetools platform](http://dev.commercetools.com/).
This package is built to be used in conjunction with [sphere-node-cli](https://github.com/sphereio/sphere-node-cli)

## Configuration

The constructor accepts two arguments:
- A required object containing the following values:
- `apiConfig` (Object): `AuthMiddleware` options for authentication on the commercetools platform. (Required. See [here](https://commercetools.github.io/nodejs/sdk/api/sdkMiddlewareAuth.html#named-arguments-options))
- `batchSize` (Number): Amount of codes not more than 500 to process concurrently (Optional. Default: 50)
- `continueOnProblems` (Boolean): Flag whether to continue processing if an error occurs (Optional. Default: false)
- An optional logger object having four functions (`info`, `warn`, `error` and `verbose`)

## Usage with `sphere-node-cli`
You can use this package from the [`sphere-node-cli`](https://github.com/sphereio/sphere-node-cli). In order for the cli to import discount codes, the file to import from must be a valid JSON and follow this structure:
```json
[
{
"name": {
"en": "Sammuy",
"de": "Valerian"
},
"description": {
"en": "some new promo",
"de": "super Angebot"
},
"cartDiscounts": [
{
"typeId": "cart-discount",
"id": "some-cart-discount-id"
}
],
"cartPredicate": "lineItemTotal(1 = 1) > \"10.00 USD\"",
"isActive": true,
"maxApplications": 10,
"maxApplicationsPerCustomer": 2,
"code": "MyDiscountCode1"
},
{
"name": {
"en": "Sammuy",
"de": "Valerian"
},
"description": {
"en": "some new promo",
"de": "super Angebot"
},
"cartDiscounts": [
{
"typeId": "cart-discount",
"id": "some-cart-discount-id"
}
],
"cartPredicate": "lineItemTotal(1 = 1) > \"20.00 USD\"",
"isActive": false,
"maxApplications": 10,
"maxApplicationsPerCustomer": 2,
"code": "MyDiscountCode2"
},
{
"name": {
"en": "Sammuy",
"de": "Valerian"
},
"description": {
"en": "some new promo",
"de": "super Angebot"
},
"cartDiscounts": [
{
"typeId": "cart-discount",
"id": "some-cart-discount-id"
}
],
"cartPredicate": "lineItemTotal(1 = 1) > \"50.00 USD\"",
"isActive": false,
"maxApplications": 10,
"maxApplicationsPerCustomer": 2,
"code": "MyDiscountCode3"
},
...
]
```
Then you can import this file using the cli:
```bash
sphere-node-cli -t discountCode -p my-project-key -f /sample_dir/codes.json
```
Custom optional configuration can be passed in as described above using the `-c` flag
```bash
sphere-node-cli -t discountCode -p my-project-key -f /sample_dir/codes.json -b 20 -c '{ "continueOnProblems": true }'
```

## Direct Usage
If you would like to have more control, you can also use this module directly in Javascript. To do this, you need to install it:
```bash
npm install @commercetools/discount-code-importer
```
Then you can use it to import discount codes:
```js
import DiscountCodeImport from '@commercetools/discount-code-importer'

const codes = [
{
name: {
en: 'Sammuy',
de: 'Valerian'
},
description: {
en: 'some new promo',
de: 'super Angebot'
},
cartDiscounts: [
{
typeId: 'cart-discount',
id: 'some-cart-discount-id'
}
],
cartPredicate: 'lineItemTotal(1 = 1) > "10.00 USD"',
isActive: true,
maxApplications: 10,
maxApplicationsPerCustomer: 2,
code: 'MyDiscountCode1'
},
{
name: {
en: 'Sammuy',
de: 'Valerian'
},
description: {
en: 'some new promo',
de: 'super Angebot'
},
cartDiscounts: [
{
typeId: 'cart-discount',
id: 'some-cart-discount-id'
}
],
cartPredicate: 'lineItemTotal(1 = 1) > "20.00 USD"',
isActive: false,
maxApplications: 10,
maxApplicationsPerCustomer: 2,
code: 'MyDiscountCode2'
},
{
name: {
en: 'Sammuy',
de: 'Valerian'
},
description: {
en: 'some new promo',
de: 'super Angebot'
},
cartDiscounts: [
{
typeId: 'cart-discount',
id: 'some-cart-discount-id'
}
],
cartPredicate: 'lineItemTotal(1 = 1) > "50.00 USD"',
isActive: false,
maxApplications: 10,
maxApplicationsPerCustomer: 2,
code: 'MyDiscountCode3'
},
...
]

const options = {
apiConfig: {
host: 'https://auth.commercetools.com'
project_key: <PROJECT_KEY>,
credentials: {
clientId: '*********',
clientSecret: '*********'
}
},
batchSize: 20,
continueOnProblems: true
}
}
const discountCodeImport = new DiscountCodeImport(options)

discountCodeImport.run(codes)
.then(() => {
// handle successful import
})
.catch((error) => {
// handle error
})
```

**Note:** By default, if a discount code already exists, the module tries to build update actions for it, and if no update actions can be built, the code will be ignored
Loading

0 comments on commit 2c3f677

Please sign in to comment.