Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: product category updates #47

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/integrations/src/builders/categoryPathBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { CategoryNameAndId, CategoryPath } from '@relewise/client';

export type ProductCategoryPath = {
path: PathNode[]
};

export type PathNode = {
id: string;
displayName: {
value: string;
language: string;
}[]
}

export class CategoryPathBuilder {
private paths: CategoryPath[] = [];

path(builder: (builder: PathBuilder) => void): this {
const b = new PathBuilder();
builder(b);
this.paths.push({ breadcrumbPathStartingFromRoot: b.build() });

return this;
}

build(): CategoryPath[] {
return this.paths;
}
}

export class PathBuilder {
private path: PathNode[] = [];

category(categoryIdAndName: {
id: string;
displayName: {
value: string;
language: string;
}[]
}): this {

this.path.push(categoryIdAndName);

return this;
}

build(): CategoryNameAndId[] {
return this.path.map(x=> ({
id: x.id,
displayName: { values: x.displayName.map(d => ({ text: d.value, language: { value: d.language } })) },
}));
}
}
4 changes: 3 additions & 1 deletion packages/integrations/src/builders/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './products';
export * from './categoryPathBuilder';
export * from './products';
export * from './productcategories';
2 changes: 2 additions & 0 deletions packages/integrations/src/builders/productcategories/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './productCategoryUpdateBuilder';
export * from './productCategoryAdministrativeActionBuilder';
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { FilterBuilder, ProductCategoryAdministrativeAction } from '@relewise/client';

export class ProductCategoryAdministrativeActionBuilder {
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(): ProductCategoryAdministrativeAction {
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.ProductCategoryAdministrativeAction, 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, ProductCategoryUpdate, ProductCategory } from '@relewise/client';
import { CategoryPathBuilder } from '../categoryPathBuilder';

export class ProductCategoryUpdateBuilder {
private productCategory: ProductCategory;
private kind: 'UpdateAndAppend' | 'ReplaceProvidedProperties' | 'ClearAndReplace';

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

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

return this;
}

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

return this;
}

/**
* Add multiple category paths to a product 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.productCategory.categoryPaths = b.build();

return this;
}

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

return this;
}

build(): ProductCategoryUpdate {
return {
$type: 'Relewise.Client.DataTypes.ProductCategoryUpdate, Relewise.Client',
category: this.productCategory,
kind: this.kind,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ProductAdministrativeActionBuilder {
const filters = this.filterBuilder.build();

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

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { Product, DataValue, ProductVariant, Brand, ProductUpdate, CategoryNameAndId, CategoryPath } from '@relewise/client';

export type ProductCategoryPath = {
path: PathNode[]
};

export type PathNode = {
id: string;
displayName: {
value: string;
language: string;
}[]
}
import { Product, DataValue, ProductVariant, Brand, ProductUpdate } from '@relewise/client';
import { CategoryPathBuilder } from '../categoryPathBuilder';

export class ProductUpdateBuilder {
private product: Product;
Expand Down Expand Up @@ -62,24 +51,13 @@ export class ProductUpdateBuilder {
* @param paths
* @returns
*/

categoryPaths(builder: (b: CategoryPathBuilder) => void): this {
const b = new CategoryPathBuilder();
builder(b);
this.product.categoryPaths = b.build();

return this;
}
// categoryPaths(paths: ProductCategoryPath[]): this {
// this.product.categoryPaths = paths.map(p => ({
// breadcrumbPathStartingFromRoot: p.path.map(path => ({
// id: path.id,
// displayName: { values: path.displayName.map(x => ({ text: x.value, language: { value: x.language } })) },
// })),
// }));

// return this;
// }

assortments(assortments: number[]): this {
this.product.assortments = assortments;
Expand Down Expand Up @@ -116,44 +94,4 @@ export class ProductUpdateBuilder {
replaceExistingVariants: this.replaceExistingVariants,
};
}
}

export class CategoryPathBuilder {
private paths: CategoryPath[] = [];

path(builder: (builder: PathBuilder) => void): this {
const b = new PathBuilder();
builder(b);
this.paths.push({ breadcrumbPathStartingFromRoot: b.build() });

return this;
}

build(): CategoryPath[] {
return this.paths;
}
}

export class PathBuilder {
private path: PathNode[] = [];

category(categoryIdAndName: {
id: string;
displayName: {
value: string;
language: string;
}[]
}): this {

this.path.push(categoryIdAndName);

return this;
}

build(): CategoryNameAndId[] {
return this.path.map(x=> ({
id: x.id,
displayName: { values: x.displayName.map(d => ({ text: d.value, language: { value: d.language } })) },
}));
}
}
22 changes: 21 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 } from '@relewise/client';
import { RelewiseClient, RelewiseClientOptions, ProductUpdate, RelewiseRequestOptions, TrackProductUpdateRequest, ProductAdministrativeAction, TrackProductAdministrativeActionRequest, Trackable, SearchResponseCollection, BatchedTrackingRequest, ProductCategoryUpdate, TrackProductCategoryUpdateRequest, TrackProductCategoryAdministrativeActionRequest, ProductCategoryAdministrativeAction } from '@relewise/client';

export class Integrator extends RelewiseClient {

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

public async updateProductCategory(request: ProductCategoryUpdate, options?: RelewiseRequestOptions): Promise<void | undefined> {
return this.request<TrackProductCategoryUpdateRequest, void>(
'TrackProductCategoryUpdateRequest',
{
$type: 'Relewise.Client.Requests.Tracking.TrackProductCategoryUpdateRequest, Relewise.Client',
productCategoryUpdate: request,
},
options);
}

public async executeProductCategoryAdministrativeAction(request: ProductCategoryAdministrativeAction, options?: RelewiseRequestOptions): Promise<void | undefined> {
return this.request<TrackProductCategoryAdministrativeActionRequest, void>(
'TrackProductCategoryAdministrativeActionRequest',
{
$type: 'Relewise.Client.Requests.Tracking.TrackProductCategoryAdministrativeActionRequest, 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 { Integrator, ProductCategoryAdministrativeActionBuilder, ProductCategoryUpdateBuilder } 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 Product Category', async() => {
const category = new ProductCategoryUpdateBuilder({
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.updateProductCategory(category.build());

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

const disable = new ProductCategoryAdministrativeActionBuilder({
filters: (f) => f.addProductCategoryDataFilter('UnixTimeStamp', c => c.addEqualsCondition(DataValueFactory.number(unixTimeStamp), /* negated: */ true)),
kind: 'Disable',
});
await integrator.executeProductCategoryAdministrativeAction(disable.build());
SWH-Relewise marked this conversation as resolved.
Show resolved Hide resolved
});