Skip to content

Commit d844812

Browse files
committed
added mapper
1 parent f63cf45 commit d844812

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

api/src/services/contentful.service.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ function makeChunks(assetData: any) {
8686
return chunks;
8787
}
8888

89+
const mapLocales = ({ masterLocale, locale, locales }: any) => {
90+
if (locales?.masterLocale?.[masterLocale ?? ''] === locale) {
91+
return Object?.keys(locales?.masterLocale)?.[0]
92+
}
93+
for (const [key, value] of Object?.entries?.(locales) ?? {}) {
94+
if (typeof value !== 'object' && value === locale) {
95+
return key;
96+
}
97+
}
98+
return locale.toLowerCase();
99+
}
100+
89101
const transformCloudinaryObject = (input: any) => {
90102
const result: any = [];
91103
if (!Array.isArray(input)) {
@@ -702,18 +714,6 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
702714
}
703715
};
704716

705-
const mapLocales = ({ masterLocale, locale, locales }: any) => {
706-
console.info("🚀 ~ mapLocales ~ locales?.masterLocale?.[masterLocale ?? ''] :", locales, locales?.masterLocale, locale)
707-
if (locales?.masterLocale?.[masterLocale ?? ''] === locale) {
708-
return Object?.keys(locales?.masterLocale)?.[0]
709-
}
710-
for (const [key, value] of Object?.entries?.(locales) ?? {}) {
711-
if (typeof value !== 'object' && value === locale) {
712-
return key;
713-
}
714-
}
715-
return locale.toLowerCase();
716-
}
717717

718718
/**
719719
* Creates and processes entries from a given package file and saves them to the destination stack directory.
@@ -888,6 +888,10 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
888888
}
889889
};
890890

891+
function getKeyByValue(obj: Record<string, string>, targetValue: string): string | undefined {
892+
return Object.entries(obj).find(([_, value]) => value === targetValue)?.[0];
893+
}
894+
891895
/**
892896
* Processes and creates locale configurations from a given package file and saves them to the destination stack directory.
893897
*
@@ -912,7 +916,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
912916
*
913917
* @throws Will log errors encountered during file reading, processing, or writing of locale configurations.
914918
*/
915-
const createLocale = async (packagePath: string, destination_stack_id: string, projectId: string) => {
919+
const createLocale = async (packagePath: string, destination_stack_id: string, projectId: string, project: any) => {
916920
const srcFunc = 'createLocale';
917921
const localeSave = path.join(DATA, destination_stack_id, LOCALE_DIR_NAME);
918922
const globalFieldSave = path.join(DATA, destination_stack_id, GLOBAL_FIELDS_DIR_NAME);
@@ -936,30 +940,30 @@ const createLocale = async (packagePath: string, destination_stack_id: string, p
936940
)
937941
await customLogger(projectId, destination_stack_id, 'error', message);
938942
}
939-
940-
await Promise.all(locales.map(async (localeData: any) => {
941-
const title = localeData.sys.id;
943+
const fallbackMapLocales: any = { ...project?.master_locale ?? {}, ...project?.locales ?? {} }
944+
await Promise?.all(locales?.map?.(async (localeData: any) => {
945+
const currentMapLocale = getKeyByValue?.(fallbackMapLocales, localeData?.code) ?? `${localeData.code.toLowerCase()}`;
946+
const title = localeData?.sys?.id;
942947
const newLocale: Locale = {
943-
code: `${localeData.code.toLowerCase()}`,
944-
name: localeCodes?.[localeData.code.toLowerCase()] || "English - United States",
945-
fallback_locale: "",
948+
code: currentMapLocale,
949+
name: localeCodes?.[currentMapLocale] || "English - United States",
950+
fallback_locale: getKeyByValue(fallbackMapLocales, localeData?.fallbackCode) ?? '',
946951
uid: `${title}`,
947952
};
948953

949954
if (localeData.default === true) {
950955
msLocale[title] = newLocale;
951956
const message = getLogMessage(
952957
srcFunc,
953-
`Master Locale ${newLocale.code} has been successfully transformed.`,
958+
`Master Locale ${newLocale?.code} has been successfully transformed.`,
954959
{}
955960
)
956961
await customLogger(projectId, destination_stack_id, 'info', message);
957962
} else {
958-
newLocale.name = `${localeData.name}`;
959963
allLocales[title] = newLocale;
960964
const message = getLogMessage(
961965
srcFunc,
962-
`Locale ${newLocale.code} has been successfully transformed.`,
966+
`Locale ${newLocale?.code} has been successfully transformed.`,
963967
{}
964968
)
965969
await customLogger(projectId, destination_stack_id, 'info', message);

api/src/services/migration.service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ const startTestMigration = async (req: Request): Promise<any> => {
254254
break;
255255
}
256256
case CMS.CONTENTFUL: {
257-
await contentfulService?.createLocale(file_path, project?.current_test_stack_id, projectId);
257+
await contentfulService?.createLocale(file_path, project?.current_test_stack_id, projectId, project);
258258
await contentfulService?.createRefrence(file_path, project?.current_test_stack_id, projectId);
259259
await contentfulService?.createWebhooks(file_path, project?.current_test_stack_id, projectId);
260260
await contentfulService?.createEnvironment(file_path, project?.current_test_stack_id, projectId);
@@ -266,8 +266,8 @@ const startTestMigration = async (req: Request): Promise<any> => {
266266
default:
267267
break;
268268
}
269-
// await testFolderCreator?.({ destinationStackId: project?.current_test_stack_id });
270-
// await utilsCli?.runCli(region, user_id, project?.current_test_stack_id, projectId, true, loggerPath);
269+
await testFolderCreator?.({ destinationStackId: project?.current_test_stack_id });
270+
await utilsCli?.runCli(region, user_id, project?.current_test_stack_id, projectId, true, loggerPath);
271271
}
272272
}
273273

@@ -330,12 +330,12 @@ const startMigration = async (req: Request): Promise<any> => {
330330
break;
331331
}
332332
case CMS.CONTENTFUL: {
333-
await contentfulService?.createLocale(file_path, project?.destination_stack_id, projectId);
333+
await contentfulService?.createLocale(file_path, project?.destination_stack_id, projectId, project);
334334
await contentfulService?.createRefrence(file_path, project?.destination_stack_id, projectId);
335335
await contentfulService?.createWebhooks(file_path, project?.destination_stack_id, projectId);
336336
await contentfulService?.createEnvironment(file_path, project?.destination_stack_id, projectId);
337337
await contentfulService?.createAssets(file_path, project?.destination_stack_id, projectId);
338-
await contentfulService?.createEntry(file_path, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale);
338+
await contentfulService?.createEntry(file_path, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale, project);
339339
await contentfulService?.createVersionFile(project?.destination_stack_id, projectId);
340340
break;
341341
}

0 commit comments

Comments
 (0)