informaticage

Validate rec

Mar 24th, 2021 (edited)
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getRandomValues = require("get-random-values");
  2.  
  3. const validateContent = function (
  4.   contentMaster,
  5.   contentSlave,
  6.   validateCurrentLevel = false,
  7.   failures = []
  8. ) {
  9.   if (contentMaster === null) {
  10.     const outcome = !validateCurrentLevel || contentSlave === null;
  11.     if (outcome === false) {
  12.       console.log("Failing on null mutation");
  13.     }
  14.     return outcome;
  15.   }
  16.   for ([key, value] of Object.entries(contentMaster)) {
  17.     // Le primitive del master
  18.     const haveToValidate =
  19.       contentMaster.isLocked !== undefined
  20.         ? contentMaster.isLocked
  21.         : validateCurrentLevel;
  22.  
  23.     // console.log(
  24.     //   key,
  25.     //   "is primitive: ",
  26.     //   !Array.isArray(value) && !(typeof value === "object")
  27.     // );
  28.  
  29.     if (!Array.isArray(value) && !(typeof value === "object")) {
  30.       if (haveToValidate) {
  31.         if (
  32.           contentSlave === undefined ||
  33.           contentMaster[key] !== contentSlave[key]
  34.         ) {
  35.           console.log("Failing on", key);
  36.           console.log(!!contentMaster[key] && !!contentMaster[key].isLocked);
  37.           failures.push(!!contentMaster[key]);
  38.           continue;
  39.         }
  40.       }
  41.     }
  42.  
  43.     if (Array.isArray(value) || typeof value === "object") {
  44.       // console.log("REC", key, contentMaster[key], 'vs', contentSlave[key]);
  45.       if (
  46.         (contentSlave === undefined) ||
  47.         (contentSlave[key] === undefined && haveToValidate)
  48.       ) {
  49.         console.log("Failing on deleted", key);
  50.         failures.push(!!contentMaster[key] && !!contentMaster[key].isLocked);
  51.         continue;
  52.       }
  53.  
  54.       if (
  55.         !validateContent(
  56.           contentMaster[key],
  57.           contentSlave[key],
  58.           haveToValidate,
  59.           failures
  60.         )
  61.       ) {
  62.         failures.push(!!contentMaster[key] && !!contentMaster[key].isLocked);
  63.         continue;
  64.       }
  65.     }
  66.   }
  67.   return [
  68.     failures,
  69.     failures.length === 0 || failures.some((failure) => failure === false),
  70.   ];
  71. };
  72.  
  73. const generateRandomCmsBlockUID = function () {
  74.   return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
  75.     (c ^ (getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
  76.   );
  77. };
  78.  
  79. const getOwnerTypeByReferenceType = function (referenceType) {
  80.   if (!referenceType || referenceType === "hq") {
  81.     return "hq";
  82.   }
  83.  
  84.   if (referenceType === "group") {
  85.     return "group";
  86.   }
  87.  
  88.   if (referenceType === "dealer") {
  89.     return "dealer";
  90.   }
  91. };
  92.  
  93. const mapContentBlocksByCmsBlockUID = function (cmsBlocks) {
  94.   const mapByCmsBlockUID = {};
  95.   // console.log();
  96.   cmsBlocks.forEach((cmsItem, index) => {
  97.     // Assigning owner
  98.     // console.log("SONO NELL'iterazione: ", cmsItem);
  99.     cmsItem["cmsCustomBlockPosition"] = index;
  100.  
  101.     if (!cmsItem.cmsBlockOwner) {
  102.       cmsItem.cmsBlockOwner = getOwnerTypeByReferenceType(
  103.         cmsItem.referenceType
  104.       );
  105.     }
  106.     if (cmsItem.cmsBlockUID) {
  107.       mapByCmsBlockUID[cmsItem.cmsBlockUID] = cmsItem;
  108.     } else {
  109.       cmsItem.cmsBlockUID = generateRandomCmsBlockUID();
  110.       mapByCmsBlockUID[cmsItem.cmsBlockUID] = cmsItem;
  111.     }
  112.  
  113.     Object.keys(cmsItem).forEach((cmsItemKey) => {
  114.       if (Array.isArray(cmsItem[cmsItemKey])) {
  115.         cmsItem[cmsItemKey] = mapContentBlocksByCmsBlockUID(
  116.           cmsItem[cmsItemKey]
  117.         );
  118.       }
  119.     });
  120.   });
  121.  
  122.   return mapByCmsBlockUID;
  123. };
  124.  
  125. const map1 = {
  126.   "2be4f7c3-1eed-443b-a6d6-0ca67de96bee": {
  127.     type: "BoxGrid",
  128.     cmsBlockUID: "2be4f7c3-1eed-443b-a6d6-0ca67de96bee",
  129.     cmsBlockOwner: "hq",
  130.     isLocked: false,
  131.     isPinned: false,
  132.     palette: "",
  133.     title: "Pagina test merge 4",
  134.     filters: null,
  135.     slug: "Pagina test merge 4",
  136.     chapter: { name: "Pagina test merge 4", nav: false },
  137.     share: null,
  138.     items: {
  139.       "c3d81502-3641-4666-881c-3a681c7c1239": {
  140.         type: "BoxGridRow",
  141.         cmsBlockUID: "c3d81502-3641-4666-881c-3a681c7c1239",
  142.         cmsBlockOwner: "hq",
  143.         isLocked: true,
  144.         isPinned: false,
  145.         items: {
  146.           "a7d6c4de-8fd5-418a-b20a-1324aaeaf07a": {
  147.             cmsBlockUID: "a7d6c4de-8fd5-418a-b20a-1324aaeaf07a",
  148.             cmsBlockOwner: "hq",
  149.             isLocked: true,
  150.             isPinned: false,
  151.             preTitle: "Pre Title",
  152.             title: "Pre Title ",
  153.             subTitle: "Pagina test merge 4",
  154.             description: "Pagina test merge 4 Subtitle",
  155.             width: "50%",
  156.             media: {
  157.               landscapeBig: { id: "603794de41c6d945e01235d3" },
  158.               portraitMedium: { id: "603794de41c6d945e01235d3" },
  159.             },
  160.             thronPlaylistId: "",
  161.             cta: null,
  162.             ctaType: "standard",
  163.             filterValues: { filter1: "", filter2: "", filter3: "" },
  164.           },
  165.           "cb393c42-7e78-4568-8073-26b331239b0f": {
  166.             cmsBlockUID: "cb393c42-7e78-4568-8073-26b331239b0f",
  167.             cmsBlockOwner: "hq",
  168.             isLocked: true,
  169.             isPinned: false,
  170.             preTitle: "Pre Title i2",
  171.             title: "Pre Title i2",
  172.             subTitle: "Pre Title i2",
  173.             description: "Pre Title i2",
  174.             width: "100%",
  175.             media: {
  176.               landscapeBig: { id: "6037778241c6d945e0123271" },
  177.               portraitMedium: { id: "6037778241c6d945e0123271" },
  178.             },
  179.             thronPlaylistId: "",
  180.             cta: null,
  181.             ctaType: "standard",
  182.             filterValues: {
  183.               filter1: "Filter 1",
  184.               filter2: "Filter 2",
  185.               filter3: "Filter 3",
  186.             },
  187.           },
  188.         },
  189.       },
  190.     },
  191.   },
  192. };
  193.  
  194. const map2 = {
  195.   "2be4f7c3-1eed-443b-a6d6-0ca67de96bee": {
  196.     type: "BoxGrid",
  197.     cmsBlockUID: "2be4f7c3-1eed-443b-a6d6-0ca67de96bee",
  198.     cmsBlockOwner: "hq",
  199.     isLocked: true,
  200.     isPinned: false,
  201.     palette: "",
  202.     title: "Pagina test merge 114",
  203.     filters: null,
  204.     slug: "Pagina test merge 4",
  205.     chapter: { name: "Pagina test merge 4", nav: false },
  206.     share: null,
  207.     items: {
  208.       "c3d81502-3641-4666-881c-3a681c7c1239": {
  209.         type: "BoxGridRow",
  210.         cmsBlockUID: "c3d81502-3641-4666-881c-3a681c7c1239",
  211.         cmsBlockOwner: "hq",
  212.         isLocked: true,
  213.         isPinned: false,
  214.         items: {
  215.           "a7d6c4de-8fd5-418a-b20a-1324aaeaf07a": {
  216.             cmsBlockUID: "a7d6c4de-8fd5-418a-b20a-1324aaeaf07a",
  217.             cmsBlockOwner: "hq",
  218.             isLocked: true,
  219.             isPinned: false,
  220.             preTitle: "Pre Title",
  221.             title: "Pre Title ",
  222.             subTitle: "Pagina test merge 4",
  223.             description: "Pagina test merge 5 Subtitle",
  224.             width: "50%",
  225.             media: {
  226.               landscapeBig: { id: "603794de41c6d945e01235d3" },
  227.               portraitMedium: { id: "603794de41c6d945e01235d3" },
  228.             },
  229.             thronPlaylistId: "",
  230.             cta: null,
  231.             ctaType: "standard",
  232.             filterValues: { filter1: "", filter2: "", filter3: "" },
  233.           },
  234.           "cb393c42-7e78-4568-8073-26b331239b0f": {
  235.             cmsBlockUID: "cb393c42-7e78-4568-8073-26b331239b0f",
  236.             cmsBlockOwner: "hq",
  237.             isLocked: true,
  238.             isPinned: false,
  239.             preTitle: "Pre Title i2",
  240.             title: "Pre Title i2",
  241.             subTitle: "Pre Title i2",
  242.             description: "Pre Title i2",
  243.             width: "100%",
  244.             media: {
  245.               landscapeBig: { id: "6037778241c6d945e0123271" },
  246.               portraitMedium: { id: "6037778241c6d945e0123271" },
  247.             },
  248.             thronPlaylistId: "",
  249.             cta: null,
  250.             ctaType: "standard",
  251.             filterValues: {
  252.               filter1: "Filter 1",
  253.               filter2: "Filter 2",
  254.               filter3: "Filter 3",
  255.             },
  256.           },
  257.         },
  258.       },
  259.     },
  260.   },
  261. };
  262.  
  263. console.log("FINAL RESULT", validateContent(map1, map2));
  264.  
Add Comment
Please, Sign In to add comment