Skip to content

Commit

Permalink
improve price mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
mzanoni committed May 28, 2024
1 parent afacfe4 commit 4dca688
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
12 changes: 10 additions & 2 deletions full-sync/src/mapping/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ export function groupBy<T>(list: T[], fN: (item: T) => string): { [key: string]:
return grouped;
}

export function mapPrice(price: Price, includeDiscount = false) {
export function mapListPrice(price: Price) {
return mapPrice(price, /* mapDiscount */ false);
}

export function mapSalesPrice(price: Price) {
return mapPrice(price, /* mapDiscount */ true);
}

function mapPrice(price: Price, mapDiscount = false) {
return {
amount: (price.discounted && includeDiscount ? price.discounted.value.centAmount : price.value.centAmount) / 100,
amount: (price.discounted && mapDiscount ? price.discounted.value.centAmount : price.value.centAmount) / 100,
currency: `${price.country}-${price.value.currencyCode}`
}
}
10 changes: 5 additions & 5 deletions full-sync/src/mapping/mapProduct.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ProductProjection, Category, ProductVariant as CTProductVariant } from '@commercetools/platform-sdk';
import { DataValueFactory, ProductVariant } from '@relewise/client';
import { ProductVariantBuilder, ProductUpdateBuilder } from '@relewise/integrations';
import { groupBy, localizedToLanguageLookUp, localizedToMultilingual, mapPrice, searchKeywordsToMultilingual } from './helpers';
import { groupBy, localizedToLanguageLookUp, localizedToMultilingual, mapListPrice, mapSalesPrice, searchKeywordsToMultilingual } from './helpers';

export default function mapProduct(product: ProductProjection, unixTimeStamp: number, categoriesMap: Map<string, Category>) {

Expand Down Expand Up @@ -59,21 +59,21 @@ function mapVariants(variants: CTProductVariant[], product: ProductProjection):
'InStock': DataValueFactory.boolean(variant.availability?.isOnStock ?? false),
'AvailableQuantity': DataValueFactory.number(variant.availability?.availableQuantity ?? 0),
})
.listPrice(variant.prices?.map(p => mapPrice(p)) ?? [])
.salesPrice(variant.prices?.map(p => mapPrice(p, true)) ?? []);
.listPrice(variant.prices?.map(p => mapListPrice(p)) ?? [])
.salesPrice(variant.prices?.map(p => mapSalesPrice(p)) ?? []);

return builder.build();
});
}

function mapPriceOnProduct(builder: ProductUpdateBuilder, variants: CTProductVariant[]) {
const lowestListPrice = Object.entries(groupBy(
variants.flatMap(v => v.prices?.map(p => mapPrice(p)) ?? []),
variants.flatMap(v => v.prices?.map(p => mapListPrice(p)) ?? []),
(t) => t.currency))
.map(currencyGroup => ({ currency: currencyGroup[0], amount: currencyGroup[1].sort(x => x.amount)[0].amount }));

const lowestSalesPrice = Object.entries(groupBy(
variants.flatMap(v => v.prices?.map(p => mapPrice(p, true)) ?? []),
variants.flatMap(v => v.prices?.map(p => mapSalesPrice(p)) ?? []),
(t) => t.currency))
.map(currencyGroup => ({ currency: currencyGroup[0], amount: currencyGroup[1].sort(x => x.amount)[0].amount }));

Expand Down
80 changes: 78 additions & 2 deletions full-sync/tests/mapping/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from '@jest/globals';
import { localizedToLanguageLookUp, localizedToMultilingual, searchKeywordsToMultilingual } from '../../src/mapping/helpers';
import { localizedToLanguageLookUp, localizedToMultilingual, mapListPrice, mapSalesPrice, searchKeywordsToMultilingual } from '../../src/mapping/helpers';
import { DataValueFactory } from '@relewise/client';
import { SearchKeywords } from '@commercetools/platform-sdk';
import { Price, SearchKeywords } from '@commercetools/platform-sdk';

describe('Testing helpers', () => {
test('localizedToMultilingual null value', () => {
Expand Down Expand Up @@ -97,4 +97,80 @@ describe('Testing helpers', () => {
}
]));
});


test('mapSalesPrice', () => {

const subject: Price = {
country: 'dk',
id: 'a',
value: {
centAmount: 10_000,
currencyCode: 'DKK',
fractionDigits: 2,
type: 'centPrecision'
}
}

const result = mapSalesPrice(subject);

expect(result).toStrictEqual(
{
currency: "dk-DKK",
amount: 100
});
});

test('mapSalesPrice with discount', () => {

const subject: Price = {
country: 'dk',
id: 'a',
discounted: {
value: {
centAmount: 5_000,
currencyCode: 'DKK',
fractionDigits: 2,
type: 'centPrecision'
},
discount: { id: 'b', typeId: 'product-discount' }
},
value: {
centAmount: 10_000,
currencyCode: 'DKK',
fractionDigits: 2,
type: 'centPrecision'
}
}

const result = mapSalesPrice(subject);

expect(result).toStrictEqual(
{
currency: "dk-DKK",
amount: 50
});
});

test('mapListPrice', () => {

const subject: Price = {
country: 'dk',
id: 'a',
value: {
centAmount: 10_000,
currencyCode: 'DKK',
fractionDigits: 2,
type: 'centPrecision'
}
}

const result = mapListPrice(subject);

expect(result).toStrictEqual(
{
currency: "dk-DKK",
amount: 100
});
});
});

0 comments on commit 4dca688

Please sign in to comment.