@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
@@ -1,23 +1,33 @@
1
1
  import type {
2
2
  State,
3
+ StateAddRolesAction,
3
4
  StateChangeInitialAction,
4
5
  StateChangeKeyAction,
6
+ StateChangeTypeAction,
5
7
  StateDraft,
6
8
  StateReference,
9
+ StateRemoveRolesAction,
7
10
  StateSetDescriptionAction,
8
11
  StateSetNameAction,
9
12
  StateSetRolesAction,
10
13
  StateSetTransitionsAction,
11
14
  StateUpdateAction,
12
- } from '@commercetools/platform-sdk'
13
- import { getBaseResourceProperties } from '../helpers.js'
14
- import type { Writable } from '../types.js'
15
- import { AbstractResourceRepository, RepositoryContext } from './abstract.js'
16
- import { getReferenceFromResourceIdentifier } from './helpers.js'
15
+ } from "@commercetools/platform-sdk";
16
+ import { getBaseResourceProperties } from "../helpers";
17
+ import { AbstractStorage } from "../storage/abstract";
18
+ import type { Writable } from "../types";
19
+ import {
20
+ AbstractResourceRepository,
21
+ AbstractUpdateHandler,
22
+ RepositoryContext,
23
+ UpdateHandlerInterface,
24
+ } from "./abstract";
25
+ import { getReferenceFromResourceIdentifier } from "./helpers";
17
26
 
18
- export class StateRepository extends AbstractResourceRepository<'state'> {
19
- getTypeId() {
20
- return 'state' as const
27
+ export class StateRepository extends AbstractResourceRepository<"state"> {
28
+ constructor(storage: AbstractStorage) {
29
+ super("state", storage);
30
+ this.actions = new StateUpdateHandler(storage);
21
31
  }
22
32
 
23
33
  create(context: RepositoryContext, draft: StateDraft): State {
@@ -27,70 +37,105 @@ export class StateRepository extends AbstractResourceRepository<'state'> {
27
37
  builtIn: false,
28
38
  initial: draft.initial || false,
29
39
  transitions: (draft.transitions || []).map((t) =>
30
- getReferenceFromResourceIdentifier(t, context.projectKey, this._storage)
40
+ getReferenceFromResourceIdentifier(
41
+ t,
42
+ context.projectKey,
43
+ this._storage,
44
+ ),
31
45
  ),
46
+ };
47
+
48
+ return this.saveNew(context, resource);
49
+ }
50
+ }
51
+
52
+ class StateUpdateHandler
53
+ extends AbstractUpdateHandler
54
+ implements UpdateHandlerInterface<State, StateUpdateAction>
55
+ {
56
+ addRoles(
57
+ context: RepositoryContext,
58
+ resource: Writable<State>,
59
+ action: StateAddRolesAction,
60
+ ) {
61
+ if (!resource.roles) {
62
+ resource.roles = [];
63
+ }
64
+ for (const role of action.roles) {
65
+ if (!resource.roles.includes(role)) {
66
+ resource.roles.push(role);
67
+ }
32
68
  }
69
+ }
70
+
71
+ changeInitial(
72
+ context: RepositoryContext,
73
+ resource: Writable<State>,
74
+ { initial }: StateChangeInitialAction,
75
+ ) {
76
+ resource.initial = initial;
77
+ }
78
+
79
+ changeKey(
80
+ context: RepositoryContext,
81
+ resource: Writable<State>,
82
+ { key }: StateChangeKeyAction,
83
+ ) {
84
+ resource.key = key;
85
+ }
86
+
87
+ changeType(
88
+ context: RepositoryContext,
89
+ resource: Writable<State>,
90
+ action: StateChangeTypeAction,
91
+ ) {
92
+ resource.type = action.type;
93
+ }
94
+
95
+ removeRoles(
96
+ context: RepositoryContext,
97
+ resource: Writable<State>,
98
+ action: StateRemoveRolesAction,
99
+ ) {
100
+ resource.roles = resource.roles?.filter(
101
+ (role) => !action.roles.includes(role),
102
+ );
103
+ }
104
+
105
+ setDescription(
106
+ context: RepositoryContext,
107
+ resource: Writable<State>,
108
+ { description }: StateSetDescriptionAction,
109
+ ) {
110
+ resource.description = description;
111
+ }
112
+
113
+ setName(
114
+ context: RepositoryContext,
115
+ resource: Writable<State>,
116
+ { name }: StateSetNameAction,
117
+ ) {
118
+ resource.name = name;
119
+ }
33
120
 
34
- this.saveNew(context, resource)
35
- return resource
121
+ setRoles(
122
+ context: RepositoryContext,
123
+ resource: Writable<State>,
124
+ { roles }: StateSetRolesAction,
125
+ ) {
126
+ resource.roles = roles;
36
127
  }
37
128
 
38
- actions: Partial<
39
- Record<
40
- StateUpdateAction['action'],
41
- (
42
- context: RepositoryContext,
43
- resource: Writable<State>,
44
- action: any
45
- ) => void
46
- >
47
- > = {
48
- changeKey: (
49
- context: RepositoryContext,
50
- resource: Writable<State>,
51
- { key }: StateChangeKeyAction
52
- ) => {
53
- resource.key = key
54
- },
55
- changeInitial: (
56
- context: RepositoryContext,
57
- resource: Writable<State>,
58
- { initial }: StateChangeInitialAction
59
- ) => {
60
- resource.initial = initial
61
- },
62
- setDescription: (
63
- context: RepositoryContext,
64
- resource: Writable<State>,
65
- { description }: StateSetDescriptionAction
66
- ) => {
67
- resource.description = description
68
- },
69
- setName: (
70
- context: RepositoryContext,
71
- resource: Writable<State>,
72
- { name }: StateSetNameAction
73
- ) => {
74
- resource.name = name
75
- },
76
- setRoles: (
77
- context: RepositoryContext,
78
- resource: Writable<State>,
79
- { roles }: StateSetRolesAction
80
- ) => {
81
- resource.roles = roles
82
- },
83
- setTransitions: (
84
- context: RepositoryContext,
85
- resource: Writable<State>,
86
- { transitions }: StateSetTransitionsAction
87
- ) => {
88
- resource.transitions = transitions?.map(
89
- (resourceId): StateReference => ({
90
- id: resourceId.id || '',
91
- typeId: 'state',
92
- })
93
- )
94
- },
129
+ setTransitions(
130
+ context: RepositoryContext,
131
+ resource: Writable<State>,
132
+ { transitions }: StateSetTransitionsAction,
133
+ ) {
134
+ resource.transitions = transitions?.map(
135
+ (resourceId): StateReference => ({
136
+ id: resourceId.id || "",
137
+ typeId: "state",
138
+ }),
139
+ );
95
140
  }
96
141
  }
@@ -10,18 +10,25 @@ import type {
10
10
  StoreSetLanguagesAction,
11
11
  StoreSetNameAction,
12
12
  StoreUpdateAction,
13
- } from '@commercetools/platform-sdk'
14
- import { getBaseResourceProperties } from '../helpers.js'
15
- import type { Writable } from '../types.js'
16
- import { AbstractResourceRepository, RepositoryContext } from './abstract.js'
13
+ } from "@commercetools/platform-sdk";
14
+ import { getBaseResourceProperties } from "../helpers";
15
+ import { AbstractStorage } from "../storage/abstract";
16
+ import type { Writable } from "../types";
17
+ import {
18
+ AbstractResourceRepository,
19
+ AbstractUpdateHandler,
20
+ RepositoryContext,
21
+ UpdateHandlerInterface,
22
+ } from "./abstract";
17
23
  import {
18
24
  createCustomFields,
19
25
  getReferenceFromResourceIdentifier,
20
- } from './helpers.js'
26
+ } from "./helpers";
21
27
 
22
- export class StoreRepository extends AbstractResourceRepository<'store'> {
23
- getTypeId() {
24
- return 'store' as const
28
+ export class StoreRepository extends AbstractResourceRepository<"store"> {
29
+ constructor(storage: AbstractStorage) {
30
+ super("store", storage);
31
+ this.actions = new StoreUpdateHandler(this._storage);
25
32
  }
26
33
 
27
34
  create(context: RepositoryContext, draft: StoreDraft): Store {
@@ -31,106 +38,111 @@ export class StoreRepository extends AbstractResourceRepository<'store'> {
31
38
  name: draft.name,
32
39
  languages: draft.languages ?? [],
33
40
  countries: draft.countries ?? [],
34
- distributionChannels: this.transformChannels(
41
+ distributionChannels: transformChannels(
42
+ context,
43
+ this._storage,
44
+ draft.distributionChannels,
45
+ ),
46
+ supplyChannels: transformChannels(
35
47
  context,
36
- draft.distributionChannels
48
+ this._storage,
49
+ draft.supplyChannels,
37
50
  ),
38
- supplyChannels: this.transformChannels(context, draft.supplyChannels),
39
51
  productSelections: [],
40
52
  custom: createCustomFields(
41
53
  draft.custom,
42
54
  context.projectKey,
43
- this._storage
55
+ this._storage,
44
56
  ),
45
- }
46
- this.saveNew(context, resource)
47
- return resource
57
+ };
58
+ return this.saveNew(context, resource);
59
+ }
60
+ }
61
+
62
+ const transformChannels = (
63
+ context: RepositoryContext,
64
+ storage: AbstractStorage,
65
+ channels?: ChannelResourceIdentifier[],
66
+ ) => {
67
+ if (!channels) return [];
68
+
69
+ return channels.map((ref) =>
70
+ getReferenceFromResourceIdentifier<ChannelReference>(
71
+ ref,
72
+ context.projectKey,
73
+ storage,
74
+ ),
75
+ );
76
+ };
77
+
78
+ class StoreUpdateHandler
79
+ extends AbstractUpdateHandler
80
+ implements Partial<UpdateHandlerInterface<Store, StoreUpdateAction>>
81
+ {
82
+ setCountries(
83
+ context: RepositoryContext,
84
+ resource: Writable<Store>,
85
+ { countries }: StoreSetCountriesAction,
86
+ ) {
87
+ resource.countries = countries ?? [];
48
88
  }
49
89
 
50
- private transformChannels(
90
+ setCustomField(
51
91
  context: RepositoryContext,
52
- channels?: ChannelResourceIdentifier[]
92
+ resource: Writable<Store>,
93
+ { name, value }: StoreSetCustomFieldAction,
53
94
  ) {
54
- if (!channels) return []
95
+ if (!resource.custom) {
96
+ return;
97
+ }
98
+ if (value === null) {
99
+ delete resource.custom.fields[name];
100
+ } else {
101
+ resource.custom.fields[name] = value;
102
+ }
103
+ }
55
104
 
56
- return channels.map((ref) =>
57
- getReferenceFromResourceIdentifier<ChannelReference>(
58
- ref,
105
+ setCustomType(
106
+ context: RepositoryContext,
107
+ resource: Writable<Store>,
108
+ { type, fields }: StoreSetCustomTypeAction,
109
+ ) {
110
+ if (type) {
111
+ resource.custom = createCustomFields(
112
+ { type, fields },
59
113
  context.projectKey,
60
- this._storage
61
- )
62
- )
114
+ this._storage,
115
+ );
116
+ } else {
117
+ resource.custom = undefined;
118
+ }
63
119
  }
64
120
 
65
- actions: Partial<
66
- Record<
67
- StoreUpdateAction['action'],
68
- (
69
- context: RepositoryContext,
70
- resource: Writable<Store>,
71
- action: any
72
- ) => void
73
- >
74
- > = {
75
- setName: (
76
- context: RepositoryContext,
77
- resource: Writable<Store>,
78
- { name }: StoreSetNameAction
79
- ) => {
80
- resource.name = name
81
- },
82
- setDistributionChannels: (
83
- context: RepositoryContext,
84
- resource: Writable<Store>,
85
- { distributionChannels }: StoreSetDistributionChannelsAction
86
- ) => {
87
- resource.distributionChannels = this.transformChannels(
88
- context,
89
- distributionChannels
90
- )
91
- },
92
- setLanguages: (
93
- context: RepositoryContext,
94
- resource: Writable<Store>,
95
- { languages }: StoreSetLanguagesAction
96
- ) => {
97
- resource.languages = languages ?? []
98
- },
99
- setCustomType: (
100
- context: RepositoryContext,
101
- resource: Writable<Store>,
102
- { type, fields }: StoreSetCustomTypeAction
103
- ) => {
104
- if (type) {
105
- resource.custom = createCustomFields(
106
- { type, fields },
107
- context.projectKey,
108
- this._storage
109
- )
110
- } else {
111
- resource.custom = undefined
112
- }
113
- },
114
- setCustomField: (
115
- context: RepositoryContext,
116
- resource: Writable<Store>,
117
- { name, value }: StoreSetCustomFieldAction
118
- ) => {
119
- if (!resource.custom) {
120
- return
121
- }
122
- if (value === null) {
123
- delete resource.custom.fields[name]
124
- } else {
125
- resource.custom.fields[name] = value
126
- }
127
- },
128
- setCountries: (
129
- context: RepositoryContext,
130
- resource: Writable<Store>,
131
- { countries }: StoreSetCountriesAction
132
- ) => {
133
- resource.countries = countries ?? []
134
- },
121
+ setDistributionChannels(
122
+ context: RepositoryContext,
123
+ resource: Writable<Store>,
124
+ { distributionChannels }: StoreSetDistributionChannelsAction,
125
+ ) {
126
+ resource.distributionChannels = transformChannels(
127
+ context,
128
+ this._storage,
129
+ distributionChannels,
130
+ );
131
+ }
132
+
133
+ setLanguages(
134
+ context: RepositoryContext,
135
+ resource: Writable<Store>,
136
+ { languages }: StoreSetLanguagesAction,
137
+ ) {
138
+ resource.languages = languages ?? [];
139
+ }
140
+
141
+ setName(
142
+ context: RepositoryContext,
143
+ resource: Writable<Store>,
144
+ { name }: StoreSetNameAction,
145
+ ) {
146
+ resource.name = name;
135
147
  }
136
148
  }
@@ -2,33 +2,44 @@ import type {
2
2
  InvalidInputError,
3
3
  Subscription,
4
4
  SubscriptionDraft,
5
- } from '@commercetools/platform-sdk'
6
- import { CommercetoolsError } from '../exceptions.js'
7
- import { getBaseResourceProperties } from '../helpers.js'
8
- import { AbstractResourceRepository, RepositoryContext } from './abstract.js'
5
+ SubscriptionSetKeyAction,
6
+ SubscriptionUpdateAction,
7
+ } from "@commercetools/platform-sdk";
8
+ import { CommercetoolsError } from "~src/exceptions";
9
+ import { getBaseResourceProperties } from "../helpers";
10
+ import { AbstractStorage } from "../storage/abstract";
11
+ import { Writable } from "../types";
12
+ import {
13
+ AbstractResourceRepository,
14
+ AbstractUpdateHandler,
15
+ RepositoryContext,
16
+ UpdateHandlerInterface,
17
+ } from "./abstract";
9
18
 
10
- export class SubscriptionRepository extends AbstractResourceRepository<'subscription'> {
11
- getTypeId() {
12
- return 'subscription' as const
19
+ export class SubscriptionRepository extends AbstractResourceRepository<"subscription"> {
20
+ constructor(storage: AbstractStorage) {
21
+ super("subscription", storage);
22
+ this.actions = new SubscriptionUpdateHandler(storage);
13
23
  }
24
+
14
25
  create(context: RepositoryContext, draft: SubscriptionDraft): Subscription {
15
26
  // TODO: We could actually test this here by using the aws sdk. For now
16
27
  // hardcode a failed check when account id is 0000000000
17
- if (draft.destination.type === 'SQS') {
18
- const queueURL = new URL(draft.destination.queueUrl)
19
- const accountId = queueURL.pathname.split('/')[1]
20
- if (accountId === '0000000000') {
21
- const dest = draft.destination
28
+ if (draft.destination.type === "SQS") {
29
+ const queueURL = new URL(draft.destination.queueUrl);
30
+ const accountId = queueURL.pathname.split("/")[1];
31
+ if (accountId === "0000000000") {
32
+ const dest = draft.destination;
22
33
  throw new CommercetoolsError<InvalidInputError>(
23
34
  {
24
- code: 'InvalidInput',
35
+ code: "InvalidInput",
25
36
  message:
26
- 'A test message could not be delivered to this destination: ' +
37
+ "A test message could not be delivered to this destination: " +
27
38
  `SQS ${dest.queueUrl} in ${dest.region} for ${dest.accessKey}. ` +
28
- 'Please make sure your destination is correctly configured.',
39
+ "Please make sure your destination is correctly configured.",
29
40
  },
30
- 400
31
- )
41
+ 400,
42
+ );
32
43
  }
33
44
  }
34
45
 
@@ -37,13 +48,26 @@ export class SubscriptionRepository extends AbstractResourceRepository<'subscrip
37
48
  changes: draft.changes || [],
38
49
  destination: draft.destination,
39
50
  format: draft.format || {
40
- type: 'Platform',
51
+ type: "Platform",
41
52
  },
42
53
  key: draft.key,
43
54
  messages: draft.messages || [],
44
- status: 'Healthy',
45
- }
46
- this.saveNew(context, resource)
47
- return resource
55
+ status: "Healthy",
56
+ };
57
+ return this.saveNew(context, resource);
58
+ }
59
+ }
60
+
61
+ class SubscriptionUpdateHandler
62
+ extends AbstractUpdateHandler
63
+ implements
64
+ Partial<UpdateHandlerInterface<Subscription, SubscriptionUpdateAction>>
65
+ {
66
+ setKey(
67
+ _context: RepositoryContext,
68
+ resource: Writable<Subscription>,
69
+ { key }: SubscriptionSetKeyAction,
70
+ ) {
71
+ resource.key = key;
48
72
  }
49
73
  }
@@ -0,0 +1,94 @@
1
+ import type {
2
+ TaxCategory,
3
+ TaxCategoryAddTaxRateAction,
4
+ TaxCategoryChangeNameAction,
5
+ TaxCategoryRemoveTaxRateAction,
6
+ TaxCategoryReplaceTaxRateAction,
7
+ TaxCategorySetDescriptionAction,
8
+ TaxCategorySetKeyAction,
9
+ TaxCategoryUpdateAction,
10
+ } from "@commercetools/platform-sdk";
11
+ import type { Writable } from "~src/types";
12
+ import { AbstractUpdateHandler, RepositoryContext } from "../abstract";
13
+ import { taxRateFromTaxRateDraft } from "./helpers";
14
+
15
+ type TaxCategoryUpdateHandlerMethod<T> = (
16
+ context: RepositoryContext,
17
+ resource: Writable<TaxCategory>,
18
+ action: T,
19
+ ) => void;
20
+
21
+ type TaxCategoryUpdateActions = {
22
+ [P in TaxCategoryUpdateAction as P["action"]]: TaxCategoryUpdateHandlerMethod<P>;
23
+ };
24
+
25
+ export class TaxCategoryUpdateHandler
26
+ extends AbstractUpdateHandler
27
+ implements TaxCategoryUpdateActions
28
+ {
29
+ addTaxRate(
30
+ context: RepositoryContext,
31
+ resource: Writable<TaxCategory>,
32
+ { taxRate }: TaxCategoryAddTaxRateAction,
33
+ ) {
34
+ if (resource.rates === undefined) {
35
+ resource.rates = [];
36
+ }
37
+ resource.rates.push(taxRateFromTaxRateDraft(taxRate));
38
+ }
39
+
40
+ changeName(
41
+ context: RepositoryContext,
42
+ resource: Writable<TaxCategory>,
43
+ { name }: TaxCategoryChangeNameAction,
44
+ ) {
45
+ resource.name = name;
46
+ }
47
+
48
+ removeTaxRate(
49
+ context: RepositoryContext,
50
+ resource: Writable<TaxCategory>,
51
+ { taxRateId }: TaxCategoryRemoveTaxRateAction,
52
+ ) {
53
+ if (resource.rates === undefined) {
54
+ resource.rates = [];
55
+ }
56
+ resource.rates = resource.rates.filter(
57
+ (taxRate) => taxRate.id !== taxRateId,
58
+ );
59
+ }
60
+
61
+ replaceTaxRate(
62
+ context: RepositoryContext,
63
+ resource: Writable<TaxCategory>,
64
+ { taxRateId, taxRate }: TaxCategoryReplaceTaxRateAction,
65
+ ) {
66
+ if (resource.rates === undefined) {
67
+ resource.rates = [];
68
+ }
69
+
70
+ const taxRateObj = taxRateFromTaxRateDraft(taxRate);
71
+ for (let i = 0; i < resource.rates.length; i++) {
72
+ const rate = resource.rates[i];
73
+ if (rate.id === taxRateId) {
74
+ resource.rates[i] = taxRateObj;
75
+ }
76
+ }
77
+ }
78
+
79
+ setDescription(
80
+ context: RepositoryContext,
81
+ resource: Writable<TaxCategory>,
82
+ { description }: TaxCategorySetDescriptionAction,
83
+ ) {
84
+ resource.description = description;
85
+ }
86
+
87
+ setKey(
88
+ context: RepositoryContext,
89
+ resource: Writable<TaxCategory>,
90
+ { key }: TaxCategorySetKeyAction,
91
+ ) {
92
+ resource.key = key;
93
+ }
94
+ }
@@ -0,0 +1,8 @@
1
+ import { TaxRate, TaxRateDraft } from "@commercetools/platform-sdk";
2
+ import { v4 as uuidv4 } from "uuid";
3
+
4
+ export const taxRateFromTaxRateDraft = (draft: TaxRateDraft): TaxRate => ({
5
+ ...draft,
6
+ id: uuidv4(),
7
+ amount: draft.amount || 0,
8
+ });
@@ -0,0 +1,25 @@
1
+ import type {
2
+ TaxCategory,
3
+ TaxCategoryDraft,
4
+ } from "@commercetools/platform-sdk";
5
+ import { getBaseResourceProperties } from "~src/helpers";
6
+ import { AbstractStorage } from "~src/storage/abstract";
7
+ import { AbstractResourceRepository, RepositoryContext } from "../abstract";
8
+ import { TaxCategoryUpdateHandler } from "./actions";
9
+ import { taxRateFromTaxRateDraft } from "./helpers";
10
+
11
+ export class TaxCategoryRepository extends AbstractResourceRepository<"tax-category"> {
12
+ constructor(storage: AbstractStorage) {
13
+ super("tax-category", storage);
14
+ this.actions = new TaxCategoryUpdateHandler(this._storage);
15
+ }
16
+
17
+ create(context: RepositoryContext, draft: TaxCategoryDraft): TaxCategory {
18
+ const resource: TaxCategory = {
19
+ ...getBaseResourceProperties(),
20
+ ...draft,
21
+ rates: draft.rates?.map(taxRateFromTaxRateDraft) || [],
22
+ };
23
+ return this.saveNew(context, resource);
24
+ }
25
+ }