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 not empty
- mergeDeepLevels(hqContent[key], dealerContent[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]);
- }
- // dealerContent[key] = { ...hqContent[key] };
- }
- } else {
- /// Is primitive
- console.log('HQ is primitive', key);
- if(hqContent.isLocked === undefined) {
- 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: null,
- isCreatedByHq: true,
- sitemapId: "Pagina Rossocorsa",
- parentId: null,
- title: "Pagina Rossocorsa",
- internalTitle: "Pagina Rossocorsa",
- locale: "it",
- content: {
- blocks: [
- {
- type: "TextBlock",
- isLocked: false,
- 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: [
- { },
- ],
- },
- 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,
- };
Add Comment
Please, Sign In to add comment