@labdigital/commercetools-mock 0.5.22 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commercetools-mock.cjs.development.js +149 -13
- package/dist/commercetools-mock.cjs.development.js.map +1 -1
- package/dist/commercetools-mock.cjs.production.min.js +1 -1
- package/dist/commercetools-mock.cjs.production.min.js.map +1 -1
- package/dist/commercetools-mock.esm.js +149 -13
- package/dist/commercetools-mock.esm.js.map +1 -1
- package/dist/projectAPI.d.ts +2 -1
- package/dist/repositories/cart.d.ts +3 -1
- package/package.json +5 -2
- package/src/projectAPI.ts +4 -2
- package/src/repositories/cart.ts +166 -6
- package/src/repositories/customer.ts +3 -1
- package/src/services/cart.test.ts +48 -8
- package/src/services/cart.ts +15 -3
|
@@ -919,8 +919,8 @@ class ProjectAPI {
|
|
|
919
919
|
}
|
|
920
920
|
}
|
|
921
921
|
|
|
922
|
-
get(typeId, id) {
|
|
923
|
-
return this._storage.get(this.projectKey, typeId, id,
|
|
922
|
+
get(typeId, id, params) {
|
|
923
|
+
return this._storage.get(this.projectKey, typeId, id, params);
|
|
924
924
|
} // TODO: Not sure if we want to expose this...
|
|
925
925
|
|
|
926
926
|
|
|
@@ -1416,7 +1416,17 @@ class CartRepository extends AbstractResourceRepository {
|
|
|
1416
1416
|
});
|
|
1417
1417
|
}
|
|
1418
1418
|
|
|
1419
|
-
const
|
|
1419
|
+
const currency = resource.totalPrice.currencyCode;
|
|
1420
|
+
const price = selectPrice({
|
|
1421
|
+
prices: variant.prices,
|
|
1422
|
+
currency,
|
|
1423
|
+
country: resource.country
|
|
1424
|
+
});
|
|
1425
|
+
|
|
1426
|
+
if (!price) {
|
|
1427
|
+
throw new Error(`No valid price found for ${productId} for country ${resource.country} and currency ${currency}`);
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1420
1430
|
resource.lineItems.push({
|
|
1421
1431
|
id: v4(),
|
|
1422
1432
|
productId: product.id,
|
|
@@ -1479,6 +1489,24 @@ class CartRepository extends AbstractResourceRepository {
|
|
|
1479
1489
|
}) => {
|
|
1480
1490
|
resource.billingAddress = address;
|
|
1481
1491
|
},
|
|
1492
|
+
setShippingMethod: (projectKey, resource, {
|
|
1493
|
+
shippingMethod
|
|
1494
|
+
}) => {
|
|
1495
|
+
const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
|
|
1496
|
+
shippingMethod);
|
|
1497
|
+
|
|
1498
|
+
if (!resolvedType) {
|
|
1499
|
+
throw new Error(`Type ${shippingMethod} not found`);
|
|
1500
|
+
} //@ts-ignore
|
|
1501
|
+
|
|
1502
|
+
|
|
1503
|
+
resource.shippingInfo = {
|
|
1504
|
+
shippingMethod: {
|
|
1505
|
+
typeId: 'shipping-method',
|
|
1506
|
+
id: resolvedType.id
|
|
1507
|
+
}
|
|
1508
|
+
};
|
|
1509
|
+
},
|
|
1482
1510
|
setCountry: (projectKey, resource, {
|
|
1483
1511
|
country
|
|
1484
1512
|
}) => {
|
|
@@ -1532,6 +1560,81 @@ class CartRepository extends AbstractResourceRepository {
|
|
|
1532
1560
|
resource.shippingAddress = address;
|
|
1533
1561
|
}
|
|
1534
1562
|
};
|
|
1563
|
+
|
|
1564
|
+
this.draftLineItemtoLineItem = (projectKey, draftLineItem, currency, country) => {
|
|
1565
|
+
const {
|
|
1566
|
+
productId,
|
|
1567
|
+
quantity,
|
|
1568
|
+
variantId,
|
|
1569
|
+
sku
|
|
1570
|
+
} = draftLineItem;
|
|
1571
|
+
let product = null;
|
|
1572
|
+
let variant;
|
|
1573
|
+
|
|
1574
|
+
if (productId && variantId) {
|
|
1575
|
+
// Fetch product and variant by ID
|
|
1576
|
+
product = this._storage.get(projectKey, 'product', productId, {});
|
|
1577
|
+
} else if (sku) {
|
|
1578
|
+
// Fetch product and variant by SKU
|
|
1579
|
+
const items = this._storage.query(projectKey, 'product', {
|
|
1580
|
+
where: [`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`]
|
|
1581
|
+
});
|
|
1582
|
+
|
|
1583
|
+
if (items.count === 1) {
|
|
1584
|
+
product = items.results[0];
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
if (!product) {
|
|
1589
|
+
// Check if product is found
|
|
1590
|
+
throw new CommercetoolsError({
|
|
1591
|
+
code: 'General',
|
|
1592
|
+
message: sku ? `A product containing a variant with SKU '${sku}' not found.` : `A product with ID '${productId}' not found.`
|
|
1593
|
+
});
|
|
1594
|
+
} // Find matching variant
|
|
1595
|
+
|
|
1596
|
+
|
|
1597
|
+
variant = [product.masterData.current.masterVariant, ...product.masterData.current.variants].find(x => {
|
|
1598
|
+
if (sku) return x.sku === sku;
|
|
1599
|
+
if (variantId) return x.id === variantId;
|
|
1600
|
+
return false;
|
|
1601
|
+
});
|
|
1602
|
+
|
|
1603
|
+
if (!variant) {
|
|
1604
|
+
// Check if variant is found
|
|
1605
|
+
throw new Error(sku ? `A variant with SKU '${sku}' for product '${product.id}' not found.` : `A variant with ID '${variantId}' for product '${product.id}' not found.`);
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
const quant = quantity != null ? quantity : 1;
|
|
1609
|
+
const price = selectPrice({
|
|
1610
|
+
prices: variant.prices,
|
|
1611
|
+
currency,
|
|
1612
|
+
country
|
|
1613
|
+
});
|
|
1614
|
+
|
|
1615
|
+
if (!price) {
|
|
1616
|
+
throw new Error(`No valid price found for ${productId} for country ${country} and currency ${currency}`);
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
return {
|
|
1620
|
+
id: v4(),
|
|
1621
|
+
productId: product.id,
|
|
1622
|
+
productKey: product.key,
|
|
1623
|
+
name: product.masterData.current.name,
|
|
1624
|
+
productSlug: product.masterData.current.slug,
|
|
1625
|
+
productType: product.productType,
|
|
1626
|
+
variant,
|
|
1627
|
+
price: price,
|
|
1628
|
+
totalPrice: { ...price.value,
|
|
1629
|
+
centAmount: price.value.centAmount * quant
|
|
1630
|
+
},
|
|
1631
|
+
quantity: quant,
|
|
1632
|
+
discountedPricePerQuantity: [],
|
|
1633
|
+
lineItemMode: 'Standard',
|
|
1634
|
+
priceMode: 'Platform',
|
|
1635
|
+
state: []
|
|
1636
|
+
};
|
|
1637
|
+
};
|
|
1535
1638
|
}
|
|
1536
1639
|
|
|
1537
1640
|
getTypeId() {
|
|
@@ -1539,9 +1642,12 @@ class CartRepository extends AbstractResourceRepository {
|
|
|
1539
1642
|
}
|
|
1540
1643
|
|
|
1541
1644
|
create(projectKey, draft) {
|
|
1645
|
+
var _draft$lineItems$map, _draft$lineItems, _draft$taxMode, _draft$taxRoundingMod, _draft$taxCalculation, _draft$origin;
|
|
1646
|
+
|
|
1647
|
+
const lineItems = (_draft$lineItems$map = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem, draft.currency, draft.country))) != null ? _draft$lineItems$map : [];
|
|
1542
1648
|
const resource = { ...getBaseResourceProperties(),
|
|
1543
1649
|
cartState: 'Active',
|
|
1544
|
-
lineItems
|
|
1650
|
+
lineItems,
|
|
1545
1651
|
customLineItems: [],
|
|
1546
1652
|
totalPrice: {
|
|
1547
1653
|
type: 'centPrecision',
|
|
@@ -1549,13 +1655,17 @@ class CartRepository extends AbstractResourceRepository {
|
|
|
1549
1655
|
currencyCode: draft.currency,
|
|
1550
1656
|
fractionDigits: 0
|
|
1551
1657
|
},
|
|
1552
|
-
taxMode: 'Platform',
|
|
1553
|
-
taxRoundingMode: 'HalfEven',
|
|
1554
|
-
taxCalculationMode: 'LineItemLevel',
|
|
1658
|
+
taxMode: (_draft$taxMode = draft.taxMode) != null ? _draft$taxMode : 'Platform',
|
|
1659
|
+
taxRoundingMode: (_draft$taxRoundingMod = draft.taxRoundingMode) != null ? _draft$taxRoundingMod : 'HalfEven',
|
|
1660
|
+
taxCalculationMode: (_draft$taxCalculation = draft.taxCalculationMode) != null ? _draft$taxCalculation : 'LineItemLevel',
|
|
1555
1661
|
refusedGifts: [],
|
|
1556
|
-
|
|
1662
|
+
locale: draft.locale,
|
|
1663
|
+
country: draft.country,
|
|
1664
|
+
origin: (_draft$origin = draft.origin) != null ? _draft$origin : 'Customer',
|
|
1557
1665
|
custom: createCustomFields(draft.custom, projectKey, this._storage)
|
|
1558
|
-
};
|
|
1666
|
+
}; // @ts-ignore
|
|
1667
|
+
|
|
1668
|
+
resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
|
|
1559
1669
|
this.save(projectKey, resource);
|
|
1560
1670
|
return resource;
|
|
1561
1671
|
}
|
|
@@ -1575,6 +1685,25 @@ class CartRepository extends AbstractResourceRepository {
|
|
|
1575
1685
|
|
|
1576
1686
|
}
|
|
1577
1687
|
|
|
1688
|
+
const selectPrice = ({
|
|
1689
|
+
prices,
|
|
1690
|
+
currency,
|
|
1691
|
+
country
|
|
1692
|
+
}) => {
|
|
1693
|
+
if (!prices) {
|
|
1694
|
+
return undefined;
|
|
1695
|
+
} // Quick-and-dirty way of selecting price based on the given currency and country.
|
|
1696
|
+
// Can be improved later to give more priority to exact matches over
|
|
1697
|
+
// 'all country' matches, and include customer groups in the mix as well
|
|
1698
|
+
|
|
1699
|
+
|
|
1700
|
+
return prices.find(price => {
|
|
1701
|
+
const countryMatch = !price.country || price.country === country;
|
|
1702
|
+
const currencyMatch = price.value.currencyCode === currency;
|
|
1703
|
+
return countryMatch && currencyMatch;
|
|
1704
|
+
});
|
|
1705
|
+
};
|
|
1706
|
+
|
|
1578
1707
|
const calculateLineItemTotalPrice = lineItem => lineItem.price.value.centAmount * lineItem.quantity;
|
|
1579
1708
|
|
|
1580
1709
|
const calculateCartTotalPrice = cart => cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
|
|
@@ -1873,10 +2002,17 @@ class CartService extends AbstractService {
|
|
|
1873
2002
|
return response.status(400).send();
|
|
1874
2003
|
}
|
|
1875
2004
|
|
|
1876
|
-
const
|
|
2005
|
+
const cartDraft = { ...cartOrOrder,
|
|
1877
2006
|
currency: cartOrOrder.totalPrice.currencyCode,
|
|
1878
|
-
discountCodes: []
|
|
1879
|
-
|
|
2007
|
+
discountCodes: [],
|
|
2008
|
+
lineItems: cartOrOrder.lineItems.map(lineItem => {
|
|
2009
|
+
return { ...lineItem,
|
|
2010
|
+
variantId: lineItem.variant.id,
|
|
2011
|
+
sku: lineItem.variant.sku
|
|
2012
|
+
};
|
|
2013
|
+
})
|
|
2014
|
+
};
|
|
2015
|
+
const newCart = this.repository.create(request.params.projectKey, cartDraft);
|
|
1880
2016
|
return response.status(200).send(newCart);
|
|
1881
2017
|
});
|
|
1882
2018
|
}
|
|
@@ -2110,7 +2246,7 @@ class CustomerRepository extends AbstractResourceRepository {
|
|
|
2110
2246
|
create(projectKey, draft) {
|
|
2111
2247
|
const resource = { ...getBaseResourceProperties(),
|
|
2112
2248
|
email: draft.email,
|
|
2113
|
-
password: Buffer.from(draft.password).toString('base64'),
|
|
2249
|
+
password: draft.password ? Buffer.from(draft.password).toString('base64') : undefined,
|
|
2114
2250
|
isEmailVerified: draft.isEmailVerified || false,
|
|
2115
2251
|
addresses: []
|
|
2116
2252
|
};
|