Skip to content

Commit

Permalink
Merge pull request #43 from contentstack/fix/DX-777,DX-778
Browse files Browse the repository at this point in the history
fixed management token on variants, fixed asset reference handling is…
  • Loading branch information
shafeeqd959 authored Jun 25, 2024
2 parents 6af75a1 + d2c8334 commit f646893
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default class ExportPersonalization {

async start(): Promise<void> {
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 = {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-import/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Import, LogType } from '@contentstack/cli-variants';

import { log } from '../../utils';
import { ImportConfig, ModuleClassParams } from '../../types';

Expand All @@ -18,6 +17,10 @@ export default class ImportPersonalization {
*/
async start(): Promise<void> {
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 = {
Expand Down
28 changes: 24 additions & 4 deletions packages/contentstack-import/src/import/modules/variant-entries.ts
Original file line number Diff line number Diff line change
@@ -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',
);
}

/**
Expand All @@ -18,7 +34,9 @@ export default class ImportVarientEntries {
*/
async start(): Promise<void> {
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,
Expand All @@ -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');
Expand Down
16 changes: 13 additions & 3 deletions packages/contentstack-variants/src/import/project.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
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<ImportConfig> {
private projectMapperFolderPath: string;
constructor(public readonly config: ImportConfig, private readonly log: LogType = console.log) {
const conf: APIConfig = {
config,
baseURL: config.modules.personalization.baseURL[config.region.name],
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',
);
}

/**
Expand Down Expand Up @@ -49,9 +57,11 @@ export default class Project extends PersonalizationAdapter<ImportConfig> {
};

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 {
Expand Down
6 changes: 4 additions & 2 deletions packages/contentstack-variants/src/import/variant-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,13 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
}
};

const pathsToUpdate = variantEntry._metadata.references
const pathsToUpdate = variantEntry?._metadata?.references
.filter((ref: any) => 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('.')));
}
}

/**
Expand Down

0 comments on commit f646893

Please sign in to comment.