@labdigital/commercetools-mock 0.5.22 → 0.5.23
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 +102 -4
- 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 +102 -4
- 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 +1 -1
- package/src/projectAPI.ts +4 -2
- package/src/repositories/cart.ts +118 -1
package/dist/projectAPI.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { GetParams } from 'repositories/abstract';
|
|
1
2
|
import { AbstractStorage } from './storage';
|
|
2
3
|
import { RepositoryMap, ResourceMap, Services } from './types';
|
|
3
4
|
export declare class ProjectAPI {
|
|
@@ -6,6 +7,6 @@ export declare class ProjectAPI {
|
|
|
6
7
|
private _services;
|
|
7
8
|
constructor(projectKey: string, services: Services, storage: AbstractStorage);
|
|
8
9
|
add<ReferenceTypeId extends keyof ResourceMap>(typeId: ReferenceTypeId | 'custom-object', resource: ResourceMap[ReferenceTypeId]): void;
|
|
9
|
-
get<ReferenceTypeId extends keyof ResourceMap>(typeId: ReferenceTypeId, id: string): ResourceMap[ReferenceTypeId];
|
|
10
|
+
get<ReferenceTypeId extends keyof ResourceMap>(typeId: ReferenceTypeId, id: string, params: GetParams): ResourceMap[ReferenceTypeId];
|
|
10
11
|
getRepository<ReferenceTypeId extends keyof RepositoryMap>(typeId: ReferenceTypeId): RepositoryMap[ReferenceTypeId];
|
|
11
12
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Cart, CartAddLineItemAction, CartDraft, CartRemoveLineItemAction, CartSetBillingAddressAction, CartSetCountryAction, CartSetCustomerEmailAction, CartSetCustomFieldAction, CartSetCustomTypeAction, CartSetLocaleAction, CartSetShippingAddressAction, ReferenceTypeId } from '@commercetools/platform-sdk';
|
|
1
|
+
import { Cart, CartAddLineItemAction, CartDraft, CartRemoveLineItemAction, CartSetBillingAddressAction, CartSetCountryAction, CartSetCustomerEmailAction, CartSetCustomFieldAction, CartSetCustomTypeAction, CartSetLocaleAction, CartSetShippingAddressAction, CartSetShippingMethodAction, LineItem, LineItemDraft, ReferenceTypeId } from '@commercetools/platform-sdk';
|
|
2
2
|
import { AbstractResourceRepository } from './abstract';
|
|
3
3
|
import { Writable } from '../types';
|
|
4
4
|
export declare class CartRepository extends AbstractResourceRepository {
|
|
@@ -9,6 +9,7 @@ export declare class CartRepository extends AbstractResourceRepository {
|
|
|
9
9
|
addLineItem: (projectKey: string, resource: Writable<Cart>, { productId, variantId, sku, quantity }: CartAddLineItemAction) => void;
|
|
10
10
|
removeLineItem: (projectKey: string, resource: Writable<Cart>, { lineItemId, quantity }: CartRemoveLineItemAction) => void;
|
|
11
11
|
setBillingAddress: (projectKey: string, resource: Writable<Cart>, { address }: CartSetBillingAddressAction) => void;
|
|
12
|
+
setShippingMethod: (projectKey: string, resource: Writable<Cart>, { shippingMethod }: CartSetShippingMethodAction) => void;
|
|
12
13
|
setCountry: (projectKey: string, resource: Writable<Cart>, { country }: CartSetCountryAction) => void;
|
|
13
14
|
setCustomerEmail: (projectKey: string, resource: Writable<Cart>, { email }: CartSetCustomerEmailAction) => void;
|
|
14
15
|
setCustomField: (projectKey: string, resource: Cart, { name, value }: CartSetCustomFieldAction) => void;
|
|
@@ -16,4 +17,5 @@ export declare class CartRepository extends AbstractResourceRepository {
|
|
|
16
17
|
setLocale: (projectKey: string, resource: Writable<Cart>, { locale }: CartSetLocaleAction) => void;
|
|
17
18
|
setShippingAddress: (projectKey: string, resource: Writable<Cart>, { address }: CartSetShippingAddressAction) => void;
|
|
18
19
|
};
|
|
20
|
+
draftLineItemtoLineItem: (projectKey: string, draftLineItem: LineItemDraft) => LineItem;
|
|
19
21
|
}
|
package/package.json
CHANGED
package/src/projectAPI.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { GetParams } from 'repositories/abstract'
|
|
1
2
|
import { getBaseResourceProperties } from './helpers'
|
|
2
3
|
import { AbstractStorage } from './storage'
|
|
3
4
|
import { RepositoryMap, ResourceMap, Services } from './types'
|
|
@@ -39,13 +40,14 @@ export class ProjectAPI {
|
|
|
39
40
|
|
|
40
41
|
get<ReferenceTypeId extends keyof ResourceMap>(
|
|
41
42
|
typeId: ReferenceTypeId,
|
|
42
|
-
id: string
|
|
43
|
+
id: string,
|
|
44
|
+
params: GetParams
|
|
43
45
|
): ResourceMap[ReferenceTypeId] {
|
|
44
46
|
return this._storage.get(
|
|
45
47
|
this.projectKey,
|
|
46
48
|
typeId,
|
|
47
49
|
id,
|
|
48
|
-
|
|
50
|
+
params
|
|
49
51
|
) as ResourceMap[ReferenceTypeId]
|
|
50
52
|
}
|
|
51
53
|
|
package/src/repositories/cart.ts
CHANGED
|
@@ -10,8 +10,10 @@ import {
|
|
|
10
10
|
CartSetCustomTypeAction,
|
|
11
11
|
CartSetLocaleAction,
|
|
12
12
|
CartSetShippingAddressAction,
|
|
13
|
+
CartSetShippingMethodAction,
|
|
13
14
|
GeneralError,
|
|
14
15
|
LineItem,
|
|
16
|
+
LineItemDraft,
|
|
15
17
|
Product,
|
|
16
18
|
ProductPagedQueryResponse,
|
|
17
19
|
ProductVariant,
|
|
@@ -30,10 +32,14 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
create(projectKey: string, draft: CartDraft): Cart {
|
|
35
|
+
const lineItems = draft.lineItems?.map(draftLineItem =>
|
|
36
|
+
this.draftLineItemtoLineItem(projectKey, draftLineItem)
|
|
37
|
+
)
|
|
38
|
+
|
|
33
39
|
const resource: Cart = {
|
|
34
40
|
...getBaseResourceProperties(),
|
|
35
41
|
cartState: 'Active',
|
|
36
|
-
lineItems: [],
|
|
42
|
+
lineItems: lineItems ?? [],
|
|
37
43
|
customLineItems: [],
|
|
38
44
|
totalPrice: {
|
|
39
45
|
type: 'centPrecision',
|
|
@@ -48,6 +54,10 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
48
54
|
origin: 'Customer',
|
|
49
55
|
custom: createCustomFields(draft.custom, projectKey, this._storage),
|
|
50
56
|
}
|
|
57
|
+
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
resource.totalPrice.centAmount = calculateCartTotalPrice(resource)
|
|
60
|
+
|
|
51
61
|
this.save(projectKey, resource)
|
|
52
62
|
return resource
|
|
53
63
|
}
|
|
@@ -204,6 +214,28 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
204
214
|
) => {
|
|
205
215
|
resource.billingAddress = address
|
|
206
216
|
},
|
|
217
|
+
setShippingMethod: (
|
|
218
|
+
projectKey: string,
|
|
219
|
+
resource: Writable<Cart>,
|
|
220
|
+
{ shippingMethod }: CartSetShippingMethodAction
|
|
221
|
+
) => {
|
|
222
|
+
const resolvedType = this._storage.getByResourceIdentifier(
|
|
223
|
+
projectKey,
|
|
224
|
+
//@ts-ignore
|
|
225
|
+
shippingMethod
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
if (!resolvedType) {
|
|
229
|
+
throw new Error(`Type ${shippingMethod} not found`)
|
|
230
|
+
}
|
|
231
|
+
//@ts-ignore
|
|
232
|
+
resource.shippingInfo = {
|
|
233
|
+
shippingMethod: {
|
|
234
|
+
typeId: 'shipping-method',
|
|
235
|
+
id: resolvedType.id,
|
|
236
|
+
},
|
|
237
|
+
}
|
|
238
|
+
},
|
|
207
239
|
setCountry: (
|
|
208
240
|
projectKey: string,
|
|
209
241
|
resource: Writable<Cart>,
|
|
@@ -268,6 +300,91 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
268
300
|
resource.shippingAddress = address
|
|
269
301
|
},
|
|
270
302
|
}
|
|
303
|
+
draftLineItemtoLineItem = (
|
|
304
|
+
projectKey: string,
|
|
305
|
+
draftLineItem: LineItemDraft
|
|
306
|
+
): LineItem => {
|
|
307
|
+
const { productId, quantity } = draftLineItem
|
|
308
|
+
// @ts-ignore
|
|
309
|
+
let variantId = draftLineItem.variant.id
|
|
310
|
+
// @ts-ignore
|
|
311
|
+
let sku = draftLineItem.variant.sku
|
|
312
|
+
let product: Product | null = null
|
|
313
|
+
let variant: ProductVariant | undefined
|
|
314
|
+
|
|
315
|
+
if (productId && variantId) {
|
|
316
|
+
// Fetch product and variant by ID
|
|
317
|
+
product = this._storage.get(projectKey, 'product', productId, {})
|
|
318
|
+
} else if (sku) {
|
|
319
|
+
// Fetch product and variant by SKU
|
|
320
|
+
const items = this._storage.query(projectKey, 'product', {
|
|
321
|
+
where: [
|
|
322
|
+
`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`,
|
|
323
|
+
],
|
|
324
|
+
}) as ProductPagedQueryResponse
|
|
325
|
+
|
|
326
|
+
if (items.count === 1) {
|
|
327
|
+
product = items.results[0]
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (!product) {
|
|
332
|
+
// Check if product is found
|
|
333
|
+
throw new CommercetoolsError<GeneralError>({
|
|
334
|
+
code: 'General',
|
|
335
|
+
message: sku
|
|
336
|
+
? `A product containing a variant with SKU '${sku}' not found.`
|
|
337
|
+
: `A product with ID '${productId}' not found.`,
|
|
338
|
+
})
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Find matching variant
|
|
342
|
+
variant = [
|
|
343
|
+
product.masterData.current.masterVariant,
|
|
344
|
+
...product.masterData.current.variants,
|
|
345
|
+
].find(x => {
|
|
346
|
+
if (sku) return x.sku === sku
|
|
347
|
+
if (variantId) return x.id === variantId
|
|
348
|
+
return false
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
if (!variant) {
|
|
352
|
+
// Check if variant is found
|
|
353
|
+
throw new Error(
|
|
354
|
+
sku
|
|
355
|
+
? `A variant with SKU '${sku}' for product '${product.id}' not found.`
|
|
356
|
+
: `A variant with ID '${variantId}' for product '${product.id}' not found.`
|
|
357
|
+
)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const price = variant.prices?.[0]
|
|
361
|
+
|
|
362
|
+
const quant = quantity ?? 1
|
|
363
|
+
|
|
364
|
+
if (!price) {
|
|
365
|
+
throw new Error(`Price not set on ${productId}`)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return {
|
|
369
|
+
id: uuidv4(),
|
|
370
|
+
productId: product.id,
|
|
371
|
+
productKey: product.key,
|
|
372
|
+
name: product.masterData.current.name,
|
|
373
|
+
productSlug: product.masterData.current.slug,
|
|
374
|
+
productType: product.productType,
|
|
375
|
+
variant,
|
|
376
|
+
price: price,
|
|
377
|
+
totalPrice: {
|
|
378
|
+
...price.value,
|
|
379
|
+
centAmount: price.value.centAmount * quant,
|
|
380
|
+
},
|
|
381
|
+
quantity: quant,
|
|
382
|
+
discountedPricePerQuantity: [],
|
|
383
|
+
lineItemMode: 'Standard',
|
|
384
|
+
priceMode: 'Platform',
|
|
385
|
+
state: [],
|
|
386
|
+
}
|
|
387
|
+
}
|
|
271
388
|
}
|
|
272
389
|
|
|
273
390
|
const calculateLineItemTotalPrice = (lineItem: LineItem): number =>
|