@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.
Files changed (180) hide show
  1. package/dist/index.cjs +4351 -4099
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +266 -413
  4. package/dist/index.d.ts +266 -413
  5. package/dist/index.js +4351 -4099
  6. package/dist/index.js.map +1 -1
  7. package/package.json +47 -47
  8. package/src/constants.ts +2 -2
  9. package/src/ctMock.test.ts +11 -11
  10. package/src/ctMock.ts +141 -127
  11. package/src/deprecation.ts +8 -0
  12. package/src/exceptions.ts +17 -15
  13. package/src/helpers.ts +32 -32
  14. package/src/index.test.ts +128 -128
  15. package/src/index.ts +3 -3
  16. package/src/lib/expandParser.ts +13 -13
  17. package/src/lib/haversine.test.ts +9 -9
  18. package/src/lib/haversine.ts +11 -11
  19. package/src/lib/masking.ts +11 -11
  20. package/src/lib/parser.ts +2 -2
  21. package/src/lib/password.ts +23 -3
  22. package/src/lib/predicateParser.test.ts +185 -183
  23. package/src/lib/predicateParser.ts +234 -234
  24. package/src/lib/projectionSearchFilter.test.ts +103 -101
  25. package/src/lib/projectionSearchFilter.ts +152 -150
  26. package/src/lib/proxy.ts +5 -5
  27. package/src/oauth/errors.ts +4 -4
  28. package/src/oauth/helpers.ts +6 -6
  29. package/src/oauth/server.test.ts +86 -86
  30. package/src/oauth/server.ts +158 -144
  31. package/src/oauth/store.ts +44 -43
  32. package/src/priceSelector.test.ts +35 -35
  33. package/src/priceSelector.ts +30 -30
  34. package/src/product-projection-search.ts +136 -134
  35. package/src/projectAPI.test.ts +7 -7
  36. package/src/projectAPI.ts +24 -22
  37. package/src/repositories/abstract.ts +168 -116
  38. package/src/repositories/associate-role.ts +90 -77
  39. package/src/repositories/attribute-group.ts +51 -40
  40. package/src/repositories/business-unit.ts +168 -148
  41. package/src/repositories/cart/actions.ts +489 -0
  42. package/src/repositories/cart/helpers.ts +30 -0
  43. package/src/repositories/cart/index.ts +180 -0
  44. package/src/repositories/cart-discount/actions.ts +148 -0
  45. package/src/repositories/cart-discount/index.ts +86 -0
  46. package/src/repositories/category/actions.ts +231 -0
  47. package/src/repositories/category/index.ts +52 -0
  48. package/src/repositories/channel.ts +88 -90
  49. package/src/repositories/custom-object.ts +46 -45
  50. package/src/repositories/customer/actions.ts +165 -0
  51. package/src/repositories/customer/index.ts +79 -0
  52. package/src/repositories/customer-group.ts +66 -55
  53. package/src/repositories/discount-code/actions.ts +149 -0
  54. package/src/repositories/discount-code/index.ts +50 -0
  55. package/src/repositories/errors.ts +10 -10
  56. package/src/repositories/extension.ts +64 -62
  57. package/src/repositories/helpers.ts +117 -118
  58. package/src/repositories/index.ts +80 -79
  59. package/src/repositories/inventory-entry/actions.ts +84 -0
  60. package/src/repositories/inventory-entry/index.ts +44 -0
  61. package/src/repositories/my-customer.ts +114 -0
  62. package/src/repositories/my-order.ts +8 -8
  63. package/src/repositories/order/actions.ts +281 -0
  64. package/src/repositories/{order.test.ts → order/index.test.ts} +77 -77
  65. package/src/repositories/order/index.ts +260 -0
  66. package/src/repositories/order-edit.ts +10 -23
  67. package/src/repositories/payment/actions.ts +305 -0
  68. package/src/repositories/payment/helpers.ts +17 -0
  69. package/src/repositories/payment/index.ts +56 -0
  70. package/src/repositories/product/actions.ts +943 -0
  71. package/src/repositories/product/helpers.ts +98 -0
  72. package/src/repositories/product/index.ts +130 -0
  73. package/src/repositories/product-discount.ts +127 -117
  74. package/src/repositories/product-projection.ts +56 -62
  75. package/src/repositories/product-selection.ts +31 -28
  76. package/src/repositories/product-type.ts +136 -134
  77. package/src/repositories/project.ts +133 -118
  78. package/src/repositories/quote-request.ts +7 -19
  79. package/src/repositories/quote.ts +7 -22
  80. package/src/repositories/review.ts +13 -26
  81. package/src/repositories/shipping-method/actions.ts +198 -0
  82. package/src/repositories/shipping-method/helpers.ts +10 -0
  83. package/src/repositories/shipping-method/index.ts +138 -0
  84. package/src/repositories/shopping-list/actions.ts +295 -0
  85. package/src/repositories/shopping-list/index.ts +122 -0
  86. package/src/repositories/staged-quote.ts +7 -20
  87. package/src/repositories/standalone-price.ts +57 -44
  88. package/src/repositories/state.ts +113 -68
  89. package/src/repositories/store.ts +106 -94
  90. package/src/repositories/subscription.ts +46 -22
  91. package/src/repositories/tax-category/actions.ts +94 -0
  92. package/src/repositories/tax-category/helpers.ts +8 -0
  93. package/src/repositories/tax-category/index.ts +25 -0
  94. package/src/repositories/type/actions.ts +162 -0
  95. package/src/repositories/type/index.ts +24 -0
  96. package/src/repositories/zone.ts +62 -58
  97. package/src/schemas/update-request.ts +12 -0
  98. package/src/server.ts +9 -9
  99. package/src/services/abstract.ts +85 -72
  100. package/src/services/associate-roles.test.ts +27 -27
  101. package/src/services/associate-roles.ts +7 -7
  102. package/src/services/attribute-group.ts +7 -7
  103. package/src/services/business-units.test.ts +28 -28
  104. package/src/services/business-units.ts +7 -7
  105. package/src/services/cart-discount.test.ts +199 -199
  106. package/src/services/cart-discount.ts +7 -7
  107. package/src/services/cart.test.ts +261 -261
  108. package/src/services/cart.ts +22 -21
  109. package/src/services/category.test.ts +121 -121
  110. package/src/services/category.ts +7 -7
  111. package/src/services/channel.ts +7 -7
  112. package/src/services/custom-object.test.ts +130 -130
  113. package/src/services/custom-object.ts +34 -31
  114. package/src/services/customer-group.ts +7 -7
  115. package/src/services/customer.test.ts +205 -205
  116. package/src/services/customer.ts +23 -36
  117. package/src/services/discount-code.ts +7 -7
  118. package/src/services/extension.ts +7 -7
  119. package/src/services/index.ts +85 -81
  120. package/src/services/inventory-entry.test.ts +106 -106
  121. package/src/services/inventory-entry.ts +7 -7
  122. package/src/services/my-cart.test.ts +56 -56
  123. package/src/services/my-cart.ts +20 -20
  124. package/src/services/my-customer.test.ts +155 -104
  125. package/src/services/my-customer.ts +66 -75
  126. package/src/services/my-order.ts +16 -16
  127. package/src/services/my-payment.test.ts +40 -40
  128. package/src/services/my-payment.ts +7 -7
  129. package/src/services/my-shopping-list.ts +7 -7
  130. package/src/services/order.test.ts +243 -243
  131. package/src/services/order.ts +23 -18
  132. package/src/services/payment.test.ts +40 -40
  133. package/src/services/payment.ts +7 -7
  134. package/src/services/product-discount.ts +7 -7
  135. package/src/services/product-projection.test.ts +190 -190
  136. package/src/services/product-projection.ts +34 -32
  137. package/src/services/product-selection.test.ts +19 -19
  138. package/src/services/product-selection.ts +7 -7
  139. package/src/services/product-type.test.ts +38 -38
  140. package/src/services/product-type.ts +7 -7
  141. package/src/services/product.test.ts +658 -656
  142. package/src/services/product.ts +7 -7
  143. package/src/services/project.test.ts +29 -24
  144. package/src/services/project.ts +22 -17
  145. package/src/services/reviews.ts +7 -7
  146. package/src/services/shipping-method.test.ts +78 -78
  147. package/src/services/shipping-method.ts +16 -16
  148. package/src/services/shopping-list.test.ts +170 -170
  149. package/src/services/shopping-list.ts +7 -7
  150. package/src/services/standalone-price.test.ts +112 -112
  151. package/src/services/standalone-price.ts +7 -7
  152. package/src/services/state.test.ts +30 -30
  153. package/src/services/state.ts +7 -7
  154. package/src/services/store.test.ts +40 -40
  155. package/src/services/store.ts +7 -7
  156. package/src/services/subscription.ts +7 -7
  157. package/src/services/tax-category.test.ts +43 -43
  158. package/src/services/tax-category.ts +7 -7
  159. package/src/services/type.ts +7 -7
  160. package/src/services/zone.ts +7 -7
  161. package/src/shippingCalculator.test.ts +43 -43
  162. package/src/shippingCalculator.ts +23 -23
  163. package/src/storage/abstract.ts +36 -34
  164. package/src/storage/in-memory.ts +237 -233
  165. package/src/storage/index.ts +2 -2
  166. package/src/types.ts +91 -91
  167. package/src/validate.ts +18 -0
  168. package/src/repositories/cart-discount.ts +0 -219
  169. package/src/repositories/cart.ts +0 -659
  170. package/src/repositories/category.ts +0 -256
  171. package/src/repositories/customer.ts +0 -228
  172. package/src/repositories/discount-code.ts +0 -181
  173. package/src/repositories/inventory-entry.ts +0 -109
  174. package/src/repositories/order.ts +0 -514
  175. package/src/repositories/payment.ts +0 -342
  176. package/src/repositories/product.ts +0 -1106
  177. package/src/repositories/shipping-method.ts +0 -312
  178. package/src/repositories/shopping-list.ts +0 -392
  179. package/src/repositories/tax-category.ts +0 -111
  180. 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
+ }