@labdigital/commercetools-mock 0.6.0 → 0.6.3
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 +423 -352
- 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 +423 -352
- package/dist/commercetools-mock.esm.js.map +1 -1
- package/dist/repositories/abstract.d.ts +13 -9
- package/dist/repositories/cart-discount.d.ts +3 -3
- package/dist/repositories/cart.d.ts +13 -13
- package/dist/repositories/category.d.ts +11 -11
- package/dist/repositories/channel.d.ts +2 -2
- package/dist/repositories/custom-object.d.ts +3 -3
- package/dist/repositories/customer-group.d.ts +4 -4
- package/dist/repositories/customer.d.ts +4 -4
- package/dist/repositories/discount-code.d.ts +3 -3
- package/dist/repositories/extension.d.ts +3 -3
- package/dist/repositories/helpers.d.ts +3 -0
- package/dist/repositories/inventory-entry.d.ts +7 -7
- package/dist/repositories/my-order.d.ts +6 -0
- package/dist/repositories/order.d.ts +18 -17
- package/dist/repositories/payment.d.ts +8 -8
- package/dist/repositories/product-projection.d.ts +3 -3
- package/dist/repositories/product-type.d.ts +5 -5
- package/dist/repositories/product.d.ts +4 -4
- package/dist/repositories/project.d.ts +4 -4
- package/dist/repositories/shipping-method.d.ts +3 -3
- package/dist/repositories/shopping-list.d.ts +2 -2
- package/dist/repositories/state.d.ts +3 -3
- package/dist/repositories/store.d.ts +4 -4
- package/dist/repositories/subscription.d.ts +2 -2
- package/dist/repositories/tax-category.d.ts +4 -4
- package/dist/repositories/type.d.ts +3 -3
- package/dist/repositories/zone.d.ts +3 -3
- package/dist/services/my-order.d.ts +2 -2
- package/package.json +1 -1
- package/src/ctMock.ts +6 -0
- package/src/repositories/abstract.ts +37 -17
- package/src/repositories/cart-discount.ts +11 -11
- package/src/repositories/cart.ts +88 -36
- package/src/repositories/category.ts +17 -13
- package/src/repositories/channel.ts +3 -3
- package/src/repositories/custom-object.ts +16 -8
- package/src/repositories/customer-group.ts +5 -5
- package/src/repositories/customer.ts +10 -6
- package/src/repositories/discount-code.ts +14 -14
- package/src/repositories/extension.ts +12 -8
- package/src/repositories/helpers.ts +9 -0
- package/src/repositories/inventory-entry.ts +17 -10
- package/src/repositories/my-order.ts +19 -0
- package/src/repositories/order.test.ts +79 -3
- package/src/repositories/order.ts +77 -37
- package/src/repositories/payment.ts +21 -17
- package/src/repositories/product-projection.ts +5 -5
- package/src/repositories/product-type.ts +14 -10
- package/src/repositories/product.ts +5 -5
- package/src/repositories/project.ts +21 -17
- package/src/repositories/shipping-method.ts +27 -20
- package/src/repositories/shopping-list.ts +10 -6
- package/src/repositories/state.ts +13 -9
- package/src/repositories/store.ts +18 -14
- package/src/repositories/subscription.ts +3 -3
- package/src/repositories/tax-category.ts +16 -12
- package/src/repositories/type.ts +15 -11
- package/src/repositories/zone.ts +13 -9
- package/src/services/abstract.ts +21 -10
- package/src/services/cart.test.ts +48 -8
- package/src/services/cart.ts +17 -11
- package/src/services/custom-object.ts +8 -4
- package/src/services/customer.ts +5 -2
- package/src/services/my-customer.ts +7 -3
- package/src/services/my-order.ts +3 -3
- package/src/services/order.ts +3 -2
- package/src/services/product-projection.ts +2 -1
- package/src/services/product-type.ts +2 -1
- package/src/services/project.ts +4 -3
- package/src/services/store.ts +2 -1
- package/src/services/tax-category.ts +2 -1
|
@@ -22,20 +22,30 @@ export type GetParams = {
|
|
|
22
22
|
expand?: string[]
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
export type RepositoryContext = {
|
|
26
|
+
projectKey: string
|
|
27
|
+
storeKey?: string
|
|
28
|
+
}
|
|
25
29
|
export abstract class AbstractRepository {
|
|
26
30
|
protected _storage: AbstractStorage
|
|
27
31
|
protected actions: Partial<
|
|
28
|
-
Record<
|
|
32
|
+
Record<
|
|
33
|
+
any,
|
|
34
|
+
(context: RepositoryContext, resource: any, action: any) => void
|
|
35
|
+
>
|
|
29
36
|
> = {}
|
|
30
37
|
|
|
31
38
|
constructor(storage: AbstractStorage) {
|
|
32
39
|
this._storage = storage
|
|
33
40
|
}
|
|
34
41
|
|
|
35
|
-
abstract save(
|
|
42
|
+
abstract save(
|
|
43
|
+
{ projectKey }: RepositoryContext,
|
|
44
|
+
resource: BaseResource | Project
|
|
45
|
+
): void
|
|
36
46
|
|
|
37
47
|
processUpdateActions(
|
|
38
|
-
|
|
48
|
+
context: RepositoryContext,
|
|
39
49
|
resource: BaseResource | Project,
|
|
40
50
|
actions: UpdateAction[]
|
|
41
51
|
): BaseResource {
|
|
@@ -49,18 +59,18 @@ export abstract class AbstractRepository {
|
|
|
49
59
|
console.error(`No mock implemented for update action ${action.action}`)
|
|
50
60
|
return
|
|
51
61
|
}
|
|
52
|
-
updateFunc(
|
|
62
|
+
updateFunc(context, modifiedResource, action)
|
|
53
63
|
})
|
|
54
64
|
|
|
55
65
|
if (!deepEqual(modifiedResource, resource)) {
|
|
56
|
-
this.save(
|
|
66
|
+
this.save(context, modifiedResource)
|
|
57
67
|
}
|
|
58
68
|
return modifiedResource
|
|
59
69
|
}
|
|
60
70
|
}
|
|
61
71
|
|
|
62
72
|
export abstract class AbstractResourceRepository extends AbstractRepository {
|
|
63
|
-
abstract create(
|
|
73
|
+
abstract create(context: RepositoryContext, draft: any): BaseResource
|
|
64
74
|
abstract getTypeId(): RepositoryTypes
|
|
65
75
|
|
|
66
76
|
constructor(storage: AbstractStorage) {
|
|
@@ -68,8 +78,8 @@ export abstract class AbstractResourceRepository extends AbstractRepository {
|
|
|
68
78
|
this._storage.assertStorage(this.getTypeId())
|
|
69
79
|
}
|
|
70
80
|
|
|
71
|
-
query(
|
|
72
|
-
return this._storage.query(projectKey, this.getTypeId(), {
|
|
81
|
+
query(context: RepositoryContext, params: QueryParams = {}) {
|
|
82
|
+
return this._storage.query(context.projectKey, this.getTypeId(), {
|
|
73
83
|
expand: params.expand,
|
|
74
84
|
where: params.where,
|
|
75
85
|
offset: params.offset,
|
|
@@ -78,31 +88,41 @@ export abstract class AbstractResourceRepository extends AbstractRepository {
|
|
|
78
88
|
}
|
|
79
89
|
|
|
80
90
|
get(
|
|
81
|
-
|
|
91
|
+
context: RepositoryContext,
|
|
82
92
|
id: string,
|
|
83
93
|
params: GetParams = {}
|
|
84
94
|
): BaseResource | null {
|
|
85
|
-
return this._storage.get(projectKey, this.getTypeId(), id, params)
|
|
95
|
+
return this._storage.get(context.projectKey, this.getTypeId(), id, params)
|
|
86
96
|
}
|
|
87
97
|
|
|
88
98
|
getByKey(
|
|
89
|
-
|
|
99
|
+
context: RepositoryContext,
|
|
90
100
|
key: string,
|
|
91
101
|
params: GetParams = {}
|
|
92
102
|
): BaseResource | null {
|
|
93
|
-
return this._storage.getByKey(
|
|
103
|
+
return this._storage.getByKey(
|
|
104
|
+
context.projectKey,
|
|
105
|
+
this.getTypeId(),
|
|
106
|
+
key,
|
|
107
|
+
params
|
|
108
|
+
)
|
|
94
109
|
}
|
|
95
110
|
|
|
96
111
|
delete(
|
|
97
|
-
|
|
112
|
+
context: RepositoryContext,
|
|
98
113
|
id: string,
|
|
99
114
|
params: GetParams = {}
|
|
100
115
|
): BaseResource | null {
|
|
101
|
-
return this._storage.delete(
|
|
116
|
+
return this._storage.delete(
|
|
117
|
+
context.projectKey,
|
|
118
|
+
this.getTypeId(),
|
|
119
|
+
id,
|
|
120
|
+
params
|
|
121
|
+
)
|
|
102
122
|
}
|
|
103
123
|
|
|
104
|
-
save(
|
|
105
|
-
const current = this.get(
|
|
124
|
+
save(context: RepositoryContext, resource: BaseResource) {
|
|
125
|
+
const current = this.get(context, resource.id)
|
|
106
126
|
|
|
107
127
|
if (current) {
|
|
108
128
|
checkConcurrentModification(current, resource.version)
|
|
@@ -120,6 +140,6 @@ export abstract class AbstractResourceRepository extends AbstractRepository {
|
|
|
120
140
|
|
|
121
141
|
// @ts-ignore
|
|
122
142
|
resource.version += 1
|
|
123
|
-
this._storage.add(projectKey, this.getTypeId(), resource as any)
|
|
143
|
+
this._storage.add(context.projectKey, this.getTypeId(), resource as any)
|
|
124
144
|
}
|
|
125
145
|
}
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
} from '@commercetools/platform-sdk'
|
|
19
19
|
import { Writable } from 'types'
|
|
20
20
|
import { getBaseResourceProperties } from '../helpers'
|
|
21
|
-
import { AbstractResourceRepository } from './abstract'
|
|
21
|
+
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
22
22
|
import { createTypedMoney } from './helpers'
|
|
23
23
|
|
|
24
24
|
export class CartDiscountRepository extends AbstractResourceRepository {
|
|
@@ -26,7 +26,7 @@ export class CartDiscountRepository extends AbstractResourceRepository {
|
|
|
26
26
|
return 'cart-discount'
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
create(
|
|
29
|
+
create(context: RepositoryContext, draft: CartDiscountDraft): CartDiscount {
|
|
30
30
|
const resource: CartDiscount = {
|
|
31
31
|
...getBaseResourceProperties(),
|
|
32
32
|
key: draft.key,
|
|
@@ -43,7 +43,7 @@ export class CartDiscountRepository extends AbstractResourceRepository {
|
|
|
43
43
|
validUntil: draft.validUntil,
|
|
44
44
|
value: this.transformValueDraft(draft.value),
|
|
45
45
|
}
|
|
46
|
-
this.save(
|
|
46
|
+
this.save(context, resource)
|
|
47
47
|
return resource
|
|
48
48
|
}
|
|
49
49
|
|
|
@@ -80,42 +80,42 @@ export class CartDiscountRepository extends AbstractResourceRepository {
|
|
|
80
80
|
Record<
|
|
81
81
|
CartDiscountUpdateAction['action'],
|
|
82
82
|
(
|
|
83
|
-
|
|
83
|
+
context: RepositoryContext,
|
|
84
84
|
resource: Writable<CartDiscount>,
|
|
85
85
|
action: any
|
|
86
86
|
) => void
|
|
87
87
|
>
|
|
88
88
|
> = {
|
|
89
89
|
setKey: (
|
|
90
|
-
|
|
90
|
+
context: RepositoryContext,
|
|
91
91
|
resource: Writable<CartDiscount>,
|
|
92
92
|
{ key }: CartDiscountSetKeyAction
|
|
93
93
|
) => {
|
|
94
94
|
resource.key = key
|
|
95
95
|
},
|
|
96
96
|
setDescription: (
|
|
97
|
-
|
|
97
|
+
context: RepositoryContext,
|
|
98
98
|
resource: Writable<CartDiscount>,
|
|
99
99
|
{ description }: CartDiscountSetDescriptionAction
|
|
100
100
|
) => {
|
|
101
101
|
resource.description = description
|
|
102
102
|
},
|
|
103
103
|
setValidFrom: (
|
|
104
|
-
|
|
104
|
+
context: RepositoryContext,
|
|
105
105
|
resource: Writable<CartDiscount>,
|
|
106
106
|
{ validFrom }: CartDiscountSetValidFromAction
|
|
107
107
|
) => {
|
|
108
108
|
resource.validFrom = validFrom
|
|
109
109
|
},
|
|
110
110
|
setValidUntil: (
|
|
111
|
-
|
|
111
|
+
context: RepositoryContext,
|
|
112
112
|
resource: Writable<CartDiscount>,
|
|
113
113
|
{ validUntil }: CartDiscountSetValidUntilAction
|
|
114
114
|
) => {
|
|
115
115
|
resource.validUntil = validUntil
|
|
116
116
|
},
|
|
117
117
|
setValidFromAndUntil: (
|
|
118
|
-
|
|
118
|
+
context: RepositoryContext,
|
|
119
119
|
resource: Writable<CartDiscount>,
|
|
120
120
|
{ validFrom, validUntil }: CartDiscountSetValidFromAndUntilAction
|
|
121
121
|
) => {
|
|
@@ -123,14 +123,14 @@ export class CartDiscountRepository extends AbstractResourceRepository {
|
|
|
123
123
|
resource.validUntil = validUntil
|
|
124
124
|
},
|
|
125
125
|
changeSortOrder: (
|
|
126
|
-
|
|
126
|
+
context: RepositoryContext,
|
|
127
127
|
resource: Writable<CartDiscount>,
|
|
128
128
|
{ sortOrder }: CartDiscountChangeSortOrderAction
|
|
129
129
|
) => {
|
|
130
130
|
resource.sortOrder = sortOrder
|
|
131
131
|
},
|
|
132
132
|
changeIsActive: (
|
|
133
|
-
|
|
133
|
+
context: RepositoryContext,
|
|
134
134
|
resource: Writable<CartDiscount>,
|
|
135
135
|
{ isActive }: CartDiscountChangeIsActiveAction
|
|
136
136
|
) => {
|
package/src/repositories/cart.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
GeneralError,
|
|
15
15
|
LineItem,
|
|
16
16
|
LineItemDraft,
|
|
17
|
+
Price,
|
|
17
18
|
Product,
|
|
18
19
|
ProductPagedQueryResponse,
|
|
19
20
|
ProductVariant,
|
|
@@ -21,7 +22,7 @@ import {
|
|
|
21
22
|
} from '@commercetools/platform-sdk'
|
|
22
23
|
import { v4 as uuidv4 } from 'uuid'
|
|
23
24
|
import { getBaseResourceProperties } from '../helpers'
|
|
24
|
-
import { AbstractResourceRepository } from './abstract'
|
|
25
|
+
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
25
26
|
import { createCustomFields } from './helpers'
|
|
26
27
|
import { Writable } from '../types'
|
|
27
28
|
import { CommercetoolsError } from '../exceptions'
|
|
@@ -31,15 +32,21 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
31
32
|
return 'cart'
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
create(
|
|
35
|
-
const lineItems =
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
create(context: RepositoryContext, draft: CartDraft): Cart {
|
|
36
|
+
const lineItems =
|
|
37
|
+
draft.lineItems?.map(draftLineItem =>
|
|
38
|
+
this.draftLineItemtoLineItem(
|
|
39
|
+
context.projectKey,
|
|
40
|
+
draftLineItem,
|
|
41
|
+
draft.currency,
|
|
42
|
+
draft.country
|
|
43
|
+
)
|
|
44
|
+
) ?? []
|
|
38
45
|
|
|
39
46
|
const resource: Cart = {
|
|
40
47
|
...getBaseResourceProperties(),
|
|
41
48
|
cartState: 'Active',
|
|
42
|
-
lineItems
|
|
49
|
+
lineItems,
|
|
43
50
|
customLineItems: [],
|
|
44
51
|
totalPrice: {
|
|
45
52
|
type: 'centPrecision',
|
|
@@ -47,18 +54,24 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
47
54
|
currencyCode: draft.currency,
|
|
48
55
|
fractionDigits: 0,
|
|
49
56
|
},
|
|
50
|
-
taxMode: 'Platform',
|
|
51
|
-
taxRoundingMode: 'HalfEven',
|
|
52
|
-
taxCalculationMode: 'LineItemLevel',
|
|
57
|
+
taxMode: draft.taxMode ?? 'Platform',
|
|
58
|
+
taxRoundingMode: draft.taxRoundingMode ?? 'HalfEven',
|
|
59
|
+
taxCalculationMode: draft.taxCalculationMode ?? 'LineItemLevel',
|
|
53
60
|
refusedGifts: [],
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
locale: draft.locale,
|
|
62
|
+
country: draft.country,
|
|
63
|
+
origin: draft.origin ?? 'Customer',
|
|
64
|
+
custom: createCustomFields(
|
|
65
|
+
draft.custom,
|
|
66
|
+
context.projectKey,
|
|
67
|
+
this._storage
|
|
68
|
+
),
|
|
56
69
|
}
|
|
57
70
|
|
|
58
71
|
// @ts-ignore
|
|
59
72
|
resource.totalPrice.centAmount = calculateCartTotalPrice(resource)
|
|
60
73
|
|
|
61
|
-
this.save(
|
|
74
|
+
this.save(context, resource)
|
|
62
75
|
return resource
|
|
63
76
|
}
|
|
64
77
|
|
|
@@ -76,7 +89,7 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
76
89
|
|
|
77
90
|
actions = {
|
|
78
91
|
addLineItem: (
|
|
79
|
-
|
|
92
|
+
context: RepositoryContext,
|
|
80
93
|
resource: Writable<Cart>,
|
|
81
94
|
{ productId, variantId, sku, quantity = 1 }: CartAddLineItemAction
|
|
82
95
|
) => {
|
|
@@ -85,10 +98,15 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
85
98
|
|
|
86
99
|
if (productId && variantId) {
|
|
87
100
|
// Fetch product and variant by ID
|
|
88
|
-
product = this._storage.get(
|
|
101
|
+
product = this._storage.get(
|
|
102
|
+
context.projectKey,
|
|
103
|
+
'product',
|
|
104
|
+
productId,
|
|
105
|
+
{}
|
|
106
|
+
)
|
|
89
107
|
} else if (sku) {
|
|
90
108
|
// Fetch product and variant by SKU
|
|
91
|
-
const items = this._storage.query(projectKey, 'product', {
|
|
109
|
+
const items = this._storage.query(context.projectKey, 'product', {
|
|
92
110
|
where: [
|
|
93
111
|
`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`,
|
|
94
112
|
],
|
|
@@ -150,7 +168,18 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
150
168
|
})
|
|
151
169
|
}
|
|
152
170
|
|
|
153
|
-
const
|
|
171
|
+
const currency = resource.totalPrice.currencyCode
|
|
172
|
+
|
|
173
|
+
const price = selectPrice({
|
|
174
|
+
prices: variant.prices,
|
|
175
|
+
currency,
|
|
176
|
+
country: resource.country,
|
|
177
|
+
})
|
|
178
|
+
if (!price) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
`No valid price found for ${productId} for country ${resource.country} and currency ${currency}`
|
|
181
|
+
)
|
|
182
|
+
}
|
|
154
183
|
resource.lineItems.push({
|
|
155
184
|
id: uuidv4(),
|
|
156
185
|
productId: product.id,
|
|
@@ -176,7 +205,7 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
176
205
|
resource.totalPrice.centAmount = calculateCartTotalPrice(resource)
|
|
177
206
|
},
|
|
178
207
|
removeLineItem: (
|
|
179
|
-
|
|
208
|
+
context: RepositoryContext,
|
|
180
209
|
resource: Writable<Cart>,
|
|
181
210
|
{ lineItemId, quantity }: CartRemoveLineItemAction
|
|
182
211
|
) => {
|
|
@@ -208,19 +237,19 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
208
237
|
resource.totalPrice.centAmount = calculateCartTotalPrice(resource)
|
|
209
238
|
},
|
|
210
239
|
setBillingAddress: (
|
|
211
|
-
|
|
240
|
+
context: RepositoryContext,
|
|
212
241
|
resource: Writable<Cart>,
|
|
213
242
|
{ address }: CartSetBillingAddressAction
|
|
214
243
|
) => {
|
|
215
244
|
resource.billingAddress = address
|
|
216
245
|
},
|
|
217
246
|
setShippingMethod: (
|
|
218
|
-
|
|
247
|
+
context: RepositoryContext,
|
|
219
248
|
resource: Writable<Cart>,
|
|
220
249
|
{ shippingMethod }: CartSetShippingMethodAction
|
|
221
250
|
) => {
|
|
222
251
|
const resolvedType = this._storage.getByResourceIdentifier(
|
|
223
|
-
projectKey,
|
|
252
|
+
context.projectKey,
|
|
224
253
|
//@ts-ignore
|
|
225
254
|
shippingMethod
|
|
226
255
|
)
|
|
@@ -237,21 +266,21 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
237
266
|
}
|
|
238
267
|
},
|
|
239
268
|
setCountry: (
|
|
240
|
-
|
|
269
|
+
context: RepositoryContext,
|
|
241
270
|
resource: Writable<Cart>,
|
|
242
271
|
{ country }: CartSetCountryAction
|
|
243
272
|
) => {
|
|
244
273
|
resource.country = country
|
|
245
274
|
},
|
|
246
275
|
setCustomerEmail: (
|
|
247
|
-
|
|
276
|
+
context: RepositoryContext,
|
|
248
277
|
resource: Writable<Cart>,
|
|
249
278
|
{ email }: CartSetCustomerEmailAction
|
|
250
279
|
) => {
|
|
251
280
|
resource.customerEmail = email
|
|
252
281
|
},
|
|
253
282
|
setCustomField: (
|
|
254
|
-
|
|
283
|
+
context: RepositoryContext,
|
|
255
284
|
resource: Cart,
|
|
256
285
|
{ name, value }: CartSetCustomFieldAction
|
|
257
286
|
) => {
|
|
@@ -261,7 +290,7 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
261
290
|
resource.custom.fields[name] = value
|
|
262
291
|
},
|
|
263
292
|
setCustomType: (
|
|
264
|
-
|
|
293
|
+
context: RepositoryContext,
|
|
265
294
|
resource: Writable<Cart>,
|
|
266
295
|
{ type, fields }: CartSetCustomTypeAction
|
|
267
296
|
) => {
|
|
@@ -269,7 +298,7 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
269
298
|
resource.custom = undefined
|
|
270
299
|
} else {
|
|
271
300
|
const resolvedType = this._storage.getByResourceIdentifier(
|
|
272
|
-
projectKey,
|
|
301
|
+
context.projectKey,
|
|
273
302
|
type
|
|
274
303
|
)
|
|
275
304
|
if (!resolvedType) {
|
|
@@ -286,14 +315,14 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
286
315
|
}
|
|
287
316
|
},
|
|
288
317
|
setLocale: (
|
|
289
|
-
|
|
318
|
+
context: RepositoryContext,
|
|
290
319
|
resource: Writable<Cart>,
|
|
291
320
|
{ locale }: CartSetLocaleAction
|
|
292
321
|
) => {
|
|
293
322
|
resource.locale = locale
|
|
294
323
|
},
|
|
295
324
|
setShippingAddress: (
|
|
296
|
-
|
|
325
|
+
context: RepositoryContext,
|
|
297
326
|
resource: Writable<Cart>,
|
|
298
327
|
{ address }: CartSetShippingAddressAction
|
|
299
328
|
) => {
|
|
@@ -302,13 +331,12 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
302
331
|
}
|
|
303
332
|
draftLineItemtoLineItem = (
|
|
304
333
|
projectKey: string,
|
|
305
|
-
draftLineItem: LineItemDraft
|
|
334
|
+
draftLineItem: LineItemDraft,
|
|
335
|
+
currency: string,
|
|
336
|
+
country: string | undefined
|
|
306
337
|
): LineItem => {
|
|
307
|
-
const { productId, quantity } = draftLineItem
|
|
308
|
-
|
|
309
|
-
let variantId = draftLineItem.variant.id
|
|
310
|
-
// @ts-ignore
|
|
311
|
-
let sku = draftLineItem.variant.sku
|
|
338
|
+
const { productId, quantity, variantId, sku } = draftLineItem
|
|
339
|
+
|
|
312
340
|
let product: Product | null = null
|
|
313
341
|
let variant: ProductVariant | undefined
|
|
314
342
|
|
|
@@ -357,12 +385,13 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
357
385
|
)
|
|
358
386
|
}
|
|
359
387
|
|
|
360
|
-
const price = variant.prices?.[0]
|
|
361
|
-
|
|
362
388
|
const quant = quantity ?? 1
|
|
363
389
|
|
|
390
|
+
const price = selectPrice({ prices: variant.prices, currency, country })
|
|
364
391
|
if (!price) {
|
|
365
|
-
throw new Error(
|
|
392
|
+
throw new Error(
|
|
393
|
+
`No valid price found for ${productId} for country ${country} and currency ${currency}`
|
|
394
|
+
)
|
|
366
395
|
}
|
|
367
396
|
|
|
368
397
|
return {
|
|
@@ -387,6 +416,29 @@ export class CartRepository extends AbstractResourceRepository {
|
|
|
387
416
|
}
|
|
388
417
|
}
|
|
389
418
|
|
|
419
|
+
const selectPrice = ({
|
|
420
|
+
prices,
|
|
421
|
+
currency,
|
|
422
|
+
country,
|
|
423
|
+
}: {
|
|
424
|
+
prices: Price[] | undefined
|
|
425
|
+
currency: string
|
|
426
|
+
country: string | undefined
|
|
427
|
+
}) => {
|
|
428
|
+
if (!prices) {
|
|
429
|
+
return undefined
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Quick-and-dirty way of selecting price based on the given currency and country.
|
|
433
|
+
// Can be improved later to give more priority to exact matches over
|
|
434
|
+
// 'all country' matches, and include customer groups in the mix as well
|
|
435
|
+
return prices.find(price => {
|
|
436
|
+
const countryMatch = !price.country || price.country === country
|
|
437
|
+
const currencyMatch = price.value.currencyCode === currency
|
|
438
|
+
return countryMatch && currencyMatch
|
|
439
|
+
})
|
|
440
|
+
}
|
|
441
|
+
|
|
390
442
|
const calculateLineItemTotalPrice = (lineItem: LineItem): number =>
|
|
391
443
|
lineItem.price!.value.centAmount * lineItem.quantity
|
|
392
444
|
|
|
@@ -16,14 +16,14 @@ import {
|
|
|
16
16
|
import { Writable } from 'types'
|
|
17
17
|
import { getBaseResourceProperties } from '../helpers'
|
|
18
18
|
import { createCustomFields } from './helpers'
|
|
19
|
-
import { AbstractResourceRepository } from './abstract'
|
|
19
|
+
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
20
20
|
|
|
21
21
|
export class CategoryRepository extends AbstractResourceRepository {
|
|
22
22
|
getTypeId(): ReferenceTypeId {
|
|
23
23
|
return 'category'
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
create(
|
|
26
|
+
create(context: RepositoryContext, draft: CategoryDraft): Category {
|
|
27
27
|
const resource: Category = {
|
|
28
28
|
...getBaseResourceProperties(),
|
|
29
29
|
key: draft.key,
|
|
@@ -44,17 +44,21 @@ export class CategoryRepository extends AbstractResourceRepository {
|
|
|
44
44
|
sources: d.sources,
|
|
45
45
|
tags: d.tags,
|
|
46
46
|
key: d.key,
|
|
47
|
-
custom: createCustomFields(
|
|
47
|
+
custom: createCustomFields(
|
|
48
|
+
draft.custom,
|
|
49
|
+
context.projectKey,
|
|
50
|
+
this._storage
|
|
51
|
+
),
|
|
48
52
|
}
|
|
49
53
|
}) || [],
|
|
50
54
|
}
|
|
51
|
-
this.save(
|
|
55
|
+
this.save(context, resource)
|
|
52
56
|
return resource
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
actions = {
|
|
56
60
|
changeAssetName: (
|
|
57
|
-
|
|
61
|
+
context: RepositoryContext,
|
|
58
62
|
resource: Writable<Category>,
|
|
59
63
|
{ assetId, assetKey, name }: CategoryChangeAssetNameAction
|
|
60
64
|
) => {
|
|
@@ -68,21 +72,21 @@ export class CategoryRepository extends AbstractResourceRepository {
|
|
|
68
72
|
})
|
|
69
73
|
},
|
|
70
74
|
changeSlug: (
|
|
71
|
-
|
|
75
|
+
context: RepositoryContext,
|
|
72
76
|
resource: Writable<Category>,
|
|
73
77
|
{ slug }: CategoryChangeSlugAction
|
|
74
78
|
) => {
|
|
75
79
|
resource.slug = slug
|
|
76
80
|
},
|
|
77
81
|
setKey: (
|
|
78
|
-
|
|
82
|
+
context: RepositoryContext,
|
|
79
83
|
resource: Writable<Category>,
|
|
80
84
|
{ key }: CategorySetKeyAction
|
|
81
85
|
) => {
|
|
82
86
|
resource.key = key
|
|
83
87
|
},
|
|
84
88
|
setAssetDescription: (
|
|
85
|
-
|
|
89
|
+
context: RepositoryContext,
|
|
86
90
|
resource: Writable<Category>,
|
|
87
91
|
{ assetId, assetKey, description }: CategorySetAssetDescriptionAction
|
|
88
92
|
) => {
|
|
@@ -96,7 +100,7 @@ export class CategoryRepository extends AbstractResourceRepository {
|
|
|
96
100
|
})
|
|
97
101
|
},
|
|
98
102
|
setAssetSources: (
|
|
99
|
-
|
|
103
|
+
context: RepositoryContext,
|
|
100
104
|
resource: Writable<Category>,
|
|
101
105
|
{ assetId, assetKey, sources }: CategorySetAssetSourcesAction
|
|
102
106
|
) => {
|
|
@@ -110,28 +114,28 @@ export class CategoryRepository extends AbstractResourceRepository {
|
|
|
110
114
|
})
|
|
111
115
|
},
|
|
112
116
|
setDescription: (
|
|
113
|
-
|
|
117
|
+
context: RepositoryContext,
|
|
114
118
|
resource: Writable<Category>,
|
|
115
119
|
{ description }: CategorySetDescriptionAction
|
|
116
120
|
) => {
|
|
117
121
|
resource.description = description
|
|
118
122
|
},
|
|
119
123
|
setMetaDescription: (
|
|
120
|
-
|
|
124
|
+
context: RepositoryContext,
|
|
121
125
|
resource: Writable<Category>,
|
|
122
126
|
{ metaDescription }: CategorySetMetaDescriptionAction
|
|
123
127
|
) => {
|
|
124
128
|
resource.metaDescription = metaDescription
|
|
125
129
|
},
|
|
126
130
|
setMetaKeywords: (
|
|
127
|
-
|
|
131
|
+
context: RepositoryContext,
|
|
128
132
|
resource: Writable<Category>,
|
|
129
133
|
{ metaKeywords }: CategorySetMetaKeywordsAction
|
|
130
134
|
) => {
|
|
131
135
|
resource.metaKeywords = metaKeywords
|
|
132
136
|
},
|
|
133
137
|
setMetaTitle: (
|
|
134
|
-
|
|
138
|
+
context: RepositoryContext,
|
|
135
139
|
resource: Writable<Category>,
|
|
136
140
|
{ metaTitle }: CategorySetMetaTitleAction
|
|
137
141
|
) => {
|
|
@@ -4,20 +4,20 @@ import {
|
|
|
4
4
|
ReferenceTypeId,
|
|
5
5
|
} from '@commercetools/platform-sdk'
|
|
6
6
|
import { getBaseResourceProperties } from '../helpers'
|
|
7
|
-
import { AbstractResourceRepository } from './abstract'
|
|
7
|
+
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
8
8
|
|
|
9
9
|
export class ChannelRepository extends AbstractResourceRepository {
|
|
10
10
|
getTypeId(): ReferenceTypeId {
|
|
11
11
|
return 'channel'
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
create(
|
|
14
|
+
create(context: RepositoryContext, draft: ChannelDraft): Channel {
|
|
15
15
|
const resource: Channel = {
|
|
16
16
|
...getBaseResourceProperties(),
|
|
17
17
|
key: draft.key,
|
|
18
18
|
roles: draft.roles || [],
|
|
19
19
|
}
|
|
20
|
-
this.save(
|
|
20
|
+
this.save(context, resource)
|
|
21
21
|
return resource
|
|
22
22
|
}
|
|
23
23
|
}
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
ReferenceTypeId,
|
|
5
5
|
} from '@commercetools/platform-sdk'
|
|
6
6
|
import { checkConcurrentModification } from './errors'
|
|
7
|
-
import { AbstractResourceRepository } from './abstract'
|
|
7
|
+
import { AbstractResourceRepository, RepositoryContext } from './abstract'
|
|
8
8
|
import { Writable } from '../types'
|
|
9
9
|
import { getBaseResourceProperties } from '../helpers'
|
|
10
10
|
|
|
@@ -13,9 +13,12 @@ export class CustomObjectRepository extends AbstractResourceRepository {
|
|
|
13
13
|
return 'key-value-document'
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
create(
|
|
16
|
+
create(
|
|
17
|
+
context: RepositoryContext,
|
|
18
|
+
draft: Writable<CustomObjectDraft>
|
|
19
|
+
): CustomObject {
|
|
17
20
|
const current = this.getWithContainerAndKey(
|
|
18
|
-
|
|
21
|
+
context,
|
|
19
22
|
draft.container,
|
|
20
23
|
draft.key
|
|
21
24
|
)
|
|
@@ -47,14 +50,19 @@ export class CustomObjectRepository extends AbstractResourceRepository {
|
|
|
47
50
|
value: draft.value,
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
this.save(
|
|
53
|
+
this.save(context, resource)
|
|
51
54
|
return resource
|
|
52
55
|
}
|
|
53
56
|
|
|
54
|
-
getWithContainerAndKey(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
getWithContainerAndKey(
|
|
58
|
+
context: RepositoryContext,
|
|
59
|
+
container: string,
|
|
60
|
+
key: string
|
|
61
|
+
) {
|
|
62
|
+
const items = this._storage.all(
|
|
63
|
+
context.projectKey,
|
|
64
|
+
this.getTypeId()
|
|
65
|
+
) as Array<CustomObject>
|
|
58
66
|
return items.find(item => item.container === container && item.key === key)
|
|
59
67
|
}
|
|
60
68
|
}
|