Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mergeContents = (hqObj, dealerObj) => {
- const mergeFirstLevel = (hqObj, dealerObj) => {
- const toInheritFromDealer = [
- "dealerCode",
- "isCreatedByHq",
- "revision",
- "status",
- "original",
- "createdBy",
- "updatedBy",
- "createdAt",
- "updatedAt",
- "expireAt",
- "active",
- ];
- // Every key from HQ except the keys in toInheritFromDealer
- const toCheckForOverride = Object.keys(hqObj).filter(
- (_) => !toInheritFromDealer.includes(_)
- );
- /// For each key to be overrided
- for (const key of toCheckForOverride) {
- if (key === "content") {
- mergeDeepLevels(
- hqObj["content"]["blocks"],
- dealerObj["content"]["blocks"]
- );
- } else {
- dealerObj[key] = hqObj[key];
- }
- }
- return dealerObj;
- };
- const mergeDeepLevels = (hqContent, dealerContent) => {
- const toCheckForOverride = Object.keys(hqContent);
- for (const key of toCheckForOverride) {
- console.log('Checking key', key);
- if (isObject(hqContent[key])) {
- console.log('HQ is object', key);
- /// hqContent[key] is an object
- if (isEmptyOrNotObject(dealerContent[key])) {
- console.log('HQ is object and dealer is not or is empty', key);
- // Both are objects and dealer is empty
- dealerContent[key] = {...hqContent[key]};
- } else {
- console.log('HQ is object and dealer is object and not empty', key);
- // Either dealer is not an object or is an empty object
- // dealerContect did not previusly containt key
- if(hqContent[key].isLocked === undefined && hqContent.isLocked) {
- dealerContent[key] = { ...hqContent[key] };
- } else {
- mergeDeepLevels(hqContent[key], dealerContent[key]);
- }
- }
- } else {
- /// Is primitive
- console.log('HQ is primitive', key);
- if(hqContent.isLocked === undefined) {
- dealerContent = {};
- dealerContent[key] = hqContent[key];
- }
- if (hqContent.isLocked) {
- console.log('HQ is primitive and locked', key);
- dealerContent[key] = hqContent[key];
- }
- }
- }
- };
- return mergeFirstLevel(hqObj, dealerObj);
- };
- const isObject = (item) => item && typeof item === "object";
- const isEmptyOrNotObject = (obj) => !isObject(obj) || Object.keys(obj).length === 0;
- let hq = {
- "dealerCode":"052222",
- "isCreatedByHq":false,
- "sitemapId":"Pagina 4",
- "parentId":null,
- "title":"Pagina 4",
- "internalTitle":"Pagina 4",
- "locale":"it",
- "content":{
- "blocks":[
- {
- "type":"DWSCoverCarousel",
- "isLocked":true,
- "isPinned":false,
- "scrollpointer":false,
- "items":[
- {
- "type":"DwsCover",
- "isLocked":true,
- "isPinned":false,
- "owner":"hidden",
- "palette":"dark",
- "gradient":false,
- "title":"Title Slide 1",
- "preTitle":"Pre Title Slide 1",
- "chapter":{
- "name":"Chapter Slide 1",
- "nav":false
- },
- "scrollIndicator":false,
- "link":{
- "content":"Link Content Slide 1",
- "title":"Link Title Slide 1",
- "url":"Link Url Slide 1",
- "target":"_blank"
- },
- "cta2":{
- "content":"Cta 2 Content Slide 1",
- "url":"Cta 2 Url Slide 1",
- "target":"_blank"
- },
- "cta3":null,
- "background":null
- },
- {
- "type":"DwsCover",
- "isLocked":false,
- "isPinned":false,
- "owner":"hidden",
- "palette":"",
- "gradient":false,
- "title":"Title Slide 2",
- "preTitle":"Pre Title Slide 2",
- "chapter":{
- "name":"Chapter Slide 2",
- "nav":false
- },
- "scrollIndicator":false,
- "link":{
- "content":"Link Content Slide 2",
- "title":"Link Title Slide 2",
- "url":"Link url Slide 2",
- "target":"_blank"
- },
- "cta2":{
- "content":"Cta 2 Content Slide 2",
- "url":"Cta 2 Url Slide 2",
- "target":"_blank"
- },
- "cta3":null,
- "background":null
- }
- ]
- }
- ]
- },
- "slug":"pagina-4",
- "status":"draft",
- "id":"6013ea9d072b325aa8ed856c",
- "original":"6013ea9d072b325aa8ed856c",
- "createdBy":"5ff7784e74d73ea4d1e43501",
- "updatedBy":null,
- "createdAt":"2021-01-29T10:59:41.280Z",
- "updatedAt":"2021-01-29T10:59:42.141Z",
- "expireAt":null,
- "revision":0,
- "active":true
- };
- let dealer = {
- "dealerCode":"052222",
- "isCreatedByHq":false,
- "sitemapId":"Pagina 4",
- "parentId":null,
- "title":"Pagina 4",
- "internalTitle":"Pagina 4",
- "locale":"it",
- "content":{
- "blocks":[
- {
- "type":"DWSCoverCarousel",
- "isLocked":true,
- "isPinned":false,
- "scrollpointer":true,
- "items":[
- {
- "type":"DwsCover",
- "isLocked":true,
- "isPinned":false,
- "owner":"hidden",
- "palette":"dark",
- "gradient":false,
- "title":"Title Slide 1",
- "preTitle":"Pre Title Slide 1",
- "chapter":{
- "name":"Chapter Slide 1",
- "nav":false
- },
- "scrollIndicator":false,
- "link":{
- "content":"Link Content Slide 1",
- "title":"Link Title Slide 1",
- "url":"Link Url Slide 1",
- "target":"_blank"
- },
- "cta2":{
- "content":"Cta 2 Content Slide 1",
- "url":"Cta 2 Url Slide 1",
- "target":"_blank"
- },
- "cta3":null,
- "background":null
- },
- {
- "type":"DwsCover",
- "isLocked":false,
- "isPinned":false,
- "owner":"hidden",
- "palette":"",
- "gradient":false,
- "title":"Title Slide 2",
- "preTitle":"Pre Title Slide 2",
- "chapter":{
- // "name":"Chapter Slide 2",
- // "nav":false
- },
- "scrollIndicator":false,
- "link":{
- "content":"Link Content Slide 2",
- "title":"Link Title Slide 2",
- "url":"Link url Slide 2",
- "target":"_blank"
- },
- "cta2":{
- "content":"Cta 2 Content Slide 2",
- "url":"Cta 2 Url Slide 2",
- "target":"_blank"
- },
- "cta3":null,
- "background":null
- }
- ]
- }
- ]
- },
- "slug":"pagina-4",
- "status":"draft",
- "id":"6013ea9d072b325aa8ed856c",
- "original":"6013ea9d072b325aa8ed856c",
- "createdBy":"5ff7784e74d73ea4d1e43501",
- "updatedBy":null,
- "createdAt":"2021-01-29T10:59:41.280Z",
- "updatedAt":"2021-01-29T10:59:42.141Z",
- "expireAt":null,
- "revision":0,
- "active":true
- };
- let _hq = {
- dealerCode: null,
- isCreatedByHq: true,
- sitemapId: "Pagina Rossocorsa",
- parentId: null,
- title: "Pagina Rossocorsa",
- internalTitle: "Pagina Rossocorsa",
- locale: "it",
- content: {
- blocks: [
- {
- type: "TextBlock",
- isLocked: true,
- isPinned: false,
- palette: "",
- variant: "text_centered",
- preTitle: "Pre title",
- title: "Title",
- subTitle: "Subtitle",
- slug: "slug",
- description: "Description",
- cta: {
- content: "Content Cta 1",
- url:
- "http://localhost:4200/back-office/cms/dws-pages/edit/60102ac170155b59983481f6",
- target: "_blank",
- },
- cta2: null,
- cta3: null,
- chapter: {
- name: "Chapter",
- nav: false,
- },
- },
- ],
- },
- slug: "pagina-rossocorsa",
- status: "published",
- publishedAt: "2021-01-19T11:37:15.353Z",
- id: "60118dad28de2d312c16da4d",
- original: "6006c1cd98dab63f18ac8a22",
- createdBy: null,
- updatedBy: null,
- createdAt: "2021-01-27T15:58:37.966Z",
- updatedAt: "2021-01-28T11:31:23.746Z",
- expireAt: "2021-02-28T11:31:23.519Z",
- revision: 20,
- active: false,
- };
- let _dealer = {
- dealerCode: "052222",
- isCreatedByHq: false,
- sitemapId: "Pagina Rossocorsa",
- parentId: null,
- title: "Pagina Rossocorsa",
- internalTitle: "Pagina Rossocorsa",
- locale: "it",
- content: {
- blocks: [
- {
- type: "TextBlock",
- isLocked: true,
- isPinned: false,
- palette: "",
- variant: "text_centereddwdfdsfdfdfd",
- preTitle: "Pre titledfdfddfdf",
- title: "Titledfdfddff",
- subTitle: "Subtitle",
- slug: "slug",
- description: "Description",
- cta: {
- content: "Content Cta 1 bdgfhdghdfhdf",
- url:
- "http://localhost:4200/back-office/cms/dws-pages/edit/60102ac170155b59983481f6",
- target: "_blank",
- },
- cta2: null,
- cta3: null,
- chapter: {
- name: "Chapter",
- nav: false,
- },
- },
- ],
- },
- slug: "pagina-rossocorsa",
- status: "published",
- publishedAt: "2021-01-19T11:37:15.353Z",
- original: "6006c1cd98dab63f18ac8a22",
- createdBy: null,
- updatedBy: null,
- createdAt: "2021-01-27T15:58:37.966Z",
- updatedAt: "2021-01-28T11:31:23.746Z",
- expireAt: "2021-02-28T11:31:23.519Z",
- revision: 20,
- active: false,
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement