Advertisement
informaticage

merge items - 2

Jan 29th, 2021
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mergeContents = (hqObj, dealerObj) => {
  2.   const mergeFirstLevel = (hqObj, dealerObj) => {
  3.     const toInheritFromDealer = [
  4.       "dealerCode",
  5.       "isCreatedByHq",
  6.       "revision",
  7.       "status",
  8.       "original",
  9.       "createdBy",
  10.       "updatedBy",
  11.       "createdAt",
  12.       "updatedAt",
  13.       "expireAt",
  14.       "active",
  15.     ];
  16.  
  17.     // Every key from HQ except the keys in toInheritFromDealer
  18.     const toCheckForOverride = Object.keys(hqObj).filter(
  19.       (_) => !toInheritFromDealer.includes(_)
  20.     );
  21.  
  22.     /// For each key to be overrided
  23.     for (const key of toCheckForOverride) {
  24.       if (key === "content") {
  25.         mergeDeepLevels(
  26.           hqObj["content"]["blocks"],
  27.           dealerObj["content"]["blocks"]
  28.         );
  29.       } else {
  30.         dealerObj[key] = hqObj[key];
  31.       }
  32.     }
  33.  
  34.     return dealerObj;
  35.   };
  36.  
  37.   const mergeDeepLevels = (hqContent, dealerContent) => {
  38.     const toCheckForOverride = Object.keys(hqContent);
  39.  
  40.     for (const key of toCheckForOverride) {
  41.       console.log('Checking key', key);
  42.       if (isObject(hqContent[key])) {
  43.         console.log('HQ is object', key);
  44.         /// hqContent[key] is an object
  45.         if (isEmptyOrNotObject(dealerContent[key])) {
  46.           console.log('HQ is object and dealer is not or is empty', key);
  47.           // Both are objects and dealer is empty
  48.           dealerContent[key] = {...hqContent[key]};
  49.         } else {
  50.           console.log('HQ is object and dealer is object and not empty', key);
  51.           // Either dealer is not an object or is an empty object
  52.           // dealerContect did not previusly containt key
  53.           if(hqContent[key].isLocked === undefined && hqContent.isLocked) {
  54.             dealerContent[key] = { ...hqContent[key] };
  55.           } else {
  56.             mergeDeepLevels(hqContent[key], dealerContent[key]);
  57.           }
  58.         }
  59.       } else {
  60.         /// Is primitive
  61.         console.log('HQ is primitive', key);
  62.         if(hqContent.isLocked === undefined) {
  63.             dealerContent = {};
  64.             dealerContent[key] = hqContent[key];
  65.         }
  66.         if (hqContent.isLocked) {
  67.             console.log('HQ is primitive and locked', key);
  68.             dealerContent[key] = hqContent[key];
  69.         }
  70.       }
  71.     }
  72.   };
  73.  
  74.   return mergeFirstLevel(hqObj, dealerObj);
  75. };
  76. const isObject = (item) => item && typeof item === "object";
  77.  
  78. const isEmptyOrNotObject = (obj) => !isObject(obj) || Object.keys(obj).length === 0;
  79.  
  80. let hq = {
  81.   "dealerCode":"052222",
  82.   "isCreatedByHq":false,
  83.   "sitemapId":"Pagina 4",
  84.   "parentId":null,
  85.   "title":"Pagina 4",
  86.   "internalTitle":"Pagina 4",
  87.   "locale":"it",
  88.   "content":{
  89.      "blocks":[
  90.         {
  91.            "type":"DWSCoverCarousel",
  92.            "isLocked":true,
  93.            "isPinned":false,
  94.            "scrollpointer":false,
  95.            "items":[
  96.               {
  97.                  "type":"DwsCover",
  98.                  "isLocked":true,
  99.                  "isPinned":false,
  100.                  "owner":"hidden",
  101.                  "palette":"dark",
  102.                  "gradient":false,
  103.                  "title":"Title Slide 1",
  104.                  "preTitle":"Pre Title Slide 1",
  105.                  "chapter":{
  106.                     "name":"Chapter Slide 1",
  107.                     "nav":false
  108.                  },
  109.                  "scrollIndicator":false,
  110.                  "link":{
  111.                     "content":"Link Content Slide 1",
  112.                     "title":"Link Title Slide 1",
  113.                     "url":"Link Url Slide 1",
  114.                     "target":"_blank"
  115.                  },
  116.                  "cta2":{
  117.                     "content":"Cta 2 Content Slide 1",
  118.                     "url":"Cta 2 Url Slide 1",
  119.                     "target":"_blank"
  120.                  },
  121.                  "cta3":null,
  122.                  "background":null
  123.               },
  124.               {
  125.                  "type":"DwsCover",
  126.                  "isLocked":false,
  127.                  "isPinned":false,
  128.                  "owner":"hidden",
  129.                  "palette":"",
  130.                  "gradient":false,
  131.                  "title":"Title Slide 2",
  132.                  "preTitle":"Pre Title Slide 2",
  133.                  "chapter":{
  134.                     "name":"Chapter Slide 2",
  135.                     "nav":false
  136.                  },
  137.                  "scrollIndicator":false,
  138.                  "link":{
  139.                     "content":"Link Content Slide 2",
  140.                     "title":"Link Title Slide 2",
  141.                     "url":"Link url Slide 2",
  142.                     "target":"_blank"
  143.                  },
  144.                  "cta2":{
  145.                     "content":"Cta 2 Content Slide 2",
  146.                     "url":"Cta 2 Url Slide 2",
  147.                     "target":"_blank"
  148.                  },
  149.                  "cta3":null,
  150.                  "background":null
  151.               }
  152.            ]
  153.         }
  154.      ]
  155.   },
  156.   "slug":"pagina-4",
  157.   "status":"draft",
  158.   "id":"6013ea9d072b325aa8ed856c",
  159.   "original":"6013ea9d072b325aa8ed856c",
  160.   "createdBy":"5ff7784e74d73ea4d1e43501",
  161.   "updatedBy":null,
  162.   "createdAt":"2021-01-29T10:59:41.280Z",
  163.   "updatedAt":"2021-01-29T10:59:42.141Z",
  164.   "expireAt":null,
  165.   "revision":0,
  166.   "active":true
  167. };
  168.  
  169. let dealer = {
  170.   "dealerCode":"052222",
  171.   "isCreatedByHq":false,
  172.   "sitemapId":"Pagina 4",
  173.   "parentId":null,
  174.   "title":"Pagina 4",
  175.   "internalTitle":"Pagina 4",
  176.   "locale":"it",
  177.   "content":{
  178.      "blocks":[
  179.         {
  180.            "type":"DWSCoverCarousel",
  181.            "isLocked":true,
  182.            "isPinned":false,
  183.            "scrollpointer":true,
  184.            "items":[
  185.               {
  186.                  "type":"DwsCover",
  187.                  "isLocked":true,
  188.                  "isPinned":false,
  189.                  "owner":"hidden",
  190.                  "palette":"dark",
  191.                  "gradient":false,
  192.                  "title":"Title Slide 1",
  193.                  "preTitle":"Pre Title Slide 1",
  194.                  "chapter":{
  195.                     "name":"Chapter Slide 1",
  196.                     "nav":false
  197.                  },
  198.                  "scrollIndicator":false,
  199.                  "link":{
  200.                     "content":"Link Content Slide 1",
  201.                     "title":"Link Title Slide 1",
  202.                     "url":"Link Url Slide 1",
  203.                     "target":"_blank"
  204.                  },
  205.                  "cta2":{
  206.                     "content":"Cta 2 Content Slide 1",
  207.                     "url":"Cta 2 Url Slide 1",
  208.                     "target":"_blank"
  209.                  },
  210.                  "cta3":null,
  211.                  "background":null
  212.               },
  213.               {
  214.                  "type":"DwsCover",
  215.                  "isLocked":false,
  216.                  "isPinned":false,
  217.                  "owner":"hidden",
  218.                  "palette":"",
  219.                  "gradient":false,
  220.                  "title":"Title Slide 2",
  221.                  "preTitle":"Pre Title Slide 2",
  222.                  "chapter":{
  223.                     // "name":"Chapter Slide 2",
  224.                     // "nav":false
  225.                  },
  226.                  "scrollIndicator":false,
  227.                  "link":{
  228.                     "content":"Link Content Slide 2",
  229.                     "title":"Link Title Slide 2",
  230.                     "url":"Link url Slide 2",
  231.                     "target":"_blank"
  232.                  },
  233.                  "cta2":{
  234.                     "content":"Cta 2 Content Slide 2",
  235.                     "url":"Cta 2 Url Slide 2",
  236.                     "target":"_blank"
  237.                  },
  238.                  "cta3":null,
  239.                  "background":null
  240.               }
  241.            ]
  242.         }
  243.      ]
  244.   },
  245.   "slug":"pagina-4",
  246.   "status":"draft",
  247.   "id":"6013ea9d072b325aa8ed856c",
  248.   "original":"6013ea9d072b325aa8ed856c",
  249.   "createdBy":"5ff7784e74d73ea4d1e43501",
  250.   "updatedBy":null,
  251.   "createdAt":"2021-01-29T10:59:41.280Z",
  252.   "updatedAt":"2021-01-29T10:59:42.141Z",
  253.   "expireAt":null,
  254.   "revision":0,
  255.   "active":true
  256. };
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276. let _hq = {
  277.   dealerCode: null,
  278.   isCreatedByHq: true,
  279.   sitemapId: "Pagina Rossocorsa",
  280.   parentId: null,
  281.   title: "Pagina Rossocorsa",
  282.   internalTitle: "Pagina Rossocorsa",
  283.   locale: "it",
  284.   content: {
  285.     blocks: [
  286.       {
  287.         type: "TextBlock",
  288.         isLocked: true,
  289.         isPinned: false,
  290.         palette: "",
  291.         variant: "text_centered",
  292.         preTitle: "Pre title",
  293.         title: "Title",
  294.         subTitle: "Subtitle",
  295.         slug: "slug",
  296.         description: "Description",
  297.         cta: {
  298.           content: "Content Cta 1",
  299.           url:
  300.             "http://localhost:4200/back-office/cms/dws-pages/edit/60102ac170155b59983481f6",
  301.           target: "_blank",
  302.         },
  303.         cta2: null,
  304.         cta3: null,
  305.         chapter: {
  306.           name: "Chapter",
  307.           nav: false,
  308.         },
  309.       },
  310.     ],
  311.   },
  312.   slug: "pagina-rossocorsa",
  313.   status: "published",
  314.   publishedAt: "2021-01-19T11:37:15.353Z",
  315.   id: "60118dad28de2d312c16da4d",
  316.   original: "6006c1cd98dab63f18ac8a22",
  317.   createdBy: null,
  318.   updatedBy: null,
  319.   createdAt: "2021-01-27T15:58:37.966Z",
  320.   updatedAt: "2021-01-28T11:31:23.746Z",
  321.   expireAt: "2021-02-28T11:31:23.519Z",
  322.   revision: 20,
  323.   active: false,
  324. };
  325.  
  326. let _dealer = {
  327.   dealerCode: "052222",
  328.   isCreatedByHq: false,
  329.   sitemapId: "Pagina Rossocorsa",
  330.   parentId: null,
  331.   title: "Pagina Rossocorsa",
  332.   internalTitle: "Pagina Rossocorsa",
  333.   locale: "it",
  334.   content: {
  335.     blocks: [
  336.       {
  337.         type: "TextBlock",
  338.         isLocked: true,
  339.         isPinned: false,
  340.         palette: "",
  341.         variant: "text_centereddwdfdsfdfdfd",
  342.         preTitle: "Pre titledfdfddfdf",
  343.         title: "Titledfdfddff",
  344.         subTitle: "Subtitle",
  345.         slug: "slug",
  346.         description: "Description",
  347.         cta: {
  348.           content: "Content Cta 1 bdgfhdghdfhdf",
  349.           url:
  350.             "http://localhost:4200/back-office/cms/dws-pages/edit/60102ac170155b59983481f6",
  351.           target: "_blank",
  352.         },
  353.         cta2: null,
  354.         cta3: null,
  355.         chapter: {
  356.           name: "Chapter",
  357.           nav: false,
  358.         },
  359.       },
  360.     ],
  361.   },
  362.   slug: "pagina-rossocorsa",
  363.   status: "published",
  364.   publishedAt: "2021-01-19T11:37:15.353Z",
  365.   original: "6006c1cd98dab63f18ac8a22",
  366.   createdBy: null,
  367.   updatedBy: null,
  368.   createdAt: "2021-01-27T15:58:37.966Z",
  369.   updatedAt: "2021-01-28T11:31:23.746Z",
  370.   expireAt: "2021-02-28T11:31:23.519Z",
  371.   revision: 20,
  372.   active: false,
  373. };
  374.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement