From 1b2b6531277ffd2af643965cd0d7ef39633e7f74 Mon Sep 17 00:00:00 2001 From: philippspo Date: Mon, 20 Jun 2016 12:17:22 +0200 Subject: [PATCH 1/2] chore(github): add github issue and pr templates --- .github/ISSUE_TEMPLATE.md | 5 +++++ .github/PULL_REQUEST_TEMPLATE.md | 6 ++++++ 2 files changed, 11 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..30bc368 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,5 @@ +### Steps to reproduce the behavior + +### Expected behavior + +### Actual behavior diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..7b3e91b --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,6 @@ +- [ ] commit messages are commitizen-friendly +- [ ] code is unit tested +- [ ] code is integration tested +- [ ] public code is documented +- [ ] code is reviewed by at least one person +- [ ] code satisfies linter rules From 8e85b0a4fbf70457af0d0162aca7cb18d9c23dba Mon Sep 17 00:00:00 2001 From: philippspo Date: Mon, 27 Jun 2016 12:21:51 +0200 Subject: [PATCH 2/2] fix(import): fix setting the default billing and shipping address --- .github/ISSUE_TEMPLATE.md | 4 +- src/customer-import.js | 4 +- src/schema.js | 27 +++++ tests/integration/customer-import.js | 166 ++++++++++++++++++++------- tests/unit/customer-import.js | 6 +- 5 files changed, 160 insertions(+), 47 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 30bc368..600a27e 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -1,5 +1,5 @@ ### Steps to reproduce the behavior -### Expected behavior - ### Actual behavior + +### Expected behavior diff --git a/src/customer-import.js b/src/customer-import.js index 71cfa9f..f126529 100644 --- a/src/customer-import.js +++ b/src/customer-import.js @@ -68,10 +68,10 @@ export default class CustomerImport { // user does not exist yet -> generate password for him customer.password = generatePassword() if (_.isNumber(this.config.defaultShippingAddress)) { - customer.defaultShippingAddressId = this.config.defaultShippingAddress + customer.defaultShippingAddress = this.config.defaultShippingAddress } if (_.isNumber(this.config.defaultBillingAddress)) { - customer.defaultBillingAddressId = this.config.defaultBillingAddress + customer.defaultBillingAddress = this.config.defaultBillingAddress } // TODO should this be configurable? // so that you choose whether you already have customer group id or name diff --git a/src/schema.js b/src/schema.js index 5340bb4..7ec393b 100644 --- a/src/schema.js +++ b/src/schema.js @@ -8,17 +8,44 @@ const schema = { items: { type: 'object', properties: { + firstName: { type: 'string' }, + lastName: { type: 'string' }, streetName: { type: 'string' }, postalCode: { type: 'string' }, + company: { type: 'string' }, city: { type: 'string' }, country: { type: 'string', minLength: 2, maxLength: 2 }, } }, }, + firstName: { type: 'string' }, + lastName: { type: 'string' }, + middleName: { type: 'string' }, + title: { type: 'string' }, + anonymousCartId: { type: 'string' }, + dateOfBirth: { type: 'string' }, + externalId: { type: 'string' }, + isEmailVerified: { type: 'boolean' }, email: { type: 'string' }, phone: { type: 'string' }, customerGroup: { type: 'string' }, vatId: { type: 'string' }, + custom: { + type: 'object', + properties: { + type: { + type: 'object', + properties: { + key: { + type: 'string' + } + } + }, + fields: { + type: 'object' + } + } + } }, required: ['email'], additionalProperties: false, diff --git a/tests/integration/customer-import.js b/tests/integration/customer-import.js index 6fa8bb7..1283b99 100644 --- a/tests/integration/customer-import.js +++ b/tests/integration/customer-import.js @@ -28,39 +28,36 @@ describe('customer import module', function () { } client = new SphereClient(options) - customerImport = new CustomerImport( - logger, - { sphereClientConfig: options } - ) - done() - }) - }) - - afterEach((done) => { - // remove all customers - const deleteAll = (service) => { - return client[service].process(({ body: { results } }) => { - return Promise.map(results, (customer) => { - return client[service].byId(customer.id) - .delete(customer.version) + // remove all customers + const deleteAll = (service) => { + return client[service].process(({ body: { results } }) => { + return Promise.map(results, (customer) => { + return client[service].byId(customer.id) + .delete(customer.version) + }) }) + } + deleteAll('customers') + .then(() => deleteAll('customerGroups')) + .then(() => deleteAll('types')) + .then(() => { + customerImport = new CustomerImport( + logger, + { sphereClientConfig: options } + ) + done() }) - } - deleteAll('customers') - .then(() => { - return deleteAll('customerGroups') - }) - .then(() => { - done() }) - .catch(done) }) it('should import a complete customer', (done) => { const customer = { 'customerNumber':'12341234', + 'firstName': 'Max', + 'lastName': 'Mustermann', + 'externalId': '1-nc0r98nc1-390r8cn-1309rcn8', + 'companyName':'Some random company', 'addresses':[{ - 'companyName':'Some random company', 'streetName':'Musterstraße 123', 'postalCode':'11111', 'city':'Stadt', @@ -126,30 +123,119 @@ describe('customer import module', function () { const customer = { email: 'philipp.sporrer@commercetools.de', + companyName: 'Some random company', addresses: [{ - streetName: 'Ernst-Platz-Straße 45a', - postalCode: '80992', - city: 'München', + streetName: 'Musterstraße 123', + postalCode: '11111', + city: 'Stadt', country: 'DE' }] } customerImport.loadCustomerGroups() + .then(() => customerImport.importCustomer(customer)) .then(() => { - customerImport.importCustomer(customer) - .then(() => { - const summary = JSON.parse(customerImport.summaryReport()) - const actual = summary.errors.length - const expected = 0 + const summary = JSON.parse(customerImport.summaryReport()) + const actual = summary.errors.length + const expected = 0 - expect(actual).to.equal(expected) - client.customers.where(`email="${customer.email}"`).fetch() - .then(({ body: { results: customers } }) => { - const actual = _.omit(customers[0].addresses[0], 'id') - const expected = customer.addresses[0] + expect(actual).to.equal(expected) + return client.customers.where(`email="${customer.email}"`).fetch() + }) + .then(({ body: { results: customers } }) => { + const actual = _.omit(customers[0].addresses[0], 'id') + const expected = customer.addresses[0] - expect(actual).to.deep.equal(expected) - done() - }) + expect(actual).to.deep.equal(expected) + done() + }) + .catch(done) + }) + + it('should import a customer with a default shipping and billing address', + (done) => { + + const customer = { + email: 'philipp.sporrer@commercetools.de', + companyName: 'Some random company', + addresses: [{ + streetName: 'Musterstraße 123', + postalCode: '11111', + city: 'Stadt', + country: 'DE' + }] + } + customerImport.config.defaultShippingAddress = 0 + customerImport.config.defaultBillingAddress = 0 + customerImport.loadCustomerGroups() + .then(() => customerImport.importCustomer(customer)) + .then(() => { + const summary = JSON.parse(customerImport.summaryReport()) + const actual = summary.errors.length + const expected = 0 + + expect(actual).to.equal(expected) + return client.customers.where(`email="${customer.email}"`).fetch() + }) + .then(({ body: { results: customers } }) => { + const { id } = customers[0].addresses[0] + + expect(id).to.equal(customers[0].defaultBillingAddressId) + expect(id).to.equal(customers[0].defaultShippingAddressId) + done() + }) + .catch(done) + }) + + it('should import a customer with custom fields', (done) => { + + let customer + client.types.create({ + key: 'custom-customer', + name: { en: 'custom customer' }, + resourceTypeIds: ['customer'], + fieldDefinitions: [ + { + name: 'customField1', + type: { name: 'String' }, + required: false, + label: { label: 'Custom field 1' }, + inputHint: 'SingleLine' + }, + { + name: 'customField2', + type: { name: 'Boolean' }, + required: false, + label: { en: 'Custom field 2' }, + } + ] + }).then(({ body: customType }) => { + customer = { + email: 'philipp.sporrer@commercetools.de', + custom: { + type: { + key: customType.key + }, + fields: { + customField1: 'customValue1', + customField2: true + } + } + } + return customerImport.importCustomer(customer) + }) + .then(() => { + const summary = JSON.parse(customerImport.summaryReport()) + const actual = summary.errors.length + const expected = 0 + + expect(actual).to.equal(expected) + client.customers.where(`email="${customer.email}"`).fetch() + .then(({ body: { results: customers } }) => { + const actual = customers[0].custom.fields + const expected = customer.custom.fields + + expect(actual).to.deep.equal(expected) + done() }) }) .catch(done) diff --git a/tests/unit/customer-import.js b/tests/unit/customer-import.js index b8e608f..c76ad07 100644 --- a/tests/unit/customer-import.js +++ b/tests/unit/customer-import.js @@ -212,11 +212,11 @@ describe('customer import module', () => { const savedCustomer = importer.client.customers.save.getCall(0).args[0] const actual = _.pick( savedCustomer, - ['defaultShippingAddressId', 'defaultBillingAddressId'] + ['defaultShippingAddress', 'defaultBillingAddress'] ) const expected = { - defaultShippingAddressId: 0, - defaultBillingAddressId: 0 + defaultShippingAddress: 0, + defaultBillingAddress: 0 } expect(actual).to.deep.equal(expected) done()