diff options
Diffstat (limited to 'src/frontend/services')
| -rw-r--r-- | src/frontend/services/ProductCatalog.service.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/frontend/services/ProductCatalog.service.ts b/src/frontend/services/ProductCatalog.service.ts new file mode 100644 index 0000000..9854531 --- /dev/null +++ b/src/frontend/services/ProductCatalog.service.ts @@ -0,0 +1,40 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +import ProductCatalogGateway from '../gateways/rpc/ProductCatalog.gateway'; +import CurrencyGateway from '../gateways/rpc/Currency.gateway'; +import { Money } from '../protos/demo'; + +const defaultCurrencyCode = 'USD'; + +const ProductCatalogService = () => ({ + async getProductPrice(price: Money, currencyCode: string) { + return !!currencyCode && currencyCode !== defaultCurrencyCode + ? await CurrencyGateway.convert(price, currencyCode) + : price; + }, + async listProducts(currencyCode = 'USD') { + const { products: productList } = await ProductCatalogGateway.listProducts(); + + return Promise.all( + productList.map(async product => { + const priceUsd = await this.getProductPrice(product.priceUsd!, currencyCode); + + return { + ...product, + priceUsd, + }; + }) + ); + }, + async getProduct(id: string, currencyCode = 'USD') { + const product = await ProductCatalogGateway.getProduct(id); + + return { + ...product, + priceUsd: await this.getProductPrice(product.priceUsd!, currencyCode), + }; + }, +}); + +export default ProductCatalogService(); |
