Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion api/src/services/migration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ export const createSourceLocales = async (req: Request) => {
* @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 mapperObject = req.body;
const projectFilePath = path.join(process.cwd(), 'database', 'project.json'); // Adjusted path to project.json
const projectId = req.params.projectId;

Expand Down
7 changes: 5 additions & 2 deletions upload-api/migration-wordpress/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable @typescript-eslint/no-var-requires */

const extractContentTypes = require('./libs/content_types.js');
const contentTypeMaker = require('./libs/contenttypemapper.js');
const extractLocale = require('./libs/extractLocale.js')

module.exports = {
extractContentTypes, //
contentTypeMaker //
extractContentTypes,
contentTypeMaker,
extractLocale
};
42 changes: 42 additions & 0 deletions upload-api/migration-wordpress/libs/extractLocale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');

/**
* Extracts unique languages/locales from a WordPress exported JSON file.
*
* @param {string} path - The file path to the WordPress JSON export.
* @returns {string[]} - An array of unique language codes found in the JSON data.
* @throws {Error} - Throws an error if the file cannot be read or parsed.
*/
const extractLocale = (path) => {
try {
const rawData = fs.readFileSync(path, 'utf8');
const jsonData = JSON.parse(rawData);
const uniqueLanguages = new Set();

// Extract global language (if exists)
if (jsonData.rss?.channel?.language) {
uniqueLanguages.add(jsonData.rss.channel.language);
}

// Extract entry-level languages (if available)
const items = jsonData.rss?.channel?.item || [];
items.forEach((item) => {
if (item['wp:postmeta']) {
const postMeta = Array.isArray(item['wp:postmeta']) ? item['wp:postmeta'] : [item['wp:postmeta']];
postMeta.forEach((meta) => {
if (meta['wp:meta_key']?.toLowerCase() === 'language' && meta['wp:meta_value']) {
uniqueLanguages.add(meta['wp:meta_value']);
}
});
}
});

return [...uniqueLanguages];
} catch (err) {
throw new Error(`Error reading JSON file: ${err.message}`);
}
};


module.exports = extractLocale;
29 changes: 27 additions & 2 deletions upload-api/src/controllers/wordpress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import axios from "axios";
import logger from "../../utils/logger";
import { HTTP_CODES, HTTP_TEXTS } from "../../constants";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { extractContentTypes, contentTypeMaker } = require('migration-wordpress')
const { extractContentTypes, contentTypeMaker, extractLocale } = require('migration-wordpress')



const createWordpressMapper = async (filePath: string = "", projectId: string | string[], app_token: string | string[], affix: string | string[], config: object) => {
try {

const localeData = await extractLocale(filePath);

await extractContentTypes(affix);
const contentTypeData = await contentTypeMaker(affix)
if(contentTypeData){
Expand All @@ -27,7 +32,27 @@ const createWordpressMapper = async (filePath: string = "", projectId: string |
data: JSON.stringify(fieldMapping),
};
const response = await axios.request(config)
// console.log(response);

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("🚀 ~ createWordpressMapper ~ err:", err?.response?.data ?? err)
Expand Down