Skip to content

Commit

Permalink
Merge pull request #26 from contentstack/feat/CS-44627
Browse files Browse the repository at this point in the history
Added variants mapper file
  • Loading branch information
shafeeqd959 authored Apr 25, 2024
2 parents 894af8f + e7cb502 commit 223dd1a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 19 deletions.
14 changes: 7 additions & 7 deletions packages/contentstack-audit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -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]`

Expand Down Expand Up @@ -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]`

Expand Down Expand Up @@ -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]`

Expand Down Expand Up @@ -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)_
<!-- commandsstop -->
22 changes: 16 additions & 6 deletions packages/contentstack-variants/src/export/experiences.ts
Original file line number Diff line number Diff line change
@@ -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<ExportConfig> {
Expand Down Expand Up @@ -29,16 +29,26 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
// write experiences in to a file
log(this.exportConfig, 'Starting experiences export', 'info');
await fsUtil.makeDirectory(this.experiencesFolderPath);
const experiences = await this.getExperiences();
const experiences: Array<ExperienceStruct> = 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<string> = [];
for (let experience of experiences) {
let variants = experience?._cms?.variants;
if (variants) {
Object.keys(variants).forEach((variantShortId: string) => {
const experienceToVarianceStr = `${experience.uid}-${variantShortId}-${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;
Expand Down
40 changes: 37 additions & 3 deletions packages/contentstack-variants/src/import/experiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
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<string, unknown>;
private cmsVariants: Record<string, Record<string, string>>;
private cmsVariantGroups: Record<string, unknown>;
private experiencesUidMapper: Record<string, string>;
private pendingVariantAndVariantGrpForExperience: string[];
Expand All @@ -46,6 +48,14 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
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 = {};
Expand Down Expand Up @@ -98,8 +108,9 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
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');
Expand Down Expand Up @@ -164,15 +175,38 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
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');
}
}),
);
} catch (error) {
throw error;
}
}

async createVariantIdMapper() {
try {
const experienceVariantIds: any = fsUtil.readFile(this.experienceVariantsIdsPath, true) || [];
const variantUIDMapper: Record<string, string> = {};
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;
}
}
}



6 changes: 5 additions & 1 deletion packages/contentstack-variants/src/import/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export default class Project extends PersonalizationAdapter<ImportConfig> {

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ export type ExperienceStruct = {
status: string;
metadata?: object;
_cms?: {
variantGroup?: object;
variants?: object;
variantGroup: object;
variants: Record<string, string>;
}
} & AnyProperty;

Expand Down

0 comments on commit 223dd1a

Please sign in to comment.