Skip to content

Commit 038a1af

Browse files
Add detailed JSDoc comments to AEM service functions for better documentation and clarity. Updated CarouselComponent to use optional chaining for safer property access, enhancing code robustness.
1 parent 718bbb7 commit 038a1af

File tree

2 files changed

+126
-43
lines changed

2 files changed

+126
-43
lines changed

api/src/services/aem.service.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ interface AssetJSON {
8989
_version?: number;
9090
}
9191

92+
93+
/**
94+
*
95+
* @param assetJsonPath - The path to the asset JSON file.
96+
* @returns True if the asset JSON file exists, false otherwise.
97+
*/
9298
async function isAssetJsonCreated(assetJsonPath: string): Promise<boolean> {
9399
try {
94100
await fs.promises.access(assetJsonPath, fs.constants.F_OK);
@@ -366,6 +372,18 @@ function uidCorrector(str: string): string {
366372

367373
declare const contentstackComponents: Record<string, any> | undefined;
368374

375+
/**
376+
* Function to create assets for the given destination stack.
377+
* @param destinationStackId - The ID of the destination stack.
378+
* @param projectId - The ID of the project.
379+
* @param packagePath - The path to the package.
380+
* @returns void - The function does not return a value.
381+
* @description - This function creates the assets for the given destination stack.
382+
* The assets are created in the following files:
383+
* - index.json - The asset index.
384+
* - path-mapping.json - The path to UID mapping.
385+
* @throws Will log an error if the assets are not created.
386+
*/
369387
const createAssets = async ({
370388
destinationStackId,
371389
projectId,
@@ -528,6 +546,33 @@ const createAssets = async ({
528546
);
529547
};
530548

549+
/**
550+
*
551+
* @param fields - The fields object.
552+
* @param items - The items object.
553+
* @param title - The title of the entry.
554+
* @param pathToUidMap - The path to UID map.
555+
* @param assetDetailsMap - The asset details map.
556+
* @returns The processed fields object.
557+
* @description - This function processes the fields recursively.
558+
* The fields are processed in the following order:
559+
* - Modular blocks
560+
* - Group
561+
* - Container
562+
* - Single line text
563+
* - Multiple line text
564+
* - Rich text
565+
* - Date
566+
* - Number
567+
* - Boolean
568+
* - Image
569+
* - Video
570+
* - Audio
571+
* - Link
572+
* - Embed
573+
* - Custom
574+
* @throws Will log an error if the fields are not processed.
575+
*/
531576
function processFieldsRecursive(
532577
fields: any[],
533578
items: any,
@@ -1139,6 +1184,20 @@ const getTitle = (parseData: any) => {
11391184
return parseData?.title ?? parseData?.templateType;
11401185
}
11411186

1187+
1188+
/**
1189+
*
1190+
* @param packagePath - The path to the package.
1191+
* @param contentTypes - The content types.
1192+
* @param destinationStackId - The ID of the destination stack.
1193+
* @param projectId - The ID of the project.
1194+
* @param project - The project object.
1195+
* @returns void - The function does not return a value.
1196+
* @description - This function creates the entry for the given destination stack.
1197+
* The entry is created in the following files:
1198+
* - index.json - The entry index.
1199+
* @throws Will log an error if the entry is not created.
1200+
*/
11421201
const createEntry = async ({
11431202
packagePath,
11441203
contentTypes,
@@ -1254,6 +1313,20 @@ const createEntry = async ({
12541313
}
12551314
}
12561315

1316+
/**
1317+
*
1318+
* @param req - The request object.
1319+
* @param destinationStackId - The ID of the destination stack.
1320+
* @param projectId - The ID of the project.
1321+
* @param project - The project object.
1322+
* @returns void - The function does not return a value.
1323+
* @description - This function creates the locales for the given destination stack.
1324+
* The locales are created in the following files:
1325+
* - locale.json - The master locale.
1326+
* - allLocales.json - All the locales.
1327+
* @throws Will log an error if the file writing operation fails.
1328+
* @throws Will log an error if the locales are not created.
1329+
*/
12571330
const createLocale = async (
12581331
req: Request,
12591332
destinationStackId: string,
@@ -1328,6 +1401,16 @@ const createLocale = async (
13281401
}
13291402
};
13301403

1404+
/**
1405+
*
1406+
* @param destinationStackId - The ID of the destination stack.
1407+
* @returns void - The function does not return a value.
1408+
* @description - This function creates a version file for the given destination stack.
1409+
* The version file contains the following information:
1410+
* - contentVersion: The version of the content schema (set to `2`).
1411+
* - logsPath: An empty string reserved for future log path information.
1412+
* @throws Will log an error if the file writing operation fails.
1413+
*/
13311414
const createVersionFile = async (destinationStackId: string) => {
13321415
const baseDir = path.join(baseDirName, destinationStackId);
13331416
fs.writeFile(

upload-api/migration-aem/libs/contentType/components/CarouselComponent.ts

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class CarouselComponent extends ContentstackComponent {
3232
* Store a component schema for later reuse
3333
*/
3434
static storeComponentSchema(componentType: string, schema: any): void {
35-
if (!globalComponentSchemas.has(componentType)) {
35+
if (!globalComponentSchemas?.has(componentType)) {
3636
globalComponentSchemas.set(componentType, schema);
3737
}
3838
}
@@ -41,16 +41,16 @@ export class CarouselComponent extends ContentstackComponent {
4141
* Get a stored component schema
4242
*/
4343
static getStoredComponentSchema(componentType: string): any | null {
44-
return globalComponentSchemas.get(componentType) || null;
44+
return globalComponentSchemas?.get(componentType) || null;
4545
}
4646

4747
static isCarousel(component: any): boolean {
4848
const properties = component?.convertedSchema?.properties;
4949
if (properties && typeof properties === 'object') {
5050
const typeField = properties[":type"];
5151
if (
52-
(typeof typeField === "string" && typeField.includes("/components/carousel")) ||
53-
(typeof typeField === "object" && typeField.value?.includes("/components/carousel"))
52+
(typeof typeField === "string" && typeField?.includes("/components/carousel")) ||
53+
(typeof typeField === "object" && typeField?.value?.includes("/components/carousel"))
5454
) {
5555
return true;
5656
}
@@ -87,7 +87,7 @@ export class CarouselComponent extends ContentstackComponent {
8787
const isCarouselItems = normalizedUid === 'items' || fieldKey === 'items' || fieldKey === ':items';
8888

8989
if (isCarouselItems) {
90-
const typesToProcess = globalCarouselItemTypes && globalCarouselItemTypes.size > 0
90+
const typesToProcess = globalCarouselItemTypes && globalCarouselItemTypes?.size > 0
9191
? globalCarouselItemTypes
9292
: new Set(Object.keys(countObject).map(t => t.split('/').pop()).filter(Boolean));
9393

@@ -101,15 +101,15 @@ export class CarouselComponent extends ContentstackComponent {
101101
if (componentType === 'teaser' || componentType === 'heroTeaser' || componentType === 'overlayBoxTeaser') {
102102

103103
let teaserData = null;
104-
if (currentComponent && TeaserComponent.isTeaser(currentComponent)) {
105-
teaserData = TeaserComponent.mapTeaserToContentstack(currentComponent, "teaser");
104+
if (currentComponent && TeaserComponent?.isTeaser(currentComponent)) {
105+
teaserData = TeaserComponent?.mapTeaserToContentstack(currentComponent, "teaser");
106106
if (teaserData) {
107107
//store for reuse
108-
CarouselComponent.storeComponentSchema('teaser', teaserData);
108+
CarouselComponent?.storeComponentSchema('teaser', teaserData);
109109
}
110110
} else {
111111
// Try to get from stored schemas
112-
teaserData = CarouselComponent.getStoredComponentSchema('teaser');
112+
teaserData = CarouselComponent?.getStoredComponentSchema('teaser');
113113
if (teaserData) {
114114
console.log('Reusing previously stored teaser schema');
115115
} else {
@@ -129,15 +129,15 @@ export class CarouselComponent extends ContentstackComponent {
129129
} else if (componentType === 'image') {
130130

131131
let imageData = null;
132-
if (currentComponent && ImageComponent.isImage(currentComponent)) {
132+
if (currentComponent && ImageComponent?.isImage(currentComponent)) {
133133
imageData = ImageComponent.mapImageToContentstack(currentComponent, "image");
134134
if (imageData) {
135135
// Store for future reuse
136-
CarouselComponent.storeComponentSchema('image', imageData);
136+
CarouselComponent?.storeComponentSchema('image', imageData);
137137
}
138138
} else {
139139
// Try to get from stored schemas
140-
imageData = CarouselComponent.getStoredComponentSchema('image');
140+
imageData = CarouselComponent?.getStoredComponentSchema('image');
141141
if (imageData) {
142142
console.log('Reusing previously stored image schema');
143143
}
@@ -155,13 +155,13 @@ export class CarouselComponent extends ContentstackComponent {
155155
} else if (componentType === 'button') {
156156
let buttonData = null;
157157

158-
if (currentComponent && ButtonComponent.isButton(currentComponent)) {
158+
if (currentComponent && ButtonComponent?.isButton(currentComponent)) {
159159
buttonData = ButtonComponent.mapButtonToContentstack(currentComponent, "button");
160160
if (buttonData) {
161-
CarouselComponent.storeComponentSchema('button', buttonData);
161+
CarouselComponent?.storeComponentSchema('button', buttonData);
162162
}
163163
} else {
164-
buttonData = CarouselComponent.getStoredComponentSchema('button');
164+
buttonData = CarouselComponent?.getStoredComponentSchema('button');
165165
}
166166

167167
if (buttonData) {
@@ -176,13 +176,13 @@ export class CarouselComponent extends ContentstackComponent {
176176
} else if (componentType === 'textbanner' || componentType === 'textBanner') {
177177
let textBannerData = null;
178178

179-
if (currentComponent && TextBannerComponent.isTextBanner(currentComponent)) {
180-
textBannerData = TextBannerComponent.mapTextBannerToContentstack(currentComponent, "textBanner");
179+
if (currentComponent && TextBannerComponent?.isTextBanner(currentComponent)) {
180+
textBannerData = TextBannerComponent?.mapTextBannerToContentstack(currentComponent, "textBanner");
181181
if (textBannerData) {
182-
CarouselComponent.storeComponentSchema('textbanner', textBannerData);
182+
CarouselComponent?.storeComponentSchema('textbanner', textBannerData);
183183
}
184184
} else {
185-
textBannerData = CarouselComponent.getStoredComponentSchema('textbanner');
185+
textBannerData = CarouselComponent?.getStoredComponentSchema('textbanner');
186186
}
187187

188188
if (textBannerData) {
@@ -197,13 +197,13 @@ export class CarouselComponent extends ContentstackComponent {
197197
} else if (componentType === 'text') {
198198
let textData = null;
199199

200-
if (currentComponent && TextComponent.isText(currentComponent)) {
201-
textData = TextComponent.mapTextToContentstack(currentComponent);
200+
if (currentComponent && TextComponent?.isText(currentComponent)) {
201+
textData = TextComponent?.mapTextToContentstack(currentComponent);
202202
if (textData) {
203-
CarouselComponent.storeComponentSchema('text', textData);
203+
CarouselComponent?.storeComponentSchema('text', textData);
204204
}
205205
} else {
206-
textData = CarouselComponent.getStoredComponentSchema('text');
206+
textData = CarouselComponent?.getStoredComponentSchema('text');
207207
}
208208

209209
if (textData) {
@@ -214,13 +214,13 @@ export class CarouselComponent extends ContentstackComponent {
214214
} else if (componentType === 'title') {
215215
let titleData = null;
216216

217-
if (currentComponent && TitleComponent.isTitle(currentComponent)) {
218-
titleData = TitleComponent.mapTitleToContentstack(currentComponent, "title");
217+
if (currentComponent && TitleComponent?.isTitle(currentComponent)) {
218+
titleData = TitleComponent?.mapTitleToContentstack(currentComponent, "title");
219219
if (titleData) {
220-
CarouselComponent.storeComponentSchema('title', titleData);
220+
CarouselComponent?.storeComponentSchema('title', titleData);
221221
}
222222
} else {
223-
titleData = CarouselComponent.getStoredComponentSchema('title');
223+
titleData = CarouselComponent?.getStoredComponentSchema('title');
224224
}
225225

226226
if (titleData) {
@@ -231,13 +231,13 @@ export class CarouselComponent extends ContentstackComponent {
231231
} else if (componentType === 'search') {
232232
let searchData = null;
233233

234-
if (currentComponent && SearchComponent.isSearch(currentComponent)) {
235-
searchData = SearchComponent.mapSearchToContentstack(currentComponent, "search");
234+
if (currentComponent && SearchComponent?.isSearch(currentComponent)) {
235+
searchData = SearchComponent?.mapSearchToContentstack(currentComponent, "search");
236236
if (searchData) {
237-
CarouselComponent.storeComponentSchema('search', searchData);
237+
CarouselComponent?.storeComponentSchema('search', searchData);
238238
}
239239
} else {
240-
searchData = CarouselComponent.getStoredComponentSchema('search');
240+
searchData = CarouselComponent?.getStoredComponentSchema('search');
241241
}
242242

243243
if (searchData) {
@@ -248,13 +248,13 @@ export class CarouselComponent extends ContentstackComponent {
248248
} else if (componentType === 'spacer') {
249249
let spacerData = null;
250250

251-
if (currentComponent && SpacerComponent.isSpacer(currentComponent)) {
252-
spacerData = SpacerComponent.mapSpacerToContentstack(currentComponent, "spacer");
251+
if (currentComponent && SpacerComponent?.isSpacer(currentComponent)) {
252+
spacerData = SpacerComponent?.mapSpacerToContentstack(currentComponent, "spacer");
253253
if (spacerData) {
254-
CarouselComponent.storeComponentSchema('spacer', spacerData);
254+
CarouselComponent?.storeComponentSchema('spacer', spacerData);
255255
}
256256
} else {
257-
spacerData = CarouselComponent.getStoredComponentSchema('spacer');
257+
spacerData = CarouselComponent?.getStoredComponentSchema('spacer');
258258
}
259259

260260
if (spacerData) {
@@ -265,13 +265,13 @@ export class CarouselComponent extends ContentstackComponent {
265265
} else if (componentType === 'separator') {
266266
let separatorData = null;
267267

268-
if (currentComponent && SeparatorComponent.isSeparator(currentComponent)) {
269-
separatorData = SeparatorComponent.mapSeparatorToContentstack(currentComponent, "separator");
268+
if (currentComponent && SeparatorComponent?.isSeparator(currentComponent)) {
269+
separatorData = SeparatorComponent?.mapSeparatorToContentstack(currentComponent, "separator");
270270
if (separatorData) {
271-
CarouselComponent.storeComponentSchema('separator', separatorData);
271+
CarouselComponent?.storeComponentSchema?.('separator', separatorData);
272272
}
273273
} else {
274-
separatorData = CarouselComponent.getStoredComponentSchema('separator');
274+
separatorData = CarouselComponent?.getStoredComponentSchema?.('separator');
275275
}
276276

277277
if (separatorData) {
@@ -284,7 +284,7 @@ export class CarouselComponent extends ContentstackComponent {
284284
}
285285
}
286286
}
287-
if (schema.length === 0) {
287+
if (schema?.length === 0) {
288288
console.warn('Carousel items schema is empty! No components were mapped.');
289289
}
290290

@@ -303,11 +303,11 @@ export class CarouselComponent extends ContentstackComponent {
303303
const componentSchema = component?.convertedSchema;
304304
if (componentSchema?.type === 'object' && componentSchema?.properties) {
305305
const fields: any[] = [];
306-
for (const [key, value] of Object.entries(componentSchema.properties)) {
306+
for (const [key, value] of Object.entries(componentSchema?.properties)) {
307307
if (!carouselExclude.includes(key)) {
308308
const schemaProp = value as SchemaProperty;
309-
if (schemaProp?.type && CarouselComponent.fieldTypeMap[schemaProp.type]) {
310-
const mappedField = CarouselComponent.fieldTypeMap[schemaProp.type](key, schemaProp);
309+
if (schemaProp?.type && CarouselComponent?.fieldTypeMap?.[schemaProp?.type]) {
310+
const mappedField = CarouselComponent?.fieldTypeMap?.[schemaProp?.type](key, schemaProp);
311311
if (mappedField) {
312312
fields.push(mappedField);
313313
}

0 commit comments

Comments
 (0)