From 54159cdcd02c31746210bd400ddbb4d2a52f7313 Mon Sep 17 00:00:00 2001 From: shafeeqd959 Date: Wed, 24 Apr 2024 14:14:14 +0530 Subject: [PATCH 1/2] added entry variants mapper logic --- packages/contentstack-audit/README.md | 14 +++---- .../contentstack-export/src/config/index.ts | 2 +- .../contentstack-import/src/config/index.ts | 2 +- .../src/export/experiences.ts | 21 +++++++--- .../src/import/experiences.ts | 40 +++++++++++++++++-- .../src/import/project.ts | 6 ++- .../src/types/personalization-api-adapter.ts | 4 +- 7 files changed, 68 insertions(+), 21 deletions(-) diff --git a/packages/contentstack-audit/README.md b/packages/contentstack-audit/README.md index f0c03e1998..ac107e5645 100644 --- a/packages/contentstack-audit/README.md +++ b/packages/contentstack-audit/README.md @@ -285,7 +285,7 @@ EXAMPLES $ csdx plugins ``` -_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.9/src/commands/plugins/index.ts)_ +_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.11/src/commands/plugins/index.ts)_ ## `csdx plugins:add PLUGIN` @@ -359,7 +359,7 @@ EXAMPLES $ csdx plugins:inspect myplugin ``` -_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.9/src/commands/plugins/inspect.ts)_ +_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.11/src/commands/plugins/inspect.ts)_ ## `csdx plugins:install PLUGIN` @@ -408,7 +408,7 @@ EXAMPLES $ csdx plugins:install someuser/someplugin ``` -_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.9/src/commands/plugins/install.ts)_ +_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.11/src/commands/plugins/install.ts)_ ## `csdx plugins:link PATH` @@ -438,7 +438,7 @@ EXAMPLES $ csdx plugins:link myplugin ``` -_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.9/src/commands/plugins/link.ts)_ +_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.11/src/commands/plugins/link.ts)_ ## `csdx plugins:remove [PLUGIN]` @@ -479,7 +479,7 @@ FLAGS --reinstall Reinstall all plugins after uninstalling. ``` -_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.9/src/commands/plugins/reset.ts)_ +_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.11/src/commands/plugins/reset.ts)_ ## `csdx plugins:uninstall [PLUGIN]` @@ -507,7 +507,7 @@ EXAMPLES $ csdx plugins:uninstall myplugin ``` -_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.9/src/commands/plugins/uninstall.ts)_ +_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.11/src/commands/plugins/uninstall.ts)_ ## `csdx plugins:unlink [PLUGIN]` @@ -551,5 +551,5 @@ DESCRIPTION Update installed plugins. ``` -_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.9/src/commands/plugins/update.ts)_ +_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.0.11/src/commands/plugins/update.ts)_ diff --git a/packages/contentstack-export/src/config/index.ts b/packages/contentstack-export/src/config/index.ts index 72faae627e..0c852c7ddb 100644 --- a/packages/contentstack-export/src/config/index.ts +++ b/packages/contentstack-export/src/config/index.ts @@ -145,7 +145,7 @@ const config: DefaultConfig = { exportVersions: false, }, personalization: { - baseURL: 'https://personalization-api.contentstack.com', + baseURL: 'https://dev-personalization-api.csnonprod.com', dirName: 'personalization', exportOrder: ['attributes', 'audiences', 'events', 'experiences'], projects: { diff --git a/packages/contentstack-import/src/config/index.ts b/packages/contentstack-import/src/config/index.ts index e2a12e0623..49d841b456 100644 --- a/packages/contentstack-import/src/config/index.ts +++ b/packages/contentstack-import/src/config/index.ts @@ -15,7 +15,7 @@ const config: DefaultConfig = { // locales: ['fr-fr'], host: 'https://api.contentstack.io/v3', extensionHost: 'https://app.contentstack.com', - personalizationHost: 'https://personalization-api.contentstack.com', + personalizationHost: 'https://dev-personalization-api.csnonprod.com', developerHubUrls: { 'https://api.contentstack.io': 'https://developerhub-api.contentstack.com', 'https://eu-api.contentstack.com': 'https://eu-developerhub-api.contentstack.com', diff --git a/packages/contentstack-variants/src/export/experiences.ts b/packages/contentstack-variants/src/export/experiences.ts index d1af497df6..06be866b99 100644 --- a/packages/contentstack-variants/src/export/experiences.ts +++ b/packages/contentstack-variants/src/export/experiences.ts @@ -1,5 +1,5 @@ import * as path from 'path'; -import { PersonalizationConfig, ExportConfig } from '../types'; +import { PersonalizationConfig, ExportConfig, ExperienceStruct } from '../types'; import { formatError, fsUtil, log, PersonalizationAdapter } from '../utils'; export default class ExportExperiences extends PersonalizationAdapter { @@ -29,16 +29,25 @@ export default class ExportExperiences extends PersonalizationAdapter = await this.getExperiences() || []; if (!experiences || experiences?.length < 1) { log(this.exportConfig, 'No Experiences found with the give project', 'info'); return; } fsUtil.writeFile(path.resolve(this.experiencesFolderPath, 'experiences.json'), experiences); - // const variantGroupDetails = []; - // for (let experience of experiences) { - // variantGroupDetails.push(await this.getVariantGroup({ experienceUid: experience.uid })); - // } + const experienceToVarianceStrList: Array = []; + for (let experience of experiences) { + if (experience?._cms?.variants) { + Object.keys(experience?._cms?.variants).forEach((variantShortId: string) => { + const experienceToVarianceStr = `${experience.uid}-${variantShortId}-${experience?._cms?.variants[variantShortId]}`; + experienceToVarianceStrList.push(experienceToVarianceStr); + }); + } + } + fsUtil.writeFile( + path.resolve(this.experiencesFolderPath, 'experiences-variants-ids.json'), + experienceToVarianceStrList, + ); } catch (error) { log(this.exportConfig, `Failed to export experiences ${formatError(error)}`, 'error'); throw error; diff --git a/packages/contentstack-variants/src/import/experiences.ts b/packages/contentstack-variants/src/import/experiences.ts index b6d9dba70a..ca171595c9 100644 --- a/packages/contentstack-variants/src/import/experiences.ts +++ b/packages/contentstack-variants/src/import/experiences.ts @@ -16,11 +16,13 @@ export default class Experiences extends PersonalizationAdapter { private eventsMapperPath: string; private audiencesMapperPath: string; private cmsVariantGroupPath: string; + private experienceVariantsIdsPath: string; + private variantUidMapperFilePath: string; private expThresholdTimer: number; private maxValidateRetry: number; private experiencesUidMapperPath: string; private expCheckIntervalDuration: number; - private cmsVariants: Record; + private cmsVariants: Record>; private cmsVariantGroups: Record; private experiencesUidMapper: Record; private pendingVariantAndVariantGrpForExperience: string[]; @@ -46,6 +48,14 @@ export default class Experiences extends PersonalizationAdapter { this.audiencesMapperPath = resolve(this.mapperDirPath, this.audienceConfig.dirName, 'uid-mapping.json'); this.eventsMapperPath = resolve(this.mapperDirPath, 'events', 'uid-mapping.json'); this.failedCmsExpPath = resolve(this.expMapperDirPath, 'failed-cms-experience.json'); + this.failedCmsExpPath = resolve(this.expMapperDirPath, 'failed-cms-experience.json'); + this.experienceVariantsIdsPath = resolve( + this.config.data, + this.personalizationConfig.dirName, + this.experienceConfig.dirName, + 'experience-variants-ids.json', + ); + this.variantUidMapperFilePath = resolve(this.expMapperDirPath, 'variants-uid-mapping.json'); this.experiencesUidMapper = {}; this.cmsVariantGroups = {}; this.cmsVariants = {}; @@ -98,8 +108,9 @@ export default class Experiences extends PersonalizationAdapter { if (this.personalizationConfig.importData) { this.log(this.config, this.messages.UPDATING_CT_IN_EXP, 'info'); await this.attachCTsInExperience(); - this.log(this.config, this.messages.UPDATED_CT_IN_EXP, 'info'); } + + await this.createVariantIdMapper(); } catch (error: any) { if (error?.errorMessage || error?.message || error?.error_message) { this.log(this.config, this.$t(this.messages.CREATE_FAILURE, { module: 'Experiences' }), 'error'); @@ -164,10 +175,12 @@ export default class Experiences extends PersonalizationAdapter { if (this.createdCTs && expRes?.contentTypes) { // Filter content types that were created const updatedContentTypes = expRes.contentTypes?.filter((ct: string) => this.createdCTs.includes(ct)); - if(updatedContentTypes?.length){ + if (updatedContentTypes?.length) { // Update content types detail in the new experience asynchronously await this.updateCTsInExperience({ contentTypes: updatedContentTypes }, newExpUid); } + } else { + this.log(this.config, `Failed to attach content type for ${newExpUid}`, 'error'); } }), ); @@ -175,4 +188,25 @@ export default class Experiences extends PersonalizationAdapter { throw error; } } + + async createVariantIdMapper() { + try { + const experienceVariantIds: any = fsUtil.readFile(this.experienceVariantsIdsPath, true) || []; + const variantUIDMapper: Record = {}; + for (let experienceVariantId of experienceVariantIds) { + const [experienceId, variantShortId, oldVariantId] = experienceVariantId.split('-'); + const latestVariantId = this.cmsVariants[this.experiencesUidMapper[experienceId]]?.[variantShortId]; + if (latestVariantId) { + variantUIDMapper[oldVariantId] = latestVariantId; + } + } + + fsUtil.writeFile(this.variantUidMapperFilePath, variantUIDMapper); + } catch (error) { + throw error; + } + } } + + + diff --git a/packages/contentstack-variants/src/import/project.ts b/packages/contentstack-variants/src/import/project.ts index ad39009ad5..e3741e4680 100644 --- a/packages/contentstack-variants/src/import/project.ts +++ b/packages/contentstack-variants/src/import/project.ts @@ -29,7 +29,11 @@ export default class Project extends PersonalizationAdapter { for (const project of projects) { const { name, description } = project; - const projectRes = await this.createProject({ name, description, connectedStackApiKey: this.config.apiKey }); + const projectRes = await this.createProject({ + name: `Copy of ${name}`, + description, + connectedStackApiKey: this.config.apiKey, + }); this.config.modules.personalization.project_id = projectRes?.uid; } diff --git a/packages/contentstack-variants/src/types/personalization-api-adapter.ts b/packages/contentstack-variants/src/types/personalization-api-adapter.ts index b496f6d98f..dd6d278780 100644 --- a/packages/contentstack-variants/src/types/personalization-api-adapter.ts +++ b/packages/contentstack-variants/src/types/personalization-api-adapter.ts @@ -112,8 +112,8 @@ export type ExperienceStruct = { status: string; metadata?: object; _cms?: { - variantGroup?: object; - variants?: object; + variantGroup: object; + variants: Record; } } & AnyProperty; From e7cb5023e31714323d930c335f30ad6cb9d071df Mon Sep 17 00:00:00 2001 From: shafeeqd959 Date: Thu, 25 Apr 2024 12:04:58 +0530 Subject: [PATCH 2/2] updated personalization url --- packages/contentstack-export/src/config/index.ts | 2 +- packages/contentstack-import/src/config/index.ts | 2 +- packages/contentstack-variants/src/export/experiences.ts | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/contentstack-export/src/config/index.ts b/packages/contentstack-export/src/config/index.ts index 5f5c6b032c..1b7e9fcc14 100644 --- a/packages/contentstack-export/src/config/index.ts +++ b/packages/contentstack-export/src/config/index.ts @@ -145,7 +145,7 @@ const config: DefaultConfig = { exportVersions: false, }, personalization: { - baseURL: 'https://dev-personalization-api.csnonprod.com', + baseURL: 'https://personalization-api.contentstack.com', dirName: 'personalization', exportOrder: ['attributes', 'audiences', 'events', 'experiences'], projects: { diff --git a/packages/contentstack-import/src/config/index.ts b/packages/contentstack-import/src/config/index.ts index 418000cd05..c3145358ee 100644 --- a/packages/contentstack-import/src/config/index.ts +++ b/packages/contentstack-import/src/config/index.ts @@ -15,7 +15,7 @@ const config: DefaultConfig = { // locales: ['fr-fr'], host: 'https://api.contentstack.io/v3', extensionHost: 'https://app.contentstack.com', - personalizationHost: 'https://dev-personalization-api.csnonprod.com', + personalizationHost: 'https://personalization-api.contentstack.com', developerHubUrls: { 'https://api.contentstack.io': 'https://developerhub-api.contentstack.com', 'https://eu-api.contentstack.com': 'https://eu-developerhub-api.contentstack.com', diff --git a/packages/contentstack-variants/src/export/experiences.ts b/packages/contentstack-variants/src/export/experiences.ts index 06be866b99..ba113667e1 100644 --- a/packages/contentstack-variants/src/export/experiences.ts +++ b/packages/contentstack-variants/src/export/experiences.ts @@ -37,9 +37,10 @@ export default class ExportExperiences extends PersonalizationAdapter = []; for (let experience of experiences) { - if (experience?._cms?.variants) { - Object.keys(experience?._cms?.variants).forEach((variantShortId: string) => { - const experienceToVarianceStr = `${experience.uid}-${variantShortId}-${experience?._cms?.variants[variantShortId]}`; + let variants = experience?._cms?.variants; + if (variants) { + Object.keys(variants).forEach((variantShortId: string) => { + const experienceToVarianceStr = `${experience.uid}-${variantShortId}-${variants[variantShortId]}`; experienceToVarianceStrList.push(experienceToVarianceStr); }); }