@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.
Files changed (178) hide show
  1. package/dist/index.cjs +4186 -3974
  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 +4186 -3974
  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 +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/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 +23 -36
  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
@@ -3,47 +3,50 @@ import type {
3
3
  ProductSelectionChangeNameAction,
4
4
  ProductSelectionDraft,
5
5
  ProductSelectionUpdateAction,
6
- } from '@commercetools/platform-sdk'
7
- import { getBaseResourceProperties } from '../helpers.js'
8
- import type { Writable } from '../types.js'
9
- import { AbstractResourceRepository, RepositoryContext } from './abstract.js'
6
+ } from "@commercetools/platform-sdk";
7
+ import { getBaseResourceProperties } from "../helpers";
8
+ import { AbstractStorage } from "../storage/abstract";
9
+ import type { Writable } from "../types";
10
+ import {
11
+ AbstractResourceRepository,
12
+ AbstractUpdateHandler,
13
+ RepositoryContext,
14
+ UpdateHandlerInterface,
15
+ } from "./abstract";
10
16
 
11
- export class ProductSelectionRepository extends AbstractResourceRepository<'product-selection'> {
12
- getTypeId() {
13
- return 'product-selection' as const
17
+ export class ProductSelectionRepository extends AbstractResourceRepository<"product-selection"> {
18
+ constructor(storage: AbstractStorage) {
19
+ super("product-selection", storage);
20
+ this.actions = new ProductSelectionUpdateHandler(this._storage);
14
21
  }
15
22
 
16
23
  create(
17
24
  context: RepositoryContext,
18
- draft: ProductSelectionDraft
25
+ draft: ProductSelectionDraft,
19
26
  ): ProductSelection {
20
27
  const resource: ProductSelection = {
21
28
  ...getBaseResourceProperties(),
22
29
  productCount: 0,
23
30
  key: draft.key,
24
31
  name: draft.name,
25
- mode: 'Individual',
26
- }
27
- this.saveNew(context, resource)
28
- return resource
32
+ mode: "Individual",
33
+ };
34
+ return this.saveNew(context, resource);
29
35
  }
36
+ }
30
37
 
31
- actions: Partial<
32
- Record<
33
- ProductSelectionUpdateAction['action'],
34
- (
35
- context: RepositoryContext,
36
- resource: Writable<ProductSelection>,
37
- action: any
38
- ) => void
38
+ class ProductSelectionUpdateHandler
39
+ extends AbstractUpdateHandler
40
+ implements
41
+ Partial<
42
+ UpdateHandlerInterface<ProductSelection, ProductSelectionUpdateAction>
39
43
  >
40
- > = {
41
- changeName: (
42
- context: RepositoryContext,
43
- resource: Writable<ProductSelection>,
44
- { name }: ProductSelectionChangeNameAction
45
- ) => {
46
- resource.name = name
47
- },
44
+ {
45
+ changeName(
46
+ context: RepositoryContext,
47
+ resource: Writable<ProductSelection>,
48
+ { name }: ProductSelectionChangeNameAction,
49
+ ) {
50
+ resource.name = name;
48
51
  }
49
52
  }
@@ -11,14 +11,21 @@ import type {
11
11
  ProductTypeRemoveAttributeDefinitionAction,
12
12
  ProductTypeRemoveEnumValuesAction,
13
13
  ProductTypeUpdateAction,
14
- } from '@commercetools/platform-sdk'
15
- import { getBaseResourceProperties } from '../helpers.js'
16
- import type { Writable } from '../types.js'
17
- import { AbstractResourceRepository, RepositoryContext } from './abstract.js'
14
+ } from "@commercetools/platform-sdk";
15
+ import { getBaseResourceProperties } from "../helpers";
16
+ import { AbstractStorage } from "../storage/abstract";
17
+ import type { Writable } from "../types";
18
+ import {
19
+ AbstractResourceRepository,
20
+ AbstractUpdateHandler,
21
+ RepositoryContext,
22
+ UpdateHandlerInterface,
23
+ } from "./abstract";
18
24
 
19
- export class ProductTypeRepository extends AbstractResourceRepository<'product-type'> {
20
- getTypeId() {
21
- return 'product-type' as const
25
+ export class ProductTypeRepository extends AbstractResourceRepository<"product-type"> {
26
+ constructor(storage: AbstractStorage) {
27
+ super("product-type", storage);
28
+ this.actions = new ProductTypeUpdateHandler(storage);
22
29
  }
23
30
 
24
31
  create(context: RepositoryContext, draft: ProductTypeDraft): ProductType {
@@ -28,146 +35,141 @@ export class ProductTypeRepository extends AbstractResourceRepository<'product-t
28
35
  name: draft.name,
29
36
  description: draft.description,
30
37
  attributes: (draft.attributes ?? []).map((a) =>
31
- this.attributeDefinitionFromAttributeDefinitionDraft(context, a)
38
+ attributeDefinitionFromAttributeDefinitionDraft(context, a),
32
39
  ),
33
- }
40
+ };
34
41
 
35
- this.saveNew(context, resource)
36
- return resource
42
+ return this.saveNew(context, resource);
37
43
  }
44
+ }
38
45
 
39
- attributeDefinitionFromAttributeDefinitionDraft = (
40
- _context: RepositoryContext,
41
- draft: AttributeDefinitionDraft
42
- ): AttributeDefinition => ({
43
- ...draft,
44
- attributeConstraint: draft.attributeConstraint ?? 'None',
45
- inputHint: draft.inputHint ?? 'SingleLine',
46
- inputTip:
47
- draft.inputTip && Object.keys(draft.inputTip).length > 0
48
- ? draft.inputTip
49
- : undefined,
50
- isSearchable: draft.isSearchable ?? true,
51
- })
46
+ const attributeDefinitionFromAttributeDefinitionDraft = (
47
+ _context: RepositoryContext,
48
+ draft: AttributeDefinitionDraft,
49
+ ): AttributeDefinition => ({
50
+ ...draft,
51
+ attributeConstraint: draft.attributeConstraint ?? "None",
52
+ inputHint: draft.inputHint ?? "SingleLine",
53
+ inputTip:
54
+ draft.inputTip && Object.keys(draft.inputTip).length > 0
55
+ ? draft.inputTip
56
+ : undefined,
57
+ isSearchable: draft.isSearchable ?? true,
58
+ });
52
59
 
53
- actions: Partial<
54
- Record<
55
- ProductTypeUpdateAction['action'],
56
- (
57
- context: RepositoryContext,
58
- resource: Writable<ProductType>,
59
- action: any
60
- ) => void
61
- >
62
- > = {
63
- changeLocalizedEnumValueLabel: (
64
- context: RepositoryContext,
65
- resource: Writable<ProductType>,
66
- {
67
- attributeName,
68
- newValue,
69
- }: ProductTypeChangeLocalizedEnumValueLabelAction
70
- ) => {
71
- const updateAttributeType = (type: Writable<AttributeType>) => {
72
- switch (type.name) {
73
- case 'lenum':
74
- type.values.forEach((v) => {
75
- if (v.key === newValue.key) {
76
- v.label = newValue.label
77
- }
78
- })
79
- return
80
- case 'set':
81
- updateAttributeType(type.elementType)
82
- return
83
- }
84
- }
60
+ class ProductTypeUpdateHandler
61
+ extends AbstractUpdateHandler
62
+ implements
63
+ Partial<UpdateHandlerInterface<ProductType, ProductTypeUpdateAction>>
64
+ {
65
+ addAttributeDefinition(
66
+ context: RepositoryContext,
67
+ resource: Writable<ProductType>,
68
+ { attribute }: ProductTypeAddAttributeDefinitionAction,
69
+ ) {
70
+ resource.attributes?.push(
71
+ attributeDefinitionFromAttributeDefinitionDraft(context, attribute),
72
+ );
73
+ }
85
74
 
86
- resource.attributes?.forEach((value) => {
87
- if (value.name === attributeName) {
88
- updateAttributeType(value.type)
89
- }
90
- })
91
- },
92
- changeLabel: (
93
- context: RepositoryContext,
94
- resource: Writable<ProductType>,
95
- { attributeName, label }: ProductTypeChangeLabelAction
96
- ) => {
97
- resource.attributes?.forEach((value) => {
98
- if (value.name === attributeName) {
99
- value.label = label
100
- }
101
- })
102
- },
103
- addAttributeDefinition: (
104
- context: RepositoryContext,
105
- resource: Writable<ProductType>,
106
- { attribute }: ProductTypeAddAttributeDefinitionAction
107
- ) => {
108
- resource.attributes?.push(
109
- this.attributeDefinitionFromAttributeDefinitionDraft(context, attribute)
110
- )
111
- },
112
- changeAttributeOrderByName: (
113
- context: RepositoryContext,
114
- resource: Writable<ProductType>,
115
- { attributeNames }: ProductTypeChangeAttributeOrderByNameAction
116
- ) => {
117
- const attrs = new Map(
118
- resource.attributes?.map((item) => [item.name, item])
119
- )
120
- const result: AttributeDefinition[] = []
121
- let current = resource.attributes
75
+ changeAttributeOrderByName(
76
+ context: RepositoryContext,
77
+ resource: Writable<ProductType>,
78
+ { attributeNames }: ProductTypeChangeAttributeOrderByNameAction,
79
+ ) {
80
+ const attrs = new Map(
81
+ resource.attributes?.map((item) => [item.name, item]),
82
+ );
83
+ const result: AttributeDefinition[] = [];
84
+ let current = resource.attributes;
122
85
 
123
- attributeNames.forEach((attrName) => {
124
- const attr = attrs.get(attrName)
125
- if (attr === undefined) {
126
- throw new Error('New attr')
127
- }
128
- result.push(attr)
86
+ attributeNames.forEach((attrName) => {
87
+ const attr = attrs.get(attrName);
88
+ if (attr === undefined) {
89
+ throw new Error("New attr");
90
+ }
91
+ result.push(attr);
129
92
 
130
- // Remove from current items
131
- current = current?.filter((f) => f.name !== attrName)
132
- })
93
+ // Remove from current items
94
+ current = current?.filter((f) => f.name !== attrName);
95
+ });
96
+
97
+ resource.attributes = result;
98
+ // Add attrs which were not specified in the order as last items. Not
99
+ // sure if this follows commercetools
100
+ if (current) {
101
+ resource.attributes.push(...current);
102
+ }
103
+ }
133
104
 
134
- resource.attributes = result
135
- // Add attrs which were not specified in the order as last items. Not
136
- // sure if this follows commercetools
137
- if (current) {
138
- resource.attributes.push(...current)
105
+ changeLabel(
106
+ context: RepositoryContext,
107
+ resource: Writable<ProductType>,
108
+ { attributeName, label }: ProductTypeChangeLabelAction,
109
+ ) {
110
+ resource.attributes?.forEach((value) => {
111
+ if (value.name === attributeName) {
112
+ value.label = label;
139
113
  }
140
- },
141
- removeAttributeDefinition: (
142
- context: RepositoryContext,
143
- resource: Writable<ProductType>,
144
- { name }: ProductTypeRemoveAttributeDefinitionAction
145
- ) => {
146
- resource.attributes = resource.attributes?.filter((f) => f.name !== name)
147
- },
148
- removeEnumValues: (
149
- context: RepositoryContext,
150
- resource: Writable<ProductType>,
151
- { attributeName, keys }: ProductTypeRemoveEnumValuesAction
152
- ) => {
153
- resource.attributes?.forEach((attr) => {
154
- if (attr.name == attributeName) {
155
- if (attr.type.name == 'enum') {
156
- attr.type.values = attr.type.values.filter(
157
- (v) => !keys.includes(v.key)
158
- )
159
- }
114
+ });
115
+ }
160
116
 
161
- if (attr.type.name == 'set') {
162
- if (attr.type.elementType.name == 'enum') {
163
- attr.type.elementType.values =
164
- attr.type.elementType.values.filter(
165
- (v) => !keys.includes(v.key)
166
- )
117
+ changeLocalizedEnumValueLabel(
118
+ context: RepositoryContext,
119
+ resource: Writable<ProductType>,
120
+ { attributeName, newValue }: ProductTypeChangeLocalizedEnumValueLabelAction,
121
+ ) {
122
+ const updateAttributeType = (type: Writable<AttributeType>) => {
123
+ switch (type.name) {
124
+ case "lenum":
125
+ type.values.forEach((v) => {
126
+ if (v.key === newValue.key) {
127
+ v.label = newValue.label;
167
128
  }
129
+ });
130
+ return;
131
+ case "set":
132
+ updateAttributeType(type.elementType);
133
+ return;
134
+ }
135
+ };
136
+
137
+ resource.attributes?.forEach((value) => {
138
+ if (value.name === attributeName) {
139
+ updateAttributeType(value.type);
140
+ }
141
+ });
142
+ }
143
+
144
+ removeAttributeDefinition(
145
+ context: RepositoryContext,
146
+ resource: Writable<ProductType>,
147
+ { name }: ProductTypeRemoveAttributeDefinitionAction,
148
+ ) {
149
+ resource.attributes = resource.attributes?.filter((f) => f.name !== name);
150
+ }
151
+
152
+ removeEnumValues(
153
+ context: RepositoryContext,
154
+ resource: Writable<ProductType>,
155
+ { attributeName, keys }: ProductTypeRemoveEnumValuesAction,
156
+ ) {
157
+ resource.attributes?.forEach((attr) => {
158
+ if (attr.name == attributeName) {
159
+ if (attr.type.name == "enum") {
160
+ attr.type.values = attr.type.values.filter(
161
+ (v) => !keys.includes(v.key),
162
+ );
163
+ }
164
+
165
+ if (attr.type.name == "set") {
166
+ if (attr.type.elementType.name == "enum") {
167
+ attr.type.elementType.values = attr.type.elementType.values.filter(
168
+ (v) => !keys.includes(v.key),
169
+ );
168
170
  }
169
171
  }
170
- })
171
- },
172
+ }
173
+ });
172
174
  }
173
175
  }
@@ -12,137 +12,152 @@ import type {
12
12
  ProjectSetExternalOAuthAction,
13
13
  ProjectSetShippingRateInputTypeAction,
14
14
  ProjectUpdateAction,
15
- } from '@commercetools/platform-sdk'
16
- import { maskSecretValue } from '../lib/masking.js'
17
- import type { Writable } from '../types.js'
18
- import { AbstractRepository, RepositoryContext } from './abstract.js'
15
+ } from "@commercetools/platform-sdk";
16
+ import { maskSecretValue } from "../lib/masking";
17
+ import { AbstractStorage } from "../storage/abstract";
18
+ import type { Writable } from "../types";
19
+ import {
20
+ AbstractRepository,
21
+ AbstractUpdateHandler,
22
+ RepositoryContext,
23
+ UpdateHandlerInterface,
24
+ } from "./abstract";
19
25
 
20
26
  export class ProjectRepository extends AbstractRepository<Project> {
27
+ constructor(storage: AbstractStorage) {
28
+ super(storage);
29
+ this.actions = new ProjectUpdateHandler(this._storage);
30
+ }
31
+
21
32
  get(context: RepositoryContext): Project | null {
22
- const resource = this._storage.getProject(context.projectKey)
23
- return this.postProcessResource(resource)
33
+ const resource = this._storage.getProject(context.projectKey);
34
+ return this.postProcessResource(resource);
24
35
  }
25
36
 
26
37
  postProcessResource(resource: Project): Project {
27
38
  if (resource) {
28
- return maskSecretValue(resource, 'externalOAuth.authorizationHeader')
39
+ return maskSecretValue(resource, "externalOAuth.authorizationHeader");
29
40
  }
30
- return resource
41
+ return resource;
31
42
  }
32
43
 
33
44
  saveNew(context: RepositoryContext, resource: Writable<Project>) {
34
- resource.version = 1
35
- this._storage.saveProject(resource)
45
+ resource.version = 1;
46
+ this._storage.saveProject(resource);
36
47
  }
37
48
 
38
49
  saveUpdate(context: RepositoryContext, version: number, resource: Project) {
39
- this._storage.saveProject(resource)
40
- }
41
-
42
- actions: Partial<
43
- Record<
44
- ProjectUpdateAction['action'],
45
- (
46
- context: RepositoryContext,
47
- resource: Writable<Project>,
48
- action: any
49
- ) => void
50
- >
51
- > = {
52
- changeName: (
53
- context: RepositoryContext,
54
- resource: Writable<Project>,
55
- { name }: ProjectChangeNameAction
56
- ) => {
57
- resource.name = name
58
- },
59
- changeCurrencies: (
60
- context: RepositoryContext,
61
- resource: Writable<Project>,
62
- { currencies }: ProjectChangeCurrenciesAction
63
- ) => {
64
- resource.currencies = currencies
65
- },
66
- changeCountries: (
67
- context: RepositoryContext,
68
- resource: Writable<Project>,
69
- { countries }: ProjectChangeCountriesAction
70
- ) => {
71
- resource.countries = countries
72
- },
73
- changeLanguages: (
74
- context: RepositoryContext,
75
- resource: Writable<Project>,
76
- { languages }: ProjectChangeLanguagesAction
77
- ) => {
78
- resource.languages = languages
79
- },
80
- changeMessagesConfiguration: (
81
- context: RepositoryContext,
82
- resource: Writable<Project>,
83
- { messagesConfiguration }: ProjectChangeMessagesConfigurationAction
84
- ) => {
85
- resource.messages.enabled = messagesConfiguration.enabled
86
- resource.messages.deleteDaysAfterCreation =
87
- messagesConfiguration.deleteDaysAfterCreation
88
- },
89
- changeProductSearchIndexingEnabled: (
90
- context: RepositoryContext,
91
- resource: Writable<Project>,
92
- { enabled }: ProjectChangeProductSearchIndexingEnabledAction
93
- ) => {
94
- if (!resource.searchIndexing?.products) {
95
- throw new Error('Invalid project state')
96
- }
97
- resource.searchIndexing.products.status = enabled
98
- ? 'Activated'
99
- : 'Deactivated'
100
- resource.searchIndexing.products.lastModifiedAt = new Date().toISOString()
101
- },
102
- changeOrderSearchStatus: (
103
- context: RepositoryContext,
104
- resource: Writable<Project>,
105
- { status }: ProjectChangeOrderSearchStatusAction
106
- ) => {
107
- if (!resource.searchIndexing?.orders) {
108
- throw new Error('Invalid project state')
109
- }
110
- resource.searchIndexing.orders.status = status
111
- resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString()
112
- },
113
- setShippingRateInputType: (
114
- context: RepositoryContext,
115
- resource: Writable<Project>,
116
- { shippingRateInputType }: ProjectSetShippingRateInputTypeAction
117
- ) => {
118
- resource.shippingRateInputType = shippingRateInputType
119
- },
120
- setExternalOAuth: (
121
- context: RepositoryContext,
122
- resource: Writable<Project>,
123
- { externalOAuth }: ProjectSetExternalOAuthAction
124
- ) => {
125
- resource.externalOAuth = externalOAuth
126
- },
127
- changeCountryTaxRateFallbackEnabled: (
128
- context: RepositoryContext,
129
- resource: Writable<Project>,
130
- {
131
- countryTaxRateFallbackEnabled,
132
- }: ProjectChangeCountryTaxRateFallbackEnabledAction
133
- ) => {
134
- resource.carts.countryTaxRateFallbackEnabled =
135
- countryTaxRateFallbackEnabled
136
- },
137
- changeCartsConfiguration: (
138
- context: RepositoryContext,
139
- resource: Writable<Project>,
140
- { cartsConfiguration }: ProjectChangeCartsConfigurationAction
141
- ) => {
142
- resource.carts = cartsConfiguration || {
143
- countryTaxRateFallbackEnabled: false,
144
- deleteDaysAfterLastModification: 90,
145
- }
146
- },
50
+ this._storage.saveProject(resource);
51
+ }
52
+ }
53
+
54
+ class ProjectUpdateHandler
55
+ extends AbstractUpdateHandler
56
+ implements Partial<UpdateHandlerInterface<Project, ProjectUpdateAction>>
57
+ {
58
+ changeCartsConfiguration(
59
+ context: RepositoryContext,
60
+ resource: Writable<Project>,
61
+ { cartsConfiguration }: ProjectChangeCartsConfigurationAction,
62
+ ) {
63
+ resource.carts = cartsConfiguration || {
64
+ countryTaxRateFallbackEnabled: false,
65
+ deleteDaysAfterLastModification: 90,
66
+ };
67
+ }
68
+
69
+ changeCountries(
70
+ context: RepositoryContext,
71
+ resource: Writable<Project>,
72
+ { countries }: ProjectChangeCountriesAction,
73
+ ) {
74
+ resource.countries = countries;
75
+ }
76
+
77
+ changeCountryTaxRateFallbackEnabled(
78
+ context: RepositoryContext,
79
+ resource: Writable<Project>,
80
+ {
81
+ countryTaxRateFallbackEnabled,
82
+ }: ProjectChangeCountryTaxRateFallbackEnabledAction,
83
+ ) {
84
+ resource.carts.countryTaxRateFallbackEnabled =
85
+ countryTaxRateFallbackEnabled;
86
+ }
87
+
88
+ changeCurrencies(
89
+ context: RepositoryContext,
90
+ resource: Writable<Project>,
91
+ { currencies }: ProjectChangeCurrenciesAction,
92
+ ) {
93
+ resource.currencies = currencies;
94
+ }
95
+
96
+ changeLanguages(
97
+ context: RepositoryContext,
98
+ resource: Writable<Project>,
99
+ { languages }: ProjectChangeLanguagesAction,
100
+ ) {
101
+ resource.languages = languages;
102
+ }
103
+
104
+ changeMessagesConfiguration(
105
+ context: RepositoryContext,
106
+ resource: Writable<Project>,
107
+ { messagesConfiguration }: ProjectChangeMessagesConfigurationAction,
108
+ ) {
109
+ resource.messages.enabled = messagesConfiguration.enabled;
110
+ resource.messages.deleteDaysAfterCreation =
111
+ messagesConfiguration.deleteDaysAfterCreation;
112
+ }
113
+
114
+ changeName(
115
+ context: RepositoryContext,
116
+ resource: Writable<Project>,
117
+ { name }: ProjectChangeNameAction,
118
+ ) {
119
+ resource.name = name;
120
+ }
121
+
122
+ changeOrderSearchStatus(
123
+ context: RepositoryContext,
124
+ resource: Writable<Project>,
125
+ { status }: ProjectChangeOrderSearchStatusAction,
126
+ ) {
127
+ if (!resource.searchIndexing?.orders) {
128
+ throw new Error("Invalid project state");
129
+ }
130
+ resource.searchIndexing.orders.status = status;
131
+ resource.searchIndexing.orders.lastModifiedAt = new Date().toISOString();
132
+ }
133
+
134
+ changeProductSearchIndexingEnabled(
135
+ context: RepositoryContext,
136
+ resource: Writable<Project>,
137
+ { enabled }: ProjectChangeProductSearchIndexingEnabledAction,
138
+ ) {
139
+ if (!resource.searchIndexing?.products) {
140
+ throw new Error("Invalid project state");
141
+ }
142
+ resource.searchIndexing.products.status = enabled
143
+ ? "Activated"
144
+ : "Deactivated";
145
+ resource.searchIndexing.products.lastModifiedAt = new Date().toISOString();
146
+ }
147
+
148
+ setExternalOAuth(
149
+ context: RepositoryContext,
150
+ resource: Writable<Project>,
151
+ { externalOAuth }: ProjectSetExternalOAuthAction,
152
+ ) {
153
+ resource.externalOAuth = externalOAuth;
154
+ }
155
+
156
+ setShippingRateInputType(
157
+ context: RepositoryContext,
158
+ resource: Writable<Project>,
159
+ { shippingRateInputType }: ProjectSetShippingRateInputTypeAction,
160
+ ) {
161
+ resource.shippingRateInputType = shippingRateInputType;
147
162
  }
148
163
  }