Skip to content

Commit

Permalink
Merge pull request #48 from Relewise/feat/content-and-categories
Browse files Browse the repository at this point in the history
Feat: Content and Content categories updates
  • Loading branch information
mzanoni authored Nov 17, 2023
2 parents 3181e20 + f81e91b commit 36c8499
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ContentAdministrativeAction, FilterBuilder } from '@relewise/client';

export class ContentAdministrativeActionBuilder {
private filterBuilder = new FilterBuilder();
private kind: 'Disable' | 'Enable' | 'Delete';

language: string | null | undefined;
currency: string | null | undefined;

constructor({ language, currency, kind, filters }: {
currency?: string | null,
language?: string | null,
kind: 'Disable' | 'Enable' | 'Delete',
filters: (filterBuilder: FilterBuilder) => void
}) {
this.language = language;
this.currency = currency;
this.kind = kind;

filters(this.filterBuilder);
}

filters(filters: (filterBuilder: FilterBuilder) => void): this {
filters(this.filterBuilder);

return this;
}

build(): ContentAdministrativeAction {
const filters = this.filterBuilder.build();

if (!filters || !filters.items || filters.items.length === 0) {
throw new Error('No filters were provided for the content administrative action');
}

return {
$type: 'Relewise.Client.DataTypes.ContentAdministrativeAction, Relewise.Client',
...(this.language && { language: { value: this.language } }),
...(this.currency && { currency: { value: this.currency } }),
filters: filters,
kind: this.kind,
};
}
}
60 changes: 60 additions & 0 deletions packages/integrations/src/builders/content/contentUpdateBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { DataValue,Content, ContentUpdate } from '@relewise/client';
import { CategoryPathBuilder } from '../categoryPathBuilder';

export class ContentUpdateBuilder {
private content: Content;
private updateKind: 'UpdateAndAppend' | 'ReplaceProvidedProperties' | 'ClearAndReplace';

constructor({ id, updateKind }: {
id: string,
updateKind: 'UpdateAndAppend' | 'ReplaceProvidedProperties' | 'ClearAndReplace',
}) {
this.content = { id: id };
this.updateKind = updateKind;
}

displayName(values: {
value: string;
language: string;
}[]): this {
this.content.displayName = {
values: values.map(x => ({ text: x.value, language: { value: x.language } })),
};

return this;
}

data(data: Record<string, DataValue | null>): this {
this.content.data = data as Record<string, DataValue>; // TODO remove dirty hack

return this;
}

/**
* Add multiple category paths to a content. Start from the root to the lowest child. Example: Tools -> Outdoor -> Shovel
* @param paths
* @returns
*/
categoryPaths(builder: (b: CategoryPathBuilder) => void): this {
const b = new CategoryPathBuilder();
builder(b);
this.content.categoryPaths = b.build();

return this;
}

assortments(assortments: number[]): this {
this.content.assortments = assortments;

return this;
}


build(): ContentUpdate {
return {
$type: 'Relewise.Client.DataTypes.ContentUpdate, Relewise.Client',
content: this.content,
kind: this.updateKind,
};
}
}
2 changes: 2 additions & 0 deletions packages/integrations/src/builders/content/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './contentUpdateBuilder';
export * from './contentAdministrativeActionBuilder';
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ContentCategoryAdministrativeAction, FilterBuilder } from '@relewise/client';

export class ContentCategoryAdministrativeActionBuilder {
private filterBuilder = new FilterBuilder();
private kind: 'Disable' | 'Enable' | 'Delete';

language: string | null | undefined;
currency: string | null | undefined;

constructor({ language, currency, kind, filters }: {
currency?: string | null,
language?: string | null,
kind: 'Disable' | 'Enable' | 'Delete',
filters: (filterBuilder: FilterBuilder) => void
}) {
this.language = language;
this.currency = currency;
this.kind = kind;

filters(this.filterBuilder);
}

filters(filters: (filterBuilder: FilterBuilder) => void): this {
filters(this.filterBuilder);

return this;
}

build(): ContentCategoryAdministrativeAction {
const filters = this.filterBuilder.build();

if (!filters || !filters.items || filters.items.length === 0) {
throw new Error('No filters were provided for the product category administrative action');
}

return {
$type: 'Relewise.Client.DataTypes.ContentCategoryAdministrativeAction, Relewise.Client',
...(this.language && { language: { value: this.language } }),
...(this.currency && { currency: { value: this.currency } }),
filters: filters,
kind: this.kind,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { DataValue, ContentCategoryUpdate, ContentCategory } from '@relewise/client';
import { CategoryPathBuilder } from '../categoryPathBuilder';

export class ContentCategoryUpdateBuilder {
private contentCategory: ContentCategory;
private kind: 'UpdateAndAppend' | 'ReplaceProvidedProperties' | 'ClearAndReplace';

constructor({ id, kind }: {
id: string,
kind: 'UpdateAndAppend' | 'ReplaceProvidedProperties' | 'ClearAndReplace'
}) {
this.contentCategory = {
$type: 'Relewise.Client.DataTypes.ContentCategory, Relewise.Client',
id: id,
};
this.kind = kind;
}

displayName(values: {
value: string;
language: string;
}[]): this {
this.contentCategory.displayName = {
values: values.map(x => ({ text: x.value, language: { value: x.language } })),
};

return this;
}

data(data: Record<string, DataValue | null>): this {
this.contentCategory.data = data as Record<string, DataValue>; // TODO remove dirty hack

return this;
}

/**
* Add multiple category paths to a content category. Start from the root to the lowest child. Example: Tools -> Outdoor -> Shovel
* @param paths
* @returns
*/
categoryPaths(builder: (b: CategoryPathBuilder) => void): this {
const b = new CategoryPathBuilder();
builder(b);
this.contentCategory.categoryPaths = b.build();

return this;
}

assortments(assortments: number[]): this {
this.contentCategory.assortments = assortments;

return this;
}

build(): ContentCategoryUpdate {
return {
$type: 'Relewise.Client.DataTypes.ContentCategoryUpdate, Relewise.Client',
category: this.contentCategory,
kind: this.kind,
};
}
}
2 changes: 2 additions & 0 deletions packages/integrations/src/builders/contentcategories/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './contentCategoryUpdateBuilder';
export * from './contentCategoryAdministrativeActionBuilder';
4 changes: 3 additions & 1 deletion packages/integrations/src/builders/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './categoryPathBuilder';
export * from './products';
export * from './productcategories';
export * from './productcategories';
export * from './contentcategories';
export * from './content';
42 changes: 41 additions & 1 deletion packages/integrations/src/integrator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RelewiseClient, RelewiseClientOptions, ProductUpdate, RelewiseRequestOptions, TrackProductUpdateRequest, ProductAdministrativeAction, TrackProductAdministrativeActionRequest, Trackable, SearchResponseCollection, BatchedTrackingRequest, ProductCategoryUpdate, TrackProductCategoryUpdateRequest, TrackProductCategoryAdministrativeActionRequest, ProductCategoryAdministrativeAction } from '@relewise/client';
import { RelewiseClient, RelewiseClientOptions, ProductUpdate, RelewiseRequestOptions, TrackProductUpdateRequest, ProductAdministrativeAction, TrackProductAdministrativeActionRequest, Trackable, SearchResponseCollection, BatchedTrackingRequest, ProductCategoryUpdate, TrackProductCategoryUpdateRequest, TrackProductCategoryAdministrativeActionRequest, ProductCategoryAdministrativeAction, ContentCategoryAdministrativeAction, ContentCategoryUpdate, TrackContentCategoryAdministrativeActionRequest, TrackContentCategoryUpdateRequest, ContentUpdate, TrackContentUpdateRequest, ContentAdministrativeAction, TrackContentAdministrativeActionRequest } from '@relewise/client';

export class Integrator extends RelewiseClient {

Expand Down Expand Up @@ -48,6 +48,46 @@ export class Integrator extends RelewiseClient {
options);
}

public async updateContentCategory(request: ContentCategoryUpdate, options?: RelewiseRequestOptions): Promise<void | undefined> {
return this.request<TrackContentCategoryUpdateRequest, void>(
'TrackContentCategoryUpdateRequest',
{
$type: 'Relewise.Client.Requests.Tracking.TrackContentCategoryUpdateRequest, Relewise.Client',
contentCategoryUpdate: request,
},
options);
}

public async executeContentCategoryAdministrativeAction(request: ContentCategoryAdministrativeAction, options?: RelewiseRequestOptions): Promise<void | undefined> {
return this.request<TrackContentCategoryAdministrativeActionRequest, void>(
'TrackContentCategoryAdministrativeActionRequest',
{
$type: 'Relewise.Client.Requests.Tracking.TrackContentCategoryAdministrativeActionRequest, Relewise.Client',
administrativeAction: request,
},
options);
}

public async updateContent(request: ContentUpdate, options?: RelewiseRequestOptions): Promise<void | undefined> {
return this.request<TrackContentUpdateRequest, void>(
'TrackContentUpdateRequest',
{
$type: 'Relewise.Client.Requests.Tracking.TrackContentUpdateRequest, Relewise.Client',
contentUpdate: request,
},
options);
}

public async executeContentAdministrativeAction(request: ContentAdministrativeAction, options?: RelewiseRequestOptions): Promise<void | undefined> {
return this.request<TrackContentAdministrativeActionRequest, void>(
'TrackContentAdministrativeActionRequest',
{
$type: 'Relewise.Client.Requests.Tracking.TrackContentAdministrativeActionRequest, Relewise.Client',
administrativeAction: request,
},
options);
}

public async batch(trackable: Trackable[], options?: RelewiseRequestOptions): Promise<SearchResponseCollection | undefined> {
if (!trackable) {
throw new Error('No trackable items was provided');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { test } from '@jest/globals';
import { ContentCategoryUpdateBuilder, Integrator, ContentCategoryAdministrativeActionBuilder } from '../../../src';
import { DataValueFactory } from '@relewise/client';
const { npm_config_API_KEY: API_KEY, npm_config_DATASET_ID: DATASET_ID, npm_config_SERVER_URL: SERVER_URL } = process.env;

const integrator = new Integrator(DATASET_ID!, API_KEY!, { serverUrl: SERVER_URL });

const unixTimeStamp: number = Date.now();

test('Create Content Category', async() => {
const category = new ContentCategoryUpdateBuilder({
id: '1234',
kind: 'ReplaceProvidedProperties',
})
.displayName([
{ language: 'da', value: 'Skovle' },
])
.data({
'UnixTimestamp': DataValueFactory.number(unixTimeStamp),
'Description': DataValueFactory.string('Misc. skovle'),
'Tags': DataValueFactory.stringCollection(['outdoor', 'quality', 'good-deal']),
'InStock': DataValueFactory.boolean(true),
'Removed': null,
'Complex': DataValueFactory.object({
'nestedDataKey': DataValueFactory.string('Key'),
}),
})
.assortments([1, 2, 3])
.categoryPaths(b => b
.path(p => p
.category({
id: '1',
displayName: [{ language: 'da', value: 'Værktøj' }],
})
.category({
id: '2',
displayName: [{ language: 'da', value: 'Udendørs' }],
})
.category({
id: '3',
displayName: [{ language: 'da', value: 'Skovle' }],
})));

await integrator.updateContentCategory(category.build());

const enable = new ContentCategoryAdministrativeActionBuilder({
filters: (f) => f.addContentCategoryDataFilter('UnixTimeStamp', c => c.addEqualsCondition(DataValueFactory.number(unixTimeStamp))),
kind: 'Enable',
});
await integrator.executeContentCategoryAdministrativeAction(enable.build());

const disable = new ContentCategoryAdministrativeActionBuilder({
filters: (f) => f.addContentCategoryDataFilter('UnixTimeStamp', c => c.addEqualsCondition(DataValueFactory.number(unixTimeStamp), /* negated: */ true)),
kind: 'Disable',
});
await integrator.executeContentCategoryAdministrativeAction(disable.build());
});
Loading

0 comments on commit 36c8499

Please sign in to comment.