SHOW:
|
|
- or go back to the newest paste.
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 | if (isObject(hqContent[key])) { | |
42 | /// hqContent[key] is an object | |
43 | if (isObject(dealerContent[key]) && !isEmptyOrNotObject(dealerContent[key])) { | |
44 | // Both are objects | |
45 | if (hqContent[key].isLocked) { | |
46 | mergeDeepLevels(hqContent[key], dealerContent[key]); | |
47 | } | |
48 | } else { | |
49 | // dealerContect did not previusly containt key | |
50 | if ( | |
51 | dealerContent[key] === undefined || | |
52 | isEmptyOrNotObject(dealerContent[key]) | |
53 | ) { | |
54 | console.log("overriding object since not existing", key); | |
55 | dealerContent[key] = { ...hqContent[key] }; | |
56 | } | |
57 | } | |
58 | } else { | |
59 | /// Is primitive | |
60 | if (hqContent.isLocked) { | |
61 | dealerContent[key] = hqContent[key]; | |
62 | } | |
63 | } | |
64 | } | |
65 | }; | |
66 | ||
67 | return mergeFirstLevel(hqObj, dealerObj); | |
68 | }; | |
69 | ||
70 | const isObject = (item) => item && typeof item === "object"; | |
71 | ||
72 | const isEmptyOrNotObject = (obj) => | |
73 | isObject(obj) ? Object.keys(obj).length === 0 : true; | |
74 | ||
75 | let hq = { | |
76 | dealerCode: null, | |
77 | isCreatedByHq: true, | |
78 | sitemapId: "Pagina Rossocorsa", | |
79 | parentId: null, | |
80 | title: "Pagina Rossocorsa", | |
81 | internalTitle: "Pagina Rossocorsa", | |
82 | locale: "it", | |
83 | content: { | |
84 | blocks: [ | |
85 | { | |
86 | type: "TextBlock", | |
87 | isLocked: true, | |
88 | isPinned: false, | |
89 | palette: "", | |
90 | variant: "text_centered", | |
91 | preTitle: "Pre title", | |
92 | title: "Title", | |
93 | subTitle: "Subtitle", | |
94 | slug: "slug", | |
95 | description: "Description", | |
96 | cta: { | |
97 | content: "Content Cta 1", | |
98 | url: | |
99 | "http://localhost:4200/back-office/cms/dws-pages/edit/60102ac170155b59983481f6", | |
100 | target: "_blank", | |
101 | }, | |
102 | cta2: null, | |
103 | cta3: null, | |
104 | chapter: { | |
105 | name: "Chapter", | |
106 | nav: false, | |
107 | }, | |
108 | }, | |
109 | { | |
110 | type: "DWSCoverCarousel", | |
111 | isLocked: true, | |
112 | isPinned: false, | |
113 | scrollpointer: false, | |
114 | items: [ | |
115 | { | |
116 | type: "DwsCover", | |
117 | isLocked: true, | |
118 | isPinned: false, | |
119 | owner: "hidden", | |
120 | palette: "f8-tributo", | |
121 | gradient: false, | |
122 | title: "Title 1", | |
123 | preTitle: "Pre Title 1", | |
124 | chapter: { | |
125 | name: "Chapter 1", | |
126 | nav: false, | |
127 | }, | |
128 | scrollIndicator: false, | |
129 | link: { | |
130 | content: "", | |
131 | title: "", | |
132 | url: "", | |
133 | target: "_blank", | |
134 | }, | |
135 | cta2: { | |
136 | content: "Content Cta 2", | |
137 | url: | |
138 | "http://localhost:4200/back-office/cms/dws-pages/edit/6011491d70155b59983482bd", | |
139 | target: "_blank", | |
140 | }, | |
141 | cta3: null, | |
142 | background: null, | |
143 | }, | |
144 | { | |
145 | type: "DwsCover", | |
146 | isLocked: true, | |
147 | isPinned: false, | |
148 | owner: "hidden", | |
149 | palette: "dark", | |
150 | gradient: false, | |
151 | title: "Title 2", | |
152 | preTitle: "Pre Title 2", | |
153 | chapter: { | |
154 | name: "Chapter 2", | |
155 | nav: false, | |
156 | }, | |
157 | scrollIndicator: false, | |
158 | link: { | |
159 | content: "", | |
160 | title: "", | |
161 | url: "", | |
162 | target: "_blank", | |
163 | }, | |
164 | cta2: null, | |
165 | cta3: null, | |
166 | background: { | |
167 | type: "image", | |
168 | media: { | |
169 | landscapeBig: { | |
170 | id: "1-land", | |
171 | }, | |
172 | portraitMedium: { | |
173 | id: "1-portait", | |
174 | }, | |
175 | }, | |
176 | }, | |
177 | }, | |
178 | { | |
179 | type: "DwsCover", | |
180 | isLocked: false, | |
181 | isPinned: false, | |
182 | owner: "hidden", | |
183 | palette: "", | |
184 | gradient: false, | |
185 | title: "Title 3", | |
186 | preTitle: "Pre title 3", | |
187 | chapter: { | |
188 | name: "Chapter 3", | |
189 | nav: false, | |
190 | }, | |
191 | scrollIndicator: false, | |
192 | link: { | |
193 | content: "", | |
194 | title: "", | |
195 | url: "", | |
196 | target: "_blank", | |
197 | }, | |
198 | cta2: null, | |
199 | cta3: { | |
200 | content: "Cta 3 content", | |
201 | url: | |
202 | "http://localhost:4200/back-office/cms/dws-pages/edit/6011491d70155b59983482bd", | |
203 | target: "_blank", | |
204 | }, | |
205 | background: null, | |
206 | }, | |
207 | ], | |
208 | }, | |
209 | ], | |
210 | }, | |
211 | slug: "pagina-rossocorsa", | |
212 | status: "published", | |
213 | publishedAt: "2021-01-19T11:37:15.353Z", | |
214 | id: "60118dad28de2d312c16da4d", | |
215 | original: "6006c1cd98dab63f18ac8a22", | |
216 | createdBy: null, | |
217 | updatedBy: null, | |
218 | createdAt: "2021-01-27T15:58:37.966Z", | |
219 | updatedAt: "2021-01-28T11:31:23.746Z", | |
220 | expireAt: "2021-02-28T11:31:23.519Z", | |
221 | revision: 20, | |
222 | active: false, | |
223 | }; | |
224 | ||
225 | let dealer = { | |
226 | dealerCode: "052222", | |
227 | isCreatedByHq: false, | |
228 | sitemapId: "Pagina Rossocorsa", | |
229 | parentId: null, | |
230 | title: "Pagina Rossocorsa", | |
231 | internalTitle: "Pagina Rossocorsa", | |
232 | locale: "it", | |
233 | content: { | |
234 | blocks: [ | |
235 | { | |
236 | cta: {}, | |
237 | chapter: {}, | |
238 | }, | |
239 | { | |
240 | items: [ | |
241 | { | |
242 | chapter: {}, | |
243 | link: {}, | |
244 | cta2: {}, | |
245 | }, | |
246 | { | |
247 | chapter: {}, | |
248 | link: {}, | |
249 | background: { | |
250 | media: { | |
251 | landscapeBig: {}, | |
252 | portraitMedium: {}, | |
253 | }, | |
254 | }, | |
255 | }, | |
256 | { | |
257 | type: "DwsCover", | |
258 | isLocked: false, | |
259 | isPinned: false, | |
260 | owner: "hidden", | |
261 | palette: "", | |
262 | gradient: false, | |
263 | title: "Title 3", | |
264 | preTitle: "Pre title 3", | |
265 | chapter: {}, | |
266 | scrollIndicator: false, | |
267 | link: {}, | |
268 | cta2: null, | |
269 | cta3: {}, | |
270 | background: null, | |
271 | }, | |
272 | ], | |
273 | }, | |
274 | ], | |
275 | }, | |
276 | slug: "pagina-rossocorsa", | |
277 | status: "published", | |
278 | publishedAt: "2021-01-19T11:37:15.353Z", | |
279 | original: "6006c1cd98dab63f18ac8a22", | |
280 | createdBy: null, | |
281 | updatedBy: null, | |
282 | createdAt: "2021-01-27T15:58:37.966Z", | |
283 | updatedAt: "2021-01-28T11:31:23.746Z", | |
284 | expireAt: "2021-02-28T11:31:23.519Z", | |
285 | revision: 20, | |
286 | active: false, | |
287 | }; | |
288 |