diff --git a/api/src/services/contentful.service.ts b/api/src/services/contentful.service.ts index 7a230d5b..68b548c6 100644 --- a/api/src/services/contentful.service.ts +++ b/api/src/services/contentful.service.ts @@ -94,20 +94,20 @@ const transformCloudinaryObject = (input: any) => { for (const metaData of input ?? []) { if (metaData?.public_id) { result?.push({ - public_id: metaData.public_id, - resource_type: metaData.resource_type, - type: metaData.type, - format: metaData.format, - version: metaData.version, - url: metaData.original_url, - secure_url: metaData.original_secure_url, - width: metaData.width, - height: metaData.height, - bytes: metaData.bytes, - duration: metaData.duration, - tags: metaData.tags, - metadata: metaData.metadata, - created_at: metaData.created_at, + public_id: metaData?.public_id, + resource_type: metaData?.resource_type, + type: metaData?.type, + format: metaData?.format, + version: metaData?.version, + url: metaData?.original_url, + secure_url: metaData?.original_secure_url, + width: metaData?.width, + height: metaData?.height, + bytes: metaData?.bytes, + duration: metaData?.duration, + tags: metaData?.tags, + metadata: metaData?.metadata, + created_at: metaData?.created_at, access_mode: "public", access_control: [], created_by: { diff --git a/api/src/services/migration.service.ts b/api/src/services/migration.service.ts index acaaf407..bc11378d 100644 --- a/api/src/services/migration.service.ts +++ b/api/src/services/migration.service.ts @@ -244,10 +244,10 @@ const startTestMigration = async (req: Request): Promise => { await wordpressService?.extractChunks(file_path, packagePath, project?.current_test_stack_id, projectId) await wordpressService?.getAllAuthors(file_path, packagePath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale) //await wordpressService?.extractContentTypes(projectId, project?.current_test_stack_id, contentTypes) - await wordpressService?.getAllTerms(file_path, packagePath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys,project?.stackDetails?.master_locale) - await wordpressService?.getAllTags(file_path, packagePath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys,project?.stackDetails?.master_locale) - await wordpressService?.getAllCategories(file_path, packagePath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys,project?.stackDetails?.master_locale) - await wordpressService?.extractPosts(packagePath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys,project?.stackDetails?.master_locale) + await wordpressService?.getAllTerms(file_path, packagePath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale) + await wordpressService?.getAllTags(file_path, packagePath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale) + await wordpressService?.getAllCategories(file_path, packagePath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale) + await wordpressService?.extractPosts(packagePath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale) await wordpressService?.extractGlobalFields(project?.current_test_stack_id, projectId) await wordpressService?.createVersionFile(project?.current_test_stack_id, projectId); } @@ -318,12 +318,12 @@ const startMigration = async (req: Request): Promise => { await wordpressService?.createAssetFolderFile(file_path, project?.destination_stack_id, projectId) await wordpressService?.getAllreference(file_path, packagePath, project?.destination_stack_id, projectId) await wordpressService?.extractChunks(file_path, packagePath, project?.destination_stack_id, projectId) - await wordpressService?.getAllAuthors(file_path, packagePath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys,project?.stackDetails?.master_locale) + await wordpressService?.getAllAuthors(file_path, packagePath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale) //await wordpressService?.extractContentTypes(projectId, project?.destination_stack_id) await wordpressService?.getAllTerms(file_path, packagePath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale) - await wordpressService?.getAllTags(file_path, packagePath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys,project?.stackDetails?.master_locale) - await wordpressService?.getAllCategories(file_path, packagePath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys,project?.stackDetails?.master_locale) - await wordpressService?.extractPosts(packagePath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys,project?.stackDetails?.master_locale) + await wordpressService?.getAllTags(file_path, packagePath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale) + await wordpressService?.getAllCategories(file_path, packagePath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale) + await wordpressService?.extractPosts(packagePath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale) await wordpressService?.extractGlobalFields(project?.destination_stack_id, projectId) await wordpressService?.createVersionFile(project?.destination_stack_id, projectId); @@ -426,23 +426,23 @@ export const createSourceLocales = async (req: Request) => { try { // Check if the project.json file exists - if (!fs.existsSync(projectFilePath)) { + if (!fs?.existsSync?.(projectFilePath)) { console.error(`project.json not found at ${projectFilePath}`); throw new Error(`project.json not found.`); } - + // Find the project with the specified projectId - const project: any = ProjectModelLowdb.chain.get("projects").find({ id: projectId }).value(); + const project: any = ProjectModelLowdb?.chain?.get?.("projects")?.find?.({ id: projectId })?.value?.(); if (project) { - const index = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value(); + const index = ProjectModelLowdb?.chain?.get?.("projects")?.findIndex?.({ id: projectId })?.value(); if (index > -1) { - - ProjectModelLowdb.update((data: any) => { + + ProjectModelLowdb?.update((data: any) => { data.projects[index].source_locales = locales; }); } // Write back the updated projects } else { - logger.error(`Project with ID: ${projectId} not found`,{ + logger.error(`Project with ID: ${projectId} not found`, { status: HTTP_CODES?.NOT_FOUND, message: HTTP_TEXTS?.INVALID_ID }) @@ -467,30 +467,30 @@ export const createSourceLocales = async (req: Request) => { * @return - void * @throws Exception if the project ID is invalid or the when the path to project.json is incorrect */ -export const updateLocaleMapper = async (req:Request) =>{ - const mapperObject = req.body; - const projectFilePath = path.join(process.cwd(), 'database', 'project.json'); // Adjusted path to project.json - const projectId = req.params.projectId; +export const updateLocaleMapper = async (req: Request) => { + const mapperObject = req?.body; + const projectFilePath = path?.join?.(process?.cwd(), 'database', 'project.json'); // Adjusted path to project.json + const projectId = req?.params?.projectId; try { // Check if the project.json file exists - if (!fs.existsSync(projectFilePath)) { + if (!fs?.existsSync?.(projectFilePath)) { console.error(`project.json not found at ${projectFilePath}`); throw new Error(`project.json not found.`); } - + // Find the project with the specified projectId - const project: any = ProjectModelLowdb.chain.get("projects").find({ id: projectId }).value(); + const project: any = ProjectModelLowdb?.chain?.get?.("projects")?.find?.({ id: projectId })?.value(); if (project) { - const index = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value(); + const index = ProjectModelLowdb?.chain?.get("projects")?.findIndex?.({ id: projectId })?.value(); if (index > -1) { - ProjectModelLowdb.update((data: any) => { + ProjectModelLowdb?.update((data: any) => { data.projects[index].master_locale = mapperObject?.master_locale; data.projects[index].locales = mapperObject?.locales; }); } // Write back the updated projects } else { - logger.error(`Project with ID: ${projectId} not found`,{ + logger.error(`Project with ID: ${projectId} not found`, { status: HTTP_CODES?.NOT_FOUND, message: HTTP_TEXTS?.INVALID_ID }) diff --git a/upload-api/migration-contentful/libs/extractLocale.js b/upload-api/migration-contentful/libs/extractLocale.js index 85cac4d8..7f702694 100644 --- a/upload-api/migration-contentful/libs/extractLocale.js +++ b/upload-api/migration-contentful/libs/extractLocale.js @@ -13,19 +13,19 @@ const fs = require("fs"); */ const extractLocale = async (jsonFilePath) => { try { - const rawData = fs.readFileSync(jsonFilePath, "utf8"); - const jsonData = JSON.parse(rawData); - + const rawData = fs?.readFileSync?.(jsonFilePath, "utf8"); + const jsonData = JSON?.parse?.(rawData); + // Extract unique language codes from locales array const uniqueLanguages = new Set(); - if (Array.isArray(jsonData.locales)) { - jsonData.locales.forEach(locale => { - if (locale.code) { - uniqueLanguages.add(locale.code.toLowerCase()); // Normalize to lowercase + if (Array?.isArray?.(jsonData?.locales)) { + jsonData?.locales?.forEach?.(locale => { + if (locale?.code) { + uniqueLanguages.add(locale?.code?.toLowerCase?.()); // Normalize to lowercase } }); } - + return [...uniqueLanguages]; // Convert Set to array for output } catch (error) { console.error(`Error reading JSON file:`, error.message); diff --git a/upload-api/migration-sitecore/libs/extractLocales.js b/upload-api/migration-sitecore/libs/extractLocales.js index ff979ad5..539634ff 100644 --- a/upload-api/migration-sitecore/libs/extractLocales.js +++ b/upload-api/migration-sitecore/libs/extractLocales.js @@ -6,24 +6,24 @@ const path = require("path"); const uniqueLanguages = new Set(); // Define uniqueLanguages globally or pass it as a parameter const extractLocales = (dir) => { - const items = fs.readdirSync(dir, { withFileTypes: true }); + const items = fs?.readdirSync?.(dir, { withFileTypes: true }); for (const item of items) { - const fullPath = path.join(dir, item.name); - + const fullPath = path?.join?.(dir, item?.name); + if (item.isDirectory()) { - extractLocales(fullPath); // Proper recursion - } else if (item.isFile() && item.name === "data.json.json") { + extractLocales?.(fullPath); // Proper recursion + } else if (item?.isFile() && item?.name === "data.json.json") { try { - const rawData = fs.readFileSync(fullPath, "utf8"); - const jsonData = JSON.parse(rawData); + const rawData = fs?.readFileSync?.(fullPath, "utf8"); + const jsonData = JSON?.parse?.(rawData); const language = jsonData?.item?.$?.language; if (language) { - uniqueLanguages.add(language); + uniqueLanguages?.add?.(language); } } catch (error) { - console.error(`Error reading ${fullPath}:`, error.message); + console.error(`Error reading ${fullPath}:`, error?.message); } } }