diff --git a/api/src/services/contentful.service.ts b/api/src/services/contentful.service.ts index 31cabb857..8c645cd6b 100644 --- a/api/src/services/contentful.service.ts +++ b/api/src/services/contentful.service.ts @@ -84,7 +84,7 @@ function makeChunks(assetData: any) { return chunks; } -const mapLocales = ({ masterLocale, locale, locales }: any) => { +const mapLocales = ({ masterLocale, locale, locales, isNull = false }: any) => { if (locales?.masterLocale?.[masterLocale ?? ''] === locale) { return Object?.keys(locales?.masterLocale)?.[0] } @@ -93,7 +93,11 @@ const mapLocales = ({ masterLocale, locale, locales }: any) => { return key; } } - return locale.toLowerCase(); + if (isNull !== true) { + return locale?.toLowerCase?.(); + } else { + return null; + } } const transformCloudinaryObject = (input: any) => { @@ -842,7 +846,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje for await (const [localeKey, localeValues] of Object.entries( values as { [key: string]: any } )) { - const localeCode = mapLocales({ masterLocale: master_locale, locale: localeKey, locales: LocaleMapper }); + const localeCode = mapLocales({ masterLocale: master_locale, locale: localeKey, locales: LocaleMapper, isNull: true }); const chunks = makeChunks(localeValues); for (const [entryKey, entryValue] of Object.entries(localeValues)) { const message = getLogMessage( @@ -854,15 +858,18 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje } const refs: { [key: string]: any } = {}; let chunkIndex = 1; - const filePath = path.join( - entriesSave, - ctName, localeCode - ); - for await (const [chunkId, chunkData] of Object.entries(chunks)) { - refs[chunkIndex++] = `${chunkId}-entries.json`; - await writeFile(filePath, `${chunkId}-entries.json`, chunkData); + if (localeCode) { + const filePath = path.join( + entriesSave, + ctName, + localeCode + ); + for await (const [chunkId, chunkData] of Object.entries(chunks)) { + refs[chunkIndex++] = `${chunkId}-entries.json`; + await writeFile(filePath, `${chunkId}-entries.json`, chunkData); + } + await writeFile(filePath, ENTRIES_MASTER_FILE, refs); } - await writeFile(filePath, ENTRIES_MASTER_FILE, refs); } } } else { @@ -957,7 +964,8 @@ const createLocale = async (packagePath: string, destination_stack_id: string, p ) await customLogger(projectId, destination_stack_id, 'info', message); } else { - if (project?.locales?.[localeData?.code]) { + const newValueLocale = getKeyByValue(project?.locales, localeData?.code); + if (newValueLocale) { allLocales[title] = newLocale; const message = getLogMessage( srcFunc, diff --git a/api/src/services/migration.service.ts b/api/src/services/migration.service.ts index 6147fdd58..2377af978 100644 --- a/api/src/services/migration.service.ts +++ b/api/src/services/migration.service.ts @@ -254,12 +254,13 @@ const startTestMigration = async (req: Request): Promise => { break; } case CMS.CONTENTFUL: { - await contentfulService?.createLocale(file_path, project?.current_test_stack_id, projectId, project); - await contentfulService?.createRefrence(file_path, project?.current_test_stack_id, projectId); - await contentfulService?.createWebhooks(file_path, project?.current_test_stack_id, projectId); - await contentfulService?.createEnvironment(file_path, project?.current_test_stack_id, projectId); - await contentfulService?.createAssets(file_path, project?.current_test_stack_id, projectId, true); - await contentfulService?.createEntry(file_path, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale, project); + const cleanLocalPath = file_path?.replace?.(/\/$/, ''); + await contentfulService?.createLocale(cleanLocalPath, project?.current_test_stack_id, projectId, project); + await contentfulService?.createRefrence(cleanLocalPath, project?.current_test_stack_id, projectId); + await contentfulService?.createWebhooks(cleanLocalPath, project?.current_test_stack_id, projectId); + await contentfulService?.createEnvironment(cleanLocalPath, project?.current_test_stack_id, projectId); + await contentfulService?.createAssets(cleanLocalPath, project?.current_test_stack_id, projectId, true); + await contentfulService?.createEntry(cleanLocalPath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale, project); await contentfulService?.createVersionFile(project?.current_test_stack_id, projectId); break; } @@ -330,12 +331,13 @@ const startMigration = async (req: Request): Promise => { break; } case CMS.CONTENTFUL: { - await contentfulService?.createLocale(file_path, project?.destination_stack_id, projectId, project); - await contentfulService?.createRefrence(file_path, project?.destination_stack_id, projectId); - await contentfulService?.createWebhooks(file_path, project?.destination_stack_id, projectId); - await contentfulService?.createEnvironment(file_path, project?.destination_stack_id, projectId); - await contentfulService?.createAssets(file_path, project?.destination_stack_id, projectId); - await contentfulService?.createEntry(file_path, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale, project); + const cleanLocalPath = file_path?.replace?.(/\/$/, ''); + await contentfulService?.createLocale(cleanLocalPath, project?.destination_stack_id, projectId, project); + await contentfulService?.createRefrence(cleanLocalPath, project?.destination_stack_id, projectId); + await contentfulService?.createWebhooks(cleanLocalPath, project?.destination_stack_id, projectId); + await contentfulService?.createEnvironment(cleanLocalPath, project?.destination_stack_id, projectId); + await contentfulService?.createAssets(cleanLocalPath, project?.destination_stack_id, projectId); + await contentfulService?.createEntry(cleanLocalPath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale, project); await contentfulService?.createVersionFile(project?.destination_stack_id, projectId); break; } @@ -419,7 +421,6 @@ export const createSourceLocales = async (req: Request) => { const projectId = req?.params?.projectId; const locales = req?.body?.locale; - console.info("🚀 ~ createSourceLocales ~ locales:", locales); try { // Find the project with the specified projectId diff --git a/upload-api/src/services/contentful/index.ts b/upload-api/src/services/contentful/index.ts index e3e1b3075..62b22aa21 100644 --- a/upload-api/src/services/contentful/index.ts +++ b/upload-api/src/services/contentful/index.ts @@ -15,9 +15,10 @@ const createContentfulMapper = async ( ) => { try { const { localPath } = config; - const fetchedLocales: [] = await extractLocale(localPath); + const cleanLocalPath = localPath?.replace?.(/\/$/, ''); + const fetchedLocales: [] = await extractLocale(cleanLocalPath); - await extractContentTypes(localPath, affix); + await extractContentTypes(cleanLocalPath, affix); const initialMapper = await createInitialMapper(); const req = { method: 'post', @@ -29,7 +30,7 @@ const createContentfulMapper = async ( }, data: JSON.stringify(initialMapper) }; - const {data, status} = await axios.request(req); + const { data, status } = await axios.request(req); if (data?.data?.content_mapper?.length) { logger.info('Validation success:', { status: HTTP_CODES?.OK,