diff --git a/packages/contentstack-export/src/export/modules/personalization.ts b/packages/contentstack-export/src/export/modules/personalization.ts index 423a00d7ce..e2384e9eba 100644 --- a/packages/contentstack-export/src/export/modules/personalization.ts +++ b/packages/contentstack-export/src/export/modules/personalization.ts @@ -17,6 +17,10 @@ export default class ExportPersonalization { async start(): Promise { try { + if (this.exportConfig.management_token) { + log(this.exportConfig, 'Skipping Personalize project export when using management token', 'info'); + return; + } await new ExportProjects(this.exportConfig).start(); if (this.exportConfig.personalizationEnabled) { const moduleMapper = { diff --git a/packages/contentstack-import/src/config/index.ts b/packages/contentstack-import/src/config/index.ts index 4115e5b3c6..b1a5997382 100644 --- a/packages/contentstack-import/src/config/index.ts +++ b/packages/contentstack-import/src/config/index.ts @@ -155,7 +155,7 @@ const config: DefaultConfig = { baseURL: { NA: 'https://personalization-api.contentstack.com', }, - importData: false, + importData: true, dirName: 'personalization', importOrder: ['attributes', 'audiences', 'events', 'experiences'], project_id: '', diff --git a/packages/contentstack-import/src/import/modules/personalization.ts b/packages/contentstack-import/src/import/modules/personalization.ts index a9ae395bb2..a5c50fa4b4 100644 --- a/packages/contentstack-import/src/import/modules/personalization.ts +++ b/packages/contentstack-import/src/import/modules/personalization.ts @@ -1,5 +1,4 @@ import { Import, LogType } from '@contentstack/cli-variants'; - import { log } from '../../utils'; import { ImportConfig, ModuleClassParams } from '../../types'; @@ -18,6 +17,10 @@ export default class ImportPersonalization { */ async start(): Promise { try { + if (this.config.management_token) { + log(this.config, 'Skipping Personalize project import when using management token', 'info'); + return; + } await new Import.Project(this.config, log as unknown as LogType).import(); if (this.personalization.importData) { const moduleMapper = { diff --git a/packages/contentstack-import/src/import/modules/variant-entries.ts b/packages/contentstack-import/src/import/modules/variant-entries.ts index abf8411882..cb3e4b8626 100644 --- a/packages/contentstack-import/src/import/modules/variant-entries.ts +++ b/packages/contentstack-import/src/import/modules/variant-entries.ts @@ -1,15 +1,31 @@ -import { Import, ImportHelperMethodsConfig, LogType } from '@contentstack/cli-variants'; - +import path from 'path'; +import { Import, ImportHelperMethodsConfig, LogType, ProjectStruct } from '@contentstack/cli-variants'; import { ImportConfig, ModuleClassParams } from '../../types'; -import { log, lookUpTerms, lookupAssets, lookupEntries, lookupExtension, restoreJsonRteEntryRefs } from '../../utils'; +import { + log, + lookUpTerms, + lookupAssets, + lookupEntries, + lookupExtension, + restoreJsonRteEntryRefs, + fsUtil, +} from '../../utils'; export default class ImportVarientEntries { private config: ImportConfig; public personalization: ImportConfig['modules']['personalization']; + private projectMapperFilePath: string; constructor({ importConfig }: ModuleClassParams) { this.config = importConfig; this.personalization = importConfig.modules.personalization; + this.projectMapperFilePath = path.resolve( + this.config.data, + 'mapper', + this.personalization.dirName, + 'projects', + 'projects.json', + ); } /** @@ -18,7 +34,9 @@ export default class ImportVarientEntries { */ async start(): Promise { try { - if (this.personalization.importData) { + const project = fsUtil.readFile(this.projectMapperFilePath) as ProjectStruct; + if (project && project.uid && this.personalization.importData) { + this.config.modules.personalization.project_id = project.uid; const helpers: ImportHelperMethodsConfig = { lookUpTerms, lookupAssets, @@ -27,6 +45,8 @@ export default class ImportVarientEntries { restoreJsonRteEntryRefs, }; await new Import.VariantEntries(Object.assign(this.config, { helpers })).import(); + } else { + log(this.config, 'Skipping entry variants import due to invalid project import', 'error'); } } catch (error) { log(this.config, error, 'error'); diff --git a/packages/contentstack-variants/src/import/project.ts b/packages/contentstack-variants/src/import/project.ts index a973579889..007836e26e 100644 --- a/packages/contentstack-variants/src/import/project.ts +++ b/packages/contentstack-variants/src/import/project.ts @@ -1,10 +1,11 @@ -import { join } from 'path'; +import { join, resolve as pResolve } from 'path'; import { existsSync, readFileSync } from 'fs'; -import { PersonalizationAdapter, askProjectName } from '../utils'; +import { PersonalizationAdapter, askProjectName, fsUtil } from '../utils'; import { APIConfig, CreateProjectInput, ImportConfig, LogType, ProjectStruct } from '../types'; export default class Project extends PersonalizationAdapter { + private projectMapperFolderPath: string; constructor(public readonly config: ImportConfig, private readonly log: LogType = console.log) { const conf: APIConfig = { config, @@ -12,6 +13,13 @@ export default class Project extends PersonalizationAdapter { headers: { organization_uid: config.org_uid, authtoken: config.auth_token }, }; super(Object.assign(config, conf)); + this.projectMapperFolderPath = pResolve( + config.data, + config.branchName || '', + 'mapper', + this.config.modules.personalization.dirName, + 'projects', + ); } /** @@ -49,9 +57,11 @@ export default class Project extends PersonalizationAdapter { }; const projectRes = await createProject(this.config.personalizeProjectName); - this.config.modules.personalization.project_id = projectRes.uid; this.config.modules.personalization.importData = true; + + await fsUtil.makeDirectory(this.projectMapperFolderPath); + fsUtil.writeFile(pResolve(this.projectMapperFolderPath, 'projects.json'), projectRes); this.log(this.config, `Project Created Successfully: ${projectRes.uid}`, 'info'); } } else { diff --git a/packages/contentstack-variants/src/import/variant-entries.ts b/packages/contentstack-variants/src/import/variant-entries.ts index 6a616288ab..f768c75b14 100644 --- a/packages/contentstack-variants/src/import/variant-entries.ts +++ b/packages/contentstack-variants/src/import/variant-entries.ts @@ -361,11 +361,13 @@ export default class VariantEntries extends VariantAdapter ref._content_type_uid === 'sys_assets') .map((ref: any) => ref.path); - pathsToUpdate.forEach((path: string) => setValue(variantEntry, path.split('.'))); + if (pathsToUpdate) { + pathsToUpdate.forEach((path: string) => setValue(variantEntry, path.split('.'))); + } } /**