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 27, 2024
1 parent 9e368e4 commit 38c7289
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
28 changes: 19 additions & 9 deletions deployment.http
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
@BEARER_TOKEN = '';

### Deployment documentation: https://docs.commercetools.com/connect/getting-started#deploy-a-connector

### Create a BEARER TOKEN for deployments
POST https://auth.europe-west1.gcp.commercetools.com/oauth/token
Authorization: {{BASIC_AUTH_TOKEN}}
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials


### Flow for creating draft and releasing connector app

@BEARER_TOKEN = '';

### Create draft deployment

POST https://connect.eu-central-1.aws.commercetools.com/connectors/drafts
Authorization: Bearer {{BEARER_TOKEN}}
POST https://connect.europe-west1.gcp.commercetools.com/connectors/drafts
Authorization: {{BEARER_TOKEN}}
Content-Type: application/json

{
"key": "relewise-connector",
"key": "relewise",
"name": "Relewise",
"description": "",
"creator": {
Expand All @@ -18,13 +28,13 @@ Content-Type: application/json
},
"repository": {
"url": "https://github.com/Relewise/relewise-integrations-commercetools-connect.git",
"tag": "1.0.0"
"tag": "0.0.1"
}
}

### Request a ConnectorStaged for previewable
POST https://connect.eu-central-1.aws.commercetools.com/connectors/drafts/key=relewise-connector
Authorization: Bearer {{BEARER_TOKEN}}
POST https://connect.europe-west1.gcp.commercetools.com/connectors/drafts/key=relewise
Authorization: {{BEARER_TOKEN}}
Content-Type: application/json

{
Expand All @@ -36,7 +46,7 @@ Content-Type: application/json

### Submit a ConnectorStaged for certification
POST https://connect.eu-central-1.aws.commercetools.com/connectors/drafts/key=relewise-connector
Authorization: Bearer {{BEARER_TOKEN}}
Authorization: {{BEARER_TOKEN}}
Content-Type: application/json

{
Expand Down
9 changes: 8 additions & 1 deletion full-sync/src/mapping/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LocalizedString, SearchKeywords } from '@commercetools/platform-sdk';
import { LocalizedString, Price, SearchKeywords } from '@commercetools/platform-sdk';
import { DataValueFactory } from '@relewise/client';

export function localizedToMultilingual(value?: LocalizedString) {
Expand Down Expand Up @@ -50,3 +50,10 @@ export function groupBy<T>(list: T[], fN: (item: T) => string): { [key: string]:

return grouped;
}

export function mapPrice(price: Price, includeDiscount = false) {
return {
amount: (price.discounted && includeDiscount ? price.discounted.value.centAmount : price.value.centAmount) / 100,
currency: `${price.country}-${price.value.currencyCode}`
}
}
23 changes: 9 additions & 14 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, searchKeywordsToMultilingual } from './helpers';
import { groupBy, localizedToLanguageLookUp, localizedToMultilingual, mapPrice, searchKeywordsToMultilingual } from './helpers';

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

Expand Down Expand Up @@ -56,30 +56,25 @@ function mapVariants(variants: CTProductVariant[], product: ProductProjection):
'Id': DataValueFactory.number(variant.id),
'IsMasterVariant': DataValueFactory.boolean(variant.id == product.masterVariant.id),
'Images': variant.images ? DataValueFactory.stringCollection(variant.images.map(x => x.url)) : null,
'InStock': DataValueFactory.boolean(variant.availability?.isOnStock ?? false)
'InStock': DataValueFactory.boolean(variant.availability?.isOnStock ?? false),
'AvailableQuantity': DataValueFactory.number(variant.availability?.availableQuantity ?? 0),
})
.listPrice(variant.prices?.map(p => ({
amount: p.value.centAmount / 100,
currency: p.value.currencyCode
})) ?? [])
.salesPrice(variant.prices?.map(p => ({
amount: (p.discounted ? p.discounted.value.centAmount : p.value.centAmount) / 100,
currency: p.value.currencyCode
})) ?? []);
.listPrice(variant.prices?.map(p => mapPrice(p)) ?? [])
.salesPrice(variant.prices?.map(p => mapPrice(p, true)) ?? []);

return builder.build();
});
}

function mapPriceOnProduct(builder: ProductUpdateBuilder, variants: CTProductVariant[]) {
const lowestListPrice = Object.entries(groupBy(
variants.flatMap(v => v.prices?.map(p => ({ amount: p.value.centAmount / 100, currency: p.value.currencyCode })) ?? []),
(t) => t.currency))
variants.flatMap(v => v.prices?.map(p => mapPrice(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 => ({ amount: (p.discounted ? p.discounted.value.centAmount : p.value.centAmount) / 100, currency: p.value.currencyCode })) ?? []),
(t) => t.currency))
variants.flatMap(v => v.prices?.map(p => mapPrice(p, true)) ?? []),
(t) => t.currency))
.map(currencyGroup => ({ currency: currencyGroup[0], amount: currencyGroup[1].sort(x => x.amount)[0].amount }));

builder.listPrice(lowestListPrice);
Expand Down
2 changes: 2 additions & 0 deletions full-sync/src/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export default async function syncProducts(storeKey: string) {
});

logger.info(`Product(s) has been added/updated to relewise.`);
} else {
logger.warn(`${productsToBeSynced.length} product(s) found. Make sure you have defined product selections for the store.`);
}
}

Expand Down

0 comments on commit 38c7289

Please sign in to comment.