Advertisement
informaticage

New Merge testing

Mar 29th, 2021
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mapContentBlocksByCmsBlockUID = function (cmsBlocks) {
  2.   const mapByCmsBlockUID = {};
  3.   // console.log();
  4.   cmsBlocks.forEach((cmsItem, index) => {
  5.     // Assigning owner
  6.     // console.log("SONO NELL'iterazione: ", cmsItem);
  7.     cmsItem["cmsCustomBlockPosition"] = index;
  8.  
  9.     if (!cmsItem.cmsBlockOwner) {
  10.       cmsItem.cmsBlockOwner = getOwnerTypeByReferenceType(
  11.         cmsItem.referenceType
  12.       );
  13.     }
  14.     if (cmsItem.cmsBlockUID) {
  15.       mapByCmsBlockUID[cmsItem.cmsBlockUID] = cmsItem;
  16.     } else {
  17.       cmsItem.cmsBlockUID = generateRandomCmsBlockUID();
  18.       mapByCmsBlockUID[cmsItem.cmsBlockUID] = cmsItem;
  19.     }
  20.  
  21.     Object.keys(cmsItem).forEach((cmsItemKey) => {
  22.       if (Array.isArray(cmsItem[cmsItemKey])) {
  23.         cmsItem[cmsItemKey] = mapContentBlocksByCmsBlockUID(
  24.           cmsItem[cmsItemKey]
  25.         );
  26.       }
  27.     });
  28.   });
  29.  
  30.   return mapByCmsBlockUID;
  31. };
  32.  
  33. const mergeContents = function (masterObj, slaveObj) {
  34.   const mergeFirstLevel = (masterObj, slaveObj) => {
  35.     if (!masterObj || !slaveObj) {
  36.       return null;
  37.     }
  38.  
  39.     const masterContent = mapContentBlocksByCmsBlockUID(
  40.       masterObj["content"]["blocks"] || masterObj["content"]["pillars"]
  41.     );
  42.     const slaveContent = mapContentBlocksByCmsBlockUID(
  43.       slaveObj["content"]["blocks"] || slaveObj["content"]["pillars"]
  44.     );
  45.  
  46.     mergeDeepLevels(masterContent, slaveContent);
  47.  
  48.     // 'title', 'internalTitle', 'sitemapId', 'parentId' ""
  49.     const fieldToOverrideFromHq = [];
  50.     fieldToOverrideFromHq.forEach((key) => (slaveObj[key] = masterObj[key]));
  51.  
  52.     return slaveObj;
  53.   };
  54.  
  55.   const mergeDeepLevels = (hqContent, dealerContent) => {
  56.     if (!hqContent || !dealerContent) {
  57.       return null;
  58.     }
  59.     const toCheckForOverride = Object.keys(hqContent);
  60.  
  61.     for (const key of toCheckForOverride) {
  62.       // console.log('Checking key', key);
  63.       if (isObject(hqContent[key])) {
  64.         // console.log('HQ is object', key);
  65.         /// hqContent[key] is an object
  66.         if (isEmptyOrNotObject(dealerContent[key])) {
  67.           // console.log('HQ is object and dealer is not or is empty', key);
  68.           // Both are objects and dealer is empty
  69.           // console.log('Overwrite 1', key);
  70.           dealerContent[key] = { ...hqContent[key] };
  71.         } else {
  72.           // console.log('HQ is object and dealer is object and not empty', key);
  73.           // Either dealer is not an object or is an empty object
  74.           // dealerContect did not previusly containt key
  75.           if (
  76.             hqContent[key].isLocked === undefined &&
  77.             hqContent.isLocked &&
  78.             key !== "items"
  79.           ) {
  80.             // console.log('Overwrite 2', key);
  81.             dealerContent[key] = { ...hqContent[key] };
  82.           } else {
  83.             mergeDeepLevels(hqContent[key], dealerContent[key]);
  84.           }
  85.         }
  86.       } else {
  87.         /// Is primitive
  88.         // console.log('HQ is primitive', key);
  89.         if (hqContent.isLocked === undefined) {
  90.           // console.log('HqContent locked is undefined');
  91.           dealerContent = {};
  92.           dealerContent[key] = hqContent[key];
  93.         }
  94.         if (hqContent.isLocked) {
  95.           // console.log('HQ is primitive and locked', key);
  96.           dealerContent[key] = hqContent[key];
  97.         }
  98.  
  99.         dealerContent.isPinned = hqContent.isPinned;
  100.       }
  101.     }
  102.  
  103.     if (dealerContent.items) {
  104.       const keys = Object.keys(dealerContent.items);
  105.       const itemsAsArray = Object.entries(dealerContent.items).map(
  106.         ([k, v]) => v
  107.       );
  108.       dealerContent.items = itemsAsArray;
  109.     }
  110.  
  111.     if (dealerContent.menuItems) {
  112.       const keys = Object.keys(dealerContent.menuItems);
  113.       const itemsAsArray = Object.entries(dealerContent.menuItems).map(
  114.         ([k, v]) => v
  115.       );
  116.       dealerContent.menuItems = itemsAsArray;
  117.     }
  118.   };
  119.   // console.log('Prima del merge: ', slaveObj);
  120.   return mergeFirstLevel(masterObj, slaveObj);
  121. };
  122.  
  123. const master_items = [
  124.   {
  125.     type: "BoxGridRow",
  126.     cmsBlockUID: "09ea55b0-14cf-4e02-bf68-33750991f571",
  127.     cmsBlockOwner: "hq",
  128.     isLocked: false,
  129.     isPinned: false,
  130.     items: [
  131.       {
  132.         cmsBlockUID: "5846ac58-9c8f-4094-8ff8-5a80295dbda2",
  133.         cmsBlockOwner: "hq",
  134.         isLocked: true,
  135.         isPinned: false,
  136.         preTitle: "test-new-validate",
  137.         title: "test-new-validate",
  138.         subTitle: "test-new-validate",
  139.         description: "test-new-validate",
  140.         width: "33%",
  141.         media: {
  142.           landscapeBig: {
  143.             id: "603794de41c6d945e01235d3",
  144.           },
  145.           portraitMedium: {
  146.             id: "603794de41c6d945e01235d3",
  147.           },
  148.         },
  149.         thronPlaylistId: "603794de41c6d945e01235d3",
  150.         cta: null,
  151.         ctaType: "standard",
  152.         filterValues: {
  153.           filter1: "",
  154.           filter2: "",
  155.           filter3: "",
  156.         },
  157.         cmsCustomBlockPosition: 0,
  158.       },
  159.       {
  160.         cmsBlockUID: "8156c2e3-8657-42e3-abb7-e98c8cf38280",
  161.         cmsBlockOwner: "hq",
  162.         isLocked: true,
  163.         isPinned: false,
  164.         preTitle: "test-new-validate",
  165.         title: "test-new-validate",
  166.         subTitle: "test-new-validate",
  167.         description: "test-new-validate",
  168.         width: "33%",
  169.         media: {
  170.           landscapeBig: {
  171.             id: "603794de41c6d945e01235d3",
  172.           },
  173.           portraitMedium: {
  174.             id: "603794de41c6d945e01235d3",
  175.           },
  176.         },
  177.         thronPlaylistId: "603794de41c6d945e01235d3",
  178.         cta: null,
  179.         ctaType: "standard",
  180.         filterValues: {
  181.           filter1: "",
  182.           filter2: "",
  183.           filter3: "",
  184.         },
  185.         cmsCustomBlockPosition: 1,
  186.       },
  187.     ],
  188.     cmsCustomBlockPosition: 0,
  189.   },
  190. ];
  191.  
  192. const slave_items = [
  193.   {
  194.     type: "BoxGridRow",
  195.     cmsBlockUID: "09ea55b0-14cf-4e02-bf68-33750991f571",
  196.     cmsBlockOwner: "hq",
  197.     isLocked: false,
  198.     isPinned: false,
  199.     items: [
  200.       {
  201.         cmsBlockUID: "5846ac58-9c8f-4094-8ff8-5a80295dbda2",
  202.         cmsBlockOwner: "hq",
  203.         isLocked: true,
  204.         isPinned: false,
  205.         preTitle: "test-new-validate-NOTHERE",
  206.         title: "test-new-validate",
  207.         subTitle: "test-new-validate",
  208.         description: "test-new-validate",
  209.         width: "33%",
  210.         media: {
  211.           landscapeBig: {
  212.             id: "603794de41c6d945e01235d3",
  213.           },
  214.           portraitMedium: {
  215.             id: "603794de41c6d945e01235d3",
  216.           },
  217.         },
  218.         thronPlaylistId: "603794de41c6d945e01235d3",
  219.         cta: null,
  220.         ctaType: "standard",
  221.         filterValues: {
  222.           filter1: "",
  223.           filter2: "",
  224.           filter3: "",
  225.         },
  226.         cmsCustomBlockPosition: 0,
  227.       },
  228.       {
  229.         cmsBlockUID: "8156c2e3-8657-42e3-abb7-e98c8cf38280",
  230.         cmsBlockOwner: "hq",
  231.         isLocked: false,
  232.         isPinned: false,
  233.         preTitle: "test-new-validate",
  234.         title: "test-new-validate-YESHERE",
  235.         subTitle: "test-new-validate",
  236.         description: "test-new-validate",
  237.         width: "33%",
  238.         media: {
  239.           landscapeBig: {
  240.             id: "603794de41c6d945e01235d3",
  241.           },
  242.           portraitMedium: {
  243.             id: "603794de41c6d945e01235d3",
  244.           },
  245.         },
  246.         thronPlaylistId: "603794de41c6d945e01235d3",
  247.         cta: null,
  248.         ctaType: "standard",
  249.         filterValues: {
  250.           filter1: "",
  251.           filter2: "",
  252.           filter3: "",
  253.         },
  254.         cmsCustomBlockPosition: 1,
  255.       },
  256.     ],
  257.     cmsCustomBlockPosition: 0,
  258.   },
  259. ];
  260.  
  261. const master = {
  262.   _id: "605dbb1614600dfa17334244",
  263.   reference: null,
  264.   isHiddenFromSitemap: false,
  265.   ignoreInheritance: false,
  266.   isContentLocked: false,
  267.   isCreatedByHq: true,
  268.   sitemapId: "home-demo-SR",
  269.   sitemapPriority: "1",
  270.   parentId: null,
  271.   title: "Home Demo SR",
  272.   internalTitle: "Home Demo SR",
  273.   locale: "en-GB",
  274.   metadata: [
  275.     {
  276.       name: "robots",
  277.       content: "index-follow",
  278.     },
  279.     {
  280.       name: "description",
  281.       content: "Home Demo SR",
  282.     },
  283.     {
  284.       name: "keywords",
  285.       content: "Home Demo SR",
  286.     },
  287.     {
  288.       property: "og:title",
  289.       content: "Home Demo SR",
  290.     },
  291.     {
  292.       property: "og:type",
  293.       content: "website",
  294.     },
  295.     {
  296.       property: "og:description",
  297.       content: "Home Demo SR",
  298.     },
  299.     {
  300.       property: "og:url",
  301.       content: "Home Demo SR",
  302.     },
  303.     {
  304.       property: "og:image",
  305.       content: "",
  306.     },
  307.     {
  308.       property: "twitter:title",
  309.       content: "Home Demo SR",
  310.     },
  311.     {
  312.       property: "twitter:description",
  313.       content: "Home Demo SR",
  314.     },
  315.     {
  316.       property: "twitter:image",
  317.       content: "",
  318.     },
  319.   ],
  320.   content: {
  321.     blocks: master_items,
  322.   },
  323.   slug: "Home-Demo-SR",
  324.   status: "draft",
  325.   publishedAt: null,
  326.   original: "605c9631c0b66bd61c686f25",
  327.   createdAt: "2021-03-26T10:44:38.252Z",
  328.   updatedAt: "2021-03-26T10:44:39.421Z",
  329.   revision: 4,
  330.   active: true,
  331.   ownedByDealer: "",
  332.   referenceType: "group",
  333.   disableEditing: false,
  334.   selected: true,
  335.   createdBy: "6022f67fae4c426d52e1a30b",
  336.   expireAt: null,
  337. };
  338.  
  339. const slave = {
  340.   _id: "605dbb1614600dfa17334244",
  341.   reference: null,
  342.   isHiddenFromSitemap: false,
  343.   ignoreInheritance: false,
  344.   isContentLocked: false,
  345.   isCreatedByHq: true,
  346.   sitemapId: "home-demo-SR",
  347.   sitemapPriority: "1",
  348.   parentId: null,
  349.   title: "Home Demo SR",
  350.   internalTitle: "Home Demo SR",
  351.   locale: "en-GB",
  352.   metadata: [
  353.     {
  354.       name: "robots",
  355.       content: "index-follow",
  356.     },
  357.     {
  358.       name: "description",
  359.       content: "Home Demo SR",
  360.     },
  361.     {
  362.       name: "keywords",
  363.       content: "Home Demo SR",
  364.     },
  365.     {
  366.       property: "og:title",
  367.       content: "Home Demo SR",
  368.     },
  369.     {
  370.       property: "og:type",
  371.       content: "website",
  372.     },
  373.     {
  374.       property: "og:description",
  375.       content: "Home Demo SR",
  376.     },
  377.     {
  378.       property: "og:url",
  379.       content: "Home Demo SR",
  380.     },
  381.     {
  382.       property: "og:image",
  383.       content: "",
  384.     },
  385.     {
  386.       property: "twitter:title",
  387.       content: "Home Demo SR",
  388.     },
  389.     {
  390.       property: "twitter:description",
  391.       content: "Home Demo SR",
  392.     },
  393.     {
  394.       property: "twitter:image",
  395.       content: "",
  396.     },
  397.   ],
  398.   content: {
  399.     blocks: slave_items,
  400.   },
  401.   slug: "Home-Demo-SR",
  402.   status: "draft",
  403.   publishedAt: null,
  404.   original: "605c9631c0b66bd61c686f25",
  405.   createdAt: "2021-03-26T10:44:38.252Z",
  406.   updatedAt: "2021-03-26T10:44:39.421Z",
  407.   revision: 4,
  408.   active: true,
  409.   ownedByDealer: "",
  410.   referenceType: "group",
  411.   disableEditing: false,
  412.   selected: true,
  413.   createdBy: "6022f67fae4c426d52e1a30b",
  414.   expireAt: null,
  415. };
  416.  
  417. const isObject = (item) => item && typeof item === "object";
  418. const isEmptyOrNotObject = (obj) =>
  419.   !isObject(obj) || Object.keys(obj).length === 0;
  420.  
  421. const resMerged = mergeContents(master, slave);
  422. console.log(JSON.stringify(resMerged, null, 2));
  423.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement