Skip to content

Commit a2ae912

Browse files
authored
Merge pull request #484 from contentstack/feature/sitecore-locale-mapper
Feature/sitecore locale mapper
2 parents ac3769d + 786fa0e commit a2ae912

File tree

23 files changed

+244
-29
lines changed

23 files changed

+244
-29
lines changed

.DS_Store

0 Bytes
Binary file not shown.

api/.DS_Store

0 Bytes
Binary file not shown.

api/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ dist/
1717
# Mono auto generated files
1818
mono_crash.*
1919

20+
#MAC DS_Store File
21+
/api/.DS_Store
22+
2023
# Build results
2124
[Dd]ebug/
2225
[Dd]ebugPublic/

api/production.env

Lines changed: 0 additions & 3 deletions
This file was deleted.

api/src/.DS_Store

0 Bytes
Binary file not shown.

api/src/controllers/migration.controller.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Request, Response } from "express";
2-
import { migrationService } from "../services/migration.service.js";
2+
import { migrationService } from "../services/migration.service.js"
33

44
/**
55
* Creates a test stack.
@@ -55,10 +55,22 @@ const getLogs = async (req: Request, res: Response): Promise<void> => {
5555
res.status(200).json(resp);
5656
};
5757

58+
const saveLocales = async (req:Request, res: Response):Promise<void> =>{
59+
const resp = await migrationService.createSourceLocales(req)
60+
res.status(200).json(resp);
61+
}
62+
63+
const saveMappedLocales = async (req:Request, res:Response):Promise<void> =>{
64+
const resp = await migrationService.updateLocaleMapper(req);
65+
res.status(200).json(resp);
66+
}
67+
5868
export const migrationController = {
5969
createTestStack,
6070
deleteTestStack,
6171
startTestMigration,
6272
startMigration,
6373
getLogs,
74+
saveLocales,
75+
saveMappedLocales
6476
};

api/src/routes/migration.routes.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,31 @@ router.get(
6565

6666
)
6767

68+
/**
69+
* Route for updating the source locales from legacy CMS
70+
* @route POST /validator
71+
* @group Migration
72+
* @param {string} projectID - Current project ID
73+
* @body {Object} locales - Fetched Locales
74+
* @returns {Promise<void>} - A promise which resolves when the locales are updated in the DB
75+
*/
76+
router.post(
77+
"/localeMapper/:projectId",
78+
asyncRouter(migrationController.saveLocales)
79+
)
80+
81+
/**
82+
* Route for updating the mapped locales by user
83+
* @route POST /validator
84+
* @group Migration
85+
* @param {string} projectID - Current project ID
86+
* @body {Object} locales - Mapped Locales
87+
* @returns {Promise<void>} - A promise which resolves when the locales are updated in the DB
88+
*/
89+
router.post(
90+
"/updateLocales/:projectId",
91+
asyncRouter(migrationController.saveMappedLocales)
92+
)
93+
6894

6995
export default router;

api/src/services/.DS_Store

6 KB
Binary file not shown.

api/src/services/migration.service.ts

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import { extensionService } from "./extension.service.js";
2525

2626

2727

28-
2928
/**
3029
* Creates a test stack.
3130
*
@@ -411,10 +410,111 @@ const getLogs = async (req: Request): Promise<any> => {
411410

412411
}
413412

413+
/**
414+
* @description - This function takes all the fetched locales from the exported data of the legacy CMS and stores/updates them in the project.json in DB
415+
*
416+
* @param req - A request body object with fetched locales [] as payload along with project ID in params
417+
* @return - void
418+
* @throws Exception if the project ID is invalid or the when the path to project.json is incorrect
419+
*/
420+
export const createSourceLocales = async (req: Request) => {
421+
422+
const projectFilePath = path.join(process.cwd(), 'database', 'project.json'); // Adjusted path to project.json
423+
const projectId = req.params.projectId;
424+
425+
const locales = req.body.locale
426+
427+
try {
428+
// Check if the project.json file exists
429+
if (!fs.existsSync(projectFilePath)) {
430+
console.error(`project.json not found at ${projectFilePath}`);
431+
throw new Error(`project.json not found.`);
432+
}
433+
434+
// Find the project with the specified projectId
435+
const project: any = ProjectModelLowdb.chain.get("projects").find({ id: projectId }).value();
436+
if (project) {
437+
const index = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value();
438+
if (index > -1) {
439+
440+
ProjectModelLowdb.update((data: any) => {
441+
data.projects[index].source_locales = locales;
442+
});
443+
} // Write back the updated projects
444+
} else {
445+
logger.error(`Project with ID: ${projectId} not found`,{
446+
status: HTTP_CODES?.NOT_FOUND,
447+
message: HTTP_TEXTS?.INVALID_ID
448+
})
449+
}
450+
} catch (err: any) {
451+
console.error("🚀 ~ createSourceLocales ~ err:", err?.response?.data ?? err, err)
452+
logger.warn('Bad Request', {
453+
status: HTTP_CODES?.BAD_REQUEST,
454+
message: HTTP_TEXTS?.INTERNAL_ERROR,
455+
});
456+
throw new ExceptionFunction(
457+
err?.message || HTTP_TEXTS.INTERNAL_ERROR,
458+
err?.statusCode || err?.status || HTTP_CODES.SERVER_ERROR
459+
);
460+
}
461+
}
462+
463+
464+
/**
465+
* @description - Function retrieves the mapped locales and updates them in the project.json in DB
466+
* @param req - A request body object with mapped locales as payload and project ID in the params
467+
* @return - void
468+
* @throws Exception if the project ID is invalid or the when the path to project.json is incorrect
469+
*/
470+
export const updateLocaleMapper = async (req:Request) =>{
471+
const mapperObject = req.body.mapper;
472+
const projectFilePath = path.join(process.cwd(), 'database', 'project.json'); // Adjusted path to project.json
473+
const projectId = req.params.projectId;
474+
475+
try {
476+
// Check if the project.json file exists
477+
if (!fs.existsSync(projectFilePath)) {
478+
console.error(`project.json not found at ${projectFilePath}`);
479+
throw new Error(`project.json not found.`);
480+
}
481+
482+
// Find the project with the specified projectId
483+
const project: any = ProjectModelLowdb.chain.get("projects").find({ id: projectId }).value();
484+
if (project) {
485+
const index = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value();
486+
if (index > -1) {
487+
ProjectModelLowdb.update((data: any) => {
488+
data.projects[index].master_locale = mapperObject?.master_locale;
489+
data.projects[index].locales = mapperObject?.locales;
490+
});
491+
} // Write back the updated projects
492+
} else {
493+
logger.error(`Project with ID: ${projectId} not found`,{
494+
status: HTTP_CODES?.NOT_FOUND,
495+
message: HTTP_TEXTS?.INVALID_ID
496+
})
497+
}
498+
} catch (err: any) {
499+
console.error("🚀 ~ createSourceLocales ~ err:", err?.response?.data ?? err, err)
500+
logger.warn('Bad Request', {
501+
status: HTTP_CODES?.BAD_REQUEST,
502+
message: HTTP_TEXTS?.INTERNAL_ERROR,
503+
});
504+
throw new ExceptionFunction(
505+
err?.message || HTTP_TEXTS.INTERNAL_ERROR,
506+
err?.statusCode || err?.status || HTTP_CODES.SERVER_ERROR
507+
);
508+
}
509+
510+
}
511+
414512
export const migrationService = {
415513
createTestStack,
416514
deleteTestStack,
417515
startTestMigration,
418516
startMigration,
419517
getLogs,
518+
createSourceLocales,
519+
updateLocaleMapper
420520
};

api/src/services/sitecore.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import { orgService } from './org.service.js';
99
import { getLogMessage } from '../utils/index.js';
1010
import customLogger from '../utils/custom-logger.utils.js';
1111

12+
1213
const append = "a";
1314

15+
1416
const baseDirName = MIGRATION_DATA_CONFIG.DATA
1517
const {
1618
ENTRIES_DIR_NAME,

0 commit comments

Comments
 (0)