From eb708ec329c3f259cb0e5b32c1e4e697a313e894 Mon Sep 17 00:00:00 2001 From: AishDani Date: Fri, 25 Oct 2024 17:35:27 +0530 Subject: [PATCH] refactor:resolved steps rendering issue when there is loader and getting migration data using projectId,added API to get logs --- api/src/constants/index.ts | 2 + api/src/controllers/migration.controller.ts | 8 ++- api/src/routes/migration.routes.ts | 6 ++ api/src/services/migration.service.ts | 58 ++++++++++++++++++- api/src/services/runCli.service.ts | 3 +- api/src/utils/field-attacher.utils.ts | 4 +- cli/packages/contentstack/.gitignore | 4 +- .../HorizontalStepper/HorizontalStepper.tsx | 8 ++- 8 files changed, 83 insertions(+), 10 deletions(-) diff --git a/api/src/constants/index.ts b/api/src/constants/index.ts index ac3163206..fe978ac3e 100644 --- a/api/src/constants/index.ts +++ b/api/src/constants/index.ts @@ -82,6 +82,8 @@ export const HTTP_TEXTS = { "Project Deleted Successfully", PROJECT_REVERT: "Project Reverted Successfully", + LOGS_NOT_FOUND: + "Sorry, no logs found for requested stack migration." }; export const HTTP_RESPONSE_HEADERS = { diff --git a/api/src/controllers/migration.controller.ts b/api/src/controllers/migration.controller.ts index ce5d6d184..9f46dc4ac 100644 --- a/api/src/controllers/migration.controller.ts +++ b/api/src/controllers/migration.controller.ts @@ -50,9 +50,15 @@ const deleteTestStack = async (req: Request, res: Response): Promise => { res.status(200).json(resp); }; +const getLogs = async (req: Request, res: Response): Promise => { + const resp = await migrationService.getLogs(req); + res.status(200).json(resp); +}; + export const migrationController = { createTestStack, deleteTestStack, startTestMigration, - startMigration + startMigration, + getLogs, }; diff --git a/api/src/routes/migration.routes.ts b/api/src/routes/migration.routes.ts index 7e4c29d90..e22a87cf0 100644 --- a/api/src/routes/migration.routes.ts +++ b/api/src/routes/migration.routes.ts @@ -59,5 +59,11 @@ router.post( asyncRouter(migrationController.startMigration) ); +router.get( + "/get_migration_logs/:orgId/:projectId/:stackId", + asyncRouter(migrationController.getLogs) + +) + export default router; diff --git a/api/src/services/migration.service.ts b/api/src/services/migration.service.ts index c886b2135..12ccd7f78 100644 --- a/api/src/services/migration.service.ts +++ b/api/src/services/migration.service.ts @@ -8,13 +8,14 @@ import { LoginServiceType } from "../models/types.js" import getAuthtoken from "../utils/auth.utils.js"; import logger from "../utils/logger.js"; import { HTTP_TEXTS, HTTP_CODES, LOCALE_MAPPER, STEPPER_STEPS } from "../constants/index.js"; -import { ExceptionFunction } from "../utils/custom-errors.utils.js"; +import { BadRequestError, ExceptionFunction } from "../utils/custom-errors.utils.js"; import { fieldAttacher } from "../utils/field-attacher.utils.js"; import { siteCoreService } from "./sitecore.service.js"; import { testFolderCreator } from "../utils/test-folder-creator.utils.js"; import { utilsCli } from './runCli.service.js'; import customLogger from "../utils/custom-logger.utils.js"; import { setLogFilePath } from "../server.js"; +import fs from 'fs'; @@ -250,9 +251,62 @@ const startMigration = async (req: Request): Promise => { } } +const getLogs = async (req: Request): Promise => { + const orgId = req?.params?.orgId; + const projectId = req?.params?.projectId; + const stackId = req?.params?.stackId; + const srcFunc = "getLogs"; + const { region, user_id } = req?.body?.token_payload ?? {}; + try { + const loggerPath = path.join(process.cwd(), 'logs', projectId, `${stackId}.log`); + if(fs.existsSync(loggerPath)){ + const logs = fs.readFileSync(loggerPath,'utf-8'); + const logEntries = logs + .split('\n') + .map(line => { + try { + return JSON.parse(line); + } catch (error) { + return null; + } + }) + .filter(entry => entry !== null); + return logEntries + + } + else{ + logger.error( + getLogMessage( + srcFunc, + HTTP_TEXTS.LOGS_NOT_FOUND, + + ) + ); + throw new BadRequestError(HTTP_TEXTS.LOGS_NOT_FOUND); + + } + + } catch (error:any) { + logger.error( + getLogMessage( + srcFunc, + HTTP_TEXTS.LOGS_NOT_FOUND, + error + ) + ); + throw new ExceptionFunction( + error?.message || HTTP_TEXTS.INTERNAL_ERROR, + error?.statusCode || error?.status || HTTP_CODES.SERVER_ERROR + ); + + } + +} + export const migrationService = { createTestStack, deleteTestStack, startTestMigration, - startMigration + startMigration, + getLogs, }; diff --git a/api/src/services/runCli.service.ts b/api/src/services/runCli.service.ts index 93959845a..61cd82220 100644 --- a/api/src/services/runCli.service.ts +++ b/api/src/services/runCli.service.ts @@ -25,6 +25,7 @@ const addCustomMessageInCliLogs = async (loggerPath: string, level: string = 'in export const runCli = async (rg: string, user_id: string, stack_uid: any, projectId: string, isTest = false, transformePath: string) => { try { + const message: string = isTest ? 'Test Migration Process Completed' : 'Migration Process Completed' const regionPresent = CS_REGIONS?.find((item: string) => item === rg) ?? 'NA'; await AuthenticationModel.read(); const userData = AuthenticationModel.chain @@ -56,7 +57,7 @@ export const runCli = async (rg: string, user_id: string, stack_uid: any, projec }) ProjectModelLowdb.write(); } - await addCustomMessageInCliLogs(loggerPath, 'info', 'Test Migration Process Completed'); + await addCustomMessageInCliLogs(loggerPath, 'info', message); } }); diff --git a/api/src/utils/field-attacher.utils.ts b/api/src/utils/field-attacher.utils.ts index 41c48ca5e..3977c6258 100644 --- a/api/src/utils/field-attacher.utils.ts +++ b/api/src/utils/field-attacher.utils.ts @@ -16,13 +16,13 @@ export const fieldAttacher = async ({ projectId, orgId, destinationStackId }: an for await (const contentId of projectData?.content_mapper ?? []) { const contentType: any = ContentTypesMapperModelLowdb.chain .get("ContentTypesMappers") - .find({ id: contentId }) + .find({ id: contentId, projectId: projectId }) .value(); if (contentType?.fieldMapping?.length) { contentType.fieldMapping = contentType?.fieldMapping?.map((fieldUid: any) => { const field = FieldMapperModel.chain .get("field_mapper") - .find({ id: fieldUid }) + .find({ id: fieldUid, projectId: projectId }) .value() return field; }) diff --git a/cli/packages/contentstack/.gitignore b/cli/packages/contentstack/.gitignore index 2bf7d18b4..9306742ca 100644 --- a/cli/packages/contentstack/.gitignore +++ b/cli/packages/contentstack/.gitignore @@ -11,4 +11,6 @@ coverage tsconfig.tsbuildinfo /merge_scripts /merge-summary.json -!bin \ No newline at end of file +!bin +/sitecoreMigrationData +/migration-data \ No newline at end of file diff --git a/ui/src/components/Stepper/HorizontalStepper/HorizontalStepper.tsx b/ui/src/components/Stepper/HorizontalStepper/HorizontalStepper.tsx index 5da5b88a6..33ed4955b 100644 --- a/ui/src/components/Stepper/HorizontalStepper/HorizontalStepper.tsx +++ b/ui/src/components/Stepper/HorizontalStepper/HorizontalStepper.tsx @@ -99,7 +99,7 @@ const HorizontalStepper = forwardRef( const stepIndex = parseInt(stepId || '', 10) - 1; if (!Number.isNaN(stepIndex) && stepIndex >= 0 && stepIndex < steps?.length) { - setShowStep(stepIndex); + !newMigrationData?.isprojectMapped && setShowStep(stepIndex); setStepsCompleted(prev => { const updatedStepsCompleted = [...prev]; for (let i = 0; i < stepIndex; i++) { @@ -171,7 +171,7 @@ const HorizontalStepper = forwardRef( }; const setTabStep = (idx: number) => { - if (stepsCompleted?.includes(idx) || stepsCompleted?.length === idx) { + if ((stepsCompleted?.includes(idx) || stepsCompleted?.length === idx) && !newMigrationData?.isprojectMapped) { setShowStep(idx); const url = `/projects/${projectId}/migration/steps/${idx + 1}`; navigate(url, { replace: true }); @@ -190,6 +190,8 @@ const HorizontalStepper = forwardRef( !stepsCompleted.includes(idx) && idx !== showStep && !stepsCompleted?.includes(idx - 1) ? 'disableEvents' : ''; + const disableStep = newMigrationData?.isprojectMapped && stepsCompleted.includes(idx) && idx !== showStep ? 'disableEvents' + : ''; const completeDisable = stepsCompleted?.includes(idx) && idx < stepIndex - 1 && newMigrationData?.test_migration?.isMigrationStarted ? 'disableEvents' : ''; @@ -199,7 +201,7 @@ const HorizontalStepper = forwardRef(
handleTabStep(idx)} >