Skip to content

Commit

Permalink
Merge pull request #1519 from contentstack/feat/DX-1130,DX-1133
Browse files Browse the repository at this point in the history
personalize api update for contentype attachment
  • Loading branch information
shafeeqd959 authored Aug 19, 2024
2 parents e34cbe3 + 09f8532 commit 30b0e96
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 38 deletions.
96 changes: 72 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/contentstack-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@contentstack/management": "~1.17.0",
"@contentstack/marketplace-sdk": "^1.2.1",
"@oclif/core": "^3.26.5",
"axios": "^1.6.4",
"axios": "^1.7.4",
"chalk": "^4.0.0",
"cli-cursor": "^3.1.0",
"cli-table": "^0.3.11",
Expand Down
11 changes: 8 additions & 3 deletions packages/contentstack-variants/src/export/experiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
config: exportConfig,
baseURL: exportConfig.modules.personalization.baseURL[exportConfig.region.name],
headers: { authtoken: exportConfig.auth_token, 'X-Project-Uid': exportConfig.project_id },
cmaConfig: {
baseURL: exportConfig.region.cma + `/v3`,
headers: { authtoken: exportConfig.auth_token, api_key: exportConfig.apiKey },
},
});
this.exportConfig = exportConfig;
this.personalizationConfig = exportConfig.modules.personalization;
Expand Down Expand Up @@ -48,9 +52,10 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf

try {
// fetch content of experience
const attachedCTs = ((await this.getCTsFromExperience(experience.uid)) || {}).content_types;
if (attachedCTs?.length) {
experienceToContentTypesMap[experience.uid] = attachedCTs;
const { variant_groups: [variantGroup] = [] } =
(await this.getVariantGroup({ experienceUid: experience.uid })) || {};
if (variantGroup?.content_types?.length) {
experienceToContentTypesMap[experience.uid] = variantGroup.content_types;
}
} catch (error) {
log(this.exportConfig, `Failed to fetch content types of experience ${experience.name}`, 'error');
Expand Down
11 changes: 9 additions & 2 deletions packages/contentstack-variants/src/import/experiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
config,
baseURL: config.modules.personalization.baseURL[config.region.name],
headers: { 'X-Project-Uid': config.modules.personalization.project_id, authtoken: config.auth_token },
cmaConfig: {
baseURL: config.region.cma + `/v3`,
headers: { authtoken: config.auth_token, api_key: config.apiKey },
},
};
super(Object.assign(config, conf));
this.personalizationConfig = this.config.modules.personalization;
Expand Down Expand Up @@ -177,16 +181,19 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
return;
}
const experienceCTsMap = fsUtil.readFile(this.experienceCTsPath, true) as Record<string, string[]>;
await Promise.allSettled(
return await Promise.allSettled(
Object.entries(this.experiencesUidMapper).map(async ([oldExpUid, newExpUid]) => {
if (experienceCTsMap[oldExpUid]?.length) {
// Filter content types that were created
const updatedContentTypes = experienceCTsMap[oldExpUid].filter((ct: string) =>
this.createdCTs.includes(ct),
);
if (updatedContentTypes?.length) {
const { variant_groups: [variantGroup] = [] } =
(await this.getVariantGroup({ experienceUid: newExpUid })) || {};
variantGroup.content_types = updatedContentTypes;
// Update content types detail in the new experience asynchronously
await this.updateCTsInExperience({ contentTypes: updatedContentTypes }, newExpUid);
return await this.updateVariantGroup(variantGroup);
}
}
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ export interface CreateProjectInput {
export type GetVariantGroupInput = {
experienceUid: string;
};

export type VariantGroup = {
uid: string;
name: string;
content_types: string[];
description: string;
} & AnyProperty;

export type VariantGroupStruct = {
variant_groups: Array<VariantGroup>;
} & AnyProperty;

export type EventStruct = {
_id: string;
uid: string;
Expand Down Expand Up @@ -152,6 +164,7 @@ export type VariantAPIRes =
| EventStruct
| AudienceStruct
| CMSExperienceStruct
| VariantGroupStruct
| Error;

export interface APIResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type APIConfig = HttpRequestConfig & {
personalizationURL?: string;
config: ExportConfig | Record<string, any> | undefined;
baseURL?: string;
cmaConfig?: HttpRequestConfig;
};

export interface AdapterConstructor<T, C> {
Expand Down
4 changes: 4 additions & 0 deletions packages/contentstack-variants/src/utils/adapter-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class AdapterHelper<C, ApiClient> implements AdapterHelperInterface<C, Ap
public readonly config: C;
public readonly $t: typeof $t;
public readonly apiClient: ApiClient;
public readonly cmaAPIClient?: ApiClient;
public readonly messages: typeof messages;

constructor(public readonly adapterConfig: APIConfig, options?: HttpClientOptions) {
Expand All @@ -27,6 +28,9 @@ export class AdapterHelper<C, ApiClient> implements AdapterHelperInterface<C, Ap
'responseType',
];
this.apiClient = new HttpClient(pick(adapterConfig, pickConfig), options) as ApiClient;
if (adapterConfig.cmaConfig) {
this.cmaAPIClient = new HttpClient(pick(adapterConfig.cmaConfig, pickConfig), options) as ApiClient;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
CMSExperienceStruct,
VariantAPIRes,
APIResponse,
VariantGroupStruct,
VariantGroup,
} from '../types';
import { formatErrors } from './error-helper';

Expand Down Expand Up @@ -75,13 +77,23 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
return this.handleVariantAPIRes(data) as ExperienceStruct;
}

async getVariantGroup(input: GetVariantGroupInput): Promise<ExperienceStruct | void> {
const getVariantGroupEndPoint = `/experiences/${input.experienceUid}`;
const data = await this.apiClient.get(getVariantGroupEndPoint);
return this.handleVariantAPIRes(data) as ExperienceStruct;
async getVariantGroup(input: GetVariantGroupInput): Promise<VariantGroupStruct | void> {
if (this.cmaAPIClient) {
const getVariantGroupEndPoint = `/variant_groups`;
const data = await this.cmaAPIClient
.queryParams({ experience_uid: input.experienceUid })
.get(getVariantGroupEndPoint);
return this.handleVariantAPIRes(data) as VariantGroupStruct;
}
}

async updateVariantGroup(input: unknown): Promise<ProjectStruct | void> {}
async updateVariantGroup(input: VariantGroup): Promise<VariantGroup | void> {
if (this.cmaAPIClient) {
const updateVariantGroupEndPoint = `/variant_groups/${input.uid}`;
const data = await this.cmaAPIClient.put(updateVariantGroupEndPoint, input);
return this.handleVariantAPIRes(data) as VariantGroup;
}
}

async getEvents(): Promise<EventStruct[] | void> {
const data = await this.apiClient.get<EventStruct>('/events');
Expand Down
16 changes: 13 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 30b0e96

Please sign in to comment.