From db00bb6224293534344734ec48c7adc150dfdae8 Mon Sep 17 00:00:00 2001 From: Martin Zanoni Date: Fri, 28 Jun 2024 08:14:35 +0200 Subject: [PATCH] feat: Add new RecentlyViewed and RecentlyPurchased Modifiers --- .../src/builders/relevanceModifierBuilder.ts | 298 +++++++++++++++++- 1 file changed, 297 insertions(+), 1 deletion(-) diff --git a/packages/client/src/builders/relevanceModifierBuilder.ts b/packages/client/src/builders/relevanceModifierBuilder.ts index 16b3339..2f23d31 100644 --- a/packages/client/src/builders/relevanceModifierBuilder.ts +++ b/packages/client/src/builders/relevanceModifierBuilder.ts @@ -1,19 +1,30 @@ -import { BrandIdRelevanceModifier, ConditionBuilder, DataDoubleSelector, FilterBuilder, FixedDoubleValueSelector, ProductAssortmentRelevanceModifier, ProductCategoryIdRelevanceModifier, ProductDataRelevanceModifier, ProductIdRelevanceModifier, ProductListPriceRelevanceModifier, ProductRecentlyPurchasedByUserRelevanceModifier, ProductRecentlyViewedByUserRelevanceModifier, ProductSalesPriceRelevanceModifier, RelevanceModifierCollection, UserFavoriteProductRelevanceModifier, VariantAssortmentRelevanceModifier, VariantDataRelevanceModifier, VariantListPriceRelevanceModifier, VariantSalesPriceRelevanceModifier, VariantSpecificationsInCommonRelevanceModifier, VariantSpecificationValueRelevanceModifier } from '..'; +import { BrandIdRelevanceModifier, ConditionBuilder, ContentCategoryDataRelevanceModifier, ContentCategoryRecentlyViewedByUserRelevanceModifier, ContentDataRelevanceModifier, ContentRecentlyViewedByUserRelevanceModifier, DataDoubleSelector, FilterBuilder, FixedDoubleValueSelector, ProductAssortmentRelevanceModifier, ProductCategoryDataRelevanceModifier, ProductCategoryIdRelevanceModifier, ProductCategoryRecentlyViewedByUserRelevanceModifier, ProductDataRelevanceModifier, ProductIdRelevanceModifier, ProductListPriceRelevanceModifier, ProductRecentlyPurchasedByCompanyRelevanceModifier, ProductRecentlyPurchasedByUserCompanyRelevanceModifier, ProductRecentlyPurchasedByUserRelevanceModifier, ProductRecentlyViewedByCompanyRelevanceModifier, ProductRecentlyViewedByUserCompanyRelevanceModifier, ProductRecentlyViewedByUserRelevanceModifier, ProductSalesPriceRelevanceModifier, RelevanceModifierCollection, UserFavoriteProductRelevanceModifier, VariantAssortmentRelevanceModifier, VariantDataRelevanceModifier, VariantIdRelevanceModifier, VariantListPriceRelevanceModifier, VariantSalesPriceRelevanceModifier, VariantSpecificationsInCommonRelevanceModifier, VariantSpecificationValueRelevanceModifier } from '..'; export class RelevanceModifierBuilder { private modifiers: ( | BrandIdRelevanceModifier + | ContentCategoryDataRelevanceModifier + | ContentCategoryRecentlyViewedByUserRelevanceModifier + | ContentDataRelevanceModifier + | ContentRecentlyViewedByUserRelevanceModifier | ProductAssortmentRelevanceModifier + | ProductCategoryDataRelevanceModifier | ProductCategoryIdRelevanceModifier + | ProductCategoryRecentlyViewedByUserRelevanceModifier | ProductDataRelevanceModifier | ProductIdRelevanceModifier | ProductListPriceRelevanceModifier + | ProductRecentlyPurchasedByCompanyRelevanceModifier + | ProductRecentlyPurchasedByUserCompanyRelevanceModifier | ProductRecentlyPurchasedByUserRelevanceModifier + | ProductRecentlyViewedByCompanyRelevanceModifier + | ProductRecentlyViewedByUserCompanyRelevanceModifier | ProductRecentlyViewedByUserRelevanceModifier | ProductSalesPriceRelevanceModifier | UserFavoriteProductRelevanceModifier | VariantAssortmentRelevanceModifier | VariantDataRelevanceModifier + | VariantIdRelevanceModifier | VariantListPriceRelevanceModifier | VariantSalesPriceRelevanceModifier | VariantSpecificationsInCommonRelevanceModifier @@ -172,6 +183,122 @@ export class RelevanceModifierBuilder { return this; } + public addContentCategoryDataRelevanceModifier( + key: string, + conditions: (builder: ConditionBuilder) => void, + multiplierSelector: DataDoubleSelector | FixedDoubleValueSelector, + mustMatchAllConditions: boolean = true, + considerAsMatchIfKeyIsNotFound: boolean = false, + filter?: (builder: FilterBuilder) => void): this { + + const conditionBuilder = new ConditionBuilder(); + conditions(conditionBuilder); + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: ContentCategoryDataRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.ContentCategoryDataRelevanceModifier, Relewise.Client', + key: key, + considerAsMatchIfKeyIsNotFound: considerAsMatchIfKeyIsNotFound, + mustMatchAllConditions: mustMatchAllConditions, + conditions: conditionBuilder.build()?.items, + multiplierSelector: multiplierSelector, + multiplyWeightBy: 0, // Obsolete, but required property + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + + public addContentDataRelevanceModifier( + key: string, + conditions: (builder: ConditionBuilder) => void, + multiplierSelector: DataDoubleSelector | FixedDoubleValueSelector, + mustMatchAllConditions: boolean = true, + considerAsMatchIfKeyIsNotFound: boolean = false, + filter?: (builder: FilterBuilder) => void): this { + + const conditionBuilder = new ConditionBuilder(); + conditions(conditionBuilder); + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: ContentDataRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.ContentDataRelevanceModifier, Relewise.Client', + key: key, + considerAsMatchIfKeyIsNotFound: considerAsMatchIfKeyIsNotFound, + mustMatchAllConditions: mustMatchAllConditions, + conditions: conditionBuilder.build()?.items, + multiplierSelector: multiplierSelector, + multiplyWeightBy: 0, // Obsolete, but required property + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + + public addProductCategoryDataRelevanceModifier( + key: string, + conditions: (builder: ConditionBuilder) => void, + multiplierSelector: DataDoubleSelector | FixedDoubleValueSelector, + mustMatchAllConditions: boolean = true, + considerAsMatchIfKeyIsNotFound: boolean = false, + filter?: (builder: FilterBuilder) => void): this { + + const conditionBuilder = new ConditionBuilder(); + conditions(conditionBuilder); + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: ProductCategoryDataRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.ProductCategoryDataRelevanceModifier, Relewise.Client', + key: key, + considerAsMatchIfKeyIsNotFound: considerAsMatchIfKeyIsNotFound, + mustMatchAllConditions: mustMatchAllConditions, + conditions: conditionBuilder.build()?.items, + multiplierSelector: multiplierSelector, + multiplyWeightBy: 0, // Obsolete, but required property + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + + public addVariantIdRelevanceModifier( + variantIds: string[], + multiplyWeightBy: number = 1, + negated: boolean = false, + filter?: (builder: FilterBuilder) => void): this { + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: VariantIdRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.VariantIdRelevanceModifier, Relewise.Client', + variantIds: variantIds, + negated: negated, + multiplyWeightBy: multiplyWeightBy, + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + public addProductIdRelevanceModifier( productIds: string[], multiplyWeightBy: number = 1, @@ -369,6 +496,56 @@ export class RelevanceModifierBuilder { return this; } + public addProductRecentlyPurchasedByCompanyRelevanceModifier( + sinceMinutesAgo: number, + companyIds: string[], + ifPurchasedByCompanyMultiplyWeightBy: number = 1, + elseIfNotPurchasedByCompanyMultiplyWeightBy: number = 1, + filter?: (builder: FilterBuilder) => void): this { + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: ProductRecentlyPurchasedByCompanyRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.ProductRecentlyPurchasedByCompanyRelevanceModifier, Relewise.Client', + sinceMinutesAgo: sinceMinutesAgo, + companyIds: companyIds, + ifPurchasedByCompanyMultiplyWeightBy, + elseIfNotPurchasedByCompanyMultiplyWeightBy, + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + + public addProductRecentlyPurchasedByUserCompanyRelevanceModifier( + sinceMinutesAgo: number, + ifPurchasedByCompanyMultiplyWeightBy: number = 1, + elseIfPurchasedByParentCompanyMultiplyWeightBy: number = 1, + elseIfNotPurchasedByEitherCompanyMultiplyWeightBy: number = 1, + filter?: (builder: FilterBuilder) => void): this { + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: ProductRecentlyPurchasedByUserCompanyRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.ProductRecentlyPurchasedByUserCompanyRelevanceModifier, Relewise.Client', + sinceMinutesAgo: sinceMinutesAgo, + ifPurchasedByCompanyMultiplyWeightBy, + elseIfPurchasedByParentCompanyMultiplyWeightBy, + elseIfNotPurchasedByEitherCompanyMultiplyWeightBy, + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + public addProductRecentlyViewedByUserRelevanceModifier( sinceUtc: Date, ifNotPreviouslyViewedByUserMultiplyWeightBy: number = 1, @@ -392,6 +569,125 @@ export class RelevanceModifierBuilder { return this; } + public addProductRecentlyViewedByCompanyRelevanceModifier( + sinceMinutesAgo: number, + companyIds: string[], + ifViewedByCompanyMultiplyWeightBy: number = 1, + elseIfNotViewedByCompanyMultiplyWeightBy: number = 1, + filter?: (builder: FilterBuilder) => void): this { + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: ProductRecentlyViewedByCompanyRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.ProductRecentlyViewedByCompanyRelevanceModifier, Relewise.Client', + companyIds, + ifViewedByCompanyMultiplyWeightBy, + elseIfNotViewedByCompanyMultiplyWeightBy, + sinceMinutesAgo: sinceMinutesAgo, + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + + public addProductRecentlyViewedByUserCompanyRelevanceModifier( + sinceMinutesAgo: number, + ifViewedByUserCompanyMultiplyWeightBy: number = 1, + elseIfViewedByUserParentCompanyMultiplyWeightBy: number = 1, + elseIfNotViewedByEitherCompanyMultiplyWeightBy: number = 1, + filter?: (builder: FilterBuilder) => void): this { + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: ProductRecentlyViewedByUserCompanyRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.ProductRecentlyViewedByUserCompanyRelevanceModifier, Relewise.Client', + ifViewedByUserCompanyMultiplyWeightBy, + elseIfViewedByUserParentCompanyMultiplyWeightBy, + elseIfNotViewedByEitherCompanyMultiplyWeightBy, + sinceMinutesAgo: sinceMinutesAgo, + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + + public addProductCategoryRecentlyViewedByUserRelevanceModifier( + sinceUtc: Date, + ifNotPreviouslyViewedByUserMultiplyWeightBy: number = 1, + ifPreviouslyViewedByUserMultiplyWeightBy: number = 1, + filter?: (builder: FilterBuilder) => void): this { + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: ProductCategoryRecentlyViewedByUserRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.ProductCategoryRecentlyViewedByUserRelevanceModifier, Relewise.Client', + ifNotPreviouslyViewedByUserMultiplyWeightBy, + ifPreviouslyViewedByUserMultiplyWeightBy, + sinceUtc: sinceUtc.toJSON(), + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + + public addContentRecentlyViewedByUserRelevanceModifier( + sinceUtc: Date, + ifNotPreviouslyViewedByUserMultiplyWeightBy: number = 1, + ifPreviouslyViewedByUserMultiplyWeightBy: number = 1, + filter?: (builder: FilterBuilder) => void): this { + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: ContentRecentlyViewedByUserRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.ContentRecentlyViewedByUserRelevanceModifier, Relewise.Client', + ifNotPreviouslyViewedByUserMultiplyWeightBy, + ifPreviouslyViewedByUserMultiplyWeightBy, + sinceUtc: sinceUtc.toJSON(), + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + + public addContentCategoryRecentlyViewedByUserRelevanceModifier( + sinceUtc: Date, + ifNotPreviouslyViewedByUserMultiplyWeightBy: number = 1, + ifPreviouslyViewedByUserMultiplyWeightBy: number = 1, + filter?: (builder: FilterBuilder) => void): this { + + const filterBuilder = new FilterBuilder(); + if (filter) { + filter(filterBuilder); + } + + const modifier: ContentCategoryRecentlyViewedByUserRelevanceModifier = { + $type: 'Relewise.Client.Requests.RelevanceModifiers.ContentCategoryRecentlyViewedByUserRelevanceModifier, Relewise.Client', + ifNotPreviouslyViewedByUserMultiplyWeightBy, + ifPreviouslyViewedByUserMultiplyWeightBy, + sinceUtc: sinceUtc.toJSON(), + filters: filterBuilder.build(), + }; + this.modifiers.push(modifier); + + return this; + } + public addUserFavoriteProductRelevanceModifier( sinceMinutesAgo: number, ifNotPurchasedBaseWeight: number = 1,