@labdigital/commercetools-mock 2.17.1 → 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 +4186 -3974
- 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 +4186 -3974
- 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 +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/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 +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 +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,148 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CartDiscount,
|
|
3
|
+
CartDiscountChangeIsActiveAction,
|
|
4
|
+
CartDiscountChangeSortOrderAction,
|
|
5
|
+
CartDiscountChangeTargetAction,
|
|
6
|
+
CartDiscountSetCustomFieldAction,
|
|
7
|
+
CartDiscountSetCustomTypeAction,
|
|
8
|
+
CartDiscountSetDescriptionAction,
|
|
9
|
+
CartDiscountSetKeyAction,
|
|
10
|
+
CartDiscountSetValidFromAction,
|
|
11
|
+
CartDiscountSetValidFromAndUntilAction,
|
|
12
|
+
CartDiscountSetValidUntilAction,
|
|
13
|
+
CartDiscountUpdateAction,
|
|
14
|
+
InvalidOperationError,
|
|
15
|
+
} from "@commercetools/platform-sdk";
|
|
16
|
+
import type { Writable } from "~src/types";
|
|
17
|
+
import {
|
|
18
|
+
AbstractUpdateHandler,
|
|
19
|
+
UpdateHandlerInterface,
|
|
20
|
+
type RepositoryContext,
|
|
21
|
+
} from "../abstract";
|
|
22
|
+
|
|
23
|
+
import { CommercetoolsError } from "~src/exceptions";
|
|
24
|
+
|
|
25
|
+
export class CartDiscountUpdateHandler
|
|
26
|
+
extends AbstractUpdateHandler
|
|
27
|
+
implements
|
|
28
|
+
Partial<UpdateHandlerInterface<CartDiscount, CartDiscountUpdateAction>>
|
|
29
|
+
{
|
|
30
|
+
changeIsActive(
|
|
31
|
+
context: RepositoryContext,
|
|
32
|
+
resource: Writable<CartDiscount>,
|
|
33
|
+
{ isActive }: CartDiscountChangeIsActiveAction,
|
|
34
|
+
) {
|
|
35
|
+
resource.isActive = isActive;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
changeSortOrder(
|
|
39
|
+
context: RepositoryContext,
|
|
40
|
+
resource: Writable<CartDiscount>,
|
|
41
|
+
{ sortOrder }: CartDiscountChangeSortOrderAction,
|
|
42
|
+
) {
|
|
43
|
+
resource.sortOrder = sortOrder;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
changeTarget(
|
|
47
|
+
context: RepositoryContext,
|
|
48
|
+
resource: Writable<CartDiscount>,
|
|
49
|
+
{ target }: CartDiscountChangeTargetAction,
|
|
50
|
+
) {
|
|
51
|
+
resource.target = target;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
setCustomField(
|
|
55
|
+
context: RepositoryContext,
|
|
56
|
+
resource: Writable<CartDiscount>,
|
|
57
|
+
{ name, value }: CartDiscountSetCustomFieldAction,
|
|
58
|
+
) {
|
|
59
|
+
if (!resource.custom) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (value === null) {
|
|
63
|
+
if (name in resource.custom.fields) {
|
|
64
|
+
delete resource.custom.fields[name];
|
|
65
|
+
} else {
|
|
66
|
+
throw new CommercetoolsError<InvalidOperationError>(
|
|
67
|
+
{
|
|
68
|
+
code: "InvalidOperation",
|
|
69
|
+
message:
|
|
70
|
+
"Cannot remove custom field " +
|
|
71
|
+
name +
|
|
72
|
+
" because it does not exist.",
|
|
73
|
+
},
|
|
74
|
+
400,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
resource.custom.fields[name] = value;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
setCustomType(
|
|
83
|
+
context: RepositoryContext,
|
|
84
|
+
resource: Writable<CartDiscount>,
|
|
85
|
+
{ type, fields }: CartDiscountSetCustomTypeAction,
|
|
86
|
+
) {
|
|
87
|
+
if (!type) {
|
|
88
|
+
resource.custom = undefined;
|
|
89
|
+
} else {
|
|
90
|
+
const resolvedType = this._storage.getByResourceIdentifier(
|
|
91
|
+
context.projectKey,
|
|
92
|
+
type,
|
|
93
|
+
);
|
|
94
|
+
if (!resolvedType) {
|
|
95
|
+
throw new Error(`Type ${type} not found`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
resource.custom = {
|
|
99
|
+
type: {
|
|
100
|
+
typeId: "type",
|
|
101
|
+
id: resolvedType.id,
|
|
102
|
+
},
|
|
103
|
+
fields: fields || {},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
setDescription(
|
|
109
|
+
context: RepositoryContext,
|
|
110
|
+
resource: Writable<CartDiscount>,
|
|
111
|
+
{ description }: CartDiscountSetDescriptionAction,
|
|
112
|
+
) {
|
|
113
|
+
resource.description = description;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
setKey(
|
|
117
|
+
context: RepositoryContext,
|
|
118
|
+
resource: Writable<CartDiscount>,
|
|
119
|
+
{ key }: CartDiscountSetKeyAction,
|
|
120
|
+
) {
|
|
121
|
+
resource.key = key;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
setValidFrom(
|
|
125
|
+
context: RepositoryContext,
|
|
126
|
+
resource: Writable<CartDiscount>,
|
|
127
|
+
{ validFrom }: CartDiscountSetValidFromAction,
|
|
128
|
+
) {
|
|
129
|
+
resource.validFrom = validFrom;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
setValidFromAndUntil(
|
|
133
|
+
context: RepositoryContext,
|
|
134
|
+
resource: Writable<CartDiscount>,
|
|
135
|
+
{ validFrom, validUntil }: CartDiscountSetValidFromAndUntilAction,
|
|
136
|
+
) {
|
|
137
|
+
resource.validFrom = validFrom;
|
|
138
|
+
resource.validUntil = validUntil;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
setValidUntil(
|
|
142
|
+
context: RepositoryContext,
|
|
143
|
+
resource: Writable<CartDiscount>,
|
|
144
|
+
{ validUntil }: CartDiscountSetValidUntilAction,
|
|
145
|
+
) {
|
|
146
|
+
resource.validUntil = validUntil;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CartDiscount,
|
|
3
|
+
CartDiscountDraft,
|
|
4
|
+
CartDiscountValueAbsolute,
|
|
5
|
+
CartDiscountValueDraft,
|
|
6
|
+
CartDiscountValueFixed,
|
|
7
|
+
CartDiscountValueGiftLineItem,
|
|
8
|
+
CartDiscountValueRelative,
|
|
9
|
+
} from "@commercetools/platform-sdk";
|
|
10
|
+
import { getBaseResourceProperties } from "~src/helpers";
|
|
11
|
+
import { AbstractStorage } from "~src/storage/abstract";
|
|
12
|
+
import {
|
|
13
|
+
AbstractResourceRepository,
|
|
14
|
+
type RepositoryContext,
|
|
15
|
+
} from "../abstract";
|
|
16
|
+
import {
|
|
17
|
+
createCustomFields,
|
|
18
|
+
createTypedMoney,
|
|
19
|
+
getStoreKeyReference,
|
|
20
|
+
} from "../helpers";
|
|
21
|
+
import { CartDiscountUpdateHandler } from "./actions";
|
|
22
|
+
|
|
23
|
+
export class CartDiscountRepository extends AbstractResourceRepository<"cart-discount"> {
|
|
24
|
+
constructor(storage: AbstractStorage) {
|
|
25
|
+
super("cart-discount", storage);
|
|
26
|
+
this.actions = new CartDiscountUpdateHandler(storage);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
create(context: RepositoryContext, draft: CartDiscountDraft): CartDiscount {
|
|
30
|
+
const resource: CartDiscount = {
|
|
31
|
+
...getBaseResourceProperties(),
|
|
32
|
+
key: draft.key,
|
|
33
|
+
description: draft.description,
|
|
34
|
+
cartPredicate: draft.cartPredicate,
|
|
35
|
+
isActive: draft.isActive || false,
|
|
36
|
+
name: draft.name,
|
|
37
|
+
stores:
|
|
38
|
+
draft.stores?.map((s) =>
|
|
39
|
+
getStoreKeyReference(s, context.projectKey, this._storage),
|
|
40
|
+
) ?? [],
|
|
41
|
+
references: [],
|
|
42
|
+
target: draft.target,
|
|
43
|
+
requiresDiscountCode: draft.requiresDiscountCode || false,
|
|
44
|
+
sortOrder: draft.sortOrder,
|
|
45
|
+
stackingMode: draft.stackingMode || "Stacking",
|
|
46
|
+
validFrom: draft.validFrom,
|
|
47
|
+
validUntil: draft.validUntil,
|
|
48
|
+
value: this.transformValueDraft(draft.value),
|
|
49
|
+
custom: createCustomFields(
|
|
50
|
+
draft.custom,
|
|
51
|
+
context.projectKey,
|
|
52
|
+
this._storage,
|
|
53
|
+
),
|
|
54
|
+
};
|
|
55
|
+
return this.saveNew(context, resource);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private transformValueDraft(value: CartDiscountValueDraft) {
|
|
59
|
+
switch (value.type) {
|
|
60
|
+
case "absolute": {
|
|
61
|
+
return {
|
|
62
|
+
type: "absolute",
|
|
63
|
+
money: value.money.map(createTypedMoney),
|
|
64
|
+
} as CartDiscountValueAbsolute;
|
|
65
|
+
}
|
|
66
|
+
case "fixed": {
|
|
67
|
+
return {
|
|
68
|
+
type: "fixed",
|
|
69
|
+
money: value.money.map(createTypedMoney),
|
|
70
|
+
} as CartDiscountValueFixed;
|
|
71
|
+
}
|
|
72
|
+
case "giftLineItem": {
|
|
73
|
+
return {
|
|
74
|
+
...value,
|
|
75
|
+
} as CartDiscountValueGiftLineItem;
|
|
76
|
+
}
|
|
77
|
+
case "relative": {
|
|
78
|
+
return {
|
|
79
|
+
...value,
|
|
80
|
+
} as CartDiscountValueRelative;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Asset,
|
|
3
|
+
AssetDraft,
|
|
4
|
+
Category,
|
|
5
|
+
CategoryAddAssetAction,
|
|
6
|
+
CategoryChangeAssetNameAction,
|
|
7
|
+
CategoryChangeNameAction,
|
|
8
|
+
CategoryChangeParentAction,
|
|
9
|
+
CategoryChangeSlugAction,
|
|
10
|
+
CategoryRemoveAssetAction,
|
|
11
|
+
CategorySetAssetDescriptionAction,
|
|
12
|
+
CategorySetAssetSourcesAction,
|
|
13
|
+
CategorySetCustomFieldAction,
|
|
14
|
+
CategorySetCustomTypeAction,
|
|
15
|
+
CategorySetDescriptionAction,
|
|
16
|
+
CategorySetKeyAction,
|
|
17
|
+
CategorySetMetaDescriptionAction,
|
|
18
|
+
CategorySetMetaKeywordsAction,
|
|
19
|
+
CategorySetMetaTitleAction,
|
|
20
|
+
CategoryUpdateAction,
|
|
21
|
+
} from "@commercetools/platform-sdk";
|
|
22
|
+
import { v4 as uuidv4 } from "uuid";
|
|
23
|
+
import { Writable } from "~src/types";
|
|
24
|
+
import {
|
|
25
|
+
AbstractUpdateHandler,
|
|
26
|
+
RepositoryContext,
|
|
27
|
+
UpdateHandlerInterface,
|
|
28
|
+
} from "../abstract";
|
|
29
|
+
import { createCustomFields } from "../helpers";
|
|
30
|
+
|
|
31
|
+
export class CategoryUpdateHandler
|
|
32
|
+
extends AbstractUpdateHandler
|
|
33
|
+
implements Partial<UpdateHandlerInterface<Category, CategoryUpdateAction>>
|
|
34
|
+
{
|
|
35
|
+
addAsset(
|
|
36
|
+
context: RepositoryContext,
|
|
37
|
+
resource: Writable<Category>,
|
|
38
|
+
{ asset }: CategoryAddAssetAction,
|
|
39
|
+
) {
|
|
40
|
+
if (!resource.assets) {
|
|
41
|
+
resource.assets = [this.assetFromAssetDraft(asset, context)];
|
|
42
|
+
} else {
|
|
43
|
+
resource.assets.push(this.assetFromAssetDraft(asset, context));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
changeAssetName(
|
|
48
|
+
context: RepositoryContext,
|
|
49
|
+
resource: Writable<Category>,
|
|
50
|
+
{ assetId, assetKey, name }: CategoryChangeAssetNameAction,
|
|
51
|
+
) {
|
|
52
|
+
resource.assets?.forEach((asset) => {
|
|
53
|
+
if (assetId && assetId === asset.id) {
|
|
54
|
+
asset.name = name;
|
|
55
|
+
}
|
|
56
|
+
if (assetKey && assetKey === asset.key) {
|
|
57
|
+
asset.name = name;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
changeName(
|
|
63
|
+
context: RepositoryContext,
|
|
64
|
+
resource: Writable<Category>,
|
|
65
|
+
{ name }: CategoryChangeNameAction,
|
|
66
|
+
) {
|
|
67
|
+
resource.name = name;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
changeParent(
|
|
71
|
+
context: RepositoryContext,
|
|
72
|
+
resource: Writable<Category>,
|
|
73
|
+
{ parent }: CategoryChangeParentAction,
|
|
74
|
+
) {
|
|
75
|
+
const category = this._storage.getByResourceIdentifier(
|
|
76
|
+
context.projectKey,
|
|
77
|
+
parent,
|
|
78
|
+
);
|
|
79
|
+
if (!category) {
|
|
80
|
+
throw new Error("No category found for reference");
|
|
81
|
+
}
|
|
82
|
+
resource.parent = {
|
|
83
|
+
typeId: "category",
|
|
84
|
+
id: category.id,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
changeSlug(
|
|
89
|
+
context: RepositoryContext,
|
|
90
|
+
resource: Writable<Category>,
|
|
91
|
+
{ slug }: CategoryChangeSlugAction,
|
|
92
|
+
) {
|
|
93
|
+
resource.slug = slug;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
removeAsset(
|
|
97
|
+
context: RepositoryContext,
|
|
98
|
+
resource: Writable<Category>,
|
|
99
|
+
{ assetId, assetKey }: CategoryRemoveAssetAction,
|
|
100
|
+
) {
|
|
101
|
+
if (!resource.assets) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (assetId) {
|
|
106
|
+
resource.assets = resource.assets.filter(function (obj) {
|
|
107
|
+
return obj.id !== assetId;
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (assetKey) {
|
|
114
|
+
resource.assets = resource.assets.filter(function (obj) {
|
|
115
|
+
return obj.key !== assetKey;
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
setAssetDescription(
|
|
123
|
+
context: RepositoryContext,
|
|
124
|
+
resource: Writable<Category>,
|
|
125
|
+
{ assetId, assetKey, description }: CategorySetAssetDescriptionAction,
|
|
126
|
+
) {
|
|
127
|
+
resource.assets?.forEach((asset) => {
|
|
128
|
+
if (assetId && assetId === asset.id) {
|
|
129
|
+
asset.description = description;
|
|
130
|
+
}
|
|
131
|
+
if (assetKey && assetKey === asset.key) {
|
|
132
|
+
asset.description = description;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
setAssetSources(
|
|
138
|
+
context: RepositoryContext,
|
|
139
|
+
resource: Writable<Category>,
|
|
140
|
+
{ assetId, assetKey, sources }: CategorySetAssetSourcesAction,
|
|
141
|
+
) {
|
|
142
|
+
resource.assets?.forEach((asset) => {
|
|
143
|
+
if (assetId && assetId === asset.id) {
|
|
144
|
+
asset.sources = sources;
|
|
145
|
+
}
|
|
146
|
+
if (assetKey && assetKey === asset.key) {
|
|
147
|
+
asset.sources = sources;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
setCustomField(
|
|
153
|
+
context: RepositoryContext,
|
|
154
|
+
resource: Writable<Category>,
|
|
155
|
+
{ name, value }: CategorySetCustomFieldAction,
|
|
156
|
+
) {
|
|
157
|
+
if (!resource.custom) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (value === null) {
|
|
161
|
+
delete resource.custom.fields[name];
|
|
162
|
+
} else {
|
|
163
|
+
resource.custom.fields[name] = value;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
setCustomType(
|
|
168
|
+
context: RepositoryContext,
|
|
169
|
+
resource: Writable<Category>,
|
|
170
|
+
{ type, fields }: CategorySetCustomTypeAction,
|
|
171
|
+
) {
|
|
172
|
+
if (type) {
|
|
173
|
+
resource.custom = createCustomFields(
|
|
174
|
+
{ type, fields },
|
|
175
|
+
context.projectKey,
|
|
176
|
+
this._storage,
|
|
177
|
+
);
|
|
178
|
+
} else {
|
|
179
|
+
resource.custom = undefined;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
setDescription(
|
|
184
|
+
context: RepositoryContext,
|
|
185
|
+
resource: Writable<Category>,
|
|
186
|
+
{ description }: CategorySetDescriptionAction,
|
|
187
|
+
) {
|
|
188
|
+
resource.description = description;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
setKey(
|
|
192
|
+
context: RepositoryContext,
|
|
193
|
+
resource: Writable<Category>,
|
|
194
|
+
{ key }: CategorySetKeyAction,
|
|
195
|
+
) {
|
|
196
|
+
resource.key = key;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
setMetaDescription(
|
|
200
|
+
context: RepositoryContext,
|
|
201
|
+
resource: Writable<Category>,
|
|
202
|
+
{ metaDescription }: CategorySetMetaDescriptionAction,
|
|
203
|
+
) {
|
|
204
|
+
resource.metaDescription = metaDescription;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
setMetaKeywords(
|
|
208
|
+
context: RepositoryContext,
|
|
209
|
+
resource: Writable<Category>,
|
|
210
|
+
{ metaKeywords }: CategorySetMetaKeywordsAction,
|
|
211
|
+
) {
|
|
212
|
+
resource.metaKeywords = metaKeywords;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
setMetaTitle(
|
|
216
|
+
context: RepositoryContext,
|
|
217
|
+
resource: Writable<Category>,
|
|
218
|
+
{ metaTitle }: CategorySetMetaTitleAction,
|
|
219
|
+
) {
|
|
220
|
+
resource.metaTitle = metaTitle;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
assetFromAssetDraft = (
|
|
224
|
+
draft: AssetDraft,
|
|
225
|
+
context: RepositoryContext,
|
|
226
|
+
): Asset => ({
|
|
227
|
+
...draft,
|
|
228
|
+
id: uuidv4(),
|
|
229
|
+
custom: createCustomFields(draft.custom, context.projectKey, this._storage),
|
|
230
|
+
});
|
|
231
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Category, CategoryDraft } from "@commercetools/platform-sdk";
|
|
2
|
+
import { v4 as uuidv4 } from "uuid";
|
|
3
|
+
import { getBaseResourceProperties } from "~src/helpers";
|
|
4
|
+
import { AbstractStorage } from "~src/storage/abstract";
|
|
5
|
+
import {
|
|
6
|
+
AbstractResourceRepository,
|
|
7
|
+
type RepositoryContext,
|
|
8
|
+
} from "../abstract";
|
|
9
|
+
import { createCustomFields } from "../helpers";
|
|
10
|
+
import { CategoryUpdateHandler } from "./actions";
|
|
11
|
+
|
|
12
|
+
export class CategoryRepository extends AbstractResourceRepository<"category"> {
|
|
13
|
+
constructor(storage: AbstractStorage) {
|
|
14
|
+
super("category", storage);
|
|
15
|
+
this.actions = new CategoryUpdateHandler(this._storage);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
create(context: RepositoryContext, draft: CategoryDraft): Category {
|
|
19
|
+
const resource: Category = {
|
|
20
|
+
...getBaseResourceProperties(),
|
|
21
|
+
key: draft.key,
|
|
22
|
+
name: draft.name,
|
|
23
|
+
slug: draft.slug,
|
|
24
|
+
orderHint: draft.orderHint || "",
|
|
25
|
+
externalId: draft.externalId || "",
|
|
26
|
+
parent: draft.parent
|
|
27
|
+
? { typeId: "category", id: draft.parent.id! }
|
|
28
|
+
: undefined,
|
|
29
|
+
ancestors: [], // TODO
|
|
30
|
+
assets:
|
|
31
|
+
draft.assets?.map((d) => ({
|
|
32
|
+
id: uuidv4(),
|
|
33
|
+
name: d.name,
|
|
34
|
+
description: d.description,
|
|
35
|
+
sources: d.sources,
|
|
36
|
+
tags: d.tags,
|
|
37
|
+
key: d.key,
|
|
38
|
+
custom: createCustomFields(
|
|
39
|
+
draft.custom,
|
|
40
|
+
context.projectKey,
|
|
41
|
+
this._storage,
|
|
42
|
+
),
|
|
43
|
+
})) || [],
|
|
44
|
+
custom: createCustomFields(
|
|
45
|
+
draft.custom,
|
|
46
|
+
context.projectKey,
|
|
47
|
+
this._storage,
|
|
48
|
+
),
|
|
49
|
+
};
|
|
50
|
+
return this.saveNew(context, resource);
|
|
51
|
+
}
|
|
52
|
+
}
|