@labdigital/commercetools-mock 2.65.1 → 3.0.0-beta.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/README.md +31 -8
- package/dist/abstract-BKFcva6S.mjs +1044 -0
- package/dist/abstract-BKFcva6S.mjs.map +1 -0
- package/dist/config-BcNSzPZz.d.mts +1718 -0
- package/dist/index.d.mts +50 -1633
- package/dist/index.mjs +3771 -2654
- package/dist/index.mjs.map +1 -1
- package/dist/storage/sqlite.d.mts +59 -0
- package/dist/storage/sqlite.mjs +234 -0
- package/dist/storage/sqlite.mjs.map +1 -0
- package/package.json +26 -29
- package/src/ctMock.ts +125 -136
- package/src/helpers.ts +14 -6
- package/src/index.ts +5 -0
- package/src/lib/masking.ts +4 -5
- package/src/lib/product-review-statistics.test.ts +257 -294
- package/src/lib/review-statistics.ts +17 -4
- package/src/oauth/helpers.ts +7 -4
- package/src/oauth/server.test.ts +102 -62
- package/src/oauth/server.ts +215 -213
- package/src/oauth/store.ts +20 -6
- package/src/orderSearch.ts +3 -3
- package/src/product-projection-search.ts +38 -20
- package/src/product-search-availability.test.ts +31 -52
- package/src/product-search.ts +19 -10
- package/src/projectAPI.ts +6 -22
- package/src/repositories/abstract.ts +182 -48
- package/src/repositories/as-associate.test.ts +19 -19
- package/src/repositories/associate-role.ts +12 -23
- package/src/repositories/attribute-group.test.ts +23 -23
- package/src/repositories/attribute-group.ts +6 -4
- package/src/repositories/business-unit.test.ts +63 -57
- package/src/repositories/business-unit.ts +107 -55
- package/src/repositories/cart/actions.ts +96 -65
- package/src/repositories/cart/helpers.ts +15 -11
- package/src/repositories/cart/index.test.ts +136 -30
- package/src/repositories/cart/index.ts +76 -59
- package/src/repositories/cart-discount/actions.ts +12 -44
- package/src/repositories/cart-discount/index.ts +20 -8
- package/src/repositories/category/actions.ts +27 -27
- package/src/repositories/category/index.test.ts +13 -9
- package/src/repositories/category/index.ts +40 -23
- package/src/repositories/channel.test.ts +53 -51
- package/src/repositories/channel.ts +12 -22
- package/src/repositories/custom-object.ts +34 -25
- package/src/repositories/customer/actions.ts +47 -25
- package/src/repositories/customer/index.test.ts +11 -11
- package/src/repositories/customer/index.ts +65 -35
- package/src/repositories/customer-group.test.ts +44 -42
- package/src/repositories/customer-group.ts +12 -22
- package/src/repositories/discount-code/actions.ts +3 -19
- package/src/repositories/discount-code/index.ts +9 -4
- package/src/repositories/discount-group/index.ts +8 -3
- package/src/repositories/extension.test.ts +27 -27
- package/src/repositories/extension.ts +10 -5
- package/src/repositories/helpers.ts +126 -47
- package/src/repositories/inventory-entry/actions.ts +3 -24
- package/src/repositories/inventory-entry/index.ts +19 -11
- package/src/repositories/my-customer.ts +13 -12
- package/src/repositories/my-order.ts +5 -2
- package/src/repositories/order/actions.ts +89 -56
- package/src/repositories/order/index.test.ts +36 -31
- package/src/repositories/order/index.ts +83 -49
- package/src/repositories/order-edit.ts +8 -3
- package/src/repositories/payment/actions.ts +64 -44
- package/src/repositories/payment/helpers.ts +3 -3
- package/src/repositories/payment/index.ts +28 -12
- package/src/repositories/product/actions.ts +133 -98
- package/src/repositories/product/helpers.ts +29 -16
- package/src/repositories/product/index.ts +42 -25
- package/src/repositories/product-discount.ts +6 -4
- package/src/repositories/product-projection.ts +41 -21
- package/src/repositories/product-selection.ts +8 -15
- package/src/repositories/product-tailoring.ts +22 -3
- package/src/repositories/product-type.ts +45 -4
- package/src/repositories/project.ts +57 -13
- package/src/repositories/quote/actions.ts +5 -28
- package/src/repositories/quote/index.ts +29 -6
- package/src/repositories/quote-request/actions.ts +5 -28
- package/src/repositories/quote-request/index.test.ts +3 -3
- package/src/repositories/quote-request/index.ts +31 -11
- package/src/repositories/quote-staged/actions.ts +5 -28
- package/src/repositories/quote-staged/index.ts +22 -8
- package/src/repositories/recurrence-policy/index.ts +6 -4
- package/src/repositories/recurring-order/actions.ts +7 -32
- package/src/repositories/recurring-order/index.ts +8 -6
- package/src/repositories/review.test.ts +147 -142
- package/src/repositories/review.ts +31 -37
- package/src/repositories/shipping-method/actions.ts +11 -28
- package/src/repositories/shipping-method/index.ts +26 -15
- package/src/repositories/shopping-list/actions.ts +21 -31
- package/src/repositories/shopping-list/index.ts +44 -22
- package/src/repositories/standalone-price.ts +6 -4
- package/src/repositories/state.ts +15 -9
- package/src/repositories/store.ts +21 -32
- package/src/repositories/subscription.test.ts +22 -22
- package/src/repositories/subscription.ts +8 -3
- package/src/repositories/tax-category/index.ts +8 -3
- package/src/repositories/type/actions.ts +21 -3
- package/src/repositories/type/index.ts +5 -3
- package/src/repositories/zone.test.ts +112 -77
- package/src/repositories/zone.ts +5 -3
- package/src/schemas/generated/associate-role.ts +13 -0
- package/src/schemas/generated/attribute-group.ts +12 -0
- package/src/schemas/generated/business-unit.ts +38 -0
- package/src/schemas/generated/cart-discount.ts +33 -0
- package/src/schemas/generated/cart.ts +61 -0
- package/src/schemas/generated/category.ts +25 -0
- package/src/schemas/generated/channel.ts +21 -0
- package/src/schemas/generated/common.ts +1372 -0
- package/src/schemas/generated/custom-object.ts +11 -0
- package/src/schemas/generated/customer-group.ts +11 -0
- package/src/schemas/generated/customer.ts +47 -0
- package/src/schemas/generated/discount-code.ts +25 -0
- package/src/schemas/generated/discount-group.ts +13 -0
- package/src/schemas/generated/extension.ts +15 -0
- package/src/schemas/generated/index.ts +42 -0
- package/src/schemas/generated/inventory-entry.ts +20 -0
- package/src/schemas/generated/my-quote-request.ts +10 -0
- package/src/schemas/generated/order-edit.ts +18 -0
- package/src/schemas/generated/order-from-cart.ts +25 -0
- package/src/schemas/generated/payment.ts +30 -0
- package/src/schemas/generated/product-discount.ts +20 -0
- package/src/schemas/generated/product-selection.ts +18 -0
- package/src/schemas/generated/product-tailoring.ts +26 -0
- package/src/schemas/generated/product-type.ts +12 -0
- package/src/schemas/generated/product.ts +37 -0
- package/src/schemas/generated/quote-request.ts +19 -0
- package/src/schemas/generated/quote.ts +18 -0
- package/src/schemas/generated/recurrence-policy.ts +15 -0
- package/src/schemas/generated/recurring-order.ts +19 -0
- package/src/schemas/generated/review.ts +24 -0
- package/src/schemas/generated/shipping-method.ts +24 -0
- package/src/schemas/generated/shopping-list.ts +28 -0
- package/src/schemas/generated/staged-quote.ts +18 -0
- package/src/schemas/generated/standalone-price.ts +32 -0
- package/src/schemas/generated/state.ts +20 -0
- package/src/schemas/generated/store.ts +23 -0
- package/src/schemas/generated/subscription.ts +20 -0
- package/src/schemas/generated/tax-category.ts +12 -0
- package/src/schemas/generated/type.ts +17 -0
- package/src/schemas/generated/zone.ts +12 -0
- package/src/schemas/update-request.ts +3 -5
- package/src/server.ts +32 -4
- package/src/services/abstract.ts +207 -101
- package/src/services/as-associate-cart.test.ts +28 -36
- package/src/services/as-associate-cart.ts +15 -12
- package/src/services/as-associate-order.test.ts +33 -40
- package/src/services/as-associate-order.ts +15 -12
- package/src/services/as-associate-quote-request.ts +15 -12
- package/src/services/as-associate-shopping-list.test.ts +25 -35
- package/src/services/as-associate-shopping-list.ts +15 -12
- package/src/services/as-associate.test.ts +21 -15
- package/src/services/as-associate.ts +23 -22
- package/src/services/associate-roles.test.ts +16 -22
- package/src/services/associate-roles.ts +2 -2
- package/src/services/attribute-group.test.ts +40 -44
- package/src/services/attribute-group.ts +2 -2
- package/src/services/business-units.test.ts +227 -163
- package/src/services/business-units.ts +2 -2
- package/src/services/cart-discount.test.ts +253 -187
- package/src/services/cart-discount.ts +2 -2
- package/src/services/cart.test.ts +833 -832
- package/src/services/cart.ts +31 -12
- package/src/services/category.test.ts +208 -130
- package/src/services/category.ts +2 -2
- package/src/services/channel.test.ts +39 -44
- package/src/services/channel.ts +2 -2
- package/src/services/custom-object.test.ts +103 -79
- package/src/services/custom-object.ts +106 -38
- package/src/services/customer-group.test.ts +39 -44
- package/src/services/customer-group.ts +2 -2
- package/src/services/customer.test.ts +357 -292
- package/src/services/customer.ts +70 -23
- package/src/services/discount-code.test.ts +57 -68
- package/src/services/discount-code.ts +2 -2
- package/src/services/discount-group.test.ts +111 -134
- package/src/services/discount-group.ts +2 -2
- package/src/services/draft-validation.test.ts +255 -0
- package/src/services/extension.test.ts +39 -44
- package/src/services/extension.ts +2 -2
- package/src/services/inventory-entry.test.ts +106 -87
- package/src/services/inventory-entry.ts +2 -2
- package/src/services/my-business-unit.test.ts +82 -112
- package/src/services/my-business-unit.ts +25 -19
- package/src/services/my-cart.test.ts +46 -41
- package/src/services/my-cart.ts +32 -28
- package/src/services/my-customer.test.ts +153 -88
- package/src/services/my-customer.ts +130 -61
- package/src/services/my-order.ts +15 -12
- package/src/services/my-payment.test.ts +30 -24
- package/src/services/my-payment.ts +2 -2
- package/src/services/my-shopping-list.ts +2 -2
- package/src/services/order.test.ts +332 -276
- package/src/services/order.ts +45 -27
- package/src/services/payment.test.ts +31 -29
- package/src/services/payment.ts +2 -2
- package/src/services/product-discount.test.ts +39 -46
- package/src/services/product-discount.ts +2 -2
- package/src/services/product-projection.test.ts +176 -166
- package/src/services/product-projection.ts +31 -15
- package/src/services/product-selection.test.ts +17 -9
- package/src/services/product-selection.ts +2 -2
- package/src/services/product-type.test.ts +80 -21
- package/src/services/product-type.ts +2 -2
- package/src/services/product.test.ts +569 -534
- package/src/services/product.ts +14 -7
- package/src/services/project.test.ts +22 -12
- package/src/services/project.ts +28 -13
- package/src/services/quote-request.test.ts +36 -39
- package/src/services/quote-request.ts +2 -2
- package/src/services/quote-staged.ts +2 -2
- package/src/services/quote.ts +2 -2
- package/src/services/recurrence-policy.test.ts +114 -139
- package/src/services/recurrence-policy.ts +2 -2
- package/src/services/recurring-order.test.ts +149 -194
- package/src/services/recurring-order.ts +2 -2
- package/src/services/reviews.test.ts +127 -106
- package/src/services/reviews.ts +2 -2
- package/src/services/shipping-method.test.ts +96 -125
- package/src/services/shipping-method.ts +24 -12
- package/src/services/shopping-list.test.ts +183 -141
- package/src/services/shopping-list.ts +2 -2
- package/src/services/standalone-price.test.ts +60 -46
- package/src/services/standalone-price.ts +2 -2
- package/src/services/state.test.ts +20 -25
- package/src/services/state.ts +2 -2
- package/src/services/store.test.ts +26 -45
- package/src/services/store.ts +2 -2
- package/src/services/subscription.test.ts +39 -44
- package/src/services/subscription.ts +2 -2
- package/src/services/tax-category.test.ts +33 -36
- package/src/services/tax-category.ts +2 -2
- package/src/services/type.test.ts +45 -44
- package/src/services/type.ts +2 -2
- package/src/services/zone.test.ts +40 -44
- package/src/services/zone.ts +2 -2
- package/src/shipping.ts +41 -11
- package/src/storage/abstract.ts +248 -17
- package/src/storage/in-memory.ts +147 -290
- package/src/storage/sqlite.ts +429 -0
- package/src/storage/storage-map.ts +75 -0
- package/src/storage/storage.test-helpers.ts +97 -0
- package/src/storage/storage.test.ts +802 -0
- package/src/testing/associate-role.ts +28 -0
- package/src/testing/attribute-group.ts +27 -0
- package/src/testing/business-unit.ts +9 -8
- package/src/testing/cart-discount.ts +34 -0
- package/src/testing/cart.ts +20 -0
- package/src/testing/category.ts +25 -0
- package/src/testing/channel.ts +23 -0
- package/src/testing/custom-object.ts +27 -0
- package/src/testing/customer-group.ts +26 -0
- package/src/testing/customer.ts +36 -33
- package/src/testing/discount-code.ts +29 -0
- package/src/testing/discount-group.ts +27 -0
- package/src/testing/extension.ts +32 -0
- package/src/testing/index.ts +33 -0
- package/src/testing/inventory-entry.ts +26 -0
- package/src/testing/order.ts +27 -0
- package/src/testing/payment.ts +23 -0
- package/src/testing/product-discount.ts +33 -0
- package/src/testing/product-selection.ts +28 -0
- package/src/testing/product-type.ts +27 -0
- package/src/testing/product.ts +38 -0
- package/src/testing/quote-request.ts +29 -0
- package/src/testing/recurrence-policy.ts +33 -0
- package/src/testing/recurring-order.ts +32 -0
- package/src/testing/review.ts +24 -0
- package/src/testing/shipping-method.ts +31 -0
- package/src/testing/shopping-list.ts +25 -0
- package/src/testing/standalone-price.ts +31 -0
- package/src/testing/state.ts +21 -0
- package/src/testing/store.ts +26 -0
- package/src/testing/subscription.ts +38 -0
- package/src/testing/tax-category.ts +27 -0
- package/src/testing/type.ts +9 -6
- package/src/testing/zone.ts +22 -0
- package/src/validate.test.ts +122 -0
- package/src/validate.ts +78 -7
- package/src/.env +0 -0
|
@@ -16,6 +16,7 @@ import type {
|
|
|
16
16
|
BusinessUnitChangeParentUnitAction,
|
|
17
17
|
BusinessUnitChangeStatusAction,
|
|
18
18
|
BusinessUnitDraft,
|
|
19
|
+
BusinessUnitKeyReference,
|
|
19
20
|
BusinessUnitRemoveAddressAction,
|
|
20
21
|
BusinessUnitRemoveAssociateAction,
|
|
21
22
|
BusinessUnitRemoveBillingAddressIdAction,
|
|
@@ -34,10 +35,12 @@ import type {
|
|
|
34
35
|
CompanyDraft,
|
|
35
36
|
Division,
|
|
36
37
|
DivisionDraft,
|
|
38
|
+
InvalidJsonInputError,
|
|
37
39
|
InvalidOperationError,
|
|
38
40
|
} from "@commercetools/platform-sdk";
|
|
39
41
|
import type { Config } from "#src/config.ts";
|
|
40
42
|
import { CommercetoolsError } from "#src/exceptions.ts";
|
|
43
|
+
import { BusinessUnitDraftSchema } from "#src/schemas/generated/business-unit.ts";
|
|
41
44
|
import { generateRandomString, getBaseResourceProperties } from "../helpers.ts";
|
|
42
45
|
import type { Writable } from "../types.ts";
|
|
43
46
|
import type { UpdateHandlerInterface } from "./abstract.ts";
|
|
@@ -58,9 +61,13 @@ export class BusinessUnitRepository extends AbstractResourceRepository<"business
|
|
|
58
61
|
constructor(config: Config) {
|
|
59
62
|
super("business-unit", config);
|
|
60
63
|
this.actions = new BusinessUnitUpdateHandler(this._storage);
|
|
64
|
+
this.draftSchema = BusinessUnitDraftSchema;
|
|
61
65
|
}
|
|
62
66
|
|
|
63
|
-
create(
|
|
67
|
+
async create(
|
|
68
|
+
context: RepositoryContext,
|
|
69
|
+
draft: BusinessUnitDraft,
|
|
70
|
+
): Promise<BusinessUnit> {
|
|
64
71
|
const addresses =
|
|
65
72
|
draft.addresses?.map((address) => ({
|
|
66
73
|
...address,
|
|
@@ -83,18 +90,32 @@ export class BusinessUnitRepository extends AbstractResourceRepository<"business
|
|
|
83
90
|
(i) => addresses[i].id,
|
|
84
91
|
);
|
|
85
92
|
|
|
93
|
+
const stores = draft.stores
|
|
94
|
+
? await Promise.all(
|
|
95
|
+
draft.stores.map((s) =>
|
|
96
|
+
getStoreKeyReference(s, context.projectKey, this._storage),
|
|
97
|
+
),
|
|
98
|
+
)
|
|
99
|
+
: undefined;
|
|
100
|
+
|
|
101
|
+
const associates = draft.associates
|
|
102
|
+
? await Promise.all(
|
|
103
|
+
draft.associates.map((a) =>
|
|
104
|
+
createAssociate(a, context.projectKey, this._storage),
|
|
105
|
+
),
|
|
106
|
+
)
|
|
107
|
+
: [];
|
|
108
|
+
|
|
86
109
|
const resource = {
|
|
87
|
-
...getBaseResourceProperties(),
|
|
110
|
+
...getBaseResourceProperties(context.clientId),
|
|
88
111
|
key: draft.key,
|
|
89
112
|
status: draft.status,
|
|
90
|
-
stores
|
|
91
|
-
getStoreKeyReference(s, context.projectKey, this._storage),
|
|
92
|
-
),
|
|
113
|
+
stores,
|
|
93
114
|
storeMode: draft.storeMode,
|
|
94
115
|
name: draft.name,
|
|
95
116
|
contactEmail: draft.contactEmail,
|
|
96
117
|
addresses: addresses,
|
|
97
|
-
custom: createCustomFields(
|
|
118
|
+
custom: await createCustomFields(
|
|
98
119
|
draft.custom,
|
|
99
120
|
context.projectKey,
|
|
100
121
|
this._storage,
|
|
@@ -105,38 +126,56 @@ export class BusinessUnitRepository extends AbstractResourceRepository<"business
|
|
|
105
126
|
defaultBillingAddressId: defaultBillingAddressId,
|
|
106
127
|
associateMode: draft.associateMode,
|
|
107
128
|
approvalRuleMode: draft.approvalRuleMode,
|
|
108
|
-
|
|
109
|
-
associates:
|
|
110
|
-
draft.associates?.map((a) =>
|
|
111
|
-
createAssociate(a, context.projectKey, this._storage),
|
|
112
|
-
) ?? [],
|
|
129
|
+
associates,
|
|
113
130
|
};
|
|
114
131
|
|
|
115
132
|
if (this._isDivisionDraft(draft)) {
|
|
133
|
+
const parentUnit = await getBusinessUnitKeyReference(
|
|
134
|
+
draft.parentUnit,
|
|
135
|
+
context.projectKey,
|
|
136
|
+
this._storage,
|
|
137
|
+
);
|
|
138
|
+
// Look up the parent to determine the topLevelUnit
|
|
139
|
+
const parent = (await this._storage.getByResourceIdentifier(
|
|
140
|
+
context.projectKey,
|
|
141
|
+
parentUnit,
|
|
142
|
+
)) as BusinessUnit | undefined;
|
|
143
|
+
const topLevelUnit: BusinessUnitKeyReference = parent?.topLevelUnit ?? {
|
|
144
|
+
typeId: "business-unit",
|
|
145
|
+
key: parentUnit.key!,
|
|
146
|
+
};
|
|
116
147
|
const division = {
|
|
117
148
|
...resource,
|
|
118
149
|
unitType: "Division" as const,
|
|
119
|
-
parentUnit
|
|
120
|
-
|
|
121
|
-
context.projectKey,
|
|
122
|
-
this._storage,
|
|
123
|
-
),
|
|
150
|
+
parentUnit,
|
|
151
|
+
topLevelUnit,
|
|
124
152
|
} as Division;
|
|
125
153
|
|
|
126
|
-
this.saveNew(context, division);
|
|
154
|
+
await this.saveNew(context, division);
|
|
127
155
|
return division;
|
|
128
156
|
}
|
|
129
157
|
if (this._isCompanyDraft(draft)) {
|
|
130
158
|
const company = {
|
|
131
159
|
...resource,
|
|
132
160
|
unitType: "Company" as const,
|
|
161
|
+
topLevelUnit: {
|
|
162
|
+
typeId: "business-unit" as const,
|
|
163
|
+
key: draft.key,
|
|
164
|
+
},
|
|
133
165
|
} as Company;
|
|
134
166
|
|
|
135
|
-
this.saveNew(context, company);
|
|
167
|
+
await this.saveNew(context, company);
|
|
136
168
|
return company;
|
|
137
169
|
}
|
|
138
170
|
|
|
139
|
-
throw new
|
|
171
|
+
throw new CommercetoolsError<InvalidJsonInputError>(
|
|
172
|
+
{
|
|
173
|
+
code: "InvalidJsonInput",
|
|
174
|
+
message: "Invalid business unit type",
|
|
175
|
+
detailedErrorMessage: "Invalid business unit type",
|
|
176
|
+
},
|
|
177
|
+
400,
|
|
178
|
+
);
|
|
140
179
|
}
|
|
141
180
|
|
|
142
181
|
private _isCompanyDraft(draft: BusinessUnitDraft): draft is CompanyDraft {
|
|
@@ -153,7 +192,7 @@ class BusinessUnitUpdateHandler
|
|
|
153
192
|
implements
|
|
154
193
|
Partial<UpdateHandlerInterface<BusinessUnit, BusinessUnitUpdateAction>>
|
|
155
194
|
{
|
|
156
|
-
addAddress(
|
|
195
|
+
async addAddress(
|
|
157
196
|
context: RepositoryContext,
|
|
158
197
|
resource: Writable<BusinessUnit>,
|
|
159
198
|
{ address }: BusinessUnitAddAddressAction,
|
|
@@ -168,12 +207,12 @@ class BusinessUnitUpdateHandler
|
|
|
168
207
|
}
|
|
169
208
|
}
|
|
170
209
|
|
|
171
|
-
addAssociate(
|
|
210
|
+
async addAssociate(
|
|
172
211
|
context: RepositoryContext,
|
|
173
212
|
resource: Writable<BusinessUnit>,
|
|
174
213
|
{ associate }: BusinessUnitAddAssociateAction,
|
|
175
214
|
) {
|
|
176
|
-
const newAssociate = createAssociate(
|
|
215
|
+
const newAssociate = await createAssociate(
|
|
177
216
|
associate,
|
|
178
217
|
context.projectKey,
|
|
179
218
|
this._storage,
|
|
@@ -183,12 +222,12 @@ class BusinessUnitUpdateHandler
|
|
|
183
222
|
}
|
|
184
223
|
}
|
|
185
224
|
|
|
186
|
-
addStore(
|
|
225
|
+
async addStore(
|
|
187
226
|
context: RepositoryContext,
|
|
188
227
|
resource: Writable<BusinessUnit>,
|
|
189
228
|
{ store }: BusinessUnitAddStoreAction,
|
|
190
229
|
) {
|
|
191
|
-
const newStore = getStoreKeyReference(
|
|
230
|
+
const newStore = await getStoreKeyReference(
|
|
192
231
|
store,
|
|
193
232
|
context.projectKey,
|
|
194
233
|
this._storage,
|
|
@@ -249,12 +288,12 @@ class BusinessUnitUpdateHandler
|
|
|
249
288
|
resource.name = name;
|
|
250
289
|
}
|
|
251
290
|
|
|
252
|
-
changeParentUnit(
|
|
291
|
+
async changeParentUnit(
|
|
253
292
|
context: RepositoryContext,
|
|
254
293
|
resource: Writable<BusinessUnit>,
|
|
255
294
|
{ parentUnit }: BusinessUnitChangeParentUnitAction,
|
|
256
295
|
) {
|
|
257
|
-
resource.parentUnit = getBusinessUnitKeyReference(
|
|
296
|
+
resource.parentUnit = await getBusinessUnitKeyReference(
|
|
258
297
|
parentUnit,
|
|
259
298
|
context.projectKey,
|
|
260
299
|
this._storage,
|
|
@@ -269,14 +308,18 @@ class BusinessUnitUpdateHandler
|
|
|
269
308
|
resource.status = status;
|
|
270
309
|
}
|
|
271
310
|
|
|
272
|
-
setAssociates(
|
|
311
|
+
async setAssociates(
|
|
273
312
|
context: RepositoryContext,
|
|
274
313
|
resource: Writable<BusinessUnit>,
|
|
275
314
|
{ associates }: BusinessUnitSetAssociatesAction,
|
|
276
315
|
) {
|
|
277
|
-
const newAssociates =
|
|
278
|
-
.
|
|
279
|
-
|
|
316
|
+
const newAssociates = (
|
|
317
|
+
await Promise.all(
|
|
318
|
+
associates.map((a) =>
|
|
319
|
+
createAssociate(a, context.projectKey, this._storage),
|
|
320
|
+
),
|
|
321
|
+
)
|
|
322
|
+
).filter((a): a is Writable<Associate> => a !== undefined);
|
|
280
323
|
resource.associates = newAssociates || undefined;
|
|
281
324
|
}
|
|
282
325
|
|
|
@@ -290,7 +333,7 @@ class BusinessUnitUpdateHandler
|
|
|
290
333
|
);
|
|
291
334
|
}
|
|
292
335
|
|
|
293
|
-
changeAssociate(
|
|
336
|
+
async changeAssociate(
|
|
294
337
|
context: RepositoryContext,
|
|
295
338
|
resource: Writable<BusinessUnit>,
|
|
296
339
|
{ associate }: BusinessUnitChangeAssociateAction,
|
|
@@ -299,12 +342,16 @@ class BusinessUnitUpdateHandler
|
|
|
299
342
|
(a) => a.customer.id === associate.customer.id,
|
|
300
343
|
);
|
|
301
344
|
if (existingAssociateIndex === -1) {
|
|
302
|
-
throw new
|
|
303
|
-
|
|
345
|
+
throw new CommercetoolsError<InvalidOperationError>(
|
|
346
|
+
{
|
|
347
|
+
code: "InvalidOperation",
|
|
348
|
+
message: `Associate with customer id ${associate.customer.id} not found`,
|
|
349
|
+
},
|
|
350
|
+
400,
|
|
304
351
|
);
|
|
305
352
|
}
|
|
306
353
|
|
|
307
|
-
const newAssociate = createAssociate(
|
|
354
|
+
const newAssociate = await createAssociate(
|
|
308
355
|
associate,
|
|
309
356
|
context.projectKey,
|
|
310
357
|
this._storage,
|
|
@@ -322,20 +369,12 @@ class BusinessUnitUpdateHandler
|
|
|
322
369
|
resource.contactEmail = contactEmail;
|
|
323
370
|
}
|
|
324
371
|
|
|
325
|
-
setCustomType(
|
|
372
|
+
async setCustomType(
|
|
326
373
|
context: RepositoryContext,
|
|
327
374
|
resource: Writable<BusinessUnit>,
|
|
328
375
|
{ type, fields }: BusinessUnitSetCustomTypeAction,
|
|
329
376
|
) {
|
|
330
|
-
|
|
331
|
-
resource.custom = createCustomFields(
|
|
332
|
-
{ type, fields },
|
|
333
|
-
context.projectKey,
|
|
334
|
-
this._storage,
|
|
335
|
-
);
|
|
336
|
-
} else {
|
|
337
|
-
resource.custom = undefined;
|
|
338
|
-
}
|
|
377
|
+
await this._setCustomType(context, resource, { type, fields });
|
|
339
378
|
}
|
|
340
379
|
|
|
341
380
|
setStoreMode(
|
|
@@ -378,7 +417,6 @@ class BusinessUnitUpdateHandler
|
|
|
378
417
|
if (!resource.shippingAddressIds.includes(address.id)) {
|
|
379
418
|
resource.shippingAddressIds.push(address.id);
|
|
380
419
|
}
|
|
381
|
-
return resource;
|
|
382
420
|
}
|
|
383
421
|
|
|
384
422
|
removeShippingAddressId(
|
|
@@ -450,10 +488,7 @@ class BusinessUnitUpdateHandler
|
|
|
450
488
|
resource: Writable<BusinessUnit>,
|
|
451
489
|
{ name, value }: BusinessUnitSetCustomFieldAction,
|
|
452
490
|
) {
|
|
453
|
-
|
|
454
|
-
throw new Error("Resource has no custom type");
|
|
455
|
-
}
|
|
456
|
-
resource.custom.fields[name] = value;
|
|
491
|
+
this._setCustomFieldValues(resource, { name, value });
|
|
457
492
|
}
|
|
458
493
|
|
|
459
494
|
setAddressCustomField(
|
|
@@ -463,32 +498,49 @@ class BusinessUnitUpdateHandler
|
|
|
463
498
|
) {
|
|
464
499
|
const address = resource.addresses.find((addr) => addr.id === addressId);
|
|
465
500
|
if (!address) {
|
|
466
|
-
throw new
|
|
501
|
+
throw new CommercetoolsError<InvalidOperationError>(
|
|
502
|
+
{
|
|
503
|
+
code: "InvalidOperation",
|
|
504
|
+
message: `Address with id ${addressId} not found`,
|
|
505
|
+
},
|
|
506
|
+
400,
|
|
507
|
+
);
|
|
467
508
|
}
|
|
468
509
|
if (!address.custom) {
|
|
469
510
|
// If the address doesn't have custom fields, we need to initialize them
|
|
470
511
|
// This might require a type to be set first, but we'll just create minimal structure
|
|
471
|
-
throw new
|
|
472
|
-
|
|
512
|
+
throw new CommercetoolsError<InvalidOperationError>(
|
|
513
|
+
{
|
|
514
|
+
code: "InvalidOperation",
|
|
515
|
+
message:
|
|
516
|
+
"Address has no custom type set. Use setAddressCustomType first.",
|
|
517
|
+
},
|
|
518
|
+
400,
|
|
473
519
|
);
|
|
474
520
|
}
|
|
475
521
|
address.custom.fields[name] = value;
|
|
476
522
|
}
|
|
477
523
|
|
|
478
|
-
setAddressCustomType(
|
|
524
|
+
async setAddressCustomType(
|
|
479
525
|
context: RepositoryContext,
|
|
480
526
|
resource: Writable<BusinessUnit>,
|
|
481
527
|
{ addressId, type, fields }: BusinessUnitSetAddressCustomTypeAction,
|
|
482
528
|
) {
|
|
483
529
|
const address = resource.addresses.find((addr) => addr.id === addressId);
|
|
484
530
|
if (!address) {
|
|
485
|
-
throw new
|
|
531
|
+
throw new CommercetoolsError<InvalidOperationError>(
|
|
532
|
+
{
|
|
533
|
+
code: "InvalidOperation",
|
|
534
|
+
message: `Address with id ${addressId} not found`,
|
|
535
|
+
},
|
|
536
|
+
400,
|
|
537
|
+
);
|
|
486
538
|
}
|
|
487
539
|
|
|
488
540
|
if (!type) {
|
|
489
541
|
address.custom = undefined;
|
|
490
542
|
} else {
|
|
491
|
-
address.custom = createCustomFields(
|
|
543
|
+
address.custom = await createCustomFields(
|
|
492
544
|
{ type, fields },
|
|
493
545
|
context.projectKey,
|
|
494
546
|
this._storage,
|