@labdigital/commercetools-mock 2.17.0 → 2.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/dist/index.cjs +4219 -3989
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +268 -415
  4. package/dist/index.d.ts +268 -415
  5. package/dist/index.js +4219 -3989
  6. package/dist/index.js.map +1 -1
  7. package/package.json +44 -46
  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 +110 -67
  30. package/src/oauth/server.ts +161 -141
  31. package/src/oauth/store.ts +49 -44
  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/server.ts +9 -9
  98. package/src/services/abstract.ts +75 -72
  99. package/src/services/associate-roles.test.ts +27 -27
  100. package/src/services/associate-roles.ts +7 -7
  101. package/src/services/attribute-group.ts +7 -7
  102. package/src/services/business-units.test.ts +28 -28
  103. package/src/services/business-units.ts +7 -7
  104. package/src/services/cart-discount.test.ts +199 -199
  105. package/src/services/cart-discount.ts +7 -7
  106. package/src/services/cart.test.ts +261 -261
  107. package/src/services/cart.ts +22 -21
  108. package/src/services/category.test.ts +121 -121
  109. package/src/services/category.ts +7 -7
  110. package/src/services/channel.ts +7 -7
  111. package/src/services/custom-object.test.ts +130 -130
  112. package/src/services/custom-object.ts +34 -31
  113. package/src/services/customer-group.ts +7 -7
  114. package/src/services/customer.test.ts +205 -205
  115. package/src/services/customer.ts +31 -29
  116. package/src/services/discount-code.ts +7 -7
  117. package/src/services/extension.ts +7 -7
  118. package/src/services/index.ts +85 -81
  119. package/src/services/inventory-entry.test.ts +106 -106
  120. package/src/services/inventory-entry.ts +7 -7
  121. package/src/services/my-cart.test.ts +56 -56
  122. package/src/services/my-cart.ts +20 -20
  123. package/src/services/my-customer.test.ts +155 -104
  124. package/src/services/my-customer.ts +61 -75
  125. package/src/services/my-order.ts +16 -16
  126. package/src/services/my-payment.test.ts +40 -40
  127. package/src/services/my-payment.ts +7 -7
  128. package/src/services/my-shopping-list.ts +7 -7
  129. package/src/services/order.test.ts +243 -243
  130. package/src/services/order.ts +23 -18
  131. package/src/services/payment.test.ts +40 -40
  132. package/src/services/payment.ts +7 -7
  133. package/src/services/product-discount.ts +7 -7
  134. package/src/services/product-projection.test.ts +190 -190
  135. package/src/services/product-projection.ts +34 -32
  136. package/src/services/product-selection.test.ts +19 -19
  137. package/src/services/product-selection.ts +7 -7
  138. package/src/services/product-type.test.ts +38 -38
  139. package/src/services/product-type.ts +7 -7
  140. package/src/services/product.test.ts +658 -656
  141. package/src/services/product.ts +7 -7
  142. package/src/services/project.test.ts +24 -24
  143. package/src/services/project.ts +17 -17
  144. package/src/services/reviews.ts +7 -7
  145. package/src/services/shipping-method.test.ts +78 -78
  146. package/src/services/shipping-method.ts +16 -16
  147. package/src/services/shopping-list.test.ts +170 -170
  148. package/src/services/shopping-list.ts +7 -7
  149. package/src/services/standalone-price.test.ts +112 -112
  150. package/src/services/standalone-price.ts +7 -7
  151. package/src/services/state.test.ts +30 -30
  152. package/src/services/state.ts +7 -7
  153. package/src/services/store.test.ts +40 -40
  154. package/src/services/store.ts +7 -7
  155. package/src/services/subscription.ts +7 -7
  156. package/src/services/tax-category.test.ts +43 -43
  157. package/src/services/tax-category.ts +7 -7
  158. package/src/services/type.ts +7 -7
  159. package/src/services/zone.ts +7 -7
  160. package/src/shippingCalculator.test.ts +43 -43
  161. package/src/shippingCalculator.ts +23 -23
  162. package/src/storage/abstract.ts +36 -34
  163. package/src/storage/in-memory.ts +237 -233
  164. package/src/storage/index.ts +2 -2
  165. package/src/types.ts +91 -91
  166. package/src/repositories/cart-discount.ts +0 -219
  167. package/src/repositories/cart.ts +0 -659
  168. package/src/repositories/category.ts +0 -256
  169. package/src/repositories/customer.ts +0 -228
  170. package/src/repositories/discount-code.ts +0 -181
  171. package/src/repositories/inventory-entry.ts +0 -109
  172. package/src/repositories/order.ts +0 -514
  173. package/src/repositories/payment.ts +0 -342
  174. package/src/repositories/product.ts +0 -1106
  175. package/src/repositories/shipping-method.ts +0 -312
  176. package/src/repositories/shopping-list.ts +0 -392
  177. package/src/repositories/tax-category.ts +0 -111
  178. package/src/repositories/type.ts +0 -172
@@ -4,212 +4,264 @@ import type {
4
4
  QueryParam,
5
5
  ResourceNotFoundError,
6
6
  UpdateAction,
7
- } from '@commercetools/platform-sdk'
8
- import deepEqual from 'deep-equal'
9
- import { CommercetoolsError } from '../exceptions.js'
10
- import { cloneObject } from '../helpers.js'
11
- import { AbstractStorage } from '../storage/index.js'
12
- import { ResourceMap, ResourceType, ShallowWritable } from './../types.js'
13
- import { checkConcurrentModification } from './errors.js'
7
+ } from "@commercetools/platform-sdk";
8
+ import deepEqual from "deep-equal";
9
+ import { CommercetoolsError } from "~src/exceptions";
10
+ import { cloneObject } from "../helpers";
11
+ import { AbstractStorage } from "../storage";
12
+ import {
13
+ ResourceMap,
14
+ ResourceType,
15
+ ShallowWritable,
16
+ Writable,
17
+ } from "./../types";
18
+ import { checkConcurrentModification } from "./errors";
14
19
 
15
20
  export type QueryParams = {
16
- expand?: string[]
17
- where?: string[]
18
- offset?: number
19
- limit?: number
21
+ expand?: string[];
22
+ where?: string[];
23
+ offset?: number;
24
+ limit?: number;
20
25
 
21
26
  // Predicate var values. Should always start with `var.`
22
- [key: string]: QueryParam
23
- }
27
+ [key: string]: QueryParam;
28
+ };
24
29
 
25
30
  export type GetParams = {
26
- expand?: string[]
27
- }
31
+ expand?: string[];
32
+ };
28
33
 
29
34
  export type RepositoryContext = {
30
- projectKey: string
31
- storeKey?: string
32
- }
35
+ projectKey: string;
36
+ storeKey?: string;
37
+ };
33
38
 
34
39
  export abstract class AbstractRepository<R extends BaseResource | Project> {
35
- protected _storage: AbstractStorage
36
- protected actions: Partial<
37
- Record<
38
- any,
39
- (context: RepositoryContext, resource: any, action: any) => void
40
- >
41
- > = {}
40
+ protected _storage: AbstractStorage;
41
+
42
+ protected actions: AbstractUpdateHandler | undefined;
42
43
 
43
44
  constructor(storage: AbstractStorage) {
44
- this._storage = storage
45
+ this._storage = storage;
45
46
  }
46
47
 
47
- abstract saveNew({ projectKey }: RepositoryContext, resource: R): void
48
+ abstract saveNew({ projectKey }: RepositoryContext, resource: R): void;
48
49
 
49
50
  abstract saveUpdate(
50
51
  { projectKey }: RepositoryContext,
51
52
  version: number,
52
- resource: R
53
- ): void
53
+ resource: R,
54
+ ): void;
55
+
56
+ abstract postProcessResource(resource: any): any;
54
57
 
55
58
  processUpdateActions(
56
59
  context: RepositoryContext,
57
60
  resource: R,
58
61
  version: number,
59
- actions: UpdateAction[]
62
+ actions: UpdateAction[],
60
63
  ): R {
61
- // Deep-copy
62
- const updatedResource = cloneObject(resource) as ShallowWritable<R>
63
- const identifier = (resource as BaseResource).id
64
- ? (resource as BaseResource).id
65
- : (resource as Project).key
66
-
67
- actions.forEach((action) => {
68
- const updateFunc = this.actions[action.action]
69
-
70
- if (!updateFunc) {
71
- console.error(`No mock implemented for update action ${action.action}`)
72
- throw new Error(
73
- `No mock implemented for update action ${action.action}`
74
- )
75
- }
76
-
77
- const beforeUpdate = cloneObject(resource)
78
- updateFunc(context, updatedResource, action)
79
-
80
- // Check if the object is updated. We need to increase the version of
81
- // an object per action which does an actual modification.
82
- // This isn't the most performant method to do this (the update action
83
- // should return a flag) but for now the easiest.
84
- if (!deepEqual(beforeUpdate, updatedResource)) {
85
- // We only check the version when there is an actual modification to
86
- // be stored.
87
- checkConcurrentModification(resource.version, version, identifier)
64
+ if (!this.actions) {
65
+ throw new Error("No actions defined");
66
+ }
88
67
 
89
- updatedResource.version += 1
90
- }
91
- })
68
+ const updatedResource = this.actions.apply(
69
+ context,
70
+ resource,
71
+ version,
72
+ actions,
73
+ );
92
74
 
93
75
  // If all actions succeeded we write the new version
94
76
  // to the storage.
95
77
  if (resource.version != updatedResource.version) {
96
- this.saveUpdate(context, version, updatedResource)
78
+ this.saveUpdate(context, version, updatedResource);
97
79
  }
98
80
 
99
- const result = this.postProcessResource(updatedResource)
81
+ const result = this.postProcessResource(updatedResource);
100
82
  if (!result) {
101
- throw new Error('invalid post process action')
83
+ throw new Error("invalid post process action");
102
84
  }
103
- return result
85
+ return result;
104
86
  }
105
-
106
- abstract postProcessResource(resource: any): any
107
87
  }
108
88
 
109
89
  export abstract class AbstractResourceRepository<
110
90
  T extends ResourceType,
111
91
  > extends AbstractRepository<ResourceMap[T]> {
112
- abstract create(context: RepositoryContext, draft: any): ResourceMap[T]
113
- abstract getTypeId(): T
92
+ protected _typeId: T;
114
93
 
115
- constructor(storage: AbstractStorage) {
116
- super(storage)
94
+ constructor(typeId: T, storage: AbstractStorage) {
95
+ super(storage);
96
+ this._typeId = typeId;
117
97
  }
118
98
 
119
- postProcessResource(resource: ResourceMap[T]): ResourceMap[T] {
120
- return resource
121
- }
99
+ abstract create(context: RepositoryContext, draft: any): ResourceMap[T];
122
100
 
123
- query(context: RepositoryContext, params: QueryParams = {}) {
124
- const result = this._storage.query(context.projectKey, this.getTypeId(), {
125
- ...params,
126
- })
101
+ protected getTypeId(): T {
102
+ return this._typeId;
103
+ }
127
104
 
128
- // @ts-ignore
129
- result.results = result.results.map(this.postProcessResource)
130
- return result
105
+ delete(
106
+ context: RepositoryContext,
107
+ id: string,
108
+ params: GetParams = {},
109
+ ): ResourceMap[T] | null {
110
+ const resource = this._storage.delete(
111
+ context.projectKey,
112
+ this.getTypeId(),
113
+ id,
114
+ params,
115
+ );
116
+ return resource ? this.postProcessResource(resource) : null;
131
117
  }
132
118
 
133
119
  get(
134
120
  context: RepositoryContext,
135
121
  id: string,
136
- params: GetParams = {}
122
+ params: GetParams = {},
137
123
  ): ResourceMap[T] | null {
138
124
  const resource = this._storage.get(
139
125
  context.projectKey,
140
126
  this.getTypeId(),
141
127
  id,
142
- params
143
- )
144
- return resource ? this.postProcessResource(resource) : null
128
+ params,
129
+ );
130
+ return resource ? this.postProcessResource(resource) : null;
145
131
  }
146
132
 
147
133
  getByKey(
148
134
  context: RepositoryContext,
149
135
  key: string,
150
- params: GetParams = {}
136
+ params: GetParams = {},
151
137
  ): ResourceMap[T] | null {
152
138
  const resource = this._storage.getByKey(
153
139
  context.projectKey,
154
140
  this.getTypeId(),
155
141
  key,
156
- params
157
- )
158
- return resource ? this.postProcessResource(resource) : null
142
+ params,
143
+ );
144
+ return resource ? this.postProcessResource(resource) : null;
159
145
  }
160
146
 
161
- delete(
162
- context: RepositoryContext,
163
- id: string,
164
- params: GetParams = {}
165
- ): ResourceMap[T] | null {
166
- const resource = this._storage.delete(
167
- context.projectKey,
168
- this.getTypeId(),
169
- id,
170
- params
171
- )
172
- return resource ? this.postProcessResource(resource) : null
147
+ postProcessResource(resource: ResourceMap[T]): ResourceMap[T] {
148
+ return resource;
149
+ }
150
+
151
+ query(context: RepositoryContext, params: QueryParams = {}) {
152
+ const result = this._storage.query(context.projectKey, this.getTypeId(), {
153
+ ...params,
154
+ });
155
+
156
+ // @ts-ignore
157
+ result.results = result.results.map(this.postProcessResource);
158
+ return result;
173
159
  }
174
160
 
175
161
  saveNew(
176
162
  context: RepositoryContext,
177
- resource: ShallowWritable<ResourceMap[T]>
178
- ) {
179
- resource.version = 1
180
- this._storage.add(context.projectKey, this.getTypeId(), resource as any)
163
+ resource: ShallowWritable<ResourceMap[T]>,
164
+ ): ResourceMap[T] {
165
+ resource.version = 1;
166
+ return this._storage.add(
167
+ context.projectKey,
168
+ this.getTypeId(),
169
+ resource as any,
170
+ );
181
171
  }
182
172
 
183
173
  saveUpdate(
184
174
  context: RepositoryContext,
185
175
  version: number,
186
- resource: ShallowWritable<ResourceMap[T]>
176
+ resource: ShallowWritable<ResourceMap[T]>,
187
177
  ) {
188
178
  // Check if the resource still exists.
189
179
  const current = this._storage.get(
190
180
  context.projectKey,
191
181
  this.getTypeId(),
192
- resource.id
193
- )
182
+ resource.id,
183
+ );
194
184
  if (!current) {
195
185
  throw new CommercetoolsError<ResourceNotFoundError>(
196
186
  {
197
- code: 'ResourceNotFound',
198
- message: 'Resource not found while updating',
187
+ code: "ResourceNotFound",
188
+ message: "Resource not found while updating",
199
189
  },
200
- 400
201
- )
190
+ 400,
191
+ );
202
192
  }
203
193
 
204
- checkConcurrentModification(current.version, version, resource.id)
194
+ checkConcurrentModification(current.version, version, resource.id);
205
195
 
206
196
  if (current.version === resource.version) {
207
- throw new Error('Internal error: no changes to save')
197
+ throw new Error("Internal error: no changes to save");
208
198
  }
209
- resource.lastModifiedAt = new Date().toISOString()
199
+ resource.lastModifiedAt = new Date().toISOString();
200
+
201
+ this._storage.add(context.projectKey, this.getTypeId(), resource as any);
202
+
203
+ return resource;
204
+ }
205
+ }
210
206
 
211
- this._storage.add(context.projectKey, this.getTypeId(), resource as any)
207
+ type UpdateActionHandlerMethod<A, T> = (
208
+ context: RepositoryContext,
209
+ resource: Writable<A>,
210
+ action: T,
211
+ ) => void;
212
212
 
213
- return resource
213
+ export type UpdateHandlerInterface<
214
+ A extends BaseResource | Project,
215
+ T extends UpdateAction,
216
+ > = {
217
+ [P in T as P["action"]]: UpdateActionHandlerMethod<A, P>;
218
+ };
219
+
220
+ export class AbstractUpdateHandler {
221
+ constructor(protected _storage: AbstractStorage) {
222
+ if (!_storage) {
223
+ throw new Error("No storage provided");
224
+ }
225
+ this._storage = _storage;
226
+ }
227
+
228
+ apply<R extends BaseResource | Project>(
229
+ context: RepositoryContext,
230
+ resource: R,
231
+ version: number,
232
+ actions: UpdateAction[],
233
+ ): R {
234
+ const updatedResource = cloneObject(resource) as ShallowWritable<R>;
235
+ const identifier = (resource as BaseResource).id
236
+ ? (resource as BaseResource).id
237
+ : (resource as Project).key;
238
+
239
+ for (const action of actions) {
240
+ // @ts-ignore
241
+ const updateFunc = this[action.action].bind(this);
242
+
243
+ if (!updateFunc) {
244
+ console.error(`No mock implemented for update action ${action.action}`);
245
+ throw new Error(
246
+ `No mock implemented for update action ${action.action}`,
247
+ );
248
+ }
249
+
250
+ const beforeUpdate = cloneObject(resource);
251
+ updateFunc(context, updatedResource, action);
252
+
253
+ // Check if the object is updated. We need to increase the version of
254
+ // an object per action which does an actual modification.
255
+ // This isn't the most performant method to do this (the update action
256
+ // should return a flag) but for now the easiest.
257
+ if (!deepEqual(beforeUpdate, updatedResource)) {
258
+ // We only check the version when there is an actual modification to
259
+ // be stored.
260
+ checkConcurrentModification(resource.version, version, identifier);
261
+
262
+ updatedResource.version += 1;
263
+ }
264
+ }
265
+ return updatedResource;
214
266
  }
215
267
  }
@@ -7,18 +7,23 @@ import type {
7
7
  AssociateRoleSetCustomFieldAction,
8
8
  AssociateRoleSetNameAction,
9
9
  AssociateRoleSetPermissionsAction,
10
- } from '@commercetools/platform-sdk'
10
+ AssociateRoleUpdateAction,
11
+ } from "@commercetools/platform-sdk";
12
+ import { getBaseResourceProperties } from "../helpers";
13
+ import { AbstractStorage } from "../storage/abstract";
14
+ import { Writable } from "../types";
11
15
  import {
12
16
  AbstractResourceRepository,
17
+ AbstractUpdateHandler,
18
+ UpdateHandlerInterface,
13
19
  type RepositoryContext,
14
- } from './abstract.js'
15
- import { getBaseResourceProperties } from '../helpers.js'
16
- import { createCustomFields } from './helpers.js'
17
- import { Writable } from '../types.js'
20
+ } from "./abstract";
21
+ import { createCustomFields } from "./helpers";
18
22
 
19
- export class AssociateRoleRepository extends AbstractResourceRepository<'associate-role'> {
20
- getTypeId() {
21
- return 'associate-role' as const
23
+ export class AssociateRoleRepository extends AbstractResourceRepository<"associate-role"> {
24
+ constructor(storage: AbstractStorage) {
25
+ super("associate-role", storage);
26
+ this.actions = new AssociateRoleUpdateHandler(this._storage);
22
27
  }
23
28
 
24
29
  create(context: RepositoryContext, draft: AssociateRoleDraft): AssociateRole {
@@ -31,82 +36,90 @@ export class AssociateRoleRepository extends AbstractResourceRepository<'associa
31
36
  custom: createCustomFields(
32
37
  draft.custom,
33
38
  context.projectKey,
34
- this._storage
39
+ this._storage,
35
40
  ),
41
+ };
42
+
43
+ return this.saveNew(context, resource);
44
+ }
45
+ }
46
+
47
+ class AssociateRoleUpdateHandler
48
+ extends AbstractUpdateHandler
49
+ implements
50
+ Partial<UpdateHandlerInterface<AssociateRole, AssociateRoleUpdateAction>>
51
+ {
52
+ addPermission(
53
+ context: RepositoryContext,
54
+ resource: Writable<AssociateRole>,
55
+ { permission }: AssociateRoleAddPermissionAction,
56
+ ) {
57
+ if (!resource.permissions) {
58
+ resource.permissions = [permission];
59
+ } else {
60
+ resource.permissions.push(permission);
36
61
  }
62
+ }
63
+
64
+ changeBuyerAssignable(
65
+ context: RepositoryContext,
66
+ resource: Writable<AssociateRole>,
67
+ { buyerAssignable }: AssociateRoleChangeBuyerAssignableAction,
68
+ ) {
69
+ resource.buyerAssignable = buyerAssignable;
70
+ }
37
71
 
38
- this.saveNew(context, resource)
72
+ removePermission(
73
+ context: RepositoryContext,
74
+ resource: Writable<AssociateRole>,
75
+ { permission }: AssociateRoleRemovePermissionAction,
76
+ ) {
77
+ if (!resource.permissions) {
78
+ return;
79
+ }
39
80
 
40
- return resource
81
+ resource.permissions = resource.permissions.filter((p) => {
82
+ p !== permission;
83
+ });
41
84
  }
42
85
 
43
- actions = {
44
- setName: (
45
- context: RepositoryContext,
46
- resource: Writable<AssociateRole>,
47
- { name }: AssociateRoleSetNameAction
48
- ) => {
49
- resource.name = name
50
- },
51
- setPermissions: (
52
- context: RepositoryContext,
53
- resource: Writable<AssociateRole>,
54
- { permissions }: AssociateRoleSetPermissionsAction
55
- ) => {
56
- resource.permissions = permissions || []
57
- },
58
- setBuyerAssignable: (
59
- context: RepositoryContext,
60
- resource: Writable<AssociateRole>,
61
- { buyerAssignable }: AssociateRoleChangeBuyerAssignableAction
62
- ) => {
63
- resource.buyerAssignable = buyerAssignable
64
- },
65
- changeBuyerAssignable: (
66
- context: RepositoryContext,
67
- resource: Writable<AssociateRole>,
68
- { buyerAssignable }: AssociateRoleChangeBuyerAssignableAction
69
- ) => {
70
- resource.buyerAssignable = buyerAssignable
71
- },
72
- setCustomFields: (
73
- context: RepositoryContext,
74
- resource: Writable<AssociateRole>,
75
- { name, value }: AssociateRoleSetCustomFieldAction
76
- ) => {
77
- if (!resource.custom) {
78
- return
79
- }
86
+ setBuyerAssignable(
87
+ context: RepositoryContext,
88
+ resource: Writable<AssociateRole>,
89
+ { buyerAssignable }: AssociateRoleChangeBuyerAssignableAction,
90
+ ) {
91
+ resource.buyerAssignable = buyerAssignable;
92
+ }
93
+
94
+ setCustomFields(
95
+ context: RepositoryContext,
96
+ resource: Writable<AssociateRole>,
97
+ { name, value }: AssociateRoleSetCustomFieldAction,
98
+ ) {
99
+ if (!resource.custom) {
100
+ return;
101
+ }
80
102
 
81
- if (value === null) {
82
- delete resource.custom.fields[name]
83
- } else {
84
- resource.custom.fields[name] = value
85
- }
86
- },
87
- addPermission: (
88
- context: RepositoryContext,
89
- resource: Writable<AssociateRole>,
90
- { permission }: AssociateRoleAddPermissionAction
91
- ) => {
92
- if (!resource.permissions) {
93
- resource.permissions = [permission]
94
- } else {
95
- resource.permissions.push(permission)
96
- }
97
- },
98
- removePermission: (
99
- context: RepositoryContext,
100
- resource: Writable<AssociateRole>,
101
- { permission }: AssociateRoleRemovePermissionAction
102
- ) => {
103
- if (!resource.permissions) {
104
- return
105
- }
103
+ if (value === null) {
104
+ delete resource.custom.fields[name];
105
+ } else {
106
+ resource.custom.fields[name] = value;
107
+ }
108
+ }
109
+
110
+ setName(
111
+ context: RepositoryContext,
112
+ resource: Writable<AssociateRole>,
113
+ { name }: AssociateRoleSetNameAction,
114
+ ) {
115
+ resource.name = name;
116
+ }
106
117
 
107
- resource.permissions = resource.permissions.filter((p) => {
108
- p !== permission
109
- })
110
- },
118
+ setPermissions(
119
+ context: RepositoryContext,
120
+ resource: Writable<AssociateRole>,
121
+ { permissions }: AssociateRoleSetPermissionsAction,
122
+ ) {
123
+ resource.permissions = permissions || [];
111
124
  }
112
125
  }
@@ -5,22 +5,27 @@ import type {
5
5
  AttributeGroupSetAttributesAction,
6
6
  AttributeGroupSetDescriptionAction,
7
7
  AttributeGroupSetKeyAction,
8
- } from '@commercetools/platform-sdk'
8
+ AttributeGroupUpdateAction,
9
+ } from "@commercetools/platform-sdk";
10
+ import { getBaseResourceProperties } from "../helpers";
11
+ import { AbstractStorage } from "../storage/abstract";
12
+ import { Writable } from "../types";
9
13
  import {
10
14
  AbstractResourceRepository,
15
+ AbstractUpdateHandler,
16
+ UpdateHandlerInterface,
11
17
  type RepositoryContext,
12
- } from './abstract.js'
13
- import { getBaseResourceProperties } from '../helpers.js'
14
- import { Writable } from '../types.js'
18
+ } from "./abstract";
15
19
 
16
- export class AttributeGroupRepository extends AbstractResourceRepository<'attribute-group'> {
17
- getTypeId() {
18
- return 'attribute-group' as const
20
+ export class AttributeGroupRepository extends AbstractResourceRepository<"attribute-group"> {
21
+ constructor(storage: AbstractStorage) {
22
+ super("attribute-group", storage);
23
+ this.actions = new AttributeGroupUpdateHandler(this._storage);
19
24
  }
20
25
 
21
26
  create(
22
27
  context: RepositoryContext,
23
- draft: AttributeGroupDraft
28
+ draft: AttributeGroupDraft,
24
29
  ): AttributeGroup {
25
30
  const resource: AttributeGroup = {
26
31
  ...getBaseResourceProperties(),
@@ -28,39 +33,45 @@ export class AttributeGroupRepository extends AbstractResourceRepository<'attrib
28
33
  description: draft.description,
29
34
  key: draft.key,
30
35
  attributes: draft.attributes,
31
- }
32
- this.saveNew(context, resource)
33
- return resource
36
+ };
37
+ return this.saveNew(context, resource);
38
+ }
39
+ }
40
+
41
+ class AttributeGroupUpdateHandler
42
+ extends AbstractUpdateHandler
43
+ implements
44
+ Partial<UpdateHandlerInterface<AttributeGroup, AttributeGroupUpdateAction>>
45
+ {
46
+ changeName(
47
+ _context: RepositoryContext,
48
+ resource: Writable<AttributeGroup>,
49
+ { name }: AttributeGroupChangeNameAction,
50
+ ) {
51
+ resource.name = name;
52
+ }
53
+
54
+ setAttributes(
55
+ _context: RepositoryContext,
56
+ resource: Writable<AttributeGroup>,
57
+ { attributes }: AttributeGroupSetAttributesAction,
58
+ ) {
59
+ resource.attributes = attributes;
60
+ }
61
+
62
+ setDescription(
63
+ _context: RepositoryContext,
64
+ resource: Writable<AttributeGroup>,
65
+ { description }: AttributeGroupSetDescriptionAction,
66
+ ) {
67
+ resource.description = description;
34
68
  }
35
69
 
36
- actions = {
37
- setAttributes: (
38
- _context: RepositoryContext,
39
- resource: Writable<AttributeGroup>,
40
- { attributes }: AttributeGroupSetAttributesAction
41
- ) => {
42
- resource.attributes = attributes
43
- },
44
- changeName: (
45
- _context: RepositoryContext,
46
- resource: Writable<AttributeGroup>,
47
- { name }: AttributeGroupChangeNameAction
48
- ) => {
49
- resource.name = name
50
- },
51
- setDescription: (
52
- _context: RepositoryContext,
53
- resource: Writable<AttributeGroup>,
54
- { description }: AttributeGroupSetDescriptionAction
55
- ) => {
56
- resource.description = description
57
- },
58
- setKey: (
59
- _context: RepositoryContext,
60
- resource: Writable<AttributeGroup>,
61
- { key }: AttributeGroupSetKeyAction
62
- ) => {
63
- resource.key = key
64
- },
70
+ setKey(
71
+ _context: RepositoryContext,
72
+ resource: Writable<AttributeGroup>,
73
+ { key }: AttributeGroupSetKeyAction,
74
+ ) {
75
+ resource.key = key;
65
76
  }
66
77
  }