informaticage

Merge files based on locks JS

Jan 28th, 2021 (edited)
851
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 not empty
  48.           mergeDeepLevels(hqContent[key], dealerContent[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.             //   dealerContent[key] = { ...hqContent[key] };
  59.         }
  60.       } else {
  61.         /// Is primitive
  62.         console.log('HQ is primitive', key);
  63.         if(hqContent.isLocked === undefined) {
  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: null,
  82.   isCreatedByHq: true,
  83.   sitemapId: "Pagina Rossocorsa",
  84.   parentId: null,
  85.   title: "Pagina Rossocorsa",
  86.   internalTitle: "Pagina Rossocorsa",
  87.   locale: "it",
  88.   content: {
  89.     blocks: [
  90.       {
  91.         type: "TextBlock",
  92.         isLocked: false,
  93.         isPinned: false,
  94.         palette: "",
  95.         variant: "text_centered",
  96.         preTitle: "Pre title",
  97.         title: "Title",
  98.         subTitle: "Subtitle",
  99.         slug: "slug",
  100.         description: "Description",
  101.         cta: {
  102.           content: "Content Cta 1",
  103.           url:
  104.             "http://localhost:4200/back-office/cms/dws-pages/edit/60102ac170155b59983481f6",
  105.           target: "_blank",
  106.         },
  107.         cta2: null,
  108.         cta3: null,
  109.         chapter: {
  110.           name: "Chapter",
  111.           nav: false,
  112.         },
  113.       },
  114.     ],
  115.   },
  116.   slug: "pagina-rossocorsa",
  117.   status: "published",
  118.   publishedAt: "2021-01-19T11:37:15.353Z",
  119.   id: "60118dad28de2d312c16da4d",
  120.   original: "6006c1cd98dab63f18ac8a22",
  121.   createdBy: null,
  122.   updatedBy: null,
  123.   createdAt: "2021-01-27T15:58:37.966Z",
  124.   updatedAt: "2021-01-28T11:31:23.746Z",
  125.   expireAt: "2021-02-28T11:31:23.519Z",
  126.   revision: 20,
  127.   active: false,
  128. };
  129.  
  130. let dealer = {
  131.   dealerCode: "052222",
  132.   isCreatedByHq: false,
  133.   sitemapId: "Pagina Rossocorsa",
  134.   parentId: null,
  135.   title: "Pagina Rossocorsa",
  136.   internalTitle: "Pagina Rossocorsa",
  137.   locale: "it",
  138.   content: {
  139.     blocks: [
  140.       { },
  141.     ],
  142.   },
  143.   slug: "pagina-rossocorsa",
  144.   status: "published",
  145.   publishedAt: "2021-01-19T11:37:15.353Z",
  146.   original: "6006c1cd98dab63f18ac8a22",
  147.   createdBy: null,
  148.   updatedBy: null,
  149.   createdAt: "2021-01-27T15:58:37.966Z",
  150.   updatedAt: "2021-01-28T11:31:23.746Z",
  151.   expireAt: "2021-02-28T11:31:23.519Z",
  152.   revision: 20,
  153.   active: false,
  154. };
  155.  
Add Comment
Please, Sign In to add comment