Skip to content

Commit 7b15e19

Browse files
committed
Make sure countryCode and currencyCOde exists when updating product commerce in mcp
1 parent 5d11a35 commit 7b15e19

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/api/src/mcp/tools/product/handlers/updateProduct.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Context } from '../../../../context.js';
22
import { ProductTypes } from '@unchainedshop/core-products';
33
import {
4+
CountryNotFoundError,
5+
CurrencyNotFoundError,
46
ProductNotFoundError,
57
ProductWrongStatusError,
68
ProductWrongTypeError,
@@ -9,7 +11,7 @@ import { getNormalizedProductDetails } from '../../../utils/getNormalizedProduct
911
import { Params } from '../schemas.js';
1012

1113
export default async function updateProduct(context: Context, params: Params<'UPDATE'>) {
12-
const { modules } = context;
14+
const { modules, loaders } = context;
1315
const { productId, product } = params;
1416

1517
const existingProduct = await modules.products.findProduct({ productId });
@@ -64,6 +66,15 @@ export default async function updateProduct(context: Context, params: Params<'UP
6466
}
6567

6668
if (product.commerce !== undefined) {
69+
await Promise.all(
70+
product.commerce.pricing.map(async ({ countryCode, currencyCode }) => {
71+
const currency = await loaders.currencyLoader.load({ isoCode: currencyCode });
72+
if (!currency) throw new CurrencyNotFoundError({ currencyCode });
73+
74+
const country = await loaders.countryLoader.load({ isoCode: countryCode });
75+
if (!country) throw new CountryNotFoundError({ countryCode });
76+
}),
77+
);
6778
updateData.commerce = product.commerce;
6879
}
6980

packages/api/src/mcp/tools/product/schemas.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ export const ProductSchema = z.object({
117117
)
118118
.nonempty()
119119
.describe(
120-
"List of price configurations always use country and currency codes that are already registered in the system, don't use non existing iso codes.",
120+
"List of price configurations always use countryCode and currencyCodes that are already registered in the system, don't use non existing iso codes.",
121121
),
122122
})
123123
.optional()
124-
.describe('Commerce info - Available for ALL except CONFIGURABLE_PRODUCT'),
124+
.describe(
125+
"Commerce info - Available for ALL except CONFIGURABLE_PRODUCT. always use countryCode and currencyCodes that are already registered in the system, don't use non existing iso codes.",
126+
),
125127
});
126128

127129
export const actionValidators = {

0 commit comments

Comments
 (0)