Skip to content
This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
/ customer-import Public archive

Library for importing customers from JSON into your CTP project

Notifications You must be signed in to change notification settings

sphereio/customer-import

Repository files navigation

customer-import

Travis Codecov npm Commitizen friendly NPM version

A library that helps with importing customers into the Commercetools Platform.
This library is built to be used in conjunction with sphere-node-cli.

Features

  • Import customers to your CTP project
  • Pre-validate customers using a JSON schema
  • generating a password for every new customer
  • resolve a customer group name with the corresponding customer group reference
  • create a customer group for the given name if none exists yet
  • setting a default shipping and billing address

Configuration

The configuration object may contain:

  • sphereClientConfig: see the sphere-node-sdk docs for more information on this
  • defaultShippingAddress: Index of the address in the customer's addresses list that should be used as the shipping address
  • defaultBillingAddress: Index of the address in the customer's addresses list that should be used as the billing address

Usage with sphere-node-cli

You can use the customer import from the command line using the sphere-node-cli. In order for the cli to import customer, the file to import from must be JSON and follow the this structure:

{
  "customers": [
    <customer>,
    <customer>,
    ...
  ]
}

Then you can import this file using the cli:

sphere-node-cli -t customer -p my-project-key -f /sample_dir/customers.json

You can pass a custom configuration as described above via the -c operator:

sphere-node-cli -t customer -p my-project-key -f /sample_dir/customers.json -c '{ "defaultShippingAddress": 0, "defaultBillingAddress": 0 }'

Direct usage

If you want more control, you can also use this library directly in JavaScript. To do this you first need to install it:

npm install ct-customer-import --save-dev

Then you can use it to import customers like so:

import CustomerImport from 'ct-customer-import'

const customer = {
  email: '<some-email>'
}
const config = {
  sphereClientConfig: {
    config: {
      project_key: <PROJECT_KEY>,
      client_id: '*********',
      client_secret: '*********'
    },
    defaultShippingAddress: 0, defaultBillingAddress: 0
  }
}
const customerImport = CustomerImport(config)

// load customer groups so they can be resolved to references
customerImport.loadCustomerGroups()
.then(() => customerImport.importCustomer(customer))
.then(() => {
  // done importing the customer
  // look at the summary to see errors
  customerImport.summary
  // the summary hast the following structure
  // {
  //   errors: [],
  //   inserted: [<some-email>],
  //   successfullImports: 1
  // }
})