Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
Binary file modified api/.DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ dist/
# Mono auto generated files
mono_crash.*

#MAC DS_Store File
/api/.DS_Store

# Build results
[Dd]ebug/
[Dd]ebugPublic/
Expand Down
3 changes: 0 additions & 3 deletions api/production.env

This file was deleted.

Binary file modified api/src/.DS_Store
Binary file not shown.
14 changes: 13 additions & 1 deletion api/src/controllers/migration.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import { migrationService } from "../services/migration.service.js";
import { migrationService } from "../services/migration.service.js"

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

const saveLocales = async (req:Request, res: Response):Promise<void> =>{
const resp = await migrationService.createSourceLocales(req)
res.status(200).json(resp);
}

const saveMappedLocales = async (req:Request, res:Response):Promise<void> =>{
const resp = await migrationService.updateLocaleMapper(req);
res.status(200).json(resp);
}

export const migrationController = {
createTestStack,
deleteTestStack,
startTestMigration,
startMigration,
getLogs,
saveLocales,
saveMappedLocales
};
26 changes: 26 additions & 0 deletions api/src/routes/migration.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,31 @@ router.get(

)

/**
* Route for updating the source locales from legacy CMS
* @route POST /validator
* @group Migration
* @param {string} projectID - Current project ID
* @body {Object} locales - Fetched Locales
* @returns {Promise<void>} - A promise which resolves when the locales are updated in the DB
*/
router.post(
"/localeMapper/:projectId",
asyncRouter(migrationController.saveLocales)
)

/**
* Route for updating the mapped locales by user
* @route POST /validator
* @group Migration
* @param {string} projectID - Current project ID
* @body {Object} locales - Mapped Locales
* @returns {Promise<void>} - A promise which resolves when the locales are updated in the DB
*/
router.post(
"/updateLocales/:projectId",
asyncRouter(migrationController.saveMappedLocales)
)


export default router;
Binary file added api/src/services/.DS_Store
Binary file not shown.
101 changes: 101 additions & 0 deletions api/src/services/migration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,111 @@ const getLogs = async (req: Request): Promise<any> => {

}

/**
* @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
*
* @param req - A request body object with fetched locales [] as payload along with project ID in params
* @return - void
* @throws Exception if the project ID is invalid or the when the path to project.json is incorrect
*/
export const createSourceLocales = async (req: Request) => {

const projectFilePath = path.join(process.cwd(), 'database', 'project.json'); // Adjusted path to project.json
const projectId = req.params.projectId;

const locales = req.body.locale

try {
// Check if the project.json file exists
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();
if (project) {
const index = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value();
if (index > -1) {

ProjectModelLowdb.update((data: any) => {
data.projects[index].source_locales = locales;
});
} // Write back the updated projects
} else {
logger.error(`Project with ID: ${projectId} not found`,{
status: HTTP_CODES?.NOT_FOUND,
message: HTTP_TEXTS?.INVALID_ID
})
}
} catch (err: any) {
console.error("🚀 ~ createSourceLocales ~ err:", err?.response?.data ?? err, err)
logger.warn('Bad Request', {
status: HTTP_CODES?.BAD_REQUEST,
message: HTTP_TEXTS?.INTERNAL_ERROR,
});
throw new ExceptionFunction(
err?.message || HTTP_TEXTS.INTERNAL_ERROR,
err?.statusCode || err?.status || HTTP_CODES.SERVER_ERROR
);
}
}


/**
* @description - Function retrieves the mapped locales and updates them in the project.json in DB
* @param req - A request body object with mapped locales as payload and project ID in the params
* @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.mapper;
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)) {
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();
if (project) {
const index = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value();
if (index > -1) {
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`,{
status: HTTP_CODES?.NOT_FOUND,
message: HTTP_TEXTS?.INVALID_ID
})
}
} catch (err: any) {
console.error("🚀 ~ createSourceLocales ~ err:", err?.response?.data ?? err, err)
logger.warn('Bad Request', {
status: HTTP_CODES?.BAD_REQUEST,
message: HTTP_TEXTS?.INTERNAL_ERROR,
});
throw new ExceptionFunction(
err?.message || HTTP_TEXTS.INTERNAL_ERROR,
err?.statusCode || err?.status || HTTP_CODES.SERVER_ERROR
);
}

}

export const migrationService = {
createTestStack,
deleteTestStack,
startTestMigration,
startMigration,
getLogs,
createSourceLocales,
updateLocaleMapper
};
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"husky": "^4.3.8",
"prettier": "^2.4.1",
"rimraf": "^3.0.2",
"validate-branch-name": "^1.3.0"
"validate-branch-name": "^1.3.0",
"xml2js":"^0.4.14"
},
"husky": {
"hooks": {}
Expand Down
6 changes: 0 additions & 6 deletions ui/.env.local

This file was deleted.

15 changes: 9 additions & 6 deletions upload-api/migration-sitecore/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const contentTypes = require("./libs/contenttypes.js");
import contentTypes from "./libs/contenttypes.js";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ExtractConfiguration = require("./libs/configuration.js")
import ExtractConfiguration from "./libs/configuration.js"
// eslint-disable-next-line @typescript-eslint/no-var-requires
const reference = require("./libs/reference.js");
import reference from "./libs/reference.js";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ExtractFiles = require("./libs/convert.js")
import ExtractFiles from "./libs/convert.js"

module.exports = {
import {findAndExtractLanguages} from './libs/extractLocales.js'

export {
contentTypes,
ExtractConfiguration,
reference,
ExtractFiles
ExtractFiles,
findAndExtractLanguages
}
29 changes: 29 additions & 0 deletions upload-api/migration-sitecore/libs/extractLocales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from 'fs'
import path from 'path'

const uniqueLanguages = new Set(); // Define uniqueLanguages globally or pass it as a parameter

export const findAndExtractLanguages = (dir) => {
const items = fs.readdirSync(dir, { withFileTypes: true });

for (const item of items) {
const fullPath = path.join(dir, item.name);

if (item.isDirectory()) {
findAndExtractLanguages(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 language = jsonData?.item?.$?.language;

if (language) {
uniqueLanguages.add(language);
}
} catch (error) {
console.error(`Error reading ${fullPath}:`, error.message);
}
}
}
return uniqueLanguages;
};
8 changes: 2 additions & 6 deletions upload-api/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export default {
plan: {
dropdown: { optionLimit: 100 }
},
cmsType: 'Wordpress',
cmsType: 'Sitecore',
isLocalPath: true,
awsData: {
awsRegion: 'us-east-2',
Expand All @@ -12,10 +12,6 @@ export default {
bucketName: 'migartion-test',
buketKey: 'project/package 45.zip'
},




localPath: '/home/gaurishn/Documents/contentstack/sitetitle.xml' //package 45.zip'
localPath: '/Users/shobhit.upadhyay/Downloads/package 47.zip' //package 45.zip'
// localPath: '/Users/umesh.more/Documents/ui-migration/migration-v2-node-server/upload-api/extracted_files/package 45.zip'
};
3 changes: 2 additions & 1 deletion upload-api/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const HTTP_TEXTS = {
VALIDATION_SUCCESSFULL: ' File validated successfully.',
ZIP_FILE_SAVE: 'Issue While Saving Zip File.',
XML_FILE_SAVE: 'Issue While Saving XML File.',
MAPPER_SAVED: 'Mapping process completed successfull.'
MAPPER_SAVED: 'Mapping process completed successfull.',
LOCALE_SAVED: 'Source locales stored successfully'
};

export const HTTP_RESPONSE_HEADERS = {
Expand Down
30 changes: 28 additions & 2 deletions upload-api/src/controllers/sitecore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import logger from "../../utils/logger";
import { HTTP_CODES, HTTP_TEXTS, MIGRATION_DATA_CONFIG } from "../../constants";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { contentTypes, ExtractConfiguration, reference, ExtractFiles } = require('migration-sitecore');
const { contentTypes, ExtractConfiguration, reference, ExtractFiles, findAndExtractLanguages } = require('migration-sitecore');

const {
CONTENT_TYPES_DIR_NAME,
Expand All @@ -17,6 +17,11 @@ const {
const createSitecoreMapper = async (filePath: string = "", projectId: string | string[], app_token: string | string[], affix: string | string[], config: object) => {
try {
const newPath = path.join(filePath, 'items');

const localeData = await findAndExtractLanguages(path.join(filePath, 'items','master','sitecore','content'));
console.log("Fetched Locales: ", localeData);


await ExtractFiles(newPath);
await ExtractConfiguration(newPath);
await contentTypes(newPath, affix, config);
Expand All @@ -38,7 +43,6 @@ const createSitecoreMapper = async (filePath: string = "", projectId: string | s
fieldMapping.contentTypes.push(element);
}
}
// console.log("🚀 ~ createSitecoreMapper ~ fieldMapping:", fieldMapping)
const config = {
method: 'post',
maxBodyLength: Infinity,
Expand All @@ -57,6 +61,28 @@ const createSitecoreMapper = async (filePath: string = "", projectId: string | s
message: HTTP_TEXTS?.MAPPER_SAVED,
});
}

const mapperConfig = {
method: 'post',
maxBodyLength: Infinity,
url: `${process.env.NODE_BACKEND_API}/v2/migration/localeMapper/${projectId}`,
headers: {
app_token,
'Content-Type': 'application/json'
},
data: {
locale:Array.from(localeData)
},
};

const mapRes = await axios.request(mapperConfig)
if(mapRes?.status==200){
logger.info('Legacy CMS', {
status: HTTP_CODES?.OK,
message: HTTP_TEXTS?.LOCALE_SAVED,
});
}

}
} catch (err: any) {
console.error("🚀 ~ createSitecoreMapper ~ err:", err?.response?.data ?? err)
Expand Down