Skip to content

Instantly share code, notes, and snippets.

@rubanraj54
Forked from svaj/index.js
Created March 12, 2020 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubanraj54/65cbe7f86381014fbe5db1b792a7b43f to your computer and use it in GitHub Desktop.
Save rubanraj54/65cbe7f86381014fbe5db1b792a7b43f to your computer and use it in GitHub Desktop.
product sync-actions external image example
import { createSyncProducts } from '@commercetools/sync-actions'
import {cloneDeep} from 'lodash'
const syncProducts = createSyncProducts()
const before = {
"id": "4e7ccbb5-0d06-48a8-8ff1-2b17b4bee189",
"version": 1,
"lastMessageSequenceNumber": 1,
"createdAt": "2020-02-14T16:58:52.839Z",
"lastModifiedAt": "2020-02-14T16:58:52.839Z",
"productType": {
"typeId": "product-type",
"id": "93c14bf2-1895-4720-9abc-aae7af6f22bb"
},
"catalogs": [],
"masterData": {
"current": {
"name": {
"en": "product name"
},
"description": {
"en": "Description"
},
"categories": [],
"categoryOrderHints": {},
"slug": {
"en": "01161808"
},
"metaKeywords": {
"en": ""
},
"metaDescription": {
"en": ""
},
"masterVariant": {
"id": 1,
"sku": "11461-036",
"key": "11461-036",
"prices": [
{
"value": {
"type": "centPrecision",
"currencyCode": "USD",
"centAmount": 399,
"fractionDigits": 2
},
"id": "26f258d8-3762-49fb-a7b2-5a8da9eadc65"
}
]
}
}
}
}
before.masterData.staged = cloneDeep(before.masterData.current)
const now = cloneDeep(before)
now.masterData.current.masterVariant.images=[
{
"url": "https://images.commercetools.com/is/image/ctfashion/0006_01161808_0078",
"label": "0006_01161808_0078",
"dimensions": {
"w": 400,
"h": 300
}
}
]
now.masterData.staged = cloneDeep(now.masterData.current)
console.info('before', before.masterData.current)
console.info('after', now.masterData.current)
const actions = syncProducts.buildActions(now.masterData.current, before.masterData.current)
console.info("Sync-actions generated the following actions", actions)
const productsRequest = {
url: `/products/${before.id}`,
method: 'POST',
body: JSON.stringify({ version: before.version, actions }),
}
// send to client to execute, etc.
{
"name": "product-image-test",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"private": true,
"scripts": {
"start": "node -r esm index.js"
},
"dependencies": {
"@commercetools/api-request-builder": "^5.4.2",
"@commercetools/sdk-client": "^2.1.1",
"@commercetools/sdk-middleware-auth": "^6.0.5",
"@commercetools/sdk-middleware-http": "^6.0.5",
"@commercetools/sync-actions": "^4.3.0",
"esm": "^3.2.25",
"lodash": "^4.17.15",
"node-fetch": "^2.6.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment