@labdigital/commercetools-mock 0.5.21 → 0.6.0
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 +465 -307
- 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 +465 -307
- 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/dist/repositories/order.d.ts +2 -1
- package/dist/services/cart.d.ts +3 -0
- package/dist/services/my-order.d.ts +10 -0
- package/dist/types.d.ts +1 -1
- package/package.json +5 -2
- package/src/ctMock.ts +2 -0
- package/src/projectAPI.ts +4 -2
- package/src/repositories/abstract.ts +1 -0
- package/src/repositories/cart.ts +118 -1
- package/src/repositories/customer.ts +3 -1
- package/src/repositories/helpers.ts +0 -1
- package/src/repositories/order.ts +22 -0
- package/src/services/cart.ts +32 -0
- package/src/services/my-order.ts +35 -0
- package/src/types.ts +1 -0
|
@@ -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
|
|
|
@@ -1479,6 +1479,24 @@ class CartRepository extends AbstractResourceRepository {
|
|
|
1479
1479
|
}) => {
|
|
1480
1480
|
resource.billingAddress = address;
|
|
1481
1481
|
},
|
|
1482
|
+
setShippingMethod: (projectKey, resource, {
|
|
1483
|
+
shippingMethod
|
|
1484
|
+
}) => {
|
|
1485
|
+
const resolvedType = this._storage.getByResourceIdentifier(projectKey, //@ts-ignore
|
|
1486
|
+
shippingMethod);
|
|
1487
|
+
|
|
1488
|
+
if (!resolvedType) {
|
|
1489
|
+
throw new Error(`Type ${shippingMethod} not found`);
|
|
1490
|
+
} //@ts-ignore
|
|
1491
|
+
|
|
1492
|
+
|
|
1493
|
+
resource.shippingInfo = {
|
|
1494
|
+
shippingMethod: {
|
|
1495
|
+
typeId: 'shipping-method',
|
|
1496
|
+
id: resolvedType.id
|
|
1497
|
+
}
|
|
1498
|
+
};
|
|
1499
|
+
},
|
|
1482
1500
|
setCountry: (projectKey, resource, {
|
|
1483
1501
|
country
|
|
1484
1502
|
}) => {
|
|
@@ -1532,6 +1550,81 @@ class CartRepository extends AbstractResourceRepository {
|
|
|
1532
1550
|
resource.shippingAddress = address;
|
|
1533
1551
|
}
|
|
1534
1552
|
};
|
|
1553
|
+
|
|
1554
|
+
this.draftLineItemtoLineItem = (projectKey, draftLineItem) => {
|
|
1555
|
+
var _variant$prices2;
|
|
1556
|
+
|
|
1557
|
+
const {
|
|
1558
|
+
productId,
|
|
1559
|
+
quantity
|
|
1560
|
+
} = draftLineItem; // @ts-ignore
|
|
1561
|
+
|
|
1562
|
+
let variantId = draftLineItem.variant.id; // @ts-ignore
|
|
1563
|
+
|
|
1564
|
+
let sku = draftLineItem.variant.sku;
|
|
1565
|
+
let product = null;
|
|
1566
|
+
let variant;
|
|
1567
|
+
|
|
1568
|
+
if (productId && variantId) {
|
|
1569
|
+
// Fetch product and variant by ID
|
|
1570
|
+
product = this._storage.get(projectKey, 'product', productId, {});
|
|
1571
|
+
} else if (sku) {
|
|
1572
|
+
// Fetch product and variant by SKU
|
|
1573
|
+
const items = this._storage.query(projectKey, 'product', {
|
|
1574
|
+
where: [`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`]
|
|
1575
|
+
});
|
|
1576
|
+
|
|
1577
|
+
if (items.count === 1) {
|
|
1578
|
+
product = items.results[0];
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
if (!product) {
|
|
1583
|
+
// Check if product is found
|
|
1584
|
+
throw new CommercetoolsError({
|
|
1585
|
+
code: 'General',
|
|
1586
|
+
message: sku ? `A product containing a variant with SKU '${sku}' not found.` : `A product with ID '${productId}' not found.`
|
|
1587
|
+
});
|
|
1588
|
+
} // Find matching variant
|
|
1589
|
+
|
|
1590
|
+
|
|
1591
|
+
variant = [product.masterData.current.masterVariant, ...product.masterData.current.variants].find(x => {
|
|
1592
|
+
if (sku) return x.sku === sku;
|
|
1593
|
+
if (variantId) return x.id === variantId;
|
|
1594
|
+
return false;
|
|
1595
|
+
});
|
|
1596
|
+
|
|
1597
|
+
if (!variant) {
|
|
1598
|
+
// Check if variant is found
|
|
1599
|
+
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.`);
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
const price = (_variant$prices2 = variant.prices) == null ? void 0 : _variant$prices2[0];
|
|
1603
|
+
const quant = quantity != null ? quantity : 1;
|
|
1604
|
+
|
|
1605
|
+
if (!price) {
|
|
1606
|
+
throw new Error(`Price not set on ${productId}`);
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
return {
|
|
1610
|
+
id: v4(),
|
|
1611
|
+
productId: product.id,
|
|
1612
|
+
productKey: product.key,
|
|
1613
|
+
name: product.masterData.current.name,
|
|
1614
|
+
productSlug: product.masterData.current.slug,
|
|
1615
|
+
productType: product.productType,
|
|
1616
|
+
variant,
|
|
1617
|
+
price: price,
|
|
1618
|
+
totalPrice: { ...price.value,
|
|
1619
|
+
centAmount: price.value.centAmount * quant
|
|
1620
|
+
},
|
|
1621
|
+
quantity: quant,
|
|
1622
|
+
discountedPricePerQuantity: [],
|
|
1623
|
+
lineItemMode: 'Standard',
|
|
1624
|
+
priceMode: 'Platform',
|
|
1625
|
+
state: []
|
|
1626
|
+
};
|
|
1627
|
+
};
|
|
1535
1628
|
}
|
|
1536
1629
|
|
|
1537
1630
|
getTypeId() {
|
|
@@ -1539,9 +1632,12 @@ class CartRepository extends AbstractResourceRepository {
|
|
|
1539
1632
|
}
|
|
1540
1633
|
|
|
1541
1634
|
create(projectKey, draft) {
|
|
1635
|
+
var _draft$lineItems;
|
|
1636
|
+
|
|
1637
|
+
const lineItems = (_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(draftLineItem => this.draftLineItemtoLineItem(projectKey, draftLineItem));
|
|
1542
1638
|
const resource = { ...getBaseResourceProperties(),
|
|
1543
1639
|
cartState: 'Active',
|
|
1544
|
-
lineItems: [],
|
|
1640
|
+
lineItems: lineItems != null ? lineItems : [],
|
|
1545
1641
|
customLineItems: [],
|
|
1546
1642
|
totalPrice: {
|
|
1547
1643
|
type: 'centPrecision',
|
|
@@ -1555,7 +1651,9 @@ class CartRepository extends AbstractResourceRepository {
|
|
|
1555
1651
|
refusedGifts: [],
|
|
1556
1652
|
origin: 'Customer',
|
|
1557
1653
|
custom: createCustomFields(draft.custom, projectKey, this._storage)
|
|
1558
|
-
};
|
|
1654
|
+
}; // @ts-ignore
|
|
1655
|
+
|
|
1656
|
+
resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
|
|
1559
1657
|
this.save(projectKey, resource);
|
|
1560
1658
|
return resource;
|
|
1561
1659
|
}
|
|
@@ -1579,16 +1677,308 @@ const calculateLineItemTotalPrice = lineItem => lineItem.price.value.centAmount
|
|
|
1579
1677
|
|
|
1580
1678
|
const calculateCartTotalPrice = cart => cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
|
|
1581
1679
|
|
|
1680
|
+
class OrderRepository extends AbstractResourceRepository {
|
|
1681
|
+
constructor() {
|
|
1682
|
+
super(...arguments);
|
|
1683
|
+
this.actions = {
|
|
1684
|
+
addPayment: (projectKey, resource, {
|
|
1685
|
+
payment
|
|
1686
|
+
}) => {
|
|
1687
|
+
const resolvedPayment = this._storage.getByResourceIdentifier(projectKey, payment);
|
|
1688
|
+
|
|
1689
|
+
if (!resolvedPayment) {
|
|
1690
|
+
throw new Error(`Payment ${payment.id} not found`);
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
if (!resource.paymentInfo) {
|
|
1694
|
+
resource.paymentInfo = {
|
|
1695
|
+
payments: []
|
|
1696
|
+
};
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
resource.paymentInfo.payments.push({
|
|
1700
|
+
typeId: 'payment',
|
|
1701
|
+
id: payment.id
|
|
1702
|
+
});
|
|
1703
|
+
},
|
|
1704
|
+
changeOrderState: (projectKey, resource, {
|
|
1705
|
+
orderState
|
|
1706
|
+
}) => {
|
|
1707
|
+
resource.orderState = orderState;
|
|
1708
|
+
},
|
|
1709
|
+
changePaymentState: (projectKey, resource, {
|
|
1710
|
+
paymentState
|
|
1711
|
+
}) => {
|
|
1712
|
+
resource.paymentState = paymentState;
|
|
1713
|
+
},
|
|
1714
|
+
transitionState: (projectKey, resource, {
|
|
1715
|
+
state
|
|
1716
|
+
}) => {
|
|
1717
|
+
const resolvedType = this._storage.getByResourceIdentifier(projectKey, state);
|
|
1718
|
+
|
|
1719
|
+
if (!resolvedType) {
|
|
1720
|
+
throw new Error(`No state found with key=${state.key} or id=${state.key}`);
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
resource.state = {
|
|
1724
|
+
typeId: 'state',
|
|
1725
|
+
id: resolvedType.id
|
|
1726
|
+
};
|
|
1727
|
+
},
|
|
1728
|
+
setBillingAddress: (projectKey, resource, {
|
|
1729
|
+
address
|
|
1730
|
+
}) => {
|
|
1731
|
+
resource.billingAddress = address;
|
|
1732
|
+
},
|
|
1733
|
+
setCustomerEmail: (projectKey, resource, {
|
|
1734
|
+
email
|
|
1735
|
+
}) => {
|
|
1736
|
+
resource.customerEmail = email;
|
|
1737
|
+
},
|
|
1738
|
+
setCustomField: (projectKey, resource, {
|
|
1739
|
+
name,
|
|
1740
|
+
value
|
|
1741
|
+
}) => {
|
|
1742
|
+
if (!resource.custom) {
|
|
1743
|
+
throw new Error('Resource has no custom field');
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
resource.custom.fields[name] = value;
|
|
1747
|
+
},
|
|
1748
|
+
setCustomType: (projectKey, resource, {
|
|
1749
|
+
type,
|
|
1750
|
+
fields
|
|
1751
|
+
}) => {
|
|
1752
|
+
if (!type) {
|
|
1753
|
+
resource.custom = undefined;
|
|
1754
|
+
} else {
|
|
1755
|
+
const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
|
|
1756
|
+
|
|
1757
|
+
if (!resolvedType) {
|
|
1758
|
+
throw new Error(`Type ${type} not found`);
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
resource.custom = {
|
|
1762
|
+
type: {
|
|
1763
|
+
typeId: 'type',
|
|
1764
|
+
id: resolvedType.id
|
|
1765
|
+
},
|
|
1766
|
+
fields: fields || []
|
|
1767
|
+
};
|
|
1768
|
+
}
|
|
1769
|
+
},
|
|
1770
|
+
setLocale: (projectKey, resource, {
|
|
1771
|
+
locale
|
|
1772
|
+
}) => {
|
|
1773
|
+
resource.locale = locale;
|
|
1774
|
+
},
|
|
1775
|
+
setOrderNumber: (projectKey, resource, {
|
|
1776
|
+
orderNumber
|
|
1777
|
+
}) => {
|
|
1778
|
+
resource.orderNumber = orderNumber;
|
|
1779
|
+
},
|
|
1780
|
+
setShippingAddress: (projectKey, resource, {
|
|
1781
|
+
address
|
|
1782
|
+
}) => {
|
|
1783
|
+
resource.shippingAddress = address;
|
|
1784
|
+
},
|
|
1785
|
+
setStore: (projectKey, resource, {
|
|
1786
|
+
store
|
|
1787
|
+
}) => {
|
|
1788
|
+
if (!store) return;
|
|
1789
|
+
|
|
1790
|
+
const resolvedType = this._storage.getByResourceIdentifier(projectKey, store);
|
|
1791
|
+
|
|
1792
|
+
if (!resolvedType) {
|
|
1793
|
+
throw new Error(`No store found with key=${store.key}`);
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
const storeReference = resolvedType;
|
|
1797
|
+
resource.store = {
|
|
1798
|
+
typeId: 'store',
|
|
1799
|
+
key: storeReference.key
|
|
1800
|
+
};
|
|
1801
|
+
}
|
|
1802
|
+
};
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
getTypeId() {
|
|
1806
|
+
return 'order';
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
create(projectKey, draft) {
|
|
1810
|
+
assert(draft.cart, 'draft.cart is missing');
|
|
1811
|
+
|
|
1812
|
+
const cart = this._storage.getByResourceIdentifier(projectKey, draft.cart);
|
|
1813
|
+
|
|
1814
|
+
if (!cart) {
|
|
1815
|
+
throw new Error('Cannot find cart');
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
const resource = { ...getBaseResourceProperties(),
|
|
1819
|
+
orderNumber: draft.orderNumber,
|
|
1820
|
+
orderState: 'Open',
|
|
1821
|
+
lineItems: [],
|
|
1822
|
+
customLineItems: [],
|
|
1823
|
+
totalPrice: cart.totalPrice,
|
|
1824
|
+
refusedGifts: [],
|
|
1825
|
+
origin: 'Customer',
|
|
1826
|
+
syncInfo: [],
|
|
1827
|
+
lastMessageSequenceNumber: 0
|
|
1828
|
+
};
|
|
1829
|
+
this.save(projectKey, resource);
|
|
1830
|
+
return resource;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
import(projectKey, draft) {
|
|
1834
|
+
var _draft$lineItems, _draft$customLineItem;
|
|
1835
|
+
|
|
1836
|
+
// TODO: Check if order with given orderNumber already exists
|
|
1837
|
+
assert(this, 'OrderRepository not valid');
|
|
1838
|
+
const resource = { ...getBaseResourceProperties(),
|
|
1839
|
+
billingAddress: draft.billingAddress,
|
|
1840
|
+
shippingAddress: draft.shippingAddress,
|
|
1841
|
+
custom: createCustomFields(draft.custom, projectKey, this._storage),
|
|
1842
|
+
customerEmail: draft.customerEmail,
|
|
1843
|
+
lastMessageSequenceNumber: 0,
|
|
1844
|
+
orderNumber: draft.orderNumber,
|
|
1845
|
+
orderState: draft.orderState || 'Open',
|
|
1846
|
+
origin: draft.origin || 'Customer',
|
|
1847
|
+
paymentState: draft.paymentState,
|
|
1848
|
+
refusedGifts: [],
|
|
1849
|
+
store: resolveStoreReference(draft.store, projectKey, this._storage),
|
|
1850
|
+
syncInfo: [],
|
|
1851
|
+
lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(projectKey, item))) || [],
|
|
1852
|
+
customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(projectKey, item))) || [],
|
|
1853
|
+
totalPrice: {
|
|
1854
|
+
type: 'centPrecision',
|
|
1855
|
+
...draft.totalPrice,
|
|
1856
|
+
fractionDigits: 2
|
|
1857
|
+
}
|
|
1858
|
+
};
|
|
1859
|
+
this.save(projectKey, resource);
|
|
1860
|
+
return resource;
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
lineItemFromImportDraft(projectKey, draft) {
|
|
1864
|
+
let product;
|
|
1865
|
+
let variant;
|
|
1866
|
+
|
|
1867
|
+
if (draft.variant.sku) {
|
|
1868
|
+
variant = {
|
|
1869
|
+
id: 0,
|
|
1870
|
+
sku: draft.variant.sku
|
|
1871
|
+
};
|
|
1872
|
+
|
|
1873
|
+
var items = this._storage.query(projectKey, 'product', {
|
|
1874
|
+
where: [`masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`]
|
|
1875
|
+
});
|
|
1876
|
+
|
|
1877
|
+
if (items.count !== 1) {
|
|
1878
|
+
throw new CommercetoolsError({
|
|
1879
|
+
code: 'General',
|
|
1880
|
+
message: `A product containing a variant with SKU '${draft.variant.sku}' not found.`
|
|
1881
|
+
});
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
product = items.results[0];
|
|
1885
|
+
|
|
1886
|
+
if (product.masterData.current.masterVariant.sku === draft.variant.sku) {
|
|
1887
|
+
variant = product.masterData.current.masterVariant;
|
|
1888
|
+
} else {
|
|
1889
|
+
variant = product.masterData.current.variants.find(v => v.sku === draft.variant.sku);
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1892
|
+
if (!variant) {
|
|
1893
|
+
throw new Error('Internal state error');
|
|
1894
|
+
}
|
|
1895
|
+
} else {
|
|
1896
|
+
throw new Error('No product found');
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
const lineItem = { ...getBaseResourceProperties(),
|
|
1900
|
+
custom: createCustomFields(draft.custom, projectKey, this._storage),
|
|
1901
|
+
discountedPricePerQuantity: [],
|
|
1902
|
+
lineItemMode: 'Standard',
|
|
1903
|
+
name: draft.name,
|
|
1904
|
+
price: createPrice(draft.price),
|
|
1905
|
+
priceMode: 'Platform',
|
|
1906
|
+
productId: product.id,
|
|
1907
|
+
productType: product.productType,
|
|
1908
|
+
quantity: draft.quantity,
|
|
1909
|
+
state: draft.state || [],
|
|
1910
|
+
taxRate: draft.taxRate,
|
|
1911
|
+
totalPrice: createTypedMoney(draft.price.value),
|
|
1912
|
+
variant: {
|
|
1913
|
+
id: variant.id,
|
|
1914
|
+
sku: variant.sku,
|
|
1915
|
+
price: createPrice(draft.price)
|
|
1916
|
+
}
|
|
1917
|
+
};
|
|
1918
|
+
return lineItem;
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
customLineItemFromImportDraft(projectKey, draft) {
|
|
1922
|
+
const lineItem = { ...getBaseResourceProperties(),
|
|
1923
|
+
custom: createCustomFields(draft.custom, projectKey, this._storage),
|
|
1924
|
+
discountedPricePerQuantity: [],
|
|
1925
|
+
money: createTypedMoney(draft.money),
|
|
1926
|
+
name: draft.name,
|
|
1927
|
+
quantity: draft.quantity,
|
|
1928
|
+
slug: draft.slug,
|
|
1929
|
+
state: [],
|
|
1930
|
+
totalPrice: createTypedMoney(draft.money)
|
|
1931
|
+
};
|
|
1932
|
+
return lineItem;
|
|
1933
|
+
}
|
|
1934
|
+
|
|
1935
|
+
getWithOrderNumber(projectKey, orderNumber, params = {}) {
|
|
1936
|
+
const result = this._storage.query(projectKey, this.getTypeId(), { ...params,
|
|
1937
|
+
where: [`orderNumber="${orderNumber}"`]
|
|
1938
|
+
});
|
|
1939
|
+
|
|
1940
|
+
if (result.count === 1) {
|
|
1941
|
+
return result.results[0];
|
|
1942
|
+
} // Catch this for now, should be checked when creating/updating
|
|
1943
|
+
|
|
1944
|
+
|
|
1945
|
+
if (result.count > 1) {
|
|
1946
|
+
throw new Error('Duplicate order numbers');
|
|
1947
|
+
}
|
|
1948
|
+
|
|
1949
|
+
return;
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1582
1954
|
class CartService extends AbstractService {
|
|
1583
1955
|
constructor(parent, storage) {
|
|
1584
1956
|
super(parent);
|
|
1585
1957
|
this.repository = new CartRepository(storage);
|
|
1958
|
+
this.orderRepository = new OrderRepository(storage);
|
|
1586
1959
|
}
|
|
1587
1960
|
|
|
1588
1961
|
getBasePath() {
|
|
1589
1962
|
return 'carts';
|
|
1590
1963
|
}
|
|
1591
1964
|
|
|
1965
|
+
extraRoutes(parent) {
|
|
1966
|
+
parent.post('/replicate', (request, response) => {
|
|
1967
|
+
// @ts-ignore
|
|
1968
|
+
const cartOrOrder = request.body.reference.typeId === 'order' ? this.orderRepository.get(request.params.projectKey, request.body.reference.id) : this.repository.get(request.params.projectKey, request.body.reference.id);
|
|
1969
|
+
|
|
1970
|
+
if (!cartOrOrder) {
|
|
1971
|
+
return response.status(400).send();
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
const newCart = this.repository.create(request.params.projectKey, { ...cartOrOrder,
|
|
1975
|
+
currency: cartOrOrder.totalPrice.currencyCode,
|
|
1976
|
+
discountCodes: []
|
|
1977
|
+
});
|
|
1978
|
+
return response.status(200).send(newCart);
|
|
1979
|
+
});
|
|
1980
|
+
}
|
|
1981
|
+
|
|
1592
1982
|
}
|
|
1593
1983
|
|
|
1594
1984
|
class CategoryRepository extends AbstractResourceRepository {
|
|
@@ -1818,7 +2208,7 @@ class CustomerRepository extends AbstractResourceRepository {
|
|
|
1818
2208
|
create(projectKey, draft) {
|
|
1819
2209
|
const resource = { ...getBaseResourceProperties(),
|
|
1820
2210
|
email: draft.email,
|
|
1821
|
-
password: Buffer.from(draft.password).toString('base64'),
|
|
2211
|
+
password: draft.password ? Buffer.from(draft.password).toString('base64') : undefined,
|
|
1822
2212
|
isEmailVerified: draft.isEmailVerified || false,
|
|
1823
2213
|
addresses: []
|
|
1824
2214
|
};
|
|
@@ -2257,155 +2647,13 @@ class PaymentRepository extends AbstractResourceRepository {
|
|
|
2257
2647
|
constructor() {
|
|
2258
2648
|
super(...arguments);
|
|
2259
2649
|
|
|
2260
|
-
this.transactionFromTransactionDraft = (draft, projectKey) => ({ ...draft,
|
|
2261
|
-
id: v4(),
|
|
2262
|
-
amount: createTypedMoney(draft.amount),
|
|
2263
|
-
custom: createCustomFields(draft.custom, projectKey, this._storage)
|
|
2264
|
-
});
|
|
2265
|
-
|
|
2266
|
-
this.actions = {
|
|
2267
|
-
setCustomField: (projectKey, resource, {
|
|
2268
|
-
name,
|
|
2269
|
-
value
|
|
2270
|
-
}) => {
|
|
2271
|
-
if (!resource.custom) {
|
|
2272
|
-
throw new Error('Resource has no custom field');
|
|
2273
|
-
}
|
|
2274
|
-
|
|
2275
|
-
resource.custom.fields[name] = value;
|
|
2276
|
-
},
|
|
2277
|
-
setCustomType: (projectKey, resource, {
|
|
2278
|
-
type,
|
|
2279
|
-
fields
|
|
2280
|
-
}) => {
|
|
2281
|
-
if (!type) {
|
|
2282
|
-
resource.custom = undefined;
|
|
2283
|
-
} else {
|
|
2284
|
-
const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
|
|
2285
|
-
|
|
2286
|
-
if (!resolvedType) {
|
|
2287
|
-
throw new Error(`Type ${type} not found`);
|
|
2288
|
-
}
|
|
2289
|
-
|
|
2290
|
-
resource.custom = {
|
|
2291
|
-
type: {
|
|
2292
|
-
typeId: 'type',
|
|
2293
|
-
id: resolvedType.id
|
|
2294
|
-
},
|
|
2295
|
-
fields: fields || []
|
|
2296
|
-
};
|
|
2297
|
-
}
|
|
2298
|
-
},
|
|
2299
|
-
addTransaction: (projectKey, resource, {
|
|
2300
|
-
transaction
|
|
2301
|
-
}) => {
|
|
2302
|
-
resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
|
|
2303
|
-
},
|
|
2304
|
-
changeTransactionState: (_projectKey, resource, {
|
|
2305
|
-
transactionId,
|
|
2306
|
-
state
|
|
2307
|
-
}) => {
|
|
2308
|
-
const index = resource.transactions.findIndex(e => e.id === transactionId);
|
|
2309
|
-
const updatedTransaction = { ...resource.transactions[index],
|
|
2310
|
-
state
|
|
2311
|
-
};
|
|
2312
|
-
resource.transactions[index] = updatedTransaction;
|
|
2313
|
-
},
|
|
2314
|
-
transitionState: (projectKey, resource, {
|
|
2315
|
-
state
|
|
2316
|
-
}) => {
|
|
2317
|
-
const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
|
|
2318
|
-
|
|
2319
|
-
if (!stateObj) {
|
|
2320
|
-
throw new Error(`State ${state} not found`);
|
|
2321
|
-
}
|
|
2322
|
-
|
|
2323
|
-
resource.paymentStatus.state = {
|
|
2324
|
-
typeId: 'state',
|
|
2325
|
-
id: stateObj.id,
|
|
2326
|
-
obj: stateObj
|
|
2327
|
-
};
|
|
2328
|
-
}
|
|
2329
|
-
};
|
|
2330
|
-
}
|
|
2331
|
-
|
|
2332
|
-
getTypeId() {
|
|
2333
|
-
return 'payment';
|
|
2334
|
-
}
|
|
2335
|
-
|
|
2336
|
-
create(projectKey, draft) {
|
|
2337
|
-
const resource = { ...getBaseResourceProperties(),
|
|
2338
|
-
amountPlanned: createTypedMoney(draft.amountPlanned),
|
|
2339
|
-
paymentMethodInfo: draft.paymentMethodInfo,
|
|
2340
|
-
paymentStatus: draft.paymentStatus ? { ...draft.paymentStatus,
|
|
2341
|
-
state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, projectKey, this._storage) : undefined
|
|
2342
|
-
} : {},
|
|
2343
|
-
transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, projectKey)),
|
|
2344
|
-
interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, projectKey, this._storage)),
|
|
2345
|
-
custom: createCustomFields(draft.custom, projectKey, this._storage)
|
|
2346
|
-
};
|
|
2347
|
-
this.save(projectKey, resource);
|
|
2348
|
-
return resource;
|
|
2349
|
-
}
|
|
2350
|
-
|
|
2351
|
-
}
|
|
2352
|
-
|
|
2353
|
-
class MyPaymentService extends AbstractService {
|
|
2354
|
-
constructor(parent, storage) {
|
|
2355
|
-
super(parent);
|
|
2356
|
-
this.repository = new PaymentRepository(storage);
|
|
2357
|
-
}
|
|
2358
|
-
|
|
2359
|
-
getBasePath() {
|
|
2360
|
-
return 'me/payments';
|
|
2361
|
-
}
|
|
2362
|
-
|
|
2363
|
-
}
|
|
2364
|
-
|
|
2365
|
-
class OrderRepository extends AbstractResourceRepository {
|
|
2366
|
-
constructor() {
|
|
2367
|
-
super(...arguments);
|
|
2368
|
-
this.actions = {
|
|
2369
|
-
addPayment: (projectKey, resource, {
|
|
2370
|
-
payment
|
|
2371
|
-
}) => {
|
|
2372
|
-
const resolvedPayment = this._storage.getByResourceIdentifier(projectKey, payment);
|
|
2373
|
-
|
|
2374
|
-
if (!resolvedPayment) {
|
|
2375
|
-
throw new Error(`Payment ${payment.id} not found`);
|
|
2376
|
-
}
|
|
2377
|
-
|
|
2378
|
-
if (!resource.paymentInfo) {
|
|
2379
|
-
resource.paymentInfo = {
|
|
2380
|
-
payments: []
|
|
2381
|
-
};
|
|
2382
|
-
}
|
|
2650
|
+
this.transactionFromTransactionDraft = (draft, projectKey) => ({ ...draft,
|
|
2651
|
+
id: v4(),
|
|
2652
|
+
amount: createTypedMoney(draft.amount),
|
|
2653
|
+
custom: createCustomFields(draft.custom, projectKey, this._storage)
|
|
2654
|
+
});
|
|
2383
2655
|
|
|
2384
|
-
|
|
2385
|
-
typeId: 'payment',
|
|
2386
|
-
id: payment.id
|
|
2387
|
-
});
|
|
2388
|
-
},
|
|
2389
|
-
changeOrderState: (projectKey, resource, {
|
|
2390
|
-
orderState
|
|
2391
|
-
}) => {
|
|
2392
|
-
resource.orderState = orderState;
|
|
2393
|
-
},
|
|
2394
|
-
changePaymentState: (projectKey, resource, {
|
|
2395
|
-
paymentState
|
|
2396
|
-
}) => {
|
|
2397
|
-
resource.paymentState = paymentState;
|
|
2398
|
-
},
|
|
2399
|
-
setBillingAddress: (projectKey, resource, {
|
|
2400
|
-
address
|
|
2401
|
-
}) => {
|
|
2402
|
-
resource.billingAddress = address;
|
|
2403
|
-
},
|
|
2404
|
-
setCustomerEmail: (projectKey, resource, {
|
|
2405
|
-
email
|
|
2406
|
-
}) => {
|
|
2407
|
-
resource.customerEmail = email;
|
|
2408
|
-
},
|
|
2656
|
+
this.actions = {
|
|
2409
2657
|
setCustomField: (projectKey, resource, {
|
|
2410
2658
|
name,
|
|
2411
2659
|
value
|
|
@@ -2438,186 +2686,68 @@ class OrderRepository extends AbstractResourceRepository {
|
|
|
2438
2686
|
};
|
|
2439
2687
|
}
|
|
2440
2688
|
},
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
}) => {
|
|
2444
|
-
resource.locale = locale;
|
|
2445
|
-
},
|
|
2446
|
-
setOrderNumber: (projectKey, resource, {
|
|
2447
|
-
orderNumber
|
|
2689
|
+
addTransaction: (projectKey, resource, {
|
|
2690
|
+
transaction
|
|
2448
2691
|
}) => {
|
|
2449
|
-
resource.
|
|
2692
|
+
resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
|
|
2450
2693
|
},
|
|
2451
|
-
|
|
2452
|
-
|
|
2694
|
+
changeTransactionState: (_projectKey, resource, {
|
|
2695
|
+
transactionId,
|
|
2696
|
+
state
|
|
2453
2697
|
}) => {
|
|
2454
|
-
resource.
|
|
2698
|
+
const index = resource.transactions.findIndex(e => e.id === transactionId);
|
|
2699
|
+
const updatedTransaction = { ...resource.transactions[index],
|
|
2700
|
+
state
|
|
2701
|
+
};
|
|
2702
|
+
resource.transactions[index] = updatedTransaction;
|
|
2455
2703
|
},
|
|
2456
|
-
|
|
2457
|
-
|
|
2704
|
+
transitionState: (projectKey, resource, {
|
|
2705
|
+
state
|
|
2458
2706
|
}) => {
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
const resolvedType = this._storage.getByResourceIdentifier(projectKey, store);
|
|
2707
|
+
const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
|
|
2462
2708
|
|
|
2463
|
-
if (!
|
|
2464
|
-
throw new Error(`
|
|
2709
|
+
if (!stateObj) {
|
|
2710
|
+
throw new Error(`State ${state} not found`);
|
|
2465
2711
|
}
|
|
2466
2712
|
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2713
|
+
resource.paymentStatus.state = {
|
|
2714
|
+
typeId: 'state',
|
|
2715
|
+
id: stateObj.id,
|
|
2716
|
+
obj: stateObj
|
|
2471
2717
|
};
|
|
2472
2718
|
}
|
|
2473
2719
|
};
|
|
2474
2720
|
}
|
|
2475
2721
|
|
|
2476
2722
|
getTypeId() {
|
|
2477
|
-
return '
|
|
2723
|
+
return 'payment';
|
|
2478
2724
|
}
|
|
2479
2725
|
|
|
2480
2726
|
create(projectKey, draft) {
|
|
2481
|
-
assert(draft.cart, 'draft.cart is missing');
|
|
2482
|
-
|
|
2483
|
-
const cart = this._storage.getByResourceIdentifier(projectKey, draft.cart);
|
|
2484
|
-
|
|
2485
|
-
if (!cart) {
|
|
2486
|
-
throw new Error('Cannot find cart');
|
|
2487
|
-
}
|
|
2488
|
-
|
|
2489
|
-
const resource = { ...getBaseResourceProperties(),
|
|
2490
|
-
orderNumber: draft.orderNumber,
|
|
2491
|
-
orderState: 'Open',
|
|
2492
|
-
lineItems: [],
|
|
2493
|
-
customLineItems: [],
|
|
2494
|
-
totalPrice: cart.totalPrice,
|
|
2495
|
-
refusedGifts: [],
|
|
2496
|
-
origin: 'Customer',
|
|
2497
|
-
syncInfo: [],
|
|
2498
|
-
lastMessageSequenceNumber: 0
|
|
2499
|
-
};
|
|
2500
|
-
this.save(projectKey, resource);
|
|
2501
|
-
return resource;
|
|
2502
|
-
}
|
|
2503
|
-
|
|
2504
|
-
import(projectKey, draft) {
|
|
2505
|
-
var _draft$lineItems, _draft$customLineItem;
|
|
2506
|
-
|
|
2507
|
-
// TODO: Check if order with given orderNumber already exists
|
|
2508
|
-
assert(this, 'OrderRepository not valid');
|
|
2509
2727
|
const resource = { ...getBaseResourceProperties(),
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
paymentState: draft.paymentState,
|
|
2519
|
-
refusedGifts: [],
|
|
2520
|
-
store: resolveStoreReference(draft.store, projectKey, this._storage),
|
|
2521
|
-
syncInfo: [],
|
|
2522
|
-
lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(projectKey, item))) || [],
|
|
2523
|
-
customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(projectKey, item))) || [],
|
|
2524
|
-
totalPrice: {
|
|
2525
|
-
type: 'centPrecision',
|
|
2526
|
-
...draft.totalPrice,
|
|
2527
|
-
fractionDigits: 2
|
|
2528
|
-
}
|
|
2728
|
+
amountPlanned: createTypedMoney(draft.amountPlanned),
|
|
2729
|
+
paymentMethodInfo: draft.paymentMethodInfo,
|
|
2730
|
+
paymentStatus: draft.paymentStatus ? { ...draft.paymentStatus,
|
|
2731
|
+
state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, projectKey, this._storage) : undefined
|
|
2732
|
+
} : {},
|
|
2733
|
+
transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, projectKey)),
|
|
2734
|
+
interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, projectKey, this._storage)),
|
|
2735
|
+
custom: createCustomFields(draft.custom, projectKey, this._storage)
|
|
2529
2736
|
};
|
|
2530
2737
|
this.save(projectKey, resource);
|
|
2531
2738
|
return resource;
|
|
2532
2739
|
}
|
|
2533
2740
|
|
|
2534
|
-
|
|
2535
|
-
let product;
|
|
2536
|
-
let variant;
|
|
2537
|
-
|
|
2538
|
-
if (draft.variant.sku) {
|
|
2539
|
-
variant = {
|
|
2540
|
-
id: 0,
|
|
2541
|
-
sku: draft.variant.sku
|
|
2542
|
-
};
|
|
2543
|
-
|
|
2544
|
-
var items = this._storage.query(projectKey, 'product', {
|
|
2545
|
-
where: [`masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`]
|
|
2546
|
-
});
|
|
2547
|
-
|
|
2548
|
-
if (items.count !== 1) {
|
|
2549
|
-
throw new CommercetoolsError({
|
|
2550
|
-
code: 'General',
|
|
2551
|
-
message: `A product containing a variant with SKU '${draft.variant.sku}' not found.`
|
|
2552
|
-
});
|
|
2553
|
-
}
|
|
2554
|
-
|
|
2555
|
-
product = items.results[0];
|
|
2556
|
-
|
|
2557
|
-
if (product.masterData.current.masterVariant.sku === draft.variant.sku) {
|
|
2558
|
-
variant = product.masterData.current.masterVariant;
|
|
2559
|
-
} else {
|
|
2560
|
-
variant = product.masterData.current.variants.find(v => v.sku === draft.variant.sku);
|
|
2561
|
-
}
|
|
2562
|
-
|
|
2563
|
-
if (!variant) {
|
|
2564
|
-
throw new Error('Internal state error');
|
|
2565
|
-
}
|
|
2566
|
-
} else {
|
|
2567
|
-
throw new Error('No product found');
|
|
2568
|
-
}
|
|
2569
|
-
|
|
2570
|
-
const lineItem = { ...getBaseResourceProperties(),
|
|
2571
|
-
custom: createCustomFields(draft.custom, projectKey, this._storage),
|
|
2572
|
-
discountedPricePerQuantity: [],
|
|
2573
|
-
lineItemMode: 'Standard',
|
|
2574
|
-
name: draft.name,
|
|
2575
|
-
price: createPrice(draft.price),
|
|
2576
|
-
priceMode: 'Platform',
|
|
2577
|
-
productId: product.id,
|
|
2578
|
-
productType: product.productType,
|
|
2579
|
-
quantity: draft.quantity,
|
|
2580
|
-
state: draft.state || [],
|
|
2581
|
-
taxRate: draft.taxRate,
|
|
2582
|
-
totalPrice: createTypedMoney(draft.price.value),
|
|
2583
|
-
variant: {
|
|
2584
|
-
id: variant.id,
|
|
2585
|
-
sku: variant.sku,
|
|
2586
|
-
price: createPrice(draft.price)
|
|
2587
|
-
}
|
|
2588
|
-
};
|
|
2589
|
-
return lineItem;
|
|
2590
|
-
}
|
|
2741
|
+
}
|
|
2591
2742
|
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
money: createTypedMoney(draft.money),
|
|
2597
|
-
name: draft.name,
|
|
2598
|
-
quantity: draft.quantity,
|
|
2599
|
-
slug: draft.slug,
|
|
2600
|
-
state: [],
|
|
2601
|
-
totalPrice: createTypedMoney(draft.money)
|
|
2602
|
-
};
|
|
2603
|
-
return lineItem;
|
|
2743
|
+
class MyPaymentService extends AbstractService {
|
|
2744
|
+
constructor(parent, storage) {
|
|
2745
|
+
super(parent);
|
|
2746
|
+
this.repository = new PaymentRepository(storage);
|
|
2604
2747
|
}
|
|
2605
2748
|
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
where: [`orderNumber="${orderNumber}"`]
|
|
2609
|
-
});
|
|
2610
|
-
|
|
2611
|
-
if (result.count === 1) {
|
|
2612
|
-
return result.results[0];
|
|
2613
|
-
} // Catch this for now, should be checked when creating/updating
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
if (result.count > 1) {
|
|
2617
|
-
throw new Error('Duplicate order numbers');
|
|
2618
|
-
}
|
|
2619
|
-
|
|
2620
|
-
return;
|
|
2749
|
+
getBasePath() {
|
|
2750
|
+
return 'me/payments';
|
|
2621
2751
|
}
|
|
2622
2752
|
|
|
2623
2753
|
}
|
|
@@ -3986,6 +4116,33 @@ class MyCustomerService extends AbstractService {
|
|
|
3986
4116
|
|
|
3987
4117
|
}
|
|
3988
4118
|
|
|
4119
|
+
class MyOrderService extends AbstractService {
|
|
4120
|
+
constructor(parent, storage) {
|
|
4121
|
+
super(parent);
|
|
4122
|
+
this.repository = new OrderRepository(storage);
|
|
4123
|
+
}
|
|
4124
|
+
|
|
4125
|
+
getBasePath() {
|
|
4126
|
+
return 'me';
|
|
4127
|
+
}
|
|
4128
|
+
|
|
4129
|
+
registerRoutes(parent) {
|
|
4130
|
+
// Overwrite this function to be able to handle /me/active-cart path.
|
|
4131
|
+
const basePath = this.getBasePath();
|
|
4132
|
+
const router = Router({
|
|
4133
|
+
mergeParams: true
|
|
4134
|
+
});
|
|
4135
|
+
this.extraRoutes(router);
|
|
4136
|
+
router.get('/orders/', this.get.bind(this));
|
|
4137
|
+
router.get('/orders/:id', this.getWithId.bind(this));
|
|
4138
|
+
router.delete('/orders/:id', this.deletewithId.bind(this));
|
|
4139
|
+
router.post('/orders/', this.post.bind(this));
|
|
4140
|
+
router.post('/orders/:id', this.postWithId.bind(this));
|
|
4141
|
+
parent.use(`/${basePath}`, router);
|
|
4142
|
+
}
|
|
4143
|
+
|
|
4144
|
+
}
|
|
4145
|
+
|
|
3989
4146
|
const DEFAULT_OPTIONS = {
|
|
3990
4147
|
enableAuthentication: false,
|
|
3991
4148
|
validateCredentials: false,
|
|
@@ -4084,6 +4241,7 @@ class CommercetoolsMock {
|
|
|
4084
4241
|
order: new OrderService(projectRouter, this._storage),
|
|
4085
4242
|
payment: new PaymentService(projectRouter, this._storage),
|
|
4086
4243
|
'my-cart': new MyCartService(projectRouter, this._storage),
|
|
4244
|
+
'my-order': new MyOrderService(projectRouter, this._storage),
|
|
4087
4245
|
'my-customer': new MyCustomerService(projectRouter, this._storage),
|
|
4088
4246
|
'my-payment': new MyPaymentService(projectRouter, this._storage),
|
|
4089
4247
|
'shipping-method': new ShippingMethodService(projectRouter, this._storage),
|