Skip to content

Commit

Permalink
Merge pull request #49 from Relewise/feat/brand-updates
Browse files Browse the repository at this point in the history
feat: brand updates
  • Loading branch information
mzanoni authored Nov 17, 2023
2 parents 36c8499 + 5ac29ff commit ad5a6e4
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { BrandAdministrativeAction, FilterBuilder } from '@relewise/client';

export class BrandAdministrativeActionBuilder {
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(): BrandAdministrativeAction {
const filters = this.filterBuilder.build();

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

return {
$type: 'Relewise.Client.DataTypes.BrandAdministrativeAction, Relewise.Client',
...(this.language && { language: { value: this.language } }),
...(this.currency && { currency: { value: this.currency } }),
filters: filters,
kind: this.kind,
};
}
}
40 changes: 40 additions & 0 deletions packages/integrations/src/builders/brands/brandUpdateBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { DataValue, Brand, BrandUpdate } from '@relewise/client';

export class BrandUpdateBuilder {
private brand: Brand;
private updateKind: 'None' | 'UpdateAndAppend' | 'ReplaceProvidedProperties' | 'ClearAndReplace';

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

displayName(name: string): this {
this.brand.displayName = name;

return this;
}

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

return this;
}

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

return this;
}

build(): BrandUpdate {
return {
$type: 'Relewise.Client.DataTypes.BrandUpdate, Relewise.Client',
brand: this.brand,
kind: this.updateKind,
};
}
}
2 changes: 2 additions & 0 deletions packages/integrations/src/builders/brands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './brandUpdateBuilder';
export * from './brandAdministrativeActionBuilder';
3 changes: 2 additions & 1 deletion packages/integrations/src/builders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './categoryPathBuilder';
export * from './products';
export * from './productcategories';
export * from './contentcategories';
export * from './content';
export * from './content';
export * from './brands';
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, ProductCategoryUpdate, TrackProductCategoryUpdateRequest, TrackProductCategoryAdministrativeActionRequest, ProductCategoryAdministrativeAction, ContentCategoryAdministrativeAction, ContentCategoryUpdate, TrackContentCategoryAdministrativeActionRequest, TrackContentCategoryUpdateRequest, ContentUpdate, TrackContentUpdateRequest, ContentAdministrativeAction, TrackContentAdministrativeActionRequest } 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, BrandUpdate, BrandAdministrativeAction, TrackBrandAdministrativeActionRequest, TrackBrandUpdateRequest } from '@relewise/client';

export class Integrator extends RelewiseClient {

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

public async updateBrand(request: BrandUpdate, options?: RelewiseRequestOptions): Promise<void | undefined> {
return this.request<TrackBrandUpdateRequest, void>(
'TrackBrandUpdateRequest',
{
$type: 'Relewise.Client.Requests.Tracking.TrackBrandUpdateRequest, Relewise.Client',
brandUpdate: request,
},
options);
}

public async executeBrandAdministrativeAction(request: BrandAdministrativeAction, options?: RelewiseRequestOptions): Promise<void | undefined> {
return this.request<TrackBrandAdministrativeActionRequest, void>(
'TrackBrandAdministrativeActionRequest',
{
$type: 'Relewise.Client.Requests.Tracking.TrackBrandAdministrativeActionRequest, 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,41 @@
import { test } from '@jest/globals';
import { BrandUpdateBuilder, BrandAdministrativeActionBuilder, Integrator } 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 Brand', async() => {
const brand = new BrandUpdateBuilder({
id: '1234',
updateKind: 'ReplaceProvidedProperties',
})
.displayName('HP')
.data({
'UnixTimestamp': DataValueFactory.number(unixTimeStamp),
'Description': DataValueFactory.string('Really nice Brand'),
'Tags': DataValueFactory.stringCollection(['fall collection', 'blue', 'good-deal']),
'InStock': DataValueFactory.boolean(true),
'Removed': null,
'Complex': DataValueFactory.object({
'nestedDataKey': DataValueFactory.string('Key'),
}),
})
.assortments([1, 2, 3]);

await integrator.updateBrand(brand.build());

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

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

0 comments on commit ad5a6e4

Please sign in to comment.