commercetools / nodejs

Everything related to the Node.js ecosystem for the commercetools platform.
https://commercetools.github.io/nodejs/
MIT License
75 stars 70 forks source link

syncCategories.buildActions returns action in the wrong format (array of arrays) #1895

Open islam3zzat opened 5 months ago

islam3zzat commented 5 months ago

Support reference number

Description

const { createSyncCategories } = require('@commercetools/sync-actions')

const syncCategories = createSyncCategories()

const newCategory = {
  key: 'category-8accca22-4d57-4692-bee0-483e43502088',
  name: {
    de: 'Test',
  },
  slug: {
    en: 'category-8accca22-4d57-4692-bee0-483e43502088-slug',
  },
  assets: [
    {
      key: 'asset-key-1-4ab57b6d-3a0e-46a3-b9b2-539ce4fde618',
      name: {
        en: 'asset name - 1',
      },
      description: {
        en: 'asset description - 1',
      },
      sources: [
        {
          key: 'source-key-138d0808-f4f4-4fa8-a9de-48d803eec277',
          uri: 'https://google.com',
        },
      ],
      tags: ['tag-1', 'tag-2'],
    },
    {
      key: 'asset-key-3-551a2305-25cc-4fb3-a79c-fcb272be3b89',
      name: {
        en: 'asset name - 3',
      },
      description: {
        en: 'asset description - 3',
      },
      sources: [
        {
          key: 'source-key-ca27d6c8-5b65-431f-ad65-21bed9abd2a9',
          uri: 'https://google.com',
        },
      ],
      tags: ['tag-5', 'tag-6'],
    },
    {
      key: 'asset-key-2-986daecf-f793-462f-a598-0ee769af331d',
      name: {
        en: 'asset name - updated',
      },
      description: {
        en: 'asset description - 2',
      },
      sources: [
        {
          key: 'source-key-8ccb652c-6abc-4c6b-bec0-071ca8948106',
          uri: 'https://google.com',
        },
      ],
      tags: ['tag-3', 'tag-4'],
    },
  ],
  orderHint: '0.000017176731611011768329613',
}
const category = {
  id: '5cd1b242-9e27-4199-9cc5-f7057c5f988d',
  version: 1,
  versionModifiedAt: '2024-06-06T11:26:01.104Z',
  lastMessageSequenceNumber: 1,
  createdAt: '2024-06-06T11:26:01.104Z',
  lastModifiedAt: '2024-06-06T11:26:01.104Z',
  lastModifiedBy: {
    clientId: 'REDACTED',
    isPlatformClient: false,
  },
  createdBy: {
    clientId: 'REDACTED',
    isPlatformClient: false,
  },
  key: 'category-8accca22-4d57-4692-bee0-483e43502088',
  name: {
    en: 'Category 8accca22-4d57-4692-bee0-483e43502088',
  },
  slug: {
    en: 'category-8accca22-4d57-4692-bee0-483e43502088-slug',
  },
  ancestors: [],
  orderHint: '0.000017176731611011768329613',
  assets: [
    {
      id: 'a7112cd1-0e67-4b35-84a3-3ede87d392e7',
      sources: [
        {
          uri: 'https://google.com',
          key: 'source-key-138d0808-f4f4-4fa8-a9de-48d803eec277',
        },
      ],
      name: {
        en: 'asset name - 1',
      },
      key: 'asset-key-1-4ab57b6d-3a0e-46a3-b9b2-539ce4fde618',
      description: {
        en: 'asset description - 1',
      },
      tags: ['tag-1', 'tag-2'],
    },
    {
      id: '4ed62080-ce1e-4f92-93e7-e07e2a6e06bc',
      sources: [
        {
          uri: 'https://google.com',
          key: 'source-key-8ccb652c-6abc-4c6b-bec0-071ca8948106',
        },
      ],
      name: {
        en: 'asset name - 2',
      },
      key: 'asset-key-2-986daecf-f793-462f-a598-0ee769af331d',
      description: {
        en: 'asset description - 2',
      },
      tags: ['tag-3', 'tag-4'],
    },
    {
      id: '38acda20-4722-4d84-8bcd-b7a8dc33f096',
      sources: [
        {
          uri: 'https://google.com',
          key: 'source-key-ca27d6c8-5b65-431f-ad65-21bed9abd2a9',
        },
      ],
      name: {
        en: 'asset name - 3',
      },
      key: 'asset-key-3-551a2305-25cc-4fb3-a79c-fcb272be3b89',
      description: {
        en: 'asset description - 3',
      },
      tags: ['tag-5', 'tag-6'],
    },
  ],
}

const onlyAssetNameChanged = {
  ...category,
  assets: category.assets.map((asset, index) => {
    if (index !== 1) return asset

    return {
      ...asset,
      name: {
        ...asset.name,
        en: asset.name.en + ' - updated',
      },
    }
  }),
}

const actions = syncCategories.buildActions(onlyAssetNameChanged, category)
console.log(JSON.stringify(actions, null, 2))

Expected Behavior

I expect to get array of actions


  [
    {
      "action": "removeAsset",
      "assetId": "4ed62080-ce1e-4f92-93e7-e07e2a6e06bc"
    },
    {
      "action": "addAsset",
      "asset": {
        "id": "4ed62080-ce1e-4f92-93e7-e07e2a6e06bc",
        "sources": [
          {
            "uri": "https://google.com",
            "key": "source-key-8ccb652c-6abc-4c6b-bec0-071ca8948106"
          }
        ],
        "name": {
          "en": "asset name - 2 - updated"
        },
        "key": "asset-key-2-986daecf-f793-462f-a598-0ee769af331d",
        "description": {
          "en": "asset description - 2"
        },
        "tags": [
          "tag-3",
          "tag-4"
        ]
      }
    }
  ]

Current Behavior

Currently I get, array of array of actions

[
  [
    {
      "action": "removeAsset",
      "assetId": "4ed62080-ce1e-4f92-93e7-e07e2a6e06bc"
    },
    {
      "action": "addAsset",
      "asset": {
        "id": "4ed62080-ce1e-4f92-93e7-e07e2a6e06bc",
        "sources": [
          {
            "uri": "https://google.com",
            "key": "source-key-8ccb652c-6abc-4c6b-bec0-071ca8948106"
          }
        ],
        "name": {
          "en": "asset name - 2 - updated"
        },
        "key": "asset-key-2-986daecf-f793-462f-a598-0ee769af331d",
        "description": {
          "en": "asset description - 2"
        },
        "tags": [
          "tag-3",
          "tag-4"
        ]
      }
    }
  ]
]

Context

Possible Solution

islam3zzat commented 5 months ago

deleting the asset id helps to get the correct format

const onlyAssetNameChanged = {
  ...category,
  assets: category.assets.map((asset, index) => {
    const nextAsset = { ...asset }
    delete nextAsset.id
    if (index !== 1) return nextAsset

    return {
      ...asset,
      name: {
        ...asset.name,
        en: asset.name.en + ' - updated',
      },
    }
  }),
}