@labdigital/commercetools-mock 2.17.0 → 2.18.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/index.cjs +4219 -3989
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +268 -415
- package/dist/index.d.ts +268 -415
- package/dist/index.js +4219 -3989
- package/dist/index.js.map +1 -1
- package/package.json +44 -46
- package/src/constants.ts +2 -2
- package/src/ctMock.test.ts +11 -11
- package/src/ctMock.ts +141 -127
- package/src/deprecation.ts +8 -0
- package/src/exceptions.ts +17 -15
- package/src/helpers.ts +32 -32
- package/src/index.test.ts +128 -128
- package/src/index.ts +3 -3
- package/src/lib/expandParser.ts +13 -13
- package/src/lib/haversine.test.ts +9 -9
- package/src/lib/haversine.ts +11 -11
- package/src/lib/masking.ts +11 -11
- package/src/lib/parser.ts +2 -2
- package/src/lib/password.ts +23 -3
- package/src/lib/predicateParser.test.ts +185 -183
- package/src/lib/predicateParser.ts +234 -234
- package/src/lib/projectionSearchFilter.test.ts +103 -101
- package/src/lib/projectionSearchFilter.ts +152 -150
- package/src/lib/proxy.ts +5 -5
- package/src/oauth/errors.ts +4 -4
- package/src/oauth/helpers.ts +6 -6
- package/src/oauth/server.test.ts +110 -67
- package/src/oauth/server.ts +161 -141
- package/src/oauth/store.ts +49 -44
- package/src/priceSelector.test.ts +35 -35
- package/src/priceSelector.ts +30 -30
- package/src/product-projection-search.ts +136 -134
- package/src/projectAPI.test.ts +7 -7
- package/src/projectAPI.ts +24 -22
- package/src/repositories/abstract.ts +168 -116
- package/src/repositories/associate-role.ts +90 -77
- package/src/repositories/attribute-group.ts +51 -40
- package/src/repositories/business-unit.ts +168 -148
- package/src/repositories/cart/actions.ts +489 -0
- package/src/repositories/cart/helpers.ts +30 -0
- package/src/repositories/cart/index.ts +180 -0
- package/src/repositories/cart-discount/actions.ts +148 -0
- package/src/repositories/cart-discount/index.ts +86 -0
- package/src/repositories/category/actions.ts +231 -0
- package/src/repositories/category/index.ts +52 -0
- package/src/repositories/channel.ts +88 -90
- package/src/repositories/custom-object.ts +46 -45
- package/src/repositories/customer/actions.ts +165 -0
- package/src/repositories/customer/index.ts +79 -0
- package/src/repositories/customer-group.ts +66 -55
- package/src/repositories/discount-code/actions.ts +149 -0
- package/src/repositories/discount-code/index.ts +50 -0
- package/src/repositories/errors.ts +10 -10
- package/src/repositories/extension.ts +64 -62
- package/src/repositories/helpers.ts +117 -118
- package/src/repositories/index.ts +80 -79
- package/src/repositories/inventory-entry/actions.ts +84 -0
- package/src/repositories/inventory-entry/index.ts +44 -0
- package/src/repositories/my-customer.ts +114 -0
- package/src/repositories/my-order.ts +8 -8
- package/src/repositories/order/actions.ts +281 -0
- package/src/repositories/{order.test.ts → order/index.test.ts} +77 -77
- package/src/repositories/order/index.ts +260 -0
- package/src/repositories/order-edit.ts +10 -23
- package/src/repositories/payment/actions.ts +305 -0
- package/src/repositories/payment/helpers.ts +17 -0
- package/src/repositories/payment/index.ts +56 -0
- package/src/repositories/product/actions.ts +943 -0
- package/src/repositories/product/helpers.ts +98 -0
- package/src/repositories/product/index.ts +130 -0
- package/src/repositories/product-discount.ts +127 -117
- package/src/repositories/product-projection.ts +56 -62
- package/src/repositories/product-selection.ts +31 -28
- package/src/repositories/product-type.ts +136 -134
- package/src/repositories/project.ts +133 -118
- package/src/repositories/quote-request.ts +7 -19
- package/src/repositories/quote.ts +7 -22
- package/src/repositories/review.ts +13 -26
- package/src/repositories/shipping-method/actions.ts +198 -0
- package/src/repositories/shipping-method/helpers.ts +10 -0
- package/src/repositories/shipping-method/index.ts +138 -0
- package/src/repositories/shopping-list/actions.ts +295 -0
- package/src/repositories/shopping-list/index.ts +122 -0
- package/src/repositories/staged-quote.ts +7 -20
- package/src/repositories/standalone-price.ts +57 -44
- package/src/repositories/state.ts +113 -68
- package/src/repositories/store.ts +106 -94
- package/src/repositories/subscription.ts +46 -22
- package/src/repositories/tax-category/actions.ts +94 -0
- package/src/repositories/tax-category/helpers.ts +8 -0
- package/src/repositories/tax-category/index.ts +25 -0
- package/src/repositories/type/actions.ts +162 -0
- package/src/repositories/type/index.ts +24 -0
- package/src/repositories/zone.ts +62 -58
- package/src/server.ts +9 -9
- package/src/services/abstract.ts +75 -72
- package/src/services/associate-roles.test.ts +27 -27
- package/src/services/associate-roles.ts +7 -7
- package/src/services/attribute-group.ts +7 -7
- package/src/services/business-units.test.ts +28 -28
- package/src/services/business-units.ts +7 -7
- package/src/services/cart-discount.test.ts +199 -199
- package/src/services/cart-discount.ts +7 -7
- package/src/services/cart.test.ts +261 -261
- package/src/services/cart.ts +22 -21
- package/src/services/category.test.ts +121 -121
- package/src/services/category.ts +7 -7
- package/src/services/channel.ts +7 -7
- package/src/services/custom-object.test.ts +130 -130
- package/src/services/custom-object.ts +34 -31
- package/src/services/customer-group.ts +7 -7
- package/src/services/customer.test.ts +205 -205
- package/src/services/customer.ts +31 -29
- package/src/services/discount-code.ts +7 -7
- package/src/services/extension.ts +7 -7
- package/src/services/index.ts +85 -81
- package/src/services/inventory-entry.test.ts +106 -106
- package/src/services/inventory-entry.ts +7 -7
- package/src/services/my-cart.test.ts +56 -56
- package/src/services/my-cart.ts +20 -20
- package/src/services/my-customer.test.ts +155 -104
- package/src/services/my-customer.ts +61 -75
- package/src/services/my-order.ts +16 -16
- package/src/services/my-payment.test.ts +40 -40
- package/src/services/my-payment.ts +7 -7
- package/src/services/my-shopping-list.ts +7 -7
- package/src/services/order.test.ts +243 -243
- package/src/services/order.ts +23 -18
- package/src/services/payment.test.ts +40 -40
- package/src/services/payment.ts +7 -7
- package/src/services/product-discount.ts +7 -7
- package/src/services/product-projection.test.ts +190 -190
- package/src/services/product-projection.ts +34 -32
- package/src/services/product-selection.test.ts +19 -19
- package/src/services/product-selection.ts +7 -7
- package/src/services/product-type.test.ts +38 -38
- package/src/services/product-type.ts +7 -7
- package/src/services/product.test.ts +658 -656
- package/src/services/product.ts +7 -7
- package/src/services/project.test.ts +24 -24
- package/src/services/project.ts +17 -17
- package/src/services/reviews.ts +7 -7
- package/src/services/shipping-method.test.ts +78 -78
- package/src/services/shipping-method.ts +16 -16
- package/src/services/shopping-list.test.ts +170 -170
- package/src/services/shopping-list.ts +7 -7
- package/src/services/standalone-price.test.ts +112 -112
- package/src/services/standalone-price.ts +7 -7
- package/src/services/state.test.ts +30 -30
- package/src/services/state.ts +7 -7
- package/src/services/store.test.ts +40 -40
- package/src/services/store.ts +7 -7
- package/src/services/subscription.ts +7 -7
- package/src/services/tax-category.test.ts +43 -43
- package/src/services/tax-category.ts +7 -7
- package/src/services/type.ts +7 -7
- package/src/services/zone.ts +7 -7
- package/src/shippingCalculator.test.ts +43 -43
- package/src/shippingCalculator.ts +23 -23
- package/src/storage/abstract.ts +36 -34
- package/src/storage/in-memory.ts +237 -233
- package/src/storage/index.ts +2 -2
- package/src/types.ts +91 -91
- package/src/repositories/cart-discount.ts +0 -219
- package/src/repositories/cart.ts +0 -659
- package/src/repositories/category.ts +0 -256
- package/src/repositories/customer.ts +0 -228
- package/src/repositories/discount-code.ts +0 -181
- package/src/repositories/inventory-entry.ts +0 -109
- package/src/repositories/order.ts +0 -514
- package/src/repositories/payment.ts +0 -342
- package/src/repositories/product.ts +0 -1106
- package/src/repositories/shipping-method.ts +0 -312
- package/src/repositories/shopping-list.ts +0 -392
- package/src/repositories/tax-category.ts +0 -111
- package/src/repositories/type.ts +0 -172
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
InvalidOperationError,
|
|
3
|
-
type ShippingMethod,
|
|
4
|
-
type ShippingMethodAddShippingRateAction,
|
|
5
|
-
type ShippingMethodAddZoneAction,
|
|
6
|
-
type ShippingMethodChangeIsDefaultAction,
|
|
7
|
-
type ShippingMethodChangeNameAction,
|
|
8
|
-
type ShippingMethodDraft,
|
|
9
|
-
type ShippingMethodRemoveZoneAction,
|
|
10
|
-
type ShippingMethodSetCustomFieldAction,
|
|
11
|
-
type ShippingMethodSetCustomTypeAction,
|
|
12
|
-
type ShippingMethodSetDescriptionAction,
|
|
13
|
-
type ShippingMethodSetKeyAction,
|
|
14
|
-
type ShippingMethodSetLocalizedDescriptionAction,
|
|
15
|
-
type ShippingMethodSetLocalizedNameAction,
|
|
16
|
-
type ShippingMethodSetPredicateAction,
|
|
17
|
-
type ShippingMethodUpdateAction,
|
|
18
|
-
type ShippingRate,
|
|
19
|
-
type ShippingRateDraft,
|
|
20
|
-
type ZoneRate,
|
|
21
|
-
type ZoneRateDraft,
|
|
22
|
-
type ZoneReference,
|
|
23
|
-
} from '@commercetools/platform-sdk'
|
|
24
|
-
import deepEqual from 'deep-equal'
|
|
25
|
-
import { getBaseResourceProperties } from '../helpers.js'
|
|
26
|
-
import type { Writable } from '../types.js'
|
|
27
|
-
import {
|
|
28
|
-
AbstractResourceRepository,
|
|
29
|
-
GetParams,
|
|
30
|
-
RepositoryContext,
|
|
31
|
-
} from './abstract.js'
|
|
32
|
-
import {
|
|
33
|
-
createCustomFields,
|
|
34
|
-
createTypedMoney,
|
|
35
|
-
getReferenceFromResourceIdentifier,
|
|
36
|
-
} from './helpers.js'
|
|
37
|
-
import { CommercetoolsError } from '../exceptions.js'
|
|
38
|
-
import { markMatchingShippingRate } from '../shippingCalculator.js'
|
|
39
|
-
|
|
40
|
-
export class ShippingMethodRepository extends AbstractResourceRepository<'shipping-method'> {
|
|
41
|
-
getTypeId() {
|
|
42
|
-
return 'shipping-method' as const
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
create(
|
|
46
|
-
context: RepositoryContext,
|
|
47
|
-
draft: ShippingMethodDraft
|
|
48
|
-
): ShippingMethod {
|
|
49
|
-
const resource: ShippingMethod = {
|
|
50
|
-
...getBaseResourceProperties(),
|
|
51
|
-
...draft,
|
|
52
|
-
taxCategory: getReferenceFromResourceIdentifier(
|
|
53
|
-
draft.taxCategory,
|
|
54
|
-
context.projectKey,
|
|
55
|
-
this._storage
|
|
56
|
-
),
|
|
57
|
-
zoneRates: draft.zoneRates?.map((z) =>
|
|
58
|
-
this._transformZoneRateDraft(context, z)
|
|
59
|
-
),
|
|
60
|
-
custom: createCustomFields(
|
|
61
|
-
draft.custom,
|
|
62
|
-
context.projectKey,
|
|
63
|
-
this._storage
|
|
64
|
-
),
|
|
65
|
-
}
|
|
66
|
-
this.saveNew(context, resource)
|
|
67
|
-
return resource
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private _transformZoneRateDraft = (
|
|
71
|
-
context: RepositoryContext,
|
|
72
|
-
draft: ZoneRateDraft
|
|
73
|
-
): ZoneRate => ({
|
|
74
|
-
...draft,
|
|
75
|
-
zone: getReferenceFromResourceIdentifier<ZoneReference>(
|
|
76
|
-
draft.zone,
|
|
77
|
-
context.projectKey,
|
|
78
|
-
this._storage
|
|
79
|
-
),
|
|
80
|
-
shippingRates: draft.shippingRates?.map(this._transformShippingRate),
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
private _transformShippingRate = (rate: ShippingRateDraft): ShippingRate => ({
|
|
84
|
-
price: createTypedMoney(rate.price),
|
|
85
|
-
freeAbove: rate.freeAbove && createTypedMoney(rate.freeAbove),
|
|
86
|
-
tiers: rate.tiers || [],
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
/*
|
|
90
|
-
* Retrieves all the ShippingMethods that can ship to the shipping address of
|
|
91
|
-
* the given Cart. Each ShippingMethod contains exactly one ShippingRate with
|
|
92
|
-
* the flag isMatching set to true. This ShippingRate is used when the
|
|
93
|
-
* ShippingMethod is added to the Cart.
|
|
94
|
-
*/
|
|
95
|
-
public matchingCart(
|
|
96
|
-
context: RepositoryContext,
|
|
97
|
-
cartId: string,
|
|
98
|
-
params: GetParams = {}
|
|
99
|
-
) {
|
|
100
|
-
const cart = this._storage.get(context.projectKey, 'cart', cartId)
|
|
101
|
-
if (!cart) {
|
|
102
|
-
return undefined
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (!cart.shippingAddress?.country) {
|
|
106
|
-
throw new CommercetoolsError<InvalidOperationError>({
|
|
107
|
-
code: 'InvalidOperation',
|
|
108
|
-
message: `The cart with ID '${cart.id}' does not have a shipping address set.`,
|
|
109
|
-
})
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Get all shipping methods that have a zone that matches the shipping address
|
|
113
|
-
const zones = this._storage.query<'zone'>(context.projectKey, 'zone', {
|
|
114
|
-
where: [`locations(country="${cart.shippingAddress.country}"))`],
|
|
115
|
-
limit: 100,
|
|
116
|
-
})
|
|
117
|
-
const zoneIds = zones.results.map((zone) => zone.id)
|
|
118
|
-
const shippingMethods = this.query(context, {
|
|
119
|
-
where: [
|
|
120
|
-
`zoneRates(zone(id in (:zoneIds)))`,
|
|
121
|
-
`zoneRates(shippingRates(price(currencyCode="${cart.totalPrice.currencyCode}")))`,
|
|
122
|
-
],
|
|
123
|
-
'var.zoneIds': zoneIds,
|
|
124
|
-
expand: params.expand,
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
// Make sure that each shipping method has exactly one shipping rate and
|
|
128
|
-
// that the shipping rate is marked as matching
|
|
129
|
-
const results = shippingMethods.results
|
|
130
|
-
.map((shippingMethod) => {
|
|
131
|
-
// Iterate through the zoneRates, process the shipping rates and filter
|
|
132
|
-
// out all zoneRates which have no matching shipping rates left
|
|
133
|
-
const rates = shippingMethod.zoneRates
|
|
134
|
-
.map((zoneRate) => ({
|
|
135
|
-
zone: zoneRate.zone,
|
|
136
|
-
|
|
137
|
-
// Iterate through the shippingRates and mark the matching ones
|
|
138
|
-
// then we filter out the non-matching ones
|
|
139
|
-
shippingRates: zoneRate.shippingRates
|
|
140
|
-
.map((rate) => markMatchingShippingRate(cart, rate))
|
|
141
|
-
.filter((rate) => rate.isMatching),
|
|
142
|
-
}))
|
|
143
|
-
.filter((zoneRate) => zoneRate.shippingRates.length > 0)
|
|
144
|
-
|
|
145
|
-
return {
|
|
146
|
-
...shippingMethod,
|
|
147
|
-
zoneRates: rates,
|
|
148
|
-
}
|
|
149
|
-
})
|
|
150
|
-
.filter((shippingMethod) => shippingMethod.zoneRates.length > 0)
|
|
151
|
-
|
|
152
|
-
return {
|
|
153
|
-
...shippingMethods,
|
|
154
|
-
results: results,
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
actions: Partial<
|
|
159
|
-
Record<
|
|
160
|
-
ShippingMethodUpdateAction['action'],
|
|
161
|
-
(
|
|
162
|
-
context: RepositoryContext,
|
|
163
|
-
resource: Writable<ShippingMethod>,
|
|
164
|
-
action: any
|
|
165
|
-
) => void
|
|
166
|
-
>
|
|
167
|
-
> = {
|
|
168
|
-
addShippingRate: (
|
|
169
|
-
_context: RepositoryContext,
|
|
170
|
-
resource: Writable<ShippingMethod>,
|
|
171
|
-
{ shippingRate, zone }: ShippingMethodAddShippingRateAction
|
|
172
|
-
) => {
|
|
173
|
-
const rate = this._transformShippingRate(shippingRate)
|
|
174
|
-
|
|
175
|
-
resource.zoneRates.forEach((zoneRate) => {
|
|
176
|
-
if (zoneRate.zone.id === zone.id) {
|
|
177
|
-
zoneRate.shippingRates.push(rate)
|
|
178
|
-
return
|
|
179
|
-
}
|
|
180
|
-
})
|
|
181
|
-
resource.zoneRates.push({
|
|
182
|
-
zone: {
|
|
183
|
-
typeId: 'zone',
|
|
184
|
-
id: zone.id!,
|
|
185
|
-
},
|
|
186
|
-
shippingRates: [rate],
|
|
187
|
-
})
|
|
188
|
-
},
|
|
189
|
-
removeShippingRate: (
|
|
190
|
-
_context: RepositoryContext,
|
|
191
|
-
resource: Writable<ShippingMethod>,
|
|
192
|
-
{ shippingRate, zone }: ShippingMethodAddShippingRateAction
|
|
193
|
-
) => {
|
|
194
|
-
const rate = this._transformShippingRate(shippingRate)
|
|
195
|
-
|
|
196
|
-
resource.zoneRates.forEach((zoneRate) => {
|
|
197
|
-
if (zoneRate.zone.id === zone.id) {
|
|
198
|
-
zoneRate.shippingRates = zoneRate.shippingRates.filter(
|
|
199
|
-
(otherRate) => !deepEqual(rate, otherRate)
|
|
200
|
-
)
|
|
201
|
-
}
|
|
202
|
-
})
|
|
203
|
-
},
|
|
204
|
-
addZone: (
|
|
205
|
-
context: RepositoryContext,
|
|
206
|
-
resource: Writable<ShippingMethod>,
|
|
207
|
-
{ zone }: ShippingMethodAddZoneAction
|
|
208
|
-
) => {
|
|
209
|
-
const zoneReference = getReferenceFromResourceIdentifier<ZoneReference>(
|
|
210
|
-
zone,
|
|
211
|
-
context.projectKey,
|
|
212
|
-
this._storage
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
if (resource.zoneRates === undefined) {
|
|
216
|
-
resource.zoneRates = []
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
resource.zoneRates.push({
|
|
220
|
-
zone: zoneReference,
|
|
221
|
-
shippingRates: [],
|
|
222
|
-
})
|
|
223
|
-
},
|
|
224
|
-
removeZone: (
|
|
225
|
-
_context: RepositoryContext,
|
|
226
|
-
resource: Writable<ShippingMethod>,
|
|
227
|
-
{ zone }: ShippingMethodRemoveZoneAction
|
|
228
|
-
) => {
|
|
229
|
-
resource.zoneRates = resource.zoneRates.filter(
|
|
230
|
-
(zoneRate) => zoneRate.zone.id !== zone.id
|
|
231
|
-
)
|
|
232
|
-
},
|
|
233
|
-
setKey: (
|
|
234
|
-
_context: RepositoryContext,
|
|
235
|
-
resource: Writable<ShippingMethod>,
|
|
236
|
-
{ key }: ShippingMethodSetKeyAction
|
|
237
|
-
) => {
|
|
238
|
-
resource.key = key
|
|
239
|
-
},
|
|
240
|
-
setDescription: (
|
|
241
|
-
_context: RepositoryContext,
|
|
242
|
-
resource: Writable<ShippingMethod>,
|
|
243
|
-
{ description }: ShippingMethodSetDescriptionAction
|
|
244
|
-
) => {
|
|
245
|
-
resource.description = description
|
|
246
|
-
},
|
|
247
|
-
setLocalizedDescription: (
|
|
248
|
-
_context: RepositoryContext,
|
|
249
|
-
resource: Writable<ShippingMethod>,
|
|
250
|
-
{ localizedDescription }: ShippingMethodSetLocalizedDescriptionAction
|
|
251
|
-
) => {
|
|
252
|
-
resource.localizedDescription = localizedDescription
|
|
253
|
-
},
|
|
254
|
-
setLocalizedName: (
|
|
255
|
-
_context: RepositoryContext,
|
|
256
|
-
resource: Writable<ShippingMethod>,
|
|
257
|
-
{ localizedName }: ShippingMethodSetLocalizedNameAction
|
|
258
|
-
) => {
|
|
259
|
-
resource.localizedName = localizedName
|
|
260
|
-
},
|
|
261
|
-
setPredicate: (
|
|
262
|
-
_context: RepositoryContext,
|
|
263
|
-
resource: Writable<ShippingMethod>,
|
|
264
|
-
{ predicate }: ShippingMethodSetPredicateAction
|
|
265
|
-
) => {
|
|
266
|
-
resource.predicate = predicate
|
|
267
|
-
},
|
|
268
|
-
changeIsDefault: (
|
|
269
|
-
_context: RepositoryContext,
|
|
270
|
-
resource: Writable<ShippingMethod>,
|
|
271
|
-
{ isDefault }: ShippingMethodChangeIsDefaultAction
|
|
272
|
-
) => {
|
|
273
|
-
resource.isDefault = isDefault
|
|
274
|
-
},
|
|
275
|
-
changeName: (
|
|
276
|
-
_context: RepositoryContext,
|
|
277
|
-
resource: Writable<ShippingMethod>,
|
|
278
|
-
{ name }: ShippingMethodChangeNameAction
|
|
279
|
-
) => {
|
|
280
|
-
resource.name = name
|
|
281
|
-
},
|
|
282
|
-
setCustomType: (
|
|
283
|
-
context: RepositoryContext,
|
|
284
|
-
resource: Writable<ShippingMethod>,
|
|
285
|
-
{ type, fields }: ShippingMethodSetCustomTypeAction
|
|
286
|
-
) => {
|
|
287
|
-
if (type) {
|
|
288
|
-
resource.custom = createCustomFields(
|
|
289
|
-
{ type, fields },
|
|
290
|
-
context.projectKey,
|
|
291
|
-
this._storage
|
|
292
|
-
)
|
|
293
|
-
} else {
|
|
294
|
-
resource.custom = undefined
|
|
295
|
-
}
|
|
296
|
-
},
|
|
297
|
-
setCustomField: (
|
|
298
|
-
context: RepositoryContext,
|
|
299
|
-
resource: Writable<ShippingMethod>,
|
|
300
|
-
{ name, value }: ShippingMethodSetCustomFieldAction
|
|
301
|
-
) => {
|
|
302
|
-
if (!resource.custom) {
|
|
303
|
-
return
|
|
304
|
-
}
|
|
305
|
-
if (value === null) {
|
|
306
|
-
delete resource.custom.fields[name]
|
|
307
|
-
} else {
|
|
308
|
-
resource.custom.fields[name] = value
|
|
309
|
-
}
|
|
310
|
-
},
|
|
311
|
-
}
|
|
312
|
-
}
|
|
@@ -1,392 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CustomerReference,
|
|
3
|
-
GeneralError,
|
|
4
|
-
LineItemDraft,
|
|
5
|
-
Product,
|
|
6
|
-
ProductPagedQueryResponse,
|
|
7
|
-
ShoppingList,
|
|
8
|
-
ShoppingListAddLineItemAction,
|
|
9
|
-
ShoppingListChangeLineItemQuantityAction,
|
|
10
|
-
ShoppingListChangeNameAction,
|
|
11
|
-
ShoppingListDraft,
|
|
12
|
-
ShoppingListLineItem,
|
|
13
|
-
ShoppingListRemoveLineItemAction,
|
|
14
|
-
ShoppingListSetAnonymousIdAction,
|
|
15
|
-
ShoppingListSetCustomFieldAction,
|
|
16
|
-
ShoppingListSetCustomTypeAction,
|
|
17
|
-
ShoppingListSetCustomerAction,
|
|
18
|
-
ShoppingListSetDeleteDaysAfterLastModificationAction,
|
|
19
|
-
ShoppingListSetDescriptionAction,
|
|
20
|
-
ShoppingListSetKeyAction,
|
|
21
|
-
ShoppingListSetSlugAction,
|
|
22
|
-
ShoppingListSetStoreAction,
|
|
23
|
-
} from '@commercetools/platform-sdk'
|
|
24
|
-
import { v4 as uuidv4 } from 'uuid'
|
|
25
|
-
import { CommercetoolsError } from '../exceptions.js'
|
|
26
|
-
import { getBaseResourceProperties } from '../helpers.js'
|
|
27
|
-
import { Writable } from '../types.js'
|
|
28
|
-
import { AbstractResourceRepository, RepositoryContext } from './abstract.js'
|
|
29
|
-
import {
|
|
30
|
-
createCustomFields,
|
|
31
|
-
getReferenceFromResourceIdentifier,
|
|
32
|
-
getStoreKeyReference,
|
|
33
|
-
} from './helpers.js'
|
|
34
|
-
|
|
35
|
-
export class ShoppingListRepository extends AbstractResourceRepository<'shopping-list'> {
|
|
36
|
-
getTypeId() {
|
|
37
|
-
return 'shopping-list' as const
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
create(context: RepositoryContext, draft: ShoppingListDraft): ShoppingList {
|
|
41
|
-
const lineItems =
|
|
42
|
-
draft.lineItems?.map((draftLineItem) =>
|
|
43
|
-
this.draftLineItemtoLineItem(context.projectKey, draftLineItem)
|
|
44
|
-
) ?? []
|
|
45
|
-
|
|
46
|
-
const resource: ShoppingList = {
|
|
47
|
-
...getBaseResourceProperties(),
|
|
48
|
-
...draft,
|
|
49
|
-
custom: createCustomFields(
|
|
50
|
-
draft.custom,
|
|
51
|
-
context.projectKey,
|
|
52
|
-
this._storage
|
|
53
|
-
),
|
|
54
|
-
textLineItems: [],
|
|
55
|
-
lineItems,
|
|
56
|
-
customer: draft.customer
|
|
57
|
-
? getReferenceFromResourceIdentifier<CustomerReference>(
|
|
58
|
-
draft.customer,
|
|
59
|
-
context.projectKey,
|
|
60
|
-
this._storage
|
|
61
|
-
)
|
|
62
|
-
: undefined,
|
|
63
|
-
store: draft.store
|
|
64
|
-
? getStoreKeyReference(draft.store, context.projectKey, this._storage)
|
|
65
|
-
: undefined,
|
|
66
|
-
}
|
|
67
|
-
this.saveNew(context, resource)
|
|
68
|
-
return resource
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
actions = {
|
|
72
|
-
setKey: (
|
|
73
|
-
context: RepositoryContext,
|
|
74
|
-
resource: Writable<ShoppingList>,
|
|
75
|
-
{ key }: ShoppingListSetKeyAction
|
|
76
|
-
) => {
|
|
77
|
-
resource.key = key
|
|
78
|
-
},
|
|
79
|
-
setSlug: (
|
|
80
|
-
context: RepositoryContext,
|
|
81
|
-
resource: Writable<ShoppingList>,
|
|
82
|
-
{ slug }: ShoppingListSetSlugAction
|
|
83
|
-
) => {
|
|
84
|
-
resource.slug = slug
|
|
85
|
-
},
|
|
86
|
-
changeName: (
|
|
87
|
-
context: RepositoryContext,
|
|
88
|
-
resource: Writable<ShoppingList>,
|
|
89
|
-
{ name }: ShoppingListChangeNameAction
|
|
90
|
-
) => {
|
|
91
|
-
resource.name = name
|
|
92
|
-
},
|
|
93
|
-
setDescription: (
|
|
94
|
-
context: RepositoryContext,
|
|
95
|
-
resource: Writable<ShoppingList>,
|
|
96
|
-
{ description }: ShoppingListSetDescriptionAction
|
|
97
|
-
) => {
|
|
98
|
-
resource.description = description
|
|
99
|
-
},
|
|
100
|
-
setCustomer: (
|
|
101
|
-
context: RepositoryContext,
|
|
102
|
-
resource: Writable<ShoppingList>,
|
|
103
|
-
{ customer }: ShoppingListSetCustomerAction
|
|
104
|
-
) => {
|
|
105
|
-
if (customer?.key) {
|
|
106
|
-
throw new Error('set customer on shoppinglist by key not implemented')
|
|
107
|
-
}
|
|
108
|
-
if (customer?.id) {
|
|
109
|
-
resource.customer = { typeId: 'customer', id: customer.id }
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
setStore: (
|
|
113
|
-
context: RepositoryContext,
|
|
114
|
-
resource: Writable<ShoppingList>,
|
|
115
|
-
{ store }: ShoppingListSetStoreAction
|
|
116
|
-
) => {
|
|
117
|
-
if (store?.key) {
|
|
118
|
-
resource.store = { typeId: 'store', key: store.key }
|
|
119
|
-
}
|
|
120
|
-
if (store?.id) {
|
|
121
|
-
throw new Error('set store on shoppinglist by id not implemented')
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
setAnonymousId: (
|
|
125
|
-
context: RepositoryContext,
|
|
126
|
-
resource: Writable<ShoppingList>,
|
|
127
|
-
{ anonymousId }: ShoppingListSetAnonymousIdAction
|
|
128
|
-
) => {
|
|
129
|
-
resource.anonymousId = anonymousId
|
|
130
|
-
},
|
|
131
|
-
setCustomType: (
|
|
132
|
-
context: RepositoryContext,
|
|
133
|
-
resource: Writable<ShoppingList>,
|
|
134
|
-
{ type, fields }: ShoppingListSetCustomTypeAction
|
|
135
|
-
) => {
|
|
136
|
-
if (!type) {
|
|
137
|
-
resource.custom = undefined
|
|
138
|
-
} else {
|
|
139
|
-
const resolvedType = this._storage.getByResourceIdentifier(
|
|
140
|
-
context.projectKey,
|
|
141
|
-
type
|
|
142
|
-
)
|
|
143
|
-
if (!resolvedType) {
|
|
144
|
-
throw new Error(`Type ${type} not found`)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
resource.custom = {
|
|
148
|
-
type: {
|
|
149
|
-
typeId: 'type',
|
|
150
|
-
id: resolvedType.id,
|
|
151
|
-
},
|
|
152
|
-
fields: fields || {},
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
setCustomField: (
|
|
157
|
-
context: RepositoryContext,
|
|
158
|
-
resource: ShoppingList,
|
|
159
|
-
{ name, value }: ShoppingListSetCustomFieldAction
|
|
160
|
-
) => {
|
|
161
|
-
if (!resource.custom) {
|
|
162
|
-
throw new Error('Resource has no custom field')
|
|
163
|
-
}
|
|
164
|
-
resource.custom.fields[name] = value
|
|
165
|
-
},
|
|
166
|
-
setDeleteDaysAfterLastModification: (
|
|
167
|
-
context: RepositoryContext,
|
|
168
|
-
resource: Writable<ShoppingList>,
|
|
169
|
-
{
|
|
170
|
-
deleteDaysAfterLastModification,
|
|
171
|
-
}: ShoppingListSetDeleteDaysAfterLastModificationAction
|
|
172
|
-
) => {
|
|
173
|
-
resource.deleteDaysAfterLastModification = deleteDaysAfterLastModification
|
|
174
|
-
},
|
|
175
|
-
addLineItem: (
|
|
176
|
-
context: RepositoryContext,
|
|
177
|
-
resource: Writable<ShoppingList>,
|
|
178
|
-
{ productId, variantId, sku, quantity = 1 }: ShoppingListAddLineItemAction
|
|
179
|
-
) => {
|
|
180
|
-
let product: Product | null = null
|
|
181
|
-
|
|
182
|
-
if (productId) {
|
|
183
|
-
// Fetch product and variant by ID
|
|
184
|
-
product = this._storage.get(
|
|
185
|
-
context.projectKey,
|
|
186
|
-
'product',
|
|
187
|
-
productId,
|
|
188
|
-
{}
|
|
189
|
-
)
|
|
190
|
-
} else if (sku) {
|
|
191
|
-
// Fetch product and variant by SKU
|
|
192
|
-
const items = this._storage.query(context.projectKey, 'product', {
|
|
193
|
-
where: [
|
|
194
|
-
`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`,
|
|
195
|
-
],
|
|
196
|
-
}) as ProductPagedQueryResponse
|
|
197
|
-
|
|
198
|
-
if (items.count === 1) {
|
|
199
|
-
product = items.results[0]
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (!product) {
|
|
204
|
-
// Check if product is found
|
|
205
|
-
throw new CommercetoolsError<GeneralError>({
|
|
206
|
-
code: 'General',
|
|
207
|
-
message: sku
|
|
208
|
-
? `A product containing a variant with SKU '${sku}' not found.`
|
|
209
|
-
: `A product with ID '${productId}' not found.`,
|
|
210
|
-
})
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
let varId: number | undefined = variantId
|
|
214
|
-
if (sku) {
|
|
215
|
-
varId = [
|
|
216
|
-
product.masterData.current.masterVariant,
|
|
217
|
-
...product.masterData.current.variants,
|
|
218
|
-
].find((x) => x.sku === sku)?.id
|
|
219
|
-
}
|
|
220
|
-
if (!varId) {
|
|
221
|
-
varId = product.masterData.current.masterVariant.id
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const alreadyAdded = resource.lineItems.some(
|
|
225
|
-
(x) => x.productId === product?.id && x.variantId === varId
|
|
226
|
-
)
|
|
227
|
-
if (alreadyAdded) {
|
|
228
|
-
// increase quantity and update total price
|
|
229
|
-
resource.lineItems.forEach((x) => {
|
|
230
|
-
if (x.productId === product?.id && x.variantId === varId) {
|
|
231
|
-
x.quantity += quantity
|
|
232
|
-
}
|
|
233
|
-
})
|
|
234
|
-
} else {
|
|
235
|
-
// add line item
|
|
236
|
-
resource.lineItems.push({
|
|
237
|
-
addedAt: new Date().toISOString(),
|
|
238
|
-
id: uuidv4(),
|
|
239
|
-
productId: product.id,
|
|
240
|
-
productSlug: product.masterData.current.slug,
|
|
241
|
-
productType: product.productType,
|
|
242
|
-
name: product.masterData.current.name,
|
|
243
|
-
variantId: varId,
|
|
244
|
-
quantity,
|
|
245
|
-
})
|
|
246
|
-
}
|
|
247
|
-
},
|
|
248
|
-
removeLineItem: (
|
|
249
|
-
context: RepositoryContext,
|
|
250
|
-
resource: Writable<ShoppingList>,
|
|
251
|
-
{ lineItemId, quantity }: ShoppingListRemoveLineItemAction
|
|
252
|
-
) => {
|
|
253
|
-
const lineItem = resource.lineItems.find((x) => x.id === lineItemId)
|
|
254
|
-
if (!lineItem) {
|
|
255
|
-
// Check if product is found
|
|
256
|
-
throw new CommercetoolsError<GeneralError>({
|
|
257
|
-
code: 'General',
|
|
258
|
-
message: `A line item with ID '${lineItemId}' not found.`,
|
|
259
|
-
})
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
const shouldDelete = !quantity || quantity >= lineItem.quantity
|
|
263
|
-
if (shouldDelete) {
|
|
264
|
-
// delete line item
|
|
265
|
-
resource.lineItems = resource.lineItems.filter(
|
|
266
|
-
(x) => x.id !== lineItemId
|
|
267
|
-
)
|
|
268
|
-
} else {
|
|
269
|
-
// decrease quantity and update total price
|
|
270
|
-
resource.lineItems.forEach((x) => {
|
|
271
|
-
if (x.id === lineItemId && quantity) {
|
|
272
|
-
x.quantity -= quantity
|
|
273
|
-
}
|
|
274
|
-
})
|
|
275
|
-
}
|
|
276
|
-
},
|
|
277
|
-
changeLineItemQuantity: (
|
|
278
|
-
context: RepositoryContext,
|
|
279
|
-
resource: Writable<ShoppingList>,
|
|
280
|
-
{
|
|
281
|
-
lineItemId,
|
|
282
|
-
lineItemKey,
|
|
283
|
-
quantity,
|
|
284
|
-
}: ShoppingListChangeLineItemQuantityAction
|
|
285
|
-
) => {
|
|
286
|
-
let lineItem: Writable<ShoppingListLineItem> | undefined
|
|
287
|
-
|
|
288
|
-
if (lineItemId) {
|
|
289
|
-
lineItem = resource.lineItems.find((x) => x.id === lineItemId)
|
|
290
|
-
if (!lineItem) {
|
|
291
|
-
throw new CommercetoolsError<GeneralError>({
|
|
292
|
-
code: 'General',
|
|
293
|
-
message: `A line item with ID '${lineItemId}' not found.`,
|
|
294
|
-
})
|
|
295
|
-
}
|
|
296
|
-
} else if (lineItemKey) {
|
|
297
|
-
lineItem = resource.lineItems.find((x) => x.id === lineItemId)
|
|
298
|
-
if (!lineItem) {
|
|
299
|
-
throw new CommercetoolsError<GeneralError>({
|
|
300
|
-
code: 'General',
|
|
301
|
-
message: `A line item with Key '${lineItemKey}' not found.`,
|
|
302
|
-
})
|
|
303
|
-
}
|
|
304
|
-
} else {
|
|
305
|
-
throw new CommercetoolsError<GeneralError>({
|
|
306
|
-
code: 'General',
|
|
307
|
-
message: `Either lineItemid or lineItemKey needs to be provided.`,
|
|
308
|
-
})
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
if (quantity === 0) {
|
|
312
|
-
// delete line item
|
|
313
|
-
resource.lineItems = resource.lineItems.filter(
|
|
314
|
-
(x) => x.id !== lineItemId
|
|
315
|
-
)
|
|
316
|
-
} else {
|
|
317
|
-
resource.lineItems.forEach((x) => {
|
|
318
|
-
if (x.id === lineItemId && quantity) {
|
|
319
|
-
x.quantity = quantity
|
|
320
|
-
}
|
|
321
|
-
})
|
|
322
|
-
}
|
|
323
|
-
},
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
draftLineItemtoLineItem = (
|
|
327
|
-
projectKey: string,
|
|
328
|
-
draftLineItem: LineItemDraft
|
|
329
|
-
): ShoppingListLineItem => {
|
|
330
|
-
const { sku, productId, variantId } = draftLineItem
|
|
331
|
-
|
|
332
|
-
const lineItem: Writable<ShoppingListLineItem> = {
|
|
333
|
-
...getBaseResourceProperties(),
|
|
334
|
-
...draftLineItem,
|
|
335
|
-
addedAt: draftLineItem.addedAt ?? '',
|
|
336
|
-
productId: draftLineItem.productId ?? '',
|
|
337
|
-
name: {},
|
|
338
|
-
variantId,
|
|
339
|
-
quantity: draftLineItem.quantity ?? 1,
|
|
340
|
-
productType: { typeId: 'product-type', id: '' },
|
|
341
|
-
custom: createCustomFields(
|
|
342
|
-
draftLineItem.custom,
|
|
343
|
-
projectKey,
|
|
344
|
-
this._storage
|
|
345
|
-
),
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
if (productId && variantId) {
|
|
349
|
-
return lineItem
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (sku) {
|
|
353
|
-
const items = this._storage.query(projectKey, 'product', {
|
|
354
|
-
where: [
|
|
355
|
-
`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`,
|
|
356
|
-
],
|
|
357
|
-
}) as ProductPagedQueryResponse
|
|
358
|
-
|
|
359
|
-
if (items.count === 0) {
|
|
360
|
-
throw new Error(`Product with sku ${sku} not found`)
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
const product = items.results[0]
|
|
364
|
-
const allVariants = [
|
|
365
|
-
product.masterData.current.masterVariant,
|
|
366
|
-
...product.masterData.current.variants,
|
|
367
|
-
]
|
|
368
|
-
const variantId = allVariants.find((e) => e.sku === sku)?.id
|
|
369
|
-
lineItem.variantId = variantId
|
|
370
|
-
lineItem.productId = product.id
|
|
371
|
-
return lineItem
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
if (productId) {
|
|
375
|
-
const items = this._storage.query(projectKey, 'product', {
|
|
376
|
-
where: [`id="${productId}"`],
|
|
377
|
-
}) as ProductPagedQueryResponse
|
|
378
|
-
|
|
379
|
-
if (items.count === 0) {
|
|
380
|
-
throw new Error(`Product with id ${productId} not found`)
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
const variantId = items.results[0].masterData.current.masterVariant.id
|
|
384
|
-
lineItem.variantId = variantId
|
|
385
|
-
return lineItem
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
throw new Error(
|
|
389
|
-
`must provide either sku, productId or variantId for ShoppingListLineItem`
|
|
390
|
-
)
|
|
391
|
-
}
|
|
392
|
-
}
|