Skip to content

Commit 50f3232

Browse files
authored
Merge branch 'master' into TypeScript-Repo-Notification
2 parents 55de2cc + be0b7ab commit 50f3232

File tree

8 files changed

+235
-14
lines changed

8 files changed

+235
-14
lines changed

CHANGELOG.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,58 @@
1+
## @commercetools/sync-actions@4.8.1 (2020-07-14)
2+
3+
#### :bug: Bug Fix
4+
5+
- `sync-actions`
6+
- [#1580](https://github.com/commercetools/nodejs/pull/1580) Add array check for copy empty array props ([@markus-azer](https://github.com/markus-azer))
7+
8+
#### Committers: 1
9+
10+
- Markus Azer ([@markus-azer](https://github.com/markus-azer))
11+
12+
## @commercetools/state-importer@3.0.24 (2020-07-14)
13+
14+
#### :bug: Bug Fix
15+
16+
- `sync-actions`
17+
- [#1580](https://github.com/commercetools/nodejs/pull/1580) Add array check for copy empty array props ([@markus-azer](https://github.com/markus-azer))
18+
19+
#### Committers: 1
20+
21+
- Markus Azer ([@markus-azer](https://github.com/markus-azer))
22+
23+
## @commercetools/integration-tests@0.1.2 (2020-07-14)
24+
25+
#### :bug: Bug Fix
26+
27+
- `sync-actions`
28+
- [#1580](https://github.com/commercetools/nodejs/pull/1580) Add array check for copy empty array props ([@markus-azer](https://github.com/markus-azer))
29+
30+
#### Committers: 1
31+
32+
- Markus Azer ([@markus-azer](https://github.com/markus-azer))
33+
34+
## @commercetools/discount-code-importer@3.0.23 (2020-07-14)
35+
36+
#### :bug: Bug Fix
37+
38+
- `sync-actions`
39+
- [#1580](https://github.com/commercetools/nodejs/pull/1580) Add array check for copy empty array props ([@markus-azer](https://github.com/markus-azer))
40+
41+
#### Committers: 1
42+
43+
- Markus Azer ([@markus-azer](https://github.com/markus-azer))
44+
45+
## @commercetools/custom-objects-importer@3.0.24 (2020-07-14)
46+
47+
#### :bug: Bug Fix
48+
49+
- `sync-actions`
50+
- [#1580](https://github.com/commercetools/nodejs/pull/1580) Add array check for copy empty array props ([@markus-azer](https://github.com/markus-azer))
51+
52+
#### Committers: 1
53+
54+
- Markus Azer ([@markus-azer](https://github.com/markus-azer))
55+
156
## @commercetools/sync-actions@4.8.0 (2020-06-18)
257

358
#### :rocket: New Feature

integration-tests/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@commercetools/integration-tests",
4-
"version": "0.1.1",
4+
"version": "0.1.2",
55
"dependencies": {
66
"@commercetools/api-request-builder": "^5.6.1",
77
"@commercetools/category-exporter": "^3.0.16",
@@ -10,11 +10,11 @@
1010
"@commercetools/csv-parser-price": "^4.0.17",
1111
"@commercetools/csv-parser-state": "^3.0.15",
1212
"@commercetools/custom-objects-exporter": "^3.0.16",
13-
"@commercetools/custom-objects-importer": "^3.0.23",
13+
"@commercetools/custom-objects-importer": "^3.0.24",
1414
"@commercetools/customer-groups-exporter": "^3.0.16",
1515
"@commercetools/discount-code-exporter": "^4.0.16",
1616
"@commercetools/discount-code-generator": "^2.0.9",
17-
"@commercetools/discount-code-importer": "^3.0.22",
17+
"@commercetools/discount-code-importer": "^3.0.23",
1818
"@commercetools/get-credentials": "^3.0.16",
1919
"@commercetools/http-user-agent": "2.x",
2020
"@commercetools/inventories-exporter": "^3.0.17",
@@ -31,8 +31,8 @@
3131
"@commercetools/sdk-middleware-logger": "2.x",
3232
"@commercetools/sdk-middleware-queue": "^2.1.4",
3333
"@commercetools/sdk-middleware-user-agent": "^2.1.5",
34-
"@commercetools/state-importer": "^3.0.23",
35-
"@commercetools/sync-actions": "^4.8.0",
34+
"@commercetools/state-importer": "^3.0.24",
35+
"@commercetools/sync-actions": "^4.8.1",
3636
"@commercetools/typescript-sdk": "^0.0.16",
3737
"bluebird": "^3.5.3",
3838
"common-tags": "^1.8.0",

packages/custom-objects-importer/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"access": "public"
55
},
66
"name": "@commercetools/custom-objects-importer",
7-
"version": "3.0.23",
7+
"version": "3.0.24",
88
"description": "Import custom objects to the commercetools platform",
99
"main": "lib/main.js",
1010
"scripts": {
@@ -37,7 +37,7 @@
3737
"@commercetools/sdk-middleware-auth": "^5.1.6",
3838
"@commercetools/sdk-middleware-http": "^6.0.7",
3939
"@commercetools/sdk-middleware-user-agent": "^2.1.5",
40-
"@commercetools/sync-actions": "^4.8.0",
40+
"@commercetools/sync-actions": "^4.8.1",
4141
"common-tags": "^1.8.0",
4242
"lodash.compact": "^3.0.1",
4343
"lodash.isequal": "^4.5.0",

packages/discount-code-importer/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"access": "public"
55
},
66
"name": "@commercetools/discount-code-importer",
7-
"version": "3.0.22",
7+
"version": "3.0.23",
88
"description": "Import discount codes to the commercetools platform.",
99
"keywords": [
1010
"commercetools",
@@ -39,7 +39,7 @@
3939
"@commercetools/sdk-middleware-http": "^6.0.7",
4040
"@commercetools/sdk-middleware-queue": "^2.1.4",
4141
"@commercetools/sdk-middleware-user-agent": "^2.1.5",
42-
"@commercetools/sync-actions": "^4.8.0",
42+
"@commercetools/sync-actions": "^4.8.1",
4343
"lodash": "^4.17.11",
4444
"node-fetch": "^2.3.0",
4545
"npmlog": "^4.1.2"

packages/state-importer/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"access": "public"
55
},
66
"name": "@commercetools/state-importer",
7-
"version": "3.0.23",
7+
"version": "3.0.24",
88
"description": "Import states to the commercetools platform",
99
"main": "lib/main.js",
1010
"files": [
@@ -39,7 +39,7 @@
3939
"@commercetools/sdk-middleware-auth": "^5.1.6",
4040
"@commercetools/sdk-middleware-http": "^6.0.7",
4141
"@commercetools/sdk-middleware-user-agent": "^2.1.5",
42-
"@commercetools/sync-actions": "^4.8.0",
42+
"@commercetools/sync-actions": "^4.8.1",
4343
"node-fetch": "^2.3.0"
4444
},
4545
"devDependencies": {

packages/sync-actions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"access": "public"
55
},
66
"name": "@commercetools/sync-actions",
7-
"version": "4.8.0",
7+
"version": "4.8.1",
88
"description": "Build API update actions for the commercetools platform.",
99
"keywords": [
1010
"commercetools",

packages/sync-actions/src/utils/copy-empty-array-props.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import isNil from 'lodash.isnil'
22

3+
const CUSTOM = 'custom'
4+
35
/**
46
* @function copyEmptyArrayProps
5-
* @description Takes two objects and if there are Array props in oldObj which doesn't exist in newObj, then copy it with an empty value.
7+
* @description Create new key with empty array value on `newobj` for the arrays exist on `oldObj` and doesnt exist on `newobj`
8+
* One use case is to easily compare two object without generating this error `Cannot read property '0' of undefined`
69
* @param {Object} oldObj
710
* @param {Object} newObj
811
* @returns {Array} Ordered Array [oldObj, newObj]
@@ -15,13 +18,49 @@ export default function copyEmptyArrayProps(oldObj = {}, newObj = {}) {
1518
...newObj,
1619
...nextObject,
1720
}
21+
22+
// Ignore CUSTOM key as this object is dynamic and its up to the user to dynamically change it
23+
// todo, it would be better if we pass it as ignored keys param
24+
if (key === CUSTOM) return merged
25+
26+
if (Array.isArray(value) && newObj[key] && newObj[key].length >= 1) {
27+
/* eslint-disable no-plusplus */
28+
for (let i = 0; i < newObj[key].length; i++) {
29+
if (
30+
!isNil(newObj[key][i]) &&
31+
typeof newObj[key][i] === 'object' &&
32+
!isNil(newObj[key][i].id)
33+
) {
34+
// Since its unordered array elements then check if the element on `oldObj` exists by id
35+
const foundObject = value.find(
36+
(v) => Number(v.id) === Number(newObj[key][i].id)
37+
)
38+
if (!isNil(foundObject)) {
39+
const [, nestedObject] = copyEmptyArrayProps(
40+
foundObject,
41+
newObj[key][i]
42+
)
43+
/* eslint-disable no-param-reassign */
44+
newObj[key][i] = nestedObject
45+
}
46+
}
47+
}
48+
49+
return merged
50+
}
1851
if (Array.isArray(value)) {
1952
return {
2053
...merged,
2154
[key]: isNil(newObj[key]) ? [] : newObj[key],
2255
}
2356
}
24-
if (!isNil(newObj[key]) && typeof value === 'object') {
57+
if (
58+
!isNil(newObj[key]) &&
59+
typeof value === 'object' &&
60+
// Ignore Date as this will create invalid object since typeof date === 'object' return true
61+
// ex: {date: new Date()} will result {date: {}}
62+
!(value instanceof Date)
63+
) {
2564
const [, nestedObject] = copyEmptyArrayProps(value, newObj[key])
2665
return {
2766
...merged,

packages/sync-actions/test/utils/copy-empty-array-props.spec.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,133 @@ test('should add empty array for `nestedObject`, `nestedObjectOne`', () => {
147147
})
148148
})
149149

150+
test('should init empty arrays into nested objects', () => {
151+
const oldObj = {
152+
variants: [
153+
{
154+
id: 1,
155+
prices: [
156+
{
157+
id: 1,
158+
customerGroup: [],
159+
},
160+
{
161+
id: 2,
162+
customerGroup: [],
163+
},
164+
],
165+
assets: [],
166+
},
167+
{
168+
id: 2,
169+
},
170+
{
171+
id: 3,
172+
prices: [],
173+
},
174+
{
175+
id: 4,
176+
att: '44444',
177+
prices: [],
178+
},
179+
],
180+
}
181+
182+
const newObj = {
183+
variants: [
184+
{
185+
id: 1,
186+
prices: [
187+
{
188+
id: 1,
189+
att: '11111',
190+
p: [],
191+
},
192+
{
193+
id: 2,
194+
att: '22222',
195+
},
196+
{
197+
id: 3,
198+
att: '333333',
199+
},
200+
],
201+
att: 'anything',
202+
},
203+
{
204+
id: 4,
205+
},
206+
],
207+
}
208+
const [old, fixedNewObj] = copyEmptyArrayProps(oldObj, newObj)
209+
210+
expect(old).toEqual(oldObj)
211+
expect(fixedNewObj).toEqual({
212+
variants: [
213+
{
214+
id: 1,
215+
prices: [
216+
{
217+
id: 1,
218+
att: '11111',
219+
p: [],
220+
customerGroup: [],
221+
},
222+
{
223+
id: 2,
224+
att: '22222',
225+
customerGroup: [],
226+
},
227+
{
228+
id: 3,
229+
att: '333333',
230+
},
231+
],
232+
att: 'anything',
233+
assets: [],
234+
},
235+
{
236+
id: 4,
237+
prices: [],
238+
},
239+
],
240+
})
241+
})
242+
243+
test('should ignore dates', () => {
244+
const dateNow = new Date()
245+
const oldObj = {
246+
variants: [
247+
{
248+
id: 1,
249+
prices: [],
250+
date: dateNow,
251+
},
252+
],
253+
}
254+
255+
const newObj = {
256+
variants: [
257+
{
258+
id: 1,
259+
date: dateNow,
260+
},
261+
],
262+
}
263+
const [old, fixedNewObj] = copyEmptyArrayProps(oldObj, newObj)
264+
265+
expect(old).toEqual(oldObj)
266+
expect(fixedNewObj).toEqual({
267+
variants: [
268+
{
269+
id: 1,
270+
prices: [],
271+
date: dateNow,
272+
},
273+
],
274+
})
275+
})
276+
150277
test('shouldnt mutate `newObj`', () => {
151278
const oldObj = {
152279
emptyArray: [],

0 commit comments

Comments
 (0)