@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
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
GeneralError,
|
|
3
|
+
Product,
|
|
4
|
+
ProductPagedQueryResponse,
|
|
5
|
+
ShoppingList,
|
|
6
|
+
ShoppingListAddLineItemAction,
|
|
7
|
+
ShoppingListChangeLineItemQuantityAction,
|
|
8
|
+
ShoppingListChangeNameAction,
|
|
9
|
+
ShoppingListLineItem,
|
|
10
|
+
ShoppingListRemoveLineItemAction,
|
|
11
|
+
ShoppingListSetAnonymousIdAction,
|
|
12
|
+
ShoppingListSetCustomFieldAction,
|
|
13
|
+
ShoppingListSetCustomTypeAction,
|
|
14
|
+
ShoppingListSetCustomerAction,
|
|
15
|
+
ShoppingListSetDeleteDaysAfterLastModificationAction,
|
|
16
|
+
ShoppingListSetDescriptionAction,
|
|
17
|
+
ShoppingListSetKeyAction,
|
|
18
|
+
ShoppingListSetSlugAction,
|
|
19
|
+
ShoppingListSetStoreAction,
|
|
20
|
+
ShoppingListUpdateAction,
|
|
21
|
+
} from "@commercetools/platform-sdk";
|
|
22
|
+
import { v4 as uuidv4 } from "uuid";
|
|
23
|
+
import { CommercetoolsError } from "~src/exceptions";
|
|
24
|
+
import { Writable } from "../../types";
|
|
25
|
+
import {
|
|
26
|
+
AbstractUpdateHandler,
|
|
27
|
+
RepositoryContext,
|
|
28
|
+
UpdateHandlerInterface,
|
|
29
|
+
} from "../abstract";
|
|
30
|
+
|
|
31
|
+
export class ShoppingListUpdateHandler
|
|
32
|
+
extends AbstractUpdateHandler
|
|
33
|
+
implements
|
|
34
|
+
Partial<UpdateHandlerInterface<ShoppingList, ShoppingListUpdateAction>>
|
|
35
|
+
{
|
|
36
|
+
addLineItem(
|
|
37
|
+
context: RepositoryContext,
|
|
38
|
+
resource: Writable<ShoppingList>,
|
|
39
|
+
{ productId, variantId, sku, quantity = 1 }: ShoppingListAddLineItemAction,
|
|
40
|
+
) {
|
|
41
|
+
let product: Product | null = null;
|
|
42
|
+
|
|
43
|
+
if (productId) {
|
|
44
|
+
// Fetch product and variant by ID
|
|
45
|
+
product = this._storage.get(context.projectKey, "product", productId, {});
|
|
46
|
+
} else if (sku) {
|
|
47
|
+
// Fetch product and variant by SKU
|
|
48
|
+
const items = this._storage.query(context.projectKey, "product", {
|
|
49
|
+
where: [
|
|
50
|
+
`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`,
|
|
51
|
+
],
|
|
52
|
+
}) as ProductPagedQueryResponse;
|
|
53
|
+
|
|
54
|
+
if (items.count === 1) {
|
|
55
|
+
product = items.results[0];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!product) {
|
|
60
|
+
// Check if product is found
|
|
61
|
+
throw new CommercetoolsError<GeneralError>({
|
|
62
|
+
code: "General",
|
|
63
|
+
message: sku
|
|
64
|
+
? `A product containing a variant with SKU '${sku}' not found.`
|
|
65
|
+
: `A product with ID '${productId}' not found.`,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let varId: number | undefined = variantId;
|
|
70
|
+
if (sku) {
|
|
71
|
+
varId = [
|
|
72
|
+
product.masterData.current.masterVariant,
|
|
73
|
+
...product.masterData.current.variants,
|
|
74
|
+
].find((x) => x.sku === sku)?.id;
|
|
75
|
+
}
|
|
76
|
+
if (!varId) {
|
|
77
|
+
varId = product.masterData.current.masterVariant.id;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const alreadyAdded = resource.lineItems.some(
|
|
81
|
+
(x) => x.productId === product?.id && x.variantId === varId,
|
|
82
|
+
);
|
|
83
|
+
if (alreadyAdded) {
|
|
84
|
+
// increase quantity and update total price
|
|
85
|
+
resource.lineItems.forEach((x) => {
|
|
86
|
+
if (x.productId === product?.id && x.variantId === varId) {
|
|
87
|
+
x.quantity += quantity;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
} else {
|
|
91
|
+
// add line item
|
|
92
|
+
resource.lineItems.push({
|
|
93
|
+
addedAt: new Date().toISOString(),
|
|
94
|
+
id: uuidv4(),
|
|
95
|
+
productId: product.id,
|
|
96
|
+
productSlug: product.masterData.current.slug,
|
|
97
|
+
productType: product.productType,
|
|
98
|
+
name: product.masterData.current.name,
|
|
99
|
+
variantId: varId,
|
|
100
|
+
quantity,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
changeLineItemQuantity(
|
|
106
|
+
context: RepositoryContext,
|
|
107
|
+
resource: Writable<ShoppingList>,
|
|
108
|
+
{
|
|
109
|
+
lineItemId,
|
|
110
|
+
lineItemKey,
|
|
111
|
+
quantity,
|
|
112
|
+
}: ShoppingListChangeLineItemQuantityAction,
|
|
113
|
+
) {
|
|
114
|
+
let lineItem: Writable<ShoppingListLineItem> | undefined;
|
|
115
|
+
|
|
116
|
+
if (lineItemId) {
|
|
117
|
+
lineItem = resource.lineItems.find((x) => x.id === lineItemId);
|
|
118
|
+
if (!lineItem) {
|
|
119
|
+
throw new CommercetoolsError<GeneralError>({
|
|
120
|
+
code: "General",
|
|
121
|
+
message: `A line item with ID '${lineItemId}' not found.`,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
} else if (lineItemKey) {
|
|
125
|
+
lineItem = resource.lineItems.find((x) => x.id === lineItemId);
|
|
126
|
+
if (!lineItem) {
|
|
127
|
+
throw new CommercetoolsError<GeneralError>({
|
|
128
|
+
code: "General",
|
|
129
|
+
message: `A line item with Key '${lineItemKey}' not found.`,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
throw new CommercetoolsError<GeneralError>({
|
|
134
|
+
code: "General",
|
|
135
|
+
message: `Either lineItemid or lineItemKey needs to be provided.`,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (quantity === 0) {
|
|
140
|
+
// delete line item
|
|
141
|
+
resource.lineItems = resource.lineItems.filter(
|
|
142
|
+
(x) => x.id !== lineItemId,
|
|
143
|
+
);
|
|
144
|
+
} else {
|
|
145
|
+
resource.lineItems.forEach((x) => {
|
|
146
|
+
if (x.id === lineItemId && quantity) {
|
|
147
|
+
x.quantity = quantity;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
changeName(
|
|
154
|
+
context: RepositoryContext,
|
|
155
|
+
resource: Writable<ShoppingList>,
|
|
156
|
+
{ name }: ShoppingListChangeNameAction,
|
|
157
|
+
) {
|
|
158
|
+
resource.name = name;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
removeLineItem(
|
|
162
|
+
context: RepositoryContext,
|
|
163
|
+
resource: Writable<ShoppingList>,
|
|
164
|
+
{ lineItemId, quantity }: ShoppingListRemoveLineItemAction,
|
|
165
|
+
) {
|
|
166
|
+
const lineItem = resource.lineItems.find((x) => x.id === lineItemId);
|
|
167
|
+
if (!lineItem) {
|
|
168
|
+
// Check if product is found
|
|
169
|
+
throw new CommercetoolsError<GeneralError>({
|
|
170
|
+
code: "General",
|
|
171
|
+
message: `A line item with ID '${lineItemId}' not found.`,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const shouldDelete = !quantity || quantity >= lineItem.quantity;
|
|
176
|
+
if (shouldDelete) {
|
|
177
|
+
// delete line item
|
|
178
|
+
resource.lineItems = resource.lineItems.filter(
|
|
179
|
+
(x) => x.id !== lineItemId,
|
|
180
|
+
);
|
|
181
|
+
} else {
|
|
182
|
+
// decrease quantity and update total price
|
|
183
|
+
resource.lineItems.forEach((x) => {
|
|
184
|
+
if (x.id === lineItemId && quantity) {
|
|
185
|
+
x.quantity -= quantity;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
setAnonymousId(
|
|
192
|
+
context: RepositoryContext,
|
|
193
|
+
resource: Writable<ShoppingList>,
|
|
194
|
+
{ anonymousId }: ShoppingListSetAnonymousIdAction,
|
|
195
|
+
) {
|
|
196
|
+
resource.anonymousId = anonymousId;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
setCustomer(
|
|
200
|
+
context: RepositoryContext,
|
|
201
|
+
resource: Writable<ShoppingList>,
|
|
202
|
+
{ customer }: ShoppingListSetCustomerAction,
|
|
203
|
+
) {
|
|
204
|
+
if (customer?.key) {
|
|
205
|
+
throw new Error("set customer on shoppinglist by key not implemented");
|
|
206
|
+
}
|
|
207
|
+
if (customer?.id) {
|
|
208
|
+
resource.customer = { typeId: "customer", id: customer.id };
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
setCustomField(
|
|
213
|
+
context: RepositoryContext,
|
|
214
|
+
resource: ShoppingList,
|
|
215
|
+
{ name, value }: ShoppingListSetCustomFieldAction,
|
|
216
|
+
) {
|
|
217
|
+
if (!resource.custom) {
|
|
218
|
+
throw new Error("Resource has no custom field");
|
|
219
|
+
}
|
|
220
|
+
resource.custom.fields[name] = value;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
setCustomType(
|
|
224
|
+
context: RepositoryContext,
|
|
225
|
+
resource: Writable<ShoppingList>,
|
|
226
|
+
{ type, fields }: ShoppingListSetCustomTypeAction,
|
|
227
|
+
) {
|
|
228
|
+
if (!type) {
|
|
229
|
+
resource.custom = undefined;
|
|
230
|
+
} else {
|
|
231
|
+
const resolvedType = this._storage.getByResourceIdentifier(
|
|
232
|
+
context.projectKey,
|
|
233
|
+
type,
|
|
234
|
+
);
|
|
235
|
+
if (!resolvedType) {
|
|
236
|
+
throw new Error(`Type ${type} not found`);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
resource.custom = {
|
|
240
|
+
type: {
|
|
241
|
+
typeId: "type",
|
|
242
|
+
id: resolvedType.id,
|
|
243
|
+
},
|
|
244
|
+
fields: fields || {},
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
setDeleteDaysAfterLastModification(
|
|
250
|
+
context: RepositoryContext,
|
|
251
|
+
resource: Writable<ShoppingList>,
|
|
252
|
+
{
|
|
253
|
+
deleteDaysAfterLastModification,
|
|
254
|
+
}: ShoppingListSetDeleteDaysAfterLastModificationAction,
|
|
255
|
+
) {
|
|
256
|
+
resource.deleteDaysAfterLastModification = deleteDaysAfterLastModification;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
setDescription(
|
|
260
|
+
context: RepositoryContext,
|
|
261
|
+
resource: Writable<ShoppingList>,
|
|
262
|
+
{ description }: ShoppingListSetDescriptionAction,
|
|
263
|
+
) {
|
|
264
|
+
resource.description = description;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
setKey(
|
|
268
|
+
context: RepositoryContext,
|
|
269
|
+
resource: Writable<ShoppingList>,
|
|
270
|
+
{ key }: ShoppingListSetKeyAction,
|
|
271
|
+
) {
|
|
272
|
+
resource.key = key;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
setSlug(
|
|
276
|
+
context: RepositoryContext,
|
|
277
|
+
resource: Writable<ShoppingList>,
|
|
278
|
+
{ slug }: ShoppingListSetSlugAction,
|
|
279
|
+
) {
|
|
280
|
+
resource.slug = slug;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
setStore(
|
|
284
|
+
context: RepositoryContext,
|
|
285
|
+
resource: Writable<ShoppingList>,
|
|
286
|
+
{ store }: ShoppingListSetStoreAction,
|
|
287
|
+
) {
|
|
288
|
+
if (store?.key) {
|
|
289
|
+
resource.store = { typeId: "store", key: store.key };
|
|
290
|
+
}
|
|
291
|
+
if (store?.id) {
|
|
292
|
+
throw new Error("set store on shoppinglist by id not implemented");
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CustomerReference,
|
|
3
|
+
LineItemDraft,
|
|
4
|
+
ProductPagedQueryResponse,
|
|
5
|
+
ShoppingList,
|
|
6
|
+
ShoppingListDraft,
|
|
7
|
+
ShoppingListLineItem,
|
|
8
|
+
} from "@commercetools/platform-sdk";
|
|
9
|
+
import { getBaseResourceProperties } from "../../helpers";
|
|
10
|
+
import { AbstractStorage } from "../../storage/abstract";
|
|
11
|
+
import { Writable } from "../../types";
|
|
12
|
+
import { AbstractResourceRepository, RepositoryContext } from "../abstract";
|
|
13
|
+
import {
|
|
14
|
+
createCustomFields,
|
|
15
|
+
getReferenceFromResourceIdentifier,
|
|
16
|
+
getStoreKeyReference,
|
|
17
|
+
} from "../helpers";
|
|
18
|
+
import { ShoppingListUpdateHandler } from "./actions";
|
|
19
|
+
|
|
20
|
+
export class ShoppingListRepository extends AbstractResourceRepository<"shopping-list"> {
|
|
21
|
+
constructor(storage: AbstractStorage) {
|
|
22
|
+
super("shopping-list", storage);
|
|
23
|
+
this.actions = new ShoppingListUpdateHandler(storage);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
create(context: RepositoryContext, draft: ShoppingListDraft): ShoppingList {
|
|
27
|
+
const lineItems =
|
|
28
|
+
draft.lineItems?.map((draftLineItem) =>
|
|
29
|
+
this.draftLineItemtoLineItem(context.projectKey, draftLineItem),
|
|
30
|
+
) ?? [];
|
|
31
|
+
|
|
32
|
+
const resource: ShoppingList = {
|
|
33
|
+
...getBaseResourceProperties(),
|
|
34
|
+
...draft,
|
|
35
|
+
custom: createCustomFields(
|
|
36
|
+
draft.custom,
|
|
37
|
+
context.projectKey,
|
|
38
|
+
this._storage,
|
|
39
|
+
),
|
|
40
|
+
textLineItems: [],
|
|
41
|
+
lineItems,
|
|
42
|
+
customer: draft.customer
|
|
43
|
+
? getReferenceFromResourceIdentifier<CustomerReference>(
|
|
44
|
+
draft.customer,
|
|
45
|
+
context.projectKey,
|
|
46
|
+
this._storage,
|
|
47
|
+
)
|
|
48
|
+
: undefined,
|
|
49
|
+
store: draft.store
|
|
50
|
+
? getStoreKeyReference(draft.store, context.projectKey, this._storage)
|
|
51
|
+
: undefined,
|
|
52
|
+
};
|
|
53
|
+
return this.saveNew(context, resource);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
draftLineItemtoLineItem = (
|
|
57
|
+
projectKey: string,
|
|
58
|
+
draftLineItem: LineItemDraft,
|
|
59
|
+
): ShoppingListLineItem => {
|
|
60
|
+
const { sku, productId, variantId } = draftLineItem;
|
|
61
|
+
|
|
62
|
+
const lineItem: Writable<ShoppingListLineItem> = {
|
|
63
|
+
...getBaseResourceProperties(),
|
|
64
|
+
...draftLineItem,
|
|
65
|
+
addedAt: draftLineItem.addedAt ?? "",
|
|
66
|
+
productId: draftLineItem.productId ?? "",
|
|
67
|
+
name: {},
|
|
68
|
+
variantId,
|
|
69
|
+
quantity: draftLineItem.quantity ?? 1,
|
|
70
|
+
productType: { typeId: "product-type", id: "" },
|
|
71
|
+
custom: createCustomFields(
|
|
72
|
+
draftLineItem.custom,
|
|
73
|
+
projectKey,
|
|
74
|
+
this._storage,
|
|
75
|
+
),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
if (productId && variantId) {
|
|
79
|
+
return lineItem;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (sku) {
|
|
83
|
+
const items = this._storage.query(projectKey, "product", {
|
|
84
|
+
where: [
|
|
85
|
+
`masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`,
|
|
86
|
+
],
|
|
87
|
+
}) as ProductPagedQueryResponse;
|
|
88
|
+
|
|
89
|
+
if (items.count === 0) {
|
|
90
|
+
throw new Error(`Product with sku ${sku} not found`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const product = items.results[0];
|
|
94
|
+
const allVariants = [
|
|
95
|
+
product.masterData.current.masterVariant,
|
|
96
|
+
...product.masterData.current.variants,
|
|
97
|
+
];
|
|
98
|
+
const variantId = allVariants.find((e) => e.sku === sku)?.id;
|
|
99
|
+
lineItem.variantId = variantId;
|
|
100
|
+
lineItem.productId = product.id;
|
|
101
|
+
return lineItem;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (productId) {
|
|
105
|
+
const items = this._storage.query(projectKey, "product", {
|
|
106
|
+
where: [`id="${productId}"`],
|
|
107
|
+
}) as ProductPagedQueryResponse;
|
|
108
|
+
|
|
109
|
+
if (items.count === 0) {
|
|
110
|
+
throw new Error(`Product with id ${productId} not found`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const variantId = items.results[0].masterData.current.masterVariant.id;
|
|
114
|
+
lineItem.variantId = variantId;
|
|
115
|
+
return lineItem;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
throw new Error(
|
|
119
|
+
`must provide either sku, productId or variantId for ShoppingListLineItem`,
|
|
120
|
+
);
|
|
121
|
+
};
|
|
122
|
+
}
|
|
@@ -1,29 +1,16 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
Quote,
|
|
3
2
|
StagedQuote,
|
|
4
3
|
StagedQuoteDraft,
|
|
5
|
-
|
|
6
|
-
} from
|
|
7
|
-
import
|
|
8
|
-
import { AbstractResourceRepository, RepositoryContext } from './abstract.js'
|
|
4
|
+
} from "@commercetools/platform-sdk";
|
|
5
|
+
import { AbstractStorage } from "~src/storage";
|
|
6
|
+
import { AbstractResourceRepository, RepositoryContext } from "./abstract";
|
|
9
7
|
|
|
10
|
-
export class StagedQuoteRepository extends AbstractResourceRepository<
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
export class StagedQuoteRepository extends AbstractResourceRepository<"staged-quote"> {
|
|
9
|
+
constructor(storage: AbstractStorage) {
|
|
10
|
+
super("staged-quote", storage);
|
|
13
11
|
}
|
|
14
12
|
|
|
15
13
|
create(context: RepositoryContext, draft: StagedQuoteDraft): StagedQuote {
|
|
16
|
-
throw new Error(
|
|
14
|
+
throw new Error("not implemented");
|
|
17
15
|
}
|
|
18
|
-
|
|
19
|
-
actions: Partial<
|
|
20
|
-
Record<
|
|
21
|
-
StagedQuoteUpdateAction['action'],
|
|
22
|
-
(
|
|
23
|
-
context: RepositoryContext,
|
|
24
|
-
resource: Writable<Quote>,
|
|
25
|
-
action: any
|
|
26
|
-
) => void
|
|
27
|
-
>
|
|
28
|
-
> = {}
|
|
29
16
|
}
|
|
@@ -7,20 +7,28 @@ import type {
|
|
|
7
7
|
StandalonePriceChangeValueAction,
|
|
8
8
|
StandalonePriceDraft,
|
|
9
9
|
StandalonePriceSetDiscountedPriceAction,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
import
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
10
|
+
StandalonePriceUpdateAction,
|
|
11
|
+
} from "@commercetools/platform-sdk";
|
|
12
|
+
import { getBaseResourceProperties } from "../helpers";
|
|
13
|
+
import { AbstractStorage } from "../storage/abstract";
|
|
14
|
+
import type { Writable } from "../types";
|
|
15
|
+
import {
|
|
16
|
+
AbstractResourceRepository,
|
|
17
|
+
AbstractUpdateHandler,
|
|
18
|
+
RepositoryContext,
|
|
19
|
+
UpdateHandlerInterface,
|
|
20
|
+
} from "./abstract";
|
|
21
|
+
import { createTypedMoney } from "./helpers";
|
|
15
22
|
|
|
16
|
-
export class StandAlonePriceRepository extends AbstractResourceRepository<
|
|
17
|
-
|
|
18
|
-
|
|
23
|
+
export class StandAlonePriceRepository extends AbstractResourceRepository<"standalone-price"> {
|
|
24
|
+
constructor(storage: AbstractStorage) {
|
|
25
|
+
super("standalone-price", storage);
|
|
26
|
+
this.actions = new StandalonePriceUpdateHandler(this._storage);
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
create(
|
|
22
30
|
context: RepositoryContext,
|
|
23
|
-
draft: StandalonePriceDraft
|
|
31
|
+
draft: StandalonePriceDraft,
|
|
24
32
|
): StandalonePrice {
|
|
25
33
|
const resource: StandalonePrice = {
|
|
26
34
|
...getBaseResourceProperties(),
|
|
@@ -29,57 +37,62 @@ export class StandAlonePriceRepository extends AbstractResourceRepository<'stand
|
|
|
29
37
|
value: createTypedMoney(draft.value),
|
|
30
38
|
country: draft.country,
|
|
31
39
|
discounted: draft.discounted
|
|
32
|
-
?
|
|
40
|
+
? transformDiscountDraft(draft.discounted)
|
|
33
41
|
: undefined,
|
|
34
42
|
channel: draft.channel?.id
|
|
35
43
|
? this.transformChannelReferenceDraft(draft.channel)
|
|
36
44
|
: undefined,
|
|
37
45
|
validFrom: draft.validFrom,
|
|
38
46
|
validUntil: draft.validUntil,
|
|
39
|
-
}
|
|
40
|
-
this.saveNew(context, resource)
|
|
41
|
-
return resource
|
|
47
|
+
};
|
|
48
|
+
return this.saveNew(context, resource);
|
|
42
49
|
}
|
|
43
50
|
|
|
44
51
|
transformChannelReferenceDraft(
|
|
45
|
-
channel: ChannelResourceIdentifier
|
|
52
|
+
channel: ChannelResourceIdentifier,
|
|
46
53
|
): ChannelReference {
|
|
47
54
|
return {
|
|
48
55
|
typeId: channel.typeId,
|
|
49
56
|
id: channel.id as string,
|
|
50
|
-
}
|
|
57
|
+
};
|
|
51
58
|
}
|
|
59
|
+
}
|
|
52
60
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
const transformDiscountDraft = (discounted: DiscountedPriceDraft) => ({
|
|
62
|
+
value: createTypedMoney(discounted.value),
|
|
63
|
+
discount: discounted.discount,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
class StandalonePriceUpdateHandler
|
|
67
|
+
extends AbstractUpdateHandler
|
|
68
|
+
implements
|
|
69
|
+
Partial<
|
|
70
|
+
UpdateHandlerInterface<StandalonePrice, StandalonePriceUpdateAction>
|
|
71
|
+
>
|
|
72
|
+
{
|
|
73
|
+
changeValue(
|
|
74
|
+
context: RepositoryContext,
|
|
75
|
+
resource: Writable<StandalonePrice>,
|
|
76
|
+
action: StandalonePriceChangeValueAction,
|
|
77
|
+
) {
|
|
78
|
+
resource.value = createTypedMoney(action.value);
|
|
58
79
|
}
|
|
59
80
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
context: RepositoryContext,
|
|
77
|
-
resource: Writable<StandalonePrice>,
|
|
78
|
-
action: StandalonePriceSetDiscountedPriceAction
|
|
79
|
-
) => {
|
|
80
|
-
resource.discounted = action.discounted
|
|
81
|
-
? this.transformDiscountDraft(action.discounted)
|
|
82
|
-
: undefined
|
|
83
|
-
},
|
|
81
|
+
setActive(
|
|
82
|
+
context: RepositoryContext,
|
|
83
|
+
resource: Writable<StandalonePrice>,
|
|
84
|
+
action: StandalonePriceChangeActiveAction,
|
|
85
|
+
) {
|
|
86
|
+
resource.active = action.active;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
setDiscountedPrice(
|
|
90
|
+
context: RepositoryContext,
|
|
91
|
+
resource: Writable<StandalonePrice>,
|
|
92
|
+
action: StandalonePriceSetDiscountedPriceAction,
|
|
93
|
+
) {
|
|
94
|
+
resource.discounted = action.discounted
|
|
95
|
+
? transformDiscountDraft(action.discounted)
|
|
96
|
+
: undefined;
|
|
84
97
|
}
|
|
85
98
|
}
|