Skip to content
This repository was archived by the owner on Mar 10, 2021. It is now read-only.

Commit

Permalink
Merge pull request #3 from sphereio/bugfix/default-shipping-and-billing
Browse files Browse the repository at this point in the history
default shipping and billing
  • Loading branch information
PhilippSpo authored Jun 27, 2016
2 parents a18ee24 + 8e85b0a commit 4f74d13
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Steps to reproduce the behavior

### Actual behavior

### Expected behavior
6 changes: 6 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/customer-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
166 changes: 126 additions & 40 deletions tests/integration/customer-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -126,30 +123,119 @@ describe('customer import module', function () {

const customer = {
email: '[email protected]',
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: '[email protected]',
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: '[email protected]',
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)
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/customer-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 4f74d13

Please sign in to comment.