@labdigital/commercetools-mock 2.17.1 → 2.18.1
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 +4351 -4099
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +266 -413
- package/dist/index.d.ts +266 -413
- package/dist/index.js +4351 -4099
- package/dist/index.js.map +1 -1
- package/package.json +47 -47
- 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 +86 -86
- package/src/oauth/server.ts +158 -144
- package/src/oauth/store.ts +44 -43
- 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/schemas/update-request.ts +12 -0
- package/src/server.ts +9 -9
- package/src/services/abstract.ts +85 -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 +23 -36
- 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 +66 -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 +29 -24
- package/src/services/project.ts +22 -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/validate.ts +18 -0
- 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,44 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
InventoryEntry,
|
|
3
|
+
InventoryEntryDraft,
|
|
4
|
+
} from "@commercetools/platform-sdk";
|
|
5
|
+
import { getBaseResourceProperties } from "~src/helpers";
|
|
6
|
+
import { AbstractStorage } from "~src/storage/abstract";
|
|
7
|
+
import {
|
|
8
|
+
AbstractResourceRepository,
|
|
9
|
+
type RepositoryContext,
|
|
10
|
+
} from "../abstract";
|
|
11
|
+
import { createCustomFields } from "../helpers";
|
|
12
|
+
import { InventoryEntryUpdateHandler } from "./actions";
|
|
13
|
+
|
|
14
|
+
export class InventoryEntryRepository extends AbstractResourceRepository<"inventory-entry"> {
|
|
15
|
+
constructor(storage: AbstractStorage) {
|
|
16
|
+
super("inventory-entry", storage);
|
|
17
|
+
this.actions = new InventoryEntryUpdateHandler(storage);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
create(
|
|
21
|
+
context: RepositoryContext,
|
|
22
|
+
draft: InventoryEntryDraft,
|
|
23
|
+
): InventoryEntry {
|
|
24
|
+
const resource: InventoryEntry = {
|
|
25
|
+
...getBaseResourceProperties(),
|
|
26
|
+
sku: draft.sku,
|
|
27
|
+
quantityOnStock: draft.quantityOnStock,
|
|
28
|
+
availableQuantity: draft.quantityOnStock,
|
|
29
|
+
expectedDelivery: draft.expectedDelivery,
|
|
30
|
+
restockableInDays: draft.restockableInDays,
|
|
31
|
+
supplyChannel: {
|
|
32
|
+
...draft.supplyChannel,
|
|
33
|
+
typeId: "channel",
|
|
34
|
+
id: draft.supplyChannel?.id ?? "",
|
|
35
|
+
},
|
|
36
|
+
custom: createCustomFields(
|
|
37
|
+
draft.custom,
|
|
38
|
+
context.projectKey,
|
|
39
|
+
this._storage,
|
|
40
|
+
),
|
|
41
|
+
};
|
|
42
|
+
return this.saveNew(context, resource);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Customer,
|
|
3
|
+
InvalidCurrentPasswordError,
|
|
4
|
+
MyCustomerChangePassword,
|
|
5
|
+
ResourceNotFoundError,
|
|
6
|
+
type MyCustomerResetPassword,
|
|
7
|
+
} from "@commercetools/platform-sdk";
|
|
8
|
+
import { CommercetoolsError } from "~src/exceptions";
|
|
9
|
+
import { hashPassword, validatePasswordResetToken } from "../lib/password";
|
|
10
|
+
import { Writable } from "../types";
|
|
11
|
+
import { type RepositoryContext } from "./abstract";
|
|
12
|
+
import { CustomerRepository } from "./customer";
|
|
13
|
+
|
|
14
|
+
export class MyCustomerRepository extends CustomerRepository {
|
|
15
|
+
changePassword(
|
|
16
|
+
context: RepositoryContext,
|
|
17
|
+
changePassword: MyCustomerChangePassword,
|
|
18
|
+
) {
|
|
19
|
+
const { currentPassword, newPassword } = changePassword;
|
|
20
|
+
const encodedPassword = hashPassword(currentPassword);
|
|
21
|
+
|
|
22
|
+
const result = this._storage.query(context.projectKey, "customer", {
|
|
23
|
+
where: [`password = "${encodedPassword}"`],
|
|
24
|
+
});
|
|
25
|
+
if (result.count === 0) {
|
|
26
|
+
throw new CommercetoolsError<InvalidCurrentPasswordError>({
|
|
27
|
+
code: "InvalidCurrentPassword",
|
|
28
|
+
message: "Account with the given credentials not found.",
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const customer = result.results[0] as Writable<Customer>;
|
|
33
|
+
if (customer.password !== hashPassword(currentPassword)) {
|
|
34
|
+
throw new CommercetoolsError<InvalidCurrentPasswordError>({
|
|
35
|
+
code: "InvalidCurrentPassword",
|
|
36
|
+
message: "The current password is invalid.",
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
customer.password = hashPassword(newPassword);
|
|
41
|
+
customer.version += 1;
|
|
42
|
+
|
|
43
|
+
// Update storage
|
|
44
|
+
this._storage.add(context.projectKey, "customer", customer);
|
|
45
|
+
return customer;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
deleteMe(context: RepositoryContext): Customer | undefined {
|
|
49
|
+
// grab the first customer you can find for now. In the future we should
|
|
50
|
+
// use the customer id from the scope of the token
|
|
51
|
+
const results = this._storage.query(
|
|
52
|
+
context.projectKey,
|
|
53
|
+
this.getTypeId(),
|
|
54
|
+
{},
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
if (results.count > 0) {
|
|
58
|
+
return this.delete(context, results.results[0].id) as Customer;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
getMe(context: RepositoryContext): Customer | undefined {
|
|
65
|
+
// grab the first customer you can find for now. In the future we should
|
|
66
|
+
// use the customer id from the scope of the token
|
|
67
|
+
const results = this._storage.query(
|
|
68
|
+
context.projectKey,
|
|
69
|
+
this.getTypeId(),
|
|
70
|
+
{},
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
if (results.count > 0) {
|
|
74
|
+
return results.results[0] as Customer;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
resetPassword(
|
|
81
|
+
context: RepositoryContext,
|
|
82
|
+
resetPassword: MyCustomerResetPassword,
|
|
83
|
+
) {
|
|
84
|
+
const { newPassword, tokenValue } = resetPassword;
|
|
85
|
+
|
|
86
|
+
const customerId = validatePasswordResetToken(tokenValue);
|
|
87
|
+
if (!customerId) {
|
|
88
|
+
throw new CommercetoolsError<ResourceNotFoundError>({
|
|
89
|
+
code: "ResourceNotFound",
|
|
90
|
+
message: `The Customer with ID 'Token(${tokenValue})' was not found.`,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const customer = this._storage.get(
|
|
95
|
+
context.projectKey,
|
|
96
|
+
"customer",
|
|
97
|
+
customerId,
|
|
98
|
+
) as Writable<Customer> | undefined;
|
|
99
|
+
|
|
100
|
+
if (!customer) {
|
|
101
|
+
throw new CommercetoolsError<ResourceNotFoundError>({
|
|
102
|
+
code: "ResourceNotFound",
|
|
103
|
+
message: `The Customer with ID 'Token(${tokenValue})' was not found.`,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
customer.password = hashPassword(newPassword);
|
|
108
|
+
customer.version += 1;
|
|
109
|
+
|
|
110
|
+
// Update storage
|
|
111
|
+
this._storage.add(context.projectKey, "customer", customer);
|
|
112
|
+
return customer;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -2,18 +2,18 @@ import type {
|
|
|
2
2
|
CartReference,
|
|
3
3
|
MyOrderFromCartDraft,
|
|
4
4
|
Order,
|
|
5
|
-
} from
|
|
6
|
-
import assert from
|
|
7
|
-
import { type RepositoryContext } from
|
|
8
|
-
import { OrderRepository } from
|
|
5
|
+
} from "@commercetools/platform-sdk";
|
|
6
|
+
import assert from "assert";
|
|
7
|
+
import { type RepositoryContext } from "./abstract";
|
|
8
|
+
import { OrderRepository } from "./order";
|
|
9
9
|
|
|
10
10
|
export class MyOrderRepository extends OrderRepository {
|
|
11
11
|
create(context: RepositoryContext, draft: MyOrderFromCartDraft): Order {
|
|
12
|
-
assert(draft.id,
|
|
12
|
+
assert(draft.id, "draft.id is missing");
|
|
13
13
|
const cartIdentifier = {
|
|
14
14
|
id: draft.id,
|
|
15
|
-
typeId:
|
|
16
|
-
} as CartReference
|
|
17
|
-
return this.createFromCart(context, cartIdentifier)
|
|
15
|
+
typeId: "cart",
|
|
16
|
+
} as CartReference;
|
|
17
|
+
return this.createFromCart(context, cartIdentifier);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CustomLineItemReturnItem,
|
|
3
|
+
LineItemReturnItem,
|
|
4
|
+
Order,
|
|
5
|
+
OrderAddPaymentAction,
|
|
6
|
+
OrderAddReturnInfoAction,
|
|
7
|
+
OrderChangeOrderStateAction,
|
|
8
|
+
OrderChangePaymentStateAction,
|
|
9
|
+
OrderSetBillingAddressAction,
|
|
10
|
+
OrderSetCustomFieldAction,
|
|
11
|
+
OrderSetCustomTypeAction,
|
|
12
|
+
OrderSetCustomerEmailAction,
|
|
13
|
+
OrderSetLocaleAction,
|
|
14
|
+
OrderSetOrderNumberAction,
|
|
15
|
+
OrderSetShippingAddressAction,
|
|
16
|
+
OrderSetStoreAction,
|
|
17
|
+
OrderTransitionStateAction,
|
|
18
|
+
OrderUpdateAction,
|
|
19
|
+
OrderUpdateSyncInfoAction,
|
|
20
|
+
ReturnInfo,
|
|
21
|
+
State,
|
|
22
|
+
Store,
|
|
23
|
+
SyncInfo,
|
|
24
|
+
} from "@commercetools/platform-sdk";
|
|
25
|
+
import { getBaseResourceProperties } from "~src/helpers";
|
|
26
|
+
import type { Writable } from "~src/types";
|
|
27
|
+
import {
|
|
28
|
+
AbstractUpdateHandler,
|
|
29
|
+
RepositoryContext,
|
|
30
|
+
UpdateHandlerInterface,
|
|
31
|
+
} from "../abstract";
|
|
32
|
+
import { createAddress } from "../helpers";
|
|
33
|
+
|
|
34
|
+
export class OrderUpdateHandler
|
|
35
|
+
extends AbstractUpdateHandler
|
|
36
|
+
implements Partial<UpdateHandlerInterface<Order, OrderUpdateAction>>
|
|
37
|
+
{
|
|
38
|
+
addPayment(
|
|
39
|
+
context: RepositoryContext,
|
|
40
|
+
resource: Writable<Order>,
|
|
41
|
+
{ payment }: OrderAddPaymentAction,
|
|
42
|
+
) {
|
|
43
|
+
const resolvedPayment = this._storage.getByResourceIdentifier(
|
|
44
|
+
context.projectKey,
|
|
45
|
+
payment,
|
|
46
|
+
);
|
|
47
|
+
if (!resolvedPayment) {
|
|
48
|
+
throw new Error(`Payment ${payment.id} not found`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!resource.paymentInfo) {
|
|
52
|
+
resource.paymentInfo = {
|
|
53
|
+
payments: [],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
resource.paymentInfo.payments.push({
|
|
58
|
+
typeId: "payment",
|
|
59
|
+
id: payment.id!,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
addReturnInfo(
|
|
64
|
+
context: RepositoryContext,
|
|
65
|
+
resource: Writable<Order>,
|
|
66
|
+
info: OrderAddReturnInfoAction,
|
|
67
|
+
) {
|
|
68
|
+
if (!resource.returnInfo) {
|
|
69
|
+
resource.returnInfo = [];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const resolved: ReturnInfo = {
|
|
73
|
+
items: info.items.map((item) => {
|
|
74
|
+
const common = {
|
|
75
|
+
...getBaseResourceProperties(),
|
|
76
|
+
quantity: item.quantity,
|
|
77
|
+
paymentState: "Initial",
|
|
78
|
+
shipmentState: "Initial",
|
|
79
|
+
comment: item.comment,
|
|
80
|
+
};
|
|
81
|
+
if (item.customLineItemId) {
|
|
82
|
+
return {
|
|
83
|
+
...common,
|
|
84
|
+
type: "CustomLineItemReturnItem",
|
|
85
|
+
customLineItemId: item.customLineItemId,
|
|
86
|
+
} as CustomLineItemReturnItem;
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
...common,
|
|
90
|
+
type: "LineItemReturnItem",
|
|
91
|
+
lineItemId: item.customLineItemId || item.lineItemId,
|
|
92
|
+
} as LineItemReturnItem;
|
|
93
|
+
}),
|
|
94
|
+
returnTrackingId: info.returnTrackingId,
|
|
95
|
+
returnDate: info.returnDate,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
resource.returnInfo.push(resolved);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
changeOrderState(
|
|
102
|
+
context: RepositoryContext,
|
|
103
|
+
resource: Writable<Order>,
|
|
104
|
+
{ orderState }: OrderChangeOrderStateAction,
|
|
105
|
+
) {
|
|
106
|
+
resource.orderState = orderState;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
changePaymentState(
|
|
110
|
+
context: RepositoryContext,
|
|
111
|
+
resource: Writable<Order>,
|
|
112
|
+
{ paymentState }: OrderChangePaymentStateAction,
|
|
113
|
+
) {
|
|
114
|
+
resource.paymentState = paymentState;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
setBillingAddress(
|
|
118
|
+
context: RepositoryContext,
|
|
119
|
+
resource: Writable<Order>,
|
|
120
|
+
{ address }: OrderSetBillingAddressAction,
|
|
121
|
+
) {
|
|
122
|
+
resource.billingAddress = createAddress(
|
|
123
|
+
address,
|
|
124
|
+
context.projectKey,
|
|
125
|
+
this._storage,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
setCustomerEmail(
|
|
130
|
+
context: RepositoryContext,
|
|
131
|
+
resource: Writable<Order>,
|
|
132
|
+
{ email }: OrderSetCustomerEmailAction,
|
|
133
|
+
) {
|
|
134
|
+
resource.customerEmail = email;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
setCustomField(
|
|
138
|
+
context: RepositoryContext,
|
|
139
|
+
resource: Order,
|
|
140
|
+
{ name, value }: OrderSetCustomFieldAction,
|
|
141
|
+
) {
|
|
142
|
+
if (!resource.custom) {
|
|
143
|
+
throw new Error("Resource has no custom field");
|
|
144
|
+
}
|
|
145
|
+
resource.custom.fields[name] = value;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
setCustomType(
|
|
149
|
+
context: RepositoryContext,
|
|
150
|
+
resource: Writable<Order>,
|
|
151
|
+
{ type, fields }: OrderSetCustomTypeAction,
|
|
152
|
+
) {
|
|
153
|
+
if (!type) {
|
|
154
|
+
resource.custom = undefined;
|
|
155
|
+
} else {
|
|
156
|
+
const resolvedType = this._storage.getByResourceIdentifier(
|
|
157
|
+
context.projectKey,
|
|
158
|
+
type,
|
|
159
|
+
);
|
|
160
|
+
if (!resolvedType) {
|
|
161
|
+
throw new Error(`Type ${type} not found`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
resource.custom = {
|
|
165
|
+
type: {
|
|
166
|
+
typeId: "type",
|
|
167
|
+
id: resolvedType.id,
|
|
168
|
+
},
|
|
169
|
+
fields: fields || {},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
setLocale(
|
|
175
|
+
context: RepositoryContext,
|
|
176
|
+
resource: Writable<Order>,
|
|
177
|
+
{ locale }: OrderSetLocaleAction,
|
|
178
|
+
) {
|
|
179
|
+
resource.locale = locale;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
setOrderNumber(
|
|
183
|
+
context: RepositoryContext,
|
|
184
|
+
resource: Writable<Order>,
|
|
185
|
+
{ orderNumber }: OrderSetOrderNumberAction,
|
|
186
|
+
) {
|
|
187
|
+
resource.orderNumber = orderNumber;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
setShippingAddress(
|
|
191
|
+
context: RepositoryContext,
|
|
192
|
+
resource: Writable<Order>,
|
|
193
|
+
{ address }: OrderSetShippingAddressAction,
|
|
194
|
+
) {
|
|
195
|
+
resource.shippingAddress = createAddress(
|
|
196
|
+
address,
|
|
197
|
+
context.projectKey,
|
|
198
|
+
this._storage,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
setStore(
|
|
203
|
+
context: RepositoryContext,
|
|
204
|
+
resource: Writable<Order>,
|
|
205
|
+
{ store }: OrderSetStoreAction,
|
|
206
|
+
) {
|
|
207
|
+
if (!store) return;
|
|
208
|
+
const resolvedType = this._storage.getByResourceIdentifier(
|
|
209
|
+
context.projectKey,
|
|
210
|
+
store,
|
|
211
|
+
);
|
|
212
|
+
if (!resolvedType) {
|
|
213
|
+
throw new Error(`No store found with key=${store.key}`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const storeReference = resolvedType as Store;
|
|
217
|
+
resource.store = {
|
|
218
|
+
typeId: "store",
|
|
219
|
+
key: storeReference.key,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
transitionState(
|
|
224
|
+
context: RepositoryContext,
|
|
225
|
+
resource: Writable<Order>,
|
|
226
|
+
{ state }: OrderTransitionStateAction,
|
|
227
|
+
) {
|
|
228
|
+
const resolvedType = this._storage.getByResourceIdentifier(
|
|
229
|
+
context.projectKey,
|
|
230
|
+
state,
|
|
231
|
+
) as State | null;
|
|
232
|
+
|
|
233
|
+
if (!resolvedType) {
|
|
234
|
+
throw new Error(
|
|
235
|
+
`No state found with key=${state.key} or id=${state.key}`,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
resource.state = {
|
|
240
|
+
typeId: "state",
|
|
241
|
+
id: resolvedType.id,
|
|
242
|
+
obj: { ...resolvedType, key: state.key ?? "" },
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
updateSyncInfo(
|
|
247
|
+
context: RepositoryContext,
|
|
248
|
+
resource: Writable<Order>,
|
|
249
|
+
{ channel, externalId, syncedAt }: OrderUpdateSyncInfoAction,
|
|
250
|
+
) {
|
|
251
|
+
if (!channel) return;
|
|
252
|
+
const resolvedType = this._storage.getByResourceIdentifier(
|
|
253
|
+
context.projectKey,
|
|
254
|
+
channel,
|
|
255
|
+
);
|
|
256
|
+
if (!resolvedType) {
|
|
257
|
+
throw new Error(`Channel ${channel} not found`);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const syncData: SyncInfo = {
|
|
261
|
+
channel: {
|
|
262
|
+
typeId: "channel",
|
|
263
|
+
id: resolvedType.id,
|
|
264
|
+
},
|
|
265
|
+
externalId,
|
|
266
|
+
syncedAt: syncedAt ?? new Date().toISOString(),
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
if (!resource.syncInfo?.length) {
|
|
270
|
+
resource.syncInfo = [syncData];
|
|
271
|
+
} else {
|
|
272
|
+
const lastSyncInfo = resource.syncInfo[resource.syncInfo.length - 1];
|
|
273
|
+
if (
|
|
274
|
+
lastSyncInfo.channel.id !== syncData.channel.id ||
|
|
275
|
+
lastSyncInfo.externalId !== syncData.externalId
|
|
276
|
+
) {
|
|
277
|
+
resource.syncInfo.push(syncData);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|