@labdigital/commercetools-mock 0.5.21 → 0.5.22
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 +362 -302
- 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 +362 -302
- package/dist/commercetools-mock.esm.js.map +1 -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 +1 -1
- package/src/ctMock.ts +2 -0
- package/src/repositories/abstract.ts +1 -0
- 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
|
@@ -1579,16 +1579,308 @@ const calculateLineItemTotalPrice = lineItem => lineItem.price.value.centAmount
|
|
|
1579
1579
|
|
|
1580
1580
|
const calculateCartTotalPrice = cart => cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
|
|
1581
1581
|
|
|
1582
|
+
class OrderRepository extends AbstractResourceRepository {
|
|
1583
|
+
constructor() {
|
|
1584
|
+
super(...arguments);
|
|
1585
|
+
this.actions = {
|
|
1586
|
+
addPayment: (projectKey, resource, {
|
|
1587
|
+
payment
|
|
1588
|
+
}) => {
|
|
1589
|
+
const resolvedPayment = this._storage.getByResourceIdentifier(projectKey, payment);
|
|
1590
|
+
|
|
1591
|
+
if (!resolvedPayment) {
|
|
1592
|
+
throw new Error(`Payment ${payment.id} not found`);
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
if (!resource.paymentInfo) {
|
|
1596
|
+
resource.paymentInfo = {
|
|
1597
|
+
payments: []
|
|
1598
|
+
};
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
resource.paymentInfo.payments.push({
|
|
1602
|
+
typeId: 'payment',
|
|
1603
|
+
id: payment.id
|
|
1604
|
+
});
|
|
1605
|
+
},
|
|
1606
|
+
changeOrderState: (projectKey, resource, {
|
|
1607
|
+
orderState
|
|
1608
|
+
}) => {
|
|
1609
|
+
resource.orderState = orderState;
|
|
1610
|
+
},
|
|
1611
|
+
changePaymentState: (projectKey, resource, {
|
|
1612
|
+
paymentState
|
|
1613
|
+
}) => {
|
|
1614
|
+
resource.paymentState = paymentState;
|
|
1615
|
+
},
|
|
1616
|
+
transitionState: (projectKey, resource, {
|
|
1617
|
+
state
|
|
1618
|
+
}) => {
|
|
1619
|
+
const resolvedType = this._storage.getByResourceIdentifier(projectKey, state);
|
|
1620
|
+
|
|
1621
|
+
if (!resolvedType) {
|
|
1622
|
+
throw new Error(`No state found with key=${state.key} or id=${state.key}`);
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
resource.state = {
|
|
1626
|
+
typeId: 'state',
|
|
1627
|
+
id: resolvedType.id
|
|
1628
|
+
};
|
|
1629
|
+
},
|
|
1630
|
+
setBillingAddress: (projectKey, resource, {
|
|
1631
|
+
address
|
|
1632
|
+
}) => {
|
|
1633
|
+
resource.billingAddress = address;
|
|
1634
|
+
},
|
|
1635
|
+
setCustomerEmail: (projectKey, resource, {
|
|
1636
|
+
email
|
|
1637
|
+
}) => {
|
|
1638
|
+
resource.customerEmail = email;
|
|
1639
|
+
},
|
|
1640
|
+
setCustomField: (projectKey, resource, {
|
|
1641
|
+
name,
|
|
1642
|
+
value
|
|
1643
|
+
}) => {
|
|
1644
|
+
if (!resource.custom) {
|
|
1645
|
+
throw new Error('Resource has no custom field');
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
resource.custom.fields[name] = value;
|
|
1649
|
+
},
|
|
1650
|
+
setCustomType: (projectKey, resource, {
|
|
1651
|
+
type,
|
|
1652
|
+
fields
|
|
1653
|
+
}) => {
|
|
1654
|
+
if (!type) {
|
|
1655
|
+
resource.custom = undefined;
|
|
1656
|
+
} else {
|
|
1657
|
+
const resolvedType = this._storage.getByResourceIdentifier(projectKey, type);
|
|
1658
|
+
|
|
1659
|
+
if (!resolvedType) {
|
|
1660
|
+
throw new Error(`Type ${type} not found`);
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
resource.custom = {
|
|
1664
|
+
type: {
|
|
1665
|
+
typeId: 'type',
|
|
1666
|
+
id: resolvedType.id
|
|
1667
|
+
},
|
|
1668
|
+
fields: fields || []
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
},
|
|
1672
|
+
setLocale: (projectKey, resource, {
|
|
1673
|
+
locale
|
|
1674
|
+
}) => {
|
|
1675
|
+
resource.locale = locale;
|
|
1676
|
+
},
|
|
1677
|
+
setOrderNumber: (projectKey, resource, {
|
|
1678
|
+
orderNumber
|
|
1679
|
+
}) => {
|
|
1680
|
+
resource.orderNumber = orderNumber;
|
|
1681
|
+
},
|
|
1682
|
+
setShippingAddress: (projectKey, resource, {
|
|
1683
|
+
address
|
|
1684
|
+
}) => {
|
|
1685
|
+
resource.shippingAddress = address;
|
|
1686
|
+
},
|
|
1687
|
+
setStore: (projectKey, resource, {
|
|
1688
|
+
store
|
|
1689
|
+
}) => {
|
|
1690
|
+
if (!store) return;
|
|
1691
|
+
|
|
1692
|
+
const resolvedType = this._storage.getByResourceIdentifier(projectKey, store);
|
|
1693
|
+
|
|
1694
|
+
if (!resolvedType) {
|
|
1695
|
+
throw new Error(`No store found with key=${store.key}`);
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
const storeReference = resolvedType;
|
|
1699
|
+
resource.store = {
|
|
1700
|
+
typeId: 'store',
|
|
1701
|
+
key: storeReference.key
|
|
1702
|
+
};
|
|
1703
|
+
}
|
|
1704
|
+
};
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
getTypeId() {
|
|
1708
|
+
return 'order';
|
|
1709
|
+
}
|
|
1710
|
+
|
|
1711
|
+
create(projectKey, draft) {
|
|
1712
|
+
assert(draft.cart, 'draft.cart is missing');
|
|
1713
|
+
|
|
1714
|
+
const cart = this._storage.getByResourceIdentifier(projectKey, draft.cart);
|
|
1715
|
+
|
|
1716
|
+
if (!cart) {
|
|
1717
|
+
throw new Error('Cannot find cart');
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
const resource = { ...getBaseResourceProperties(),
|
|
1721
|
+
orderNumber: draft.orderNumber,
|
|
1722
|
+
orderState: 'Open',
|
|
1723
|
+
lineItems: [],
|
|
1724
|
+
customLineItems: [],
|
|
1725
|
+
totalPrice: cart.totalPrice,
|
|
1726
|
+
refusedGifts: [],
|
|
1727
|
+
origin: 'Customer',
|
|
1728
|
+
syncInfo: [],
|
|
1729
|
+
lastMessageSequenceNumber: 0
|
|
1730
|
+
};
|
|
1731
|
+
this.save(projectKey, resource);
|
|
1732
|
+
return resource;
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
import(projectKey, draft) {
|
|
1736
|
+
var _draft$lineItems, _draft$customLineItem;
|
|
1737
|
+
|
|
1738
|
+
// TODO: Check if order with given orderNumber already exists
|
|
1739
|
+
assert(this, 'OrderRepository not valid');
|
|
1740
|
+
const resource = { ...getBaseResourceProperties(),
|
|
1741
|
+
billingAddress: draft.billingAddress,
|
|
1742
|
+
shippingAddress: draft.shippingAddress,
|
|
1743
|
+
custom: createCustomFields(draft.custom, projectKey, this._storage),
|
|
1744
|
+
customerEmail: draft.customerEmail,
|
|
1745
|
+
lastMessageSequenceNumber: 0,
|
|
1746
|
+
orderNumber: draft.orderNumber,
|
|
1747
|
+
orderState: draft.orderState || 'Open',
|
|
1748
|
+
origin: draft.origin || 'Customer',
|
|
1749
|
+
paymentState: draft.paymentState,
|
|
1750
|
+
refusedGifts: [],
|
|
1751
|
+
store: resolveStoreReference(draft.store, projectKey, this._storage),
|
|
1752
|
+
syncInfo: [],
|
|
1753
|
+
lineItems: ((_draft$lineItems = draft.lineItems) == null ? void 0 : _draft$lineItems.map(item => this.lineItemFromImportDraft.bind(this)(projectKey, item))) || [],
|
|
1754
|
+
customLineItems: ((_draft$customLineItem = draft.customLineItems) == null ? void 0 : _draft$customLineItem.map(item => this.customLineItemFromImportDraft.bind(this)(projectKey, item))) || [],
|
|
1755
|
+
totalPrice: {
|
|
1756
|
+
type: 'centPrecision',
|
|
1757
|
+
...draft.totalPrice,
|
|
1758
|
+
fractionDigits: 2
|
|
1759
|
+
}
|
|
1760
|
+
};
|
|
1761
|
+
this.save(projectKey, resource);
|
|
1762
|
+
return resource;
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
lineItemFromImportDraft(projectKey, draft) {
|
|
1766
|
+
let product;
|
|
1767
|
+
let variant;
|
|
1768
|
+
|
|
1769
|
+
if (draft.variant.sku) {
|
|
1770
|
+
variant = {
|
|
1771
|
+
id: 0,
|
|
1772
|
+
sku: draft.variant.sku
|
|
1773
|
+
};
|
|
1774
|
+
|
|
1775
|
+
var items = this._storage.query(projectKey, 'product', {
|
|
1776
|
+
where: [`masterData(current(masterVariant(sku="${draft.variant.sku}"))) or masterData(current(variants(sku="${draft.variant.sku}")))`]
|
|
1777
|
+
});
|
|
1778
|
+
|
|
1779
|
+
if (items.count !== 1) {
|
|
1780
|
+
throw new CommercetoolsError({
|
|
1781
|
+
code: 'General',
|
|
1782
|
+
message: `A product containing a variant with SKU '${draft.variant.sku}' not found.`
|
|
1783
|
+
});
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
product = items.results[0];
|
|
1787
|
+
|
|
1788
|
+
if (product.masterData.current.masterVariant.sku === draft.variant.sku) {
|
|
1789
|
+
variant = product.masterData.current.masterVariant;
|
|
1790
|
+
} else {
|
|
1791
|
+
variant = product.masterData.current.variants.find(v => v.sku === draft.variant.sku);
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
if (!variant) {
|
|
1795
|
+
throw new Error('Internal state error');
|
|
1796
|
+
}
|
|
1797
|
+
} else {
|
|
1798
|
+
throw new Error('No product found');
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
const lineItem = { ...getBaseResourceProperties(),
|
|
1802
|
+
custom: createCustomFields(draft.custom, projectKey, this._storage),
|
|
1803
|
+
discountedPricePerQuantity: [],
|
|
1804
|
+
lineItemMode: 'Standard',
|
|
1805
|
+
name: draft.name,
|
|
1806
|
+
price: createPrice(draft.price),
|
|
1807
|
+
priceMode: 'Platform',
|
|
1808
|
+
productId: product.id,
|
|
1809
|
+
productType: product.productType,
|
|
1810
|
+
quantity: draft.quantity,
|
|
1811
|
+
state: draft.state || [],
|
|
1812
|
+
taxRate: draft.taxRate,
|
|
1813
|
+
totalPrice: createTypedMoney(draft.price.value),
|
|
1814
|
+
variant: {
|
|
1815
|
+
id: variant.id,
|
|
1816
|
+
sku: variant.sku,
|
|
1817
|
+
price: createPrice(draft.price)
|
|
1818
|
+
}
|
|
1819
|
+
};
|
|
1820
|
+
return lineItem;
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1823
|
+
customLineItemFromImportDraft(projectKey, draft) {
|
|
1824
|
+
const lineItem = { ...getBaseResourceProperties(),
|
|
1825
|
+
custom: createCustomFields(draft.custom, projectKey, this._storage),
|
|
1826
|
+
discountedPricePerQuantity: [],
|
|
1827
|
+
money: createTypedMoney(draft.money),
|
|
1828
|
+
name: draft.name,
|
|
1829
|
+
quantity: draft.quantity,
|
|
1830
|
+
slug: draft.slug,
|
|
1831
|
+
state: [],
|
|
1832
|
+
totalPrice: createTypedMoney(draft.money)
|
|
1833
|
+
};
|
|
1834
|
+
return lineItem;
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1837
|
+
getWithOrderNumber(projectKey, orderNumber, params = {}) {
|
|
1838
|
+
const result = this._storage.query(projectKey, this.getTypeId(), { ...params,
|
|
1839
|
+
where: [`orderNumber="${orderNumber}"`]
|
|
1840
|
+
});
|
|
1841
|
+
|
|
1842
|
+
if (result.count === 1) {
|
|
1843
|
+
return result.results[0];
|
|
1844
|
+
} // Catch this for now, should be checked when creating/updating
|
|
1845
|
+
|
|
1846
|
+
|
|
1847
|
+
if (result.count > 1) {
|
|
1848
|
+
throw new Error('Duplicate order numbers');
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
return;
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1582
1856
|
class CartService extends AbstractService {
|
|
1583
1857
|
constructor(parent, storage) {
|
|
1584
1858
|
super(parent);
|
|
1585
1859
|
this.repository = new CartRepository(storage);
|
|
1860
|
+
this.orderRepository = new OrderRepository(storage);
|
|
1586
1861
|
}
|
|
1587
1862
|
|
|
1588
1863
|
getBasePath() {
|
|
1589
1864
|
return 'carts';
|
|
1590
1865
|
}
|
|
1591
1866
|
|
|
1867
|
+
extraRoutes(parent) {
|
|
1868
|
+
parent.post('/replicate', (request, response) => {
|
|
1869
|
+
// @ts-ignore
|
|
1870
|
+
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);
|
|
1871
|
+
|
|
1872
|
+
if (!cartOrOrder) {
|
|
1873
|
+
return response.status(400).send();
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
const newCart = this.repository.create(request.params.projectKey, { ...cartOrOrder,
|
|
1877
|
+
currency: cartOrOrder.totalPrice.currencyCode,
|
|
1878
|
+
discountCodes: []
|
|
1879
|
+
});
|
|
1880
|
+
return response.status(200).send(newCart);
|
|
1881
|
+
});
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1592
1884
|
}
|
|
1593
1885
|
|
|
1594
1886
|
class CategoryRepository extends AbstractResourceRepository {
|
|
@@ -2257,155 +2549,13 @@ class PaymentRepository extends AbstractResourceRepository {
|
|
|
2257
2549
|
constructor() {
|
|
2258
2550
|
super(...arguments);
|
|
2259
2551
|
|
|
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
|
-
}
|
|
2552
|
+
this.transactionFromTransactionDraft = (draft, projectKey) => ({ ...draft,
|
|
2553
|
+
id: v4(),
|
|
2554
|
+
amount: createTypedMoney(draft.amount),
|
|
2555
|
+
custom: createCustomFields(draft.custom, projectKey, this._storage)
|
|
2556
|
+
});
|
|
2383
2557
|
|
|
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
|
-
},
|
|
2558
|
+
this.actions = {
|
|
2409
2559
|
setCustomField: (projectKey, resource, {
|
|
2410
2560
|
name,
|
|
2411
2561
|
value
|
|
@@ -2438,186 +2588,68 @@ class OrderRepository extends AbstractResourceRepository {
|
|
|
2438
2588
|
};
|
|
2439
2589
|
}
|
|
2440
2590
|
},
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
}) => {
|
|
2444
|
-
resource.locale = locale;
|
|
2445
|
-
},
|
|
2446
|
-
setOrderNumber: (projectKey, resource, {
|
|
2447
|
-
orderNumber
|
|
2591
|
+
addTransaction: (projectKey, resource, {
|
|
2592
|
+
transaction
|
|
2448
2593
|
}) => {
|
|
2449
|
-
resource.
|
|
2594
|
+
resource.transactions = [...resource.transactions, this.transactionFromTransactionDraft(transaction, projectKey)];
|
|
2450
2595
|
},
|
|
2451
|
-
|
|
2452
|
-
|
|
2596
|
+
changeTransactionState: (_projectKey, resource, {
|
|
2597
|
+
transactionId,
|
|
2598
|
+
state
|
|
2453
2599
|
}) => {
|
|
2454
|
-
resource.
|
|
2600
|
+
const index = resource.transactions.findIndex(e => e.id === transactionId);
|
|
2601
|
+
const updatedTransaction = { ...resource.transactions[index],
|
|
2602
|
+
state
|
|
2603
|
+
};
|
|
2604
|
+
resource.transactions[index] = updatedTransaction;
|
|
2455
2605
|
},
|
|
2456
|
-
|
|
2457
|
-
|
|
2606
|
+
transitionState: (projectKey, resource, {
|
|
2607
|
+
state
|
|
2458
2608
|
}) => {
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
const resolvedType = this._storage.getByResourceIdentifier(projectKey, store);
|
|
2609
|
+
const stateObj = this._storage.getByResourceIdentifier(projectKey, state);
|
|
2462
2610
|
|
|
2463
|
-
if (!
|
|
2464
|
-
throw new Error(`
|
|
2611
|
+
if (!stateObj) {
|
|
2612
|
+
throw new Error(`State ${state} not found`);
|
|
2465
2613
|
}
|
|
2466
2614
|
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2615
|
+
resource.paymentStatus.state = {
|
|
2616
|
+
typeId: 'state',
|
|
2617
|
+
id: stateObj.id,
|
|
2618
|
+
obj: stateObj
|
|
2471
2619
|
};
|
|
2472
2620
|
}
|
|
2473
2621
|
};
|
|
2474
2622
|
}
|
|
2475
2623
|
|
|
2476
2624
|
getTypeId() {
|
|
2477
|
-
return '
|
|
2625
|
+
return 'payment';
|
|
2478
2626
|
}
|
|
2479
2627
|
|
|
2480
2628
|
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
2629
|
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
|
-
}
|
|
2630
|
+
amountPlanned: createTypedMoney(draft.amountPlanned),
|
|
2631
|
+
paymentMethodInfo: draft.paymentMethodInfo,
|
|
2632
|
+
paymentStatus: draft.paymentStatus ? { ...draft.paymentStatus,
|
|
2633
|
+
state: draft.paymentStatus.state ? getReferenceFromResourceIdentifier(draft.paymentStatus.state, projectKey, this._storage) : undefined
|
|
2634
|
+
} : {},
|
|
2635
|
+
transactions: (draft.transactions || []).map(t => this.transactionFromTransactionDraft(t, projectKey)),
|
|
2636
|
+
interfaceInteractions: (draft.interfaceInteractions || []).map(interaction => createCustomFields(interaction, projectKey, this._storage)),
|
|
2637
|
+
custom: createCustomFields(draft.custom, projectKey, this._storage)
|
|
2529
2638
|
};
|
|
2530
2639
|
this.save(projectKey, resource);
|
|
2531
2640
|
return resource;
|
|
2532
2641
|
}
|
|
2533
2642
|
|
|
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
|
-
}
|
|
2643
|
+
}
|
|
2591
2644
|
|
|
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;
|
|
2645
|
+
class MyPaymentService extends AbstractService {
|
|
2646
|
+
constructor(parent, storage) {
|
|
2647
|
+
super(parent);
|
|
2648
|
+
this.repository = new PaymentRepository(storage);
|
|
2604
2649
|
}
|
|
2605
2650
|
|
|
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;
|
|
2651
|
+
getBasePath() {
|
|
2652
|
+
return 'me/payments';
|
|
2621
2653
|
}
|
|
2622
2654
|
|
|
2623
2655
|
}
|
|
@@ -3986,6 +4018,33 @@ class MyCustomerService extends AbstractService {
|
|
|
3986
4018
|
|
|
3987
4019
|
}
|
|
3988
4020
|
|
|
4021
|
+
class MyOrderService extends AbstractService {
|
|
4022
|
+
constructor(parent, storage) {
|
|
4023
|
+
super(parent);
|
|
4024
|
+
this.repository = new OrderRepository(storage);
|
|
4025
|
+
}
|
|
4026
|
+
|
|
4027
|
+
getBasePath() {
|
|
4028
|
+
return 'me';
|
|
4029
|
+
}
|
|
4030
|
+
|
|
4031
|
+
registerRoutes(parent) {
|
|
4032
|
+
// Overwrite this function to be able to handle /me/active-cart path.
|
|
4033
|
+
const basePath = this.getBasePath();
|
|
4034
|
+
const router = Router({
|
|
4035
|
+
mergeParams: true
|
|
4036
|
+
});
|
|
4037
|
+
this.extraRoutes(router);
|
|
4038
|
+
router.get('/orders/', this.get.bind(this));
|
|
4039
|
+
router.get('/orders/:id', this.getWithId.bind(this));
|
|
4040
|
+
router.delete('/orders/:id', this.deletewithId.bind(this));
|
|
4041
|
+
router.post('/orders/', this.post.bind(this));
|
|
4042
|
+
router.post('/orders/:id', this.postWithId.bind(this));
|
|
4043
|
+
parent.use(`/${basePath}`, router);
|
|
4044
|
+
}
|
|
4045
|
+
|
|
4046
|
+
}
|
|
4047
|
+
|
|
3989
4048
|
const DEFAULT_OPTIONS = {
|
|
3990
4049
|
enableAuthentication: false,
|
|
3991
4050
|
validateCredentials: false,
|
|
@@ -4084,6 +4143,7 @@ class CommercetoolsMock {
|
|
|
4084
4143
|
order: new OrderService(projectRouter, this._storage),
|
|
4085
4144
|
payment: new PaymentService(projectRouter, this._storage),
|
|
4086
4145
|
'my-cart': new MyCartService(projectRouter, this._storage),
|
|
4146
|
+
'my-order': new MyOrderService(projectRouter, this._storage),
|
|
4087
4147
|
'my-customer': new MyCustomerService(projectRouter, this._storage),
|
|
4088
4148
|
'my-payment': new MyPaymentService(projectRouter, this._storage),
|
|
4089
4149
|
'shipping-method': new ShippingMethodService(projectRouter, this._storage),
|