@contentstack/cli-variants 1.2.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/export/attributes.js +27 -10
- package/lib/export/audiences.js +28 -10
- package/lib/export/events.js +28 -10
- package/lib/export/experiences.js +48 -13
- package/lib/export/projects.js +24 -6
- package/lib/export/variant-entries.js +25 -4
- package/lib/import/attribute.d.ts +2 -3
- package/lib/import/attribute.js +16 -8
- package/lib/import/audiences.d.ts +2 -3
- package/lib/import/audiences.js +21 -8
- package/lib/import/events.d.ts +3 -4
- package/lib/import/events.js +16 -9
- package/lib/import/experiences.d.ts +2 -3
- package/lib/import/experiences.js +60 -17
- package/lib/import/project.d.ts +2 -3
- package/lib/import/project.js +11 -6
- package/lib/import/variant-entries.js +62 -25
- package/lib/types/export-config.d.ts +2 -1
- package/lib/types/utils.d.ts +11 -0
- package/lib/utils/attributes-helper.js +17 -1
- package/lib/utils/audiences-helper.js +37 -6
- package/lib/utils/events-helper.js +17 -4
- package/lib/utils/personalization-api-adapter.d.ts +2 -1
- package/lib/utils/personalization-api-adapter.js +119 -27
- package/lib/utils/variant-api-adapter.d.ts +4 -1
- package/lib/utils/variant-api-adapter.js +91 -17
- package/package.json +5 -2
- package/src/export/attributes.ts +34 -10
- package/src/export/audiences.ts +35 -7
- package/src/export/events.ts +35 -7
- package/src/export/experiences.ts +74 -24
- package/src/export/projects.ts +31 -7
- package/src/export/variant-entries.ts +47 -12
- package/src/import/attribute.ts +22 -9
- package/src/import/audiences.ts +28 -10
- package/src/import/events.ts +21 -10
- package/src/import/experiences.ts +74 -20
- package/src/import/project.ts +22 -8
- package/src/import/variant-entries.ts +116 -40
- package/src/types/export-config.ts +2 -1
- package/src/types/utils.ts +12 -0
- package/src/utils/attributes-helper.ts +21 -2
- package/src/utils/audiences-helper.ts +41 -1
- package/src/utils/events-helper.ts +19 -1
- package/src/utils/personalization-api-adapter.ts +95 -19
- package/src/utils/variant-api-adapter.ts +79 -8
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { log } from '@contentstack/cli-utilities';
|
|
1
2
|
import { CreateExperienceInput, CreateExperienceVersionInput } from '../types';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -6,14 +7,22 @@ import { CreateExperienceInput, CreateExperienceVersionInput } from '../types';
|
|
|
6
7
|
* @param audiencesUid - {audiencesUid} audiences mapper data in format {<old-uid>: <new-uid>}
|
|
7
8
|
*/
|
|
8
9
|
function updateAudiences(audiences: string[], audiencesUid: Record<string, string>) {
|
|
10
|
+
log.debug(`Updating ${audiences.length} audiences`);
|
|
11
|
+
|
|
9
12
|
for (let audienceIndex = audiences.length - 1; audienceIndex >= 0; audienceIndex--) {
|
|
10
13
|
const audienceUid = audiences[audienceIndex];
|
|
14
|
+
|
|
11
15
|
if (audiencesUid.hasOwnProperty(audienceUid) && audiencesUid[audienceUid]) {
|
|
12
|
-
|
|
16
|
+
const newAudienceUid = audiencesUid[audienceUid];
|
|
17
|
+
log.debug(`Mapping audience: ${audienceUid} -> ${newAudienceUid}`);
|
|
18
|
+
audiences[audienceIndex] = newAudienceUid;
|
|
13
19
|
} else {
|
|
20
|
+
log.warn(`Audience not found in mapping: ${audienceUid}. Removing from list.`);
|
|
14
21
|
audiences.splice(audienceIndex, 1);
|
|
15
22
|
}
|
|
16
23
|
}
|
|
24
|
+
|
|
25
|
+
log.debug(`Updated audiences count: ${audiences.length}`);
|
|
17
26
|
}
|
|
18
27
|
|
|
19
28
|
/**
|
|
@@ -26,35 +35,66 @@ export const lookUpAudiences = (
|
|
|
26
35
|
experience: CreateExperienceInput,
|
|
27
36
|
audiencesUid: Record<string, string>,
|
|
28
37
|
): CreateExperienceInput => {
|
|
38
|
+
log.debug('Starting audience lookup for experience');
|
|
39
|
+
log.debug(`Available audience mappings: ${Object.keys(audiencesUid)?.length}`);
|
|
40
|
+
|
|
29
41
|
// Update experience variations
|
|
30
42
|
if (experience?.variations?.length) {
|
|
43
|
+
log.debug(`Processing ${experience.variations.length} experience variations`);
|
|
44
|
+
|
|
31
45
|
for (let index = experience.variations.length - 1; index >= 0; index--) {
|
|
32
46
|
const expVariations = experience.variations[index];
|
|
47
|
+
log.debug(`Processing variation ${index + 1}/${experience.variations.length} of type: ${expVariations['__type']}`);
|
|
48
|
+
|
|
33
49
|
if (expVariations['__type'] === 'AudienceBasedVariation' && expVariations?.audiences?.length) {
|
|
50
|
+
log.debug(`Found ${expVariations.audiences.length} audiences in AudienceBasedVariation`);
|
|
34
51
|
updateAudiences(expVariations.audiences, audiencesUid);
|
|
52
|
+
|
|
35
53
|
if (!expVariations.audiences.length) {
|
|
54
|
+
log.warn('No audiences remaining after mapping. Removing variation.');
|
|
36
55
|
experience.variations.splice(index, 1);
|
|
37
56
|
}
|
|
57
|
+
} else {
|
|
58
|
+
log.debug(`Skipping variation of type: ${expVariations['__type']}`);
|
|
38
59
|
}
|
|
39
60
|
}
|
|
40
61
|
} else if (experience.variants) {
|
|
62
|
+
log.debug(`Processing ${experience.variants.length} experience variants`);
|
|
63
|
+
|
|
41
64
|
for (let index = experience.variants.length - 1; index >= 0; index--) {
|
|
42
65
|
const expVariations = experience.variants[index];
|
|
66
|
+
log.debug(`Processing variant ${index + 1}/${experience.variants.length} of type: ${expVariations['__type']}`);
|
|
67
|
+
|
|
43
68
|
if (expVariations['__type'] === 'SegmentedVariant' && expVariations?.audiences?.length) {
|
|
69
|
+
log.debug(`Found ${expVariations.audiences.length} audiences in SegmentedVariant`);
|
|
44
70
|
updateAudiences(expVariations.audiences, audiencesUid);
|
|
71
|
+
|
|
45
72
|
if (!expVariations.audiences.length) {
|
|
73
|
+
log.warn('No audiences remaining after mapping. Removing variant.');
|
|
46
74
|
experience.variants.splice(index, 1);
|
|
47
75
|
}
|
|
76
|
+
} else {
|
|
77
|
+
log.debug(`Skipping variant of type: ${expVariations['__type']}`);
|
|
48
78
|
}
|
|
49
79
|
}
|
|
80
|
+
} else {
|
|
81
|
+
log.debug('No variations or variants found in experience');
|
|
50
82
|
}
|
|
51
83
|
|
|
52
84
|
if (experience?.targeting?.hasOwnProperty('audience') && experience?.targeting?.audience?.audiences?.length) {
|
|
85
|
+
log.debug(`Processing ${experience.targeting.audience.audiences.length} targeting audiences`);
|
|
86
|
+
|
|
53
87
|
// Update targeting audiences
|
|
54
88
|
updateAudiences(experience.targeting.audience.audiences, audiencesUid);
|
|
89
|
+
|
|
55
90
|
if (!experience.targeting.audience.audiences.length) {
|
|
91
|
+
log.warn('No targeting audiences remaining after mapping. Removing targeting.');
|
|
56
92
|
experience.targeting = {};
|
|
57
93
|
}
|
|
94
|
+
} else {
|
|
95
|
+
log.debug('No targeting audiences found in experience');
|
|
58
96
|
}
|
|
97
|
+
|
|
98
|
+
log.debug('Audience lookup completed for experience');
|
|
59
99
|
return experience;
|
|
60
100
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { log } from '@contentstack/cli-utilities';
|
|
1
2
|
import { CreateExperienceInput, ExpMetric } from '../types';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -10,17 +11,34 @@ export const lookUpEvents = (
|
|
|
10
11
|
experience: CreateExperienceInput,
|
|
11
12
|
eventsUid: Record<string, string>,
|
|
12
13
|
): CreateExperienceInput => {
|
|
14
|
+
log.debug('Starting event lookup for experience');
|
|
15
|
+
log.debug(`Available event mappings: ${Object.keys(eventsUid)?.length}`);
|
|
16
|
+
|
|
13
17
|
// Update events uid in experience metrics
|
|
14
18
|
if (experience?.metrics?.length) {
|
|
19
|
+
log.debug(`Processing ${experience.metrics.length} experience metrics`);
|
|
20
|
+
|
|
15
21
|
for (let metricIndex = experience?.metrics?.length - 1; metricIndex >= 0; metricIndex--) {
|
|
16
22
|
const metric: ExpMetric = experience?.metrics[metricIndex];
|
|
17
23
|
const eventUid = metric.event;
|
|
24
|
+
|
|
25
|
+
log.debug(`Processing metric ${metricIndex + 1}/${experience.metrics.length} with event: ${eventUid}`);
|
|
26
|
+
|
|
18
27
|
if (eventsUid.hasOwnProperty(eventUid) && eventsUid[eventUid]) {
|
|
19
|
-
|
|
28
|
+
const newEventUid = eventsUid[eventUid];
|
|
29
|
+
log.debug(`Mapping event: ${eventUid} -> ${newEventUid}`);
|
|
30
|
+
experience.metrics[metricIndex].event = newEventUid;
|
|
20
31
|
} else {
|
|
32
|
+
log.warn(`Event not found in mapping: ${eventUid}. Removing metric.`);
|
|
21
33
|
experience?.metrics.splice(metricIndex, 1);
|
|
22
34
|
}
|
|
23
35
|
}
|
|
36
|
+
|
|
37
|
+
log.debug(`Final metrics count: ${experience.metrics.length}`);
|
|
38
|
+
} else {
|
|
39
|
+
log.debug('No metrics found in experience');
|
|
24
40
|
}
|
|
41
|
+
|
|
42
|
+
log.debug('Event lookup completed for experience');
|
|
25
43
|
return experience;
|
|
26
44
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AdapterHelper } from './adapter-helper';
|
|
2
|
-
import { HttpClient, authenticationHandler } from '@contentstack/cli-utilities';
|
|
2
|
+
import { HttpClient, authenticationHandler, log } from '@contentstack/cli-utilities';
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
ProjectStruct,
|
|
@@ -23,35 +23,51 @@ import {
|
|
|
23
23
|
VariantGroupStruct,
|
|
24
24
|
VariantGroup,
|
|
25
25
|
CreateExperienceVersionInput,
|
|
26
|
+
ExportConfig
|
|
26
27
|
} from '../types';
|
|
27
28
|
import { formatErrors } from './error-helper';
|
|
28
29
|
|
|
29
30
|
export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> implements Personalization<T> {
|
|
31
|
+
public exportConfig?: ExportConfig; // Add exportConfig property to access context
|
|
32
|
+
|
|
30
33
|
constructor(options: APIConfig) {
|
|
31
34
|
super(options);
|
|
35
|
+
log.debug('PersonalizationAdapter initialized', this.exportConfig?.context);
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
async init(): Promise<void> {
|
|
39
|
+
log.debug('Initializing personalization adapter...', this.exportConfig?.context );
|
|
35
40
|
await authenticationHandler.getAuthDetails();
|
|
36
41
|
const token = authenticationHandler.accessToken;
|
|
42
|
+
log.debug(`Authentication type: ${authenticationHandler.isOauthEnabled ? 'OAuth' : 'Token'}`, this.exportConfig?.context );
|
|
43
|
+
|
|
37
44
|
if (authenticationHandler.isOauthEnabled) {
|
|
45
|
+
log.debug('Setting OAuth authorization header', this.exportConfig?.context );
|
|
38
46
|
this.apiClient.headers({ authorization: token });
|
|
39
47
|
if (this.adapterConfig.cmaConfig) {
|
|
48
|
+
log.debug('Setting OAuth authorization header for CMA client', this.exportConfig?.context );
|
|
40
49
|
this.cmaAPIClient?.headers({ authorization: token });
|
|
41
50
|
}
|
|
42
51
|
} else {
|
|
52
|
+
log.debug('Setting authtoken header', this.exportConfig?.context );
|
|
43
53
|
this.apiClient.headers({ authtoken: token });
|
|
44
54
|
if (this.adapterConfig.cmaConfig) {
|
|
55
|
+
log.debug('Setting authtoken header for CMA client', this.exportConfig?.context );
|
|
45
56
|
this.cmaAPIClient?.headers({ authtoken: token });
|
|
46
57
|
}
|
|
47
58
|
}
|
|
59
|
+
log.debug('Personalization adapter initialization completed', this.exportConfig?.context );
|
|
48
60
|
}
|
|
49
61
|
|
|
50
62
|
async projects(options: GetProjectsParams): Promise<ProjectStruct[]> {
|
|
63
|
+
log.debug(`Fetching projects for stack API key: ${options.connectedStackApiKey}`, this.exportConfig?.context );
|
|
51
64
|
await this.init();
|
|
52
65
|
const getProjectEndPoint = `/projects?connectedStackApiKey=${options.connectedStackApiKey}`;
|
|
66
|
+
log.debug(`Making API call to: ${getProjectEndPoint}`, this.exportConfig?.context );
|
|
53
67
|
const data = await this.apiClient.get(getProjectEndPoint);
|
|
54
|
-
|
|
68
|
+
const result = (await this.handleVariantAPIRes(data)) as ProjectStruct[];
|
|
69
|
+
log.debug(`Fetched ${result?.length || 0} projects`, this.exportConfig?.context );
|
|
70
|
+
return result;
|
|
55
71
|
}
|
|
56
72
|
|
|
57
73
|
/**
|
|
@@ -65,8 +81,11 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
65
81
|
* `ProjectStruct` object or `void`.
|
|
66
82
|
*/
|
|
67
83
|
async createProject(project: CreateProjectInput): Promise<ProjectStruct> {
|
|
84
|
+
log.debug(`Creating project: ${project.name}`, this.exportConfig?.context );
|
|
68
85
|
const data = await this.apiClient.post<ProjectStruct>('/projects', project);
|
|
69
|
-
|
|
86
|
+
const result = (await this.handleVariantAPIRes(data)) as ProjectStruct;
|
|
87
|
+
log.debug(`Project created successfully: ${result.uid}`, this.exportConfig?.context );
|
|
88
|
+
return result;
|
|
70
89
|
}
|
|
71
90
|
|
|
72
91
|
/**
|
|
@@ -79,41 +98,56 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
79
98
|
* `ProjectStruct`.
|
|
80
99
|
*/
|
|
81
100
|
async createAttribute(attribute: CreateAttributeInput): Promise<AttributeStruct> {
|
|
101
|
+
log.debug(`Creating attribute: ${attribute.name}`, this.exportConfig?.context );
|
|
82
102
|
const data = await this.apiClient.post<AttributeStruct>('/attributes', attribute);
|
|
83
|
-
|
|
103
|
+
const result = (await this.handleVariantAPIRes(data)) as AttributeStruct;
|
|
104
|
+
log.debug(`Attribute created successfully: ${result.uid}`, this.exportConfig?.context );
|
|
105
|
+
return result;
|
|
84
106
|
}
|
|
85
107
|
|
|
86
108
|
async getExperiences(): Promise<ExperienceStruct[]> {
|
|
109
|
+
log.debug('Fetching experiences from personalization API', this.exportConfig?.context );
|
|
87
110
|
const getExperiencesEndPoint = `/experiences`;
|
|
88
111
|
const data = await this.apiClient.get(getExperiencesEndPoint);
|
|
89
|
-
|
|
112
|
+
const result = (await this.handleVariantAPIRes(data)) as ExperienceStruct[];
|
|
113
|
+
log.debug(`Fetched ${result?.length || 0} experiences`, this.exportConfig?.context );
|
|
114
|
+
return result;
|
|
90
115
|
}
|
|
91
116
|
|
|
92
117
|
async getExperience(experienceUid: string): Promise<ExperienceStruct | void> {
|
|
118
|
+
log.debug(`Fetching experience: ${experienceUid}`, this.exportConfig?.context );
|
|
93
119
|
const getExperiencesEndPoint = `/experiences/${experienceUid}`;
|
|
94
120
|
if (this.apiClient.requestConfig?.().data) {
|
|
95
121
|
delete this.apiClient.requestConfig?.().data; // explicitly prevent any accidental body
|
|
96
122
|
}
|
|
97
123
|
const data = await this.apiClient.get(getExperiencesEndPoint);
|
|
98
|
-
|
|
124
|
+
const result = (await this.handleVariantAPIRes(data)) as ExperienceStruct;
|
|
125
|
+
log.debug(`Experience fetched successfully: ${result?.uid}`, this.exportConfig?.context );
|
|
126
|
+
return result;
|
|
99
127
|
}
|
|
100
128
|
|
|
101
129
|
async getExperienceVersions(experienceUid: string): Promise<ExperienceStruct | void> {
|
|
130
|
+
log.debug(`Fetching versions for experience: ${experienceUid}`, this.exportConfig?.context );
|
|
102
131
|
const getExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions`;
|
|
103
132
|
if (this.apiClient.requestConfig?.().data) {
|
|
104
133
|
delete this.apiClient.requestConfig?.().data; // explicitly prevent any accidental body
|
|
105
134
|
}
|
|
106
135
|
const data = await this.apiClient.get(getExperiencesVersionsEndPoint);
|
|
107
|
-
|
|
136
|
+
const result = (await this.handleVariantAPIRes(data)) as ExperienceStruct;
|
|
137
|
+
log.debug(`Experience versions fetched successfully for: ${experienceUid}`, this.exportConfig?.context );
|
|
138
|
+
return result;
|
|
108
139
|
}
|
|
109
140
|
|
|
110
141
|
async createExperienceVersion(
|
|
111
142
|
experienceUid: string,
|
|
112
143
|
input: CreateExperienceVersionInput,
|
|
113
144
|
): Promise<ExperienceStruct | void> {
|
|
145
|
+
log.debug(`Creating experience version for: ${experienceUid}`, this.exportConfig?.context );
|
|
114
146
|
const createExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions`;
|
|
115
147
|
const data = await this.apiClient.post(createExperiencesVersionsEndPoint, input);
|
|
116
|
-
|
|
148
|
+
const result = (await this.handleVariantAPIRes(data)) as ExperienceStruct;
|
|
149
|
+
log.debug(`Experience version created successfully for: ${experienceUid}`, this.exportConfig?.context );
|
|
150
|
+
return result;
|
|
117
151
|
}
|
|
118
152
|
|
|
119
153
|
async updateExperienceVersion(
|
|
@@ -121,51 +155,77 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
121
155
|
versionId: string,
|
|
122
156
|
input: CreateExperienceVersionInput,
|
|
123
157
|
): Promise<ExperienceStruct | void> {
|
|
158
|
+
log.debug(`Updating experience version: ${versionId} for experience: ${experienceUid}`, this.exportConfig?.context );
|
|
124
159
|
// loop through input and remove shortId from variant
|
|
125
160
|
if (input?.variants) {
|
|
126
161
|
input.variants = input.variants.map(({ shortUid, ...rest }) => rest);
|
|
162
|
+
log.debug(`Processed ${input.variants.length} variants for update`, this.exportConfig?.context );
|
|
127
163
|
}
|
|
128
164
|
const updateExperiencesVersionsEndPoint = `/experiences/${experienceUid}/versions/${versionId}`;
|
|
129
165
|
const data = await this.apiClient.put(updateExperiencesVersionsEndPoint, input);
|
|
130
|
-
|
|
166
|
+
const result = (await this.handleVariantAPIRes(data)) as ExperienceStruct;
|
|
167
|
+
log.debug(`Experience version updated successfully: ${versionId}`, this.exportConfig?.context );
|
|
168
|
+
return result;
|
|
131
169
|
}
|
|
132
170
|
|
|
133
171
|
async getVariantGroup(input: GetVariantGroupInput): Promise<VariantGroupStruct | void> {
|
|
172
|
+
log.debug(`Fetching variant group for experience: ${input.experienceUid}`, this.exportConfig?.context );
|
|
134
173
|
if (this.cmaAPIClient) {
|
|
135
174
|
const getVariantGroupEndPoint = `/variant_groups`;
|
|
136
175
|
const data = await this.cmaAPIClient
|
|
137
176
|
.queryParams({ experience_uid: input.experienceUid })
|
|
138
177
|
.get(getVariantGroupEndPoint);
|
|
139
|
-
|
|
178
|
+
const result = (await this.handleVariantAPIRes(data)) as VariantGroupStruct;
|
|
179
|
+
log.debug(`Variant group fetched successfully for experience: ${input.experienceUid}`, this.exportConfig?.context );
|
|
180
|
+
return result;
|
|
181
|
+
} else {
|
|
182
|
+
log.debug('CMA API client not available for variant group fetch', this.exportConfig?.context );
|
|
140
183
|
}
|
|
141
184
|
}
|
|
142
185
|
|
|
143
186
|
async updateVariantGroup(input: VariantGroup): Promise<VariantGroup | void> {
|
|
187
|
+
log.debug(`Updating variant group: ${input.uid}`, this.exportConfig?.context );
|
|
144
188
|
if (this.cmaAPIClient) {
|
|
145
189
|
const updateVariantGroupEndPoint = `/variant_groups/${input.uid}`;
|
|
146
190
|
const data = await this.cmaAPIClient.put(updateVariantGroupEndPoint, input);
|
|
147
|
-
|
|
191
|
+
const result = (await this.handleVariantAPIRes(data)) as VariantGroup;
|
|
192
|
+
log.debug(`Variant group updated successfully: ${input.uid}`, this.exportConfig?.context );
|
|
193
|
+
return result;
|
|
194
|
+
} else {
|
|
195
|
+
log.debug('CMA API client not available for variant group update', this.exportConfig?.context );
|
|
148
196
|
}
|
|
149
197
|
}
|
|
150
198
|
|
|
151
199
|
async getEvents(): Promise<EventStruct[] | void> {
|
|
200
|
+
log.debug('Fetching events from personalization API', this.exportConfig?.context );
|
|
152
201
|
const data = await this.apiClient.get<EventStruct>('/events');
|
|
153
|
-
|
|
202
|
+
const result = (await this.handleVariantAPIRes(data)) as EventStruct[];
|
|
203
|
+
log.debug(`Fetched ${result?.length || 0} events`, this.exportConfig?.context );
|
|
204
|
+
return result;
|
|
154
205
|
}
|
|
155
206
|
|
|
156
207
|
async createEvents(event: CreateEventInput): Promise<void | EventStruct> {
|
|
208
|
+
log.debug(`Creating event: ${event.key}`, this.exportConfig?.context );
|
|
157
209
|
const data = await this.apiClient.post<EventStruct>('/events', event);
|
|
158
|
-
|
|
210
|
+
const result = (await this.handleVariantAPIRes(data)) as EventStruct;
|
|
211
|
+
log.debug(`Event created successfully: ${result.uid}`, this.exportConfig?.context );
|
|
212
|
+
return result;
|
|
159
213
|
}
|
|
160
214
|
|
|
161
215
|
async getAudiences(): Promise<AudienceStruct[] | void> {
|
|
216
|
+
log.debug('Fetching audiences from personalization API', this.exportConfig?.context );
|
|
162
217
|
const data = await this.apiClient.get<AudienceStruct>('/audiences');
|
|
163
|
-
|
|
218
|
+
const result = (await this.handleVariantAPIRes(data)) as AudienceStruct[];
|
|
219
|
+
log.debug(`Fetched ${result?.length || 0} audiences`, this.exportConfig?.context );
|
|
220
|
+
return result;
|
|
164
221
|
}
|
|
165
222
|
|
|
166
223
|
async getAttributes(): Promise<AttributeStruct[] | void> {
|
|
224
|
+
log.debug('Fetching attributes from personalization API', this.exportConfig?.context );
|
|
167
225
|
const data = await this.apiClient.get<AttributeStruct>('/attributes');
|
|
168
|
-
|
|
226
|
+
const result = (await this.handleVariantAPIRes(data)) as AttributeStruct[];
|
|
227
|
+
log.debug(`Fetched ${result?.length || 0} attributes`, this.exportConfig?.context );
|
|
228
|
+
return result;
|
|
169
229
|
}
|
|
170
230
|
|
|
171
231
|
/**
|
|
@@ -177,8 +237,11 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
177
237
|
* `AudienceStruct`.
|
|
178
238
|
*/
|
|
179
239
|
async createAudience(audience: CreateAudienceInput): Promise<void | AudienceStruct> {
|
|
240
|
+
log.debug(`Creating audience: ${audience.name}`, this.exportConfig?.context );
|
|
180
241
|
const data = await this.apiClient.post<AudienceStruct>('/audiences', audience);
|
|
181
|
-
|
|
242
|
+
const result = (await this.handleVariantAPIRes(data)) as AudienceStruct;
|
|
243
|
+
log.debug(`Audience created successfully: ${result.uid}`, this.exportConfig?.context );
|
|
244
|
+
return result;
|
|
182
245
|
}
|
|
183
246
|
|
|
184
247
|
/**
|
|
@@ -190,8 +253,11 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
190
253
|
* `ExperienceStruct`.
|
|
191
254
|
*/
|
|
192
255
|
async createExperience(experience: CreateExperienceInput): Promise<void | ExperienceStruct> {
|
|
256
|
+
log.debug(`Creating experience: ${experience.name}`, this.exportConfig?.context );
|
|
193
257
|
const data = await this.apiClient.post<ExperienceStruct>('/experiences', experience);
|
|
194
|
-
|
|
258
|
+
const result = (await this.handleVariantAPIRes(data)) as ExperienceStruct;
|
|
259
|
+
log.debug(`Experience created successfully: ${result.uid}`, this.exportConfig?.context );
|
|
260
|
+
return result;
|
|
195
261
|
}
|
|
196
262
|
|
|
197
263
|
/**
|
|
@@ -203,9 +269,12 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
203
269
|
experience: UpdateExperienceInput,
|
|
204
270
|
experienceUid: string,
|
|
205
271
|
): Promise<void | CMSExperienceStruct> {
|
|
272
|
+
log.debug(`Updating content types in experience: ${experienceUid}`, this.exportConfig?.context );
|
|
206
273
|
const updateCTInExpEndPoint = `/experiences/${experienceUid}/cms-integration/variant-group`;
|
|
207
274
|
const data = await this.apiClient.post<CMSExperienceStruct>(updateCTInExpEndPoint, experience);
|
|
208
|
-
|
|
275
|
+
const result = (await this.handleVariantAPIRes(data)) as CMSExperienceStruct;
|
|
276
|
+
log.debug(`Content types updated successfully in experience: ${experienceUid}`, this.exportConfig?.context );
|
|
277
|
+
return result;
|
|
209
278
|
}
|
|
210
279
|
|
|
211
280
|
/**
|
|
@@ -214,9 +283,12 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
214
283
|
* needed to fetch CT details related to experience.
|
|
215
284
|
*/
|
|
216
285
|
async getCTsFromExperience(experienceUid: string): Promise<void | CMSExperienceStruct> {
|
|
286
|
+
log.debug(`Fetching content types from experience: ${experienceUid}`, this.exportConfig?.context );
|
|
217
287
|
const getCTFromExpEndPoint = `/experiences/${experienceUid}/cms-integration/variant-group`;
|
|
218
288
|
const data = await this.apiClient.get<CMSExperienceStruct>(getCTFromExpEndPoint);
|
|
219
|
-
|
|
289
|
+
const result = (await this.handleVariantAPIRes(data)) as CMSExperienceStruct;
|
|
290
|
+
log.debug(`Content types fetched successfully from experience: ${experienceUid}`, this.exportConfig?.context );
|
|
291
|
+
return result;
|
|
220
292
|
}
|
|
221
293
|
|
|
222
294
|
/**
|
|
@@ -227,17 +299,21 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
|
|
|
227
299
|
*/
|
|
228
300
|
async handleVariantAPIRes(res: APIResponse): Promise<VariantAPIRes> {
|
|
229
301
|
const { status, data } = res;
|
|
302
|
+
log.debug(`API response status: ${status}`, this.exportConfig?.context );
|
|
230
303
|
|
|
231
304
|
if (status >= 200 && status < 300) {
|
|
305
|
+
log.debug('API request successful', this.exportConfig?.context );
|
|
232
306
|
return data;
|
|
233
307
|
}
|
|
234
308
|
|
|
309
|
+
log.debug(`API request failed with status: ${status}`, this.exportConfig?.context );
|
|
235
310
|
// Refresh the access token if it has expired
|
|
236
311
|
await authenticationHandler.refreshAccessToken(res);
|
|
237
312
|
|
|
238
313
|
const errorMsg = data?.errors
|
|
239
314
|
? formatErrors(data.errors)
|
|
240
315
|
: data?.error || data?.error_message || data?.message || data;
|
|
316
|
+
log.debug(`API error: ${errorMsg}`, this.exportConfig?.context );
|
|
241
317
|
throw errorMsg;
|
|
242
318
|
}
|
|
243
319
|
}
|